4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "ares_setup.h"
22 #ifdef HAVE_SYS_SOCKET_H
23 # include <sys/socket.h>
25 #ifdef HAVE_NETINET_IN_H
26 # include <netinet/in.h>
28 #ifdef HAVE_ARPA_INET_H
29 # include <arpa/inet.h>
31 #ifdef HAVE_ARPA_NAMESER_H
32 # include <arpa/nameser.h>
36 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
37 # include <arpa/nameser_compat.h>
46 #include "ares_ipv6.h"
47 #include "inet_net_pton.h"
49 #if !defined(HAVE_INET_NET_PTON) || !defined(HAVE_INET_NET_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 * On Windows we store the error in the thread errno, not
66 * in the winsock error code. This is to avoid loosing the
67 * actual last winsock error. So use macro ERRNO to fetch the
68 * errno this funtion sets when returning (-1), not SOCKERRNO.
70 * Paul Vixie (ISC), June 1996
73 inet_net_pton_ipv4(const char *src, unsigned char *dst, size_t size)
75 static const char xdigits[] = "0123456789abcdef";
76 static const char digits[] = "0123456789";
77 int n, ch, tmp = 0, dirty, bits;
78 const unsigned char *odst = dst;
81 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
82 && ISXDIGIT(src[1])) {
83 /* Hexadecimal: Eat nybble string. */
87 src++; /* skip x or X. */
88 while ((ch = *src++) != '\0' && ISXDIGIT(ch)) {
91 n = (int)(strchr(xdigits, ch) - xdigits);
99 *dst++ = (unsigned char) tmp;
103 if (dirty) { /* Odd trailing nybble? */
106 *dst++ = (unsigned char) (tmp << 4);
108 } else if (ISDIGIT(ch)) {
109 /* Decimal: eat dotted digit string. */
113 n = (int)(strchr(digits, ch) - digits);
118 } while ((ch = *src++) != '\0' &&
122 *dst++ = (unsigned char) tmp;
123 if (ch == '\0' || ch == '/')
136 ISDIGIT(src[0]) && dst > odst) {
137 /* CIDR width specifier. Nothing can follow it. */
138 ch = *src++; /* Skip over the /. */
141 n = (int)(strchr(digits, ch) - digits);
144 } while ((ch = *src++) != '\0' && ISDIGIT(ch));
151 /* Firey death and destruction unless we prefetched EOS. */
155 /* If nothing was written to the destination, we found no address. */
158 /* If no CIDR spec was given, infer width from net class. */
160 if (*odst >= 240) /* Class E */
162 else if (*odst >= 224) /* Class D */
164 else if (*odst >= 192) /* Class C */
166 else if (*odst >= 128) /* Class B */
170 /* If imputed mask is narrower than specified octets, widen. */
171 if (bits < ((dst - odst) * 8))
172 bits = (int)(dst - odst) * 8;
174 * If there are no additional bits specified for a class D
175 * address adjust bits to 4.
177 if (bits == 8 && *odst == 224)
180 /* Extend network to cover the actual mask. */
181 while (bits > ((dst - odst) * 8)) {
198 getbits(const char *src, int *bitsp)
200 static const char digits[] = "0123456789";
207 while ((ch = *src++) != '\0') {
210 pch = strchr(digits, ch);
212 if (n++ != 0 && val == 0) /* no leading zeros */
215 val += (pch - digits);
216 if (val > 128) /* range */
229 getv4(const char *src, unsigned char *dst, int *bitsp)
231 static const char digits[] = "0123456789";
232 unsigned char *odst = dst;
239 while ((ch = *src++) != '\0') {
242 pch = strchr(digits, ch);
244 if (n++ != 0 && val == 0) /* no leading zeros */
247 val += (pch - digits);
248 if (val > 255) /* range */
252 if (ch == '.' || ch == '/') {
253 if (dst - odst > 3) /* too many octets? */
255 *dst++ = (unsigned char)val;
257 return (getbits(src, bitsp));
266 if (dst - odst > 3) /* too many octets? */
268 *dst++ = (unsigned char)val;
273 inet_net_pton_ipv6(const char *src, unsigned char *dst, size_t size)
275 static const char xdigits_l[] = "0123456789abcdef",
276 xdigits_u[] = "0123456789ABCDEF";
277 unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
278 const char *xdigits, *curtok;
287 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
288 endp = tp + NS_IN6ADDRSZ;
290 /* Leading :: requires some special handling. */
300 while ((ch = *src++) != '\0') {
303 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
304 pch = strchr((xdigits = xdigits_u), ch);
307 val |= (pch - xdigits);
320 } else if (*src == '\0')
322 if (tp + NS_INT16SZ > endp)
324 *tp++ = (unsigned char)((val >> 8) & 0xff);
325 *tp++ = (unsigned char)(val & 0xff);
331 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
332 getv4(curtok, tp, &bits) > 0) {
336 break; /* '\0' was seen by inet_pton4(). */
338 if (ch == '/' && getbits(src, &bits) > 0)
343 if (tp + NS_INT16SZ > endp)
345 *tp++ = (unsigned char)((val >> 8) & 0xff);
346 *tp++ = (unsigned char)(val & 0xff);
351 words = (bits + 15) / 16;
356 endp = tmp + 2 * words;
358 if (colonp != NULL) {
360 * Since some memmove()'s erroneously fail to handle
361 * overlapping regions, we'll do the shift by hand.
363 const int n = (int)(tp - colonp);
368 for (i = 1; i <= n; i++) {
369 endp[- i] = colonp[n - i];
377 bytes = (bits + 7) / 8;
380 memcpy(dst, tmp, bytes);
394 * inet_net_pton(af, src, dst, size)
395 * convert network number from presentation to network format.
396 * accepts hex octets, hex strings, decimal octets, and /CIDR.
397 * "size" is in bytes and describes "dst".
399 * number of bits, either imputed classfully or specified with /CIDR,
400 * or -1 if some failure occurred (check errno). ENOENT means it was
401 * not a valid network specification.
403 * On Windows we store the error in the thread errno, not
404 * in the winsock error code. This is to avoid loosing the
405 * actual last winsock error. So use macro ERRNO to fetch the
406 * errno this funtion sets when returning (-1), not SOCKERRNO.
408 * Paul Vixie (ISC), June 1996
411 ares_inet_net_pton(int af, const char *src, void *dst, size_t size)
415 return (inet_net_pton_ipv4(src, dst, size));
417 return (inet_net_pton_ipv6(src, dst, size));
419 SET_ERRNO(EAFNOSUPPORT);
426 #ifndef HAVE_INET_PTON
427 int ares_inet_pton(int af, const char *src, void *dst)
433 size = sizeof(struct in_addr);
434 else if (af == AF_INET6)
435 size = sizeof(struct in6_addr);
438 SET_ERRNO(EAFNOSUPPORT);
441 result = ares_inet_net_pton(af, src, dst, size);
442 if (result == -1 && ERRNO == ENOENT)
444 return (result > -1 ? 1 : -1);