xref: /csrg-svn/lib/libc/net/inet_network.c (revision 61150)
121370Sdist /*
2*61150Sbostic  * Copyright (c) 1983, 1993
3*61150Sbostic  *	The Regents of the University of California.  All rights reserved.
433676Sbostic  *
542626Sbostic  * %sccs.include.redist.c%
621370Sdist  */
78362Ssam 
826615Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*61150Sbostic static char sccsid[] = "@(#)inet_network.c	8.1 (Berkeley) 06/04/93";
1033676Sbostic #endif /* LIBC_SCCS and not lint */
1121370Sdist 
128362Ssam #include <sys/types.h>
1332315Sbostic #include <netinet/in.h>
1446604Sbostic #include <arpa/inet.h>
1546606Sbostic #include <ctype.h>
168362Ssam 
178362Ssam /*
188362Ssam  * Internet network address interpretation routine.
198362Ssam  * The library routines call this routine to interpret
208362Ssam  * network numbers.
218362Ssam  */
228362Ssam u_long
inet_network(cp)238362Ssam inet_network(cp)
2446604Sbostic 	register const char *cp;
258362Ssam {
268362Ssam 	register u_long val, base, n;
278362Ssam 	register char c;
288362Ssam 	u_long parts[4], *pp = parts;
298363Ssam 	register int i;
308362Ssam 
318362Ssam again:
328362Ssam 	val = 0; base = 10;
338362Ssam 	if (*cp == '0')
348362Ssam 		base = 8, cp++;
358362Ssam 	if (*cp == 'x' || *cp == 'X')
368362Ssam 		base = 16, cp++;
378362Ssam 	while (c = *cp) {
388362Ssam 		if (isdigit(c)) {
398362Ssam 			val = (val * base) + (c - '0');
408362Ssam 			cp++;
418362Ssam 			continue;
428362Ssam 		}
438362Ssam 		if (base == 16 && isxdigit(c)) {
448362Ssam 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
458362Ssam 			cp++;
468362Ssam 			continue;
478362Ssam 		}
488362Ssam 		break;
498362Ssam 	}
508362Ssam 	if (*cp == '.') {
518362Ssam 		if (pp >= parts + 4)
5232315Sbostic 			return (INADDR_NONE);
538362Ssam 		*pp++ = val, cp++;
548362Ssam 		goto again;
558362Ssam 	}
568362Ssam 	if (*cp && !isspace(*cp))
5732315Sbostic 		return (INADDR_NONE);
588362Ssam 	*pp++ = val;
598362Ssam 	n = pp - parts;
608362Ssam 	if (n > 4)
6132315Sbostic 		return (INADDR_NONE);
628362Ssam 	for (val = 0, i = 0; i < n; i++) {
638362Ssam 		val <<= 8;
648362Ssam 		val |= parts[i] & 0xff;
658362Ssam 	}
668362Ssam 	return (val);
678362Ssam }
68