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 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid[] = "$Id: inet_net_pton.c,v 1.10 2008/11/14 02:36:51 marka Exp $";
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
26 #include "port_before.h"
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32 #include <arpa/inet.h>
41 #include "port_after.h"
44 # define SPRINTF(x) strlen(sprintf/**/x)
46 # define SPRINTF(x) ((size_t)sprintf x)
51 * inet_net_pton_ipv4(src, dst, size)
52 * convert IPv4 network number from presentation to network format.
53 * accepts hex octets, hex strings, decimal octets, and /CIDR.
54 * "size" is in bytes and describes "dst".
56 * number of bits, either imputed classfully or specified with /CIDR,
57 * or -1 if some failure occurred (check errno). ENOENT means it was
58 * not an IPv4 network specification.
60 * network byte order assumed. this means 192.5.5.240/28 has
61 * 0b11110000 in its fourth octet.
63 * Paul Vixie (ISC), June 1996
66 inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) {
67 static const char xdigits[] = "0123456789abcdef";
68 static const char digits[] = "0123456789";
69 int n, ch, tmp = 0, dirty, bits;
70 const u_char *odst = dst;
73 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
74 && isascii((unsigned char)(src[1]))
75 && isxdigit((unsigned char)(src[1]))) {
76 /* Hexadecimal: Eat nybble string. */
80 src++; /*%< skip x or X. */
81 while ((ch = *src++) != '\0' && isascii(ch) && isxdigit(ch)) {
84 n = strchr(xdigits, ch) - xdigits;
85 assert(n >= 0 && n <= 15);
93 *dst++ = (u_char) tmp;
97 if (dirty) { /*%< Odd trailing nybble? */
100 *dst++ = (u_char) (tmp << 4);
102 } else if (isascii(ch) && isdigit(ch)) {
103 /* Decimal: eat dotted digit string. */
107 n = strchr(digits, ch) - digits;
108 assert(n >= 0 && n <= 9);
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;
137 assert(n >= 0 && n <= 9);
142 } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
147 /* Firey death and destruction unless we prefetched EOS. */
151 /* If nothing was written to the destination, we found no address. */
154 /* If no CIDR spec was given, infer width from net class. */
156 if (*odst >= 240) /*%< Class E */
158 else if (*odst >= 224) /*%< Class D */
160 else if (*odst >= 192) /*%< Class C */
162 else if (*odst >= 128) /*%< Class B */
166 /* If imputed mask is narrower than specified octets, widen. */
167 if (bits < ((dst - odst) * 8))
168 bits = (dst - odst) * 8;
170 * If there are no additional bits specified for a class D
171 * address adjust bits to 4.
173 if (bits == 8 && *odst == 224)
176 /* Extend network to cover the actual mask. */
177 while (bits > ((dst - odst) * 8)) {
194 getbits(const char *src, int *bitsp) {
195 static const char digits[] = "0123456789";
202 while ((ch = *src++) != '\0') {
205 pch = strchr(digits, ch);
207 if (n++ != 0 && val == 0) /*%< no leading zeros */
210 val += (pch - digits);
211 if (val > 128) /*%< range */
224 getv4(const char *src, u_char *dst, int *bitsp) {
225 static const char digits[] = "0123456789";
233 while ((ch = *src++) != '\0') {
236 pch = strchr(digits, ch);
238 if (n++ != 0 && val == 0) /*%< no leading zeros */
241 val += (pch - digits);
242 if (val > 255) /*%< range */
246 if (ch == '.' || ch == '/') {
247 if (dst - odst > 3) /*%< too many octets? */
251 return (getbits(src, bitsp));
260 if (dst - odst > 3) /*%< too many octets? */
267 inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) {
268 static const char xdigits_l[] = "0123456789abcdef",
269 xdigits_u[] = "0123456789ABCDEF";
270 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
271 const char *xdigits, *curtok;
280 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
281 endp = tp + NS_IN6ADDRSZ;
283 /* Leading :: requires some special handling. */
293 while ((ch = *src++) != '\0') {
296 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
297 pch = strchr((xdigits = xdigits_u), ch);
300 val |= (pch - xdigits);
313 } else if (*src == '\0')
315 if (tp + NS_INT16SZ > endp)
317 *tp++ = (u_char) (val >> 8) & 0xff;
318 *tp++ = (u_char) val & 0xff;
324 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
325 getv4(curtok, tp, &bits) > 0) {
329 break; /*%< '\\0' was seen by inet_pton4(). */
331 if (ch == '/' && getbits(src, &bits) > 0)
336 if (tp + NS_INT16SZ > endp)
338 *tp++ = (u_char) (val >> 8) & 0xff;
339 *tp++ = (u_char) val & 0xff;
344 words = (bits + 15) / 16;
349 endp = tmp + 2 * words;
351 if (colonp != NULL) {
353 * Since some memmove()'s erroneously fail to handle
354 * overlapping regions, we'll do the shift by hand.
356 const int n = tp - colonp;
361 for (i = 1; i <= n; i++) {
362 endp[- i] = colonp[n - i];
370 bytes = (bits + 7) / 8;
373 memcpy(dst, tmp, bytes);
387 * inet_net_pton(af, src, dst, size)
388 * convert network number from presentation to network format.
389 * accepts hex octets, hex strings, decimal octets, and /CIDR.
390 * "size" is in bytes and describes "dst".
392 * number of bits, either imputed classfully or specified with /CIDR,
393 * or -1 if some failure occurred (check errno). ENOENT means it was
394 * not a valid network specification.
396 * Paul Vixie (ISC), June 1996
399 inet_net_pton(int af, const char *src, void *dst, size_t size) {
402 return (inet_net_pton_ipv4(src, dst, size));
404 return (inet_net_pton_ipv6(src, dst, size));
406 errno = EAFNOSUPPORT;
412 * Weak aliases for applications that use certain private entry points,
413 * and fail to include <arpa/inet.h>.
416 __weak_reference(__inet_net_pton, inet_net_pton);