1 /* $OpenBSD: inet_net_pton.c,v 1.14 2022/12/27 17:10:06 jmc Exp $ */
4 * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org>
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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
12 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
14 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
16 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
17 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
21 #include "openbsd-compat.h"
23 #if !defined(HAVE_INET_NET_PTON) || defined(HAVE_LIBBSD)
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
37 static int inet_net_pton_ipv4(const char *, u_char *, size_t);
38 static int inet_net_pton_ipv6(const char *, u_char *, size_t);
42 * inet_net_pton(af, src, dst, size)
43 * convert network number from presentation to network format.
44 * accepts hex octets, hex strings, decimal octets, and /CIDR.
45 * "size" is in bytes and describes "dst".
47 * number of bits, either imputed classfully or specified with /CIDR,
48 * or -1 if some failure occurred (check errno). ENOENT means it was
49 * not a valid network specification.
51 * Paul Vixie (ISC), June 1996
54 inet_net_pton(int af, const char *src, void *dst, size_t size)
58 return (inet_net_pton_ipv4(src, dst, size));
60 return (inet_net_pton_ipv6(src, dst, size));
69 * inet_net_pton_ipv4(src, dst, size)
70 * convert IPv4 network number from presentation to network format.
71 * accepts hex octets, hex strings, decimal octets, and /CIDR.
72 * "size" is in bytes and describes "dst".
74 * number of bits, either imputed classfully or specified with /CIDR,
75 * or -1 if some failure occurred (check errno). ENOENT means it was
76 * not an IPv4 network specification.
78 * network byte order assumed. this means 192.5.5.240/28 has
79 * 0x11110000 in its fourth octet.
81 * Paul Vixie (ISC), June 1996
84 inet_net_pton_ipv4(const char *src, u_char *dst, size_t size)
87 xdigits[] = "0123456789abcdef",
88 digits[] = "0123456789";
89 int n, ch, tmp, dirty, bits;
90 const u_char *odst = dst;
92 ch = (unsigned char)*src++;
93 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
94 && isascii((unsigned char)src[1]) && isxdigit((unsigned char)src[1])) {
95 /* Hexadecimal: Eat nybble string. */
99 src++; /* skip x or X. */
100 while ((ch = (unsigned char)*src++) != '\0' &&
101 isascii(ch) && isxdigit(ch)) {
104 n = strchr(xdigits, ch) - xdigits;
105 assert(n >= 0 && n <= 15);
109 tmp = (tmp << 4) | n;
113 *dst++ = (u_char) tmp;
117 if (dirty) { /* Odd trailing nybble? */
120 *dst++ = (u_char) (tmp << 4);
122 } else if (isascii(ch) && isdigit(ch)) {
123 /* Decimal: eat dotted digit string. */
127 n = strchr(digits, ch) - digits;
128 assert(n >= 0 && n <= 9);
133 } while ((ch = (unsigned char)*src++) != '\0' &&
134 isascii(ch) && isdigit(ch));
137 *dst++ = (u_char) tmp;
138 if (ch == '\0' || ch == '/')
142 ch = (unsigned char)*src++;
143 if (!isascii(ch) || !isdigit(ch))
150 if (ch == '/' && isascii((unsigned char)src[0]) &&
151 isdigit((unsigned char)src[0]) && dst > odst) {
152 /* CIDR width specifier. Nothing can follow it. */
153 ch = (unsigned char)*src++; /* Skip over the /. */
156 n = strchr(digits, ch) - digits;
157 assert(n >= 0 && n <= 9);
162 } while ((ch = (unsigned char)*src++) != '\0' &&
163 isascii(ch) && isdigit(ch));
168 /* Fiery death and destruction unless we prefetched EOS. */
172 /* If nothing was written to the destination, we found no address. */
175 /* If no CIDR spec was given, infer width from net class. */
177 if (*odst >= 240) /* Class E */
179 else if (*odst >= 224) /* Class D */
181 else if (*odst >= 192) /* Class C */
183 else if (*odst >= 128) /* Class B */
187 /* If imputed mask is narrower than specified octets, widen. */
188 if (bits < ((dst - odst) * 8))
189 bits = (dst - odst) * 8;
191 /* Extend network to cover the actual mask. */
192 while (bits > ((dst - odst) * 8)) {
210 inet_net_pton_ipv6(const char *src, u_char *dst, size_t size)
216 char buf[INET6_ADDRSTRLEN + sizeof("/128")];
220 if (strlcpy(buf, src, sizeof buf) >= sizeof buf) {
225 sep = strchr(buf, '/');
229 ret = inet_pton(AF_INET6, buf, &in6);
236 bits = strtonum(sep, 0, 128, &errstr);
243 bytes = (bits + 7) / 8;
248 memcpy(dst, &in6.s6_addr, bytes);