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