]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ip/Address.cc
Fixed never-true tests in testRefCount
[thirdparty/squid.git] / src / ip / Address.cc
CommitLineData
41d93087 1/*
bbc27441
AJ
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
41d93087 7 */
bbc27441
AJ
8
9/* DEBUG: section 14 IP Storage and Handling */
10
f7f3304a 11#include "squid.h"
82b7abe3 12#include "Debug.h"
96d89ea0 13#include "ip/Address.h"
055421ee 14#include "ip/tools.h"
1ef0b9ce
AJ
15#include "util.h"
16
074d6a40
AJ
17#include <cassert>
18#include <cstring>
41d93087 19#if HAVE_ARPA_INET_H
489520a9
AJ
20/* for inet_ntoa() */
21#include <arpa/inet.h>
41d93087 22#endif
760da772
AJ
23#if HAVE_WS2TCPIP_H
24// Windows IPv6 definitions
25#include <ws2tcpip.h>
26#endif
27
28// some OS (ie WIndows) define IN6_ADDR_EQUAL instead
29#if !defined(IN6_ARE_ADDR_EQUAL) && _SQUID_WINDOWS_
30#define IN6_ARE_ADDR_EQUAL IN6_ADDR_EQUAL
31#endif
41d93087 32
41d93087 33/* Debugging only. Dump the address content when a fatal assert is encountered. */
41d93087 34#define IASSERT(a,b) \
f53969cc
SM
35 if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \
36 printf("Ip::Address invalid? with isIPv4()=%c, isIPv6()=%c\n",(isIPv4()?'T':'F'),(isIPv6()?'T':'F')); \
37 printf("ADDRESS:"); \
38 for(unsigned int i = 0; i < sizeof(mSocketAddr_.sin6_addr); ++i) { \
39 printf(" %x", mSocketAddr_.sin6_addr.s6_addr[i]); \
40 } printf("\n"); assert(b); \
41 }
41d93087 42
41d93087 43int
4dd643d5 44Ip::Address::cidr() const
41d93087 45{
760da772 46 uint8_t shift,ipbyte;
41d93087 47 uint8_t bit,caught;
48 int len = 0;
4dd643d5 49 const uint8_t *ptr= mSocketAddr_.sin6_addr.s6_addr;
41d93087 50
51 /* Let's scan all the bits from Most Significant to Least */
52 /* Until we find an "0" bit. Then, we return */
53 shift=0;
54
41d93087 55 /* return IPv4 CIDR for any Mapped address */
56 /* Thus only check the mapped bit */
57
4dd643d5 58 if ( !isIPv6() ) {
41d93087 59 shift = 12;
60 }
61
4dd643d5 62 for (; shift<sizeof(mSocketAddr_.sin6_addr) ; ++shift) {
760da772 63 ipbyte= *(ptr+shift);
41d93087 64
760da772 65 if (ipbyte == 0xFF) {
41d93087 66 len += 8;
67 continue ; /* A short-cut */
68 }
69
5e263176 70 for (caught = 0 , bit= 7 ; !caught && (bit <= 7); --bit) {
760da772 71 caught = ((ipbyte & 0x80) == 0x00); /* Found a '0' at 'bit' ? */
41d93087 72
73 if (!caught)
d7ae3534 74 ++len;
41d93087 75
760da772 76 ipbyte <<= 1;
41d93087 77 }
78
79 if (caught)
80 break; /* We have found the most significant "0" bit. */
81 }
82
83 return len;
84}
85
881c4733 86int
4dd643d5 87Ip::Address::applyMask(Ip::Address const &mask_addr)
41d93087 88{
4dd643d5
AJ
89 uint32_t *p1 = (uint32_t*)(&mSocketAddr_.sin6_addr);
90 uint32_t const *p2 = (uint32_t const *)(&mask_addr.mSocketAddr_.sin6_addr);
91 unsigned int blen = sizeof(mSocketAddr_.sin6_addr)/sizeof(uint32_t);
41d93087 92 unsigned int changes = 0;
93
d7ae3534 94 for (unsigned int i = 0; i < blen; ++i) {
26ac0430 95 if ((p1[i] & p2[i]) != p1[i])
d7ae3534 96 ++changes;
41d93087 97
98 p1[i] &= p2[i];
99 }
100
41d93087 101 return changes;
102}
103
b7ac5457 104bool
4dd643d5 105Ip::Address::applyMask(const unsigned int cidrMask, int mtype)
41d93087 106{
107 uint8_t clearbits = 0;
108 uint8_t* p = NULL;
109
41d93087 110 // validation and short-cuts.
4dd643d5 111 if (cidrMask > 128)
41d93087 112 return false;
113
4dd643d5 114 if (cidrMask > 32 && mtype == AF_INET)
41d93087 115 return false;
116
4dd643d5 117 if (cidrMask == 0) {
2c1b9590 118 /* CIDR /0 is NoAddr regardless of the IPv4/IPv6 protocol */
4dd643d5 119 setNoAddr();
2c1b9590
AJ
120 return true;
121 }
122
4dd643d5 123 clearbits = (uint8_t)( (mtype==AF_INET6?128:32) - cidrMask);
41d93087 124
125 // short-cut
26ac0430 126 if (clearbits == 0)
41d93087 127 return true;
128
4dd643d5 129 p = (uint8_t*)(&mSocketAddr_.sin6_addr) + 15;
41d93087 130
4dd643d5 131 for (; clearbits>0 && p >= (uint8_t*)&mSocketAddr_.sin6_addr ; --p ) {
26ac0430 132 if (clearbits < 8) {
41d93087 133 *p &= ((0xFF << clearbits) & 0xFF);
134 clearbits = 0;
135 } else {
136 *p &= 0x00;
137 clearbits -= 8;
138 }
139 }
140
141 return true;
142}
143
b7ac5457 144bool
4dd643d5 145Ip::Address::isSockAddr() const
41d93087 146{
4dd643d5 147 return (mSocketAddr_.sin6_port != 0);
41d93087 148}
149
b7ac5457 150bool
4dd643d5 151Ip::Address::isIPv4() const
41d93087 152{
4dd643d5 153 return IN6_IS_ADDR_V4MAPPED( &mSocketAddr_.sin6_addr );
41d93087 154}
155
b7ac5457 156bool
4dd643d5 157Ip::Address::isIPv6() const
41d93087 158{
4dd643d5 159 return !isIPv4();
41d93087 160}
161
b7ac5457 162bool
4dd643d5 163Ip::Address::isAnyAddr() const
41d93087 164{
4dd643d5 165 return IN6_IS_ADDR_UNSPECIFIED(&mSocketAddr_.sin6_addr) || IN6_ARE_ADDR_EQUAL(&mSocketAddr_.sin6_addr, &v4_anyaddr);
41d93087 166}
167
168/// NOTE: Does NOT clear the Port stored. Ony the Address and Type.
b7ac5457 169void
4dd643d5 170Ip::Address::setAnyAddr()
41d93087 171{
4dd643d5 172 memset(&mSocketAddr_.sin6_addr, 0, sizeof(struct in6_addr) );
41d93087 173}
174
b7ac5457
AJ
175/// NOTE: completely empties the Ip::Address structure. Address, Port, Type, everything.
176void
4dd643d5 177Ip::Address::setEmpty()
41d93087 178{
4dd643d5 179 memset(&mSocketAddr_, 0, sizeof(mSocketAddr_) );
41d93087 180}
181
349a322f 182#if _SQUID_AIX_
d48a52bf
A
183// Bug 2885 comment 78 explains.
184// In short AIX has a different netinet/in.h union definition
349a322f
AJ
185const struct in6_addr Ip::Address::v4_localhost = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x7f000001 }}};
186const struct in6_addr Ip::Address::v4_anyaddr = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x00000000 }}};
187const struct in6_addr Ip::Address::v4_noaddr = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0xffffffff }}};
188const struct in6_addr Ip::Address::v6_noaddr = {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}};
189#else
f53969cc
SM
190const struct in6_addr Ip::Address::v4_localhost = {{{
191 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01
193 }
194 }
c0248e14 195};
f53969cc
SM
196const struct in6_addr Ip::Address::v4_anyaddr = {{{
197 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00
199 }
200 }
c0248e14 201};
f53969cc
SM
202const struct in6_addr Ip::Address::v4_noaddr = {{{
203 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
204 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
205 }
206 }
0eb08770 207};
f53969cc
SM
208const struct in6_addr Ip::Address::v6_noaddr = {{{
209 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
210 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
211 }
212 }
c0248e14 213};
349a322f 214#endif
41d93087 215
b7ac5457 216bool
4dd643d5 217Ip::Address::setIPv4()
c0248e14 218{
4dd643d5
AJ
219 if ( isLocalhost() ) {
220 mSocketAddr_.sin6_addr = v4_localhost;
41d93087 221 return true;
222 }
41d93087 223
4dd643d5
AJ
224 if ( isAnyAddr() ) {
225 mSocketAddr_.sin6_addr = v4_anyaddr;
41d93087 226 return true;
227 }
228
4dd643d5
AJ
229 if ( isNoAddr() ) {
230 mSocketAddr_.sin6_addr = v4_noaddr;
0906f01d
CT
231 return true;
232 }
233
4dd643d5 234 if ( isIPv4())
41d93087 235 return true;
236
237 // anything non-IPv4 and non-convertable is BAD.
238 return false;
41d93087 239}
240
b7ac5457 241bool
4dd643d5 242Ip::Address::isLocalhost() const
41d93087 243{
4dd643d5 244 return IN6_IS_ADDR_LOOPBACK( &mSocketAddr_.sin6_addr ) || IN6_ARE_ADDR_EQUAL( &mSocketAddr_.sin6_addr, &v4_localhost );
41d93087 245}
246
b7ac5457 247void
4dd643d5 248Ip::Address::setLocalhost()
41d93087 249{
055421ee 250 if (Ip::EnableIpv6) {
4dd643d5
AJ
251 mSocketAddr_.sin6_addr = in6addr_loopback;
252 mSocketAddr_.sin6_family = AF_INET6;
055421ee 253 } else {
4dd643d5
AJ
254 mSocketAddr_.sin6_addr = v4_localhost;
255 mSocketAddr_.sin6_family = AF_INET;
055421ee 256 }
41d93087 257}
258
b7ac5457 259bool
4dd643d5 260Ip::Address::isSiteLocal6() const
a98c2da5 261{
dd19b634
AJ
262 // RFC 4193 the site-local allocated range is fc00::/7
263 // with fd00::/8 as the only currently allocated range (so we test it first).
264 // BUG: as of 2010-02 Linux and BSD define IN6_IS_ADDR_SITELOCAL() to check for fec::/10
4dd643d5
AJ
265 return mSocketAddr_.sin6_addr.s6_addr[0] == static_cast<uint8_t>(0xfd) ||
266 mSocketAddr_.sin6_addr.s6_addr[0] == static_cast<uint8_t>(0xfc);
a98c2da5
AJ
267}
268
b7ac5457 269bool
4dd643d5 270Ip::Address::isSiteLocalAuto() const
a98c2da5 271{
b6a91265
AJ
272 return mSocketAddr_.sin6_addr.s6_addr[11] == static_cast<uint8_t>(0xff) &&
273 mSocketAddr_.sin6_addr.s6_addr[12] == static_cast<uint8_t>(0xfe);
a98c2da5
AJ
274}
275
b7ac5457 276bool
4dd643d5 277Ip::Address::isNoAddr() const
41d93087 278{
279 // IFF the address == 0xff..ff (all ones)
4dd643d5
AJ
280 return IN6_ARE_ADDR_EQUAL( &mSocketAddr_.sin6_addr, &v6_noaddr )
281 || IN6_ARE_ADDR_EQUAL( &mSocketAddr_.sin6_addr, &v4_noaddr );
41d93087 282}
283
b7ac5457 284void
4dd643d5 285Ip::Address::setNoAddr()
41d93087 286{
4dd643d5
AJ
287 memset(&mSocketAddr_.sin6_addr, 0xFF, sizeof(struct in6_addr) );
288 mSocketAddr_.sin6_family = AF_INET6;
41d93087 289}
290
b7ac5457 291bool
4dd643d5 292Ip::Address::getReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const
41d93087 293{
294 char *p = buf;
295 unsigned char const *r = dat.s6_addr;
296
297 /* RFC1886 says: */
298 /* 4321:0:1:2:3:4:567:89ab */
299 /* must be sent */
300 /* 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. */
301
302 /* Work from the binary field. Anything else may have representation changes. */
303 /* The sin6_port and sin6_addr members shall be in network byte order. */
304
305 /* Compile Err: 'Too many arguments for format. */
306
5e263176 307 for (int i = 15; i >= 0; --i, p+=4) {
41d93087 308 snprintf(p, 5, "%x.%x.", ((r[i])&0xf), (((r[i])>>4)&0xf) );
309 }
310
311 /* RFC3152 says: */
312 /* ip6.int is now deprecated TLD, use ip6.arpa instead. */
313 snprintf(p,10,"ip6.arpa.");
314
315 return true;
316}
41d93087 317
b7ac5457 318bool
4dd643d5 319Ip::Address::getReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const
41d93087 320{
321 unsigned int i = (unsigned int) ntohl(dat.s_addr);
322 snprintf(buf, 32, "%u.%u.%u.%u.in-addr.arpa.",
323 i & 255,
324 (i >> 8) & 255,
325 (i >> 16) & 255,
326 (i >> 24) & 255);
327 return true;
328}
329
b7ac5457 330bool
4dd643d5 331Ip::Address::getReverseString(char buf[MAX_IPSTRLEN], int show_type) const
41d93087 332{
333
26ac0430 334 if (show_type == AF_UNSPEC) {
4dd643d5 335 show_type = isIPv6() ? AF_INET6 : AF_INET ;
41d93087 336 }
337
4dd643d5
AJ
338 if (show_type == AF_INET && isIPv4()) {
339 struct in_addr* tmp = (struct in_addr*)&mSocketAddr_.sin6_addr.s6_addr[12];
340 return getReverseString4(buf, *tmp);
341 } else if ( show_type == AF_INET6 && isIPv6() ) {
342 return getReverseString6(buf, mSocketAddr_.sin6_addr);
41d93087 343 }
344
4dd643d5 345 debugs(14, DBG_CRITICAL, "Unable to convert '" << toStr(buf,MAX_IPSTRLEN) << "' to the rDNS type requested.");
41d93087 346
347 buf[0] = '\0';
348
349 return false;
350}
351
b7ac5457
AJ
352Ip::Address&
353Ip::Address::operator =(const Ip::Address &s)
41d93087 354{
b7ac5457 355 memcpy(this, &s, sizeof(Ip::Address));
41d93087 356 return *this;
357};
358
b7ac5457 359Ip::Address::Address(const char*s)
41d93087 360{
4dd643d5
AJ
361 setEmpty();
362 lookupHostIP(s, true);
41d93087 363}
364
b7ac5457
AJ
365bool
366Ip::Address::operator =(const char* s)
41d93087 367{
4dd643d5 368 return lookupHostIP(s, true);
41d93087 369}
370
b7ac5457
AJ
371bool
372Ip::Address::GetHostByName(const char* s)
41d93087 373{
4dd643d5 374 return lookupHostIP(s, false);
41d93087 375}
376
b7ac5457 377bool
4dd643d5 378Ip::Address::lookupHostIP(const char *s, bool nodns)
41d93087 379{
41d93087 380 struct addrinfo want;
41d93087 381 memset(&want, 0, sizeof(struct addrinfo));
26ac0430 382 if (nodns) {
41d93087 383 want.ai_flags = AI_NUMERICHOST; // prevent actual DNS lookups!
384 }
41d93087 385
4dd643d5
AJ
386 int err = 0;
387 struct addrinfo *res = NULL;
27bc2077 388 if ( (err = getaddrinfo(s, NULL, &want, &res)) != 0) {
380de14e 389 debugs(14,3, HERE << "Given Non-IP '" << s << "': " << gai_strerror(err) );
27bc2077 390 /* free the memory getaddrinfo() dynamically allocated. */
4dd643d5 391 if (res)
27bc2077 392 freeaddrinfo(res);
41d93087 393 return false;
394 }
395
396 /*
397 * NP: =(sockaddr_*) may alter the port. we don't want that.
398 * all we have been given as input was an IPA.
399 */
4dd643d5 400 short portSaved = port();
41d93087 401 operator=(*res);
4dd643d5 402 port(portSaved);
41d93087 403
27bc2077
AJ
404 /* free the memory getaddrinfo() dynamically allocated. */
405 freeaddrinfo(res);
41d93087 406 return true;
407}
408
b7ac5457 409Ip::Address::Address(struct sockaddr_in const &s)
41d93087 410{
4dd643d5 411 setEmpty();
41d93087 412 operator=(s);
413};
414
b7ac5457
AJ
415Ip::Address &
416Ip::Address::operator =(struct sockaddr_in const &s)
41d93087 417{
4dd643d5
AJ
418 map4to6((const in_addr)s.sin_addr, mSocketAddr_.sin6_addr);
419 mSocketAddr_.sin6_port = s.sin_port;
420 mSocketAddr_.sin6_family = AF_INET6;
41d93087 421 return *this;
422};
423
b7ac5457
AJ
424Ip::Address &
425Ip::Address::operator =(const struct sockaddr_storage &s)
0e1a47f0 426{
0e1a47f0 427 /* some AF_* magic to tell socket types apart and what we need to do */
26ac0430 428 if (s.ss_family == AF_INET6) {
7da22087 429 memcpy(&mSocketAddr_, &s, sizeof(struct sockaddr_in6));
26ac0430 430 } else { // convert it to our storage mapping.
0e1a47f0 431 struct sockaddr_in *sin = (struct sockaddr_in*)&s;
4dd643d5
AJ
432 mSocketAddr_.sin6_port = sin->sin_port;
433 map4to6( sin->sin_addr, mSocketAddr_.sin6_addr);
0e1a47f0 434 }
0e1a47f0
AJ
435 return *this;
436};
437
b7ac5457 438Ip::Address::Address(struct sockaddr_in6 const &s)
41d93087 439{
4dd643d5 440 setEmpty();
41d93087 441 operator=(s);
442};
443
b7ac5457
AJ
444Ip::Address &
445Ip::Address::operator =(struct sockaddr_in6 const &s)
41d93087 446{
4dd643d5 447 memcpy(&mSocketAddr_, &s, sizeof(struct sockaddr_in6));
b5587d15 448
41d93087 449 return *this;
450};
41d93087 451
b7ac5457 452Ip::Address::Address(struct in_addr const &s)
41d93087 453{
4dd643d5 454 setEmpty();
41d93087 455 operator=(s);
456};
457
b7ac5457
AJ
458Ip::Address &
459Ip::Address::operator =(struct in_addr const &s)
41d93087 460{
4dd643d5
AJ
461 map4to6((const in_addr)s, mSocketAddr_.sin6_addr);
462 mSocketAddr_.sin6_family = AF_INET6;
41d93087 463 return *this;
464};
465
b7ac5457 466Ip::Address::Address(struct in6_addr const &s)
41d93087 467{
4dd643d5 468 setEmpty();
41d93087 469 operator=(s);
470};
471
b7ac5457
AJ
472Ip::Address &
473Ip::Address::operator =(struct in6_addr const &s)
41d93087 474{
b5587d15 475
4dd643d5
AJ
476 memcpy(&mSocketAddr_.sin6_addr, &s, sizeof(struct in6_addr));
477 mSocketAddr_.sin6_family = AF_INET6;
b5587d15 478
41d93087 479 return *this;
480};
41d93087 481
b7ac5457 482Ip::Address::Address(const Ip::Address &s)
41d93087 483{
4dd643d5 484 setEmpty();
41d93087 485 operator=(s);
486}
487
b7ac5457 488Ip::Address::Address(const struct hostent &s)
41d93087 489{
4dd643d5 490 setEmpty();
41d93087 491 operator=(s);
492}
493
b7ac5457
AJ
494bool
495Ip::Address::operator =(const struct hostent &s)
41d93087 496{
497
498 struct in_addr* ipv4 = NULL;
499
500 struct in6_addr* ipv6 = NULL;
501
502 //struct hostent {
503 // char *h_name; /* official name of host */
504 // char **h_aliases; /* alias list */
505 // int h_addrtype; /* host address type */
506 // int h_length; /* length of address */
507 // char **h_addr_list; /* list of addresses */
508 //}
509
26ac0430 510 switch (s.h_addrtype) {
41d93087 511
512 case AF_INET:
513 ipv4 = (in_addr*)(s.h_addr_list[0]);
514 /* this */
515 operator=(*ipv4);
516 break;
517
518 case AF_INET6:
519 ipv6 = (in6_addr*)(s.h_addr_list[0]);
41d93087 520 /* this */
521 operator=(*ipv6);
41d93087 522 break;
523
524 default:
26ac0430
AJ
525 IASSERT("false",false);
526 return false;
41d93087 527 }
528
529 return true;
530}
531
b7ac5457 532Ip::Address::Address(const struct addrinfo &s)
41d93087 533{
4dd643d5 534 setEmpty();
41d93087 535 operator=(s);
536}
537
b7ac5457
AJ
538bool
539Ip::Address::operator =(const struct addrinfo &s)
41d93087 540{
541
542 struct sockaddr_in* ipv4 = NULL;
543
544 struct sockaddr_in6* ipv6 = NULL;
545
546 //struct addrinfo {
547 // int ai_flags; /* input flags */
548 // int ai_family; /* protocol family for socket */
549 // int ai_socktype; /* socket type */
550 // int ai_protocol; /* protocol for socket */
551 // socklen_t ai_addrlen; /* length of socket-address */
552 // struct sockaddr *ai_addr; /* socket-address for socket */
553 // char *ai_canonname; /* canonical name for service location */
554 // struct addrinfo *ai_next; /* pointer to next in list */
555 //}
556
26ac0430 557 switch (s.ai_family) {
41d93087 558
559 case AF_INET:
560 ipv4 = (sockaddr_in*)(s.ai_addr);
561 /* this */
562 assert(ipv4);
563 operator=(*ipv4);
564 break;
565
566 case AF_INET6:
567 ipv6 = (sockaddr_in6*)(s.ai_addr);
41d93087 568 /* this */
569 assert(ipv6);
570 operator=(*ipv6);
41d93087 571 break;
572
573 case AF_UNSPEC:
574 default:
575 // attempt to handle partially initialised addrinfo.
576 // such as those where data only comes from getsockopt()
26ac0430 577 if (s.ai_addr != NULL) {
26ac0430 578 if (s.ai_addrlen == sizeof(struct sockaddr_in6)) {
41d93087 579 operator=(*((struct sockaddr_in6*)s.ai_addr));
580 return true;
055421ee
AJ
581 } else if (s.ai_addrlen == sizeof(struct sockaddr_in)) {
582 operator=(*((struct sockaddr_in*)s.ai_addr));
583 return true;
584 }
41d93087 585 }
586 return false;
587 }
588
589 return true;
590}
591
b7ac5457 592void
4dd643d5 593Ip::Address::getAddrInfo(struct addrinfo *&dst, int force) const
41d93087 594{
26ac0430 595 if (dst == NULL) {
41d93087 596 dst = new addrinfo;
597 }
598
599 memset(dst, 0, sizeof(struct addrinfo));
600
601 // set defaults
3db830d3
AJ
602 // Mac OS X does not emit a flag indicating the output is numeric (IP address)
603#if _SQUID_APPLE_
604 dst->ai_flags = 0;
605#else
41d93087 606 dst->ai_flags = AI_NUMERICHOST;
3db830d3 607#endif
41d93087 608
26ac0430 609 if (dst->ai_socktype == 0)
41d93087 610 dst->ai_socktype = SOCK_STREAM;
611
26ac0430 612 if (dst->ai_socktype == SOCK_STREAM // implies TCP
41d93087 613 && dst->ai_protocol == 0)
614 dst->ai_protocol = IPPROTO_TCP;
615
26ac0430 616 if (dst->ai_socktype == SOCK_DGRAM // implies UDP
41d93087 617 && dst->ai_protocol == 0)
618 dst->ai_protocol = IPPROTO_UDP;
619
4dd643d5 620 if (force == AF_INET6 || (force == AF_UNSPEC && Ip::EnableIpv6 && isIPv6()) ) {
41d93087 621 dst->ai_addr = (struct sockaddr*)new sockaddr_in6;
622
623 memset(dst->ai_addr,0,sizeof(struct sockaddr_in6));
624
4dd643d5 625 getSockAddr(*((struct sockaddr_in6*)dst->ai_addr));
41d93087 626
627 dst->ai_addrlen = sizeof(struct sockaddr_in6);
628
629 dst->ai_family = ((struct sockaddr_in6*)dst->ai_addr)->sin6_family;
9d92af86
AJ
630
631#if 0
632 /**
633 * Enable only if you must and please report to squid-dev if you find a need for this.
634 *
635 * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY).
636 * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx
637 * Linux appears to only do some things when its present.
638 * (93) Bad Protocol
639 * FreeBSD dies horribly when using dual-stack with it set.
640 * (43) Protocol not supported
641 */
41d93087 642 dst->ai_protocol = IPPROTO_IPV6;
9d92af86
AJ
643#endif
644
4dd643d5 645 } else if ( force == AF_INET || (force == AF_UNSPEC && isIPv4()) ) {
41d93087 646
055421ee 647 dst->ai_addr = (struct sockaddr*)new sockaddr_in;
41d93087 648
055421ee 649 memset(dst->ai_addr,0,sizeof(struct sockaddr_in));
41d93087 650
4dd643d5 651 getSockAddr(*((struct sockaddr_in*)dst->ai_addr));
41d93087 652
055421ee 653 dst->ai_addrlen = sizeof(struct sockaddr_in);
41d93087 654
055421ee
AJ
655 dst->ai_family = ((struct sockaddr_in*)dst->ai_addr)->sin_family;
656 } else {
657 IASSERT("false",false);
658 }
41d93087 659}
660
b7ac5457 661void
851614a8 662Ip::Address::InitAddr(struct addrinfo *&ai)
41d93087 663{
26ac0430 664 if (ai == NULL) {
41d93087 665 ai = new addrinfo;
666 memset(ai,0,sizeof(struct addrinfo));
667 }
668
669 // remove any existing data.
26ac0430 670 if (ai->ai_addr) delete ai->ai_addr;
41d93087 671
672 ai->ai_addr = (struct sockaddr*)new sockaddr_in6;
673 memset(ai->ai_addr, 0, sizeof(struct sockaddr_in6));
674
675 ai->ai_addrlen = sizeof(struct sockaddr_in6);
676
677}
678
b7ac5457 679void
851614a8 680Ip::Address::FreeAddr(struct addrinfo *&ai)
41d93087 681{
26ac0430 682 if (ai == NULL) return;
41d93087 683
26ac0430 684 if (ai->ai_addr) delete ai->ai_addr;
41d93087 685
686 ai->ai_addr = NULL;
687
688 ai->ai_addrlen = 0;
689
690 // NP: name fields are NOT allocated at present.
691 delete ai;
692
693 ai = NULL;
694}
695
b7ac5457
AJ
696int
697Ip::Address::matchIPAddr(const Ip::Address &rhs) const
41d93087 698{
4dd643d5
AJ
699 uint8_t *l = (uint8_t*)mSocketAddr_.sin6_addr.s6_addr;
700 uint8_t *r = (uint8_t*)rhs.mSocketAddr_.sin6_addr.s6_addr;
41d93087 701
702 // loop a byte-wise compare
703 // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR
704 // expected difference on CIDR is gt/eq or lt/eq ONLY.
4dd643d5 705 for (unsigned int i = 0 ; i < sizeof(mSocketAddr_.sin6_addr) ; ++i) {
41d93087 706
26ac0430 707 if (l[i] < r[i])
41d93087 708 return -1;
709
26ac0430 710 if (l[i] > r[i])
41d93087 711 return 1;
712 }
713
714 return 0;
715}
716
a67d2b2e
AR
717int
718Ip::Address::compareWhole(const Ip::Address &rhs) const
607a7bd4
AR
719{
720 return memcmp(this, &rhs, sizeof(*this));
721}
722
b7ac5457
AJ
723bool
724Ip::Address::operator ==(const Ip::Address &s) const
41d93087 725{
726 return (0 == matchIPAddr(s));
727}
728
b7ac5457
AJ
729bool
730Ip::Address::operator !=(const Ip::Address &s) const
41d93087 731{
732 return ! ( operator==(s) );
733}
734
b7ac5457
AJ
735bool
736Ip::Address::operator <=(const Ip::Address &rhs) const
41d93087 737{
4dd643d5 738 if (isAnyAddr() && !rhs.isAnyAddr())
41d93087 739 return true;
740
741 return (matchIPAddr(rhs) <= 0);
742}
743
b7ac5457
AJ
744bool
745Ip::Address::operator >=(const Ip::Address &rhs) const
41d93087 746{
4dd643d5 747 if (isNoAddr() && !rhs.isNoAddr())
41d93087 748 return true;
749
750 return ( matchIPAddr(rhs) >= 0);
751}
752
b7ac5457
AJ
753bool
754Ip::Address::operator >(const Ip::Address &rhs) const
41d93087 755{
4dd643d5 756 if (isNoAddr() && !rhs.isNoAddr())
41d93087 757 return true;
758
759 return ( matchIPAddr(rhs) > 0);
760}
761
b7ac5457
AJ
762bool
763Ip::Address::operator <(const Ip::Address &rhs) const
41d93087 764{
4dd643d5 765 if (isAnyAddr() && !rhs.isAnyAddr())
41d93087 766 return true;
767
768 return ( matchIPAddr(rhs) < 0);
769}
770
f45dd259 771unsigned short
4dd643d5 772Ip::Address::port() const
41d93087 773{
4dd643d5 774 return ntohs( mSocketAddr_.sin6_port );
41d93087 775}
776
f45dd259 777unsigned short
4dd643d5 778Ip::Address::port(unsigned short prt)
41d93087 779{
4dd643d5 780 mSocketAddr_.sin6_port = htons(prt);
41d93087 781
782 return prt;
783}
784
785/**
4dd643d5 786 * toStr Given a buffer writes a readable ascii version of the IPA and/or port stored
41d93087 787 *
788 * Buffer must be of a size large enough to hold the converted address.
789 * This size is provided in the form of a global defined variable MAX_IPSTRLEN
790 * Should a buffer shorter be provided the string result will be truncated
791 * at the length of the available buffer.
792 *
793 * A copy of the buffer is also returned for simple immediate display.
794 */
b7ac5457 795char *
4dd643d5 796Ip::Address::toStr(char* buf, const unsigned int blen, int force) const
41d93087 797{
798 // Ensure we have a buffer.
26ac0430 799 if (buf == NULL) {
41d93087 800 return NULL;
801 }
802
803 /* some external code may have blindly memset a parent. */
804 /* thats okay, our default is known */
4dd643d5
AJ
805 if ( isAnyAddr() ) {
806 if (isIPv6())
8ea75675 807 memcpy(buf,"::\0", min(static_cast<unsigned int>(3),blen));
4dd643d5 808 else if (isIPv4())
8ea75675 809 memcpy(buf,"0.0.0.0\0", min(static_cast<unsigned int>(8),blen));
41d93087 810 return buf;
811 }
812
813 memset(buf,0,blen); // clear buffer before write
814
815 /* Pure-IPv6 CANNOT be displayed in IPv4 format. */
816 /* However IPv4 CAN. */
4dd643d5
AJ
817 if ( force == AF_INET && !isIPv4() ) {
818 if ( isIPv6() ) {
1dc746da 819 memcpy(buf, "{!IPv4}\0", min(static_cast<unsigned int>(8),blen));
41d93087 820 }
821 return buf;
822 }
823
4dd643d5 824 if ( force == AF_INET6 || (force == AF_UNSPEC && isIPv6()) ) {
41d93087 825
4dd643d5 826 inet_ntop(AF_INET6, &mSocketAddr_.sin6_addr, buf, blen);
41d93087 827
4dd643d5 828 } else if ( force == AF_INET || (force == AF_UNSPEC && isIPv4()) ) {
41d93087 829
830 struct in_addr tmp;
4dd643d5 831 getInAddr(tmp);
27bc2077 832 inet_ntop(AF_INET, &tmp, buf, blen);
41d93087 833 } else {
fa84c01d 834 debugs(14, DBG_CRITICAL, "WARNING: Corrupt IP Address details OR required to display in unknown format (" <<
26ac0430 835 force << "). accepted={" << AF_UNSPEC << "," << AF_INET << "," << AF_INET6 << "}");
41d93087 836 fprintf(stderr,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ",
837 force, AF_UNSPEC, AF_INET, AF_INET6);
1dc746da 838 memcpy(buf,"dead:beef::\0", min(static_cast<unsigned int>(13),blen));
41d93087 839 assert(false);
840 }
841
842 return buf;
843}
844
b7ac5457 845unsigned int
4dd643d5 846Ip::Address::toHostStr(char *buf, const unsigned int blen) const
e1381638 847{
41d93087 848 char *p = buf;
849
4dd643d5 850 if (isIPv6() && blen > 0) {
41d93087 851 *p = '[';
d7ae3534 852 ++p;
41d93087 853 }
854
188a640f 855 /* 8 being space for [ ] : and port digits */
4dd643d5
AJ
856 if ( isIPv6() )
857 toStr(p, blen-8, AF_INET6);
41d93087 858 else
4dd643d5 859 toStr(p, blen-8, AF_INET);
41d93087 860
861 // find the end of the new string
26ac0430 862 while (*p != '\0' && p < buf+blen)
d7ae3534 863 ++p;
41d93087 864
4dd643d5 865 if (isIPv6() && p < (buf+blen-1) ) {
41d93087 866 *p = ']';
d7ae3534 867 ++p;
41d93087 868 }
869
870 /* terminate just in case. */
871 *p = '\0';
872
873 /* return size of buffer now used */
874 return (p - buf);
875}
876
b7ac5457 877char *
4dd643d5 878Ip::Address::toUrl(char* buf, unsigned int blen) const
e1381638 879{
41d93087 880 char *p = buf;
881
882 // Ensure we have a buffer.
883
26ac0430 884 if (buf == NULL) {
41d93087 885 return NULL;
886 }
887
4dd643d5 888 p += toHostStr(p, blen);
41d93087 889
4dd643d5 890 if (mSocketAddr_.sin6_port > 0 && p <= (buf+blen-7) ) {
842fe4cc 891 // ':port' (short int) needs at most 6 bytes plus 1 for 0-terminator
4dd643d5 892 snprintf(p, 7, ":%d", port() );
41d93087 893 }
894
895 // force a null-terminated string
303bfd76 896 buf[blen-1] = '\0';
41d93087 897
898 return buf;
899}
900
b7ac5457 901void
4dd643d5 902Ip::Address::getSockAddr(struct sockaddr_storage &addr, const int family) const
e1381638 903{
52b694c2
AJ
904 struct sockaddr_in *sin = NULL;
905
4dd643d5 906 if ( family == AF_INET && !isIPv4()) {
0e1a47f0 907 // FIXME INET6: caller using the wrong socket type!
4dd643d5 908 debugs(14, DBG_CRITICAL, HERE << "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this);
0e1a47f0
AJ
909 assert(false);
910 }
911
4dd643d5 912 if ( family == AF_INET6 || (family == AF_UNSPEC && isIPv6()) ) {
1ef0b9ce 913 struct sockaddr_in6 *ss6 = (struct sockaddr_in6*)&addr;
4dd643d5
AJ
914 getSockAddr(*ss6);
915 } else if ( family == AF_INET || (family == AF_UNSPEC && isIPv4()) ) {
52b694c2 916 sin = (struct sockaddr_in*)&addr;
4dd643d5 917 getSockAddr(*sin);
52b694c2
AJ
918 } else {
919 IASSERT("false",false);
0e1a47f0 920 }
0e1a47f0
AJ
921}
922
b7ac5457 923void
4dd643d5 924Ip::Address::getSockAddr(struct sockaddr_in &buf) const
e1381638 925{
4dd643d5 926 if ( isIPv4() ) {
41d93087 927 buf.sin_family = AF_INET;
4dd643d5
AJ
928 buf.sin_port = mSocketAddr_.sin6_port;
929 map6to4( mSocketAddr_.sin6_addr, buf.sin_addr);
26ac0430 930 } else {
4dd643d5 931 debugs(14, DBG_CRITICAL, HERE << "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this );
41d93087 932
933 memset(&buf,0xFFFFFFFF,sizeof(struct sockaddr_in));
934 assert(false);
935 }
936
12f45551
AJ
937#if HAVE_SIN_LEN_IN_SAI
938 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
1ef0b9ce 939 buf.sin_len = sizeof(struct sockaddr_in);
12f45551 940#endif
41d93087 941}
942
b7ac5457 943void
4dd643d5 944Ip::Address::getSockAddr(struct sockaddr_in6 &buf) const
e1381638 945{
4dd643d5 946 memcpy(&buf, &mSocketAddr_, sizeof(struct sockaddr_in6));
41d93087 947 /* maintain address family. It may have changed inside us. */
948 buf.sin6_family = AF_INET6;
12f45551
AJ
949
950#if HAVE_SIN6_LEN_IN_SAI
951 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
1ef0b9ce 952 buf.sin6_len = sizeof(struct sockaddr_in6);
12f45551 953#endif
41d93087 954}
41d93087 955
b7ac5457 956void
4dd643d5 957Ip::Address::map4to6(const struct in_addr &in, struct in6_addr &out) const
e1381638 958{
41d93087 959 /* check for special cases */
960
26ac0430 961 if ( in.s_addr == 0x00000000) {
41d93087 962 /* ANYADDR */
0eb08770 963 out = v4_anyaddr;
26ac0430 964 } else if ( in.s_addr == 0xFFFFFFFF) {
41d93087 965 /* NOADDR */
0eb08770 966 out = v4_noaddr;
26ac0430 967 } else {
41d93087 968 /* general */
0eb08770 969 out = v4_anyaddr;
2c8fff41
AJ
970 out.s6_addr[12] = ((uint8_t *)&in.s_addr)[0];
971 out.s6_addr[13] = ((uint8_t *)&in.s_addr)[1];
972 out.s6_addr[14] = ((uint8_t *)&in.s_addr)[2];
973 out.s6_addr[15] = ((uint8_t *)&in.s_addr)[3];
41d93087 974 }
975}
976
b7ac5457 977void
4dd643d5 978Ip::Address::map6to4(const struct in6_addr &in, struct in_addr &out) const
e1381638 979{
41d93087 980 /* ANYADDR */
981 /* NOADDR */
982 /* general */
983
984 memset(&out, 0, sizeof(struct in_addr));
2c8fff41
AJ
985 ((uint8_t *)&out.s_addr)[0] = in.s6_addr[12];
986 ((uint8_t *)&out.s_addr)[1] = in.s6_addr[13];
987 ((uint8_t *)&out.s_addr)[2] = in.s6_addr[14];
988 ((uint8_t *)&out.s_addr)[3] = in.s6_addr[15];
41d93087 989}
990
b7ac5457 991void
68fb74b0 992Ip::Address::getInAddr(struct in6_addr &buf) const
e1381638 993{
4dd643d5 994 memcpy(&buf, &mSocketAddr_.sin6_addr, sizeof(struct in6_addr));
41d93087 995}
41d93087 996
b7ac5457 997bool
4dd643d5 998Ip::Address::getInAddr(struct in_addr &buf) const
e1381638 999{
4dd643d5 1000 if ( isIPv4() ) {
68fb74b0 1001 map6to4(mSocketAddr_.sin6_addr, buf);
41d93087 1002 return true;
1003 }
41d93087 1004
1005 // default:
1006 // non-compatible IPv6 Pure Address
1007
4dd643d5 1008 debugs(14, DBG_IMPORTANT, HERE << "Ip::Address::getInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this);
41d93087 1009 memset(&buf,0xFFFFFFFF,sizeof(struct in_addr));
1010 assert(false);
1011 return false;
1012}
f53969cc 1013