]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/inet_ntop.c
75315ef35d25e0d65e15ff7a40f56a45424f9adc
[thirdparty/squid.git] / compat / inet_ntop.c
1 /*
2 * Shamelessly duplicated from the bind9 public sources
3 * for use by the Squid Project under ISC written permission
4 * included "as found" below.
5 *
6 * Update/Maintenance History:
7 *
8 * 24-Sep-2007 : Copied from bind 9.3.3
9 * - Added protection around libray headers
10 * - Altered configure checks
11 * - Un-hacked slightly to use system gethostbyname()
12 *
13 * 06-Oct-2007 : Various fixes to allow the build on MinGW
14 *
15 * 28-Oct-2007: drop some dead code. now tested working without.
16 *
17 * 04-Nov-2010: drop SPRINTF casting macro
18 *
19 * Squid CVS $Id$
20 *
21 * Original License and code follows.
22 */
23
24 #include "config.h"
25
26 #if !HAVE_INET_NTOP
27 #include "inet_ntop.h"
28
29 /*
30 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
31 * Copyright (c) 1996-1999 by Internet Software Consortium.
32 *
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies.
36 *
37 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
38 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
39 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
40 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
42 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
43 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 */
45
46 #if defined(LIBC_SCCS) && !defined(lint)
47 static const char rcsid[] = "inet_ntop.c,v 1.1.2.1.8.2 2005/11/03 23:08:40 marka Exp";
48 #endif /* LIBC_SCCS and not lint */
49
50 #if HAVE_SYS_PARAM_H
51 #include <sys/param.h>
52 #endif
53 #if HAVE_SYS_TYPES_H
54 #include <sys/types.h>
55 #endif
56 #if HAVE_SYS_SOCKET_H
57 #include <sys/socket.h>
58 #endif
59
60 #if HAVE_NETINET_IN_H
61 #include <netinet/in.h>
62 #endif
63 #if HAVE_ARPA_INET_H
64 #include <arpa/inet.h>
65 #endif
66 #if HAVE_ARPA_NAMESER_H
67 #include <arpa/nameser.h>
68 #endif
69
70 #if HAVE_ERRNO_H
71 #include <errno.h>
72 #endif
73 #if HAVE_STRING_H
74 #include <string.h>
75 #endif
76
77 #if ! defined(NS_INADDRSZ)
78 #define NS_INADDRSZ 4
79 #endif
80 #if ! defined(NS_IN6ADDRSZ)
81 #define NS_IN6ADDRSZ 16
82 #endif
83 #if ! defined(NS_INT16SZ)
84 #define NS_INT16SZ 2
85 #endif
86
87 /*
88 * WARNING: Don't even consider trying to compile this on a system where
89 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
90 */
91
92 static const char *inet_ntop4 (const u_char *src, char *dst, size_t size);
93 static const char *inet_ntop6 (const u_char *src, char *dst, size_t size);
94
95 /* char *
96 * inet_ntop(af, src, dst, size)
97 * convert a network format address to presentation format.
98 * return:
99 * pointer to presentation format address (`dst'), or NULL (see errno).
100 * author:
101 * Paul Vixie, 1996.
102 */
103 const char *
104 xinet_ntop(af, src, dst, size)
105 int af;
106 const void *src;
107 char *dst;
108 size_t size;
109 {
110 switch (af) {
111 case AF_INET:
112 return (inet_ntop4(src, dst, size));
113 case AF_INET6:
114 return (inet_ntop6(src, dst, size));
115 default:
116 errno = EAFNOSUPPORT;
117 return (NULL);
118 }
119 /* NOTREACHED */
120 }
121
122 /* const char *
123 * inet_ntop4(src, dst, size)
124 * format an IPv4 address
125 * return:
126 * `dst' (as a const)
127 * notes:
128 * (1) uses no statics
129 * (2) takes a u_char* not an in_addr as input
130 * author:
131 * Paul Vixie, 1996.
132 */
133 static const char *
134 inet_ntop4(src, dst, size)
135 const u_char *src;
136 char *dst;
137 size_t size;
138 {
139 static const char fmt[] = "%u.%u.%u.%u";
140 char tmp[sizeof "255.255.255.255"];
141
142 if (snprintf(tmp, min(sizeof("255.255.255.255"),size), fmt, src[0], src[1], src[2], src[3]) >= size) {
143 errno = ENOSPC;
144 return (NULL);
145 }
146 strcpy(dst, tmp);
147 return (dst);
148 }
149
150 /* const char *
151 * inet_ntop6(src, dst, size)
152 * convert IPv6 binary address into presentation (printable) format
153 * author:
154 * Paul Vixie, 1996.
155 */
156 static const char *
157 inet_ntop6(src, dst, size)
158 const u_char *src;
159 char *dst;
160 size_t size;
161 {
162 /*
163 * Note that int32_t and int16_t need only be "at least" large enough
164 * to contain a value of the specified size. On some systems, like
165 * Crays, there is no such thing as an integer variable with 16 bits.
166 * Keep this in mind if you think this function should have been coded
167 * to use pointer overlays. All the world's not a VAX.
168 */
169 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
170 struct { int base, len; } best, cur;
171 u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
172 int i;
173
174 /*
175 * Preprocess:
176 * Copy the input (bytewise) array into a wordwise array.
177 * Find the longest run of 0x00's in src[] for :: shorthanding.
178 */
179 memset(words, '\0', sizeof words);
180 for (i = 0; i < NS_IN6ADDRSZ; i++)
181 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
182 best.base = -1;
183 best.len = 0;
184 cur.base = -1;
185 cur.len = 0;
186 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
187 if (words[i] == 0) {
188 if (cur.base == -1)
189 cur.base = i, cur.len = 1;
190 else
191 cur.len++;
192 } else {
193 if (cur.base != -1) {
194 if (best.base == -1 || cur.len > best.len)
195 best = cur;
196 cur.base = -1;
197 }
198 }
199 }
200 if (cur.base != -1) {
201 if (best.base == -1 || cur.len > best.len)
202 best = cur;
203 }
204 if (best.base != -1 && best.len < 2)
205 best.base = -1;
206
207 /*
208 * Format the result.
209 */
210 tp = tmp;
211 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
212 /* Are we inside the best run of 0x00's? */
213 if (best.base != -1 && i >= best.base &&
214 i < (best.base + best.len)) {
215 if (i == best.base)
216 *tp++ = ':';
217 continue;
218 }
219 /* Are we following an initial run of 0x00s or any real hex? */
220 if (i != 0)
221 *tp++ = ':';
222 /* Is this address an encapsulated IPv4? */
223 if (i == 6 && best.base == 0 && (best.len == 6 ||
224 (best.len == 7 && words[7] != 0x0001) ||
225 (best.len == 5 && words[5] == 0xffff))) {
226 if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
227 return (NULL);
228 tp += strlen(tp);
229 break;
230 }
231 tp += snprintf(tp, (tmp + sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") - tp), "%x", words[i]);
232 }
233 /* Was it a trailing run of 0x00's? */
234 if (best.base != -1 && (best.base + best.len) ==
235 (NS_IN6ADDRSZ / NS_INT16SZ))
236 *tp++ = ':';
237 *tp++ = '\0';
238
239 /*
240 * Check for overflow, copy, and we're done.
241 */
242 if ((size_t)(tp - tmp) > size) {
243 errno = ENOSPC;
244 return (NULL);
245 }
246 strcpy(dst, tmp);
247 return (dst);
248 }
249
250 #endif /* HAVE_INET_NTOP */