xref: /illumos-gate/usr/src/lib/libc/port/inet/inet_ntoa.c (revision 7c478bd9)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
28 /*
29  * Portions of this source code were derived from Berkeley
30  * 4.3 BSD under license from the Regents of the University of
31  * California.
32  */
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 /*
37  * Convert network-format internet address
38  * to base 256 d.d.d.d representation.
39  *
40  * Reentrant interface
41  */
42 
43 #include "mt.h"
44 #include "rpc_mt.h"
45 #include <errno.h>
46 #include <sys/types.h>
47 #include <rpc/trace.h>
48 #include <ctype.h>
49 #include <netinet/in.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 
53 
54 char *
55 inet_ntoa_r(in, b)
56 	struct in_addr in;
57 	char	b[];	/* Assumed >= 18 bytes */
58 {
59 	char	*p;
60 
61 	p = (char *)&in;
62 #define	UC(b)	(((int)b)&0xff)
63 	sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
64 	return (b);
65 }
66 
67 char *
68 inet_ntoa(in)
69 	struct in_addr in;
70 {
71 	char *b;
72 	static char b_main[18];
73 	static pthread_key_t ntoa_key;
74 
75 	if (thr_main())
76 		b = b_main;
77 	else if ((b = thr_get_storage(&ntoa_key, 18, free)) == NULL)
78 		b = b_main;
79 
80 	return (inet_ntoa_r(in, b));
81 }
82 
83 #pragma weak inet_aton = _inet_aton
84 /*
85  * Check whether "cp" is a valid ascii representation
86  * of an Internet address and convert to a binary address.
87  * Returns 1 if the address is valid, 0 if not.
88  * This replaces inet_addr, the return value from which
89  * cannot distinguish between failure and a local broadcast address.
90  */
91 int
92 _inet_aton(char *cp, struct in_addr *addr)
93 {
94 	uint32_t val;
95 	int base, n;
96 	char c;
97 	unsigned int parts[4];
98 	register unsigned int *pp = parts;
99 
100 	trace1(TR_inet_aton, 0);
101 
102 	c = *cp;
103 	for (;;) {
104 		/*
105 		 * Collect number up to ``.''.
106 		 * Values are specified as for C:
107 		 * 0x=hex, 0=octal, isdigit=decimal.
108 		 */
109 		if (!isdigit(c))
110 			return (0);
111 		val = 0; base = 10;
112 		if (c == '0') {
113 			c = *++cp;
114 			if (c == 'x' || c == 'X')
115 				base = 16, c = *++cp;
116 			else
117 				base = 8;
118 		}
119 		for (;;) {
120 			if (isascii(c) && isdigit(c)) {
121 				val = (val * base) + (c - '0');
122 				c = *++cp;
123 			} else if (base == 16 && isascii(c) && isxdigit(c)) {
124 				val = (val << 4) |
125 					(c + 10 - (islower(c) ? 'a' : 'A'));
126 				c = *++cp;
127 			} else
128 				break;
129 		}
130 		if (c == '.') {
131 			/*
132 			 * Internet format:
133 			 *	a.b.c.d
134 			 *	a.b.c	(with c treated as 16 bits)
135 			 *	a.b	(with b treated as 24 bits)
136 			 */
137 			if (pp >= parts + 3)
138 				return (0);
139 			*pp++ = val;
140 			c = *++cp;
141 		} else
142 			break;
143 	}
144 	/*
145 	 * Check for trailing characters.
146 	 */
147 	if (c != '\0' && (!isascii(c) || !isspace(c)))
148 		return (0);
149 	/*
150 	 * Concoct the address according to
151 	 * the number of parts specified.
152 	 */
153 	n = pp - parts + 1;
154 	switch (n) {
155 
156 	case 0:
157 		return (0);		/* initial nondigit */
158 
159 	case 1:				/* a -- 32 bits */
160 		break;
161 
162 	case 2:				/* a.b -- 8.24 bits */
163 		if ((val > 0xffffff) || (parts[0] > 0xff))
164 			return (0);
165 		val |= parts[0] << 24;
166 		break;
167 
168 	case 3:				/* a.b.c -- 8.8.16 bits */
169 		if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
170 			return (0);
171 		val |= (parts[0] << 24) | (parts[1] << 16);
172 		break;
173 
174 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
175 		if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) ||
176 		    (parts[2] > 0xff))
177 			return (0);
178 		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
179 		break;
180 	}
181 	if (addr)
182 		addr->s_addr = htonl(val);
183 	trace1(TR_inet_aton, 1);
184 	return (1);
185 }
186 
187 /*
188  * Internet address interpretation routine.
189  * All the network library routines call this
190  * routine to interpret entries in the data bases
191  * which are expected to be an address.
192  * The value returned is in network order.
193  */
194 in_addr_t
195 inet_addr(char *cp)
196 {
197 	struct in_addr val;
198 
199 	if (_inet_aton(cp, &val))
200 		return (val.s_addr);
201 	return (INADDR_NONE);
202 }
203 
204 /*
205  * Return the network number from an internet
206  * address; handles class a/b/c network #'s.
207  */
208 int
209 inet_netof(in)
210 	struct in_addr in;
211 {
212 	uint32_t i = ntohl(in.s_addr);
213 
214 	trace1(TR_inet_netof, 0);
215 	if (IN_CLASSA(i)) {
216 		trace1(TR_inet_netof, 1);
217 		return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
218 	} else if (IN_CLASSB(i)) {
219 		trace1(TR_inet_netof, 1);
220 		return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
221 	} else {
222 		trace1(TR_inet_netof, 1);
223 		return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
224 	}
225 }
226