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.
17 * $Id: inet_cidr_pton.c,v 1.6 2005/04/27 04:56:19 sra Exp $
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
26 #include <arpa/inet.h>
35 #include "port_after.h"
37 static int inet_cidr_pton_ipv4(const char *src
, u_char
*dst
,
39 static int inet_cidr_pton_ipv6(const char *src
, u_char
*dst
,
42 static int getbits(const char *, int ipv6
);
46 * inet_cidr_pton(af, src, dst, *bits)
47 * convert network address from presentation to network format.
48 * accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
49 * "dst" is assumed large enough for its "af". "bits" is set to the
50 * /CIDR prefix length, which can have defaults (like /32 for IPv4).
52 * -1 if an error occurred (inspect errno; ENOENT means bad format).
53 * 0 if successful conversion occurred.
55 * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
56 * as called for by inet_net_pton() but it can be a host address with
57 * an included netmask.
59 * Paul Vixie (ISC), October 1998
62 inet_cidr_pton(int af
, const char *src
, void *dst
, int *bits
) {
65 return (inet_cidr_pton_ipv4(src
, dst
, bits
, 0));
67 return (inet_cidr_pton_ipv6(src
, dst
, bits
));
74 static const char digits
[] = "0123456789";
77 inet_cidr_pton_ipv4(const char *src
, u_char
*dst
, int *pbits
, int ipv6
) {
78 const u_char
*odst
= dst
;
82 /* Get the mantissa. */
83 while (ch
= *src
++, (isascii(ch
) && isdigit(ch
))) {
86 n
= strchr(digits
, ch
) - digits
;
87 assert(n
>= 0 && n
<= 9);
92 } while ((ch
= *src
++) != '\0' && isascii(ch
) && isdigit(ch
));
95 *dst
++ = (u_char
) tmp
;
96 if (ch
== '\0' || ch
== '/')
102 /* Get the prefix length if any. */
104 if (ch
== '/' && dst
> odst
) {
105 bits
= getbits(src
, ipv6
);
108 } else if (ch
!= '\0')
111 /* Prefix length can default to /32 only if all four octets spec'd. */
114 bits
= ipv6
? 128 : 32;
119 /* If nothing was written to the destination, we found no address. */
123 /* If prefix length overspecifies mantissa, life is bad. */
124 if (((bits
- (ipv6
? 96 : 0)) / 8) > (dst
- odst
))
127 /* Extend address to four octets. */
144 inet_cidr_pton_ipv6(const char *src
, u_char
*dst
, int *pbits
) {
145 static const char xdigits_l
[] = "0123456789abcdef",
146 xdigits_u
[] = "0123456789ABCDEF";
147 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
148 const char *xdigits
, *curtok
;
153 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
154 endp
= tp
+ NS_IN6ADDRSZ
;
156 /* Leading :: requires some special handling. */
164 while ((ch
= *src
++) != '\0') {
167 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
168 pch
= strchr((xdigits
= xdigits_u
), ch
);
171 val
|= (pch
- xdigits
);
184 } else if (*src
== '\0') {
187 if (tp
+ NS_INT16SZ
> endp
)
189 *tp
++ = (u_char
) (val
>> 8) & 0xff;
190 *tp
++ = (u_char
) val
& 0xff;
195 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
196 inet_cidr_pton_ipv4(curtok
, tp
, &bits
, 1) == 0) {
199 break; /*%< '\\0' was seen by inet_pton4(). */
202 bits
= getbits(src
, 1);
210 if (tp
+ NS_INT16SZ
> endp
)
212 *tp
++ = (u_char
) (val
>> 8) & 0xff;
213 *tp
++ = (u_char
) val
& 0xff;
215 if (colonp
!= NULL
) {
217 * Since some memmove()'s erroneously fail to handle
218 * overlapping regions, we'll do the shift by hand.
220 const int n
= tp
- colonp
;
225 for (i
= 1; i
<= n
; i
++) {
226 endp
[- i
] = colonp
[n
- i
];
232 memcpy(dst
, tmp
, NS_IN6ADDRSZ
);
247 getbits(const char *src
, int ipv6
) {
251 if (*src
== '\0') /*%< syntax */
255 cp
= strchr(digits
, ch
);
256 if (cp
== NULL
) /*%< syntax */
260 if (bits
== 0 && *src
!= '\0') /*%< no leading zeros */
262 if (bits
> (ipv6
? 128 : 32)) /*%< range error */
264 } while (*src
!= '\0');