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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * 4.3 BSD under license from the Regents of the University of
36 * Copyright (c) 1983, 1990, 1993
37 * The Regents of the University of California. All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
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.
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
69 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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
89 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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
105 #pragma ident "%Z%%M% %I% %E% SMI"
108 * Convert network-format internet address
109 * to base 256 d.d.d.d representation.
111 * Reentrant interface
117 #include <sys/types.h>
119 #include <netinet/in.h>
127 char b
[]; /* Assumed >= 18 bytes */
132 #define UC(b) (((int)b)&0xff)
133 (void) sprintf(b
, "%d.%d.%d.%d",
134 UC(p
[0]), UC(p
[1]), UC(p
[2]), UC(p
[3]));
143 static char b_main
[18];
144 static pthread_key_t ntoa_key
= PTHREAD_ONCE_KEY_NP
;
148 else if ((b
= thr_get_storage(&ntoa_key
, 18, free
)) == NULL
)
151 return (inet_ntoa_r(in
, b
));
155 * Check whether "cp" is a valid ascii representation
156 * of an Internet address and convert to a binary address.
157 * Returns 1 if the address is valid, 0 if not.
158 * This replaces inet_addr, the return value from which
159 * cannot distinguish between failure and a local broadcast address.
162 inet_aton(const char *cp
, struct in_addr
*addr
)
167 unsigned int parts
[4];
168 unsigned int *pp
= parts
;
174 * Collect number up to ``.''.
175 * Values are specified as for C:
176 * 0x=hex, 0=octal, isdigit=decimal.
183 if (c
== 'x' || c
== 'X')
184 base
= 16, c
= *++cp
;
189 if (isascii(c
) && isdigit(c
)) {
190 val
= (val
* base
) + (c
- '0');
192 } else if (base
== 16 && isascii(c
) && isxdigit(c
)) {
194 (c
+ 10 - (islower(c
) ? 'a' : 'A'));
203 * a.b.c (with c treated as 16 bits)
204 * a.b (with b treated as 24 bits)
214 * Check for trailing characters.
216 if (c
!= '\0' && (!isascii(c
) || !isspace(c
)))
219 * Concoct the address according to
220 * the number of parts specified.
226 return (0); /* initial nondigit */
228 case 1: /* a -- 32 bits */
231 case 2: /* a.b -- 8.24 bits */
232 if ((val
> 0xffffff) || (parts
[0] > 0xff))
234 val
|= parts
[0] << 24;
237 case 3: /* a.b.c -- 8.8.16 bits */
238 if ((val
> 0xffff) || (parts
[0] > 0xff) || (parts
[1] > 0xff))
240 val
|= (parts
[0] << 24) | (parts
[1] << 16);
243 case 4: /* a.b.c.d -- 8.8.8.8 bits */
244 if ((val
> 0xff) || (parts
[0] > 0xff) || (parts
[1] > 0xff) ||
247 val
|= (parts
[0] << 24) | (parts
[1] << 16) | (parts
[2] << 8);
251 addr
->s_addr
= htonl(val
);
256 * Internet address interpretation routine.
257 * All the network library routines call this
258 * routine to interpret entries in the data bases
259 * which are expected to be an address.
260 * The value returned is in network order.
263 inet_addr(const char *cp
)
267 if (inet_aton(cp
, &val
))
269 return (INADDR_NONE
);
273 * Return the network number from an internet
274 * address; handles class a/b/c network #'s.
277 inet_netof(struct in_addr in
)
279 uint32_t i
= ntohl(in
.s_addr
);
282 return ((i
& IN_CLASSA_NET
) >> IN_CLASSA_NSHIFT
);
284 return ((i
& IN_CLASSB_NET
) >> IN_CLASSB_NSHIFT
);
285 return ((i
& IN_CLASSC_NET
) >> IN_CLASSC_NSHIFT
);