inet_cidr_pton.c revision 1.5.6.2 1 1.5.6.2 christos /* $NetBSD: inet_cidr_pton.c,v 1.5.6.2 2008/06/21 20:41:49 christos Exp $ */
2 1.5.6.2 christos
3 1.5.6.2 christos /*
4 1.5.6.2 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.5.6.2 christos * Copyright (c) 1998,1999 by Internet Software Consortium.
6 1.5.6.2 christos *
7 1.5.6.2 christos * Permission to use, copy, modify, and distribute this software for any
8 1.5.6.2 christos * purpose with or without fee is hereby granted, provided that the above
9 1.5.6.2 christos * copyright notice and this permission notice appear in all copies.
10 1.5.6.2 christos *
11 1.5.6.2 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.5.6.2 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.5.6.2 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.5.6.2 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.5.6.2 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.5.6.2 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.5.6.2 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.5.6.2 christos */
19 1.5.6.2 christos
20 1.5.6.2 christos #include <sys/cdefs.h>
21 1.5.6.2 christos #if defined(LIBC_SCCS) && !defined(lint)
22 1.5.6.2 christos #if 0
23 1.5.6.2 christos static const char rcsid[] = "Id: inet_cidr_pton.c,v 1.6 2005/04/27 04:56:19 sra Exp";
24 1.5.6.2 christos #else
25 1.5.6.2 christos __RCSID("$NetBSD: inet_cidr_pton.c,v 1.5.6.2 2008/06/21 20:41:49 christos Exp $");
26 1.5.6.2 christos #endif
27 1.5.6.2 christos #endif
28 1.5.6.2 christos
29 1.5.6.2 christos #include "port_before.h"
30 1.5.6.2 christos
31 1.5.6.2 christos #include "namespace.h"
32 1.5.6.2 christos #include <sys/types.h>
33 1.5.6.2 christos #include <sys/socket.h>
34 1.5.6.2 christos #include <netinet/in.h>
35 1.5.6.2 christos #include <arpa/nameser.h>
36 1.5.6.2 christos #include <arpa/inet.h>
37 1.5.6.2 christos
38 1.5.6.2 christos #include <isc/assertions.h>
39 1.5.6.2 christos #include <ctype.h>
40 1.5.6.2 christos #include <errno.h>
41 1.5.6.2 christos #include <stdio.h>
42 1.5.6.2 christos #include <string.h>
43 1.5.6.2 christos #include <stdlib.h>
44 1.5.6.2 christos
45 1.5.6.2 christos #include "port_after.h"
46 1.5.6.2 christos
47 1.5.6.2 christos #ifdef SPRINTF_CHAR
48 1.5.6.2 christos # define SPRINTF(x) strlen(sprintf/**/x)
49 1.5.6.2 christos #else
50 1.5.6.2 christos # define SPRINTF(x) ((size_t)sprintf x)
51 1.5.6.2 christos #endif
52 1.5.6.2 christos
53 1.5.6.2 christos #ifdef __weak_alias
54 1.5.6.2 christos __weak_alias(inet_cidr_pton,_inet_cidr_pton)
55 1.5.6.2 christos #endif
56 1.5.6.2 christos
57 1.5.6.2 christos static int inet_cidr_pton_ipv4 __P((const char *src, u_char *dst,
58 1.5.6.2 christos int *bits, int ipv6));
59 1.5.6.2 christos static int inet_cidr_pton_ipv6 __P((const char *src, u_char *dst,
60 1.5.6.2 christos int *bits));
61 1.5.6.2 christos
62 1.5.6.2 christos static int getbits(const char *, int ipv6);
63 1.5.6.2 christos
64 1.5.6.2 christos /*%
65 1.5.6.2 christos * int
66 1.5.6.2 christos * inet_cidr_pton(af, src, dst, *bits)
67 1.5.6.2 christos * convert network address from presentation to network format.
68 1.5.6.2 christos * accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
69 1.5.6.2 christos * "dst" is assumed large enough for its "af". "bits" is set to the
70 1.5.6.2 christos * /CIDR prefix length, which can have defaults (like /32 for IPv4).
71 1.5.6.2 christos * return:
72 1.5.6.2 christos * -1 if an error occurred (inspect errno; ENOENT means bad format).
73 1.5.6.2 christos * 0 if successful conversion occurred.
74 1.5.6.2 christos * note:
75 1.5.6.2 christos * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
76 1.5.6.2 christos * as called for by inet_net_pton() but it can be a host address with
77 1.5.6.2 christos * an included netmask.
78 1.5.6.2 christos * author:
79 1.5.6.2 christos * Paul Vixie (ISC), October 1998
80 1.5.6.2 christos */
81 1.5.6.2 christos int
82 1.5.6.2 christos inet_cidr_pton(int af, const char *src, void *dst, int *bits) {
83 1.5.6.2 christos switch (af) {
84 1.5.6.2 christos case AF_INET:
85 1.5.6.2 christos return (inet_cidr_pton_ipv4(src, dst, bits, 0));
86 1.5.6.2 christos case AF_INET6:
87 1.5.6.2 christos return (inet_cidr_pton_ipv6(src, dst, bits));
88 1.5.6.2 christos default:
89 1.5.6.2 christos errno = EAFNOSUPPORT;
90 1.5.6.2 christos return (-1);
91 1.5.6.2 christos }
92 1.5.6.2 christos }
93 1.5.6.2 christos
94 1.5.6.2 christos static const char digits[] = "0123456789";
95 1.5.6.2 christos
96 1.5.6.2 christos static int
97 1.5.6.2 christos inet_cidr_pton_ipv4(const char *src, u_char *dst, int *pbits, int ipv6) {
98 1.5.6.2 christos const u_char *odst = dst;
99 1.5.6.2 christos int n, ch, tmp, bits;
100 1.5.6.2 christos size_t size = 4;
101 1.5.6.2 christos
102 1.5.6.2 christos /* Get the mantissa. */
103 1.5.6.2 christos while (ch = *src++, (isascii(ch) && isdigit(ch))) {
104 1.5.6.2 christos tmp = 0;
105 1.5.6.2 christos do {
106 1.5.6.2 christos n = strchr(digits, ch) - digits;
107 1.5.6.2 christos INSIST(n >= 0 && n <= 9);
108 1.5.6.2 christos tmp *= 10;
109 1.5.6.2 christos tmp += n;
110 1.5.6.2 christos if (tmp > 255)
111 1.5.6.2 christos goto enoent;
112 1.5.6.2 christos } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
113 1.5.6.2 christos if (size-- == 0U)
114 1.5.6.2 christos goto emsgsize;
115 1.5.6.2 christos *dst++ = (u_char) tmp;
116 1.5.6.2 christos if (ch == '\0' || ch == '/')
117 1.5.6.2 christos break;
118 1.5.6.2 christos if (ch != '.')
119 1.5.6.2 christos goto enoent;
120 1.5.6.2 christos }
121 1.5.6.2 christos
122 1.5.6.2 christos /* Get the prefix length if any. */
123 1.5.6.2 christos bits = -1;
124 1.5.6.2 christos if (ch == '/' && dst > odst) {
125 1.5.6.2 christos bits = getbits(src, ipv6);
126 1.5.6.2 christos if (bits == -2)
127 1.5.6.2 christos goto enoent;
128 1.5.6.2 christos } else if (ch != '\0')
129 1.5.6.2 christos goto enoent;
130 1.5.6.2 christos
131 1.5.6.2 christos /* Prefix length can default to /32 only if all four octets spec'd. */
132 1.5.6.2 christos if (bits == -1) {
133 1.5.6.2 christos if (dst - odst == 4)
134 1.5.6.2 christos bits = ipv6 ? 128 : 32;
135 1.5.6.2 christos else
136 1.5.6.2 christos goto enoent;
137 1.5.6.2 christos }
138 1.5.6.2 christos
139 1.5.6.2 christos /* If nothing was written to the destination, we found no address. */
140 1.5.6.2 christos if (dst == odst)
141 1.5.6.2 christos goto enoent;
142 1.5.6.2 christos
143 1.5.6.2 christos /* If prefix length overspecifies mantissa, life is bad. */
144 1.5.6.2 christos if (((bits - (ipv6 ? 96 : 0)) / 8) > (dst - odst))
145 1.5.6.2 christos goto enoent;
146 1.5.6.2 christos
147 1.5.6.2 christos /* Extend address to four octets. */
148 1.5.6.2 christos while (size-- > 0U)
149 1.5.6.2 christos *dst++ = 0;
150 1.5.6.2 christos
151 1.5.6.2 christos *pbits = bits;
152 1.5.6.2 christos return (0);
153 1.5.6.2 christos
154 1.5.6.2 christos enoent:
155 1.5.6.2 christos errno = ENOENT;
156 1.5.6.2 christos return (-1);
157 1.5.6.2 christos
158 1.5.6.2 christos emsgsize:
159 1.5.6.2 christos errno = EMSGSIZE;
160 1.5.6.2 christos return (-1);
161 1.5.6.2 christos }
162 1.5.6.2 christos
163 1.5.6.2 christos static int
164 1.5.6.2 christos inet_cidr_pton_ipv6(const char *src, u_char *dst, int *pbits) {
165 1.5.6.2 christos static const char xdigits_l[] = "0123456789abcdef",
166 1.5.6.2 christos xdigits_u[] = "0123456789ABCDEF";
167 1.5.6.2 christos u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
168 1.5.6.2 christos const char *xdigits, *curtok;
169 1.5.6.2 christos int ch, saw_xdigit;
170 1.5.6.2 christos u_int val;
171 1.5.6.2 christos int bits;
172 1.5.6.2 christos
173 1.5.6.2 christos memset((tp = tmp), '\0', NS_IN6ADDRSZ);
174 1.5.6.2 christos endp = tp + NS_IN6ADDRSZ;
175 1.5.6.2 christos colonp = NULL;
176 1.5.6.2 christos /* Leading :: requires some special handling. */
177 1.5.6.2 christos if (*src == ':')
178 1.5.6.2 christos if (*++src != ':')
179 1.5.6.2 christos return (0);
180 1.5.6.2 christos curtok = src;
181 1.5.6.2 christos saw_xdigit = 0;
182 1.5.6.2 christos val = 0;
183 1.5.6.2 christos bits = -1;
184 1.5.6.2 christos while ((ch = *src++) != '\0') {
185 1.5.6.2 christos const char *pch;
186 1.5.6.2 christos
187 1.5.6.2 christos if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
188 1.5.6.2 christos pch = strchr((xdigits = xdigits_u), ch);
189 1.5.6.2 christos if (pch != NULL) {
190 1.5.6.2 christos val <<= 4;
191 1.5.6.2 christos val |= (pch - xdigits);
192 1.5.6.2 christos if (val > 0xffff)
193 1.5.6.2 christos return (0);
194 1.5.6.2 christos saw_xdigit = 1;
195 1.5.6.2 christos continue;
196 1.5.6.2 christos }
197 1.5.6.2 christos if (ch == ':') {
198 1.5.6.2 christos curtok = src;
199 1.5.6.2 christos if (!saw_xdigit) {
200 1.5.6.2 christos if (colonp)
201 1.5.6.2 christos return (0);
202 1.5.6.2 christos colonp = tp;
203 1.5.6.2 christos continue;
204 1.5.6.2 christos } else if (*src == '\0') {
205 1.5.6.2 christos return (0);
206 1.5.6.2 christos }
207 1.5.6.2 christos if (tp + NS_INT16SZ > endp)
208 1.5.6.2 christos return (0);
209 1.5.6.2 christos *tp++ = (u_char) (val >> 8) & 0xff;
210 1.5.6.2 christos *tp++ = (u_char) val & 0xff;
211 1.5.6.2 christos saw_xdigit = 0;
212 1.5.6.2 christos val = 0;
213 1.5.6.2 christos continue;
214 1.5.6.2 christos }
215 1.5.6.2 christos if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
216 1.5.6.2 christos inet_cidr_pton_ipv4(curtok, tp, &bits, 1) == 0) {
217 1.5.6.2 christos tp += NS_INADDRSZ;
218 1.5.6.2 christos saw_xdigit = 0;
219 1.5.6.2 christos break; /*%< '\\0' was seen by inet_pton4(). */
220 1.5.6.2 christos }
221 1.5.6.2 christos if (ch == '/') {
222 1.5.6.2 christos bits = getbits(src, 1);
223 1.5.6.2 christos if (bits == -2)
224 1.5.6.2 christos goto enoent;
225 1.5.6.2 christos break;
226 1.5.6.2 christos }
227 1.5.6.2 christos goto enoent;
228 1.5.6.2 christos }
229 1.5.6.2 christos if (saw_xdigit) {
230 1.5.6.2 christos if (tp + NS_INT16SZ > endp)
231 1.5.6.2 christos goto emsgsize;
232 1.5.6.2 christos *tp++ = (u_char) (val >> 8) & 0xff;
233 1.5.6.2 christos *tp++ = (u_char) val & 0xff;
234 1.5.6.2 christos }
235 1.5.6.2 christos if (colonp != NULL) {
236 1.5.6.2 christos /*
237 1.5.6.2 christos * Since some memmove()'s erroneously fail to handle
238 1.5.6.2 christos * overlapping regions, we'll do the shift by hand.
239 1.5.6.2 christos */
240 1.5.6.2 christos const int n = tp - colonp;
241 1.5.6.2 christos int i;
242 1.5.6.2 christos
243 1.5.6.2 christos if (tp == endp)
244 1.5.6.2 christos goto enoent;
245 1.5.6.2 christos for (i = 1; i <= n; i++) {
246 1.5.6.2 christos endp[- i] = colonp[n - i];
247 1.5.6.2 christos colonp[n - i] = 0;
248 1.5.6.2 christos }
249 1.5.6.2 christos tp = endp;
250 1.5.6.2 christos }
251 1.5.6.2 christos
252 1.5.6.2 christos memcpy(dst, tmp, NS_IN6ADDRSZ);
253 1.5.6.2 christos
254 1.5.6.2 christos *pbits = bits;
255 1.5.6.2 christos return (0);
256 1.5.6.2 christos
257 1.5.6.2 christos enoent:
258 1.5.6.2 christos errno = ENOENT;
259 1.5.6.2 christos return (-1);
260 1.5.6.2 christos
261 1.5.6.2 christos emsgsize:
262 1.5.6.2 christos errno = EMSGSIZE;
263 1.5.6.2 christos return (-1);
264 1.5.6.2 christos }
265 1.5.6.2 christos
266 1.5.6.2 christos static int
267 1.5.6.2 christos getbits(const char *src, int ipv6) {
268 1.5.6.2 christos int bits = 0;
269 1.5.6.2 christos char *cp, ch;
270 1.5.6.2 christos
271 1.5.6.2 christos if (*src == '\0') /*%< syntax */
272 1.5.6.2 christos return (-2);
273 1.5.6.2 christos do {
274 1.5.6.2 christos ch = *src++;
275 1.5.6.2 christos cp = strchr(digits, ch);
276 1.5.6.2 christos if (cp == NULL) /*%< syntax */
277 1.5.6.2 christos return (-2);
278 1.5.6.2 christos bits *= 10;
279 1.5.6.2 christos bits += cp - digits;
280 1.5.6.2 christos if (bits == 0 && *src != '\0') /*%< no leading zeros */
281 1.5.6.2 christos return (-2);
282 1.5.6.2 christos if (bits > (ipv6 ? 128 : 32)) /*%< range error */
283 1.5.6.2 christos return (-2);
284 1.5.6.2 christos } while (*src != '\0');
285 1.5.6.2 christos
286 1.5.6.2 christos return (bits);
287 1.5.6.2 christos }
288 1.5.6.2 christos
289 1.5.6.2 christos /*! \file */
290