2 * SPDX-License-Identifier: ISC
4 * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1996, 1998, 1999, 2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or 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 WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
26 #include <arpa/inet.h>
35 #include "port_after.h"
38 # define SPRINTF(x) strlen(sprintf/**/x)
40 # define SPRINTF(x) ((size_t)sprintf x)
45 * inet_net_pton_ipv4(src, dst, size)
46 * convert IPv4 network number from presentation to network format.
47 * accepts hex octets, hex strings, decimal octets, and /CIDR.
48 * "size" is in bytes and describes "dst".
50 * number of bits, either imputed classfully or specified with /CIDR,
51 * or -1 if some failure occurred (check errno). ENOENT means it was
52 * not an IPv4 network specification.
54 * network byte order assumed. this means 192.5.5.240/28 has
55 * 0b11110000 in its fourth octet.
57 * Paul Vixie (ISC), June 1996
60 inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) {
61 static const char xdigits[] = "0123456789abcdef";
62 static const char digits[] = "0123456789";
63 int n, ch, tmp = 0, dirty, bits;
64 const u_char *odst = dst;
67 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
68 && isascii((unsigned char)(src[1]))
69 && isxdigit((unsigned char)(src[1]))) {
70 /* Hexadecimal: Eat nybble string. */
74 src++; /*%< skip x or X. */
75 while ((ch = *src++) != '\0' && isascii(ch) && isxdigit(ch)) {
78 n = strchr(xdigits, ch) - xdigits;
79 assert(n >= 0 && n <= 15);
87 *dst++ = (u_char) tmp;
91 if (dirty) { /*%< Odd trailing nybble? */
94 *dst++ = (u_char) (tmp << 4);
96 } else if (isascii(ch) && isdigit(ch)) {
97 /* Decimal: eat dotted digit string. */
101 n = strchr(digits, ch) - digits;
102 assert(n >= 0 && n <= 9);
107 } while ((ch = *src++) != '\0' &&
108 isascii(ch) && isdigit(ch));
111 *dst++ = (u_char) tmp;
112 if (ch == '\0' || ch == '/')
117 if (!isascii(ch) || !isdigit(ch))
124 if (ch == '/' && isascii((unsigned char)(src[0])) &&
125 isdigit((unsigned char)(src[0])) && dst > odst) {
126 /* CIDR width specifier. Nothing can follow it. */
127 ch = *src++; /*%< Skip over the /. */
130 n = strchr(digits, ch) - digits;
131 assert(n >= 0 && n <= 9);
136 } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
141 /* Firey death and destruction unless we prefetched EOS. */
145 /* If nothing was written to the destination, we found no address. */
148 /* If no CIDR spec was given, infer width from net class. */
150 if (*odst >= 240) /*%< Class E */
152 else if (*odst >= 224) /*%< Class D */
154 else if (*odst >= 192) /*%< Class C */
156 else if (*odst >= 128) /*%< Class B */
160 /* If imputed mask is narrower than specified octets, widen. */
161 if (bits < ((dst - odst) * 8))
162 bits = (dst - odst) * 8;
164 * If there are no additional bits specified for a class D
165 * address adjust bits to 4.
167 if (bits == 8 && *odst == 224)
170 /* Extend network to cover the actual mask. */
171 while (bits > ((dst - odst) * 8)) {
188 getbits(const char *src, int *bitsp) {
189 static const char digits[] = "0123456789";
196 while ((ch = *src++) != '\0') {
199 pch = strchr(digits, ch);
201 if (n++ != 0 && val == 0) /*%< no leading zeros */
204 val += (pch - digits);
205 if (val > 128) /*%< range */
218 getv4(const char *src, u_char *dst, int *bitsp) {
219 static const char digits[] = "0123456789";
227 while ((ch = *src++) != '\0') {
230 pch = strchr(digits, ch);
232 if (n++ != 0 && val == 0) /*%< no leading zeros */
235 val += (pch - digits);
236 if (val > 255) /*%< range */
240 if (ch == '.' || ch == '/') {
241 if (dst - odst > 3) /*%< too many octets? */
245 return (getbits(src, bitsp));
254 if (dst - odst > 3) /*%< too many octets? */
261 inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) {
262 static const char xdigits_l[] = "0123456789abcdef",
263 xdigits_u[] = "0123456789ABCDEF";
264 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
265 const char *xdigits, *curtok;
274 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
275 endp = tp + NS_IN6ADDRSZ;
277 /* Leading :: requires some special handling. */
287 while ((ch = *src++) != '\0') {
290 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
291 pch = strchr((xdigits = xdigits_u), ch);
294 val |= (pch - xdigits);
307 } else if (*src == '\0')
309 if (tp + NS_INT16SZ > endp)
311 *tp++ = (u_char) (val >> 8) & 0xff;
312 *tp++ = (u_char) val & 0xff;
318 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
319 getv4(curtok, tp, &bits) > 0) {
323 break; /*%< '\\0' was seen by inet_pton4(). */
325 if (ch == '/' && getbits(src, &bits) > 0)
330 if (tp + NS_INT16SZ > endp)
332 *tp++ = (u_char) (val >> 8) & 0xff;
333 *tp++ = (u_char) val & 0xff;
338 words = (bits + 15) / 16;
343 endp = tmp + 2 * words;
345 if (colonp != NULL) {
347 * Since some memmove()'s erroneously fail to handle
348 * overlapping regions, we'll do the shift by hand.
350 const int n = tp - colonp;
355 for (i = 1; i <= n; i++) {
356 endp[- i] = colonp[n - i];
364 bytes = (bits + 7) / 8;
367 memcpy(dst, tmp, bytes);
381 * inet_net_pton(af, src, dst, size)
382 * convert network number from presentation to network format.
383 * accepts hex octets, hex strings, decimal octets, and /CIDR.
384 * "size" is in bytes and describes "dst".
386 * number of bits, either imputed classfully or specified with /CIDR,
387 * or -1 if some failure occurred (check errno). ENOENT means it was
388 * not a valid network specification.
390 * Paul Vixie (ISC), June 1996
393 inet_net_pton(int af, const char *src, void *dst, size_t size) {
396 return (inet_net_pton_ipv4(src, dst, size));
398 return (inet_net_pton_ipv6(src, dst, size));
400 errno = EAFNOSUPPORT;
406 * Weak aliases for applications that use certain private entry points,
407 * and fail to include <arpa/inet.h>.
410 __weak_reference(__inet_net_pton, inet_net_pton);