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.
22 #if defined(WIN32) && !defined(WATT32)
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
29 #ifdef HAVE_NETINET_IN_H
30 #include <netinet/in.h>
32 #ifdef HAVE_ARPA_NAMESER_H
33 #include <arpa/nameser.h>
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
47 #include "ares_ipv6.h"
48 #include "inet_net_pton.h"
50 #if !defined(HAVE_INET_NET_PTON) || !defined(HAVE_INET_NET_PTON_IPV6) || \
51 !defined(HAVE_INET_PTON) || !defined(HAVE_INET_PTON_IPV6)
55 * inet_net_pton_ipv4(src, dst, size)
56 * convert IPv4 network number from presentation to network format.
57 * accepts hex octets, hex strings, decimal octets, and /CIDR.
58 * "size" is in bytes and describes "dst".
60 * number of bits, either imputed classfully or specified with /CIDR,
61 * or -1 if some failure occurred (check errno). ENOENT means it was
62 * not an IPv4 network specification.
64 * network byte order assumed. this means 192.5.5.240/28 has
65 * 0b11110000 in its fourth octet.
67 * On Windows we store the error in the thread errno, not
68 * in the winsock error code. This is to avoid loosing the
69 * actual last winsock error. So use macro ERRNO to fetch the
70 * errno this funtion sets when returning (-1), not SOCKERRNO.
72 * Paul Vixie (ISC), June 1996
75 inet_net_pton_ipv4(const char *src, unsigned char *dst, size_t size)
77 static const char xdigits[] = "0123456789abcdef";
78 static const char digits[] = "0123456789";
79 int n, ch, tmp = 0, dirty, bits;
80 const unsigned char *odst = dst;
83 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
84 && ISXDIGIT(src[1])) {
85 /* Hexadecimal: Eat nybble string. */
89 src++; /* skip x or X. */
90 while ((ch = *src++) != '\0' && ISXDIGIT(ch)) {
93 n = (int)(strchr(xdigits, ch) - xdigits);
101 *dst++ = (unsigned char) tmp;
105 if (dirty) { /* Odd trailing nybble? */
108 *dst++ = (unsigned char) (tmp << 4);
110 } else if (ISDIGIT(ch)) {
111 /* Decimal: eat dotted digit string. */
115 n = (int)(strchr(digits, ch) - digits);
120 } while ((ch = *src++) != '\0' &&
124 *dst++ = (unsigned char) tmp;
125 if (ch == '\0' || ch == '/')
138 ISDIGIT(src[0]) && dst > odst) {
139 /* CIDR width specifier. Nothing can follow it. */
140 ch = *src++; /* Skip over the /. */
143 n = (int)(strchr(digits, ch) - digits);
146 } while ((ch = *src++) != '\0' && ISDIGIT(ch));
153 /* Firey death and destruction unless we prefetched EOS. */
157 /* If nothing was written to the destination, we found no address. */
160 /* If no CIDR spec was given, infer width from net class. */
162 if (*odst >= 240) /* Class E */
164 else if (*odst >= 224) /* Class D */
166 else if (*odst >= 192) /* Class C */
168 else if (*odst >= 128) /* Class B */
172 /* If imputed mask is narrower than specified octets, widen. */
173 if (bits < ((dst - odst) * 8))
174 bits = (int)(dst - odst) * 8;
176 * If there are no additional bits specified for a class D
177 * address adjust bits to 4.
179 if (bits == 8 && *odst == 224)
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 += (pch - digits);
218 if (val > 128) /* range */
231 getv4(const char *src, unsigned char *dst, int *bitsp)
233 static const char digits[] = "0123456789";
234 unsigned char *odst = dst;
241 while ((ch = *src++) != '\0') {
244 pch = strchr(digits, ch);
246 if (n++ != 0 && val == 0) /* no leading zeros */
249 val += (pch - digits);
250 if (val > 255) /* range */
254 if (ch == '.' || ch == '/') {
255 if (dst - odst > 3) /* too many octets? */
257 *dst++ = (unsigned char)val;
259 return (getbits(src, bitsp));
268 if (dst - odst > 3) /* too many octets? */
270 *dst++ = (unsigned char)val;
275 inet_net_pton_ipv6(const char *src, unsigned char *dst, size_t size)
277 static const char xdigits_l[] = "0123456789abcdef",
278 xdigits_u[] = "0123456789ABCDEF";
279 unsigned 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 |= (pch - xdigits);
322 } else if (*src == '\0')
324 if (tp + NS_INT16SZ > endp)
326 *tp++ = (unsigned char)((val >> 8) & 0xff);
327 *tp++ = (unsigned 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++ = (unsigned char)((val >> 8) & 0xff);
348 *tp++ = (unsigned 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 int n = (int)(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 * On Windows we store the error in the thread errno, not
406 * in the winsock error code. This is to avoid loosing the
407 * actual last winsock error. So use macro ERRNO to fetch the
408 * errno this funtion sets when returning (-1), not SOCKERRNO.
410 * Paul Vixie (ISC), June 1996
413 ares_inet_net_pton(int af, const char *src, void *dst, size_t size)
417 return (inet_net_pton_ipv4(src, dst, size));
419 return (inet_net_pton_ipv6(src, dst, size));
421 SET_ERRNO(EAFNOSUPPORT);
428 #if !defined(HAVE_INET_PTON) || !defined(HAVE_INET_PTON_IPV6)
429 int ares_inet_pton(int af, const char *src, void *dst)
434 size = sizeof(struct in_addr);
435 else if (af == AF_INET6)
436 size = sizeof(struct in6_addr);
439 SET_ERRNO(EAFNOSUPPORT);
442 result = ares_inet_net_pton(af, src, dst, size);
443 if (result == -1 && ERRNO == ENOENT)
445 return (result > -1 ? 1 : -1);