inet_ntop: guard extra work by IF_LINT
[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 /* Use this to suppress gcc's "...may be used before initialized" warnings.
42    Beware: The Code argument must not contain commas.  */
43 #ifndef IF_LINT
44 # ifdef lint
45 #  define IF_LINT(Code) Code
46 # else
47 #  define IF_LINT(Code) /* empty */
48 # endif
49 #endif
50
51 #if HAVE_DECL_INET_NTOP
52
53 # undef inet_ntop
54
55 const char *
56 rpl_inet_ntop (int af, const void *restrict src,
57                char *restrict dst, socklen_t cnt)
58 {
59   return inet_ntop (af, src, dst, cnt);
60 }
61
62 #else
63
64 # include <stdio.h>
65 # include <string.h>
66 # include <errno.h>
67
68 # define NS_IN6ADDRSZ 16
69 # define NS_INT16SZ 2
70
71 /*
72  * WARNING: Don't even consider trying to compile this on a system where
73  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
74  */
75 typedef int verify_int_size[4 <= sizeof (int) ? 1 : -1];
76
77 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
78 # if HAVE_IPV6
79 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
80 # endif
81
82
83 /* char *
84  * inet_ntop(af, src, dst, size)
85  *      convert a network format address to presentation format.
86  * return:
87  *      pointer to presentation format address ('dst'), or NULL (see errno).
88  * author:
89  *      Paul Vixie, 1996.
90  */
91 const char *
92 inet_ntop (int af, const void *restrict src,
93            char *restrict dst, socklen_t cnt)
94 {
95   switch (af)
96     {
97 # if HAVE_IPV4
98     case AF_INET:
99       return (inet_ntop4 (src, dst, cnt));
100 # endif
101
102 # if HAVE_IPV6
103     case AF_INET6:
104       return (inet_ntop6 (src, dst, cnt));
105 # endif
106
107     default:
108       errno = EAFNOSUPPORT;
109       return (NULL);
110     }
111   /* NOTREACHED */
112 }
113
114 /* const char *
115  * inet_ntop4(src, dst, size)
116  *      format an IPv4 address
117  * return:
118  *      'dst' (as a const)
119  * notes:
120  *      (1) uses no statics
121  *      (2) takes a u_char* not an in_addr as input
122  * author:
123  *      Paul Vixie, 1996.
124  */
125 static const char *
126 inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
127 {
128   char tmp[sizeof "255.255.255.255"];
129   int len;
130
131   len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
132   if (len < 0)
133     return NULL;
134
135   if (len > size)
136     {
137       errno = ENOSPC;
138       return NULL;
139     }
140
141   return strcpy (dst, tmp);
142 }
143
144 # if HAVE_IPV6
145
146 /* const char *
147  * inet_ntop6(src, dst, size)
148  *      convert IPv6 binary address into presentation (printable) format
149  * author:
150  *      Paul Vixie, 1996.
151  */
152 static const char *
153 inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
154 {
155   /*
156    * Note that int32_t and int16_t need only be "at least" large enough
157    * to contain a value of the specified size.  On some systems, like
158    * Crays, there is no such thing as an integer variable with 16 bits.
159    * Keep this in mind if you think this function should have been coded
160    * to use pointer overlays.  All the world's not a VAX.
161    */
162   char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
163   struct
164   {
165     int base, len;
166   } best, cur;
167   unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
168   int i;
169
170   /*
171    * Preprocess:
172    *      Copy the input (bytewise) array into a wordwise array.
173    *      Find the longest run of 0x00's in src[] for :: shorthanding.
174    */
175   memset (words, '\0', sizeof words);
176   for (i = 0; i < NS_IN6ADDRSZ; i += 2)
177     words[i / 2] = (src[i] << 8) | src[i + 1];
178   best.base = -1;
179   cur.base = -1;
180   IF_LINT(best.len = 0);
181   IF_LINT(cur.len = 0);
182   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
183     {
184       if (words[i] == 0)
185         {
186           if (cur.base == -1)
187             cur.base = i, cur.len = 1;
188           else
189             cur.len++;
190         }
191       else
192         {
193           if (cur.base != -1)
194             {
195               if (best.base == -1 || cur.len > best.len)
196                 best = cur;
197               cur.base = -1;
198             }
199         }
200     }
201   if (cur.base != -1)
202     {
203       if (best.base == -1 || cur.len > best.len)
204         best = cur;
205     }
206   if (best.base != -1 && best.len < 2)
207     best.base = -1;
208
209   /*
210    * Format the result.
211    */
212   tp = tmp;
213   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
214     {
215       /* Are we inside the best run of 0x00's? */
216       if (best.base != -1 && i >= best.base && i < (best.base + best.len))
217         {
218           if (i == best.base)
219             *tp++ = ':';
220           continue;
221         }
222       /* Are we following an initial run of 0x00s or any real hex? */
223       if (i != 0)
224         *tp++ = ':';
225       /* Is this address an encapsulated IPv4? */
226       if (i == 6 && best.base == 0 &&
227           (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
228         {
229           if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
230             return (NULL);
231           tp += strlen (tp);
232           break;
233         }
234       {
235         int len = sprintf (tp, "%x", words[i]);
236         if (len < 0)
237           return NULL;
238         tp += len;
239       }
240     }
241   /* Was it a trailing run of 0x00's? */
242   if (best.base != -1 && (best.base + best.len) ==
243       (NS_IN6ADDRSZ / NS_INT16SZ))
244     *tp++ = ':';
245   *tp++ = '\0';
246
247   /*
248    * Check for overflow, copy, and we're done.
249    */
250   if ((socklen_t) (tp - tmp) > size)
251     {
252       errno = ENOSPC;
253       return NULL;
254     }
255
256   return strcpy (dst, tmp);
257 }
258
259 # endif
260
261 #endif