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