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