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