xref: /csrg-svn/lib/libc/net/inet_network.c (revision 46604)
121370Sdist /*
221370Sdist  * Copyright (c) 1983 Regents of the University of California.
333676Sbostic  * All rights reserved.
433676Sbostic  *
542626Sbostic  * %sccs.include.redist.c%
621370Sdist  */
78362Ssam 
826615Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*46604Sbostic static char sccsid[] = "@(#)inet_network.c	5.7 (Berkeley) 02/24/91";
1033676Sbostic #endif /* LIBC_SCCS and not lint */
1121370Sdist 
128362Ssam #include <sys/types.h>
1332315Sbostic #include <netinet/in.h>
14*46604Sbostic #include <arpa/inet.h>
158362Ssam 
168362Ssam /*
178362Ssam  * Internet network address interpretation routine.
188362Ssam  * The library routines call this routine to interpret
198362Ssam  * network numbers.
208362Ssam  */
218362Ssam u_long
228362Ssam inet_network(cp)
23*46604Sbostic 	register const char *cp;
248362Ssam {
258362Ssam 	register u_long val, base, n;
268362Ssam 	register char c;
278362Ssam 	u_long parts[4], *pp = parts;
288363Ssam 	register int i;
298362Ssam 
308362Ssam again:
318362Ssam 	val = 0; base = 10;
328362Ssam 	if (*cp == '0')
338362Ssam 		base = 8, cp++;
348362Ssam 	if (*cp == 'x' || *cp == 'X')
358362Ssam 		base = 16, cp++;
368362Ssam 	while (c = *cp) {
378362Ssam 		if (isdigit(c)) {
388362Ssam 			val = (val * base) + (c - '0');
398362Ssam 			cp++;
408362Ssam 			continue;
418362Ssam 		}
428362Ssam 		if (base == 16 && isxdigit(c)) {
438362Ssam 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
448362Ssam 			cp++;
458362Ssam 			continue;
468362Ssam 		}
478362Ssam 		break;
488362Ssam 	}
498362Ssam 	if (*cp == '.') {
508362Ssam 		if (pp >= parts + 4)
5132315Sbostic 			return (INADDR_NONE);
528362Ssam 		*pp++ = val, cp++;
538362Ssam 		goto again;
548362Ssam 	}
558362Ssam 	if (*cp && !isspace(*cp))
5632315Sbostic 		return (INADDR_NONE);
578362Ssam 	*pp++ = val;
588362Ssam 	n = pp - parts;
598362Ssam 	if (n > 4)
6032315Sbostic 		return (INADDR_NONE);
618362Ssam 	for (val = 0, i = 0; i < n; i++) {
628362Ssam 		val <<= 8;
638362Ssam 		val |= parts[i] & 0xff;
648362Ssam 	}
658362Ssam 	return (val);
668362Ssam }
67