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