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