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