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