]>
Commit | Line | Data |
---|---|---|
41d93087 | 1 | /* |
41d93087 | 2 | * DEBUG: section 14 IP Storage and Handling |
3 | * AUTHOR: Amos Jeffries | |
4 | * | |
5 | * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ | |
6 | * ---------------------------------------------------------- | |
7 | * | |
8 | * Squid is the result of efforts by numerous individuals from the | |
9 | * Internet community. Development is led by Duane Wessels of the | |
10 | * National Laboratory for Applied Network Research and funded by the | |
11 | * National Science Foundation. Squid is Copyrighted (C) 1998 by | |
12 | * the Regents of the University of California. Please see the | |
13 | * COPYRIGHT file for full details. Squid incorporates software | |
14 | * developed and/or copyrighted by other sources. Please see the | |
15 | * CREDITS file for full details. | |
16 | * | |
565b233e | 17 | * This IpAddress code is copyright (C) 2007 by Treehouse Networks Ltd |
41d93087 | 18 | * of New Zealand. It is published and Lisenced as an extension of |
19 | * squid under the same conditions as the main squid application. | |
20 | * | |
21 | * This program is free software; you can redistribute it and/or modify | |
22 | * it under the terms of the GNU General Public License as published by | |
23 | * the Free Software Foundation; either version 2 of the License, or | |
24 | * (at your option) any later version. | |
26ac0430 | 25 | * |
41d93087 | 26 | * This program is distributed in the hope that it will be useful, |
27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
29 | * GNU General Public License for more details. | |
26ac0430 | 30 | * |
41d93087 | 31 | * You should have received a copy of the GNU General Public License |
32 | * along with this program; if not, write to the Free Software | |
33 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
34 | * | |
35 | */ | |
36 | ||
37 | #include "config.h" | |
82b7abe3 | 38 | #include "Debug.h" |
9837e5f0 | 39 | #include "ip/IpAddress.h" |
1ef0b9ce AJ |
40 | #include "util.h" |
41 | ||
41d93087 | 42 | |
43 | #if HAVE_ASSERT_H | |
44 | #include <assert.h> | |
45 | #endif | |
46 | #if HAVE_STDLIB_H | |
47 | #include <stdlib.h> | |
48 | #endif | |
49 | #if HAVE_STRING_H | |
50 | #include <string.h> | |
51 | #endif | |
52 | #if HAVE_ARPA_INET_H | |
53 | #include <arpa/inet.h> /* inet_ntoa() */ | |
54 | #endif | |
55 | ||
41d93087 | 56 | #ifdef INET6 |
57 | #error "INET6 defined but has been deprecated! Try running bootstrap and configure again." | |
58 | #endif | |
59 | ||
41d93087 | 60 | #if !USE_IPV6 |
61 | // So there are some places where I will drop to using Macros too. | |
62 | // At least I can restrict them to this file so they don't corrupt the app with C code. | |
63 | # define sin6_addr sin_addr | |
64 | # define sin6_port sin_port | |
65 | # define sin6_family sin_family | |
66 | #undef s6_addr | |
67 | # define s6_addr s_addr | |
68 | #endif | |
69 | ||
70 | static const unsigned int STRLEN_IP4A = 16; // aaa.bbb.ccc.ddd\0 | |
71 | static const unsigned int STRLEN_IP4R = 28; // ddd.ccc.bbb.aaa.in-addr.arpa.\0 | |
72 | static const unsigned int STRLEN_IP4S = 21; // ddd.ccc.bbb.aaa:ppppp\0 | |
73 | static const unsigned int MAX_IP4_STRLEN = STRLEN_IP4R; | |
74 | static const unsigned int STRLEN_IP6A = 42; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]/0 | |
75 | static const unsigned int STRLEN_IP6R = 75; // f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f ipv6.arpa./0 | |
76 | static const unsigned int STRLEN_IP6S = 48; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:00000/0 | |
77 | static const unsigned int MAX_IP6_STRLEN = STRLEN_IP6R; | |
78 | ||
79 | ||
80 | /* Debugging only. Dump the address content when a fatal assert is encountered. */ | |
81 | #if USE_IPV6 | |
82 | #define IASSERT(a,b) \ | |
68b313c2 | 83 | if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \ |
565b233e | 84 | printf("IpAddress invalid? with IsIPv4()=%c, IsIPv6()=%c\n",(IsIPv4()?'T':'F'),(IsIPv6()?'T':'F')); \ |
41d93087 | 85 | printf("ADDRESS:"); \ |
86 | for(unsigned int i = 0; i < sizeof(m_SocketAddr.sin6_addr); i++) { \ | |
87 | printf(" %x", m_SocketAddr.sin6_addr.s6_addr[i]); \ | |
88 | } printf("\n"); assert(b); \ | |
89 | } | |
90 | #else | |
91 | #define IASSERT(a,b) \ | |
68b313c2 | 92 | if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \ |
565b233e | 93 | printf("IpAddress invalid? with IsIPv4()=%c, IsIPv6()=%c\n",(IsIPv4()?'T':'F'),(IsIPv6()?'T':'F')); \ |
41d93087 | 94 | printf("ADDRESS: %x\n", (unsigned int)m_SocketAddr.sin_addr.s_addr); \ |
95 | assert(b); \ | |
96 | } | |
97 | #endif | |
98 | ||
23f6a720 | 99 | IpAddress::IpAddress() |
41d93087 | 100 | { |
101 | SetEmpty(); | |
102 | } | |
103 | ||
23f6a720 | 104 | IpAddress::~IpAddress() |
41d93087 | 105 | { |
565b233e | 106 | memset(this,0,sizeof(IpAddress)); |
41d93087 | 107 | } |
108 | ||
109 | int | |
565b233e | 110 | IpAddress::GetCIDR() const |
41d93087 | 111 | { |
112 | uint8_t shift,byte; | |
113 | uint8_t bit,caught; | |
114 | int len = 0; | |
115 | #if USE_IPV6 | |
df9617b9 | 116 | const uint8_t *ptr= m_SocketAddr.sin6_addr.s6_addr; |
41d93087 | 117 | #else |
df9617b9 | 118 | const uint8_t *ptr= (uint8_t *)&m_SocketAddr.sin_addr.s_addr; |
41d93087 | 119 | #endif |
120 | ||
121 | /* Let's scan all the bits from Most Significant to Least */ | |
122 | /* Until we find an "0" bit. Then, we return */ | |
123 | shift=0; | |
124 | ||
125 | #if USE_IPV6 | |
126 | /* return IPv4 CIDR for any Mapped address */ | |
127 | /* Thus only check the mapped bit */ | |
128 | ||
26ac0430 | 129 | if ( !IsIPv6() ) { |
41d93087 | 130 | shift = 12; |
131 | } | |
132 | ||
133 | #endif | |
134 | ||
e1381638 | 135 | for (; shift<sizeof(m_SocketAddr.sin6_addr) ; shift++) { |
41d93087 | 136 | byte= *(ptr+shift); |
137 | ||
138 | if (byte == 0xFF) { | |
139 | len += 8; | |
140 | continue ; /* A short-cut */ | |
141 | } | |
142 | ||
143 | for (caught = 0 , bit= 7 ; !caught && (bit <= 7); bit--) { | |
144 | caught = ((byte & 0x80) == 0x00); /* Found a '0' at 'bit' ? */ | |
145 | ||
146 | if (!caught) | |
147 | len++; | |
148 | ||
149 | byte <<= 1; | |
150 | } | |
151 | ||
152 | if (caught) | |
153 | break; /* We have found the most significant "0" bit. */ | |
154 | } | |
155 | ||
156 | return len; | |
157 | } | |
158 | ||
23f6a720 | 159 | const int IpAddress::ApplyMask(IpAddress const &mask_addr) |
41d93087 | 160 | { |
161 | uint32_t *p1 = (uint32_t*)(&m_SocketAddr.sin6_addr); | |
162 | uint32_t const *p2 = (uint32_t const *)(&mask_addr.m_SocketAddr.sin6_addr); | |
163 | unsigned int blen = sizeof(m_SocketAddr.sin6_addr)/sizeof(uint32_t); | |
164 | unsigned int changes = 0; | |
165 | ||
166 | for (unsigned int i = 0; i < blen; i++) { | |
26ac0430 | 167 | if ((p1[i] & p2[i]) != p1[i]) |
41d93087 | 168 | changes++; |
169 | ||
170 | p1[i] &= p2[i]; | |
171 | } | |
172 | ||
b5587d15 AJ |
173 | /* we have found a situation where mask forms or destroys a IPv4 map. */ |
174 | check4Mapped(); | |
175 | ||
41d93087 | 176 | return changes; |
177 | } | |
178 | ||
565b233e | 179 | bool IpAddress::ApplyMask(const unsigned int cidr, int mtype) |
41d93087 | 180 | { |
181 | uint8_t clearbits = 0; | |
182 | uint8_t* p = NULL; | |
183 | ||
184 | #if !USE_IPV6 | |
185 | IASSERT("mtype != AF_INET6", mtype != AF_INET6); /* using IPv6 in IPv4 is invalid. */ | |
186 | ||
26ac0430 | 187 | if (mtype == AF_UNSPEC) |
41d93087 | 188 | mtype = AF_INET; |
189 | ||
190 | #else | |
26ac0430 | 191 | if (mtype == AF_UNSPEC) |
41d93087 | 192 | mtype = AF_INET6; |
193 | ||
194 | #endif | |
195 | ||
196 | // validation and short-cuts. | |
197 | if (cidr > 128) | |
198 | return false; | |
199 | ||
200 | if (cidr > 32 && mtype == AF_INET) | |
201 | return false; | |
202 | ||
2c1b9590 AJ |
203 | if (cidr == 0) { |
204 | /* CIDR /0 is NoAddr regardless of the IPv4/IPv6 protocol */ | |
205 | SetNoAddr(); | |
206 | return true; | |
207 | } | |
208 | ||
41d93087 | 209 | clearbits = (uint8_t)( (mtype==AF_INET6?128:32) -cidr); |
210 | ||
211 | // short-cut | |
26ac0430 | 212 | if (clearbits == 0) |
41d93087 | 213 | return true; |
214 | ||
215 | #if USE_IPV6 | |
216 | ||
217 | p = (uint8_t*)(&m_SocketAddr.sin6_addr) + 15; | |
218 | ||
219 | #else | |
220 | ||
221 | p = (uint8_t*)(&m_SocketAddr.sin_addr) + 3; | |
222 | ||
223 | #endif | |
224 | ||
225 | for (; clearbits>0 && p >= (uint8_t*)&m_SocketAddr.sin6_addr ; p-- ) { | |
26ac0430 | 226 | if (clearbits < 8) { |
41d93087 | 227 | *p &= ((0xFF << clearbits) & 0xFF); |
228 | clearbits = 0; | |
229 | } else { | |
230 | *p &= 0x00; | |
231 | clearbits -= 8; | |
232 | } | |
233 | } | |
234 | ||
235 | return true; | |
236 | } | |
237 | ||
565b233e | 238 | bool IpAddress::IsSockAddr() const |
41d93087 | 239 | { |
240 | return (m_SocketAddr.sin6_port != 0); | |
241 | } | |
242 | ||
565b233e | 243 | bool IpAddress::IsIPv4() const |
41d93087 | 244 | { |
245 | #if USE_IPV6 | |
2c8fff41 | 246 | return IsAnyAddr() || IsNoAddr() || IN6_IS_ADDR_V4MAPPED( &m_SocketAddr.sin6_addr ); |
41d93087 | 247 | #else |
248 | return true; // enforce IPv4 in IPv4-only mode. | |
249 | #endif | |
250 | } | |
251 | ||
565b233e | 252 | bool IpAddress::IsIPv6() const |
41d93087 | 253 | { |
254 | #if USE_IPV6 | |
2c8fff41 | 255 | return IsAnyAddr() || IsNoAddr() || !IN6_IS_ADDR_V4MAPPED( &m_SocketAddr.sin6_addr ); |
41d93087 | 256 | #else |
257 | return false; // enforce IPv4 in IPv4-only mode. | |
258 | #endif | |
259 | } | |
260 | ||
565b233e | 261 | bool IpAddress::IsAnyAddr() const |
41d93087 | 262 | { |
263 | #if USE_IPV6 | |
2c8fff41 | 264 | return IN6_IS_ADDR_UNSPECIFIED( &m_SocketAddr.sin6_addr ); |
41d93087 | 265 | #else |
41d93087 | 266 | return (INADDR_ANY == m_SocketAddr.sin_addr.s_addr); |
267 | #endif | |
268 | } | |
269 | ||
270 | /// NOTE: Does NOT clear the Port stored. Ony the Address and Type. | |
565b233e | 271 | void IpAddress::SetAnyAddr() |
41d93087 | 272 | { |
273 | #if USE_IPV6 | |
274 | memset(&m_SocketAddr.sin6_addr, 0, sizeof(struct in6_addr) ); | |
275 | #else | |
276 | memset(&m_SocketAddr.sin_addr, 0, sizeof(struct in_addr) ); | |
277 | #endif | |
278 | } | |
279 | ||
565b233e AJ |
280 | /// NOTE: completely empties the IpAddress structure. Address, Port, Type, everything. |
281 | void IpAddress::SetEmpty() | |
41d93087 | 282 | { |
283 | memset(&m_SocketAddr, 0, sizeof(m_SocketAddr) ); | |
284 | } | |
285 | ||
565b233e | 286 | bool IpAddress::SetIPv4() |
41d93087 | 287 | { |
288 | #if USE_IPV6 | |
3ec1df9b | 289 | static const struct in6_addr v4_localhost = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
697675ab | 290 | 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 }} |
3ec1df9b A |
291 | }; |
292 | static const struct in6_addr v4_any = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
697675ab | 293 | 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }} |
3ec1df9b | 294 | }; |
41d93087 | 295 | |
26ac0430 | 296 | if ( IsLocalhost() ) { |
2c8fff41 | 297 | m_SocketAddr.sin6_addr = v4_localhost; |
41d93087 | 298 | return true; |
299 | } | |
41d93087 | 300 | |
26ac0430 | 301 | if ( IsAnyAddr() ) { |
2c8fff41 | 302 | m_SocketAddr.sin6_addr = v4_any; |
41d93087 | 303 | return true; |
304 | } | |
305 | ||
26ac0430 | 306 | if ( IsIPv4()) |
41d93087 | 307 | return true; |
308 | ||
309 | // anything non-IPv4 and non-convertable is BAD. | |
310 | return false; | |
311 | #else | |
312 | return true; // Always IPv4 in IPv4-only builds. | |
313 | #endif | |
314 | } | |
315 | ||
565b233e | 316 | bool IpAddress::IsLocalhost() const |
41d93087 | 317 | { |
318 | #if USE_IPV6 | |
3ec1df9b | 319 | static const struct in6_addr v4_localhost = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
2924aacc | 320 | 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 }} |
3ec1df9b | 321 | }; |
41d93087 | 322 | |
2c8fff41 AJ |
323 | return IN6_IS_ADDR_LOOPBACK( &m_SocketAddr.sin6_addr ) || IN6_ARE_ADDR_EQUAL( &m_SocketAddr.sin6_addr, &v4_localhost ); |
324 | #else | |
41d93087 | 325 | return (htonl(0x7F000001) == m_SocketAddr.sin_addr.s_addr); |
326 | #endif | |
327 | } | |
328 | ||
565b233e | 329 | void IpAddress::SetLocalhost() |
41d93087 | 330 | { |
331 | #if USE_IPV6 | |
332 | SetAnyAddr(); | |
333 | m_SocketAddr.sin6_addr.s6_addr[15] = 0x1; | |
334 | m_SocketAddr.sin6_family = AF_INET6; | |
335 | ||
336 | #else | |
337 | m_SocketAddr.sin_addr.s_addr = htonl(0x7F000001); | |
338 | m_SocketAddr.sin_family = AF_INET; | |
339 | #endif | |
340 | } | |
341 | ||
a98c2da5 AJ |
342 | bool IpAddress::IsSiteLocal6() const |
343 | { | |
344 | #if USE_IPV6 | |
2c8fff41 AJ |
345 | return m_SocketAddr.sin6_addr.s6_addr[0] == 0xfe && |
346 | (m_SocketAddr.sin6_addr.s6_addr[1] & 0x80); | |
a98c2da5 AJ |
347 | #else |
348 | return false; | |
349 | #endif | |
350 | } | |
351 | ||
352 | bool IpAddress::IsSlaac() const | |
353 | { | |
354 | #if USE_IPV6 | |
2c8fff41 AJ |
355 | return m_SocketAddr.sin6_addr.s6_addr[10] == htons(0xff) && |
356 | m_SocketAddr.sin6_addr.s6_addr[11] == htons(0xfe); | |
a98c2da5 AJ |
357 | #else |
358 | return false; | |
359 | #endif | |
360 | } | |
361 | ||
565b233e | 362 | bool IpAddress::IsNoAddr() const |
41d93087 | 363 | { |
364 | // IFF the address == 0xff..ff (all ones) | |
365 | #if USE_IPV6 | |
3ec1df9b A |
366 | static const struct in6_addr v6_noaddr = {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
367 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }} | |
368 | }; | |
41d93087 | 369 | |
2c8fff41 AJ |
370 | return IN6_ARE_ADDR_EQUAL( &m_SocketAddr.sin6_addr, &v6_noaddr ); |
371 | #else | |
41d93087 | 372 | return 0xFFFFFFFF == m_SocketAddr.sin_addr.s_addr; |
373 | #endif | |
374 | } | |
375 | ||
565b233e | 376 | void IpAddress::SetNoAddr() |
41d93087 | 377 | { |
378 | #if USE_IPV6 | |
379 | memset(&m_SocketAddr.sin6_addr, 0xFFFFFFFF, sizeof(struct in6_addr) ); | |
380 | m_SocketAddr.sin6_family = AF_INET6; | |
381 | #else | |
382 | memset(&m_SocketAddr.sin_addr, 0xFFFFFFFF, sizeof(struct in_addr) ); | |
383 | m_SocketAddr.sin_family = AF_INET; | |
384 | #endif | |
385 | } | |
386 | ||
387 | #if USE_IPV6 | |
388 | ||
565b233e | 389 | bool IpAddress::GetReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const |
41d93087 | 390 | { |
391 | char *p = buf; | |
392 | unsigned char const *r = dat.s6_addr; | |
393 | ||
394 | /* RFC1886 says: */ | |
395 | /* 4321:0:1:2:3:4:567:89ab */ | |
396 | /* must be sent */ | |
397 | /* b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.2.0.0.0.1.0.0.0.0.0.0.0.1.2.3.4.ip6.int. */ | |
398 | ||
399 | /* Work from the binary field. Anything else may have representation changes. */ | |
400 | /* The sin6_port and sin6_addr members shall be in network byte order. */ | |
401 | ||
402 | /* Compile Err: 'Too many arguments for format. */ | |
403 | ||
26ac0430 | 404 | for (int i = 15; i >= 0; i--, p+=4) { |
41d93087 | 405 | snprintf(p, 5, "%x.%x.", ((r[i])&0xf), (((r[i])>>4)&0xf) ); |
406 | } | |
407 | ||
408 | /* RFC3152 says: */ | |
409 | /* ip6.int is now deprecated TLD, use ip6.arpa instead. */ | |
410 | snprintf(p,10,"ip6.arpa."); | |
411 | ||
412 | return true; | |
413 | } | |
414 | ||
415 | #endif | |
416 | ||
565b233e | 417 | bool IpAddress::GetReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const |
41d93087 | 418 | { |
419 | unsigned int i = (unsigned int) ntohl(dat.s_addr); | |
420 | snprintf(buf, 32, "%u.%u.%u.%u.in-addr.arpa.", | |
421 | i & 255, | |
422 | (i >> 8) & 255, | |
423 | (i >> 16) & 255, | |
424 | (i >> 24) & 255); | |
425 | return true; | |
426 | } | |
427 | ||
565b233e | 428 | bool IpAddress::GetReverseString(char buf[MAX_IPSTRLEN], int show_type) const |
41d93087 | 429 | { |
430 | ||
26ac0430 | 431 | if (show_type == AF_UNSPEC) { |
41d93087 | 432 | #if USE_IPV6 |
433 | show_type = IsIPv6() ? AF_INET6 : AF_INET ; | |
434 | #else | |
435 | show_type = AF_INET; | |
436 | #endif | |
437 | } | |
438 | ||
439 | if (show_type == AF_INET && IsIPv4()) { | |
440 | #if USE_IPV6 | |
2c8fff41 AJ |
441 | struct in_addr* tmp = (struct in_addr*)&m_SocketAddr.sin6_addr.s6_addr[12]; |
442 | return GetReverseString4(buf, *tmp); | |
41d93087 | 443 | } else if ( show_type == AF_INET6 && IsIPv6() ) { |
444 | return GetReverseString6(buf, m_SocketAddr.sin6_addr); | |
445 | #else | |
446 | return GetReverseString4(buf, m_SocketAddr.sin_addr); | |
447 | #endif | |
448 | } | |
449 | ||
450 | debugs(14,0, "Unable to convert '" << NtoA(buf,MAX_IPSTRLEN) << "' to the rDNS type requested."); | |
451 | ||
452 | buf[0] = '\0'; | |
453 | ||
454 | return false; | |
455 | } | |
456 | ||
9fb4efad | 457 | IpAddress& IpAddress::operator =(const IpAddress &s) |
41d93087 | 458 | { |
565b233e | 459 | memcpy(this, &s, sizeof(IpAddress)); |
41d93087 | 460 | return *this; |
461 | }; | |
462 | ||
23f6a720 | 463 | IpAddress::IpAddress(const char*s) |
41d93087 | 464 | { |
465 | SetEmpty(); | |
466 | operator=(s); | |
467 | } | |
468 | ||
565b233e | 469 | bool IpAddress::operator =(const char* s) |
41d93087 | 470 | { |
471 | return LookupHostIP(s, true); | |
472 | } | |
473 | ||
565b233e | 474 | bool IpAddress::GetHostByName(const char* s) |
41d93087 | 475 | { |
476 | return LookupHostIP(s, false); | |
477 | } | |
478 | ||
565b233e | 479 | bool IpAddress::LookupHostIP(const char *s, bool nodns) |
41d93087 | 480 | { |
481 | int err = 0; | |
482 | ||
483 | short port = 0; | |
484 | ||
485 | struct addrinfo *res = NULL; | |
486 | ||
487 | struct addrinfo want; | |
488 | ||
489 | memset(&want, 0, sizeof(struct addrinfo)); | |
26ac0430 | 490 | if (nodns) { |
41d93087 | 491 | want.ai_flags = AI_NUMERICHOST; // prevent actual DNS lookups! |
492 | } | |
493 | #if !USE_IPV6 | |
494 | want.ai_family = AF_INET; | |
495 | #endif | |
496 | ||
497 | if ( (err = xgetaddrinfo(s, NULL, &want, &res)) != 0) { | |
3f697f0d | 498 | debugs(14,3, HERE << "Given Bad IP '" << s << "': " << xgai_strerror(err) ); |
41d93087 | 499 | /* free the memory xgetaddrinfo() dynamically allocated. */ |
26ac0430 | 500 | if (res) { |
41d93087 | 501 | xfreeaddrinfo(res); |
502 | res = NULL; | |
503 | } | |
504 | return false; | |
505 | } | |
506 | ||
507 | /* | |
508 | * NP: =(sockaddr_*) may alter the port. we don't want that. | |
509 | * all we have been given as input was an IPA. | |
510 | */ | |
511 | port = GetPort(); | |
512 | operator=(*res); | |
513 | SetPort(port); | |
514 | ||
515 | /* free the memory xgetaddrinfo() dynamically allocated. */ | |
516 | xfreeaddrinfo(res); | |
517 | ||
518 | res = NULL; | |
519 | ||
520 | return true; | |
521 | } | |
522 | ||
23f6a720 | 523 | IpAddress::IpAddress(struct sockaddr_in const &s) |
41d93087 | 524 | { |
525 | SetEmpty(); | |
526 | operator=(s); | |
527 | }; | |
528 | ||
23f6a720 | 529 | IpAddress& IpAddress::operator =(struct sockaddr_in const &s) |
41d93087 | 530 | { |
531 | #if USE_IPV6 | |
532 | Map4to6((const in_addr)s.sin_addr, m_SocketAddr.sin6_addr); | |
533 | m_SocketAddr.sin6_port = s.sin_port; | |
534 | m_SocketAddr.sin6_family = AF_INET6; | |
535 | #else | |
536 | ||
537 | memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in)); | |
538 | #endif | |
539 | ||
b5587d15 AJ |
540 | /* maintain stored family values properly */ |
541 | check4Mapped(); | |
542 | ||
41d93087 | 543 | return *this; |
544 | }; | |
545 | ||
23f6a720 | 546 | IpAddress& IpAddress::operator =(const struct sockaddr_storage &s) |
0e1a47f0 AJ |
547 | { |
548 | #if USE_IPV6 | |
549 | /* some AF_* magic to tell socket types apart and what we need to do */ | |
26ac0430 | 550 | if (s.ss_family == AF_INET6) { |
0e1a47f0 | 551 | memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in)); |
26ac0430 | 552 | } else { // convert it to our storage mapping. |
0e1a47f0 AJ |
553 | struct sockaddr_in *sin = (struct sockaddr_in*)&s; |
554 | m_SocketAddr.sin6_port = sin->sin_port; | |
555 | Map4to6( sin->sin_addr, m_SocketAddr.sin6_addr); | |
556 | } | |
557 | #else | |
558 | memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in)); | |
559 | #endif | |
560 | return *this; | |
561 | }; | |
562 | ||
b5587d15 AJ |
563 | void IpAddress::check4Mapped() |
564 | { | |
565 | // obsolete. | |
566 | // TODO use this NOW to set the sin6_family properly on exporting. not on import. | |
567 | } | |
568 | ||
41d93087 | 569 | #if USE_IPV6 |
23f6a720 | 570 | IpAddress::IpAddress(struct sockaddr_in6 const &s) |
41d93087 | 571 | { |
572 | SetEmpty(); | |
573 | operator=(s); | |
574 | }; | |
575 | ||
23f6a720 | 576 | IpAddress& IpAddress::operator =(struct sockaddr_in6 const &s) |
41d93087 | 577 | { |
578 | memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in6)); | |
b5587d15 AJ |
579 | |
580 | /* maintain address family properly */ | |
581 | check4Mapped(); | |
41d93087 | 582 | return *this; |
583 | }; | |
584 | ||
585 | #endif | |
586 | ||
23f6a720 | 587 | IpAddress::IpAddress(struct in_addr const &s) |
41d93087 | 588 | { |
589 | SetEmpty(); | |
590 | operator=(s); | |
591 | }; | |
592 | ||
23f6a720 | 593 | IpAddress& IpAddress::operator =(struct in_addr const &s) |
41d93087 | 594 | { |
595 | #if USE_IPV6 | |
596 | Map4to6((const in_addr)s, m_SocketAddr.sin6_addr); | |
597 | m_SocketAddr.sin6_family = AF_INET6; | |
b5587d15 | 598 | |
41d93087 | 599 | #else |
b5587d15 | 600 | |
41d93087 | 601 | memcpy(&m_SocketAddr.sin_addr, &s, sizeof(struct in_addr)); |
b5587d15 | 602 | |
41d93087 | 603 | #endif |
b5587d15 AJ |
604 | |
605 | /* maintain stored family type properly */ | |
606 | check4Mapped(); | |
607 | ||
41d93087 | 608 | return *this; |
609 | }; | |
610 | ||
611 | #if USE_IPV6 | |
612 | ||
23f6a720 | 613 | IpAddress::IpAddress(struct in6_addr const &s) |
41d93087 | 614 | { |
615 | SetEmpty(); | |
616 | operator=(s); | |
617 | }; | |
618 | ||
23f6a720 | 619 | IpAddress& IpAddress::operator =(struct in6_addr const &s) |
41d93087 | 620 | { |
b5587d15 | 621 | |
41d93087 | 622 | memcpy(&m_SocketAddr.sin6_addr, &s, sizeof(struct in6_addr)); |
623 | m_SocketAddr.sin6_family = AF_INET6; | |
b5587d15 AJ |
624 | |
625 | /* maintain address family type properly */ | |
626 | check4Mapped(); | |
627 | ||
41d93087 | 628 | return *this; |
629 | }; | |
630 | ||
631 | #endif | |
632 | ||
9fb4efad | 633 | IpAddress::IpAddress(const IpAddress &s) |
41d93087 | 634 | { |
635 | SetEmpty(); | |
636 | operator=(s); | |
637 | } | |
638 | ||
9fb4efad | 639 | IpAddress::IpAddress(IpAddress *s) |
41d93087 | 640 | { |
641 | SetEmpty(); | |
1a1f4104 | 642 | if (s) |
af6a12ee | 643 | memcpy(this, s, sizeof(IpAddress)); |
41d93087 | 644 | } |
645 | ||
23f6a720 | 646 | IpAddress::IpAddress(const struct hostent &s) |
41d93087 | 647 | { |
648 | SetEmpty(); | |
649 | operator=(s); | |
650 | } | |
651 | ||
565b233e | 652 | bool IpAddress::operator =(const struct hostent &s) |
41d93087 | 653 | { |
654 | ||
655 | struct in_addr* ipv4 = NULL; | |
656 | ||
657 | struct in6_addr* ipv6 = NULL; | |
658 | ||
659 | //struct hostent { | |
660 | // char *h_name; /* official name of host */ | |
661 | // char **h_aliases; /* alias list */ | |
662 | // int h_addrtype; /* host address type */ | |
663 | // int h_length; /* length of address */ | |
664 | // char **h_addr_list; /* list of addresses */ | |
665 | //} | |
666 | ||
26ac0430 | 667 | switch (s.h_addrtype) { |
41d93087 | 668 | |
669 | case AF_INET: | |
670 | ipv4 = (in_addr*)(s.h_addr_list[0]); | |
671 | /* this */ | |
672 | operator=(*ipv4); | |
673 | break; | |
674 | ||
675 | case AF_INET6: | |
676 | ipv6 = (in6_addr*)(s.h_addr_list[0]); | |
677 | #if USE_IPV6 | |
678 | /* this */ | |
679 | operator=(*ipv6); | |
680 | #else | |
681 | ||
682 | debugs(14,1, HERE << "Discarded IPv6 Address. Protocol disabled."); | |
683 | ||
684 | // FIXME see if there is another address in the list that might be usable ?? | |
685 | return false; | |
686 | #endif | |
687 | ||
688 | break; | |
689 | ||
690 | default: | |
26ac0430 AJ |
691 | IASSERT("false",false); |
692 | return false; | |
41d93087 | 693 | } |
694 | ||
695 | return true; | |
696 | } | |
697 | ||
23f6a720 | 698 | IpAddress::IpAddress(const struct addrinfo &s) |
41d93087 | 699 | { |
700 | SetEmpty(); | |
701 | operator=(s); | |
702 | } | |
703 | ||
565b233e | 704 | bool IpAddress::operator =(const struct addrinfo &s) |
41d93087 | 705 | { |
706 | ||
707 | struct sockaddr_in* ipv4 = NULL; | |
708 | ||
709 | struct sockaddr_in6* ipv6 = NULL; | |
710 | ||
711 | //struct addrinfo { | |
712 | // int ai_flags; /* input flags */ | |
713 | // int ai_family; /* protocol family for socket */ | |
714 | // int ai_socktype; /* socket type */ | |
715 | // int ai_protocol; /* protocol for socket */ | |
716 | // socklen_t ai_addrlen; /* length of socket-address */ | |
717 | // struct sockaddr *ai_addr; /* socket-address for socket */ | |
718 | // char *ai_canonname; /* canonical name for service location */ | |
719 | // struct addrinfo *ai_next; /* pointer to next in list */ | |
720 | //} | |
721 | ||
26ac0430 | 722 | switch (s.ai_family) { |
41d93087 | 723 | |
724 | case AF_INET: | |
725 | ipv4 = (sockaddr_in*)(s.ai_addr); | |
726 | /* this */ | |
727 | assert(ipv4); | |
728 | operator=(*ipv4); | |
729 | break; | |
730 | ||
731 | case AF_INET6: | |
732 | ipv6 = (sockaddr_in6*)(s.ai_addr); | |
733 | #if USE_IPV6 | |
734 | /* this */ | |
735 | assert(ipv6); | |
736 | operator=(*ipv6); | |
737 | #else | |
738 | ||
739 | debugs(14,1, HERE << "Discarded IPv6 Address. Protocol disabled."); | |
740 | ||
741 | // see if there is another address in the list that might be usable ?? | |
742 | ||
743 | if (s.ai_next) | |
744 | return operator=(*s.ai_next); | |
745 | else | |
746 | return false; | |
747 | ||
748 | #endif | |
749 | break; | |
750 | ||
751 | case AF_UNSPEC: | |
752 | default: | |
753 | // attempt to handle partially initialised addrinfo. | |
754 | // such as those where data only comes from getsockopt() | |
26ac0430 | 755 | if (s.ai_addr != NULL) { |
41d93087 | 756 | #if USE_IPV6 |
26ac0430 | 757 | if (s.ai_addrlen == sizeof(struct sockaddr_in6)) { |
41d93087 | 758 | operator=(*((struct sockaddr_in6*)s.ai_addr)); |
759 | return true; | |
26ac0430 | 760 | } else |
41d93087 | 761 | #endif |
26ac0430 AJ |
762 | if (s.ai_addrlen == sizeof(struct sockaddr_in)) { |
763 | operator=(*((struct sockaddr_in*)s.ai_addr)); | |
764 | return true; | |
765 | } | |
41d93087 | 766 | } |
767 | return false; | |
768 | } | |
769 | ||
770 | return true; | |
771 | } | |
772 | ||
565b233e | 773 | void IpAddress::GetAddrInfo(struct addrinfo *&dst, int force) const |
41d93087 | 774 | { |
26ac0430 | 775 | if (dst == NULL) { |
41d93087 | 776 | dst = new addrinfo; |
777 | } | |
778 | ||
779 | memset(dst, 0, sizeof(struct addrinfo)); | |
780 | ||
781 | // set defaults | |
782 | dst->ai_flags = AI_NUMERICHOST; | |
783 | ||
26ac0430 | 784 | if (dst->ai_socktype == 0) |
41d93087 | 785 | dst->ai_socktype = SOCK_STREAM; |
786 | ||
26ac0430 | 787 | if (dst->ai_socktype == SOCK_STREAM // implies TCP |
41d93087 | 788 | && dst->ai_protocol == 0) |
789 | dst->ai_protocol = IPPROTO_TCP; | |
790 | ||
26ac0430 | 791 | if (dst->ai_socktype == SOCK_DGRAM // implies UDP |
41d93087 | 792 | && dst->ai_protocol == 0) |
793 | dst->ai_protocol = IPPROTO_UDP; | |
794 | ||
795 | #if USE_IPV6 | |
26ac0430 | 796 | if ( force == AF_INET6 || (force == AF_UNSPEC && IsIPv6()) ) { |
41d93087 | 797 | dst->ai_addr = (struct sockaddr*)new sockaddr_in6; |
798 | ||
799 | memset(dst->ai_addr,0,sizeof(struct sockaddr_in6)); | |
800 | ||
801 | GetSockAddr(*((struct sockaddr_in6*)dst->ai_addr)); | |
802 | ||
803 | dst->ai_addrlen = sizeof(struct sockaddr_in6); | |
804 | ||
805 | dst->ai_family = ((struct sockaddr_in6*)dst->ai_addr)->sin6_family; | |
9d92af86 AJ |
806 | |
807 | #if 0 | |
808 | /** | |
809 | * Enable only if you must and please report to squid-dev if you find a need for this. | |
810 | * | |
811 | * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY). | |
812 | * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx | |
813 | * Linux appears to only do some things when its present. | |
814 | * (93) Bad Protocol | |
815 | * FreeBSD dies horribly when using dual-stack with it set. | |
816 | * (43) Protocol not supported | |
817 | */ | |
41d93087 | 818 | dst->ai_protocol = IPPROTO_IPV6; |
9d92af86 AJ |
819 | #endif |
820 | ||
41d93087 | 821 | } else |
822 | #endif | |
26ac0430 | 823 | if ( force == AF_INET || (force == AF_UNSPEC && IsIPv4()) ) { |
41d93087 | 824 | |
825 | dst->ai_addr = (struct sockaddr*)new sockaddr_in; | |
826 | ||
827 | memset(dst->ai_addr,0,sizeof(struct sockaddr_in)); | |
828 | ||
829 | GetSockAddr(*((struct sockaddr_in*)dst->ai_addr)); | |
830 | ||
831 | dst->ai_addrlen = sizeof(struct sockaddr_in); | |
832 | ||
833 | dst->ai_family = ((struct sockaddr_in*)dst->ai_addr)->sin_family; | |
26ac0430 | 834 | } else { |
41d93087 | 835 | IASSERT("false",false); |
836 | } | |
837 | } | |
838 | ||
565b233e | 839 | void IpAddress::InitAddrInfo(struct addrinfo *&ai) const |
41d93087 | 840 | { |
26ac0430 | 841 | if (ai == NULL) { |
41d93087 | 842 | ai = new addrinfo; |
843 | memset(ai,0,sizeof(struct addrinfo)); | |
844 | } | |
845 | ||
846 | // remove any existing data. | |
26ac0430 | 847 | if (ai->ai_addr) delete ai->ai_addr; |
41d93087 | 848 | |
849 | ai->ai_addr = (struct sockaddr*)new sockaddr_in6; | |
850 | memset(ai->ai_addr, 0, sizeof(struct sockaddr_in6)); | |
851 | ||
852 | ai->ai_addrlen = sizeof(struct sockaddr_in6); | |
853 | ||
854 | } | |
855 | ||
565b233e | 856 | void IpAddress::FreeAddrInfo(struct addrinfo *&ai) const |
41d93087 | 857 | { |
26ac0430 | 858 | if (ai == NULL) return; |
41d93087 | 859 | |
26ac0430 | 860 | if (ai->ai_addr) delete ai->ai_addr; |
41d93087 | 861 | |
862 | ai->ai_addr = NULL; | |
863 | ||
864 | ai->ai_addrlen = 0; | |
865 | ||
866 | // NP: name fields are NOT allocated at present. | |
867 | delete ai; | |
868 | ||
869 | ai = NULL; | |
870 | } | |
871 | ||
23f6a720 | 872 | int IpAddress::matchIPAddr(const IpAddress &rhs) const |
41d93087 | 873 | { |
874 | #if USE_IPV6 | |
875 | uint8_t *l = (uint8_t*)m_SocketAddr.sin6_addr.s6_addr; | |
876 | uint8_t *r = (uint8_t*)rhs.m_SocketAddr.sin6_addr.s6_addr; | |
877 | #else | |
878 | uint8_t *l = (uint8_t*)&m_SocketAddr.sin_addr.s_addr; | |
879 | uint8_t *r = (uint8_t*)&rhs.m_SocketAddr.sin_addr.s_addr; | |
880 | #endif | |
881 | ||
882 | // loop a byte-wise compare | |
883 | // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR | |
884 | // expected difference on CIDR is gt/eq or lt/eq ONLY. | |
26ac0430 | 885 | for (unsigned int i = 0 ; i < sizeof(m_SocketAddr.sin6_addr) ; i++) { |
41d93087 | 886 | |
26ac0430 | 887 | if (l[i] < r[i]) |
41d93087 | 888 | return -1; |
889 | ||
26ac0430 | 890 | if (l[i] > r[i]) |
41d93087 | 891 | return 1; |
892 | } | |
893 | ||
894 | return 0; | |
895 | } | |
896 | ||
23f6a720 | 897 | bool IpAddress::operator ==(const IpAddress &s) const |
41d93087 | 898 | { |
899 | return (0 == matchIPAddr(s)); | |
900 | } | |
901 | ||
23f6a720 | 902 | bool IpAddress::operator !=(const IpAddress &s) const |
41d93087 | 903 | { |
904 | return ! ( operator==(s) ); | |
905 | } | |
906 | ||
23f6a720 | 907 | bool IpAddress::operator <=(const IpAddress &rhs) const |
41d93087 | 908 | { |
26ac0430 | 909 | if (IsAnyAddr() && !rhs.IsAnyAddr()) |
41d93087 | 910 | return true; |
911 | ||
912 | return (matchIPAddr(rhs) <= 0); | |
913 | } | |
914 | ||
23f6a720 | 915 | bool IpAddress::operator >=(const IpAddress &rhs) const |
41d93087 | 916 | { |
26ac0430 | 917 | if (IsNoAddr() && !rhs.IsNoAddr()) |
41d93087 | 918 | return true; |
919 | ||
920 | return ( matchIPAddr(rhs) >= 0); | |
921 | } | |
922 | ||
23f6a720 | 923 | bool IpAddress::operator >(const IpAddress &rhs) const |
41d93087 | 924 | { |
26ac0430 | 925 | if (IsNoAddr() && !rhs.IsNoAddr()) |
41d93087 | 926 | return true; |
927 | ||
928 | return ( matchIPAddr(rhs) > 0); | |
929 | } | |
930 | ||
23f6a720 | 931 | bool IpAddress::operator <(const IpAddress &rhs) const |
41d93087 | 932 | { |
26ac0430 | 933 | if (IsNoAddr() && !rhs.IsNoAddr()) |
41d93087 | 934 | return true; |
935 | ||
936 | return ( matchIPAddr(rhs) < 0); | |
937 | } | |
938 | ||
565b233e | 939 | u_short IpAddress::GetPort() const |
41d93087 | 940 | { |
941 | return ntohs( m_SocketAddr.sin6_port ); | |
942 | } | |
943 | ||
565b233e | 944 | u_short IpAddress::SetPort(u_short prt) |
41d93087 | 945 | { |
946 | m_SocketAddr.sin6_port = htons(prt); | |
947 | ||
948 | return prt; | |
949 | } | |
950 | ||
951 | /** | |
952 | * NtoA Given a buffer writes a readable ascii version of the IPA and/or port stored | |
953 | * | |
954 | * Buffer must be of a size large enough to hold the converted address. | |
955 | * This size is provided in the form of a global defined variable MAX_IPSTRLEN | |
956 | * Should a buffer shorter be provided the string result will be truncated | |
957 | * at the length of the available buffer. | |
958 | * | |
959 | * A copy of the buffer is also returned for simple immediate display. | |
960 | */ | |
565b233e | 961 | char* IpAddress::NtoA(char* buf, const unsigned int blen, int force) const |
41d93087 | 962 | { |
963 | // Ensure we have a buffer. | |
26ac0430 | 964 | if (buf == NULL) { |
41d93087 | 965 | return NULL; |
966 | } | |
967 | ||
968 | /* some external code may have blindly memset a parent. */ | |
969 | /* thats okay, our default is known */ | |
26ac0430 | 970 | if ( IsAnyAddr() ) { |
41d93087 | 971 | #if USE_IPV6 |
d85c3078 | 972 | memcpy(buf,"::\0", min((const unsigned int)3,blen)); |
41d93087 | 973 | #else |
d85c3078 | 974 | memcpy(buf,"0.0.0.0\0", min((const unsigned int)8,blen)); |
41d93087 | 975 | #endif |
976 | return buf; | |
977 | } | |
978 | ||
979 | memset(buf,0,blen); // clear buffer before write | |
980 | ||
981 | /* Pure-IPv6 CANNOT be displayed in IPv4 format. */ | |
982 | /* However IPv4 CAN. */ | |
26ac0430 AJ |
983 | if ( force == AF_INET && !IsIPv4() ) { |
984 | if ( IsIPv6() ) { | |
d85c3078 | 985 | memcpy(buf, "{!IPv4}\0", min((const unsigned int)8,blen)); |
41d93087 | 986 | } |
987 | return buf; | |
988 | } | |
989 | ||
990 | #if USE_IPV6 | |
26ac0430 | 991 | if ( force == AF_INET6 || (force == AF_UNSPEC && IsIPv6()) ) { |
41d93087 | 992 | |
993 | xinet_ntop(AF_INET6, &m_SocketAddr.sin6_addr, buf, blen); | |
994 | ||
995 | } else if ( force == AF_INET || (force == AF_UNSPEC && IsIPv4()) ) { | |
996 | ||
997 | struct in_addr tmp; | |
998 | GetInAddr(tmp); | |
999 | xinet_ntop(AF_INET, &tmp, buf, blen); | |
1000 | #else | |
26ac0430 | 1001 | if ( force == AF_UNSPEC || (force == AF_INET && IsIPv4()) ) { |
41d93087 | 1002 | xinet_ntop(AF_INET, &m_SocketAddr.sin_addr, buf, blen); |
1003 | #endif | |
1004 | } else { | |
1005 | debugs(14,0,"WARNING: Corrupt IP Address details OR required to display in unknown format (" << | |
26ac0430 | 1006 | force << "). accepted={" << AF_UNSPEC << "," << AF_INET << "," << AF_INET6 << "}"); |
41d93087 | 1007 | fprintf(stderr,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ", |
1008 | force, AF_UNSPEC, AF_INET, AF_INET6); | |
d85c3078 | 1009 | memcpy(buf,"dead:beef::\0", min((const unsigned int)13,blen)); |
41d93087 | 1010 | assert(false); |
1011 | } | |
1012 | ||
1013 | return buf; | |
1014 | } | |
1015 | ||
e1381638 AJ |
1016 | unsigned int IpAddress::ToHostname(char *buf, const unsigned int blen) const |
1017 | { | |
41d93087 | 1018 | char *p = buf; |
1019 | ||
26ac0430 | 1020 | if (IsIPv6() && blen > 0) { |
41d93087 | 1021 | *p = '['; |
1022 | p++; | |
1023 | } | |
1024 | ||
1025 | /* 7 being space for [,], and port */ | |
26ac0430 | 1026 | if ( IsIPv6() ) |
41d93087 | 1027 | NtoA(p, blen-7, AF_INET6); |
1028 | else | |
1029 | NtoA(p, blen-7, AF_INET); | |
1030 | ||
1031 | // find the end of the new string | |
26ac0430 | 1032 | while (*p != '\0' && p < buf+blen) |
41d93087 | 1033 | p++; |
1034 | ||
26ac0430 | 1035 | if (IsIPv6() && p < (buf+blen-1) ) { |
41d93087 | 1036 | *p = ']'; |
1037 | p++; | |
1038 | } | |
1039 | ||
1040 | /* terminate just in case. */ | |
1041 | *p = '\0'; | |
1042 | ||
1043 | /* return size of buffer now used */ | |
1044 | return (p - buf); | |
1045 | } | |
1046 | ||
e1381638 AJ |
1047 | char* IpAddress::ToURL(char* buf, unsigned int blen) const |
1048 | { | |
41d93087 | 1049 | char *p = buf; |
1050 | ||
1051 | // Ensure we have a buffer. | |
1052 | ||
26ac0430 | 1053 | if (buf == NULL) { |
41d93087 | 1054 | return NULL; |
1055 | } | |
1056 | ||
1057 | p += ToHostname(p, blen); | |
1058 | ||
26ac0430 | 1059 | if (m_SocketAddr.sin6_port > 0 && p < (buf+blen-6) ) { |
41d93087 | 1060 | /* 6 is max length of expected ':port' (short int) */ |
1061 | snprintf(p, 6,":%d", GetPort() ); | |
1062 | } | |
1063 | ||
1064 | // force a null-terminated string | |
303bfd76 | 1065 | buf[blen-1] = '\0'; |
41d93087 | 1066 | |
1067 | return buf; | |
1068 | } | |
1069 | ||
e1381638 AJ |
1070 | void IpAddress::GetSockAddr(struct sockaddr_storage &addr, const int family) const |
1071 | { | |
52b694c2 AJ |
1072 | struct sockaddr_in *sin = NULL; |
1073 | ||
26ac0430 | 1074 | if ( family == AF_INET && !IsIPv4()) { |
0e1a47f0 | 1075 | // FIXME INET6: caller using the wrong socket type! |
565b233e | 1076 | debugs(14, DBG_CRITICAL, HERE << "IpAddress::GetSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this); |
0e1a47f0 AJ |
1077 | assert(false); |
1078 | } | |
1079 | ||
1080 | #if USE_IPV6 | |
26ac0430 | 1081 | if ( family == AF_INET6 || (family == AF_UNSPEC && IsIPv6()) ) { |
1ef0b9ce AJ |
1082 | struct sockaddr_in6 *ss6 = (struct sockaddr_in6*)&addr; |
1083 | GetSockAddr(*ss6); | |
26ac0430 | 1084 | } else if ( family == AF_INET || (family == AF_UNSPEC && IsIPv4()) ) { |
52b694c2 | 1085 | sin = (struct sockaddr_in*)&addr; |
1ef0b9ce | 1086 | GetSockAddr(*sin); |
52b694c2 AJ |
1087 | } else { |
1088 | IASSERT("false",false); | |
0e1a47f0 | 1089 | } |
8c37ea44 | 1090 | #else /* not USE_IPV6 */ |
52b694c2 | 1091 | sin = (struct sockaddr_in*)&addr; |
1ef0b9ce | 1092 | GetSockAddr(*sin); |
8c37ea44 | 1093 | #endif /* USE_IPV6 */ |
0e1a47f0 AJ |
1094 | } |
1095 | ||
e1381638 AJ |
1096 | void IpAddress::GetSockAddr(struct sockaddr_in &buf) const |
1097 | { | |
41d93087 | 1098 | #if USE_IPV6 |
1099 | ||
26ac0430 | 1100 | if ( IsIPv4() ) { |
41d93087 | 1101 | buf.sin_family = AF_INET; |
1102 | buf.sin_port = m_SocketAddr.sin6_port; | |
1103 | Map6to4( m_SocketAddr.sin6_addr, buf.sin_addr); | |
26ac0430 | 1104 | } else { |
565b233e | 1105 | debugs(14, DBG_CRITICAL, HERE << "IpAddress::GetSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this ); |
41d93087 | 1106 | |
1107 | memset(&buf,0xFFFFFFFF,sizeof(struct sockaddr_in)); | |
1108 | assert(false); | |
1109 | } | |
1110 | ||
1111 | #else | |
1112 | ||
1113 | memcpy(&buf, &m_SocketAddr, sizeof(struct sockaddr_in)); | |
1114 | ||
26ac0430 | 1115 | if (buf.sin_family == 0) { |
41d93087 | 1116 | buf.sin_family = AF_INET; |
1117 | } | |
1118 | ||
1119 | #endif | |
12f45551 AJ |
1120 | |
1121 | #if HAVE_SIN_LEN_IN_SAI | |
1122 | /* not all OS have this field, BUT when they do it can be a problem if set wrong */ | |
1ef0b9ce | 1123 | buf.sin_len = sizeof(struct sockaddr_in); |
12f45551 AJ |
1124 | #endif |
1125 | ||
41d93087 | 1126 | } |
1127 | ||
1128 | #if USE_IPV6 | |
1129 | ||
e1381638 AJ |
1130 | void IpAddress::GetSockAddr(struct sockaddr_in6 &buf) const |
1131 | { | |
41d93087 | 1132 | memcpy(&buf, &m_SocketAddr, sizeof(struct sockaddr_in6)); |
1133 | /* maintain address family. It may have changed inside us. */ | |
1134 | buf.sin6_family = AF_INET6; | |
12f45551 AJ |
1135 | |
1136 | #if HAVE_SIN6_LEN_IN_SAI | |
1137 | /* not all OS have this field, BUT when they do it can be a problem if set wrong */ | |
1ef0b9ce | 1138 | buf.sin6_len = sizeof(struct sockaddr_in6); |
12f45551 | 1139 | #endif |
41d93087 | 1140 | } |
1141 | ||
1142 | #endif | |
1143 | ||
1144 | #if USE_IPV6 | |
1145 | ||
e1381638 AJ |
1146 | void IpAddress::Map4to6(const struct in_addr &in, struct in6_addr &out) const |
1147 | { | |
41d93087 | 1148 | /* check for special cases */ |
1149 | ||
26ac0430 | 1150 | if ( in.s_addr == 0x00000000) { |
41d93087 | 1151 | /* ANYADDR */ |
41d93087 | 1152 | memset(&out, 0, sizeof(struct in6_addr)); |
26ac0430 | 1153 | } else if ( in.s_addr == 0xFFFFFFFF) { |
41d93087 | 1154 | /* NOADDR */ |
2c8fff41 | 1155 | memset(&out, 255, sizeof(struct in6_addr)); |
26ac0430 | 1156 | } else { |
41d93087 | 1157 | /* general */ |
41d93087 | 1158 | memset(&out, 0, sizeof(struct in6_addr)); |
2c8fff41 AJ |
1159 | out.s6_addr[10] = 0xFF; |
1160 | out.s6_addr[11] = 0xFF; | |
1161 | out.s6_addr[12] = ((uint8_t *)&in.s_addr)[0]; | |
1162 | out.s6_addr[13] = ((uint8_t *)&in.s_addr)[1]; | |
1163 | out.s6_addr[14] = ((uint8_t *)&in.s_addr)[2]; | |
1164 | out.s6_addr[15] = ((uint8_t *)&in.s_addr)[3]; | |
41d93087 | 1165 | } |
1166 | } | |
1167 | ||
e1381638 AJ |
1168 | void IpAddress::Map6to4(const struct in6_addr &in, struct in_addr &out) const |
1169 | { | |
41d93087 | 1170 | /* ANYADDR */ |
1171 | /* NOADDR */ | |
1172 | /* general */ | |
1173 | ||
1174 | memset(&out, 0, sizeof(struct in_addr)); | |
2c8fff41 AJ |
1175 | ((uint8_t *)&out.s_addr)[0] = in.s6_addr[12]; |
1176 | ((uint8_t *)&out.s_addr)[1] = in.s6_addr[13]; | |
1177 | ((uint8_t *)&out.s_addr)[2] = in.s6_addr[14]; | |
1178 | ((uint8_t *)&out.s_addr)[3] = in.s6_addr[15]; | |
41d93087 | 1179 | } |
1180 | ||
1181 | #endif | |
1182 | ||
1183 | #if USE_IPV6 | |
e1381638 AJ |
1184 | void IpAddress::GetInAddr(in6_addr &buf) const |
1185 | { | |
41d93087 | 1186 | memcpy(&buf, &m_SocketAddr.sin6_addr, sizeof(struct in6_addr)); |
1187 | } | |
1188 | ||
1189 | #endif | |
1190 | ||
e1381638 AJ |
1191 | bool IpAddress::GetInAddr(struct in_addr &buf) const |
1192 | { | |
41d93087 | 1193 | |
1194 | #if USE_IPV6 | |
26ac0430 | 1195 | if ( IsIPv4() ) { |
41d93087 | 1196 | Map6to4((const in6_addr)m_SocketAddr.sin6_addr, buf); |
1197 | return true; | |
1198 | } | |
1199 | #else | |
1200 | ||
26ac0430 | 1201 | if ( IsIPv4() ) { |
41d93087 | 1202 | memcpy(&buf, &m_SocketAddr.sin_addr, sizeof(struct in_addr)); |
1203 | return true; | |
1204 | } | |
1205 | #endif | |
1206 | ||
1207 | // default: | |
1208 | // non-compatible IPv6 Pure Address | |
1209 | ||
565b233e | 1210 | debugs(14,1, HERE << "IpAddress::GetInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this); |
41d93087 | 1211 | memset(&buf,0xFFFFFFFF,sizeof(struct in_addr)); |
1212 | assert(false); | |
1213 | return false; | |
1214 | } |