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