Move portable error number symbolic name definitions to setup_once.h
[platform/upstream/curl.git] / lib / inet_ntop.c
1 /*
2  * Copyright (C) 1996-2001  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 /*
18  * Original code by Paul Vixie. "curlified" by Gisle Vanem.
19  */
20
21 #include "setup.h"
22
23 #ifndef HAVE_INET_NTOP
24
25 #ifdef HAVE_SYS_PARAM_H
26 #include <sys/param.h>
27 #endif
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
30 #endif
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
33 #endif
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37 #ifdef HAVE_ARPA_INET_H
38 #include <arpa/inet.h>
39 #endif
40 #include <string.h>
41 #include <errno.h>
42
43 #define _MPRINTF_REPLACE /* use our functions only */
44 #include <curl/mprintf.h>
45
46 #include "inet_ntop.h"
47
48 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
49 /* this platform has a inet_ntoa_r() function, but no proto declared anywhere
50    so we include our own proto to make compilers happy */
51 #include "inet_ntoa_r.h"
52 #endif
53
54 #define IN6ADDRSZ       16
55 #define INADDRSZ         4
56 #define INT16SZ          2
57
58 /*
59  * Format an IPv4 address, more or less like inet_ntoa().
60  *
61  * Returns `dst' (as a const)
62  * Note:
63  *  - uses no statics
64  *  - takes a unsigned char* not an in_addr as input
65  */
66 static char *inet_ntop4 (const unsigned char *src, char *dst, size_t size)
67 {
68 #if defined(HAVE_INET_NTOA_R_2_ARGS)
69   const char *ptr;
70   curlassert(size >= 16);
71   ptr = inet_ntoa_r(*(struct in_addr*)src, dst);
72   return (char *)memmove(dst, ptr, strlen(ptr)+1);
73
74 #elif defined(HAVE_INET_NTOA_R)
75   return inet_ntoa_r(*(struct in_addr*)src, dst, size);
76
77 #else
78   const char *addr = inet_ntoa(*(struct in_addr*)src);
79
80   if (strlen(addr) >= size)
81   {
82     SET_ERRNO(ENOSPC);
83     return (NULL);
84   }
85   return strcpy(dst, addr);
86 #endif
87 }
88
89 #ifdef ENABLE_IPV6
90 /*
91  * Convert IPv6 binary address into presentation (printable) format.
92  */
93 static char *inet_ntop6 (const unsigned char *src, char *dst, size_t size)
94 {
95   /*
96    * Note that int32_t and int16_t need only be "at least" large enough
97    * to contain a value of the specified size.  On some systems, like
98    * Crays, there is no such thing as an integer variable with 16 bits.
99    * Keep this in mind if you think this function should have been coded
100    * to use pointer overlays.  All the world's not a VAX.
101    */
102   char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
103   char *tp;
104   struct {
105     long base;
106     long len;
107   } best, cur;
108   unsigned long words[IN6ADDRSZ / INT16SZ];
109   int i;
110
111   /* Preprocess:
112    *  Copy the input (bytewise) array into a wordwise array.
113    *  Find the longest run of 0x00's in src[] for :: shorthanding.
114    */
115   memset(words, '\0', sizeof(words));
116   for (i = 0; i < IN6ADDRSZ; i++)
117       words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
118
119   best.base = -1;
120   cur.base  = -1;
121   best.len = 0;
122   cur.len = 0;
123
124   for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
125   {
126     if (words[i] == 0)
127     {
128       if (cur.base == -1)
129         cur.base = i, cur.len = 1;
130       else
131         cur.len++;
132     }
133     else if (cur.base != -1)
134     {
135       if (best.base == -1 || cur.len > best.len)
136          best = cur;
137       cur.base = -1;
138     }
139   }
140   if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
141      best = cur;
142   if (best.base != -1 && best.len < 2)
143      best.base = -1;
144
145   /* Format the result.
146    */
147   tp = tmp;
148   for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
149   {
150     /* Are we inside the best run of 0x00's?
151      */
152     if (best.base != -1 && i >= best.base && i < (best.base + best.len))
153     {
154       if (i == best.base)
155          *tp++ = ':';
156       continue;
157     }
158
159     /* Are we following an initial run of 0x00s or any real hex?
160      */
161     if (i != 0)
162        *tp++ = ':';
163
164     /* Is this address an encapsulated IPv4?
165      */
166     if (i == 6 && best.base == 0 &&
167         (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
168     {
169       if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
170       {
171         SET_ERRNO(ENOSPC);
172         return (NULL);
173       }
174       tp += strlen(tp);
175       break;
176     }
177     tp += snprintf(tp, 5, "%lx", words[i]);
178   }
179
180   /* Was it a trailing run of 0x00's?
181    */
182   if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
183      *tp++ = ':';
184   *tp++ = '\0';
185
186   /* Check for overflow, copy, and we're done.
187    */
188   if ((size_t)(tp - tmp) > size)
189   {
190     SET_ERRNO(ENOSPC);
191     return (NULL);
192   }
193   return strcpy (dst, tmp);
194 }
195 #endif  /* ENABLE_IPV6 */
196
197 /*
198  * Convert a network format address to presentation format.
199  *
200  * Returns pointer to presentation format address (`buf').
201  * Returns NULL on error and errno set with the specific
202  * error, EAFNOSUPPORT or ENOSPC.
203  *
204  * On Windows we store the error in the thread errno, not
205  * in the winsock error code. This is to avoid loosing the
206  * actual last winsock error. So use macro ERRNO to fetch the
207  * errno this funtion sets when returning NULL, not SOCKERRNO.
208  */
209 char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
210 {
211   switch (af) {
212   case AF_INET:
213     return inet_ntop4((const unsigned char*)src, buf, size);
214 #ifdef ENABLE_IPV6
215   case AF_INET6:
216     return inet_ntop6((const unsigned char*)src, buf, size);
217 #endif
218   default:
219     SET_ERRNO(EAFNOSUPPORT);
220     return NULL;
221   }
222 }
223 #endif  /* HAVE_INET_NTOP */