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