]> git.ipfire.org Git - thirdparty/squid.git/blame - compat/inet_ntop.c
Portability: fix windows socket lirary detection
[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 *
262a0e14 17 * Squid CVS $Id$
0e076fb1 18 *
19 * Original License and code follows.
20 */
21
22#include "config.h"
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
75#ifdef SPRINTF_CHAR
76# define SPRINTF(x) strlen(sprintf/**/x)
77#else
78# define SPRINTF(x) ((size_t)sprintf x)
79#endif
80
81#if ! defined(NS_INADDRSZ)
82#define NS_INADDRSZ 4
83#endif
84#if ! defined(NS_IN6ADDRSZ)
85#define NS_IN6ADDRSZ 16
86#endif
87#if ! defined(NS_INT16SZ)
88#define NS_INT16SZ 2
89#endif
90
91/*
92 * WARNING: Don't even consider trying to compile this on a system where
93 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
94 */
95
96static const char *inet_ntop4 (const u_char *src, char *dst, size_t size);
97static const char *inet_ntop6 (const u_char *src, char *dst, size_t size);
98
99/* char *
100 * inet_ntop(af, src, dst, size)
101 * convert a network format address to presentation format.
102 * return:
103 * pointer to presentation format address (`dst'), or NULL (see errno).
104 * author:
105 * Paul Vixie, 1996.
106 */
107const char *
108xinet_ntop(af, src, dst, size)
26ac0430
AJ
109int af;
110const void *src;
111char *dst;
112size_t size;
0e076fb1 113{
26ac0430
AJ
114 switch (af) {
115 case AF_INET:
116 return (inet_ntop4(src, dst, size));
117 case AF_INET6:
118 return (inet_ntop6(src, dst, size));
119 default:
120 errno = EAFNOSUPPORT;
121 return (NULL);
122 }
123 /* NOTREACHED */
0e076fb1 124}
125
126/* const char *
127 * inet_ntop4(src, dst, size)
128 * format an IPv4 address
129 * return:
130 * `dst' (as a const)
131 * notes:
132 * (1) uses no statics
133 * (2) takes a u_char* not an in_addr as input
134 * author:
135 * Paul Vixie, 1996.
136 */
137static const char *
138inet_ntop4(src, dst, size)
26ac0430
AJ
139const u_char *src;
140char *dst;
141size_t size;
0e076fb1 142{
26ac0430
AJ
143 static const char fmt[] = "%u.%u.%u.%u";
144 char tmp[sizeof "255.255.255.255"];
0e076fb1 145
26ac0430
AJ
146 if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) >= size) {
147 errno = ENOSPC;
148 return (NULL);
149 }
150 strcpy(dst, tmp);
151 return (dst);
0e076fb1 152}
153
154/* const char *
155 * inet_ntop6(src, dst, size)
156 * convert IPv6 binary address into presentation (printable) format
157 * author:
158 * Paul Vixie, 1996.
159 */
160static const char *
161inet_ntop6(src, dst, size)
26ac0430
AJ
162const u_char *src;
163char *dst;
164size_t size;
0e076fb1 165{
26ac0430
AJ
166 /*
167 * Note that int32_t and int16_t need only be "at least" large enough
168 * to contain a value of the specified size. On some systems, like
169 * Crays, there is no such thing as an integer variable with 16 bits.
170 * Keep this in mind if you think this function should have been coded
171 * to use pointer overlays. All the world's not a VAX.
172 */
173 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
174 struct { int base, len; } best, cur;
175 u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
176 int i;
0e076fb1 177
26ac0430
AJ
178 /*
179 * Preprocess:
180 * Copy the input (bytewise) array into a wordwise array.
181 * Find the longest run of 0x00's in src[] for :: shorthanding.
182 */
183 memset(words, '\0', sizeof words);
184 for (i = 0; i < NS_IN6ADDRSZ; i++)
185 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
186 best.base = -1;
187 best.len = 0;
188 cur.base = -1;
189 cur.len = 0;
190 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
191 if (words[i] == 0) {
192 if (cur.base == -1)
193 cur.base = i, cur.len = 1;
194 else
195 cur.len++;
196 } else {
197 if (cur.base != -1) {
198 if (best.base == -1 || cur.len > best.len)
199 best = cur;
200 cur.base = -1;
201 }
202 }
203 }
204 if (cur.base != -1) {
205 if (best.base == -1 || cur.len > best.len)
206 best = cur;
207 }
208 if (best.base != -1 && best.len < 2)
209 best.base = -1;
0e076fb1 210
26ac0430
AJ
211 /*
212 * Format the result.
213 */
214 tp = tmp;
215 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
216 /* Are we inside the best run of 0x00's? */
217 if (best.base != -1 && i >= best.base &&
218 i < (best.base + best.len)) {
219 if (i == best.base)
220 *tp++ = ':';
221 continue;
222 }
223 /* Are we following an initial run of 0x00s or any real hex? */
224 if (i != 0)
225 *tp++ = ':';
226 /* Is this address an encapsulated IPv4? */
227 if (i == 6 && best.base == 0 && (best.len == 6 ||
228 (best.len == 7 && words[7] != 0x0001) ||
229 (best.len == 5 && words[5] == 0xffff))) {
230 if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
231 return (NULL);
232 tp += strlen(tp);
233 break;
234 }
235 tp += SPRINTF((tp, "%x", words[i]));
236 }
237 /* Was it a trailing run of 0x00's? */
238 if (best.base != -1 && (best.base + best.len) ==
239 (NS_IN6ADDRSZ / NS_INT16SZ))
240 *tp++ = ':';
241 *tp++ = '\0';
0e076fb1 242
26ac0430
AJ
243 /*
244 * Check for overflow, copy, and we're done.
245 */
246 if ((size_t)(tp - tmp) > size) {
247 errno = ENOSPC;
248 return (NULL);
249 }
250 strcpy(dst, tmp);
251 return (dst);
0e076fb1 252}
253
254#endif /* HAVE_INET_NTOP */