2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * $Id: inet_net_pton.c,v 1.8.672.1 2008/08/26 04:42:38 marka Exp $
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
26 #include <arpa/inet.h>
35 #include "port_after.h"
39 * inet_net_pton_ipv4(src, dst, size)
40 * convert IPv4 network number from presentation to network format.
41 * accepts hex octets, hex strings, decimal octets, and /CIDR.
42 * "size" is in bytes and describes "dst".
44 * number of bits, either imputed classfully or specified with /CIDR,
45 * or -1 if some failure occurred (check errno). ENOENT means it was
46 * not an IPv4 network specification.
48 * network byte order assumed. this means 192.5.5.240/28 has
49 * 0b11110000 in its fourth octet.
51 * Paul Vixie (ISC), June 1996
54 inet_net_pton_ipv4(const char *src
, u_char
*dst
, size_t size
) {
55 static const char xdigits
[] = "0123456789abcdef";
56 static const char digits
[] = "0123456789";
57 int n
, ch
, tmp
= 0, dirty
, bits
;
58 const u_char
*odst
= dst
;
61 if (ch
== '0' && (src
[0] == 'x' || src
[0] == 'X')
62 && isascii((unsigned char)(src
[1]))
63 && isxdigit((unsigned char)(src
[1]))) {
64 /* Hexadecimal: Eat nybble string. */
68 src
++; /*%< skip x or X. */
69 while ((ch
= *src
++) != '\0' && isascii(ch
) && isxdigit(ch
)) {
72 n
= strchr(xdigits
, ch
) - xdigits
;
73 assert(n
>= 0 && n
<= 15);
81 *dst
++ = (u_char
) tmp
;
85 if (dirty
) { /*%< Odd trailing nybble? */
88 *dst
++ = (u_char
) (tmp
<< 4);
90 } else if (isascii(ch
) && isdigit(ch
)) {
91 /* Decimal: eat dotted digit string. */
95 n
= strchr(digits
, ch
) - digits
;
96 assert(n
>= 0 && n
<= 9);
101 } while ((ch
= *src
++) != '\0' &&
102 isascii(ch
) && isdigit(ch
));
105 *dst
++ = (u_char
) tmp
;
106 if (ch
== '\0' || ch
== '/')
111 if (!isascii(ch
) || !isdigit(ch
))
118 if (ch
== '/' && isascii((unsigned char)(src
[0])) &&
119 isdigit((unsigned char)(src
[0])) && dst
> odst
) {
120 /* CIDR width specifier. Nothing can follow it. */
121 ch
= *src
++; /*%< Skip over the /. */
124 n
= strchr(digits
, ch
) - digits
;
125 assert(n
>= 0 && n
<= 9);
130 } while ((ch
= *src
++) != '\0' && isascii(ch
) && isdigit(ch
));
135 /* Firey death and destruction unless we prefetched EOS. */
139 /* If nothing was written to the destination, we found no address. */
142 /* If no CIDR spec was given, infer width from net class. */
144 if (*odst
>= 240) /*%< Class E */
146 else if (*odst
>= 224) /*%< Class D */
148 else if (*odst
>= 192) /*%< Class C */
150 else if (*odst
>= 128) /*%< Class B */
154 /* If imputed mask is narrower than specified octets, widen. */
155 if (bits
< ((dst
- odst
) * 8))
156 bits
= (dst
- odst
) * 8;
158 * If there are no additional bits specified for a class D
159 * address adjust bits to 4.
161 if (bits
== 8 && *odst
== 224)
164 /* Extend network to cover the actual mask. */
165 while (bits
> ((dst
- odst
) * 8)) {
182 getbits(const char *src
, int *bitsp
) {
183 static const char digits
[] = "0123456789";
190 while ((ch
= *src
++) != '\0') {
193 pch
= strchr(digits
, ch
);
195 if (n
++ != 0 && val
== 0) /*%< no leading zeros */
198 val
+= (pch
- digits
);
199 if (val
> 128) /*%< range */
212 getv4(const char *src
, u_char
*dst
, int *bitsp
) {
213 static const char digits
[] = "0123456789";
221 while ((ch
= *src
++) != '\0') {
224 pch
= strchr(digits
, ch
);
226 if (n
++ != 0 && val
== 0) /*%< no leading zeros */
229 val
+= (pch
- digits
);
230 if (val
> 255) /*%< range */
234 if (ch
== '.' || ch
== '/') {
235 if (dst
- odst
> 3) /*%< too many octets? */
239 return (getbits(src
, bitsp
));
248 if (dst
- odst
> 3) /*%< too many octets? */
255 inet_net_pton_ipv6(const char *src
, u_char
*dst
, size_t size
) {
256 static const char xdigits_l
[] = "0123456789abcdef",
257 xdigits_u
[] = "0123456789ABCDEF";
258 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
259 const char *xdigits
, *curtok
;
268 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
269 endp
= tp
+ NS_IN6ADDRSZ
;
271 /* Leading :: requires some special handling. */
281 while ((ch
= *src
++) != '\0') {
284 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
285 pch
= strchr((xdigits
= xdigits_u
), ch
);
288 val
|= (pch
- xdigits
);
301 } else if (*src
== '\0')
303 if (tp
+ NS_INT16SZ
> endp
)
305 *tp
++ = (u_char
) (val
>> 8) & 0xff;
306 *tp
++ = (u_char
) val
& 0xff;
312 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
313 getv4(curtok
, tp
, &bits
) > 0) {
317 break; /*%< '\\0' was seen by inet_pton4(). */
319 if (ch
== '/' && getbits(src
, &bits
) > 0)
324 if (tp
+ NS_INT16SZ
> endp
)
326 *tp
++ = (u_char
) (val
>> 8) & 0xff;
327 *tp
++ = (u_char
) val
& 0xff;
332 words
= (bits
+ 15) / 16;
337 endp
= tmp
+ 2 * words
;
339 if (colonp
!= NULL
) {
341 * Since some memmove()'s erroneously fail to handle
342 * overlapping regions, we'll do the shift by hand.
344 const int n
= tp
- colonp
;
349 for (i
= 1; i
<= n
; i
++) {
350 endp
[- i
] = colonp
[n
- i
];
358 bytes
= (bits
+ 7) / 8;
361 memcpy(dst
, tmp
, bytes
);
375 * inet_net_pton(af, src, dst, size)
376 * convert network number from presentation to network format.
377 * accepts hex octets, hex strings, decimal octets, and /CIDR.
378 * "size" is in bytes and describes "dst".
380 * number of bits, either imputed classfully or specified with /CIDR,
381 * or -1 if some failure occurred (check errno). ENOENT means it was
382 * not a valid network specification.
384 * Paul Vixie (ISC), June 1996
387 inet_net_pton(int af
, const char *src
, void *dst
, size_t size
) {
390 return (inet_net_pton_ipv4(src
, dst
, size
));
392 return (inet_net_pton_ipv6(src
, dst
, size
));
394 errno
= EAFNOSUPPORT
;
400 * Weak aliases for applications that use certain private entry points,
401 * and fail to include <arpa/inet.h>.
404 __weak_reference(__inet_net_pton
, inet_net_pton
);