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