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