xref: /csrg-svn/lib/libc/net/inet_network.c (revision 21370)
1*21370Sdist /*
2*21370Sdist  * Copyright (c) 1983 Regents of the University of California.
3*21370Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21370Sdist  * specifies the terms and conditions for redistribution.
5*21370Sdist  */
68362Ssam 
7*21370Sdist #ifndef lint
8*21370Sdist static char sccsid[] = "@(#)inet_network.c	5.1 (Berkeley) 05/30/85";
9*21370Sdist #endif not lint
10*21370Sdist 
118362Ssam #include <sys/types.h>
128362Ssam #include <ctype.h>
138362Ssam 
148362Ssam /*
158362Ssam  * Internet network address interpretation routine.
168362Ssam  * The library routines call this routine to interpret
178362Ssam  * network numbers.
188362Ssam  */
198362Ssam u_long
208362Ssam inet_network(cp)
218362Ssam 	register char *cp;
228362Ssam {
238362Ssam 	register u_long val, base, n;
248362Ssam 	register char c;
258362Ssam 	u_long parts[4], *pp = parts;
268363Ssam 	register int i;
278362Ssam 
288362Ssam again:
298362Ssam 	val = 0; base = 10;
308362Ssam 	if (*cp == '0')
318362Ssam 		base = 8, cp++;
328362Ssam 	if (*cp == 'x' || *cp == 'X')
338362Ssam 		base = 16, cp++;
348362Ssam 	while (c = *cp) {
358362Ssam 		if (isdigit(c)) {
368362Ssam 			val = (val * base) + (c - '0');
378362Ssam 			cp++;
388362Ssam 			continue;
398362Ssam 		}
408362Ssam 		if (base == 16 && isxdigit(c)) {
418362Ssam 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
428362Ssam 			cp++;
438362Ssam 			continue;
448362Ssam 		}
458362Ssam 		break;
468362Ssam 	}
478362Ssam 	if (*cp == '.') {
488362Ssam 		if (pp >= parts + 4)
498362Ssam 			return (-1);
508362Ssam 		*pp++ = val, cp++;
518362Ssam 		goto again;
528362Ssam 	}
538362Ssam 	if (*cp && !isspace(*cp))
548362Ssam 		return (-1);
558362Ssam 	*pp++ = val;
568362Ssam 	n = pp - parts;
578362Ssam 	if (n > 4)
588362Ssam 		return (-1);
598362Ssam 	for (val = 0, i = 0; i < n; i++) {
608362Ssam 		val <<= 8;
618362Ssam 		val |= parts[i] & 0xff;
628362Ssam 	}
638362Ssam 	return (val);
648362Ssam }
65