3
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
4
* Copyright (c) 1996,1999 by Internet Software Consortium.
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
10
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
#include "ares_setup.h"
21
#ifdef HAVE_NETINET_IN_H
22
# include <netinet/in.h>
24
#ifdef HAVE_ARPA_INET_H
25
# include <arpa/inet.h>
27
#ifdef HAVE_ARPA_NAMESER_H
28
# include <arpa/nameser.h>
32
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
33
# include <arpa/nameser_compat.h>
37
#include "ares_ipv6.h"
38
#include "ares_nowarn.h"
39
#include "ares_inet_net_pton.h"
42
const struct ares_in6_addr ares_in6addr_any = { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
45
#ifndef HAVE_INET_NET_PTON
49
* inet_net_pton_ipv4(src, dst, size)
50
* convert IPv4 network number from presentation to network format.
51
* accepts hex octets, hex strings, decimal octets, and /CIDR.
52
* "size" is in bytes and describes "dst".
54
* number of bits, either imputed classfully or specified with /CIDR,
55
* or -1 if some failure occurred (check errno). ENOENT means it was
56
* not an IPv4 network specification.
58
* network byte order assumed. this means 192.5.5.240/28 has
59
* 0b11110000 in its fourth octet.
61
* On Windows we store the error in the thread errno, not
62
* in the winsock error code. This is to avoid loosing the
63
* actual last winsock error. So use macro ERRNO to fetch the
64
* errno this funtion sets when returning (-1), not SOCKERRNO.
66
* Paul Vixie (ISC), June 1996
69
inet_net_pton_ipv4(const char *src, unsigned char *dst, size_t size)
71
static const char xdigits[] = "0123456789abcdef";
72
static const char digits[] = "0123456789";
73
int n, ch, tmp = 0, dirty, bits;
74
const unsigned char *odst = dst;
77
if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
79
&& ISXDIGIT(src[1])) {
80
/* Hexadecimal: Eat nybble string. */
84
src++; /* skip x or X. */
85
while ((ch = *src++) != '\0' && ISASCII(ch) && ISXDIGIT(ch)) {
88
n = aresx_sztosi(strchr(xdigits, ch) - xdigits);
96
*dst++ = (unsigned char) tmp;
100
if (dirty) { /* Odd trailing nybble? */
103
*dst++ = (unsigned char) (tmp << 4);
105
} else if (ISASCII(ch) && ISDIGIT(ch)) {
106
/* Decimal: eat dotted digit string. */
110
n = aresx_sztosi(strchr(digits, ch) - digits);
115
} while ((ch = *src++) != '\0' &&
116
ISASCII(ch) && ISDIGIT(ch));
119
*dst++ = (unsigned char) tmp;
120
if (ch == '\0' || ch == '/')
125
if (!ISASCII(ch) || !ISDIGIT(ch))
132
if (ch == '/' && ISASCII(src[0]) &&
133
ISDIGIT(src[0]) && dst > odst) {
134
/* CIDR width specifier. Nothing can follow it. */
135
ch = *src++; /* Skip over the /. */
138
n = aresx_sztosi(strchr(digits, ch) - digits);
143
} while ((ch = *src++) != '\0' && ISASCII(ch) && ISDIGIT(ch));
148
/* Firey death and destruction unless we prefetched EOS. */
152
/* If nothing was written to the destination, we found no address. */
155
/* If no CIDR spec was given, infer width from net class. */
157
if (*odst >= 240) /* Class E */
159
else if (*odst >= 224) /* Class D */
161
else if (*odst >= 192) /* Class C */
163
else if (*odst >= 128) /* Class B */
167
/* If imputed mask is narrower than specified octets, widen. */
168
if (bits < ((dst - odst) * 8))
169
bits = aresx_sztosi(dst - odst) * 8;
171
* If there are no additional bits specified for a class D
172
* address adjust bits to 4.
174
if (bits == 8 && *odst == 224)
177
/* Extend network to cover the actual mask. */
178
while (bits > ((dst - odst) * 8)) {
195
getbits(const char *src, int *bitsp)
197
static const char digits[] = "0123456789";
204
while ((ch = *src++) != '\0') {
207
pch = strchr(digits, ch);
209
if (n++ != 0 && val == 0) /* no leading zeros */
212
val += aresx_sztosi(pch - digits);
213
if (val > 128) /* range */
226
getv4(const char *src, unsigned char *dst, int *bitsp)
228
static const char digits[] = "0123456789";
229
unsigned char *odst = dst;
236
while ((ch = *src++) != '\0') {
239
pch = strchr(digits, ch);
241
if (n++ != 0 && val == 0) /* no leading zeros */
244
val += aresx_sztoui(pch - digits);
245
if (val > 255) /* range */
249
if (ch == '.' || ch == '/') {
250
if (dst - odst > 3) /* too many octets? */
252
*dst++ = (unsigned char)val;
254
return (getbits(src, bitsp));
263
if (dst - odst > 3) /* too many octets? */
265
*dst = (unsigned char)val;
270
inet_net_pton_ipv6(const char *src, unsigned char *dst, size_t size)
272
static const char xdigits_l[] = "0123456789abcdef",
273
xdigits_u[] = "0123456789ABCDEF";
274
unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
275
const char *xdigits, *curtok;
284
memset((tp = tmp), '\0', NS_IN6ADDRSZ);
285
endp = tp + NS_IN6ADDRSZ;
287
/* Leading :: requires some special handling. */
297
while ((ch = *src++) != '\0') {
300
if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
301
pch = strchr((xdigits = xdigits_u), ch);
304
val |= aresx_sztoui(pch - xdigits);
317
} else if (*src == '\0')
319
if (tp + NS_INT16SZ > endp)
321
*tp++ = (unsigned char)((val >> 8) & 0xff);
322
*tp++ = (unsigned char)(val & 0xff);
328
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
329
getv4(curtok, tp, &bits) > 0) {
333
break; /* '\0' was seen by inet_pton4(). */
335
if (ch == '/' && getbits(src, &bits) > 0)
340
if (tp + NS_INT16SZ > endp)
342
*tp++ = (unsigned char)((val >> 8) & 0xff);
343
*tp++ = (unsigned char)(val & 0xff);
348
words = (bits + 15) / 16;
353
endp = tmp + 2 * words;
355
if (colonp != NULL) {
357
* Since some memmove()'s erroneously fail to handle
358
* overlapping regions, we'll do the shift by hand.
360
const ssize_t n = tp - colonp;
365
for (i = 1; i <= n; i++) {
366
*(endp - i) = *(colonp + n - i);
367
*(colonp + n - i) = 0;
374
bytes = (bits + 7) / 8;
377
memcpy(dst, tmp, bytes);
391
* inet_net_pton(af, src, dst, size)
392
* convert network number from presentation to network format.
393
* accepts hex octets, hex strings, decimal octets, and /CIDR.
394
* "size" is in bytes and describes "dst".
396
* number of bits, either imputed classfully or specified with /CIDR,
397
* or -1 if some failure occurred (check errno). ENOENT means it was
398
* not a valid network specification.
400
* On Windows we store the error in the thread errno, not
401
* in the winsock error code. This is to avoid loosing the
402
* actual last winsock error. So use macro ERRNO to fetch the
403
* errno this funtion sets when returning (-1), not SOCKERRNO.
405
* Paul Vixie (ISC), June 1996
408
ares_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
SET_ERRNO(EAFNOSUPPORT);
421
#endif /* HAVE_INET_NET_PTON */
423
#ifndef HAVE_INET_PTON
424
int ares_inet_pton(int af, const char *src, void *dst)
430
size = sizeof(struct in_addr);
431
else if (af == AF_INET6)
432
size = sizeof(struct ares_in6_addr);
435
SET_ERRNO(EAFNOSUPPORT);
438
result = ares_inet_net_pton(af, src, dst, size);
439
if (result == -1 && ERRNO == ENOENT)
441
return (result > -1 ? 1 : -1);
443
#else /* HAVE_INET_PTON */
444
int ares_inet_pton(int af, const char *src, void *dst)
446
/* just relay this to the underlying function */
447
return inet_pton(af, src, dst);