xref: /csrg-svn/lib/libc/net/inet_network.c (revision 33676)
121370Sdist /*
221370Sdist  * Copyright (c) 1983 Regents of the University of California.
3*33676Sbostic  * All rights reserved.
4*33676Sbostic  *
5*33676Sbostic  * Redistribution and use in source and binary forms are permitted
6*33676Sbostic  * provided that this notice is preserved and that due credit is given
7*33676Sbostic  * to the University of California at Berkeley. The name of the University
8*33676Sbostic  * may not be used to endorse or promote products derived from this
9*33676Sbostic  * software without specific prior written permission. This software
10*33676Sbostic  * is provided ``as is'' without express or implied warranty.
1121370Sdist  */
128362Ssam 
1326615Sdonn #if defined(LIBC_SCCS) && !defined(lint)
14*33676Sbostic static char sccsid[] = "@(#)inet_network.c	5.4 (Berkeley) 03/07/88";
15*33676Sbostic #endif /* LIBC_SCCS and not lint */
1621370Sdist 
178362Ssam #include <sys/types.h>
1832315Sbostic #include <netinet/in.h>
198362Ssam #include <ctype.h>
208362Ssam 
218362Ssam /*
228362Ssam  * Internet network address interpretation routine.
238362Ssam  * The library routines call this routine to interpret
248362Ssam  * network numbers.
258362Ssam  */
268362Ssam u_long
278362Ssam inet_network(cp)
288362Ssam 	register char *cp;
298362Ssam {
308362Ssam 	register u_long val, base, n;
318362Ssam 	register char c;
328362Ssam 	u_long parts[4], *pp = parts;
338363Ssam 	register int i;
348362Ssam 
358362Ssam again:
368362Ssam 	val = 0; base = 10;
378362Ssam 	if (*cp == '0')
388362Ssam 		base = 8, cp++;
398362Ssam 	if (*cp == 'x' || *cp == 'X')
408362Ssam 		base = 16, cp++;
418362Ssam 	while (c = *cp) {
428362Ssam 		if (isdigit(c)) {
438362Ssam 			val = (val * base) + (c - '0');
448362Ssam 			cp++;
458362Ssam 			continue;
468362Ssam 		}
478362Ssam 		if (base == 16 && isxdigit(c)) {
488362Ssam 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
498362Ssam 			cp++;
508362Ssam 			continue;
518362Ssam 		}
528362Ssam 		break;
538362Ssam 	}
548362Ssam 	if (*cp == '.') {
558362Ssam 		if (pp >= parts + 4)
5632315Sbostic 			return (INADDR_NONE);
578362Ssam 		*pp++ = val, cp++;
588362Ssam 		goto again;
598362Ssam 	}
608362Ssam 	if (*cp && !isspace(*cp))
6132315Sbostic 		return (INADDR_NONE);
628362Ssam 	*pp++ = val;
638362Ssam 	n = pp - parts;
648362Ssam 	if (n > 4)
6532315Sbostic 		return (INADDR_NONE);
668362Ssam 	for (val = 0, i = 0; i < n; i++) {
678362Ssam 		val <<= 8;
688362Ssam 		val |= parts[i] & 0xff;
698362Ssam 	}
708362Ssam 	return (val);
718362Ssam }
72