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