xref: /curl/lib/inet_ntop.c (revision e411c98f)
1 /*
2  * Copyright (C) 1996-2022  Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
9  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11  * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * SPDX-License-Identifier: ISC
18  */
19 /*
20  * Original code by Paul Vixie. "curlified" by Gisle Vanem.
21  */
22 
23 #include "curl_setup.h"
24 
25 #ifndef HAVE_INET_NTOP
26 
27 #ifdef HAVE_SYS_PARAM_H
28 #include <sys/param.h>
29 #endif
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
35 #endif
36 
37 #include "inet_ntop.h"
38 #include "curl_printf.h"
39 
40 #define IN6ADDRSZ       16
41 #define INADDRSZ         4
42 #define INT16SZ          2
43 
44 /*
45  * If USE_IPV6 is disabled, we still want to parse IPv6 addresses, so make
46  * sure we have _some_ value for AF_INET6 without polluting our fake value
47  * everywhere.
48  */
49 #if !defined(USE_IPV6) && !defined(AF_INET6)
50 #define AF_INET6 (AF_INET + 1)
51 #endif
52 
53 /*
54  * Format an IPv4 address, more or less like inet_ntop().
55  *
56  * Returns `dst' (as a const)
57  * Note:
58  *  - uses no statics
59  *  - takes a unsigned char* not an in_addr as input
60  */
inet_ntop4(const unsigned char * src,char * dst,size_t size)61 static char *inet_ntop4 (const unsigned char *src, char *dst, size_t size)
62 {
63   char tmp[sizeof("255.255.255.255")];
64   size_t len;
65 
66   DEBUGASSERT(size >= 16);
67 
68   tmp[0] = '\0';
69   (void)msnprintf(tmp, sizeof(tmp), "%d.%d.%d.%d",
70                   ((int)((unsigned char)src[0])) & 0xff,
71                   ((int)((unsigned char)src[1])) & 0xff,
72                   ((int)((unsigned char)src[2])) & 0xff,
73                   ((int)((unsigned char)src[3])) & 0xff);
74 
75   len = strlen(tmp);
76   if(len == 0 || len >= size) {
77     errno = ENOSPC;
78     return (NULL);
79   }
80   strcpy(dst, tmp);
81   return dst;
82 }
83 
84 /*
85  * Convert IPv6 binary address into presentation (printable) format.
86  */
inet_ntop6(const unsigned char * src,char * dst,size_t size)87 static char *inet_ntop6 (const unsigned char *src, char *dst, size_t size)
88 {
89   /*
90    * Note that int32_t and int16_t need only be "at least" large enough
91    * to contain a value of the specified size.  On some systems, like
92    * Crays, there is no such thing as an integer variable with 16 bits.
93    * Keep this in mind if you think this function should have been coded
94    * to use pointer overlays.  All the world's not a VAX.
95    */
96   char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
97   char *tp;
98   struct {
99     int base;
100     int len;
101   } best, cur;
102   unsigned int words[IN6ADDRSZ / INT16SZ];
103   int i;
104 
105   /* Preprocess:
106    *  Copy the input (bytewise) array into a wordwise array.
107    *  Find the longest run of 0x00's in src[] for :: shorthanding.
108    */
109   memset(words, '\0', sizeof(words));
110   for(i = 0; i < IN6ADDRSZ; i++)
111     words[i/2] |= ((unsigned int)src[i] << ((1 - (i % 2)) << 3));
112 
113   best.base = -1;
114   cur.base  = -1;
115   best.len = 0;
116   cur.len = 0;
117 
118   for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
119     if(words[i] == 0) {
120       if(cur.base == -1) {
121         cur.base = i; cur.len = 1;
122       }
123       else
124         cur.len++;
125     }
126     else if(cur.base != -1) {
127       if(best.base == -1 || cur.len > best.len)
128         best = cur;
129       cur.base = -1;
130     }
131   }
132   if((cur.base != -1) && (best.base == -1 || cur.len > best.len))
133     best = cur;
134   if(best.base != -1 && best.len < 2)
135     best.base = -1;
136   /* Format the result. */
137   tp = tmp;
138   for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
139     /* Are we inside the best run of 0x00's? */
140     if(best.base != -1 && i >= best.base && i < (best.base + best.len)) {
141       if(i == best.base)
142         *tp++ = ':';
143       continue;
144     }
145 
146     /* Are we following an initial run of 0x00s or any real hex?
147      */
148     if(i)
149       *tp++ = ':';
150 
151     /* Is this address an encapsulated IPv4?
152      */
153     if(i == 6 && best.base == 0 &&
154         (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
155       if(!inet_ntop4(src + 12, tp, sizeof(tmp) - (tp - tmp))) {
156         errno = ENOSPC;
157         return (NULL);
158       }
159       tp += strlen(tp);
160       break;
161     }
162     tp += msnprintf(tp, 5, "%x", words[i]);
163   }
164 
165   /* Was it a trailing run of 0x00's?
166    */
167   if(best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
168     *tp++ = ':';
169   *tp++ = '\0';
170 
171   /* Check for overflow, copy, and we're done.
172    */
173   if((size_t)(tp - tmp) > size) {
174     errno = ENOSPC;
175     return (NULL);
176   }
177   strcpy(dst, tmp);
178   return dst;
179 }
180 
181 /*
182  * Convert a network format address to presentation format.
183  *
184  * Returns pointer to presentation format address (`buf').
185  * Returns NULL on error and errno set with the specific
186  * error, EAFNOSUPPORT or ENOSPC.
187  *
188  * On Windows we store the error in the thread errno, not
189  * in the winsock error code. This is to avoid losing the
190  * actual last winsock error. So when this function returns
191  * NULL, check errno not SOCKERRNO.
192  */
Curl_inet_ntop(int af,const void * src,char * buf,size_t size)193 char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
194 {
195   switch(af) {
196   case AF_INET:
197     return inet_ntop4((const unsigned char *)src, buf, size);
198   case AF_INET6:
199     return inet_ntop6((const unsigned char *)src, buf, size);
200   default:
201     errno = EAFNOSUPPORT;
202     return NULL;
203   }
204 }
205 #endif  /* HAVE_INET_NTOP */
206