Fix compiler warning
[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 "setup.h"
20
21 #include <sys/types.h>
22
23 #if defined(WIN32) && !defined(WATT32)
24 #include "nameser.h"
25 #else
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
28 #endif
29 #ifdef HAVE_NETINET_IN_H
30 #include <netinet/in.h>
31 #endif
32 #ifdef HAVE_ARPA_NAMESER_H
33 #include <arpa/nameser.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
37 #endif
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_ntop.h"
48
49
50 #if !defined(HAVE_INET_NTOP) || !defined(HAVE_INET_NTOP_IPV6)
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  * author:
72  *     Paul Vixie, 1996.
73  */
74 const char *
75 ares_inet_ntop(int af, const void *src, char *dst, size_t size)
76 {
77   switch (af)
78     {
79     case AF_INET:
80       return (inet_ntop4(src, dst, size));
81     case AF_INET6:
82       return (inet_ntop6(src, dst, size));
83     default:
84       errno = EAFNOSUPPORT;
85       return (NULL);
86     }
87   /* NOTREACHED */
88 }
89
90 /* const char *
91  * inet_ntop4(src, dst, size)
92  *     format an IPv4 address, more or less like inet_ntoa()
93  * return:
94  *     `dst' (as a const)
95  * notes:
96  *     (1) uses no statics
97  *     (2) takes a unsigned char* not an in_addr as input
98  * author:
99  *     Paul Vixie, 1996.
100  */
101 static const char *
102 inet_ntop4(const unsigned char *src, char *dst, size_t size)
103 {
104   static const char fmt[] = "%u.%u.%u.%u";
105   char tmp[sizeof "255.255.255.255"];
106
107   if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size)
108     {
109       errno = ENOSPC;
110       return (NULL);
111     }
112     strcpy(dst, tmp);
113     return (dst);
114 }
115
116 /* const char *
117  * inet_ntop6(src, dst, size)
118  *    convert IPv6 binary address into presentation (printable) format
119  * author:
120  *    Paul Vixie, 1996.
121  */
122 static const char *
123 inet_ntop6(const unsigned char *src, char *dst, size_t size)
124 {
125   /*
126    * Note that int32_t and int16_t need only be "at least" large enough
127    * to contain a value of the specified size.  On some systems, like
128    * Crays, there is no such thing as an integer variable with 16 bits.
129    * Keep this in mind if you think this function should have been coded
130    * to use pointer overlays.  All the world's not a VAX.
131    */
132   char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
133   char *tp;
134   struct {
135     long base;
136     long len;
137   } best, cur;
138   unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
139   int i;
140
141   /*
142    * Preprocess:
143    *  Copy the input (bytewise) array into a wordwise array.
144    *  Find the longest run of 0x00's in src[] for :: shorthanding.
145    */
146   memset(words, '\0', sizeof words);
147   for (i = 0; i < NS_IN6ADDRSZ; i++)
148       words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
149
150   best.base = -1;
151   cur.base = -1;
152   best.len = 0;
153   cur.len = 0;
154
155   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
156     {
157       if (words[i] == 0)
158         {
159           if (cur.base == -1)
160             cur.base = i, cur.len = 1;
161           else
162             cur.len++;
163         }
164       else
165         {
166           if (cur.base != -1)
167             {
168               if (best.base == -1 || cur.len > best.len)
169                 best = cur;
170               cur.base = -1;
171             }
172         }
173     }
174   if (cur.base != -1)
175     {
176       if (best.base == -1 || cur.len > best.len)
177         best = cur;
178     }
179   if (best.base != -1 && best.len < 2)
180     best.base = -1;
181
182   /*
183    * Format the result.
184    */
185   tp = tmp;
186   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
187     {
188       /* Are we inside the best run of 0x00's? */
189       if (best.base != -1 && i >= best.base &&
190           i < (best.base + best.len))
191         {
192           if (i == best.base)
193              *tp++ = ':';
194           continue;
195         }
196       /* Are we following an initial run of 0x00s or any real hex? */
197       if (i != 0)
198         *tp++ = ':';
199       /* Is this address an encapsulated IPv4? */
200       if (i == 6 && best.base == 0 &&
201           (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
202         {
203           if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
204             return (NULL);
205           tp += strlen(tp);
206           break;
207         }
208         tp += SPRINTF((tp, "%x", words[i]));
209     }
210
211   /* Was it a trailing run of 0x00's? */
212   if (best.base != -1 && (best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ))
213     *tp++ = ':';
214   *tp++ = '\0';
215
216   /*
217    * Check for overflow, copy, and we're done.
218    */
219   if ((size_t)(tp - tmp) > size)
220     {
221       errno = ENOSPC;
222       return (NULL);
223     }
224   strcpy(dst, tmp);
225   return (dst);
226 }
227 #endif
228