inet_net_pton.c revision 1.3 1 1.1 christos /*
2 1.1 christos * Copyright (c) 1996,1999 by Internet Software Consortium.
3 1.1 christos *
4 1.1 christos * Permission to use, copy, modify, and distribute this software for any
5 1.1 christos * purpose with or without fee is hereby granted, provided that the above
6 1.1 christos * copyright notice and this permission notice appear in all copies.
7 1.1 christos *
8 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 1.1 christos * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 1.1 christos * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 1.1 christos * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 1.1 christos * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 1.1 christos * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 1.1 christos * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15 1.1 christos * SOFTWARE.
16 1.1 christos */
17 1.1 christos
18 1.1 christos #include <sys/cdefs.h>
19 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
20 1.1 christos #if 0
21 1.1 christos static const char rcsid[] = "Id: inet_net_pton.c,v 1.4.2.1 2002/08/02 02:17:21 marka Exp ";
22 1.1 christos #else
23 1.3 christos __RCSID("$NetBSD: inet_net_pton.c,v 1.3 2012/03/13 21:13:38 christos Exp $");
24 1.1 christos #endif
25 1.1 christos #endif
26 1.1 christos
27 1.1 christos #include "port_before.h"
28 1.1 christos
29 1.1 christos #include "namespace.h"
30 1.1 christos #include <sys/types.h>
31 1.1 christos #include <sys/socket.h>
32 1.1 christos #include <netinet/in.h>
33 1.1 christos #include <arpa/nameser.h>
34 1.1 christos #include <arpa/inet.h>
35 1.1 christos
36 1.1 christos #include <isc/assertions.h>
37 1.3 christos #include <stddef.h>
38 1.1 christos #include <ctype.h>
39 1.1 christos #include <errno.h>
40 1.1 christos #include <stdio.h>
41 1.1 christos #include <string.h>
42 1.1 christos #include <stdlib.h>
43 1.1 christos
44 1.1 christos #include "port_after.h"
45 1.1 christos
46 1.1 christos #ifdef __weak_alias
47 1.1 christos __weak_alias(inet_net_pton,_inet_net_pton)
48 1.1 christos #endif
49 1.1 christos
50 1.1 christos #ifdef SPRINTF_CHAR
51 1.1 christos # define SPRINTF(x) strlen(sprintf/**/x)
52 1.1 christos #else
53 1.1 christos # define SPRINTF(x) ((size_t)sprintf x)
54 1.1 christos #endif
55 1.1 christos
56 1.1 christos
57 1.1 christos /*
58 1.1 christos * static int
59 1.1 christos * inet_net_pton_ipv4(src, dst, size)
60 1.1 christos * convert IPv4 network number from presentation to network format.
61 1.1 christos * accepts hex octets, hex strings, decimal octets, and /CIDR.
62 1.1 christos * "size" is in bytes and describes "dst".
63 1.1 christos * return:
64 1.1 christos * number of bits, either imputed classfully or specified with /CIDR,
65 1.1 christos * or -1 if some failure occurred (check errno). ENOENT means it was
66 1.1 christos * not an IPv4 network specification.
67 1.1 christos * note:
68 1.1 christos * network byte order assumed. this means 192.5.5.240/28 has
69 1.1 christos * 0b11110000 in its fourth octet.
70 1.1 christos * author:
71 1.1 christos * Paul Vixie (ISC), June 1996
72 1.1 christos */
73 1.1 christos static int
74 1.1 christos inet_net_pton_ipv4( const char *src, u_char *dst, size_t size) {
75 1.1 christos static const char xdigits[] = "0123456789abcdef";
76 1.1 christos static const char digits[] = "0123456789";
77 1.3 christos int ch, dirty, bits;
78 1.3 christos ptrdiff_t n, tmp;
79 1.1 christos const u_char *odst = dst;
80 1.1 christos
81 1.3 christos tmp = 0;
82 1.1 christos ch = *src++;
83 1.1 christos if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
84 1.1 christos && isascii((u_char)(src[1]))
85 1.1 christos && isxdigit((u_char)(src[1]))) {
86 1.1 christos /* Hexadecimal: Eat nybble string. */
87 1.1 christos if (size == 0)
88 1.1 christos goto emsgsize;
89 1.1 christos dirty = 0;
90 1.1 christos src++; /* skip x or X. */
91 1.1 christos while ((ch = *src++) != '\0' && isascii((u_char)ch)
92 1.1 christos && isxdigit((u_char)ch)) {
93 1.1 christos if (isupper((u_char)ch))
94 1.1 christos ch = tolower((u_char)ch);
95 1.1 christos n = strchr(xdigits, ch) - xdigits;
96 1.1 christos INSIST(n >= 0 && n <= 15);
97 1.1 christos if (dirty == 0)
98 1.1 christos tmp = n;
99 1.1 christos else
100 1.1 christos tmp = (tmp << 4) | n;
101 1.1 christos if (++dirty == 2) {
102 1.1 christos if (size-- == 0)
103 1.1 christos goto emsgsize;
104 1.1 christos *dst++ = (u_char) tmp;
105 1.1 christos dirty = 0;
106 1.1 christos }
107 1.1 christos }
108 1.1 christos if (dirty) { /* Odd trailing nybble? */
109 1.1 christos if (size-- == 0)
110 1.1 christos goto emsgsize;
111 1.1 christos *dst++ = (u_char) (tmp << 4);
112 1.1 christos }
113 1.1 christos } else if (isascii((u_char)ch) && isdigit((u_char)ch)) {
114 1.1 christos /* Decimal: eat dotted digit string. */
115 1.1 christos for (;;) {
116 1.1 christos tmp = 0;
117 1.1 christos do {
118 1.1 christos n = strchr(digits, ch) - digits;
119 1.1 christos INSIST(n >= 0 && n <= 9);
120 1.1 christos tmp *= 10;
121 1.1 christos tmp += n;
122 1.1 christos if (tmp > 255)
123 1.1 christos goto enoent;
124 1.1 christos } while ((ch = *src++) != '\0' &&
125 1.1 christos isascii((u_char)ch) && isdigit((u_char)ch));
126 1.1 christos if (size-- == 0)
127 1.1 christos goto emsgsize;
128 1.1 christos *dst++ = (u_char) tmp;
129 1.1 christos if (ch == '\0' || ch == '/')
130 1.1 christos break;
131 1.1 christos if (ch != '.')
132 1.1 christos goto enoent;
133 1.1 christos ch = *src++;
134 1.1 christos if (!isascii((u_char)ch) || !isdigit((u_char)ch))
135 1.1 christos goto enoent;
136 1.1 christos }
137 1.1 christos } else
138 1.1 christos goto enoent;
139 1.1 christos
140 1.1 christos bits = -1;
141 1.1 christos if (ch == '/' && isascii((u_char)(src[0])) &&
142 1.1 christos isdigit((u_char)(src[0])) && dst > odst) {
143 1.1 christos /* CIDR width specifier. Nothing can follow it. */
144 1.1 christos ch = *src++; /* Skip over the /. */
145 1.1 christos bits = 0;
146 1.1 christos do {
147 1.1 christos n = strchr(digits, ch) - digits;
148 1.1 christos INSIST(n >= 0 && n <= 9);
149 1.1 christos bits *= 10;
150 1.3 christos bits += (int)n;
151 1.2 christos if (bits > 32)
152 1.2 christos goto emsgsize;
153 1.1 christos } while ((ch = *src++) != '\0' && isascii((u_char)ch)
154 1.1 christos && isdigit((u_char)ch));
155 1.1 christos if (ch != '\0')
156 1.1 christos goto enoent;
157 1.1 christos }
158 1.1 christos
159 1.1 christos /* Firey death and destruction unless we prefetched EOS. */
160 1.1 christos if (ch != '\0')
161 1.1 christos goto enoent;
162 1.1 christos
163 1.1 christos /* If nothing was written to the destination, we found no address. */
164 1.1 christos if (dst == odst)
165 1.1 christos goto enoent;
166 1.1 christos /* If no CIDR spec was given, infer width from net class. */
167 1.1 christos if (bits == -1) {
168 1.1 christos if (*odst >= 240) /* Class E */
169 1.1 christos bits = 32;
170 1.1 christos else if (*odst >= 224) /* Class D */
171 1.1 christos bits = 4;
172 1.1 christos else if (*odst >= 192) /* Class C */
173 1.1 christos bits = 24;
174 1.1 christos else if (*odst >= 128) /* Class B */
175 1.1 christos bits = 16;
176 1.1 christos else /* Class A */
177 1.1 christos bits = 8;
178 1.1 christos /* If imputed mask is narrower than specified octets, widen. */
179 1.1 christos if (bits >= 8 && bits < ((dst - odst) * 8))
180 1.3 christos bits = (int)(dst - odst) * 8;
181 1.1 christos }
182 1.1 christos /* Extend network to cover the actual mask. */
183 1.1 christos while (bits > ((dst - odst) * 8)) {
184 1.1 christos if (size-- == 0)
185 1.1 christos goto emsgsize;
186 1.1 christos *dst++ = '\0';
187 1.1 christos }
188 1.1 christos return (bits);
189 1.1 christos
190 1.1 christos enoent:
191 1.1 christos errno = ENOENT;
192 1.1 christos return (-1);
193 1.1 christos
194 1.1 christos emsgsize:
195 1.1 christos errno = EMSGSIZE;
196 1.1 christos return (-1);
197 1.1 christos }
198 1.1 christos
199 1.1 christos static int
200 1.1 christos getbits(const char *src, int *bitsp) {
201 1.1 christos static const char digits[] = "0123456789";
202 1.1 christos int n;
203 1.1 christos int val;
204 1.1 christos char ch;
205 1.1 christos
206 1.1 christos val = 0;
207 1.1 christos n = 0;
208 1.1 christos while ((ch = *src++) != '\0') {
209 1.1 christos const char *pch;
210 1.1 christos
211 1.1 christos pch = strchr(digits, ch);
212 1.1 christos if (pch != NULL) {
213 1.1 christos if (n++ != 0 && val == 0) /* no leading zeros */
214 1.1 christos return (0);
215 1.1 christos val *= 10;
216 1.3 christos val += (int)(pch - digits);
217 1.1 christos if (val > 128) /* range */
218 1.1 christos return (0);
219 1.1 christos continue;
220 1.1 christos }
221 1.1 christos return (0);
222 1.1 christos }
223 1.1 christos if (n == 0)
224 1.1 christos return (0);
225 1.1 christos *bitsp = val;
226 1.1 christos return (1);
227 1.1 christos }
228 1.1 christos
229 1.1 christos static int
230 1.1 christos getv4(const char *src, u_char *dst, int *bitsp) {
231 1.1 christos static const char digits[] = "0123456789";
232 1.1 christos u_char *odst = dst;
233 1.1 christos int n;
234 1.1 christos u_int val;
235 1.1 christos char ch;
236 1.1 christos
237 1.1 christos val = 0;
238 1.1 christos n = 0;
239 1.1 christos while ((ch = *src++) != '\0') {
240 1.1 christos const char *pch;
241 1.1 christos
242 1.1 christos pch = strchr(digits, ch);
243 1.1 christos if (pch != NULL) {
244 1.1 christos if (n++ != 0 && val == 0) /* no leading zeros */
245 1.1 christos return (0);
246 1.1 christos val *= 10;
247 1.3 christos val += (int)(pch - digits);
248 1.1 christos if (val > 255) /* range */
249 1.1 christos return (0);
250 1.1 christos continue;
251 1.1 christos }
252 1.1 christos if (ch == '.' || ch == '/') {
253 1.1 christos if (dst - odst > 3) /* too many octets? */
254 1.1 christos return (0);
255 1.1 christos *dst++ = val;
256 1.1 christos if (ch == '/')
257 1.1 christos return (getbits(src, bitsp));
258 1.1 christos val = 0;
259 1.1 christos n = 0;
260 1.1 christos continue;
261 1.1 christos }
262 1.1 christos return (0);
263 1.1 christos }
264 1.1 christos if (n == 0)
265 1.1 christos return (0);
266 1.1 christos if (dst - odst > 3) /* too many octets? */
267 1.1 christos return (0);
268 1.1 christos *dst++ = val;
269 1.1 christos return (1);
270 1.1 christos }
271 1.1 christos
272 1.1 christos static int
273 1.1 christos inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) {
274 1.1 christos static const char xdigits_l[] = "0123456789abcdef",
275 1.1 christos xdigits_u[] = "0123456789ABCDEF";
276 1.1 christos u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
277 1.1 christos const char *xdigits, *curtok;
278 1.1 christos int ch, saw_xdigit;
279 1.1 christos u_int val;
280 1.1 christos int digits;
281 1.1 christos int bits;
282 1.1 christos size_t bytes;
283 1.1 christos int words;
284 1.1 christos int ipv4;
285 1.1 christos
286 1.1 christos memset((tp = tmp), '\0', NS_IN6ADDRSZ);
287 1.1 christos endp = tp + NS_IN6ADDRSZ;
288 1.1 christos colonp = NULL;
289 1.1 christos /* Leading :: requires some special handling. */
290 1.1 christos if (*src == ':')
291 1.1 christos if (*++src != ':')
292 1.1 christos goto enoent;
293 1.1 christos curtok = src;
294 1.1 christos saw_xdigit = 0;
295 1.1 christos val = 0;
296 1.1 christos digits = 0;
297 1.1 christos bits = -1;
298 1.1 christos ipv4 = 0;
299 1.1 christos while ((ch = *src++) != '\0') {
300 1.1 christos const char *pch;
301 1.1 christos
302 1.1 christos if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
303 1.1 christos pch = strchr((xdigits = xdigits_u), ch);
304 1.1 christos if (pch != NULL) {
305 1.1 christos val <<= 4;
306 1.3 christos val |= (int)(pch - xdigits);
307 1.1 christos if (++digits > 4)
308 1.1 christos goto enoent;
309 1.1 christos saw_xdigit = 1;
310 1.1 christos continue;
311 1.1 christos }
312 1.1 christos if (ch == ':') {
313 1.1 christos curtok = src;
314 1.1 christos if (!saw_xdigit) {
315 1.1 christos if (colonp)
316 1.1 christos goto enoent;
317 1.1 christos colonp = tp;
318 1.1 christos continue;
319 1.1 christos } else if (*src == '\0')
320 1.1 christos goto enoent;
321 1.1 christos if (tp + NS_INT16SZ > endp)
322 1.1 christos return (0);
323 1.1 christos *tp++ = (u_char) (val >> 8) & 0xff;
324 1.1 christos *tp++ = (u_char) val & 0xff;
325 1.1 christos saw_xdigit = 0;
326 1.1 christos digits = 0;
327 1.1 christos val = 0;
328 1.1 christos continue;
329 1.1 christos }
330 1.1 christos if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
331 1.1 christos getv4(curtok, tp, &bits) > 0) {
332 1.1 christos tp += NS_INADDRSZ;
333 1.1 christos saw_xdigit = 0;
334 1.1 christos ipv4 = 1;
335 1.1 christos break; /* '\0' was seen by inet_pton4(). */
336 1.1 christos }
337 1.1 christos if (ch == '/' && getbits(src, &bits) > 0)
338 1.1 christos break;
339 1.1 christos goto enoent;
340 1.1 christos }
341 1.1 christos if (saw_xdigit) {
342 1.1 christos if (tp + NS_INT16SZ > endp)
343 1.1 christos goto enoent;
344 1.1 christos *tp++ = (u_char) (val >> 8) & 0xff;
345 1.1 christos *tp++ = (u_char) val & 0xff;
346 1.1 christos }
347 1.1 christos if (bits == -1)
348 1.1 christos bits = 128;
349 1.1 christos
350 1.1 christos words = (bits + 15) / 16;
351 1.1 christos if (words < 2)
352 1.1 christos words = 2;
353 1.1 christos if (ipv4)
354 1.1 christos words = 8;
355 1.1 christos endp = tmp + 2 * words;
356 1.1 christos
357 1.1 christos if (colonp != NULL) {
358 1.1 christos /*
359 1.1 christos * Since some memmove()'s erroneously fail to handle
360 1.1 christos * overlapping regions, we'll do the shift by hand.
361 1.1 christos */
362 1.3 christos const ptrdiff_t n = tp - colonp;
363 1.1 christos int i;
364 1.1 christos
365 1.1 christos if (tp == endp)
366 1.1 christos goto enoent;
367 1.1 christos for (i = 1; i <= n; i++) {
368 1.1 christos endp[- i] = colonp[n - i];
369 1.1 christos colonp[n - i] = 0;
370 1.1 christos }
371 1.1 christos tp = endp;
372 1.1 christos }
373 1.1 christos if (tp != endp)
374 1.1 christos goto enoent;
375 1.1 christos
376 1.1 christos bytes = (bits + 7) / 8;
377 1.1 christos if (bytes > size)
378 1.1 christos goto emsgsize;
379 1.1 christos memcpy(dst, tmp, bytes);
380 1.1 christos return (bits);
381 1.1 christos
382 1.1 christos enoent:
383 1.1 christos errno = ENOENT;
384 1.1 christos return (-1);
385 1.1 christos
386 1.1 christos emsgsize:
387 1.1 christos errno = EMSGSIZE;
388 1.1 christos return (-1);
389 1.1 christos }
390 1.1 christos
391 1.1 christos /*
392 1.1 christos * int
393 1.1 christos * inet_net_pton(af, src, dst, size)
394 1.1 christos * convert network number from presentation to network format.
395 1.1 christos * accepts hex octets, hex strings, decimal octets, and /CIDR.
396 1.1 christos * "size" is in bytes and describes "dst".
397 1.1 christos * return:
398 1.1 christos * number of bits, either imputed classfully or specified with /CIDR,
399 1.1 christos * or -1 if some failure occurred (check errno). ENOENT means it was
400 1.1 christos * not a valid network specification.
401 1.1 christos * author:
402 1.1 christos * Paul Vixie (ISC), June 1996
403 1.1 christos */
404 1.1 christos int
405 1.1 christos inet_net_pton(int af, const char *src, void *dst, size_t size) {
406 1.1 christos switch (af) {
407 1.1 christos case AF_INET:
408 1.1 christos return (inet_net_pton_ipv4(src, dst, size));
409 1.1 christos case AF_INET6:
410 1.1 christos return (inet_net_pton_ipv6(src, dst, size));
411 1.1 christos default:
412 1.1 christos errno = EAFNOSUPPORT;
413 1.1 christos return (-1);
414 1.1 christos }
415 1.1 christos }
416