2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: inet_net_pton.c,v 1.4.2.1.8.2 2004/03/17 00:29:47 marka Exp $";
22 #include "port_before.h"
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
28 #include <arpa/inet.h>
30 #include <isc/assertions.h>
37 #include "port_after.h"
40 # define SPRINTF(x) strlen(sprintf/**/x)
42 # define SPRINTF(x) ((size_t)sprintf x)
47 * inet_net_pton_ipv4(src, dst, size)
48 * convert IPv4 network number from presentation to network format.
49 * accepts hex octets, hex strings, decimal octets, and /CIDR.
50 * "size" is in bytes and describes "dst".
52 * number of bits, either imputed classfully or specified with /CIDR,
53 * or -1 if some failure occurred (check errno). ENOENT means it was
54 * not an IPv4 network specification.
56 * network byte order assumed. this means 192.5.5.240/28 has
57 * 0b11110000 in its fourth octet.
59 * Paul Vixie (ISC), June 1996
62 inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) {
63 static const char xdigits[] = "0123456789abcdef";
64 static const char digits[] = "0123456789";
65 int n, ch, tmp = 0, dirty, bits;
66 const u_char *odst = dst;
69 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
70 && isascii((unsigned char)(src[1]))
71 && isxdigit((unsigned char)(src[1]))) {
72 /* Hexadecimal: Eat nybble string. */
76 src++; /* skip x or X. */
77 while ((ch = *src++) != '\0' && isascii(ch) && isxdigit(ch)) {
80 n = strchr(xdigits, ch) - xdigits;
81 INSIST(n >= 0 && n <= 15);
89 *dst++ = (u_char) tmp;
93 if (dirty) { /* Odd trailing nybble? */
96 *dst++ = (u_char) (tmp << 4);
98 } else if (isascii(ch) && isdigit(ch)) {
99 /* Decimal: eat dotted digit string. */
103 n = strchr(digits, ch) - digits;
104 INSIST(n >= 0 && n <= 9);
109 } while ((ch = *src++) != '\0' &&
110 isascii(ch) && isdigit(ch));
113 *dst++ = (u_char) tmp;
114 if (ch == '\0' || ch == '/')
119 if (!isascii(ch) || !isdigit(ch))
126 if (ch == '/' && isascii((unsigned char)(src[0])) &&
127 isdigit((unsigned char)(src[0])) && dst > odst) {
128 /* CIDR width specifier. Nothing can follow it. */
129 ch = *src++; /* Skip over the /. */
132 n = strchr(digits, ch) - digits;
133 INSIST(n >= 0 && n <= 9);
136 } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
143 /* Firey death and destruction unless we prefetched EOS. */
147 /* If nothing was written to the destination, we found no address. */
150 /* If no CIDR spec was given, infer width from net class. */
152 if (*odst >= 240) /* Class E */
154 else if (*odst >= 224) /* Class D */
156 else if (*odst >= 192) /* Class C */
158 else if (*odst >= 128) /* Class B */
162 /* If imputed mask is narrower than specified octets, widen. */
163 if (bits < ((dst - odst) * 8))
164 bits = (dst - odst) * 8;
166 * If there are no additional bits specified for a class D
167 * address adjust bits to 4.
169 if (bits == 8 && *odst == 224)
172 /* Extend network to cover the actual mask. */
173 while (bits > ((dst - odst) * 8)) {
190 getbits(const char *src, int *bitsp) {
191 static const char digits[] = "0123456789";
198 while ((ch = *src++) != '\0') {
201 pch = strchr(digits, ch);
203 if (n++ != 0 && val == 0) /* no leading zeros */
206 val += (pch - digits);
207 if (val > 128) /* range */
220 getv4(const char *src, u_char *dst, int *bitsp) {
221 static const char digits[] = "0123456789";
229 while ((ch = *src++) != '\0') {
232 pch = strchr(digits, ch);
234 if (n++ != 0 && val == 0) /* no leading zeros */
237 val += (pch - digits);
238 if (val > 255) /* range */
242 if (ch == '.' || ch == '/') {
243 if (dst - odst > 3) /* too many octets? */
247 return (getbits(src, bitsp));
256 if (dst - odst > 3) /* too many octets? */
263 inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) {
264 static const char xdigits_l[] = "0123456789abcdef",
265 xdigits_u[] = "0123456789ABCDEF";
266 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
267 const char *xdigits, *curtok;
276 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
277 endp = tp + NS_IN6ADDRSZ;
279 /* Leading :: requires some special handling. */
289 while ((ch = *src++) != '\0') {
292 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
293 pch = strchr((xdigits = xdigits_u), ch);
296 val |= (pch - xdigits);
309 } else if (*src == '\0')
311 if (tp + NS_INT16SZ > endp)
313 *tp++ = (u_char) (val >> 8) & 0xff;
314 *tp++ = (u_char) val & 0xff;
320 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
321 getv4(curtok, tp, &bits) > 0) {
325 break; /* '\0' was seen by inet_pton4(). */
327 if (ch == '/' && getbits(src, &bits) > 0)
332 if (tp + NS_INT16SZ > endp)
334 *tp++ = (u_char) (val >> 8) & 0xff;
335 *tp++ = (u_char) val & 0xff;
340 words = (bits + 15) / 16;
345 endp = tmp + 2 * words;
347 if (colonp != NULL) {
349 * Since some memmove()'s erroneously fail to handle
350 * overlapping regions, we'll do the shift by hand.
352 const int n = tp - colonp;
357 for (i = 1; i <= n; i++) {
358 endp[- i] = colonp[n - i];
366 bytes = (bits + 7) / 8;
369 memcpy(dst, tmp, bytes);
383 * inet_net_pton(af, src, dst, size)
384 * convert network number from presentation to network format.
385 * accepts hex octets, hex strings, decimal octets, and /CIDR.
386 * "size" is in bytes and describes "dst".
388 * number of bits, either imputed classfully or specified with /CIDR,
389 * or -1 if some failure occurred (check errno). ENOENT means it was
390 * not a valid network specification.
392 * Paul Vixie (ISC), June 1996
395 inet_net_pton(int af, const char *src, void *dst, size_t size) {
398 return (inet_net_pton_ipv4(src, dst, size));
400 return (inet_net_pton_ipv6(src, dst, size));
402 errno = EAFNOSUPPORT;