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