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