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