2 * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1996, 1998, 1999, 2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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 WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid
[] = "$Id: inet_net_pton.c,v 1.10 2008/11/14 02:36:51 marka Exp $";
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
24 #include "port_before.h"
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
30 #include <arpa/inet.h>
39 #include "port_after.h"
42 # define SPRINTF(x) strlen(sprintf/**/x)
44 # define SPRINTF(x) ((size_t)sprintf x)
49 * inet_net_pton_ipv4(src, dst, size)
50 * convert IPv4 network number from presentation to network format.
51 * accepts hex octets, hex strings, decimal octets, and /CIDR.
52 * "size" is in bytes and describes "dst".
54 * number of bits, either imputed classfully or specified with /CIDR,
55 * or -1 if some failure occurred (check errno). ENOENT means it was
56 * not an IPv4 network specification.
58 * network byte order assumed. this means 192.5.5.240/28 has
59 * 0b11110000 in its fourth octet.
61 * Paul Vixie (ISC), June 1996
64 inet_net_pton_ipv4(const char *src
, u_char
*dst
, size_t size
) {
65 static const char xdigits
[] = "0123456789abcdef";
66 static const char digits
[] = "0123456789";
67 int n
, ch
, tmp
= 0, dirty
, bits
;
68 const u_char
*odst
= dst
;
71 if (ch
== '0' && (src
[0] == 'x' || src
[0] == 'X')
72 && isascii((unsigned char)(src
[1]))
73 && isxdigit((unsigned char)(src
[1]))) {
74 /* Hexadecimal: Eat nybble string. */
78 src
++; /*%< skip x or X. */
79 while ((ch
= *src
++) != '\0' && isascii(ch
) && isxdigit(ch
)) {
82 n
= strchr(xdigits
, ch
) - xdigits
;
83 assert(n
>= 0 && n
<= 15);
91 *dst
++ = (u_char
) tmp
;
95 if (dirty
) { /*%< Odd trailing nybble? */
98 *dst
++ = (u_char
) (tmp
<< 4);
100 } else if (isascii(ch
) && isdigit(ch
)) {
101 /* Decimal: eat dotted digit string. */
105 n
= strchr(digits
, ch
) - digits
;
106 assert(n
>= 0 && n
<= 9);
111 } while ((ch
= *src
++) != '\0' &&
112 isascii(ch
) && isdigit(ch
));
115 *dst
++ = (u_char
) tmp
;
116 if (ch
== '\0' || ch
== '/')
121 if (!isascii(ch
) || !isdigit(ch
))
128 if (ch
== '/' && isascii((unsigned char)(src
[0])) &&
129 isdigit((unsigned char)(src
[0])) && dst
> odst
) {
130 /* CIDR width specifier. Nothing can follow it. */
131 ch
= *src
++; /*%< Skip over the /. */
134 n
= strchr(digits
, ch
) - digits
;
135 assert(n
>= 0 && n
<= 9);
140 } while ((ch
= *src
++) != '\0' && isascii(ch
) && isdigit(ch
));
145 /* Firey death and destruction unless we prefetched EOS. */
149 /* If nothing was written to the destination, we found no address. */
152 /* If no CIDR spec was given, infer width from net class. */
154 if (*odst
>= 240) /*%< Class E */
156 else if (*odst
>= 224) /*%< Class D */
158 else if (*odst
>= 192) /*%< Class C */
160 else if (*odst
>= 128) /*%< Class B */
164 /* If imputed mask is narrower than specified octets, widen. */
165 if (bits
< ((dst
- odst
) * 8))
166 bits
= (dst
- odst
) * 8;
168 * If there are no additional bits specified for a class D
169 * address adjust bits to 4.
171 if (bits
== 8 && *odst
== 224)
174 /* Extend network to cover the actual mask. */
175 while (bits
> ((dst
- odst
) * 8)) {
192 getbits(const char *src
, int *bitsp
) {
193 static const char digits
[] = "0123456789";
200 while ((ch
= *src
++) != '\0') {
203 pch
= strchr(digits
, ch
);
205 if (n
++ != 0 && val
== 0) /*%< no leading zeros */
208 val
+= (pch
- digits
);
209 if (val
> 128) /*%< range */
222 getv4(const char *src
, u_char
*dst
, int *bitsp
) {
223 static const char digits
[] = "0123456789";
231 while ((ch
= *src
++) != '\0') {
234 pch
= strchr(digits
, ch
);
236 if (n
++ != 0 && val
== 0) /*%< no leading zeros */
239 val
+= (pch
- digits
);
240 if (val
> 255) /*%< range */
244 if (ch
== '.' || ch
== '/') {
245 if (dst
- odst
> 3) /*%< too many octets? */
249 return (getbits(src
, bitsp
));
258 if (dst
- odst
> 3) /*%< too many octets? */
265 inet_net_pton_ipv6(const char *src
, u_char
*dst
, size_t size
) {
266 static const char xdigits_l
[] = "0123456789abcdef",
267 xdigits_u
[] = "0123456789ABCDEF";
268 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
269 const char *xdigits
, *curtok
;
278 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
279 endp
= tp
+ NS_IN6ADDRSZ
;
281 /* Leading :: requires some special handling. */
291 while ((ch
= *src
++) != '\0') {
294 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
295 pch
= strchr((xdigits
= xdigits_u
), ch
);
298 val
|= (pch
- xdigits
);
311 } else if (*src
== '\0')
313 if (tp
+ NS_INT16SZ
> endp
)
315 *tp
++ = (u_char
) (val
>> 8) & 0xff;
316 *tp
++ = (u_char
) val
& 0xff;
322 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
323 getv4(curtok
, tp
, &bits
) > 0) {
327 break; /*%< '\\0' was seen by inet_pton4(). */
329 if (ch
== '/' && getbits(src
, &bits
) > 0)
334 if (tp
+ NS_INT16SZ
> endp
)
336 *tp
++ = (u_char
) (val
>> 8) & 0xff;
337 *tp
++ = (u_char
) val
& 0xff;
342 words
= (bits
+ 15) / 16;
347 endp
= tmp
+ 2 * words
;
349 if (colonp
!= NULL
) {
351 * Since some memmove()'s erroneously fail to handle
352 * overlapping regions, we'll do the shift by hand.
354 const int n
= tp
- colonp
;
359 for (i
= 1; i
<= n
; i
++) {
360 endp
[- i
] = colonp
[n
- i
];
368 bytes
= (bits
+ 7) / 8;
371 memcpy(dst
, tmp
, bytes
);
385 * inet_net_pton(af, src, dst, size)
386 * convert network number from presentation to network format.
387 * accepts hex octets, hex strings, decimal octets, and /CIDR.
388 * "size" is in bytes and describes "dst".
390 * number of bits, either imputed classfully or specified with /CIDR,
391 * or -1 if some failure occurred (check errno). ENOENT means it was
392 * not a valid network specification.
394 * Paul Vixie (ISC), June 1996
397 inet_net_pton(int af
, const char *src
, void *dst
, size_t size
) {
400 return (inet_net_pton_ipv4(src
, dst
, size
));
402 return (inet_net_pton_ipv6(src
, dst
, size
));
404 errno
= EAFNOSUPPORT
;
410 * Weak aliases for applications that use certain private entry points,
411 * and fail to include <arpa/inet.h>.
414 __weak_reference(__inet_net_pton
, inet_net_pton
);