ares_inet_ntop: reapply changes from previous c-ares version (III)
[platform/upstream/c-ares.git] / inet_ntop.c
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 "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   case AF_INET:
83     return (inet_ntop4(src, dst, size));
84   case AF_INET6:
85     return (inet_ntop6(src, dst, size));
86   default:
87     SET_ERRNO(EAFNOSUPPORT);
88     return (NULL);
89   }
90   /* NOTREACHED */
91 }
92
93 /* const char *
94  * inet_ntop4(src, dst, size)
95  *     format an IPv4 address
96  * return:
97  *     `dst' (as a const)
98  * notes:
99  *     (1) uses no statics
100  *     (2) takes a unsigned char* not an in_addr as input
101  * author:
102  *     Paul Vixie, 1996.
103  */
104 static const char *
105 inet_ntop4(const unsigned char *src, char *dst, size_t size)
106 {
107   static const char fmt[] = "%u.%u.%u.%u";
108   char tmp[sizeof("255.255.255.255")];
109
110   if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) >= size) {
111     SET_ERRNO(ENOSPC);
112     return (NULL);
113   }
114   strcpy(dst, tmp);
115   return (dst);
116 }
117
118 /* const char *
119  * inet_ntop6(src, dst, size)
120  *     convert IPv6 binary address into presentation (printable) format
121  * author:
122  *     Paul Vixie, 1996.
123  */
124 static const char *
125 inet_ntop6(const unsigned char *src, char *dst, size_t size)
126 {
127   /*
128    * Note that int32_t and int16_t need only be "at least" large enough
129    * to contain a value of the specified size.  On some systems, like
130    * Crays, there is no such thing as an integer variable with 16 bits.
131    * Keep this in mind if you think this function should have been coded
132    * to use pointer overlays.  All the world's not a VAX.
133    */
134   char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
135   char *tp;
136   struct { int base, len; } best, cur;
137   unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
138   int i;
139
140   /*
141    * Preprocess:
142    *  Copy the input (bytewise) array into a wordwise array.
143    *  Find the longest run of 0x00's in src[] for :: shorthanding.
144    */
145   memset(words, '\0', sizeof(words));
146   for (i = 0; i < NS_IN6ADDRSZ; i++)
147     words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
148   best.base = -1;
149   best.len = 0;
150   cur.base = -1;
151   cur.len = 0;
152   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
153     if (words[i] == 0) {
154       if (cur.base == -1)
155         cur.base = i, cur.len = 1;
156       else
157         cur.len++;
158     } else {
159       if (cur.base != -1) {
160         if (best.base == -1 || cur.len > best.len)
161           best = cur;
162         cur.base = -1;
163       }
164     }
165   }
166   if (cur.base != -1) {
167     if (best.base == -1 || cur.len > best.len)
168       best = cur;
169   }
170   if (best.base != -1 && best.len < 2)
171     best.base = -1;
172
173   /*
174    * Format the result.
175    */
176   tp = tmp;
177   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
178     /* Are we inside the best run of 0x00's? */
179     if (best.base != -1 && i >= best.base &&
180         i < (best.base + best.len)) {
181       if (i == best.base)
182         *tp++ = ':';
183       continue;
184     }
185     /* Are we following an initial run of 0x00s or any real hex? */
186     if (i != 0)
187       *tp++ = ':';
188     /* Is this address an encapsulated IPv4? */
189     if (i == 6 && best.base == 0 && (best.len == 6 ||
190         (best.len == 7 && words[7] != 0x0001) ||
191         (best.len == 5 && words[5] == 0xffff))) {
192       if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
193         return (NULL);
194       tp += strlen(tp);
195       break;
196     }
197     tp += SPRINTF((tp, "%x", words[i]));
198   }
199   /* Was it a trailing run of 0x00's? */
200   if (best.base != -1 && (best.base + best.len) == 
201       (NS_IN6ADDRSZ / NS_INT16SZ))
202     *tp++ = ':';
203   *tp++ = '\0';
204
205   /*
206    * Check for overflow, copy, and we're done.
207    */
208   if ((size_t)(tp - tmp) > size) {
209     SET_ERRNO(ENOSPC);
210     return (NULL);
211   }
212   strcpy(dst, tmp);
213   return (dst);
214 }
215 #endif