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