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