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