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