Revert slightly, from Bruno.
[gnulib.git] / lib / inet_ntop.c
1 /* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
2    Copyright (c) 2005, 2006  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 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
60 #if HAVE_IPV6
61 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
62 #endif
63
64
65 /* char *
66  * inet_ntop(af, src, dst, size)
67  *      convert a network format address to presentation format.
68  * return:
69  *      pointer to presentation format address (`dst'), or NULL (see errno).
70  * author:
71  *      Paul Vixie, 1996.
72  */
73 const char *
74 inet_ntop (int af, const void *restrict src,
75            char *restrict dst, socklen_t cnt)
76 {
77   switch (af)
78     {
79 #if HAVE_IPV4
80     case AF_INET:
81       return (inet_ntop4 (src, dst, cnt));
82 #endif
83
84 #if HAVE_IPV6
85     case AF_INET6:
86       return (inet_ntop6 (src, dst, cnt));
87 #endif
88
89     default:
90       errno = EAFNOSUPPORT;
91       return (NULL);
92     }
93   /* NOTREACHED */
94 }
95
96 /* const char *
97  * inet_ntop4(src, dst, size)
98  *      format an IPv4 address
99  * return:
100  *      `dst' (as a const)
101  * notes:
102  *      (1) uses no statics
103  *      (2) takes a u_char* not an in_addr as input
104  * author:
105  *      Paul Vixie, 1996.
106  */
107 static const char *
108 inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
109 {
110   char tmp[sizeof "255.255.255.255"];
111   int len;
112
113   len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
114   if (len < 0)
115     return NULL;
116
117   if (len > size)
118     {
119       errno = ENOSPC;
120       return NULL;
121     }
122
123   return strcpy (dst, tmp);
124 }
125
126 #if HAVE_IPV6
127
128 /* const char *
129  * inet_ntop6(src, dst, size)
130  *      convert IPv6 binary address into presentation (printable) format
131  * author:
132  *      Paul Vixie, 1996.
133  */
134 static const char *
135 inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
136 {
137   /*
138    * Note that int32_t and int16_t need only be "at least" large enough
139    * to contain a value of the specified size.  On some systems, like
140    * Crays, there is no such thing as an integer variable with 16 bits.
141    * Keep this in mind if you think this function should have been coded
142    * to use pointer overlays.  All the world's not a VAX.
143    */
144   char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
145   struct
146   {
147     int base, len;
148   } best, cur;
149   unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
150   int i;
151
152   /*
153    * Preprocess:
154    *      Copy the input (bytewise) array into a wordwise array.
155    *      Find the longest run of 0x00's in src[] for :: shorthanding.
156    */
157   memset (words, '\0', sizeof words);
158   for (i = 0; i < NS_IN6ADDRSZ; i += 2)
159     words[i / 2] = (src[i] << 8) | src[i + 1];
160   best.base = -1;
161   cur.base = -1;
162   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
163     {
164       if (words[i] == 0)
165         {
166           if (cur.base == -1)
167             cur.base = i, cur.len = 1;
168           else
169             cur.len++;
170         }
171       else
172         {
173           if (cur.base != -1)
174             {
175               if (best.base == -1 || cur.len > best.len)
176                 best = cur;
177               cur.base = -1;
178             }
179         }
180     }
181   if (cur.base != -1)
182     {
183       if (best.base == -1 || cur.len > best.len)
184         best = cur;
185     }
186   if (best.base != -1 && best.len < 2)
187     best.base = -1;
188
189   /*
190    * Format the result.
191    */
192   tp = tmp;
193   for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
194     {
195       /* Are we inside the best run of 0x00's? */
196       if (best.base != -1 && i >= best.base && i < (best.base + best.len))
197         {
198           if (i == best.base)
199             *tp++ = ':';
200           continue;
201         }
202       /* Are we following an initial run of 0x00s or any real hex? */
203       if (i != 0)
204         *tp++ = ':';
205       /* Is this address an encapsulated IPv4? */
206       if (i == 6 && best.base == 0 &&
207           (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
208         {
209           if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
210             return (NULL);
211           tp += strlen (tp);
212           break;
213         }
214       {
215         int len = sprintf (tp, "%x", words[i]);
216         if (len < 0)
217           return NULL;
218         tp += len;
219       }
220     }
221   /* Was it a trailing run of 0x00's? */
222   if (best.base != -1 && (best.base + best.len) ==
223       (NS_IN6ADDRSZ / NS_INT16SZ))
224     *tp++ = ':';
225   *tp++ = '\0';
226
227   /*
228    * Check for overflow, copy, and we're done.
229    */
230   if ((socklen_t) (tp - tmp) > size)
231     {
232       errno = ENOSPC;
233       return NULL;
234     }
235
236   return strcpy (dst, tmp);
237 }
238
239 #endif