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