Added CVS id, Detabified, applied c-ares coding-style.
[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"], *tp;
133   struct { int base, len; } best = { 0,0 }, cur = { 0,0 };
134   unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
135   int i;
136
137   /*
138    * Preprocess:
139    *  Copy the input (bytewise) array into a wordwise array.
140    *  Find the longest run of 0x00's in src[] for :: shorthanding.
141    */
142   memset(words, '\0', sizeof words);
143   for (i = 0; i < NS_IN6ADDRSZ; i++)
144       words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
145   best.base = -1;
146   cur.base = -1;
147   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
148     {
149       if (words[i] == 0)
150         {
151           if (cur.base == -1)
152             cur.base = i, cur.len = 1;
153           else
154             cur.len++;
155         }
156       else
157         {
158           if (cur.base != -1)
159             {
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     {
168       if (best.base == -1 || cur.len > best.len)
169         best = cur;
170     }
171   if (best.base != -1 && best.len < 2)
172     best.base = -1;
173
174   /*
175    * Format the result.
176    */
177   tp = tmp;
178   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
179     {
180       /* Are we inside the best run of 0x00's? */
181       if (best.base != -1 && i >= best.base &&
182           i < (best.base + best.len))
183         {
184           if (i == best.base)
185              *tp++ = ':';
186           continue;
187         }
188       /* Are we following an initial run of 0x00s or any real hex? */
189       if (i != 0)
190         *tp++ = ':';
191       /* Is this address an encapsulated IPv4? */
192       if (i == 6 && best.base == 0 &&
193           (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
194         {
195           if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
196             return (NULL);
197           tp += strlen(tp);
198           break;
199         }
200         tp += SPRINTF((tp, "%x", words[i]));
201     }
202
203   /* Was it a trailing run of 0x00's? */
204   if (best.base != -1 && (best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ))
205     *tp++ = ':';
206   *tp++ = '\0';
207
208   /*
209    * Check for overflow, copy, and we're done.
210    */
211   if ((size_t)(tp - tmp) > size)
212     {
213       errno = ENOSPC;
214       return (NULL);
215     }
216   strcpy(dst, tmp);
217   return (dst);
218 }
219 #endif
220