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