xref: /onnv-gate/usr/src/lib/libbc/libc/inet/inet_network.c (revision 6389:7ed9dd96f62b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6389Sblu  * Common Development and Distribution License (the "License").
6*6389Sblu  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6389Sblu 
22*6389Sblu /*
23*6389Sblu  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6389Sblu  * Use is subject to license terms.
25*6389Sblu  */
26*6389Sblu 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 	 /* from UCB 4.2 82/10/07 */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * Internet network address interpretation routine.
350Sstevel@tonic-gate  * The library routines call this routine to interpret
360Sstevel@tonic-gate  * network numbers.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate u_long
inet_network(cp)390Sstevel@tonic-gate inet_network(cp)
400Sstevel@tonic-gate 	register char *cp;
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	register u_long val, base, n;
430Sstevel@tonic-gate 	register char c;
440Sstevel@tonic-gate 	u_long parts[4], *pp = parts;
450Sstevel@tonic-gate 	register int i;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate again:
480Sstevel@tonic-gate 	val = 0; base = 10;
490Sstevel@tonic-gate 	if (*cp == '0') {
500Sstevel@tonic-gate 		if (*++cp == 'x' || *cp == 'X')
510Sstevel@tonic-gate 			base = 16, cp++;
520Sstevel@tonic-gate 		else
530Sstevel@tonic-gate 			base = 8;
540Sstevel@tonic-gate 	}
55*6389Sblu 	while ((c = *cp) != '\0') {
560Sstevel@tonic-gate 		if (isdigit(c)) {
570Sstevel@tonic-gate 			if ((c - '0') >= base)
580Sstevel@tonic-gate 			    break;
590Sstevel@tonic-gate 			val = (val * base) + (c - '0');
600Sstevel@tonic-gate 			cp++;
610Sstevel@tonic-gate 			continue;
620Sstevel@tonic-gate 		}
630Sstevel@tonic-gate 		if (base == 16 && isxdigit(c)) {
640Sstevel@tonic-gate 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
650Sstevel@tonic-gate 			cp++;
660Sstevel@tonic-gate 			continue;
670Sstevel@tonic-gate 		}
680Sstevel@tonic-gate 		break;
690Sstevel@tonic-gate 	}
70*6389Sblu 	if (pp >= parts + 4 || val > 0xff)
71*6389Sblu 		return (-1);
72*6389Sblu 	*pp++ = val;
730Sstevel@tonic-gate 	if (*cp == '.') {
74*6389Sblu 		cp++;
750Sstevel@tonic-gate 		goto again;
760Sstevel@tonic-gate 	}
77*6389Sblu 	if (*cp != '\0' && !isspace(*cp))
780Sstevel@tonic-gate 		return (-1);
790Sstevel@tonic-gate 	n = pp - parts;
800Sstevel@tonic-gate 	for (val = 0, i = 0; i < n; i++) {
810Sstevel@tonic-gate 		val <<= 8;
82*6389Sblu 		val |= parts[i];
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	return (val);
850Sstevel@tonic-gate }
86