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