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.
21 #include <sys/types.h>
23 #if defined(WIN32) && !defined(WATT32)
27 #ifdef HAVE_SYS_SOCKET_H
28 #include <sys/socket.h>
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
33 #ifdef HAVE_ARPA_NAMESER_H
34 #include <arpa/nameser.h>
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
48 #if !defined(HAVE_INET_NET_PTON) || !defined(HAVE_INET_NET_PTON_IPV6) || \
49 !defined(HAVE_INET_PTON) || !defined(HAVE_INET_PTON_IPV6)
53 * inet_net_pton_ipv4(src, dst, size)
54 * convert IPv4 network number from presentation to network format.
55 * accepts hex octets, hex strings, decimal octets, and /CIDR.
56 * "size" is in bytes and describes "dst".
58 * number of bits, either imputed classfully or specified with /CIDR,
59 * or -1 if some failure occurred (check errno). ENOENT means it was
60 * not an IPv4 network specification.
62 * network byte order assumed. this means 192.5.5.240/28 has
63 * 0b11110000 in its fourth octet.
65 * Paul Vixie (ISC), June 1996
68 inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) {
69 static const char xdigits[] = "0123456789abcdef";
70 static const char digits[] = "0123456789";
71 int n, ch, tmp = 0, dirty, bits;
72 const u_char *odst = dst;
75 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
76 && isascii((unsigned char)(src[1]))
77 && isxdigit((unsigned char)(src[1]))) {
78 /* Hexadecimal: Eat nybble string. */
82 src++; /* skip x or X. */
83 while ((ch = *src++) != '\0' && isascii(ch) && isxdigit(ch)) {
86 n = strchr(xdigits, ch) - xdigits;
94 *dst++ = (u_char) tmp;
98 if (dirty) { /* Odd trailing nybble? */
101 *dst++ = (u_char) (tmp << 4);
103 } else if (isascii(ch) && isdigit(ch)) {
104 /* Decimal: eat dotted digit string. */
108 n = strchr(digits, ch) - digits;
113 } while ((ch = *src++) != '\0' &&
114 isascii(ch) && isdigit(ch));
117 *dst++ = (u_char) tmp;
118 if (ch == '\0' || ch == '/')
123 if (!isascii(ch) || !isdigit(ch))
130 if (ch == '/' && isascii((unsigned char)(src[0])) &&
131 isdigit((unsigned char)(src[0])) && dst > odst) {
132 /* CIDR width specifier. Nothing can follow it. */
133 ch = *src++; /* Skip over the /. */
136 n = strchr(digits, ch) - digits;
139 } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
146 /* Firey death and destruction unless we prefetched EOS. */
150 /* If nothing was written to the destination, we found no address. */
153 /* If no CIDR spec was given, infer width from net class. */
155 if (*odst >= 240) /* Class E */
157 else if (*odst >= 224) /* Class D */
159 else if (*odst >= 192) /* Class C */
161 else if (*odst >= 128) /* Class B */
165 /* If imputed mask is narrower than specified octets, widen. */
166 if (bits < ((dst - odst) * 8))
167 bits = (dst - odst) * 8;
169 * If there are no additional bits specified for a class D
170 * address adjust bits to 4.
172 if (bits == 8 && *odst == 224)
175 /* Extend network to cover the actual mask. */
176 while (bits > ((dst - odst) * 8)) {
193 getbits(const char *src, int *bitsp) {
194 static const char digits[] = "0123456789";
201 while ((ch = *src++) != '\0') {
204 pch = strchr(digits, ch);
206 if (n++ != 0 && val == 0) /* no leading zeros */
209 val += (pch - digits);
210 if (val > 128) /* range */
223 getv4(const char *src, u_char *dst, int *bitsp) {
224 static const char digits[] = "0123456789";
232 while ((ch = *src++) != '\0') {
235 pch = strchr(digits, ch);
237 if (n++ != 0 && val == 0) /* no leading zeros */
240 val += (pch - digits);
241 if (val > 255) /* range */
245 if (ch == '.' || ch == '/') {
246 if (dst - odst > 3) /* too many octets? */
250 return (getbits(src, bitsp));
259 if (dst - odst > 3) /* too many octets? */
266 inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) {
267 static const char xdigits_l[] = "0123456789abcdef",
268 xdigits_u[] = "0123456789ABCDEF";
269 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
270 const char *xdigits, *curtok;
279 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
280 endp = tp + NS_IN6ADDRSZ;
282 /* Leading :: requires some special handling. */
292 while ((ch = *src++) != '\0') {
295 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
296 pch = strchr((xdigits = xdigits_u), ch);
299 val |= (pch - xdigits);
312 } else if (*src == '\0')
314 if (tp + NS_INT16SZ > endp)
316 *tp++ = (u_char) (val >> 8) & 0xff;
317 *tp++ = (u_char) val & 0xff;
323 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
324 getv4(curtok, tp, &bits) > 0) {
328 break; /* '\0' was seen by inet_pton4(). */
330 if (ch == '/' && getbits(src, &bits) > 0)
335 if (tp + NS_INT16SZ > endp)
337 *tp++ = (u_char) (val >> 8) & 0xff;
338 *tp++ = (u_char) val & 0xff;
343 words = (bits + 15) / 16;
348 endp = tmp + 2 * words;
350 if (colonp != NULL) {
352 * Since some memmove()'s erroneously fail to handle
353 * overlapping regions, we'll do the shift by hand.
355 const int n = tp - colonp;
360 for (i = 1; i <= n; i++) {
361 endp[- i] = colonp[n - i];
369 bytes = (bits + 7) / 8;
372 memcpy(dst, tmp, bytes);
386 * inet_net_pton(af, src, dst, size)
387 * convert network number from presentation to network format.
388 * accepts hex octets, hex strings, decimal octets, and /CIDR.
389 * "size" is in bytes and describes "dst".
391 * number of bits, either imputed classfully or specified with /CIDR,
392 * or -1 if some failure occurred (check errno). ENOENT means it was
393 * not a valid network specification.
395 * Paul Vixie (ISC), June 1996
398 ares_inet_net_pton(int af, const char *src, void *dst, size_t size) {
401 return (inet_net_pton_ipv4(src, dst, size));
403 return (inet_net_pton_ipv6(src, dst, size));
405 errno = EAFNOSUPPORT;
412 #if !defined(HAVE_INET_PTON) || !defined(HAVE_INET_PTON_IPV6)
413 int ares_inet_pton(int af, const char *src, void *dst) {
417 size = sizeof(struct in_addr);
418 else if (af == AF_INET6)
419 size = sizeof(struct in6_addr);
422 errno = EAFNOSUPPORT;
425 result = ares_inet_net_pton(af, src, dst, size);
426 if (result == -1 && errno == ENOENT)
428 return (result > -1 ? 1 : -1);