2 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 #include <sys/cdefs.h>
19 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid
[] = "Id: inet_net_pton.c,v 1.4.2.1 2002/08/02 02:17:21 marka Exp ";
23 __RCSID("$NetBSD: inet_net_pton.c,v 1.4 2012/03/20 17:08:13 matt Exp $");
27 #include "port_before.h"
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <arpa/inet.h>
35 #include <isc/assertions.h>
43 #include "port_after.h"
46 __weak_alias(inet_net_pton
,_inet_net_pton
)
50 # define SPRINTF(x) strlen(sprintf/**/x)
52 # define SPRINTF(x) ((size_t)sprintf x)
58 * inet_net_pton_ipv4(src, dst, size)
59 * convert IPv4 network number from presentation to network format.
60 * accepts hex octets, hex strings, decimal octets, and /CIDR.
61 * "size" is in bytes and describes "dst".
63 * number of bits, either imputed classfully or specified with /CIDR,
64 * or -1 if some failure occurred (check errno). ENOENT means it was
65 * not an IPv4 network specification.
67 * network byte order assumed. this means 192.5.5.240/28 has
68 * 0b11110000 in its fourth octet.
70 * Paul Vixie (ISC), June 1996
73 inet_net_pton_ipv4(const char *src
, u_char
*dst
, size_t size
)
75 static const char xdigits
[] = "0123456789abcdef";
76 static const char digits
[] = "0123456789";
79 const u_char
*odst
= dst
;
83 if (ch
== '0' && (src
[0] == 'x' || src
[0] == 'X')
84 && isascii((u_char
)(src
[1]))
85 && isxdigit((u_char
)(src
[1]))) {
86 /* Hexadecimal: Eat nybble string. */
90 src
++; /* skip x or X. */
91 while ((ch
= *src
++) != '\0' && isascii((u_char
)ch
)
92 && isxdigit((u_char
)ch
)) {
93 if (isupper((u_char
)ch
))
94 ch
= tolower((u_char
)ch
);
95 n
= strchr(xdigits
, ch
) - xdigits
;
96 INSIST(n
>= 0 && n
<= 15);
100 tmp
= (tmp
<< 4) | n
;
104 *dst
++ = (u_char
) tmp
;
108 if (dirty
) { /* Odd trailing nybble? */
111 *dst
++ = (u_char
) (tmp
<< 4);
113 } else if (isascii((u_char
)ch
) && isdigit((u_char
)ch
)) {
114 /* Decimal: eat dotted digit string. */
118 n
= strchr(digits
, ch
) - digits
;
119 INSIST(n
>= 0 && n
<= 9);
124 } while ((ch
= *src
++) != '\0' &&
125 isascii((u_char
)ch
) && isdigit((u_char
)ch
));
128 *dst
++ = (u_char
) tmp
;
129 if (ch
== '\0' || ch
== '/')
134 if (!isascii((u_char
)ch
) || !isdigit((u_char
)ch
))
141 if (ch
== '/' && isascii((u_char
)(src
[0])) &&
142 isdigit((u_char
)(src
[0])) && dst
> odst
) {
143 /* CIDR width specifier. Nothing can follow it. */
144 ch
= *src
++; /* Skip over the /. */
147 n
= strchr(digits
, ch
) - digits
;
148 INSIST(n
>= 0 && n
<= 9);
153 } while ((ch
= *src
++) != '\0' && isascii((u_char
)ch
)
154 && isdigit((u_char
)ch
));
159 /* Firey death and destruction unless we prefetched EOS. */
163 /* If nothing was written to the destination, we found no address. */
166 /* If no CIDR spec was given, infer width from net class. */
168 if (*odst
>= 240) /* Class E */
170 else if (*odst
>= 224) /* Class D */
172 else if (*odst
>= 192) /* Class C */
174 else if (*odst
>= 128) /* Class B */
178 /* If imputed mask is narrower than specified octets, widen. */
179 if (bits
>= 8 && bits
< ((dst
- odst
) * 8))
180 bits
= (int)(dst
- odst
) * 8;
182 /* Extend network to cover the actual mask. */
183 while (bits
> ((dst
- odst
) * 8)) {
200 getbits(const char *src
, int *bitsp
)
202 static const char digits
[] = "0123456789";
209 while ((ch
= *src
++) != '\0') {
212 pch
= strchr(digits
, ch
);
214 if (n
++ != 0 && val
== 0) /* no leading zeros */
217 val
+= (int)(pch
- digits
);
218 if (val
> 128) /* range */
231 getv4(const char *src
, u_char
*dst
, int *bitsp
)
233 static const char digits
[] = "0123456789";
241 while ((ch
= *src
++) != '\0') {
244 pch
= strchr(digits
, ch
);
246 if (n
++ != 0 && val
== 0) /* no leading zeros */
249 val
+= (int)(pch
- digits
);
250 if (val
> 255) /* range */
254 if (ch
== '.' || ch
== '/') {
255 if (dst
- odst
> 3) /* too many octets? */
259 return (getbits(src
, bitsp
));
268 if (dst
- odst
> 3) /* too many octets? */
275 inet_net_pton_ipv6(const char *src
, u_char
*dst
, size_t size
)
277 static const char xdigits_l
[] = "0123456789abcdef",
278 xdigits_u
[] = "0123456789ABCDEF";
279 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
280 const char *xdigits
, *curtok
;
289 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
290 endp
= tp
+ NS_IN6ADDRSZ
;
292 /* Leading :: requires some special handling. */
302 while ((ch
= *src
++) != '\0') {
305 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
306 pch
= strchr((xdigits
= xdigits_u
), ch
);
309 val
|= (int)(pch
- xdigits
);
322 } else if (*src
== '\0')
324 if (tp
+ NS_INT16SZ
> endp
)
326 *tp
++ = (u_char
) (val
>> 8) & 0xff;
327 *tp
++ = (u_char
) val
& 0xff;
333 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
334 getv4(curtok
, tp
, &bits
) > 0) {
338 break; /* '\0' was seen by inet_pton4(). */
340 if (ch
== '/' && getbits(src
, &bits
) > 0)
345 if (tp
+ NS_INT16SZ
> endp
)
347 *tp
++ = (u_char
) (val
>> 8) & 0xff;
348 *tp
++ = (u_char
) val
& 0xff;
353 words
= (bits
+ 15) / 16;
358 endp
= tmp
+ 2 * words
;
360 if (colonp
!= NULL
) {
362 * Since some memmove()'s erroneously fail to handle
363 * overlapping regions, we'll do the shift by hand.
365 const ptrdiff_t n
= tp
- colonp
;
370 for (i
= 1; i
<= n
; i
++) {
371 endp
[- i
] = colonp
[n
- i
];
379 bytes
= (bits
+ 7) / 8;
382 memcpy(dst
, tmp
, bytes
);
396 * inet_net_pton(af, src, dst, size)
397 * convert network number from presentation to network format.
398 * accepts hex octets, hex strings, decimal octets, and /CIDR.
399 * "size" is in bytes and describes "dst".
401 * number of bits, either imputed classfully or specified with /CIDR,
402 * or -1 if some failure occurred (check errno). ENOENT means it was
403 * not a valid network specification.
405 * Paul Vixie (ISC), June 1996
408 inet_net_pton(int af
, const char *src
, void *dst
, size_t size
)
412 return (inet_net_pton_ipv4(src
, dst
, size
));
414 return (inet_net_pton_ipv6(src
, dst
, size
));
416 errno
= EAFNOSUPPORT
;
422 #pragma weak inet_net_pton = __inet_net_pton