xref: /titanic_44/usr/src/lib/libnsl/rpc/inet_ntoa.c (revision d0538f66491267879b7418b21ad78e3dcc2dcc83)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * Portions of this source code were derived from Berkeley
31  * 4.3 BSD under license from the Regents of the University of
32  * California.
33  */
34 
35 /*
36  * Copyright (c) 1983, 1990, 1993
37  *    The Regents of the University of California.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  * 	This product includes software developed by the University of
50  * 	California, Berkeley and its contributors.
51  * 4. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  */
67 
68 /*
69  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
70  *
71  * Permission to use, copy, modify, and distribute this software for any
72  * purpose with or without fee is hereby granted, provided that the above
73  * copyright notice and this permission notice appear in all copies, and that
74  * the name of Digital Equipment Corporation not be used in advertising or
75  * publicity pertaining to distribution of the document or software without
76  * specific, written prior permission.
77  *
78  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
79  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
80  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
81  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
82  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
83  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
84  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
85  * SOFTWARE.
86  */
87 
88 /*
89  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
90  *
91  * Permission to use, copy, modify, and distribute this software for any
92  * purpose with or without fee is hereby granted, provided that the above
93  * copyright notice and this permission notice appear in all copies.
94  *
95  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
96  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
97  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
98  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
99  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
100  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
101  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
102  * SOFTWARE.
103  */
104 
105 #pragma ident	"%Z%%M%	%I%	%E% SMI"
106 
107 /*
108  * Convert network-format internet address
109  * to base 256 d.d.d.d representation.
110  *
111  * Reentrant interface
112  */
113 
114 #pragma weak inet_aton = _inet_aton
115 
116 #include "mt.h"
117 #include "rpc_mt.h"
118 #include <errno.h>
119 #include <sys/types.h>
120 #include <ctype.h>
121 #include <netinet/in.h>
122 #include <stdio.h>
123 #include <stdlib.h>
124 
125 
126 char *
127 inet_ntoa_r(in, b)
128 	struct in_addr in;
129 	char	b[];	/* Assumed >= 18 bytes */
130 {
131 	char	*p;
132 
133 	p = (char *)&in;
134 #define	UC(b)	(((int)b)&0xff)
135 	(void) sprintf(b, "%d.%d.%d.%d",
136 				UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
137 	return (b);
138 }
139 
140 char *
141 inet_ntoa(in)
142 	struct in_addr in;
143 {
144 	char *b;
145 	static char b_main[18];
146 	static pthread_key_t ntoa_key = PTHREAD_ONCE_KEY_NP;
147 
148 	if (thr_main())
149 		b = b_main;
150 	else if ((b = thr_get_storage(&ntoa_key, 18, free)) == NULL)
151 		b = b_main;
152 
153 	return (inet_ntoa_r(in, b));
154 }
155 
156 /*
157  * Check whether "cp" is a valid ascii representation
158  * of an Internet address and convert to a binary address.
159  * Returns 1 if the address is valid, 0 if not.
160  * This replaces inet_addr, the return value from which
161  * cannot distinguish between failure and a local broadcast address.
162  */
163 int
164 inet_aton(const char *cp, struct in_addr *addr)
165 {
166 	uint32_t val;
167 	int base, n;
168 	char c;
169 	unsigned int parts[4];
170 	unsigned int *pp = parts;
171 
172 
173 	c = *cp;
174 	for (;;) {
175 		/*
176 		 * Collect number up to ``.''.
177 		 * Values are specified as for C:
178 		 * 0x=hex, 0=octal, isdigit=decimal.
179 		 */
180 		if (!isdigit(c))
181 			return (0);
182 		val = 0; base = 10;
183 		if (c == '0') {
184 			c = *++cp;
185 			if (c == 'x' || c == 'X')
186 				base = 16, c = *++cp;
187 			else
188 				base = 8;
189 		}
190 		for (;;) {
191 			if (isascii(c) && isdigit(c)) {
192 				val = (val * base) + (c - '0');
193 				c = *++cp;
194 			} else if (base == 16 && isascii(c) && isxdigit(c)) {
195 				val = (val << 4) |
196 					(c + 10 - (islower(c) ? 'a' : 'A'));
197 				c = *++cp;
198 			} else
199 				break;
200 		}
201 		if (c == '.') {
202 			/*
203 			 * Internet format:
204 			 *	a.b.c.d
205 			 *	a.b.c	(with c treated as 16 bits)
206 			 *	a.b	(with b treated as 24 bits)
207 			 */
208 			if (pp >= parts + 3)
209 				return (0);
210 			*pp++ = val;
211 			c = *++cp;
212 		} else
213 			break;
214 	}
215 	/*
216 	 * Check for trailing characters.
217 	 */
218 	if (c != '\0' && (!isascii(c) || !isspace(c)))
219 		return (0);
220 	/*
221 	 * Concoct the address according to
222 	 * the number of parts specified.
223 	 */
224 	n = pp - parts + 1;
225 	switch (n) {
226 
227 	case 0:
228 		return (0);		/* initial nondigit */
229 
230 	case 1:				/* a -- 32 bits */
231 		break;
232 
233 	case 2:				/* a.b -- 8.24 bits */
234 		if ((val > 0xffffff) || (parts[0] > 0xff))
235 			return (0);
236 		val |= parts[0] << 24;
237 		break;
238 
239 	case 3:				/* a.b.c -- 8.8.16 bits */
240 		if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
241 			return (0);
242 		val |= (parts[0] << 24) | (parts[1] << 16);
243 		break;
244 
245 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
246 		if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) ||
247 		    (parts[2] > 0xff))
248 			return (0);
249 		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
250 		break;
251 	}
252 	if (addr)
253 		addr->s_addr = htonl(val);
254 	return (1);
255 }
256 
257 /*
258  * Internet address interpretation routine.
259  * All the network library routines call this
260  * routine to interpret entries in the data bases
261  * which are expected to be an address.
262  * The value returned is in network order.
263  */
264 in_addr_t
265 inet_addr(const char *cp)
266 {
267 	struct in_addr val;
268 
269 	if (inet_aton(cp, &val))
270 		return (val.s_addr);
271 	return (INADDR_NONE);
272 }
273 
274 /*
275  * Return the network number from an internet
276  * address; handles class a/b/c network #'s.
277  */
278 uint_t
279 inet_netof(struct in_addr in)
280 {
281 	uint32_t i = ntohl(in.s_addr);
282 
283 	if (IN_CLASSA(i))
284 		return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
285 	if (IN_CLASSB(i))
286 		return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
287 	return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
288 }
289