1 /* $NetBSD: inet_cidr_pton.c,v 1.1.1.4 2009/04/12 16:35:45 christos Exp $ */
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1998,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 #if defined(LIBC_SCCS) && !defined(lint)
23 static const char rcsid
[] = "Id: inet_cidr_pton.c,v 1.6 2005/04/27 04:56:19 sra Exp";
25 __RCSID("$NetBSD: inet_cidr_pton.c,v 1.5 2008/06/21 20:41:48 christos Exp $");
29 #include "port_before.h"
31 #include "namespace.h"
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
36 #include <arpa/inet.h>
38 #include <isc/assertions.h>
45 #include "port_after.h"
48 # define SPRINTF(x) strlen(sprintf/**/x)
50 # define SPRINTF(x) ((size_t)sprintf x)
54 __weak_alias(inet_cidr_pton
,_inet_cidr_pton
)
57 static int inet_cidr_pton_ipv4
__P((const char *src
, u_char
*dst
,
58 int *bits
, int ipv6
));
59 static int inet_cidr_pton_ipv6
__P((const char *src
, u_char
*dst
,
62 static int getbits(const char *, int ipv6
);
66 * inet_cidr_pton(af, src, dst, *bits)
67 * convert network address from presentation to network format.
68 * accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
69 * "dst" is assumed large enough for its "af". "bits" is set to the
70 * /CIDR prefix length, which can have defaults (like /32 for IPv4).
72 * -1 if an error occurred (inspect errno; ENOENT means bad format).
73 * 0 if successful conversion occurred.
75 * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
76 * as called for by inet_net_pton() but it can be a host address with
77 * an included netmask.
79 * Paul Vixie (ISC), October 1998
82 inet_cidr_pton(int af
, const char *src
, void *dst
, int *bits
) {
85 return (inet_cidr_pton_ipv4(src
, dst
, bits
, 0));
87 return (inet_cidr_pton_ipv6(src
, dst
, bits
));
94 static const char digits
[] = "0123456789";
97 inet_cidr_pton_ipv4(const char *src
, u_char
*dst
, int *pbits
, int ipv6
) {
98 const u_char
*odst
= dst
;
102 /* Get the mantissa. */
103 while (ch
= *src
++, (isascii(ch
) && isdigit(ch
))) {
106 n
= strchr(digits
, ch
) - digits
;
107 INSIST(n
>= 0 && n
<= 9);
112 } while ((ch
= *src
++) != '\0' && isascii(ch
) && isdigit(ch
));
115 *dst
++ = (u_char
) tmp
;
116 if (ch
== '\0' || ch
== '/')
122 /* Get the prefix length if any. */
124 if (ch
== '/' && dst
> odst
) {
125 bits
= getbits(src
, ipv6
);
128 } else if (ch
!= '\0')
131 /* Prefix length can default to /32 only if all four octets spec'd. */
134 bits
= ipv6
? 128 : 32;
139 /* If nothing was written to the destination, we found no address. */
143 /* If prefix length overspecifies mantissa, life is bad. */
144 if (((bits
- (ipv6
? 96 : 0)) / 8) > (dst
- odst
))
147 /* Extend address to four octets. */
164 inet_cidr_pton_ipv6(const char *src
, u_char
*dst
, int *pbits
) {
165 static const char xdigits_l
[] = "0123456789abcdef",
166 xdigits_u
[] = "0123456789ABCDEF";
167 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
168 const char *xdigits
, *curtok
;
173 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
174 endp
= tp
+ NS_IN6ADDRSZ
;
176 /* Leading :: requires some special handling. */
184 while ((ch
= *src
++) != '\0') {
187 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
188 pch
= strchr((xdigits
= xdigits_u
), ch
);
191 val
|= (pch
- xdigits
);
204 } else if (*src
== '\0') {
207 if (tp
+ NS_INT16SZ
> endp
)
209 *tp
++ = (u_char
) (val
>> 8) & 0xff;
210 *tp
++ = (u_char
) val
& 0xff;
215 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
216 inet_cidr_pton_ipv4(curtok
, tp
, &bits
, 1) == 0) {
219 break; /*%< '\\0' was seen by inet_pton4(). */
222 bits
= getbits(src
, 1);
230 if (tp
+ NS_INT16SZ
> endp
)
232 *tp
++ = (u_char
) (val
>> 8) & 0xff;
233 *tp
++ = (u_char
) val
& 0xff;
235 if (colonp
!= NULL
) {
237 * Since some memmove()'s erroneously fail to handle
238 * overlapping regions, we'll do the shift by hand.
240 const int n
= tp
- colonp
;
245 for (i
= 1; i
<= n
; i
++) {
246 endp
[- i
] = colonp
[n
- i
];
252 memcpy(dst
, tmp
, NS_IN6ADDRSZ
);
267 getbits(const char *src
, int ipv6
) {
271 if (*src
== '\0') /*%< syntax */
275 cp
= strchr(digits
, ch
);
276 if (cp
== NULL
) /*%< syntax */
280 if (bits
== 0 && *src
!= '\0') /*%< no leading zeros */
282 if (bits
> (ipv6
? 128 : 32)) /*%< range error */
284 } while (*src
!= '\0');