]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ip/Address.cc
Cleanup: un-wrap C++ header includes
[thirdparty/squid.git] / src / ip / Address.cc
1 /*
2 * DEBUG: section 14 IP Storage and Handling
3 * AUTHOR: Amos Jeffries
4 * COPYRIGHT: GPL version 2, (C)2007-2013 Treehouse Networks Ltd.
5 */
6 #include "squid.h"
7 #include "compat/getaddrinfo.h"
8 #include "compat/inet_ntop.h"
9 #include "Debug.h"
10 #include "ip/Address.h"
11 #include "ip/tools.h"
12 #include "util.h"
13
14 #include <cassert>
15 #include <cstring>
16 #if HAVE_ARPA_INET_H
17 /* for inet_ntoa() */
18 #include <arpa/inet.h>
19 #endif
20
21 /* Debugging only. Dump the address content when a fatal assert is encountered. */
22 #define IASSERT(a,b) \
23 if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \
24 printf("Ip::Address invalid? with isIPv4()=%c, isIPv6()=%c\n",(isIPv4()?'T':'F'),(isIPv6()?'T':'F')); \
25 printf("ADDRESS:"); \
26 for(unsigned int i = 0; i < sizeof(mSocketAddr_.sin6_addr); ++i) { \
27 printf(" %x", mSocketAddr_.sin6_addr.s6_addr[i]); \
28 } printf("\n"); assert(b); \
29 }
30
31 int
32 Ip::Address::cidr() const
33 {
34 uint8_t shift,byte;
35 uint8_t bit,caught;
36 int len = 0;
37 const uint8_t *ptr= mSocketAddr_.sin6_addr.s6_addr;
38
39 /* Let's scan all the bits from Most Significant to Least */
40 /* Until we find an "0" bit. Then, we return */
41 shift=0;
42
43 /* return IPv4 CIDR for any Mapped address */
44 /* Thus only check the mapped bit */
45
46 if ( !isIPv6() ) {
47 shift = 12;
48 }
49
50 for (; shift<sizeof(mSocketAddr_.sin6_addr) ; ++shift) {
51 byte= *(ptr+shift);
52
53 if (byte == 0xFF) {
54 len += 8;
55 continue ; /* A short-cut */
56 }
57
58 for (caught = 0 , bit= 7 ; !caught && (bit <= 7); --bit) {
59 caught = ((byte & 0x80) == 0x00); /* Found a '0' at 'bit' ? */
60
61 if (!caught)
62 ++len;
63
64 byte <<= 1;
65 }
66
67 if (caught)
68 break; /* We have found the most significant "0" bit. */
69 }
70
71 return len;
72 }
73
74 int
75 Ip::Address::applyMask(Ip::Address const &mask_addr)
76 {
77 uint32_t *p1 = (uint32_t*)(&mSocketAddr_.sin6_addr);
78 uint32_t const *p2 = (uint32_t const *)(&mask_addr.mSocketAddr_.sin6_addr);
79 unsigned int blen = sizeof(mSocketAddr_.sin6_addr)/sizeof(uint32_t);
80 unsigned int changes = 0;
81
82 for (unsigned int i = 0; i < blen; ++i) {
83 if ((p1[i] & p2[i]) != p1[i])
84 ++changes;
85
86 p1[i] &= p2[i];
87 }
88
89 return changes;
90 }
91
92 bool
93 Ip::Address::applyMask(const unsigned int cidrMask, int mtype)
94 {
95 uint8_t clearbits = 0;
96 uint8_t* p = NULL;
97
98 // validation and short-cuts.
99 if (cidrMask > 128)
100 return false;
101
102 if (cidrMask > 32 && mtype == AF_INET)
103 return false;
104
105 if (cidrMask == 0) {
106 /* CIDR /0 is NoAddr regardless of the IPv4/IPv6 protocol */
107 setNoAddr();
108 return true;
109 }
110
111 clearbits = (uint8_t)( (mtype==AF_INET6?128:32) - cidrMask);
112
113 // short-cut
114 if (clearbits == 0)
115 return true;
116
117 p = (uint8_t*)(&mSocketAddr_.sin6_addr) + 15;
118
119 for (; clearbits>0 && p >= (uint8_t*)&mSocketAddr_.sin6_addr ; --p ) {
120 if (clearbits < 8) {
121 *p &= ((0xFF << clearbits) & 0xFF);
122 clearbits = 0;
123 } else {
124 *p &= 0x00;
125 clearbits -= 8;
126 }
127 }
128
129 return true;
130 }
131
132 bool
133 Ip::Address::isSockAddr() const
134 {
135 return (mSocketAddr_.sin6_port != 0);
136 }
137
138 bool
139 Ip::Address::isIPv4() const
140 {
141 return IN6_IS_ADDR_V4MAPPED( &mSocketAddr_.sin6_addr );
142 }
143
144 bool
145 Ip::Address::isIPv6() const
146 {
147 return !isIPv4();
148 }
149
150 bool
151 Ip::Address::isAnyAddr() const
152 {
153 return IN6_IS_ADDR_UNSPECIFIED(&mSocketAddr_.sin6_addr) || IN6_ARE_ADDR_EQUAL(&mSocketAddr_.sin6_addr, &v4_anyaddr);
154 }
155
156 /// NOTE: Does NOT clear the Port stored. Ony the Address and Type.
157 void
158 Ip::Address::setAnyAddr()
159 {
160 memset(&mSocketAddr_.sin6_addr, 0, sizeof(struct in6_addr) );
161 }
162
163 /// NOTE: completely empties the Ip::Address structure. Address, Port, Type, everything.
164 void
165 Ip::Address::setEmpty()
166 {
167 memset(&mSocketAddr_, 0, sizeof(mSocketAddr_) );
168 }
169
170 #if _SQUID_AIX_
171 // Bug 2885 comment 78 explains.
172 // In short AIX has a different netinet/in.h union definition
173 const struct in6_addr Ip::Address::v4_localhost = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x7f000001 }}};
174 const struct in6_addr Ip::Address::v4_anyaddr = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x00000000 }}};
175 const struct in6_addr Ip::Address::v4_noaddr = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0xffffffff }}};
176 const struct in6_addr Ip::Address::v6_noaddr = {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}};
177 #else
178 const struct in6_addr Ip::Address::v4_localhost = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 }}
180 };
181 const struct in6_addr Ip::Address::v4_anyaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
182 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }}
183 };
184 const struct in6_addr Ip::Address::v4_noaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
185 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
186 };
187 const struct in6_addr Ip::Address::v6_noaddr = {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
188 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
189 };
190 #endif
191
192 bool
193 Ip::Address::setIPv4()
194 {
195 if ( isLocalhost() ) {
196 mSocketAddr_.sin6_addr = v4_localhost;
197 return true;
198 }
199
200 if ( isAnyAddr() ) {
201 mSocketAddr_.sin6_addr = v4_anyaddr;
202 return true;
203 }
204
205 if ( isNoAddr() ) {
206 mSocketAddr_.sin6_addr = v4_noaddr;
207 return true;
208 }
209
210 if ( isIPv4())
211 return true;
212
213 // anything non-IPv4 and non-convertable is BAD.
214 return false;
215 }
216
217 bool
218 Ip::Address::isLocalhost() const
219 {
220 return IN6_IS_ADDR_LOOPBACK( &mSocketAddr_.sin6_addr ) || IN6_ARE_ADDR_EQUAL( &mSocketAddr_.sin6_addr, &v4_localhost );
221 }
222
223 void
224 Ip::Address::setLocalhost()
225 {
226 if (Ip::EnableIpv6) {
227 mSocketAddr_.sin6_addr = in6addr_loopback;
228 mSocketAddr_.sin6_family = AF_INET6;
229 } else {
230 mSocketAddr_.sin6_addr = v4_localhost;
231 mSocketAddr_.sin6_family = AF_INET;
232 }
233 }
234
235 bool
236 Ip::Address::isSiteLocal6() const
237 {
238 // RFC 4193 the site-local allocated range is fc00::/7
239 // with fd00::/8 as the only currently allocated range (so we test it first).
240 // BUG: as of 2010-02 Linux and BSD define IN6_IS_ADDR_SITELOCAL() to check for fec::/10
241 return mSocketAddr_.sin6_addr.s6_addr[0] == static_cast<uint8_t>(0xfd) ||
242 mSocketAddr_.sin6_addr.s6_addr[0] == static_cast<uint8_t>(0xfc);
243 }
244
245 bool
246 Ip::Address::isSiteLocalAuto() const
247 {
248 return mSocketAddr_.sin6_addr.s6_addr[10] == static_cast<uint8_t>(0xff) &&
249 mSocketAddr_.sin6_addr.s6_addr[11] == static_cast<uint8_t>(0xfe);
250 }
251
252 bool
253 Ip::Address::isNoAddr() const
254 {
255 // IFF the address == 0xff..ff (all ones)
256 return IN6_ARE_ADDR_EQUAL( &mSocketAddr_.sin6_addr, &v6_noaddr )
257 || IN6_ARE_ADDR_EQUAL( &mSocketAddr_.sin6_addr, &v4_noaddr );
258 }
259
260 void
261 Ip::Address::setNoAddr()
262 {
263 memset(&mSocketAddr_.sin6_addr, 0xFF, sizeof(struct in6_addr) );
264 mSocketAddr_.sin6_family = AF_INET6;
265 }
266
267 bool
268 Ip::Address::getReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const
269 {
270 char *p = buf;
271 unsigned char const *r = dat.s6_addr;
272
273 /* RFC1886 says: */
274 /* 4321:0:1:2:3:4:567:89ab */
275 /* must be sent */
276 /* 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. */
277
278 /* Work from the binary field. Anything else may have representation changes. */
279 /* The sin6_port and sin6_addr members shall be in network byte order. */
280
281 /* Compile Err: 'Too many arguments for format. */
282
283 for (int i = 15; i >= 0; --i, p+=4) {
284 snprintf(p, 5, "%x.%x.", ((r[i])&0xf), (((r[i])>>4)&0xf) );
285 }
286
287 /* RFC3152 says: */
288 /* ip6.int is now deprecated TLD, use ip6.arpa instead. */
289 snprintf(p,10,"ip6.arpa.");
290
291 return true;
292 }
293
294 bool
295 Ip::Address::getReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const
296 {
297 unsigned int i = (unsigned int) ntohl(dat.s_addr);
298 snprintf(buf, 32, "%u.%u.%u.%u.in-addr.arpa.",
299 i & 255,
300 (i >> 8) & 255,
301 (i >> 16) & 255,
302 (i >> 24) & 255);
303 return true;
304 }
305
306 bool
307 Ip::Address::getReverseString(char buf[MAX_IPSTRLEN], int show_type) const
308 {
309
310 if (show_type == AF_UNSPEC) {
311 show_type = isIPv6() ? AF_INET6 : AF_INET ;
312 }
313
314 if (show_type == AF_INET && isIPv4()) {
315 struct in_addr* tmp = (struct in_addr*)&mSocketAddr_.sin6_addr.s6_addr[12];
316 return getReverseString4(buf, *tmp);
317 } else if ( show_type == AF_INET6 && isIPv6() ) {
318 return getReverseString6(buf, mSocketAddr_.sin6_addr);
319 }
320
321 debugs(14, DBG_CRITICAL, "Unable to convert '" << toStr(buf,MAX_IPSTRLEN) << "' to the rDNS type requested.");
322
323 buf[0] = '\0';
324
325 return false;
326 }
327
328 Ip::Address&
329 Ip::Address::operator =(const Ip::Address &s)
330 {
331 memcpy(this, &s, sizeof(Ip::Address));
332 return *this;
333 };
334
335 Ip::Address::Address(const char*s)
336 {
337 setEmpty();
338 lookupHostIP(s, true);
339 }
340
341 bool
342 Ip::Address::operator =(const char* s)
343 {
344 return lookupHostIP(s, true);
345 }
346
347 bool
348 Ip::Address::GetHostByName(const char* s)
349 {
350 return lookupHostIP(s, false);
351 }
352
353 bool
354 Ip::Address::lookupHostIP(const char *s, bool nodns)
355 {
356 struct addrinfo want;
357 memset(&want, 0, sizeof(struct addrinfo));
358 if (nodns) {
359 want.ai_flags = AI_NUMERICHOST; // prevent actual DNS lookups!
360 }
361
362 int err = 0;
363 struct addrinfo *res = NULL;
364 if ( (err = getaddrinfo(s, NULL, &want, &res)) != 0) {
365 debugs(14,3, HERE << "Given Non-IP '" << s << "': " << gai_strerror(err) );
366 /* free the memory getaddrinfo() dynamically allocated. */
367 if (res)
368 freeaddrinfo(res);
369 return false;
370 }
371
372 /*
373 * NP: =(sockaddr_*) may alter the port. we don't want that.
374 * all we have been given as input was an IPA.
375 */
376 short portSaved = port();
377 operator=(*res);
378 port(portSaved);
379
380 /* free the memory getaddrinfo() dynamically allocated. */
381 freeaddrinfo(res);
382 return true;
383 }
384
385 Ip::Address::Address(struct sockaddr_in const &s)
386 {
387 setEmpty();
388 operator=(s);
389 };
390
391 Ip::Address &
392 Ip::Address::operator =(struct sockaddr_in const &s)
393 {
394 map4to6((const in_addr)s.sin_addr, mSocketAddr_.sin6_addr);
395 mSocketAddr_.sin6_port = s.sin_port;
396 mSocketAddr_.sin6_family = AF_INET6;
397 return *this;
398 };
399
400 Ip::Address &
401 Ip::Address::operator =(const struct sockaddr_storage &s)
402 {
403 /* some AF_* magic to tell socket types apart and what we need to do */
404 if (s.ss_family == AF_INET6) {
405 memcpy(&mSocketAddr_, &s, sizeof(struct sockaddr_in6));
406 } else { // convert it to our storage mapping.
407 struct sockaddr_in *sin = (struct sockaddr_in*)&s;
408 mSocketAddr_.sin6_port = sin->sin_port;
409 map4to6( sin->sin_addr, mSocketAddr_.sin6_addr);
410 }
411 return *this;
412 };
413
414 Ip::Address::Address(struct sockaddr_in6 const &s)
415 {
416 setEmpty();
417 operator=(s);
418 };
419
420 Ip::Address &
421 Ip::Address::operator =(struct sockaddr_in6 const &s)
422 {
423 memcpy(&mSocketAddr_, &s, sizeof(struct sockaddr_in6));
424
425 return *this;
426 };
427
428 Ip::Address::Address(struct in_addr const &s)
429 {
430 setEmpty();
431 operator=(s);
432 };
433
434 Ip::Address &
435 Ip::Address::operator =(struct in_addr const &s)
436 {
437 map4to6((const in_addr)s, mSocketAddr_.sin6_addr);
438 mSocketAddr_.sin6_family = AF_INET6;
439 return *this;
440 };
441
442 Ip::Address::Address(struct in6_addr const &s)
443 {
444 setEmpty();
445 operator=(s);
446 };
447
448 Ip::Address &
449 Ip::Address::operator =(struct in6_addr const &s)
450 {
451
452 memcpy(&mSocketAddr_.sin6_addr, &s, sizeof(struct in6_addr));
453 mSocketAddr_.sin6_family = AF_INET6;
454
455 return *this;
456 };
457
458 Ip::Address::Address(const Ip::Address &s)
459 {
460 setEmpty();
461 operator=(s);
462 }
463
464 Ip::Address::Address(const struct hostent &s)
465 {
466 setEmpty();
467 operator=(s);
468 }
469
470 bool
471 Ip::Address::operator =(const struct hostent &s)
472 {
473
474 struct in_addr* ipv4 = NULL;
475
476 struct in6_addr* ipv6 = NULL;
477
478 //struct hostent {
479 // char *h_name; /* official name of host */
480 // char **h_aliases; /* alias list */
481 // int h_addrtype; /* host address type */
482 // int h_length; /* length of address */
483 // char **h_addr_list; /* list of addresses */
484 //}
485
486 switch (s.h_addrtype) {
487
488 case AF_INET:
489 ipv4 = (in_addr*)(s.h_addr_list[0]);
490 /* this */
491 operator=(*ipv4);
492 break;
493
494 case AF_INET6:
495 ipv6 = (in6_addr*)(s.h_addr_list[0]);
496 /* this */
497 operator=(*ipv6);
498 break;
499
500 default:
501 IASSERT("false",false);
502 return false;
503 }
504
505 return true;
506 }
507
508 Ip::Address::Address(const struct addrinfo &s)
509 {
510 setEmpty();
511 operator=(s);
512 }
513
514 bool
515 Ip::Address::operator =(const struct addrinfo &s)
516 {
517
518 struct sockaddr_in* ipv4 = NULL;
519
520 struct sockaddr_in6* ipv6 = NULL;
521
522 //struct addrinfo {
523 // int ai_flags; /* input flags */
524 // int ai_family; /* protocol family for socket */
525 // int ai_socktype; /* socket type */
526 // int ai_protocol; /* protocol for socket */
527 // socklen_t ai_addrlen; /* length of socket-address */
528 // struct sockaddr *ai_addr; /* socket-address for socket */
529 // char *ai_canonname; /* canonical name for service location */
530 // struct addrinfo *ai_next; /* pointer to next in list */
531 //}
532
533 switch (s.ai_family) {
534
535 case AF_INET:
536 ipv4 = (sockaddr_in*)(s.ai_addr);
537 /* this */
538 assert(ipv4);
539 operator=(*ipv4);
540 break;
541
542 case AF_INET6:
543 ipv6 = (sockaddr_in6*)(s.ai_addr);
544 /* this */
545 assert(ipv6);
546 operator=(*ipv6);
547 break;
548
549 case AF_UNSPEC:
550 default:
551 // attempt to handle partially initialised addrinfo.
552 // such as those where data only comes from getsockopt()
553 if (s.ai_addr != NULL) {
554 if (s.ai_addrlen == sizeof(struct sockaddr_in6)) {
555 operator=(*((struct sockaddr_in6*)s.ai_addr));
556 return true;
557 } else if (s.ai_addrlen == sizeof(struct sockaddr_in)) {
558 operator=(*((struct sockaddr_in*)s.ai_addr));
559 return true;
560 }
561 }
562 return false;
563 }
564
565 return true;
566 }
567
568 void
569 Ip::Address::getAddrInfo(struct addrinfo *&dst, int force) const
570 {
571 if (dst == NULL) {
572 dst = new addrinfo;
573 }
574
575 memset(dst, 0, sizeof(struct addrinfo));
576
577 // set defaults
578 // Mac OS X does not emit a flag indicating the output is numeric (IP address)
579 #if _SQUID_APPLE_
580 dst->ai_flags = 0;
581 #else
582 dst->ai_flags = AI_NUMERICHOST;
583 #endif
584
585 if (dst->ai_socktype == 0)
586 dst->ai_socktype = SOCK_STREAM;
587
588 if (dst->ai_socktype == SOCK_STREAM // implies TCP
589 && dst->ai_protocol == 0)
590 dst->ai_protocol = IPPROTO_TCP;
591
592 if (dst->ai_socktype == SOCK_DGRAM // implies UDP
593 && dst->ai_protocol == 0)
594 dst->ai_protocol = IPPROTO_UDP;
595
596 if (force == AF_INET6 || (force == AF_UNSPEC && Ip::EnableIpv6 && isIPv6()) ) {
597 dst->ai_addr = (struct sockaddr*)new sockaddr_in6;
598
599 memset(dst->ai_addr,0,sizeof(struct sockaddr_in6));
600
601 getSockAddr(*((struct sockaddr_in6*)dst->ai_addr));
602
603 dst->ai_addrlen = sizeof(struct sockaddr_in6);
604
605 dst->ai_family = ((struct sockaddr_in6*)dst->ai_addr)->sin6_family;
606
607 #if 0
608 /**
609 * Enable only if you must and please report to squid-dev if you find a need for this.
610 *
611 * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY).
612 * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx
613 * Linux appears to only do some things when its present.
614 * (93) Bad Protocol
615 * FreeBSD dies horribly when using dual-stack with it set.
616 * (43) Protocol not supported
617 */
618 dst->ai_protocol = IPPROTO_IPV6;
619 #endif
620
621 } else if ( force == AF_INET || (force == AF_UNSPEC && isIPv4()) ) {
622
623 dst->ai_addr = (struct sockaddr*)new sockaddr_in;
624
625 memset(dst->ai_addr,0,sizeof(struct sockaddr_in));
626
627 getSockAddr(*((struct sockaddr_in*)dst->ai_addr));
628
629 dst->ai_addrlen = sizeof(struct sockaddr_in);
630
631 dst->ai_family = ((struct sockaddr_in*)dst->ai_addr)->sin_family;
632 } else {
633 IASSERT("false",false);
634 }
635 }
636
637 void
638 Ip::Address::InitAddrInfo(struct addrinfo *&ai)
639 {
640 if (ai == NULL) {
641 ai = new addrinfo;
642 memset(ai,0,sizeof(struct addrinfo));
643 }
644
645 // remove any existing data.
646 if (ai->ai_addr) delete ai->ai_addr;
647
648 ai->ai_addr = (struct sockaddr*)new sockaddr_in6;
649 memset(ai->ai_addr, 0, sizeof(struct sockaddr_in6));
650
651 ai->ai_addrlen = sizeof(struct sockaddr_in6);
652
653 }
654
655 void
656 Ip::Address::FreeAddrInfo(struct addrinfo *&ai)
657 {
658 if (ai == NULL) return;
659
660 if (ai->ai_addr) delete ai->ai_addr;
661
662 ai->ai_addr = NULL;
663
664 ai->ai_addrlen = 0;
665
666 // NP: name fields are NOT allocated at present.
667 delete ai;
668
669 ai = NULL;
670 }
671
672 int
673 Ip::Address::matchIPAddr(const Ip::Address &rhs) const
674 {
675 uint8_t *l = (uint8_t*)mSocketAddr_.sin6_addr.s6_addr;
676 uint8_t *r = (uint8_t*)rhs.mSocketAddr_.sin6_addr.s6_addr;
677
678 // loop a byte-wise compare
679 // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR
680 // expected difference on CIDR is gt/eq or lt/eq ONLY.
681 for (unsigned int i = 0 ; i < sizeof(mSocketAddr_.sin6_addr) ; ++i) {
682
683 if (l[i] < r[i])
684 return -1;
685
686 if (l[i] > r[i])
687 return 1;
688 }
689
690 return 0;
691 }
692
693 int
694 Ip::Address::compareWhole(const Ip::Address &rhs) const
695 {
696 return memcmp(this, &rhs, sizeof(*this));
697 }
698
699 bool
700 Ip::Address::operator ==(const Ip::Address &s) const
701 {
702 return (0 == matchIPAddr(s));
703 }
704
705 bool
706 Ip::Address::operator !=(const Ip::Address &s) const
707 {
708 return ! ( operator==(s) );
709 }
710
711 bool
712 Ip::Address::operator <=(const Ip::Address &rhs) const
713 {
714 if (isAnyAddr() && !rhs.isAnyAddr())
715 return true;
716
717 return (matchIPAddr(rhs) <= 0);
718 }
719
720 bool
721 Ip::Address::operator >=(const Ip::Address &rhs) const
722 {
723 if (isNoAddr() && !rhs.isNoAddr())
724 return true;
725
726 return ( matchIPAddr(rhs) >= 0);
727 }
728
729 bool
730 Ip::Address::operator >(const Ip::Address &rhs) const
731 {
732 if (isNoAddr() && !rhs.isNoAddr())
733 return true;
734
735 return ( matchIPAddr(rhs) > 0);
736 }
737
738 bool
739 Ip::Address::operator <(const Ip::Address &rhs) const
740 {
741 if (isAnyAddr() && !rhs.isAnyAddr())
742 return true;
743
744 return ( matchIPAddr(rhs) < 0);
745 }
746
747 unsigned short
748 Ip::Address::port() const
749 {
750 return ntohs( mSocketAddr_.sin6_port );
751 }
752
753 unsigned short
754 Ip::Address::port(unsigned short prt)
755 {
756 mSocketAddr_.sin6_port = htons(prt);
757
758 return prt;
759 }
760
761 /**
762 * toStr Given a buffer writes a readable ascii version of the IPA and/or port stored
763 *
764 * Buffer must be of a size large enough to hold the converted address.
765 * This size is provided in the form of a global defined variable MAX_IPSTRLEN
766 * Should a buffer shorter be provided the string result will be truncated
767 * at the length of the available buffer.
768 *
769 * A copy of the buffer is also returned for simple immediate display.
770 */
771 char *
772 Ip::Address::toStr(char* buf, const unsigned int blen, int force) const
773 {
774 // Ensure we have a buffer.
775 if (buf == NULL) {
776 return NULL;
777 }
778
779 /* some external code may have blindly memset a parent. */
780 /* thats okay, our default is known */
781 if ( isAnyAddr() ) {
782 if (isIPv6())
783 memcpy(buf,"::\0", min(static_cast<unsigned int>(3),blen));
784 else if (isIPv4())
785 memcpy(buf,"0.0.0.0\0", min(static_cast<unsigned int>(8),blen));
786 return buf;
787 }
788
789 memset(buf,0,blen); // clear buffer before write
790
791 /* Pure-IPv6 CANNOT be displayed in IPv4 format. */
792 /* However IPv4 CAN. */
793 if ( force == AF_INET && !isIPv4() ) {
794 if ( isIPv6() ) {
795 memcpy(buf, "{!IPv4}\0", min(static_cast<unsigned int>(8),blen));
796 }
797 return buf;
798 }
799
800 if ( force == AF_INET6 || (force == AF_UNSPEC && isIPv6()) ) {
801
802 inet_ntop(AF_INET6, &mSocketAddr_.sin6_addr, buf, blen);
803
804 } else if ( force == AF_INET || (force == AF_UNSPEC && isIPv4()) ) {
805
806 struct in_addr tmp;
807 getInAddr(tmp);
808 inet_ntop(AF_INET, &tmp, buf, blen);
809 } else {
810 debugs(14, DBG_CRITICAL, "WARNING: Corrupt IP Address details OR required to display in unknown format (" <<
811 force << "). accepted={" << AF_UNSPEC << "," << AF_INET << "," << AF_INET6 << "}");
812 fprintf(stderr,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ",
813 force, AF_UNSPEC, AF_INET, AF_INET6);
814 memcpy(buf,"dead:beef::\0", min(static_cast<unsigned int>(13),blen));
815 assert(false);
816 }
817
818 return buf;
819 }
820
821 unsigned int
822 Ip::Address::toHostStr(char *buf, const unsigned int blen) const
823 {
824 char *p = buf;
825
826 if (isIPv6() && blen > 0) {
827 *p = '[';
828 ++p;
829 }
830
831 /* 8 being space for [ ] : and port digits */
832 if ( isIPv6() )
833 toStr(p, blen-8, AF_INET6);
834 else
835 toStr(p, blen-8, AF_INET);
836
837 // find the end of the new string
838 while (*p != '\0' && p < buf+blen)
839 ++p;
840
841 if (isIPv6() && p < (buf+blen-1) ) {
842 *p = ']';
843 ++p;
844 }
845
846 /* terminate just in case. */
847 *p = '\0';
848
849 /* return size of buffer now used */
850 return (p - buf);
851 }
852
853 char *
854 Ip::Address::toUrl(char* buf, unsigned int blen) const
855 {
856 char *p = buf;
857
858 // Ensure we have a buffer.
859
860 if (buf == NULL) {
861 return NULL;
862 }
863
864 p += toHostStr(p, blen);
865
866 if (mSocketAddr_.sin6_port > 0 && p <= (buf+blen-7) ) {
867 // ':port' (short int) needs at most 6 bytes plus 1 for 0-terminator
868 snprintf(p, 7, ":%d", port() );
869 }
870
871 // force a null-terminated string
872 buf[blen-1] = '\0';
873
874 return buf;
875 }
876
877 void
878 Ip::Address::getSockAddr(struct sockaddr_storage &addr, const int family) const
879 {
880 struct sockaddr_in *sin = NULL;
881
882 if ( family == AF_INET && !isIPv4()) {
883 // FIXME INET6: caller using the wrong socket type!
884 debugs(14, DBG_CRITICAL, HERE << "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this);
885 assert(false);
886 }
887
888 if ( family == AF_INET6 || (family == AF_UNSPEC && isIPv6()) ) {
889 struct sockaddr_in6 *ss6 = (struct sockaddr_in6*)&addr;
890 getSockAddr(*ss6);
891 } else if ( family == AF_INET || (family == AF_UNSPEC && isIPv4()) ) {
892 sin = (struct sockaddr_in*)&addr;
893 getSockAddr(*sin);
894 } else {
895 IASSERT("false",false);
896 }
897 }
898
899 void
900 Ip::Address::getSockAddr(struct sockaddr_in &buf) const
901 {
902 if ( isIPv4() ) {
903 buf.sin_family = AF_INET;
904 buf.sin_port = mSocketAddr_.sin6_port;
905 map6to4( mSocketAddr_.sin6_addr, buf.sin_addr);
906 } else {
907 debugs(14, DBG_CRITICAL, HERE << "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this );
908
909 memset(&buf,0xFFFFFFFF,sizeof(struct sockaddr_in));
910 assert(false);
911 }
912
913 #if HAVE_SIN_LEN_IN_SAI
914 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
915 buf.sin_len = sizeof(struct sockaddr_in);
916 #endif
917 }
918
919 void
920 Ip::Address::getSockAddr(struct sockaddr_in6 &buf) const
921 {
922 memcpy(&buf, &mSocketAddr_, sizeof(struct sockaddr_in6));
923 /* maintain address family. It may have changed inside us. */
924 buf.sin6_family = AF_INET6;
925
926 #if HAVE_SIN6_LEN_IN_SAI
927 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
928 buf.sin6_len = sizeof(struct sockaddr_in6);
929 #endif
930 }
931
932 void
933 Ip::Address::map4to6(const struct in_addr &in, struct in6_addr &out) const
934 {
935 /* check for special cases */
936
937 if ( in.s_addr == 0x00000000) {
938 /* ANYADDR */
939 out = v4_anyaddr;
940 } else if ( in.s_addr == 0xFFFFFFFF) {
941 /* NOADDR */
942 out = v4_noaddr;
943 } else {
944 /* general */
945 out = v4_anyaddr;
946 out.s6_addr[12] = ((uint8_t *)&in.s_addr)[0];
947 out.s6_addr[13] = ((uint8_t *)&in.s_addr)[1];
948 out.s6_addr[14] = ((uint8_t *)&in.s_addr)[2];
949 out.s6_addr[15] = ((uint8_t *)&in.s_addr)[3];
950 }
951 }
952
953 void
954 Ip::Address::map6to4(const struct in6_addr &in, struct in_addr &out) const
955 {
956 /* ANYADDR */
957 /* NOADDR */
958 /* general */
959
960 memset(&out, 0, sizeof(struct in_addr));
961 ((uint8_t *)&out.s_addr)[0] = in.s6_addr[12];
962 ((uint8_t *)&out.s_addr)[1] = in.s6_addr[13];
963 ((uint8_t *)&out.s_addr)[2] = in.s6_addr[14];
964 ((uint8_t *)&out.s_addr)[3] = in.s6_addr[15];
965 }
966
967 void
968 Ip::Address::getInAddr(struct in6_addr &buf) const
969 {
970 memcpy(&buf, &mSocketAddr_.sin6_addr, sizeof(struct in6_addr));
971 }
972
973 bool
974 Ip::Address::getInAddr(struct in_addr &buf) const
975 {
976 if ( isIPv4() ) {
977 map6to4(mSocketAddr_.sin6_addr, buf);
978 return true;
979 }
980
981 // default:
982 // non-compatible IPv6 Pure Address
983
984 debugs(14, DBG_IMPORTANT, HERE << "Ip::Address::getInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this);
985 memset(&buf,0xFFFFFFFF,sizeof(struct in_addr));
986 assert(false);
987 return false;
988 }