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