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