xref: /csrg-svn/lib/libc/net/inet_network.c (revision 32315)
121370Sdist /*
221370Sdist  * Copyright (c) 1983 Regents of the University of California.
321370Sdist  * All rights reserved.  The Berkeley software License Agreement
421370Sdist  * specifies the terms and conditions for redistribution.
521370Sdist  */
68362Ssam 
726615Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*32315Sbostic static char sccsid[] = "@(#)inet_network.c	5.3 (Berkeley) 10/01/87";
926615Sdonn #endif LIBC_SCCS and not lint
1021370Sdist 
118362Ssam #include <sys/types.h>
12*32315Sbostic #include <netinet/in.h>
138362Ssam #include <ctype.h>
148362Ssam 
158362Ssam /*
168362Ssam  * Internet network address interpretation routine.
178362Ssam  * The library routines call this routine to interpret
188362Ssam  * network numbers.
198362Ssam  */
208362Ssam u_long
218362Ssam inet_network(cp)
228362Ssam 	register char *cp;
238362Ssam {
248362Ssam 	register u_long val, base, n;
258362Ssam 	register char c;
268362Ssam 	u_long parts[4], *pp = parts;
278363Ssam 	register int i;
288362Ssam 
298362Ssam again:
308362Ssam 	val = 0; base = 10;
318362Ssam 	if (*cp == '0')
328362Ssam 		base = 8, cp++;
338362Ssam 	if (*cp == 'x' || *cp == 'X')
348362Ssam 		base = 16, cp++;
358362Ssam 	while (c = *cp) {
368362Ssam 		if (isdigit(c)) {
378362Ssam 			val = (val * base) + (c - '0');
388362Ssam 			cp++;
398362Ssam 			continue;
408362Ssam 		}
418362Ssam 		if (base == 16 && isxdigit(c)) {
428362Ssam 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
438362Ssam 			cp++;
448362Ssam 			continue;
458362Ssam 		}
468362Ssam 		break;
478362Ssam 	}
488362Ssam 	if (*cp == '.') {
498362Ssam 		if (pp >= parts + 4)
50*32315Sbostic 			return (INADDR_NONE);
518362Ssam 		*pp++ = val, cp++;
528362Ssam 		goto again;
538362Ssam 	}
548362Ssam 	if (*cp && !isspace(*cp))
55*32315Sbostic 		return (INADDR_NONE);
568362Ssam 	*pp++ = val;
578362Ssam 	n = pp - parts;
588362Ssam 	if (n > 4)
59*32315Sbostic 		return (INADDR_NONE);
608362Ssam 	for (val = 0, i = 0; i < n; i++) {
618362Ssam 		val <<= 8;
628362Ssam 		val |= parts[i] & 0xff;
638362Ssam 	}
648362Ssam 	return (val);
658362Ssam }
66