xref: /onnv-gate/usr/src/lib/libbc/libc/inet/inet_addr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1987 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 	  /* from UCB 4.5 82/11/14 */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <ctype.h>
32*0Sstevel@tonic-gate #include <netinet/in.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate  * Internet address interpretation routine.
36*0Sstevel@tonic-gate  * All the network library routines call this
37*0Sstevel@tonic-gate  * routine to interpret entries in the data bases
38*0Sstevel@tonic-gate  * which are expected to be an address.
39*0Sstevel@tonic-gate  * The value returned is in network order.
40*0Sstevel@tonic-gate  */
41*0Sstevel@tonic-gate u_long
inet_addr(cp)42*0Sstevel@tonic-gate inet_addr(cp)
43*0Sstevel@tonic-gate 	register char *cp;
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate 	register u_long val, base, n;
46*0Sstevel@tonic-gate 	register char c;
47*0Sstevel@tonic-gate 	u_long parts[4], *pp = parts;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate again:
50*0Sstevel@tonic-gate 	/*
51*0Sstevel@tonic-gate 	 * Collect number up to ``.''.
52*0Sstevel@tonic-gate 	 * Values are specified as for C:
53*0Sstevel@tonic-gate 	 * 0x=hex, 0=octal, other=decimal.
54*0Sstevel@tonic-gate 	 */
55*0Sstevel@tonic-gate 	val = 0; base = 10;
56*0Sstevel@tonic-gate 	if (*cp == '0') {
57*0Sstevel@tonic-gate 		if (*++cp == 'x' || *cp == 'X')
58*0Sstevel@tonic-gate 			base = 16, cp++;
59*0Sstevel@tonic-gate 		else
60*0Sstevel@tonic-gate 			base = 8;
61*0Sstevel@tonic-gate 	}
62*0Sstevel@tonic-gate 	while (c = *cp) {
63*0Sstevel@tonic-gate 		if (isdigit(c)) {
64*0Sstevel@tonic-gate 			if ((c - '0') >= base)
65*0Sstevel@tonic-gate 			    break;
66*0Sstevel@tonic-gate 			val = (val * base) + (c - '0');
67*0Sstevel@tonic-gate 			cp++;
68*0Sstevel@tonic-gate 			continue;
69*0Sstevel@tonic-gate 		}
70*0Sstevel@tonic-gate 		if (base == 16 && isxdigit(c)) {
71*0Sstevel@tonic-gate 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
72*0Sstevel@tonic-gate 			cp++;
73*0Sstevel@tonic-gate 			continue;
74*0Sstevel@tonic-gate 		}
75*0Sstevel@tonic-gate 		break;
76*0Sstevel@tonic-gate 	}
77*0Sstevel@tonic-gate 	if (*cp == '.') {
78*0Sstevel@tonic-gate 		/*
79*0Sstevel@tonic-gate 		 * Internet format:
80*0Sstevel@tonic-gate 		 *	a.b.c.d
81*0Sstevel@tonic-gate 		 *	a.b.c	(with c treated as 16-bits)
82*0Sstevel@tonic-gate 		 *	a.b	(with b treated as 24 bits)
83*0Sstevel@tonic-gate 		 */
84*0Sstevel@tonic-gate 		if (pp >= parts + 4)
85*0Sstevel@tonic-gate 			return (-1);
86*0Sstevel@tonic-gate 		*pp++ = val, cp++;
87*0Sstevel@tonic-gate 		goto again;
88*0Sstevel@tonic-gate 	}
89*0Sstevel@tonic-gate 	/*
90*0Sstevel@tonic-gate 	 * Check for trailing characters.
91*0Sstevel@tonic-gate 	 */
92*0Sstevel@tonic-gate 	if (*cp && !isspace(*cp))
93*0Sstevel@tonic-gate 		return (-1);
94*0Sstevel@tonic-gate 	*pp++ = val;
95*0Sstevel@tonic-gate 	/*
96*0Sstevel@tonic-gate 	 * Concoct the address according to
97*0Sstevel@tonic-gate 	 * the number of parts specified.
98*0Sstevel@tonic-gate 	 */
99*0Sstevel@tonic-gate 	n = pp - parts;
100*0Sstevel@tonic-gate 	switch (n) {
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	case 1:				/* a -- 32 bits */
103*0Sstevel@tonic-gate 		val = parts[0];
104*0Sstevel@tonic-gate 		break;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	case 2:				/* a.b -- 8.24 bits */
107*0Sstevel@tonic-gate 		val = (parts[0] << 24) | (parts[1] & 0xffffff);
108*0Sstevel@tonic-gate 		break;
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	case 3:				/* a.b.c -- 8.8.16 bits */
111*0Sstevel@tonic-gate 		val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
112*0Sstevel@tonic-gate 			(parts[2] & 0xffff);
113*0Sstevel@tonic-gate 		break;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
116*0Sstevel@tonic-gate 		val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
117*0Sstevel@tonic-gate 		      ((parts[2] & 0xff) << 8) | (parts[3] & 0xff);
118*0Sstevel@tonic-gate 		break;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	default:
121*0Sstevel@tonic-gate 		return (-1);
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate 	val = htonl(val);
124*0Sstevel@tonic-gate 	return (val);
125*0Sstevel@tonic-gate }
126