xref: /titanic_50/usr/src/lib/libnsl/rpc/inet_ntoa.c (revision 554ff184129088135ad2643c1c9832174a17be88)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * Convert network-format internet address
39  * to base 256 d.d.d.d representation.
40  *
41  * Reentrant interface
42  */
43 
44 #include "mt.h"
45 #include "rpc_mt.h"
46 #include <errno.h>
47 #include <sys/types.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 	(void) sprintf(b, "%d.%d.%d.%d",
64 				UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
65 	return (b);
66 }
67 
68 char *
69 inet_ntoa(in)
70 	struct in_addr in;
71 {
72 	char *b;
73 	static char b_main[18];
74 	static pthread_key_t ntoa_key;
75 
76 	if (thr_main())
77 		b = b_main;
78 	else if ((b = thr_get_storage(&ntoa_key, 18, free)) == NULL)
79 		b = b_main;
80 
81 	return (inet_ntoa_r(in, b));
82 }
83 
84 #pragma weak inet_aton = _inet_aton
85 /*
86  * Check whether "cp" is a valid ascii representation
87  * of an Internet address and convert to a binary address.
88  * Returns 1 if the address is valid, 0 if not.
89  * This replaces inet_addr, the return value from which
90  * cannot distinguish between failure and a local broadcast address.
91  */
92 int
93 _inet_aton(const char *cp, struct in_addr *addr)
94 {
95 	uint32_t val;
96 	int base, n;
97 	char c;
98 	unsigned int parts[4];
99 	unsigned int *pp = parts;
100 
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 	return (1);
184 }
185 
186 /*
187  * Internet address interpretation routine.
188  * All the network library routines call this
189  * routine to interpret entries in the data bases
190  * which are expected to be an address.
191  * The value returned is in network order.
192  */
193 in_addr_t
194 inet_addr(const char *cp)
195 {
196 	struct in_addr val;
197 
198 	if (_inet_aton(cp, &val))
199 		return (val.s_addr);
200 	return (INADDR_NONE);
201 }
202 
203 /*
204  * Return the network number from an internet
205  * address; handles class a/b/c network #'s.
206  */
207 uint_t
208 inet_netof(struct in_addr in)
209 {
210 	uint32_t i = ntohl(in.s_addr);
211 
212 	if (IN_CLASSA(i))
213 		return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
214 	if (IN_CLASSB(i))
215 		return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
216 	return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
217 }
218