Blob


1 /* $OpenBSD: inet_net_pton.c,v 1.14 2022/12/27 17:10:06 jmc Exp $ */
3 /*
4 * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org>
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
6 *
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.
10 *
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
18 * SOFTWARE.
19 */
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>
30 #include <assert.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.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);
40 /*
41 * static int
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".
46 * return:
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.
50 * author:
51 * Paul Vixie (ISC), June 1996
52 */
53 int
54 inet_net_pton(int af, const char *src, void *dst, size_t size)
55 {
56 switch (af) {
57 case AF_INET:
58 return (inet_net_pton_ipv4(src, dst, size));
59 case AF_INET6:
60 return (inet_net_pton_ipv6(src, dst, size));
61 default:
62 errno = EAFNOSUPPORT;
63 return (-1);
64 }
65 }
67 /*
68 * static int
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".
73 * return:
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.
77 * note:
78 * network byte order assumed. this means 192.5.5.240/28 has
79 * 0x11110000 in its fourth octet.
80 * author:
81 * Paul Vixie (ISC), June 1996
82 */
83 static int
84 inet_net_pton_ipv4(const char *src, u_char *dst, size_t size)
85 {
86 static const char
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. */
96 if (size == 0)
97 goto emsgsize;
98 tmp = 0, dirty = 0;
99 src++; /* skip x or X. */
100 while ((ch = (unsigned char)*src++) != '\0' &&
101 isascii(ch) && isxdigit(ch)) {
102 if (isupper(ch))
103 ch = tolower(ch);
104 n = strchr(xdigits, ch) - xdigits;
105 assert(n >= 0 && n <= 15);
106 if (dirty == 0)
107 tmp = n;
108 else
109 tmp = (tmp << 4) | n;
110 if (++dirty == 2) {
111 if (size-- == 0)
112 goto emsgsize;
113 *dst++ = (u_char) tmp;
114 dirty = 0;
117 if (dirty) { /* Odd trailing nybble? */
118 if (size-- == 0)
119 goto emsgsize;
120 *dst++ = (u_char) (tmp << 4);
122 } else if (isascii(ch) && isdigit(ch)) {
123 /* Decimal: eat dotted digit string. */
124 for (;;) {
125 tmp = 0;
126 do {
127 n = strchr(digits, ch) - digits;
128 assert(n >= 0 && n <= 9);
129 tmp *= 10;
130 tmp += n;
131 if (tmp > 255)
132 goto enoent;
133 } while ((ch = (unsigned char)*src++) != '\0' &&
134 isascii(ch) && isdigit(ch));
135 if (size-- == 0)
136 goto emsgsize;
137 *dst++ = (u_char) tmp;
138 if (ch == '\0' || ch == '/')
139 break;
140 if (ch != '.')
141 goto enoent;
142 ch = (unsigned char)*src++;
143 if (!isascii(ch) || !isdigit(ch))
144 goto enoent;
146 } else
147 goto enoent;
149 bits = -1;
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 /. */
154 bits = 0;
155 do {
156 n = strchr(digits, ch) - digits;
157 assert(n >= 0 && n <= 9);
158 bits *= 10;
159 bits += n;
160 if (bits > 32)
161 goto emsgsize;
162 } while ((ch = (unsigned char)*src++) != '\0' &&
163 isascii(ch) && isdigit(ch));
164 if (ch != '\0')
165 goto enoent;
168 /* Fiery death and destruction unless we prefetched EOS. */
169 if (ch != '\0')
170 goto enoent;
172 /* If nothing was written to the destination, we found no address. */
173 if (dst == odst)
174 goto enoent;
175 /* If no CIDR spec was given, infer width from net class. */
176 if (bits == -1) {
177 if (*odst >= 240) /* Class E */
178 bits = 32;
179 else if (*odst >= 224) /* Class D */
180 bits = 4;
181 else if (*odst >= 192) /* Class C */
182 bits = 24;
183 else if (*odst >= 128) /* Class B */
184 bits = 16;
185 else /* Class A */
186 bits = 8;
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)) {
193 if (size-- == 0)
194 goto emsgsize;
195 *dst++ = '\0';
197 return (bits);
199 enoent:
200 errno = ENOENT;
201 return (-1);
203 emsgsize:
204 errno = EMSGSIZE;
205 return (-1);
209 static int
210 inet_net_pton_ipv6(const char *src, u_char *dst, size_t size)
212 struct in6_addr in6;
213 int ret;
214 int bits;
215 size_t bytes;
216 char buf[INET6_ADDRSTRLEN + sizeof("/128")];
217 char *sep;
218 const char *errstr;
220 if (strlcpy(buf, src, sizeof buf) >= sizeof buf) {
221 errno = EMSGSIZE;
222 return (-1);
225 sep = strchr(buf, '/');
226 if (sep != NULL)
227 *sep++ = '\0';
229 ret = inet_pton(AF_INET6, buf, &in6);
230 if (ret != 1)
231 return (-1);
233 if (sep == NULL)
234 bits = 128;
235 else {
236 bits = strtonum(sep, 0, 128, &errstr);
237 if (errstr) {
238 errno = EINVAL;
239 return (-1);
243 bytes = (bits + 7) / 8;
244 if (bytes > size) {
245 errno = EMSGSIZE;
246 return (-1);
248 memcpy(dst, &in6.s6_addr, bytes);
249 return (bits);
252 #endif