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