Fix inet_ntop on mingw32.
[gnulib.git] / lib / inet_ntop.c
1 /* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
2    Copyright (c) 2005  Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /*
19  * Copyright (c) 1996-1999 by Internet Software Consortium.
20  *
21  * Permission to use, copy, modify, and distribute this software for any
22  * purpose with or without fee is hereby granted, provided that the above
23  * copyright notice and this permission notice appear in all copies.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
26  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
28  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
29  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
30  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
31  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
32  * SOFTWARE.
33  */
34
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38
39 /* Specification.  */
40 #include "inet_ntop.h"
41
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45
46 #ifndef EAFNOSUPPORT
47 # define EAFNOSUPPORT EINVAL
48 #endif
49
50 #define NS_IN6ADDRSZ 16
51 #define NS_INT16SZ 2
52
53 /*
54  * WARNING: Don't even consider trying to compile this on a system where
55  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
56  */
57 typedef int verify_int_size[2 * sizeof (int) - 7];
58
59 #if HAVE_IPV4
60 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
61 #endif
62 #if HAVE_IPV6
63 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
64 #endif
65
66
67 /* char *
68  * inet_ntop(af, src, dst, size)
69  *      convert a network format address to presentation format.
70  * return:
71  *      pointer to presentation format address (`dst'), or NULL (see errno).
72  * author:
73  *      Paul Vixie, 1996.
74  */
75 const char *
76 inet_ntop (int af, const void *restrict src,
77            char *restrict dst, socklen_t cnt)
78 {
79   switch (af)
80     {
81 #if HAVE_IPV4
82     case AF_INET:
83       return (inet_ntop4 (src, dst, cnt));
84 #endif
85
86 #if HAVE_IPV6
87     case AF_INET6:
88       return (inet_ntop6 (src, dst, cnt));
89 #endif
90
91     default:
92       errno = EAFNOSUPPORT;
93       return (NULL);
94     }
95   /* NOTREACHED */
96 }
97
98 #if HAVE_IPV4
99
100 /* const char *
101  * inet_ntop4(src, dst, size)
102  *      format an IPv4 address
103  * return:
104  *      `dst' (as a const)
105  * notes:
106  *      (1) uses no statics
107  *      (2) takes a u_char* not an in_addr as input
108  * author:
109  *      Paul Vixie, 1996.
110  */
111 static const char *
112 inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
113 {
114   char tmp[sizeof "255.255.255.255"];
115   int len;
116
117   len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
118   if (len < 0)
119     return NULL;
120
121   if (len > size)
122     {
123       errno = ENOSPC;
124       return NULL;
125     }
126
127   return strcpy (dst, tmp);
128 }
129
130 #endif
131
132 #if HAVE_IPV6
133
134 /* const char *
135  * inet_ntop6(src, dst, size)
136  *      convert IPv6 binary address into presentation (printable) format
137  * author:
138  *      Paul Vixie, 1996.
139  */
140 static const char *
141 inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
142 {
143   /*
144    * Note that int32_t and int16_t need only be "at least" large enough
145    * to contain a value of the specified size.  On some systems, like
146    * Crays, there is no such thing as an integer variable with 16 bits.
147    * Keep this in mind if you think this function should have been coded
148    * to use pointer overlays.  All the world's not a VAX.
149    */
150   char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
151   struct
152   {
153     int base, len;
154   } best, cur;
155   unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
156   int i;
157
158   /*
159    * Preprocess:
160    *      Copy the input (bytewise) array into a wordwise array.
161    *      Find the longest run of 0x00's in src[] for :: shorthanding.
162    */
163   memset (words, '\0', sizeof words);
164   for (i = 0; i < NS_IN6ADDRSZ; i += 2)
165     words[i / 2] = (src[i] << 8) | src[i + 1];
166   best.base = -1;
167   cur.base = -1;
168   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
169     {
170       if (words[i] == 0)
171         {
172           if (cur.base == -1)
173             cur.base = i, cur.len = 1;
174           else
175             cur.len++;
176         }
177       else
178         {
179           if (cur.base != -1)
180             {
181               if (best.base == -1 || cur.len > best.len)
182                 best = cur;
183               cur.base = -1;
184             }
185         }
186     }
187   if (cur.base != -1)
188     {
189       if (best.base == -1 || cur.len > best.len)
190         best = cur;
191     }
192   if (best.base != -1 && best.len < 2)
193     best.base = -1;
194
195   /*
196    * Format the result.
197    */
198   tp = tmp;
199   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
200     {
201       /* Are we inside the best run of 0x00's? */
202       if (best.base != -1 && i >= best.base && i < (best.base + best.len))
203         {
204           if (i == best.base)
205             *tp++ = ':';
206           continue;
207         }
208       /* Are we following an initial run of 0x00s or any real hex? */
209       if (i != 0)
210         *tp++ = ':';
211       /* Is this address an encapsulated IPv4? */
212       if (i == 6 && best.base == 0 &&
213           (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
214         {
215           if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
216             return (NULL);
217           tp += strlen (tp);
218           break;
219         }
220       {
221         int len = sprintf (tp, "%x", words[i]);
222         if (len < 0)
223           return NULL;
224         tp += len;
225       }
226     }
227   /* Was it a trailing run of 0x00's? */
228   if (best.base != -1 && (best.base + best.len) ==
229       (NS_IN6ADDRSZ / NS_INT16SZ))
230     *tp++ = ':';
231   *tp++ = '\0';
232
233   /*
234    * Check for overflow, copy, and we're done.
235    */
236   if ((socklen_t) (tp - tmp) > size)
237     {
238       errno = ENOSPC;
239       return NULL;
240     }
241
242   return strcpy (dst, tmp);
243 }
244
245 #endif