ares_inet_ntop: remove definition and usage of macro SPRINTF
[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 /*
52  * WARNING: Don't even consider trying to compile this on a system where
53  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
54  */
55
56 static const char *inet_ntop4(const unsigned char *src, char *dst, size_t size);
57 static const char *inet_ntop6(const unsigned char *src, char *dst, size_t size);
58
59 /* char *
60  * inet_ntop(af, src, dst, size)
61  *     convert a network format address to presentation format.
62  * return:
63  *     pointer to presentation format address (`dst'), or NULL (see errno).
64  * note:
65  *     On Windows we store the error in the thread errno, not
66  *     in the winsock error code. This is to avoid loosing the
67  *     actual last winsock error. So use macro ERRNO to fetch the
68  *     errno this funtion sets when returning NULL, not SOCKERRNO.
69  * author:
70  *     Paul Vixie, 1996.
71  */
72 const char *
73 ares_inet_ntop(int af, const void *src, char *dst, size_t size)
74 {
75   switch (af) {
76   case AF_INET:
77     return (inet_ntop4(src, dst, size));
78   case AF_INET6:
79     return (inet_ntop6(src, dst, size));
80   default:
81     SET_ERRNO(EAFNOSUPPORT);
82     return (NULL);
83   }
84   /* NOTREACHED */
85 }
86
87 /* const char *
88  * inet_ntop4(src, dst, size)
89  *     format an IPv4 address
90  * return:
91  *     `dst' (as a const)
92  * notes:
93  *     (1) uses no statics
94  *     (2) takes a unsigned char* not an in_addr as input
95  * author:
96  *     Paul Vixie, 1996.
97  */
98 static const char *
99 inet_ntop4(const unsigned char *src, char *dst, size_t size)
100 {
101   static const char fmt[] = "%u.%u.%u.%u";
102   char tmp[sizeof("255.255.255.255")];
103
104   if ((size_t)sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) >= size) {
105     SET_ERRNO(ENOSPC);
106     return (NULL);
107   }
108   strcpy(dst, tmp);
109   return (dst);
110 }
111
112 /* const char *
113  * inet_ntop6(src, dst, size)
114  *     convert IPv6 binary address into presentation (printable) format
115  * author:
116  *     Paul Vixie, 1996.
117  */
118 static const char *
119 inet_ntop6(const unsigned char *src, char *dst, size_t size)
120 {
121   /*
122    * Note that int32_t and int16_t need only be "at least" large enough
123    * to contain a value of the specified size.  On some systems, like
124    * Crays, there is no such thing as an integer variable with 16 bits.
125    * Keep this in mind if you think this function should have been coded
126    * to use pointer overlays.  All the world's not a VAX.
127    */
128   char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
129   char *tp;
130   struct { int base, len; } best, cur;
131   unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
132   int i;
133
134   /*
135    * Preprocess:
136    *  Copy the input (bytewise) array into a wordwise array.
137    *  Find the longest run of 0x00's in src[] for :: shorthanding.
138    */
139   memset(words, '\0', sizeof(words));
140   for (i = 0; i < NS_IN6ADDRSZ; i++)
141     words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
142   best.base = -1;
143   best.len = 0;
144   cur.base = -1;
145   cur.len = 0;
146   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
147     if (words[i] == 0) {
148       if (cur.base == -1)
149         cur.base = i, cur.len = 1;
150       else
151         cur.len++;
152     } else {
153       if (cur.base != -1) {
154         if (best.base == -1 || cur.len > best.len)
155           best = cur;
156         cur.base = -1;
157       }
158     }
159   }
160   if (cur.base != -1) {
161     if (best.base == -1 || cur.len > best.len)
162       best = cur;
163   }
164   if (best.base != -1 && best.len < 2)
165     best.base = -1;
166
167   /*
168    * Format the result.
169    */
170   tp = tmp;
171   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
172     /* Are we inside the best run of 0x00's? */
173     if (best.base != -1 && i >= best.base &&
174         i < (best.base + best.len)) {
175       if (i == best.base)
176         *tp++ = ':';
177       continue;
178     }
179     /* Are we following an initial run of 0x00s or any real hex? */
180     if (i != 0)
181       *tp++ = ':';
182     /* Is this address an encapsulated IPv4? */
183     if (i == 6 && best.base == 0 && (best.len == 6 ||
184         (best.len == 7 && words[7] != 0x0001) ||
185         (best.len == 5 && words[5] == 0xffff))) {
186       if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
187         return (NULL);
188       tp += strlen(tp);
189       break;
190     }
191     tp += sprintf(tp, "%x", words[i]);
192   }
193   /* Was it a trailing run of 0x00's? */
194   if (best.base != -1 && (best.base + best.len) == 
195       (NS_IN6ADDRSZ / NS_INT16SZ))
196     *tp++ = ':';
197   *tp++ = '\0';
198
199   /*
200    * Check for overflow, copy, and we're done.
201    */
202   if ((size_t)(tp - tmp) > size) {
203     SET_ERRNO(ENOSPC);
204     return (NULL);
205   }
206   strcpy(dst, tmp);
207   return (dst);
208 }
209 #endif