Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 432 lines (397 sloc) 9.777 kb
ffb4cf55 » Dominick Meglio
2005-04-05 Provided implementations of inet_net_pton and inet_pton from BIND for…
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 *
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.
8 *
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.
16 */
17
18 #include "setup.h"
19
20
21 #include <sys/types.h>
3d90cb25 »
2005-04-05 with these changes, it builds on my win32 cross-compiler
22
23 #if defined(WIN32) && !defined(WATT32)
24 #include "nameser.h"
25 #else
26
27 #ifdef HAVE_SYS_SOCKET_H
ffb4cf55 » Dominick Meglio
2005-04-05 Provided implementations of inet_net_pton and inet_pton from BIND for…
28 #include <sys/socket.h>
3d90cb25 »
2005-04-05 with these changes, it builds on my win32 cross-compiler
29 #endif
30 #ifdef HAVE_NETINET_IN_H
ffb4cf55 » Dominick Meglio
2005-04-05 Provided implementations of inet_net_pton and inet_pton from BIND for…
31 #include <netinet/in.h>
3d90cb25 »
2005-04-05 with these changes, it builds on my win32 cross-compiler
32 #endif
33 #ifdef HAVE_ARPA_NAMESER_H
ffb4cf55 » Dominick Meglio
2005-04-05 Provided implementations of inet_net_pton and inet_pton from BIND for…
34 #include <arpa/nameser.h>
3d90cb25 »
2005-04-05 with these changes, it builds on my win32 cross-compiler
35 #endif
36 #ifdef HAVE_ARPA_INET_H
ffb4cf55 » Dominick Meglio
2005-04-05 Provided implementations of inet_net_pton and inet_pton from BIND for…
37 #include <arpa/inet.h>
3d90cb25 »
2005-04-05 with these changes, it builds on my win32 cross-compiler
38 #endif
39
40 #endif
ffb4cf55 » Dominick Meglio
2005-04-05 Provided implementations of inet_net_pton and inet_pton from BIND for…
41
42 #include <ctype.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47
e10e4843 »
2005-04-06 moved the *_inet_pton protos to inet_net_pton.h instead
48 #include "inet_net_pton.h"
49
ffb4cf55 » Dominick Meglio
2005-04-05 Provided implementations of inet_net_pton and inet_pton from BIND for…
50 #if !defined(HAVE_INET_NET_PTON) || !defined(HAVE_INET_NET_PTON_IPV6) || \
51 !defined(HAVE_INET_PTON) || !defined(HAVE_INET_PTON_IPV6)
52
53 /*
54 * static int
55 * inet_net_pton_ipv4(src, dst, size)
56 * convert IPv4 network number from presentation to network format.
57 * accepts hex octets, hex strings, decimal octets, and /CIDR.
58 * "size" is in bytes and describes "dst".
59 * return:
60 * number of bits, either imputed classfully or specified with /CIDR,
61 * or -1 if some failure occurred (check errno). ENOENT means it was
62 * not an IPv4 network specification.
63 * note:
64 * network byte order assumed. this means 192.5.5.240/28 has
65 * 0b11110000 in its fourth octet.
66 * author:
67 * Paul Vixie (ISC), June 1996
68 */
69 static int
70 inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) {
71 static const char xdigits[] = "0123456789abcdef";
72 static const char digits[] = "0123456789";
73 int n, ch, tmp = 0, dirty, bits;
74 const u_char *odst = dst;
75
76 ch = *src++;
77 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
78 && isascii((unsigned char)(src[1]))
79 && isxdigit((unsigned char)(src[1]))) {
80 /* Hexadecimal: Eat nybble string. */
81 if (size <= 0U)
82 goto emsgsize;
83 dirty = 0;
84 src++; /* skip x or X. */
85 while ((ch = *src++) != '\0' && isascii(ch) && isxdigit(ch)) {
86 if (isupper(ch))
87 ch = tolower(ch);
88 n = strchr(xdigits, ch) - xdigits;
89 if (dirty == 0)
90 tmp = n;
91 else
92 tmp = (tmp << 4) | n;
93 if (++dirty == 2) {
94 if (size-- <= 0U)
95 goto emsgsize;
96 *dst++ = (u_char) tmp;
97 dirty = 0;
98 }
99 }
100 if (dirty) { /* Odd trailing nybble? */
101 if (size-- <= 0U)
102 goto emsgsize;
103 *dst++ = (u_char) (tmp << 4);
104 }
105 } else if (isascii(ch) && isdigit(ch)) {
106 /* Decimal: eat dotted digit string. */
107 for (;;) {
108 tmp = 0;
109 do {
110 n = strchr(digits, ch) - digits;
111 tmp *= 10;
112 tmp += n;
113 if (tmp > 255)
114 goto enoent;
115 } while ((ch = *src++) != '\0' &&
116 isascii(ch) && isdigit(ch));
117 if (size-- <= 0U)
118 goto emsgsize;
119 *dst++ = (u_char) tmp;
120 if (ch == '\0' || ch == '/')
121 break;
122 if (ch != '.')
123 goto enoent;
124 ch = *src++;
125 if (!isascii(ch) || !isdigit(ch))
126 goto enoent;
127 }
128 } else
129 goto enoent;
130
131 bits = -1;
132 if (ch == '/' && isascii((unsigned char)(src[0])) &&
133 isdigit((unsigned char)(src[0])) && dst > odst) {
134 /* CIDR width specifier. Nothing can follow it. */
135 ch = *src++; /* Skip over the /. */
136 bits = 0;
137 do {
138 n = strchr(digits, ch) - digits;
139 bits *= 10;
140 bits += n;
141 } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
142 if (ch != '\0')
143 goto enoent;
144 if (bits > 32)
145 goto emsgsize;
146 }
147
148 /* Firey death and destruction unless we prefetched EOS. */
149 if (ch != '\0')
150 goto enoent;
151
152 /* If nothing was written to the destination, we found no address. */
153 if (dst == odst)
154 goto enoent;
155 /* If no CIDR spec was given, infer width from net class. */
156 if (bits == -1) {
157 if (*odst >= 240) /* Class E */
158 bits = 32;
159 else if (*odst >= 224) /* Class D */
160 bits = 8;
161 else if (*odst >= 192) /* Class C */
162 bits = 24;
163 else if (*odst >= 128) /* Class B */
164 bits = 16;
165 else /* Class A */
166 bits = 8;
167 /* If imputed mask is narrower than specified octets, widen. */
168 if (bits < ((dst - odst) * 8))
169 bits = (dst - odst) * 8;
170 /*
171 * If there are no additional bits specified for a class D
172 * address adjust bits to 4.
173 */
174 if (bits == 8 && *odst == 224)
175 bits = 4;
176 }
177 /* Extend network to cover the actual mask. */
178 while (bits > ((dst - odst) * 8)) {
179 if (size-- <= 0U)
180 goto emsgsize;
181 *dst++ = '\0';
182 }
183 return (bits);
184
185 enoent:
186 errno = ENOENT;
187 return (-1);
188
189 emsgsize:
190 errno = EMSGSIZE;
191 return (-1);
192 }
193
194 static int
195 getbits(const char *src, int *bitsp) {
196 static const char digits[] = "0123456789";
197 int n;
198 int val;
199 char ch;
200
201 val = 0;
202 n = 0;
203 while ((ch = *src++) != '\0') {
204 const char *pch;
205
206 pch = strchr(digits, ch);
207 if (pch != NULL) {
208 if (n++ != 0 && val == 0) /* no leading zeros */
209 return (0);
210 val *= 10;
211 val += (pch - digits);
212 if (val > 128) /* range */
213 return (0);
214 continue;
215 }
216 return (0);
217 }
218 if (n == 0)
219 return (0);
220 *bitsp = val;
221 return (1);
222 }
223
224 static int
225 getv4(const char *src, u_char *dst, int *bitsp) {
226 static const char digits[] = "0123456789";
227 u_char *odst = dst;
228 int n;
229 u_int val;
230 char ch;
231
232 val = 0;
233 n = 0;
234 while ((ch = *src++) != '\0') {
235 const char *pch;
236
237 pch = strchr(digits, ch);
238 if (pch != NULL) {
239 if (n++ != 0 && val == 0) /* no leading zeros */
240 return (0);
241 val *= 10;
242 val += (pch - digits);
243 if (val > 255) /* range */
244 return (0);
245 continue;
246 }
247 if (ch == '.' || ch == '/') {
248 if (dst - odst > 3) /* too many octets? */
249 return (0);
250 *dst++ = val;
251 if (ch == '/')
252 return (getbits(src, bitsp));
253 val = 0;
254 n = 0;
255 continue;
256 }
257 return (0);
258 }
259 if (n == 0)
260 return (0);
261 if (dst - odst > 3) /* too many octets? */
262 return (0);
263 *dst++ = val;
264 return (1);
265 }
266
267 static int
268 inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) {
269 static const char xdigits_l[] = "0123456789abcdef",
270 xdigits_u[] = "0123456789ABCDEF";
271 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
272 const char *xdigits, *curtok;
273 int ch, saw_xdigit;
274 u_int val;
275 int digits;
276 int bits;
277 size_t bytes;
278 int words;
279 int ipv4;
280
281 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
282 endp = tp + NS_IN6ADDRSZ;
283 colonp = NULL;
284 /* Leading :: requires some special handling. */
285 if (*src == ':')
286 if (*++src != ':')
287 goto enoent;
288 curtok = src;
289 saw_xdigit = 0;
290 val = 0;
291 digits = 0;
292 bits = -1;
293 ipv4 = 0;
294 while ((ch = *src++) != '\0') {
295 const char *pch;
296
297 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
298 pch = strchr((xdigits = xdigits_u), ch);
299 if (pch != NULL) {
300 val <<= 4;
301 val |= (pch - xdigits);
302 if (++digits > 4)
303 goto enoent;
304 saw_xdigit = 1;
305 continue;
306 }
307 if (ch == ':') {
308 curtok = src;
309 if (!saw_xdigit) {
310 if (colonp)
311 goto enoent;
312 colonp = tp;
313 continue;
314 } else if (*src == '\0')
315 goto enoent;
316 if (tp + NS_INT16SZ > endp)
317 return (0);
318 *tp++ = (u_char) (val >> 8) & 0xff;
319 *tp++ = (u_char) val & 0xff;
320 saw_xdigit = 0;
321 digits = 0;
322 val = 0;
323 continue;
324 }
325 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
326 getv4(curtok, tp, &bits) > 0) {
327 tp += NS_INADDRSZ;
328 saw_xdigit = 0;
329 ipv4 = 1;
330 break; /* '\0' was seen by inet_pton4(). */
331 }
332 if (ch == '/' && getbits(src, &bits) > 0)
333 break;
334 goto enoent;
335 }
336 if (saw_xdigit) {
337 if (tp + NS_INT16SZ > endp)
338 goto enoent;
339 *tp++ = (u_char) (val >> 8) & 0xff;
340 *tp++ = (u_char) val & 0xff;
341 }
342 if (bits == -1)
343 bits = 128;
344
345 words = (bits + 15) / 16;
346 if (words < 2)
347 words = 2;
348 if (ipv4)
349 words = 8;
350 endp = tmp + 2 * words;
351
352 if (colonp != NULL) {
353 /*
354 * Since some memmove()'s erroneously fail to handle
355 * overlapping regions, we'll do the shift by hand.
356 */
357 const int n = tp - colonp;
358 int i;
359
360 if (tp == endp)
361 goto enoent;
362 for (i = 1; i <= n; i++) {
363 endp[- i] = colonp[n - i];
364 colonp[n - i] = 0;
365 }
366 tp = endp;
367 }
368 if (tp != endp)
369 goto enoent;
370
371 bytes = (bits + 7) / 8;
372 if (bytes > size)
373 goto emsgsize;
374 memcpy(dst, tmp, bytes);
375 return (bits);
376
377 enoent:
378 errno = ENOENT;
379 return (-1);
380
381 emsgsize:
382 errno = EMSGSIZE;
383 return (-1);
384 }
385
386 /*
387 * int
388 * inet_net_pton(af, src, dst, size)
389 * convert network number from presentation to network format.
390 * accepts hex octets, hex strings, decimal octets, and /CIDR.
391 * "size" is in bytes and describes "dst".
392 * return:
393 * number of bits, either imputed classfully or specified with /CIDR,
394 * or -1 if some failure occurred (check errno). ENOENT means it was
395 * not a valid network specification.
396 * author:
397 * Paul Vixie (ISC), June 1996
398 */
399 int
400 ares_inet_net_pton(int af, const char *src, void *dst, size_t size) {
401 switch (af) {
402 case AF_INET:
403 return (inet_net_pton_ipv4(src, dst, size));
404 case AF_INET6:
405 return (inet_net_pton_ipv6(src, dst, size));
406 default:
407 errno = EAFNOSUPPORT;
408 return (-1);
409 }
410 }
411
412 #endif
413
414 #if !defined(HAVE_INET_PTON) || !defined(HAVE_INET_PTON_IPV6)
415 int ares_inet_pton(int af, const char *src, void *dst) {
416 int size, result;
417
418 if (af == AF_INET)
419 size = sizeof(struct in_addr);
420 else if (af == AF_INET6)
421 size = sizeof(struct in6_addr);
422 else
423 {
424 errno = EAFNOSUPPORT;
425 return -1;
426 }
427 result = ares_inet_net_pton(af, src, dst, size);
428 if (result == -1 && errno == ENOENT)
429 return 0;
430 return (result > -1 ? 1 : -1);
431 }
432 #endif
Something went wrong with that request. Please try again.