]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ip/Address.cc
Sync with trunk
[thirdparty/squid.git] / src / ip / Address.cc
1 /*
2 * DEBUG: section 14 IP Storage and Handling
3 * AUTHOR: Amos Jeffries
4 *
5 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from the
9 * Internet community. Development is led by Duane Wessels of the
10 * National Laboratory for Applied Network Research and funded by the
11 * National Science Foundation. Squid is Copyrighted (C) 1998 by
12 * the Regents of the University of California. Please see the
13 * COPYRIGHT file for full details. Squid incorporates software
14 * developed and/or copyrighted by other sources. Please see the
15 * CREDITS file for full details.
16 *
17 * This Ip::Address code is copyright (C) 2007 by Treehouse Networks Ltd
18 * of New Zealand. It is published and Lisenced as an extension of
19 * squid under the same conditions as the main squid application.
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 *
35 */
36
37 #include "config.h"
38 #include "compat/inet_ntop.h"
39 #include "compat/getaddrinfo.h"
40 #include "Debug.h"
41 #include "ip/Address.h"
42 #include "ip/tools.h"
43 #include "util.h"
44
45 #if HAVE_ASSERT_H
46 #include <assert.h>
47 #endif
48 #if HAVE_STRING_H
49 #include <string.h>
50 #endif
51 #if HAVE_ARPA_INET_H
52 /* for inet_ntoa() */
53 #include <arpa/inet.h>
54 #endif
55
56 /* Debugging only. Dump the address content when a fatal assert is encountered. */
57 #define IASSERT(a,b) \
58 if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \
59 printf("Ip::Address invalid? with IsIPv4()=%c, IsIPv6()=%c\n",(IsIPv4()?'T':'F'),(IsIPv6()?'T':'F')); \
60 printf("ADDRESS:"); \
61 for(unsigned int i = 0; i < sizeof(m_SocketAddr.sin6_addr); i++) { \
62 printf(" %x", m_SocketAddr.sin6_addr.s6_addr[i]); \
63 } printf("\n"); assert(b); \
64 }
65
66 int
67 Ip::Address::GetCIDR() const
68 {
69 uint8_t shift,byte;
70 uint8_t bit,caught;
71 int len = 0;
72 const uint8_t *ptr= m_SocketAddr.sin6_addr.s6_addr;
73
74 /* Let's scan all the bits from Most Significant to Least */
75 /* Until we find an "0" bit. Then, we return */
76 shift=0;
77
78 /* return IPv4 CIDR for any Mapped address */
79 /* Thus only check the mapped bit */
80
81 if ( !IsIPv6() ) {
82 shift = 12;
83 }
84
85 for (; shift<sizeof(m_SocketAddr.sin6_addr) ; shift++) {
86 byte= *(ptr+shift);
87
88 if (byte == 0xFF) {
89 len += 8;
90 continue ; /* A short-cut */
91 }
92
93 for (caught = 0 , bit= 7 ; !caught && (bit <= 7); bit--) {
94 caught = ((byte & 0x80) == 0x00); /* Found a '0' at 'bit' ? */
95
96 if (!caught)
97 len++;
98
99 byte <<= 1;
100 }
101
102 if (caught)
103 break; /* We have found the most significant "0" bit. */
104 }
105
106 return len;
107 }
108
109 int
110 Ip::Address::ApplyMask(Ip::Address const &mask_addr)
111 {
112 uint32_t *p1 = (uint32_t*)(&m_SocketAddr.sin6_addr);
113 uint32_t const *p2 = (uint32_t const *)(&mask_addr.m_SocketAddr.sin6_addr);
114 unsigned int blen = sizeof(m_SocketAddr.sin6_addr)/sizeof(uint32_t);
115 unsigned int changes = 0;
116
117 for (unsigned int i = 0; i < blen; i++) {
118 if ((p1[i] & p2[i]) != p1[i])
119 changes++;
120
121 p1[i] &= p2[i];
122 }
123
124 return changes;
125 }
126
127 bool
128 Ip::Address::ApplyMask(const unsigned int cidr, int mtype)
129 {
130 uint8_t clearbits = 0;
131 uint8_t* p = NULL;
132
133 // validation and short-cuts.
134 if (cidr > 128)
135 return false;
136
137 if (cidr > 32 && mtype == AF_INET)
138 return false;
139
140 if (cidr == 0) {
141 /* CIDR /0 is NoAddr regardless of the IPv4/IPv6 protocol */
142 SetNoAddr();
143 return true;
144 }
145
146 clearbits = (uint8_t)( (mtype==AF_INET6?128:32) -cidr);
147
148 // short-cut
149 if (clearbits == 0)
150 return true;
151
152 p = (uint8_t*)(&m_SocketAddr.sin6_addr) + 15;
153
154 for (; clearbits>0 && p >= (uint8_t*)&m_SocketAddr.sin6_addr ; p-- ) {
155 if (clearbits < 8) {
156 *p &= ((0xFF << clearbits) & 0xFF);
157 clearbits = 0;
158 } else {
159 *p &= 0x00;
160 clearbits -= 8;
161 }
162 }
163
164 return true;
165 }
166
167 bool
168 Ip::Address::IsSockAddr() const
169 {
170 return (m_SocketAddr.sin6_port != 0);
171 }
172
173 bool
174 Ip::Address::IsIPv4() const
175 {
176 return IsAnyAddr() || IsNoAddr() || IN6_IS_ADDR_V4MAPPED( &m_SocketAddr.sin6_addr );
177 }
178
179 bool
180 Ip::Address::IsIPv6() const
181 {
182 return IsAnyAddr() || IsNoAddr() || !IN6_IS_ADDR_V4MAPPED( &m_SocketAddr.sin6_addr );
183 }
184
185 bool
186 Ip::Address::IsAnyAddr() const
187 {
188 return IN6_IS_ADDR_UNSPECIFIED( &m_SocketAddr.sin6_addr );
189 }
190
191 /// NOTE: Does NOT clear the Port stored. Ony the Address and Type.
192 void
193 Ip::Address::SetAnyAddr()
194 {
195 memset(&m_SocketAddr.sin6_addr, 0, sizeof(struct in6_addr) );
196 }
197
198 /// NOTE: completely empties the Ip::Address structure. Address, Port, Type, everything.
199 void
200 Ip::Address::SetEmpty()
201 {
202 memset(&m_SocketAddr, 0, sizeof(m_SocketAddr) );
203 }
204
205 #if _SQUID_AIX_
206 // Bug 2885 comment 78 explains.
207 // In short AIX has a different netinet/in.h union definition
208 const struct in6_addr Ip::Address::v4_localhost = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x7f000001 }}};
209 const struct in6_addr Ip::Address::v4_anyaddr = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x00000000 }}};
210 const struct in6_addr Ip::Address::v4_noaddr = {{{ 0x00000000, 0x00000000, 0x0000ffff, 0xffffffff }}};
211 const struct in6_addr Ip::Address::v6_noaddr = {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}};
212 #else
213 const struct in6_addr Ip::Address::v4_localhost = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
214 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 }}
215 };
216 const struct in6_addr Ip::Address::v4_anyaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }}
218 };
219 const struct in6_addr Ip::Address::v4_noaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
220 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
221 };
222 const struct in6_addr Ip::Address::v6_noaddr = {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
223 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
224 };
225 #endif
226
227 bool
228 Ip::Address::SetIPv4()
229 {
230 if ( IsLocalhost() ) {
231 m_SocketAddr.sin6_addr = v4_localhost;
232 return true;
233 }
234
235 if ( IsAnyAddr() ) {
236 m_SocketAddr.sin6_addr = v4_anyaddr;
237 return true;
238 }
239
240 if ( IsIPv4())
241 return true;
242
243 // anything non-IPv4 and non-convertable is BAD.
244 return false;
245 }
246
247 bool
248 Ip::Address::IsLocalhost() const
249 {
250 return IN6_IS_ADDR_LOOPBACK( &m_SocketAddr.sin6_addr ) || IN6_ARE_ADDR_EQUAL( &m_SocketAddr.sin6_addr, &v4_localhost );
251 }
252
253 void
254 Ip::Address::SetLocalhost()
255 {
256 if (Ip::EnableIpv6) {
257 m_SocketAddr.sin6_addr = in6addr_loopback;
258 m_SocketAddr.sin6_family = AF_INET6;
259 } else {
260 m_SocketAddr.sin6_addr = v4_localhost;
261 m_SocketAddr.sin6_family = AF_INET;
262 }
263 }
264
265 bool
266 Ip::Address::IsSiteLocal6() const
267 {
268 return IN6_IS_ADDR_SITELOCAL( &m_SocketAddr.sin6_addr );
269 }
270
271 bool
272 Ip::Address::IsSlaac() const
273 {
274 return m_SocketAddr.sin6_addr.s6_addr[10] == htons(0xff) &&
275 m_SocketAddr.sin6_addr.s6_addr[11] == htons(0xfe);
276 }
277
278 bool
279 Ip::Address::IsNoAddr() const
280 {
281 // IFF the address == 0xff..ff (all ones)
282 return IN6_ARE_ADDR_EQUAL( &m_SocketAddr.sin6_addr, &v6_noaddr );
283 }
284
285 void
286 Ip::Address::SetNoAddr()
287 {
288 memset(&m_SocketAddr.sin6_addr, 0xFF, sizeof(struct in6_addr) );
289 m_SocketAddr.sin6_family = AF_INET6;
290 }
291
292 bool
293 Ip::Address::GetReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const
294 {
295 char *p = buf;
296 unsigned char const *r = dat.s6_addr;
297
298 /* RFC1886 says: */
299 /* 4321:0:1:2:3:4:567:89ab */
300 /* must be sent */
301 /* 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. */
302
303 /* Work from the binary field. Anything else may have representation changes. */
304 /* The sin6_port and sin6_addr members shall be in network byte order. */
305
306 /* Compile Err: 'Too many arguments for format. */
307
308 for (int i = 15; i >= 0; i--, p+=4) {
309 snprintf(p, 5, "%x.%x.", ((r[i])&0xf), (((r[i])>>4)&0xf) );
310 }
311
312 /* RFC3152 says: */
313 /* ip6.int is now deprecated TLD, use ip6.arpa instead. */
314 snprintf(p,10,"ip6.arpa.");
315
316 return true;
317 }
318
319 bool
320 Ip::Address::GetReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const
321 {
322 unsigned int i = (unsigned int) ntohl(dat.s_addr);
323 snprintf(buf, 32, "%u.%u.%u.%u.in-addr.arpa.",
324 i & 255,
325 (i >> 8) & 255,
326 (i >> 16) & 255,
327 (i >> 24) & 255);
328 return true;
329 }
330
331 bool
332 Ip::Address::GetReverseString(char buf[MAX_IPSTRLEN], int show_type) const
333 {
334
335 if (show_type == AF_UNSPEC) {
336 show_type = IsIPv6() ? AF_INET6 : AF_INET ;
337 }
338
339 if (show_type == AF_INET && IsIPv4()) {
340 struct in_addr* tmp = (struct in_addr*)&m_SocketAddr.sin6_addr.s6_addr[12];
341 return GetReverseString4(buf, *tmp);
342 } else if ( show_type == AF_INET6 && IsIPv6() ) {
343 return GetReverseString6(buf, m_SocketAddr.sin6_addr);
344 }
345
346 debugs(14,0, "Unable to convert '" << NtoA(buf,MAX_IPSTRLEN) << "' to the rDNS type requested.");
347
348 buf[0] = '\0';
349
350 return false;
351 }
352
353 Ip::Address&
354 Ip::Address::operator =(const Ip::Address &s)
355 {
356 memcpy(this, &s, sizeof(Ip::Address));
357 return *this;
358 };
359
360 Ip::Address::Address(const char*s)
361 {
362 SetEmpty();
363 LookupHostIP(s, true);
364 }
365
366 bool
367 Ip::Address::operator =(const char* s)
368 {
369 return LookupHostIP(s, true);
370 }
371
372 bool
373 Ip::Address::GetHostByName(const char* s)
374 {
375 return LookupHostIP(s, false);
376 }
377
378 bool
379 Ip::Address::LookupHostIP(const char *s, bool nodns)
380 {
381 int err = 0;
382
383 short port = 0;
384
385 struct addrinfo *res = NULL;
386
387 struct addrinfo want;
388
389 memset(&want, 0, sizeof(struct addrinfo));
390 if (nodns) {
391 want.ai_flags = AI_NUMERICHOST; // prevent actual DNS lookups!
392 }
393 #if 0
394 else if (!Ip::EnableIpv6)
395 want.ai_family = AF_INET; // maybe prevent IPv6 DNS lookups.
396 #endif
397
398 if ( (err = getaddrinfo(s, NULL, &want, &res)) != 0) {
399 debugs(14,3, HERE << "Given Non-IP '" << s << "': " << gai_strerror(err) );
400 /* free the memory getaddrinfo() dynamically allocated. */
401 if (res) {
402 freeaddrinfo(res);
403 res = NULL;
404 }
405 return false;
406 }
407
408 /*
409 * NP: =(sockaddr_*) may alter the port. we don't want that.
410 * all we have been given as input was an IPA.
411 */
412 port = GetPort();
413 operator=(*res);
414 SetPort(port);
415
416 /* free the memory getaddrinfo() dynamically allocated. */
417 freeaddrinfo(res);
418
419 res = NULL;
420
421 return true;
422 }
423
424 Ip::Address::Address(struct sockaddr_in const &s)
425 {
426 SetEmpty();
427 operator=(s);
428 };
429
430 Ip::Address &
431 Ip::Address::operator =(struct sockaddr_in const &s)
432 {
433 Map4to6((const in_addr)s.sin_addr, m_SocketAddr.sin6_addr);
434 m_SocketAddr.sin6_port = s.sin_port;
435 m_SocketAddr.sin6_family = AF_INET6;
436 return *this;
437 };
438
439 Ip::Address &
440 Ip::Address::operator =(const struct sockaddr_storage &s)
441 {
442 /* some AF_* magic to tell socket types apart and what we need to do */
443 if (s.ss_family == AF_INET6) {
444 memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in));
445 } else { // convert it to our storage mapping.
446 struct sockaddr_in *sin = (struct sockaddr_in*)&s;
447 m_SocketAddr.sin6_port = sin->sin_port;
448 Map4to6( sin->sin_addr, m_SocketAddr.sin6_addr);
449 }
450 return *this;
451 };
452
453 Ip::Address::Address(struct sockaddr_in6 const &s)
454 {
455 SetEmpty();
456 operator=(s);
457 };
458
459 Ip::Address &
460 Ip::Address::operator =(struct sockaddr_in6 const &s)
461 {
462 memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in6));
463
464 return *this;
465 };
466
467 Ip::Address::Address(struct in_addr const &s)
468 {
469 SetEmpty();
470 operator=(s);
471 };
472
473 Ip::Address &
474 Ip::Address::operator =(struct in_addr const &s)
475 {
476 Map4to6((const in_addr)s, m_SocketAddr.sin6_addr);
477 m_SocketAddr.sin6_family = AF_INET6;
478 return *this;
479 };
480
481 Ip::Address::Address(struct in6_addr const &s)
482 {
483 SetEmpty();
484 operator=(s);
485 };
486
487 Ip::Address &
488 Ip::Address::operator =(struct in6_addr const &s)
489 {
490
491 memcpy(&m_SocketAddr.sin6_addr, &s, sizeof(struct in6_addr));
492 m_SocketAddr.sin6_family = AF_INET6;
493
494 return *this;
495 };
496
497 Ip::Address::Address(const Ip::Address &s)
498 {
499 SetEmpty();
500 operator=(s);
501 }
502
503 Ip::Address::Address(Ip::Address *s)
504 {
505 SetEmpty();
506 if (s)
507 memcpy(this, s, sizeof(Ip::Address));
508 }
509
510 Ip::Address::Address(const struct hostent &s)
511 {
512 SetEmpty();
513 operator=(s);
514 }
515
516 bool
517 Ip::Address::operator =(const struct hostent &s)
518 {
519
520 struct in_addr* ipv4 = NULL;
521
522 struct in6_addr* ipv6 = NULL;
523
524 //struct hostent {
525 // char *h_name; /* official name of host */
526 // char **h_aliases; /* alias list */
527 // int h_addrtype; /* host address type */
528 // int h_length; /* length of address */
529 // char **h_addr_list; /* list of addresses */
530 //}
531
532 switch (s.h_addrtype) {
533
534 case AF_INET:
535 ipv4 = (in_addr*)(s.h_addr_list[0]);
536 /* this */
537 operator=(*ipv4);
538 break;
539
540 case AF_INET6:
541 ipv6 = (in6_addr*)(s.h_addr_list[0]);
542 /* this */
543 operator=(*ipv6);
544 break;
545
546 default:
547 IASSERT("false",false);
548 return false;
549 }
550
551 return true;
552 }
553
554 Ip::Address::Address(const struct addrinfo &s)
555 {
556 SetEmpty();
557 operator=(s);
558 }
559
560 bool
561 Ip::Address::operator =(const struct addrinfo &s)
562 {
563
564 struct sockaddr_in* ipv4 = NULL;
565
566 struct sockaddr_in6* ipv6 = NULL;
567
568 //struct addrinfo {
569 // int ai_flags; /* input flags */
570 // int ai_family; /* protocol family for socket */
571 // int ai_socktype; /* socket type */
572 // int ai_protocol; /* protocol for socket */
573 // socklen_t ai_addrlen; /* length of socket-address */
574 // struct sockaddr *ai_addr; /* socket-address for socket */
575 // char *ai_canonname; /* canonical name for service location */
576 // struct addrinfo *ai_next; /* pointer to next in list */
577 //}
578
579 switch (s.ai_family) {
580
581 case AF_INET:
582 ipv4 = (sockaddr_in*)(s.ai_addr);
583 /* this */
584 assert(ipv4);
585 operator=(*ipv4);
586 break;
587
588 case AF_INET6:
589 ipv6 = (sockaddr_in6*)(s.ai_addr);
590 /* this */
591 assert(ipv6);
592 operator=(*ipv6);
593 break;
594
595 case AF_UNSPEC:
596 default:
597 // attempt to handle partially initialised addrinfo.
598 // such as those where data only comes from getsockopt()
599 if (s.ai_addr != NULL) {
600 if (s.ai_addrlen == sizeof(struct sockaddr_in6)) {
601 operator=(*((struct sockaddr_in6*)s.ai_addr));
602 return true;
603 } else if (s.ai_addrlen == sizeof(struct sockaddr_in)) {
604 operator=(*((struct sockaddr_in*)s.ai_addr));
605 return true;
606 }
607 }
608 return false;
609 }
610
611 return true;
612 }
613
614 void
615 Ip::Address::GetAddrInfo(struct addrinfo *&dst, int force) const
616 {
617 if (dst == NULL) {
618 dst = new addrinfo;
619 }
620
621 memset(dst, 0, sizeof(struct addrinfo));
622
623 // set defaults
624 dst->ai_flags = AI_NUMERICHOST;
625
626 if (dst->ai_socktype == 0)
627 dst->ai_socktype = SOCK_STREAM;
628
629 if (dst->ai_socktype == SOCK_STREAM // implies TCP
630 && dst->ai_protocol == 0)
631 dst->ai_protocol = IPPROTO_TCP;
632
633 if (dst->ai_socktype == SOCK_DGRAM // implies UDP
634 && dst->ai_protocol == 0)
635 dst->ai_protocol = IPPROTO_UDP;
636
637 if (force == AF_INET6 || (force == AF_UNSPEC && IsIPv6()) ) {
638 dst->ai_addr = (struct sockaddr*)new sockaddr_in6;
639
640 memset(dst->ai_addr,0,sizeof(struct sockaddr_in6));
641
642 GetSockAddr(*((struct sockaddr_in6*)dst->ai_addr));
643
644 dst->ai_addrlen = sizeof(struct sockaddr_in6);
645
646 dst->ai_family = ((struct sockaddr_in6*)dst->ai_addr)->sin6_family;
647
648 #if 0
649 /**
650 * Enable only if you must and please report to squid-dev if you find a need for this.
651 *
652 * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY).
653 * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx
654 * Linux appears to only do some things when its present.
655 * (93) Bad Protocol
656 * FreeBSD dies horribly when using dual-stack with it set.
657 * (43) Protocol not supported
658 */
659 dst->ai_protocol = IPPROTO_IPV6;
660 #endif
661
662 } else if ( force == AF_INET || (force == AF_UNSPEC && IsIPv4()) ) {
663
664 dst->ai_addr = (struct sockaddr*)new sockaddr_in;
665
666 memset(dst->ai_addr,0,sizeof(struct sockaddr_in));
667
668 GetSockAddr(*((struct sockaddr_in*)dst->ai_addr));
669
670 dst->ai_addrlen = sizeof(struct sockaddr_in);
671
672 dst->ai_family = ((struct sockaddr_in*)dst->ai_addr)->sin_family;
673 } else {
674 IASSERT("false",false);
675 }
676 }
677
678 void
679 Ip::Address::InitAddrInfo(struct addrinfo *&ai)
680 {
681 if (ai == NULL) {
682 ai = new addrinfo;
683 memset(ai,0,sizeof(struct addrinfo));
684 }
685
686 // remove any existing data.
687 if (ai->ai_addr) delete ai->ai_addr;
688
689 ai->ai_addr = (struct sockaddr*)new sockaddr_in6;
690 memset(ai->ai_addr, 0, sizeof(struct sockaddr_in6));
691
692 ai->ai_addrlen = sizeof(struct sockaddr_in6);
693
694 }
695
696 void
697 Ip::Address::FreeAddrInfo(struct addrinfo *&ai)
698 {
699 if (ai == NULL) return;
700
701 if (ai->ai_addr) delete ai->ai_addr;
702
703 ai->ai_addr = NULL;
704
705 ai->ai_addrlen = 0;
706
707 // NP: name fields are NOT allocated at present.
708 delete ai;
709
710 ai = NULL;
711 }
712
713 int
714 Ip::Address::matchIPAddr(const Ip::Address &rhs) const
715 {
716 uint8_t *l = (uint8_t*)m_SocketAddr.sin6_addr.s6_addr;
717 uint8_t *r = (uint8_t*)rhs.m_SocketAddr.sin6_addr.s6_addr;
718
719 // loop a byte-wise compare
720 // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR
721 // expected difference on CIDR is gt/eq or lt/eq ONLY.
722 for (unsigned int i = 0 ; i < sizeof(m_SocketAddr.sin6_addr) ; i++) {
723
724 if (l[i] < r[i])
725 return -1;
726
727 if (l[i] > r[i])
728 return 1;
729 }
730
731 return 0;
732 }
733
734 int
735 Ip::Address::compareWhole(const Ip::Address &rhs) const
736 {
737 return memcmp(this, &rhs, sizeof(*this));
738 }
739
740 bool
741 Ip::Address::operator ==(const Ip::Address &s) const
742 {
743 return (0 == matchIPAddr(s));
744 }
745
746 bool
747 Ip::Address::operator !=(const Ip::Address &s) const
748 {
749 return ! ( operator==(s) );
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 bool
762 Ip::Address::operator >=(const Ip::Address &rhs) const
763 {
764 if (IsNoAddr() && !rhs.IsNoAddr())
765 return true;
766
767 return ( matchIPAddr(rhs) >= 0);
768 }
769
770 bool
771 Ip::Address::operator >(const Ip::Address &rhs) const
772 {
773 if (IsNoAddr() && !rhs.IsNoAddr())
774 return true;
775
776 return ( matchIPAddr(rhs) > 0);
777 }
778
779 bool
780 Ip::Address::operator <(const Ip::Address &rhs) const
781 {
782 if (IsAnyAddr() && !rhs.IsAnyAddr())
783 return true;
784
785 return ( matchIPAddr(rhs) < 0);
786 }
787
788 u_short
789 Ip::Address::GetPort() const
790 {
791 return ntohs( m_SocketAddr.sin6_port );
792 }
793
794 u_short
795 Ip::Address::SetPort(u_short prt)
796 {
797 m_SocketAddr.sin6_port = htons(prt);
798
799 return prt;
800 }
801
802 /**
803 * NtoA Given a buffer writes a readable ascii version of the IPA and/or port stored
804 *
805 * Buffer must be of a size large enough to hold the converted address.
806 * This size is provided in the form of a global defined variable MAX_IPSTRLEN
807 * Should a buffer shorter be provided the string result will be truncated
808 * at the length of the available buffer.
809 *
810 * A copy of the buffer is also returned for simple immediate display.
811 */
812 char *
813 Ip::Address::NtoA(char* buf, const unsigned int blen, int force) const
814 {
815 // Ensure we have a buffer.
816 if (buf == NULL) {
817 return NULL;
818 }
819
820 /* some external code may have blindly memset a parent. */
821 /* thats okay, our default is known */
822 if ( IsAnyAddr() ) {
823 memcpy(buf,"::\0", min(static_cast<unsigned int>(3),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, &m_SocketAddr.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,0,"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::ToHostname(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 NtoA(p, blen-8, AF_INET6);
872 else
873 NtoA(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 += ToHostname(p, blen);
903
904 if (m_SocketAddr.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", GetPort() );
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 = m_SocketAddr.sin6_port;
943 Map6to4( m_SocketAddr.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, &m_SocketAddr, 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(in6_addr &buf) const
1007 {
1008 memcpy(&buf, &m_SocketAddr.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((const in6_addr)m_SocketAddr.sin6_addr, buf);
1016 return true;
1017 }
1018
1019 // default:
1020 // non-compatible IPv6 Pure Address
1021
1022 debugs(14,1, 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 }