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>
32 #define INSIST(cond) assert(cond)
34 #include <isc/assertions.h>
42 #include "port_after.h"
45 # define SPRINTF(x) strlen(sprintf/**/x)
47 # define SPRINTF(x) ((size_t)sprintf x)
52 * inet_net_pton_ipv4(src, dst, size)
53 * convert IPv4 network number from presentation to network format.
54 * accepts hex octets, hex strings, decimal octets, and /CIDR.
55 * "size" is in bytes and describes "dst".
57 * number of bits, either imputed classfully or specified with /CIDR,
58 * or -1 if some failure occurred (check errno). ENOENT means it was
59 * not an IPv4 network specification.
61 * network byte order assumed. this means 192.5.5.240/28 has
62 * 0b11110000 in its fourth octet.
64 * Paul Vixie (ISC), June 1996
67 inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) {
68 static const char xdigits[] = "0123456789abcdef";
69 static const char digits[] = "0123456789";
70 int n, ch, tmp = 0, dirty, bits;
71 const u_char *odst = dst;
74 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
75 && isascii((unsigned char)(src[1]))
76 && isxdigit((unsigned char)(src[1]))) {
77 /* Hexadecimal: Eat nybble string. */
81 src++; /* skip x or X. */
82 while ((ch = *src++) != '\0' && isascii(ch) && isxdigit(ch)) {
85 n = strchr(xdigits, ch) - xdigits;
86 INSIST(n >= 0 && n <= 15);
94 *dst++ = (u_char) tmp;
98 if (dirty) { /* Odd trailing nybble? */
101 *dst++ = (u_char) (tmp << 4);
103 } else if (isascii(ch) && isdigit(ch)) {
104 /* Decimal: eat dotted digit string. */
108 n = strchr(digits, ch) - digits;
109 INSIST(n >= 0 && n <= 9);
114 } while ((ch = *src++) != '\0' &&
115 isascii(ch) && isdigit(ch));
118 *dst++ = (u_char) tmp;
119 if (ch == '\0' || ch == '/')
124 if (!isascii(ch) || !isdigit(ch))
131 if (ch == '/' && isascii((unsigned char)(src[0])) &&
132 isdigit((unsigned char)(src[0])) && dst > odst) {
133 /* CIDR width specifier. Nothing can follow it. */
134 ch = *src++; /* Skip over the /. */
137 n = strchr(digits, ch) - digits;
138 INSIST(n >= 0 && n <= 9);
141 } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
148 /* Firey death and destruction unless we prefetched EOS. */
152 /* If nothing was written to the destination, we found no address. */
155 /* If no CIDR spec was given, infer width from net class. */
157 if (*odst >= 240) /* Class E */
159 else if (*odst >= 224) /* Class D */
161 else if (*odst >= 192) /* Class C */
163 else if (*odst >= 128) /* Class B */
167 /* If imputed mask is narrower than specified octets, widen. */
168 if (bits < ((dst - odst) * 8))
169 bits = (dst - odst) * 8;
171 * If there are no additional bits specified for a class D
172 * address adjust bits to 4.
174 if (bits == 8 && *odst == 224)
177 /* Extend network to cover the actual mask. */
178 while (bits > ((dst - odst) * 8)) {
195 getbits(const char *src, int *bitsp) {
196 static const char digits[] = "0123456789";
203 while ((ch = *src++) != '\0') {
206 pch = strchr(digits, ch);
208 if (n++ != 0 && val == 0) /* no leading zeros */
211 val += (pch - digits);
212 if (val > 128) /* range */
225 getv4(const char *src, u_char *dst, int *bitsp) {
226 static const char digits[] = "0123456789";
234 while ((ch = *src++) != '\0') {
237 pch = strchr(digits, ch);
239 if (n++ != 0 && val == 0) /* no leading zeros */
242 val += (pch - digits);
243 if (val > 255) /* range */
247 if (ch == '.' || ch == '/') {
248 if (dst - odst > 3) /* too many octets? */
252 return (getbits(src, bitsp));
261 if (dst - odst > 3) /* too many octets? */
268 inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) {
269 static const char xdigits_l[] = "0123456789abcdef",
270 xdigits_u[] = "0123456789ABCDEF";
271 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
272 const char *xdigits, *curtok;
281 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
282 endp = tp + NS_IN6ADDRSZ;
284 /* Leading :: requires some special handling. */
294 while ((ch = *src++) != '\0') {
297 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
298 pch = strchr((xdigits = xdigits_u), ch);
301 val |= (pch - xdigits);
314 } else if (*src == '\0')
316 if (tp + NS_INT16SZ > endp)
318 *tp++ = (u_char) (val >> 8) & 0xff;
319 *tp++ = (u_char) val & 0xff;
325 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
326 getv4(curtok, tp, &bits) > 0) {
330 break; /* '\0' was seen by inet_pton4(). */
332 if (ch == '/' && getbits(src, &bits) > 0)
337 if (tp + NS_INT16SZ > endp)
339 *tp++ = (u_char) (val >> 8) & 0xff;
340 *tp++ = (u_char) val & 0xff;
345 words = (bits + 15) / 16;
350 endp = tmp + 2 * words;
352 if (colonp != NULL) {
354 * Since some memmove()'s erroneously fail to handle
355 * overlapping regions, we'll do the shift by hand.
357 const int n = tp - colonp;
362 for (i = 1; i <= n; i++) {
363 endp[- i] = colonp[n - i];
371 bytes = (bits + 7) / 8;
374 memcpy(dst, tmp, bytes);
388 * inet_net_pton(af, src, dst, size)
389 * convert network number from presentation to network format.
390 * accepts hex octets, hex strings, decimal octets, and /CIDR.
391 * "size" is in bytes and describes "dst".
393 * number of bits, either imputed classfully or specified with /CIDR,
394 * or -1 if some failure occurred (check errno). ENOENT means it was
395 * not a valid network specification.
397 * Paul Vixie (ISC), June 1996
400 inet_net_pton(int af, const char *src, void *dst, size_t size) {
403 return (inet_net_pton_ipv4(src, dst, size));
405 return (inet_net_pton_ipv6(src, dst, size));
407 errno = EAFNOSUPPORT;
414 * Weak aliases for applications that use certain private entry points,
415 * and fail to include <arpa/inet.h>.
418 __weak_reference(__inet_net_pton, inet_net_pton);