]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ip/Address.cc
Merged from 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 "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,0, "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 #if 0
404 else if (!Ip::EnableIpv6)
405 want.ai_family = AF_INET; // maybe prevent IPv6 DNS lookups.
406 #endif
407
408 if ( (err = getaddrinfo(s, NULL, &want, &res)) != 0) {
409 debugs(14,3, HERE << "Given Non-IP '" << s << "': " << gai_strerror(err) );
410 /* free the memory getaddrinfo() dynamically allocated. */
411 if (res) {
412 freeaddrinfo(res);
413 res = NULL;
414 }
415 return false;
416 }
417
418 /*
419 * NP: =(sockaddr_*) may alter the port. we don't want that.
420 * all we have been given as input was an IPA.
421 */
422 port = GetPort();
423 operator=(*res);
424 SetPort(port);
425
426 /* free the memory getaddrinfo() dynamically allocated. */
427 freeaddrinfo(res);
428
429 res = NULL;
430
431 return true;
432 }
433
434 Ip::Address::Address(struct sockaddr_in const &s)
435 {
436 SetEmpty();
437 operator=(s);
438 };
439
440 Ip::Address &
441 Ip::Address::operator =(struct sockaddr_in const &s)
442 {
443 Map4to6((const in_addr)s.sin_addr, m_SocketAddr.sin6_addr);
444 m_SocketAddr.sin6_port = s.sin_port;
445 m_SocketAddr.sin6_family = AF_INET6;
446 return *this;
447 };
448
449 Ip::Address &
450 Ip::Address::operator =(const struct sockaddr_storage &s)
451 {
452 /* some AF_* magic to tell socket types apart and what we need to do */
453 if (s.ss_family == AF_INET6) {
454 memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in));
455 } else { // convert it to our storage mapping.
456 struct sockaddr_in *sin = (struct sockaddr_in*)&s;
457 m_SocketAddr.sin6_port = sin->sin_port;
458 Map4to6( sin->sin_addr, m_SocketAddr.sin6_addr);
459 }
460 return *this;
461 };
462
463 Ip::Address::Address(struct sockaddr_in6 const &s)
464 {
465 SetEmpty();
466 operator=(s);
467 };
468
469 Ip::Address &
470 Ip::Address::operator =(struct sockaddr_in6 const &s)
471 {
472 memcpy(&m_SocketAddr, &s, sizeof(struct sockaddr_in6));
473
474 return *this;
475 };
476
477 Ip::Address::Address(struct in_addr const &s)
478 {
479 SetEmpty();
480 operator=(s);
481 };
482
483 Ip::Address &
484 Ip::Address::operator =(struct in_addr const &s)
485 {
486 Map4to6((const in_addr)s, m_SocketAddr.sin6_addr);
487 m_SocketAddr.sin6_family = AF_INET6;
488 return *this;
489 };
490
491 Ip::Address::Address(struct in6_addr const &s)
492 {
493 SetEmpty();
494 operator=(s);
495 };
496
497 Ip::Address &
498 Ip::Address::operator =(struct in6_addr const &s)
499 {
500
501 memcpy(&m_SocketAddr.sin6_addr, &s, sizeof(struct in6_addr));
502 m_SocketAddr.sin6_family = AF_INET6;
503
504 return *this;
505 };
506
507 Ip::Address::Address(const Ip::Address &s)
508 {
509 SetEmpty();
510 operator=(s);
511 }
512
513 Ip::Address::Address(Ip::Address *s)
514 {
515 SetEmpty();
516 if (s)
517 memcpy(this, s, sizeof(Ip::Address));
518 }
519
520 Ip::Address::Address(const struct hostent &s)
521 {
522 SetEmpty();
523 operator=(s);
524 }
525
526 bool
527 Ip::Address::operator =(const struct hostent &s)
528 {
529
530 struct in_addr* ipv4 = NULL;
531
532 struct in6_addr* ipv6 = NULL;
533
534 //struct hostent {
535 // char *h_name; /* official name of host */
536 // char **h_aliases; /* alias list */
537 // int h_addrtype; /* host address type */
538 // int h_length; /* length of address */
539 // char **h_addr_list; /* list of addresses */
540 //}
541
542 switch (s.h_addrtype) {
543
544 case AF_INET:
545 ipv4 = (in_addr*)(s.h_addr_list[0]);
546 /* this */
547 operator=(*ipv4);
548 break;
549
550 case AF_INET6:
551 ipv6 = (in6_addr*)(s.h_addr_list[0]);
552 /* this */
553 operator=(*ipv6);
554 break;
555
556 default:
557 IASSERT("false",false);
558 return false;
559 }
560
561 return true;
562 }
563
564 Ip::Address::Address(const struct addrinfo &s)
565 {
566 SetEmpty();
567 operator=(s);
568 }
569
570 bool
571 Ip::Address::operator =(const struct addrinfo &s)
572 {
573
574 struct sockaddr_in* ipv4 = NULL;
575
576 struct sockaddr_in6* ipv6 = NULL;
577
578 //struct addrinfo {
579 // int ai_flags; /* input flags */
580 // int ai_family; /* protocol family for socket */
581 // int ai_socktype; /* socket type */
582 // int ai_protocol; /* protocol for socket */
583 // socklen_t ai_addrlen; /* length of socket-address */
584 // struct sockaddr *ai_addr; /* socket-address for socket */
585 // char *ai_canonname; /* canonical name for service location */
586 // struct addrinfo *ai_next; /* pointer to next in list */
587 //}
588
589 switch (s.ai_family) {
590
591 case AF_INET:
592 ipv4 = (sockaddr_in*)(s.ai_addr);
593 /* this */
594 assert(ipv4);
595 operator=(*ipv4);
596 break;
597
598 case AF_INET6:
599 ipv6 = (sockaddr_in6*)(s.ai_addr);
600 /* this */
601 assert(ipv6);
602 operator=(*ipv6);
603 break;
604
605 case AF_UNSPEC:
606 default:
607 // attempt to handle partially initialised addrinfo.
608 // such as those where data only comes from getsockopt()
609 if (s.ai_addr != NULL) {
610 if (s.ai_addrlen == sizeof(struct sockaddr_in6)) {
611 operator=(*((struct sockaddr_in6*)s.ai_addr));
612 return true;
613 } else if (s.ai_addrlen == sizeof(struct sockaddr_in)) {
614 operator=(*((struct sockaddr_in*)s.ai_addr));
615 return true;
616 }
617 }
618 return false;
619 }
620
621 return true;
622 }
623
624 void
625 Ip::Address::GetAddrInfo(struct addrinfo *&dst, int force) const
626 {
627 if (dst == NULL) {
628 dst = new addrinfo;
629 }
630
631 memset(dst, 0, sizeof(struct addrinfo));
632
633 // set defaults
634 dst->ai_flags = AI_NUMERICHOST;
635
636 if (dst->ai_socktype == 0)
637 dst->ai_socktype = SOCK_STREAM;
638
639 if (dst->ai_socktype == SOCK_STREAM // implies TCP
640 && dst->ai_protocol == 0)
641 dst->ai_protocol = IPPROTO_TCP;
642
643 if (dst->ai_socktype == SOCK_DGRAM // implies UDP
644 && dst->ai_protocol == 0)
645 dst->ai_protocol = IPPROTO_UDP;
646
647 if (force == AF_INET6 || (force == AF_UNSPEC && IsIPv6()) ) {
648 dst->ai_addr = (struct sockaddr*)new sockaddr_in6;
649
650 memset(dst->ai_addr,0,sizeof(struct sockaddr_in6));
651
652 GetSockAddr(*((struct sockaddr_in6*)dst->ai_addr));
653
654 dst->ai_addrlen = sizeof(struct sockaddr_in6);
655
656 dst->ai_family = ((struct sockaddr_in6*)dst->ai_addr)->sin6_family;
657
658 #if 0
659 /**
660 * Enable only if you must and please report to squid-dev if you find a need for this.
661 *
662 * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY).
663 * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx
664 * Linux appears to only do some things when its present.
665 * (93) Bad Protocol
666 * FreeBSD dies horribly when using dual-stack with it set.
667 * (43) Protocol not supported
668 */
669 dst->ai_protocol = IPPROTO_IPV6;
670 #endif
671
672 } else if ( force == AF_INET || (force == AF_UNSPEC && IsIPv4()) ) {
673
674 dst->ai_addr = (struct sockaddr*)new sockaddr_in;
675
676 memset(dst->ai_addr,0,sizeof(struct sockaddr_in));
677
678 GetSockAddr(*((struct sockaddr_in*)dst->ai_addr));
679
680 dst->ai_addrlen = sizeof(struct sockaddr_in);
681
682 dst->ai_family = ((struct sockaddr_in*)dst->ai_addr)->sin_family;
683 } else {
684 IASSERT("false",false);
685 }
686 }
687
688 void
689 Ip::Address::InitAddrInfo(struct addrinfo *&ai)
690 {
691 if (ai == NULL) {
692 ai = new addrinfo;
693 memset(ai,0,sizeof(struct addrinfo));
694 }
695
696 // remove any existing data.
697 if (ai->ai_addr) delete ai->ai_addr;
698
699 ai->ai_addr = (struct sockaddr*)new sockaddr_in6;
700 memset(ai->ai_addr, 0, sizeof(struct sockaddr_in6));
701
702 ai->ai_addrlen = sizeof(struct sockaddr_in6);
703
704 }
705
706 void
707 Ip::Address::FreeAddrInfo(struct addrinfo *&ai)
708 {
709 if (ai == NULL) return;
710
711 if (ai->ai_addr) delete ai->ai_addr;
712
713 ai->ai_addr = NULL;
714
715 ai->ai_addrlen = 0;
716
717 // NP: name fields are NOT allocated at present.
718 delete ai;
719
720 ai = NULL;
721 }
722
723 int
724 Ip::Address::matchIPAddr(const Ip::Address &rhs) const
725 {
726 uint8_t *l = (uint8_t*)m_SocketAddr.sin6_addr.s6_addr;
727 uint8_t *r = (uint8_t*)rhs.m_SocketAddr.sin6_addr.s6_addr;
728
729 // loop a byte-wise compare
730 // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR
731 // expected difference on CIDR is gt/eq or lt/eq ONLY.
732 for (unsigned int i = 0 ; i < sizeof(m_SocketAddr.sin6_addr) ; ++i) {
733
734 if (l[i] < r[i])
735 return -1;
736
737 if (l[i] > r[i])
738 return 1;
739 }
740
741 return 0;
742 }
743
744 int
745 Ip::Address::compareWhole(const Ip::Address &rhs) const
746 {
747 return memcmp(this, &rhs, sizeof(*this));
748 }
749
750 bool
751 Ip::Address::operator ==(const Ip::Address &s) const
752 {
753 return (0 == matchIPAddr(s));
754 }
755
756 bool
757 Ip::Address::operator !=(const Ip::Address &s) const
758 {
759 return ! ( operator==(s) );
760 }
761
762 bool
763 Ip::Address::operator <=(const Ip::Address &rhs) const
764 {
765 if (IsAnyAddr() && !rhs.IsAnyAddr())
766 return true;
767
768 return (matchIPAddr(rhs) <= 0);
769 }
770
771 bool
772 Ip::Address::operator >=(const Ip::Address &rhs) const
773 {
774 if (IsNoAddr() && !rhs.IsNoAddr())
775 return true;
776
777 return ( matchIPAddr(rhs) >= 0);
778 }
779
780 bool
781 Ip::Address::operator >(const Ip::Address &rhs) const
782 {
783 if (IsNoAddr() && !rhs.IsNoAddr())
784 return true;
785
786 return ( matchIPAddr(rhs) > 0);
787 }
788
789 bool
790 Ip::Address::operator <(const Ip::Address &rhs) const
791 {
792 if (IsAnyAddr() && !rhs.IsAnyAddr())
793 return true;
794
795 return ( matchIPAddr(rhs) < 0);
796 }
797
798 unsigned short
799 Ip::Address::GetPort() const
800 {
801 return ntohs( m_SocketAddr.sin6_port );
802 }
803
804 unsigned short
805 Ip::Address::SetPort(unsigned short prt)
806 {
807 m_SocketAddr.sin6_port = htons(prt);
808
809 return prt;
810 }
811
812 /**
813 * NtoA Given a buffer writes a readable ascii version of the IPA and/or port stored
814 *
815 * Buffer must be of a size large enough to hold the converted address.
816 * This size is provided in the form of a global defined variable MAX_IPSTRLEN
817 * Should a buffer shorter be provided the string result will be truncated
818 * at the length of the available buffer.
819 *
820 * A copy of the buffer is also returned for simple immediate display.
821 */
822 char *
823 Ip::Address::NtoA(char* buf, const unsigned int blen, int force) const
824 {
825 // Ensure we have a buffer.
826 if (buf == NULL) {
827 return NULL;
828 }
829
830 /* some external code may have blindly memset a parent. */
831 /* thats okay, our default is known */
832 if ( IsAnyAddr() ) {
833 if (IsIPv6())
834 memcpy(buf,"::\0", min(static_cast<unsigned int>(3),blen));
835 else if (IsIPv4())
836 memcpy(buf,"0.0.0.0\0", min(static_cast<unsigned int>(8),blen));
837 return buf;
838 }
839
840 memset(buf,0,blen); // clear buffer before write
841
842 /* Pure-IPv6 CANNOT be displayed in IPv4 format. */
843 /* However IPv4 CAN. */
844 if ( force == AF_INET && !IsIPv4() ) {
845 if ( IsIPv6() ) {
846 memcpy(buf, "{!IPv4}\0", min(static_cast<unsigned int>(8),blen));
847 }
848 return buf;
849 }
850
851 if ( force == AF_INET6 || (force == AF_UNSPEC && IsIPv6()) ) {
852
853 inet_ntop(AF_INET6, &m_SocketAddr.sin6_addr, buf, blen);
854
855 } else if ( force == AF_INET || (force == AF_UNSPEC && IsIPv4()) ) {
856
857 struct in_addr tmp;
858 GetInAddr(tmp);
859 inet_ntop(AF_INET, &tmp, buf, blen);
860 } else {
861 debugs(14,0,"WARNING: Corrupt IP Address details OR required to display in unknown format (" <<
862 force << "). accepted={" << AF_UNSPEC << "," << AF_INET << "," << AF_INET6 << "}");
863 fprintf(stderr,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ",
864 force, AF_UNSPEC, AF_INET, AF_INET6);
865 memcpy(buf,"dead:beef::\0", min(static_cast<unsigned int>(13),blen));
866 assert(false);
867 }
868
869 return buf;
870 }
871
872 unsigned int
873 Ip::Address::ToHostname(char *buf, const unsigned int blen) const
874 {
875 char *p = buf;
876
877 if (IsIPv6() && blen > 0) {
878 *p = '[';
879 ++p;
880 }
881
882 /* 8 being space for [ ] : and port digits */
883 if ( IsIPv6() )
884 NtoA(p, blen-8, AF_INET6);
885 else
886 NtoA(p, blen-8, AF_INET);
887
888 // find the end of the new string
889 while (*p != '\0' && p < buf+blen)
890 ++p;
891
892 if (IsIPv6() && p < (buf+blen-1) ) {
893 *p = ']';
894 ++p;
895 }
896
897 /* terminate just in case. */
898 *p = '\0';
899
900 /* return size of buffer now used */
901 return (p - buf);
902 }
903
904 char *
905 Ip::Address::ToURL(char* buf, unsigned int blen) const
906 {
907 char *p = buf;
908
909 // Ensure we have a buffer.
910
911 if (buf == NULL) {
912 return NULL;
913 }
914
915 p += ToHostname(p, blen);
916
917 if (m_SocketAddr.sin6_port > 0 && p <= (buf+blen-7) ) {
918 // ':port' (short int) needs at most 6 bytes plus 1 for 0-terminator
919 snprintf(p, 7, ":%d", GetPort() );
920 }
921
922 // force a null-terminated string
923 buf[blen-1] = '\0';
924
925 return buf;
926 }
927
928 void
929 Ip::Address::GetSockAddr(struct sockaddr_storage &addr, const int family) const
930 {
931 struct sockaddr_in *sin = NULL;
932
933 if ( family == AF_INET && !IsIPv4()) {
934 // FIXME INET6: caller using the wrong socket type!
935 debugs(14, DBG_CRITICAL, HERE << "Ip::Address::GetSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this);
936 assert(false);
937 }
938
939 if ( family == AF_INET6 || (family == AF_UNSPEC && IsIPv6()) ) {
940 struct sockaddr_in6 *ss6 = (struct sockaddr_in6*)&addr;
941 GetSockAddr(*ss6);
942 } else if ( family == AF_INET || (family == AF_UNSPEC && IsIPv4()) ) {
943 sin = (struct sockaddr_in*)&addr;
944 GetSockAddr(*sin);
945 } else {
946 IASSERT("false",false);
947 }
948 }
949
950 void
951 Ip::Address::GetSockAddr(struct sockaddr_in &buf) const
952 {
953 if ( IsIPv4() ) {
954 buf.sin_family = AF_INET;
955 buf.sin_port = m_SocketAddr.sin6_port;
956 Map6to4( m_SocketAddr.sin6_addr, buf.sin_addr);
957 } else {
958 debugs(14, DBG_CRITICAL, HERE << "Ip::Address::GetSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this );
959
960 memset(&buf,0xFFFFFFFF,sizeof(struct sockaddr_in));
961 assert(false);
962 }
963
964 #if HAVE_SIN_LEN_IN_SAI
965 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
966 buf.sin_len = sizeof(struct sockaddr_in);
967 #endif
968 }
969
970 void
971 Ip::Address::GetSockAddr(struct sockaddr_in6 &buf) const
972 {
973 memcpy(&buf, &m_SocketAddr, sizeof(struct sockaddr_in6));
974 /* maintain address family. It may have changed inside us. */
975 buf.sin6_family = AF_INET6;
976
977 #if HAVE_SIN6_LEN_IN_SAI
978 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
979 buf.sin6_len = sizeof(struct sockaddr_in6);
980 #endif
981 }
982
983 void
984 Ip::Address::Map4to6(const struct in_addr &in, struct in6_addr &out) const
985 {
986 /* check for special cases */
987
988 if ( in.s_addr == 0x00000000) {
989 /* ANYADDR */
990 out = v4_anyaddr;
991 } else if ( in.s_addr == 0xFFFFFFFF) {
992 /* NOADDR */
993 out = v4_noaddr;
994 } else {
995 /* general */
996 out = v4_anyaddr;
997 out.s6_addr[12] = ((uint8_t *)&in.s_addr)[0];
998 out.s6_addr[13] = ((uint8_t *)&in.s_addr)[1];
999 out.s6_addr[14] = ((uint8_t *)&in.s_addr)[2];
1000 out.s6_addr[15] = ((uint8_t *)&in.s_addr)[3];
1001 }
1002 }
1003
1004 void
1005 Ip::Address::Map6to4(const struct in6_addr &in, struct in_addr &out) const
1006 {
1007 /* ANYADDR */
1008 /* NOADDR */
1009 /* general */
1010
1011 memset(&out, 0, sizeof(struct in_addr));
1012 ((uint8_t *)&out.s_addr)[0] = in.s6_addr[12];
1013 ((uint8_t *)&out.s_addr)[1] = in.s6_addr[13];
1014 ((uint8_t *)&out.s_addr)[2] = in.s6_addr[14];
1015 ((uint8_t *)&out.s_addr)[3] = in.s6_addr[15];
1016 }
1017
1018 void
1019 Ip::Address::GetInAddr(in6_addr &buf) const
1020 {
1021 memcpy(&buf, &m_SocketAddr.sin6_addr, sizeof(struct in6_addr));
1022 }
1023
1024 bool
1025 Ip::Address::GetInAddr(struct in_addr &buf) const
1026 {
1027 if ( IsIPv4() ) {
1028 Map6to4((const in6_addr)m_SocketAddr.sin6_addr, buf);
1029 return true;
1030 }
1031
1032 // default:
1033 // non-compatible IPv6 Pure Address
1034
1035 debugs(14,1, HERE << "Ip::Address::GetInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this);
1036 memset(&buf,0xFFFFFFFF,sizeof(struct in_addr));
1037 assert(false);
1038 return false;
1039 }