2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1998,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_cidr_pton.c,v 1.2.2.1.8.2 2004/03/17 00:29:46 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(x) assert(x)
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)
50 static int inet_cidr_pton_ipv4 __P((const char *src, u_char *dst,
51 int *bits, int ipv6));
52 static int inet_cidr_pton_ipv6 __P((const char *src, u_char *dst,
55 static int getbits(const char *, int ipv6);
59 * inet_cidr_pton(af, src, dst, *bits)
60 * convert network address from presentation to network format.
61 * accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
62 * "dst" is assumed large enough for its "af". "bits" is set to the
63 * /CIDR prefix length, which can have defaults (like /32 for IPv4).
65 * -1 if an error occurred (inspect errno; ENOENT means bad format).
66 * 0 if successful conversion occurred.
68 * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
69 * as called for by inet_net_pton() but it can be a host address with
70 * an included netmask.
72 * Paul Vixie (ISC), October 1998
75 inet_cidr_pton(int af, const char *src, void *dst, int *bits) {
78 return (inet_cidr_pton_ipv4(src, dst, bits, 0));
80 return (inet_cidr_pton_ipv6(src, dst, bits));
87 static const char digits[] = "0123456789";
90 inet_cidr_pton_ipv4(const char *src, u_char *dst, int *pbits, int ipv6) {
91 const u_char *odst = dst;
95 /* Get the mantissa. */
96 while (ch = *src++, (isascii(ch) && isdigit(ch))) {
99 n = strchr(digits, ch) - digits;
100 INSIST(n >= 0 && n <= 9);
105 } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
108 *dst++ = (u_char) tmp;
109 if (ch == '\0' || ch == '/')
115 /* Get the prefix length if any. */
117 if (ch == '/' && dst > odst) {
118 bits = getbits(src, ipv6);
121 } else if (ch != '\0')
124 /* Prefix length can default to /32 only if all four octets spec'd. */
127 bits = ipv6 ? 128 : 32;
132 /* If nothing was written to the destination, we found no address. */
136 /* If prefix length overspecifies mantissa, life is bad. */
137 if (((bits - (ipv6 ? 96 : 0)) / 8) > (dst - odst))
140 /* Extend address to four octets. */
157 inet_cidr_pton_ipv6(const char *src, u_char *dst, int *pbits) {
158 static const char xdigits_l[] = "0123456789abcdef",
159 xdigits_u[] = "0123456789ABCDEF";
160 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
161 const char *xdigits, *curtok;
166 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
167 endp = tp + NS_IN6ADDRSZ;
169 /* Leading :: requires some special handling. */
177 while ((ch = *src++) != '\0') {
180 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
181 pch = strchr((xdigits = xdigits_u), ch);
184 val |= (pch - xdigits);
197 } else if (*src == '\0') {
200 if (tp + NS_INT16SZ > endp)
202 *tp++ = (u_char) (val >> 8) & 0xff;
203 *tp++ = (u_char) val & 0xff;
208 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
209 inet_cidr_pton_ipv4(curtok, tp, &bits, 1) == 0) {
212 break; /* '\0' was seen by inet_pton4(). */
215 bits = getbits(src, 1);
223 if (tp + NS_INT16SZ > endp)
225 *tp++ = (u_char) (val >> 8) & 0xff;
226 *tp++ = (u_char) val & 0xff;
228 if (colonp != NULL) {
230 * Since some memmove()'s erroneously fail to handle
231 * overlapping regions, we'll do the shift by hand.
233 const int n = tp - colonp;
238 for (i = 1; i <= n; i++) {
239 endp[- i] = colonp[n - i];
245 memcpy(dst, tmp, NS_IN6ADDRSZ);
260 getbits(const char *src, int ipv6) {
264 if (*src == '\0') /* syntax */
268 cp = strchr(digits, ch);
269 if (cp == NULL) /* syntax */
273 if (bits == 0 && *src != '\0') /* no leading zeros */
275 if (bits > (ipv6 ? 128 : 32)) /* range error */
277 } while (*src != '\0');