]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/inet_ntop.c
75315ef35d25e0d65e15ff7a40f56a45424f9adc
2 * Shamelessly duplicated from the bind9 public sources
3 * for use by the Squid Project under ISC written permission
4 * included "as found" below.
6 * Update/Maintenance History:
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()
13 * 06-Oct-2007 : Various fixes to allow the build on MinGW
15 * 28-Oct-2007: drop some dead code. now tested working without.
17 * 04-Nov-2010: drop SPRINTF casting macro
21 * Original License and code follows.
27 #include "inet_ntop.h"
30 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
31 * Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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.
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 */
51 #include <sys/param.h>
54 #include <sys/types.h>
57 #include <sys/socket.h>
61 #include <netinet/in.h>
64 #include <arpa/inet.h>
66 #if HAVE_ARPA_NAMESER_H
67 #include <arpa/nameser.h>
77 #if ! defined(NS_INADDRSZ)
80 #if ! defined(NS_IN6ADDRSZ)
81 #define NS_IN6ADDRSZ 16
83 #if ! defined(NS_INT16SZ)
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.
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
);
96 * inet_ntop(af, src, dst, size)
97 * convert a network format address to presentation format.
99 * pointer to presentation format address (`dst'), or NULL (see errno).
104 xinet_ntop(af
, src
, dst
, size
)
112 return (inet_ntop4(src
, dst
, size
));
114 return (inet_ntop6(src
, dst
, size
));
116 errno
= EAFNOSUPPORT
;
123 * inet_ntop4(src, dst, size)
124 * format an IPv4 address
128 * (1) uses no statics
129 * (2) takes a u_char* not an in_addr as input
134 inet_ntop4(src
, dst
, size
)
139 static const char fmt
[] = "%u.%u.%u.%u";
140 char tmp
[sizeof "255.255.255.255"];
142 if (snprintf(tmp
, min(sizeof("255.255.255.255"),size
), fmt
, src
[0], src
[1], src
[2], src
[3]) >= size
) {
151 * inet_ntop6(src, dst, size)
152 * convert IPv6 binary address into presentation (printable) format
157 inet_ntop6(src
, dst
, size
)
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.
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
];
176 * Copy the input (bytewise) array into a wordwise array.
177 * Find the longest run of 0x00's in src[] for :: shorthanding.
179 memset(words
, '\0', sizeof words
);
180 for (i
= 0; i
< NS_IN6ADDRSZ
; i
++)
181 words
[i
/ 2] |= (src
[i
] << ((1 - (i
% 2)) << 3));
186 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++) {
189 cur
.base
= i
, cur
.len
= 1;
193 if (cur
.base
!= -1) {
194 if (best
.base
== -1 || cur
.len
> best
.len
)
200 if (cur
.base
!= -1) {
201 if (best
.base
== -1 || cur
.len
> best
.len
)
204 if (best
.base
!= -1 && best
.len
< 2)
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
)) {
219 /* Are we following an initial run of 0x00s or any real hex? */
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
)))
231 tp
+= snprintf(tp
, (tmp
+ sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") - tp
), "%x", words
[i
]);
233 /* Was it a trailing run of 0x00's? */
234 if (best
.base
!= -1 && (best
.base
+ best
.len
) ==
235 (NS_IN6ADDRSZ
/ NS_INT16SZ
))
240 * Check for overflow, copy, and we're done.
242 if ((size_t)(tp
- tmp
) > size
) {
250 #endif /* HAVE_INET_NTOP */