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 "namespace.h"
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/nameser.h>
34 #include <arpa/inet.h>
36 #include <isc/assertions.h>
44 #include "port_after.h"
47 __weak_alias(inet_net_pton
,_inet_net_pton
)
51 # define SPRINTF(x) strlen(sprintf/**/x)
53 # define SPRINTF(x) ((size_t)sprintf x)
59 * inet_net_pton_ipv4(src, dst, size)
60 * convert IPv4 network number from presentation to network format.
61 * accepts hex octets, hex strings, decimal octets, and /CIDR.
62 * "size" is in bytes and describes "dst".
64 * number of bits, either imputed classfully or specified with /CIDR,
65 * or -1 if some failure occurred (check errno). ENOENT means it was
66 * not an IPv4 network specification.
68 * network byte order assumed. this means 192.5.5.240/28 has
69 * 0b11110000 in its fourth octet.
71 * Paul Vixie (ISC), June 1996
74 inet_net_pton_ipv4(const char *src
, u_char
*dst
, size_t size
)
76 static const char xdigits
[] = "0123456789abcdef";
77 static const char digits
[] = "0123456789";
80 const u_char
*odst
= dst
;
84 if (ch
== '0' && (src
[0] == 'x' || src
[0] == 'X')
85 && isascii((u_char
)(src
[1]))
86 && isxdigit((u_char
)(src
[1]))) {
87 /* Hexadecimal: Eat nybble string. */
91 src
++; /* skip x or X. */
92 while ((ch
= *src
++) != '\0' && isascii((u_char
)ch
)
93 && isxdigit((u_char
)ch
)) {
94 if (isupper((u_char
)ch
))
95 ch
= tolower((u_char
)ch
);
96 n
= strchr(xdigits
, ch
) - xdigits
;
97 INSIST(n
>= 0 && n
<= 15);
101 tmp
= (tmp
<< 4) | n
;
105 *dst
++ = (u_char
) tmp
;
109 if (dirty
) { /* Odd trailing nybble? */
112 *dst
++ = (u_char
) (tmp
<< 4);
114 } else if (isascii((u_char
)ch
) && isdigit((u_char
)ch
)) {
115 /* Decimal: eat dotted digit string. */
119 n
= strchr(digits
, ch
) - digits
;
120 INSIST(n
>= 0 && n
<= 9);
125 } while ((ch
= *src
++) != '\0' &&
126 isascii((u_char
)ch
) && isdigit((u_char
)ch
));
129 *dst
++ = (u_char
) tmp
;
130 if (ch
== '\0' || ch
== '/')
135 if (!isascii((u_char
)ch
) || !isdigit((u_char
)ch
))
142 if (ch
== '/' && isascii((u_char
)(src
[0])) &&
143 isdigit((u_char
)(src
[0])) && dst
> odst
) {
144 /* CIDR width specifier. Nothing can follow it. */
145 ch
= *src
++; /* Skip over the /. */
148 n
= strchr(digits
, ch
) - digits
;
149 INSIST(n
>= 0 && n
<= 9);
154 } while ((ch
= *src
++) != '\0' && isascii((u_char
)ch
)
155 && isdigit((u_char
)ch
));
160 /* Firey death and destruction unless we prefetched EOS. */
164 /* If nothing was written to the destination, we found no address. */
167 /* If no CIDR spec was given, infer width from net class. */
169 if (*odst
>= 240) /* Class E */
171 else if (*odst
>= 224) /* Class D */
173 else if (*odst
>= 192) /* Class C */
175 else if (*odst
>= 128) /* Class B */
179 /* If imputed mask is narrower than specified octets, widen. */
180 if (bits
>= 8 && bits
< ((dst
- odst
) * 8))
181 bits
= (int)(dst
- odst
) * 8;
183 /* Extend network to cover the actual mask. */
184 while (bits
> ((dst
- odst
) * 8)) {
201 getbits(const char *src
, int *bitsp
)
203 static const char digits
[] = "0123456789";
210 while ((ch
= *src
++) != '\0') {
213 pch
= strchr(digits
, ch
);
215 if (n
++ != 0 && val
== 0) /* no leading zeros */
218 val
+= (int)(pch
- digits
);
219 if (val
> 128) /* range */
232 getv4(const char *src
, u_char
*dst
, int *bitsp
)
234 static const char digits
[] = "0123456789";
242 while ((ch
= *src
++) != '\0') {
245 pch
= strchr(digits
, ch
);
247 if (n
++ != 0 && val
== 0) /* no leading zeros */
250 val
+= (int)(pch
- digits
);
251 if (val
> 255) /* range */
255 if (ch
== '.' || ch
== '/') {
256 if (dst
- odst
> 3) /* too many octets? */
260 return (getbits(src
, bitsp
));
269 if (dst
- odst
> 3) /* too many octets? */
276 inet_net_pton_ipv6(const char *src
, u_char
*dst
, size_t size
)
278 static const char xdigits_l
[] = "0123456789abcdef",
279 xdigits_u
[] = "0123456789ABCDEF";
280 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
281 const char *xdigits
, *curtok
;
290 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
291 endp
= tp
+ NS_IN6ADDRSZ
;
293 /* Leading :: requires some special handling. */
303 while ((ch
= *src
++) != '\0') {
306 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
307 pch
= strchr((xdigits
= xdigits_u
), ch
);
310 val
|= (int)(pch
- xdigits
);
323 } else if (*src
== '\0')
325 if (tp
+ NS_INT16SZ
> endp
)
327 *tp
++ = (u_char
) (val
>> 8) & 0xff;
328 *tp
++ = (u_char
) val
& 0xff;
334 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
335 getv4(curtok
, tp
, &bits
) > 0) {
339 break; /* '\0' was seen by inet_pton4(). */
341 if (ch
== '/' && getbits(src
, &bits
) > 0)
346 if (tp
+ NS_INT16SZ
> endp
)
348 *tp
++ = (u_char
) (val
>> 8) & 0xff;
349 *tp
++ = (u_char
) val
& 0xff;
354 words
= (bits
+ 15) / 16;
359 endp
= tmp
+ 2 * words
;
361 if (colonp
!= NULL
) {
363 * Since some memmove()'s erroneously fail to handle
364 * overlapping regions, we'll do the shift by hand.
366 const ptrdiff_t n
= tp
- colonp
;
371 for (i
= 1; i
<= n
; i
++) {
372 endp
[- i
] = colonp
[n
- i
];
380 bytes
= (bits
+ 7) / 8;
383 memcpy(dst
, tmp
, bytes
);
397 * inet_net_pton(af, src, dst, size)
398 * convert network number from presentation to network format.
399 * accepts hex octets, hex strings, decimal octets, and /CIDR.
400 * "size" is in bytes and describes "dst".
402 * number of bits, either imputed classfully or specified with /CIDR,
403 * or -1 if some failure occurred (check errno). ENOENT means it was
404 * not a valid network specification.
406 * Paul Vixie (ISC), June 1996
409 inet_net_pton(int af
, const char *src
, void *dst
, size_t size
)
413 return (inet_net_pton_ipv4(src
, dst
, size
));
415 return (inet_net_pton_ipv6(src
, dst
, size
));
417 errno
= EAFNOSUPPORT
;