Added IPv6 name servers support
[platform/upstream/c-ares.git] / inet_ntop.c
1 /* $Id$ */
2
3 /* Copyright (c) 1996 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16  * SOFTWARE.
17  */
18
19 #include "ares_setup.h"
20
21 #ifdef HAVE_SYS_SOCKET_H
22 #  include <sys/socket.h>
23 #endif
24 #ifdef HAVE_NETINET_IN_H
25 #  include <netinet/in.h>
26 #endif
27 #ifdef HAVE_ARPA_INET_H
28 #  include <arpa/inet.h>
29 #endif
30 #ifdef HAVE_ARPA_NAMESER_H
31 #  include <arpa/nameser.h>
32 #else
33 #  include "nameser.h"
34 #endif
35 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
36 #  include <arpa/nameser_compat.h>
37 #endif
38
39 #include <ctype.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44
45 #include "ares.h"
46 #include "ares_ipv6.h"
47 #include "inet_ntop.h"
48
49
50 #ifndef HAVE_INET_NTOP
51
52 #ifdef SPRINTF_CHAR
53 # define SPRINTF(x) strlen(sprintf/**/x)
54 #else
55 # define SPRINTF(x) ((size_t)sprintf x)
56 #endif
57
58 /*
59  * WARNING: Don't even consider trying to compile this on a system where
60  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
61  */
62
63 static const char *inet_ntop4(const unsigned char *src, char *dst, size_t size);
64 static const char *inet_ntop6(const unsigned char *src, char *dst, size_t size);
65
66 /* char *
67  * inet_ntop(af, src, dst, size)
68  *     convert a network format address to presentation format.
69  * return:
70  *     pointer to presentation format address (`dst'), or NULL (see errno).
71  * note:
72  *      On Windows we store the error in the thread errno, not
73  *      in the winsock error code. This is to avoid loosing the
74  *      actual last winsock error. So use macro ERRNO to fetch the
75  *      errno this funtion sets when returning NULL, not SOCKERRNO.
76  * author:
77  *     Paul Vixie, 1996.
78  */
79 const char *
80 ares_inet_ntop(int af, const void *src, char *dst, size_t size)
81 {
82   switch (af)
83     {
84     case AF_INET:
85       return (inet_ntop4(src, dst, size));
86     case AF_INET6:
87       return (inet_ntop6(src, dst, size));
88     default:
89       SET_ERRNO(EAFNOSUPPORT);
90       return (NULL);
91     }
92   /* NOTREACHED */
93 }
94
95 /* const char *
96  * inet_ntop4(src, dst, size)
97  *     format an IPv4 address, more or less like inet_ntoa()
98  * return:
99  *     `dst' (as a const)
100  * notes:
101  *     (1) uses no statics
102  *     (2) takes a unsigned char* not an in_addr as input
103  * author:
104  *     Paul Vixie, 1996.
105  */
106 static const char *
107 inet_ntop4(const unsigned char *src, char *dst, size_t size)
108 {
109   static const char fmt[] = "%u.%u.%u.%u";
110   char tmp[sizeof "255.255.255.255"];
111
112   if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size)
113     {
114       SET_ERRNO(ENOSPC);
115       return (NULL);
116     }
117     strcpy(dst, tmp);
118     return (dst);
119 }
120
121 /* const char *
122  * inet_ntop6(src, dst, size)
123  *    convert IPv6 binary address into presentation (printable) format
124  * author:
125  *    Paul Vixie, 1996.
126  */
127 static const char *
128 inet_ntop6(const unsigned char *src, char *dst, size_t size)
129 {
130   /*
131    * Note that int32_t and int16_t need only be "at least" large enough
132    * to contain a value of the specified size.  On some systems, like
133    * Crays, there is no such thing as an integer variable with 16 bits.
134    * Keep this in mind if you think this function should have been coded
135    * to use pointer overlays.  All the world's not a VAX.
136    */
137   char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
138   char *tp;
139   struct {
140     long base;
141     long len;
142   } best, cur;
143   unsigned long words[NS_IN6ADDRSZ / NS_INT16SZ];
144   int i;
145
146   /*
147    * Preprocess:
148    *  Copy the input (bytewise) array into a wordwise array.
149    *  Find the longest run of 0x00's in src[] for :: shorthanding.
150    */
151   memset(words, '\0', sizeof(words));
152   for (i = 0; i < NS_IN6ADDRSZ; i++)
153       words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
154
155   best.base = -1;
156   cur.base = -1;
157   best.len = 0;
158   cur.len = 0;
159
160   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
161     {
162       if (words[i] == 0)
163         {
164           if (cur.base == -1)
165             cur.base = i, cur.len = 1;
166           else
167             cur.len++;
168         }
169       else
170         {
171           if (cur.base != -1)
172             {
173               if (best.base == -1 || cur.len > best.len)
174                 best = cur;
175               cur.base = -1;
176             }
177         }
178     }
179   if (cur.base != -1)
180     {
181       if (best.base == -1 || cur.len > best.len)
182         best = cur;
183     }
184   if (best.base != -1 && best.len < 2)
185     best.base = -1;
186
187   /*
188    * Format the result.
189    */
190   tp = tmp;
191   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
192     {
193       /* Are we inside the best run of 0x00's? */
194       if (best.base != -1 && i >= best.base &&
195           i < (best.base + best.len))
196         {
197           if (i == best.base)
198              *tp++ = ':';
199           continue;
200         }
201       /* Are we following an initial run of 0x00s or any real hex? */
202       if (i != 0)
203         *tp++ = ':';
204       /* Is this address an encapsulated IPv4? */
205       if (i == 6 && best.base == 0 &&
206           (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
207         {
208           if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
209             return (NULL);
210           tp += strlen(tp);
211           break;
212         }
213         tp += SPRINTF((tp, "%lx", words[i]));
214     }
215
216   /* Was it a trailing run of 0x00's? */
217   if (best.base != -1 && (best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ))
218     *tp++ = ':';
219   *tp++ = '\0';
220
221   /*
222    * Check for overflow, copy, and we're done.
223    */
224   if ((size_t)(tp - tmp) > size)
225     {
226       SET_ERRNO(ENOSPC);
227       return (NULL);
228     }
229   strcpy(dst, tmp);
230   return (dst);
231 }
232 #endif
233