]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ip/IpAddress.cc
2 * DEBUG: section 14 IP Storage and Handling
3 * AUTHOR: Amos Jeffries
5 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
6 * ----------------------------------------------------------
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.
17 * This IpAddress 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.
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.
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.
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.
38 #include "ip/IpAddress.h"
52 #include <arpa/inet.h> /* inet_ntoa() */
56 #error "INET6 defined but has been deprecated! Try running bootstrap and configure again."
59 /* We want to use the debug routines when running as module of squid. */
60 /* otherwise fallback to printf if those are not available. */
62 # define debugs(a,b,c) // drop.
64 #warning "IpAddress built with Debugs!!"
65 # include "../src/Debug.h"
69 // So there are some places where I will drop to using Macros too.
70 // At least I can restrict them to this file so they don't corrupt the app with C code.
71 # define sin6_addr sin_addr
72 # define sin6_port sin_port
73 # define sin6_family sin_family
75 # define s6_addr s_addr
78 static const unsigned int STRLEN_IP4A
= 16; // aaa.bbb.ccc.ddd\0
79 static const unsigned int STRLEN_IP4R
= 28; // ddd.ccc.bbb.aaa.in-addr.arpa.\0
80 static const unsigned int STRLEN_IP4S
= 21; // ddd.ccc.bbb.aaa:ppppp\0
81 static const unsigned int MAX_IP4_STRLEN
= STRLEN_IP4R
;
82 static const unsigned int STRLEN_IP6A
= 42; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]/0
83 static const unsigned int STRLEN_IP6R
= 75; // f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f ipv6.arpa./0
84 static const unsigned int STRLEN_IP6S
= 48; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:00000/0
85 static const unsigned int MAX_IP6_STRLEN
= STRLEN_IP6R
;
88 /* Debugging only. Dump the address content when a fatal assert is encountered. */
90 #define IASSERT(a,b) \
91 if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \
92 printf("IpAddress invalid? with IsIPv4()=%c, IsIPv6()=%c\n",(IsIPv4()?'T':'F'),(IsIPv6()?'T':'F')); \
94 for(unsigned int i = 0; i < sizeof(m_SocketAddr.sin6_addr); i++) { \
95 printf(" %x", m_SocketAddr.sin6_addr.s6_addr[i]); \
96 } printf("\n"); assert(b); \
99 #define IASSERT(a,b) \
100 if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \
101 printf("IpAddress invalid? with IsIPv4()=%c, IsIPv6()=%c\n",(IsIPv4()?'T':'F'),(IsIPv6()?'T':'F')); \
102 printf("ADDRESS: %x\n", (unsigned int)m_SocketAddr.sin_addr.s_addr); \
107 IpAddress::IpAddress()
112 IpAddress::~IpAddress()
114 memset(this,0,sizeof(IpAddress
));
118 IpAddress::GetCIDR() const
124 const uint8_t *ptr
= m_SocketAddr
.sin6_addr
.s6_addr
;
126 const uint8_t *ptr
= (uint8_t *)&m_SocketAddr
.sin_addr
.s_addr
;
129 /* Let's scan all the bits from Most Significant to Least */
130 /* Until we find an "0" bit. Then, we return */
134 /* return IPv4 CIDR for any Mapped address */
135 /* Thus only check the mapped bit */
143 for (; shift
<sizeof(m_SocketAddr
.sin6_addr
) ; shift
++) {
148 continue ; /* A short-cut */
151 for (caught
= 0 , bit
= 7 ; !caught
&& (bit
<= 7); bit
--) {
152 caught
= ((byte
& 0x80) == 0x00); /* Found a '0' at 'bit' ? */
161 break; /* We have found the most significant "0" bit. */
167 const int IpAddress::ApplyMask(IpAddress
const &mask_addr
)
169 uint32_t *p1
= (uint32_t*)(&m_SocketAddr
.sin6_addr
);
170 uint32_t const *p2
= (uint32_t const *)(&mask_addr
.m_SocketAddr
.sin6_addr
);
171 unsigned int blen
= sizeof(m_SocketAddr
.sin6_addr
)/sizeof(uint32_t);
172 unsigned int changes
= 0;
174 for (unsigned int i
= 0; i
< blen
; i
++) {
175 if ((p1
[i
] & p2
[i
]) != p1
[i
])
181 /* we have found a situation where mask forms or destroys a IPv4 map. */
187 bool IpAddress::ApplyMask(const unsigned int cidr
, int mtype
)
189 uint8_t clearbits
= 0;
193 IASSERT("mtype != AF_INET6", mtype
!= AF_INET6
); /* using IPv6 in IPv4 is invalid. */
195 if (mtype
== AF_UNSPEC
)
199 if (mtype
== AF_UNSPEC
)
204 // validation and short-cuts.
208 if (cidr
> 32 && mtype
== AF_INET
)
212 /* CIDR /0 is NoAddr regardless of the IPv4/IPv6 protocol */
217 clearbits
= (uint8_t)( (mtype
==AF_INET6
?128:32) -cidr
);
225 p
= (uint8_t*)(&m_SocketAddr
.sin6_addr
) + 15;
229 p
= (uint8_t*)(&m_SocketAddr
.sin_addr
) + 3;
233 for (; clearbits
>0 && p
>= (uint8_t*)&m_SocketAddr
.sin6_addr
; p
-- ) {
235 *p
&= ((0xFF << clearbits
) & 0xFF);
246 bool IpAddress::IsSockAddr() const
248 return (m_SocketAddr
.sin6_port
!= 0);
251 bool IpAddress::IsIPv4() const
255 return IsAnyAddr() || IsNoAddr() ||
256 ( m_SocketAddr
.sin6_addr
.s6_addr32
[0] == htonl(0x00000000) &&
257 m_SocketAddr
.sin6_addr
.s6_addr32
[1] == htonl(0x00000000) &&
258 m_SocketAddr
.sin6_addr
.s6_addr32
[2] == htonl(0x0000FFFF)
262 return true; // enforce IPv4 in IPv4-only mode.
266 bool IpAddress::IsIPv6() const
270 return IsAnyAddr() || IsNoAddr() ||
271 !( m_SocketAddr
.sin6_addr
.s6_addr32
[0] == htonl(0x00000000) &&
272 m_SocketAddr
.sin6_addr
.s6_addr32
[1] == htonl(0x00000000) &&
273 m_SocketAddr
.sin6_addr
.s6_addr32
[2] == htonl(0x0000FFFF)
276 return false; // enforce IPv4 in IPv4-only mode.
280 bool IpAddress::IsAnyAddr() const
283 return m_SocketAddr
.sin6_addr
.s6_addr32
[0] == 0
284 && m_SocketAddr
.sin6_addr
.s6_addr32
[1] == 0
285 && m_SocketAddr
.sin6_addr
.s6_addr32
[2] == 0
286 && m_SocketAddr
.sin6_addr
.s6_addr32
[3] == 0
290 return (INADDR_ANY
== m_SocketAddr
.sin_addr
.s_addr
);
294 /// NOTE: Does NOT clear the Port stored. Ony the Address and Type.
295 void IpAddress::SetAnyAddr()
298 memset(&m_SocketAddr
.sin6_addr
, 0, sizeof(struct in6_addr
) );
300 memset(&m_SocketAddr
.sin_addr
, 0, sizeof(struct in_addr
) );
304 /// NOTE: completely empties the IpAddress structure. Address, Port, Type, everything.
305 void IpAddress::SetEmpty()
307 memset(&m_SocketAddr
, 0, sizeof(m_SocketAddr
) );
310 bool IpAddress::SetIPv4()
314 if ( IsLocalhost() ) {
315 m_SocketAddr
.sin6_addr
.s6_addr32
[2] = htonl(0xffff);
316 m_SocketAddr
.sin6_addr
.s6_addr32
[3] = htonl(0x7F000001);
321 m_SocketAddr
.sin6_addr
.s6_addr32
[2] = htonl(0xffff);
328 // anything non-IPv4 and non-convertable is BAD.
331 return true; // Always IPv4 in IPv4-only builds.
335 bool IpAddress::IsLocalhost() const
338 return ( m_SocketAddr
.sin6_addr
.s6_addr32
[0] == 0
339 && m_SocketAddr
.sin6_addr
.s6_addr32
[1] == 0
340 && m_SocketAddr
.sin6_addr
.s6_addr32
[2] == 0
341 && m_SocketAddr
.sin6_addr
.s6_addr32
[3] == htonl(0x1)
344 ( m_SocketAddr
.sin6_addr
.s6_addr32
[0] == 0
345 && m_SocketAddr
.sin6_addr
.s6_addr32
[1] == 0
346 && m_SocketAddr
.sin6_addr
.s6_addr32
[2] == htonl(0xffff)
347 && m_SocketAddr
.sin6_addr
.s6_addr32
[3] == htonl(0x7F000001)
351 return (htonl(0x7F000001) == m_SocketAddr
.sin_addr
.s_addr
);
355 void IpAddress::SetLocalhost()
359 m_SocketAddr
.sin6_addr
.s6_addr
[15] = 0x1;
360 m_SocketAddr
.sin6_family
= AF_INET6
;
363 m_SocketAddr
.sin_addr
.s_addr
= htonl(0x7F000001);
364 m_SocketAddr
.sin_family
= AF_INET
;
368 bool IpAddress::IsNoAddr() const
370 // IFF the address == 0xff..ff (all ones)
372 return m_SocketAddr
.sin6_addr
.s6_addr32
[0] == 0xFFFFFFFF
373 && m_SocketAddr
.sin6_addr
.s6_addr32
[1] == 0xFFFFFFFF
374 && m_SocketAddr
.sin6_addr
.s6_addr32
[2] == 0xFFFFFFFF
375 && m_SocketAddr
.sin6_addr
.s6_addr32
[3] == 0xFFFFFFFF
379 return 0xFFFFFFFF == m_SocketAddr
.sin_addr
.s_addr
;
383 void IpAddress::SetNoAddr()
386 memset(&m_SocketAddr
.sin6_addr
, 0xFFFFFFFF, sizeof(struct in6_addr
) );
387 m_SocketAddr
.sin6_family
= AF_INET6
;
389 memset(&m_SocketAddr
.sin_addr
, 0xFFFFFFFF, sizeof(struct in_addr
) );
390 m_SocketAddr
.sin_family
= AF_INET
;
396 bool IpAddress::GetReverseString6(char buf
[MAX_IPSTRLEN
], const struct in6_addr
&dat
) const
399 unsigned char const *r
= dat
.s6_addr
;
402 /* 4321:0:1:2:3:4:567:89ab */
404 /* 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. */
406 /* Work from the binary field. Anything else may have representation changes. */
407 /* The sin6_port and sin6_addr members shall be in network byte order. */
409 /* Compile Err: 'Too many arguments for format. */
411 for (int i
= 15; i
>= 0; i
--, p
+=4) {
412 snprintf(p
, 5, "%x.%x.", ((r
[i
])&0xf), (((r
[i
])>>4)&0xf) );
416 /* ip6.int is now deprecated TLD, use ip6.arpa instead. */
417 snprintf(p
,10,"ip6.arpa.");
424 bool IpAddress::GetReverseString4(char buf
[MAX_IPSTRLEN
], const struct in_addr
&dat
) const
426 unsigned int i
= (unsigned int) ntohl(dat
.s_addr
);
427 snprintf(buf
, 32, "%u.%u.%u.%u.in-addr.arpa.",
435 bool IpAddress::GetReverseString(char buf
[MAX_IPSTRLEN
], int show_type
) const
438 if (show_type
== AF_UNSPEC
) {
440 show_type
= IsIPv6() ? AF_INET6
: AF_INET
;
446 if (show_type
== AF_INET
&& IsIPv4()) {
449 return GetReverseString4(buf
, *(struct in_addr
*)&m_SocketAddr
.sin6_addr
.s6_addr32
[3] );
450 } else if ( show_type
== AF_INET6
&& IsIPv6() ) {
451 return GetReverseString6(buf
, m_SocketAddr
.sin6_addr
);
453 return GetReverseString4(buf
, m_SocketAddr
.sin_addr
);
457 debugs(14,0, "Unable to convert '" << NtoA(buf
,MAX_IPSTRLEN
) << "' to the rDNS type requested.");
464 IpAddress
& IpAddress::operator =(const IpAddress
&s
)
466 memcpy(this, &s
, sizeof(IpAddress
));
470 IpAddress::IpAddress(const char*s
)
476 bool IpAddress::operator =(const char* s
)
478 return LookupHostIP(s
, true);
481 bool IpAddress::GetHostByName(const char* s
)
483 return LookupHostIP(s
, false);
486 bool IpAddress::LookupHostIP(const char *s
, bool nodns
)
492 struct addrinfo
*res
= NULL
;
494 struct addrinfo want
;
496 memset(&want
, 0, sizeof(struct addrinfo
));
498 want
.ai_flags
= AI_NUMERICHOST
; // prevent actual DNS lookups!
501 want
.ai_family
= AF_INET
;
504 if ( (err
= xgetaddrinfo(s
, NULL
, &want
, &res
)) != 0) {
505 debugs(14,1, HERE
<< "Given Bad IP '" << s
<< "': " << xgai_strerror(err
) );
506 /* free the memory xgetaddrinfo() dynamically allocated. */
515 * NP: =(sockaddr_*) may alter the port. we don't want that.
516 * all we have been given as input was an IPA.
522 /* free the memory xgetaddrinfo() dynamically allocated. */
530 IpAddress::IpAddress(struct sockaddr_in
const &s
)
536 IpAddress
& IpAddress::operator =(struct sockaddr_in
const &s
)
539 Map4to6((const in_addr
)s
.sin_addr
, m_SocketAddr
.sin6_addr
);
540 m_SocketAddr
.sin6_port
= s
.sin_port
;
541 m_SocketAddr
.sin6_family
= AF_INET6
;
544 memcpy(&m_SocketAddr
, &s
, sizeof(struct sockaddr_in
));
547 /* maintain stored family values properly */
553 IpAddress
& IpAddress::operator =(const struct sockaddr_storage
&s
)
556 /* some AF_* magic to tell socket types apart and what we need to do */
557 if (s
.ss_family
== AF_INET6
) {
558 memcpy(&m_SocketAddr
, &s
, sizeof(struct sockaddr_in
));
559 } else { // convert it to our storage mapping.
560 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&s
;
561 m_SocketAddr
.sin6_port
= sin
->sin_port
;
562 Map4to6( sin
->sin_addr
, m_SocketAddr
.sin6_addr
);
565 memcpy(&m_SocketAddr
, &s
, sizeof(struct sockaddr_in
));
570 void IpAddress::check4Mapped()
573 // TODO use this NOW to set the sin6_family properly on exporting. not on import.
577 IpAddress::IpAddress(struct sockaddr_in6
const &s
)
583 IpAddress
& IpAddress::operator =(struct sockaddr_in6
const &s
)
585 memcpy(&m_SocketAddr
, &s
, sizeof(struct sockaddr_in6
));
587 /* maintain address family properly */
594 IpAddress::IpAddress(struct in_addr
const &s
)
600 IpAddress
& IpAddress::operator =(struct in_addr
const &s
)
603 Map4to6((const in_addr
)s
, m_SocketAddr
.sin6_addr
);
604 m_SocketAddr
.sin6_family
= AF_INET6
;
608 memcpy(&m_SocketAddr
.sin_addr
, &s
, sizeof(struct in_addr
));
612 /* maintain stored family type properly */
620 IpAddress::IpAddress(struct in6_addr
const &s
)
626 IpAddress
& IpAddress::operator =(struct in6_addr
const &s
)
629 memcpy(&m_SocketAddr
.sin6_addr
, &s
, sizeof(struct in6_addr
));
630 m_SocketAddr
.sin6_family
= AF_INET6
;
632 /* maintain address family type properly */
640 IpAddress::IpAddress(const IpAddress
&s
)
646 IpAddress::IpAddress(IpAddress
*s
)
650 memcpy(this, s
, sizeof(IpAddress
));
653 IpAddress::IpAddress(const struct hostent
&s
)
659 bool IpAddress::operator =(const struct hostent
&s
)
662 struct in_addr
* ipv4
= NULL
;
664 struct in6_addr
* ipv6
= NULL
;
667 // char *h_name; /* official name of host */
668 // char **h_aliases; /* alias list */
669 // int h_addrtype; /* host address type */
670 // int h_length; /* length of address */
671 // char **h_addr_list; /* list of addresses */
674 switch (s
.h_addrtype
) {
677 ipv4
= (in_addr
*)(s
.h_addr_list
[0]);
683 ipv6
= (in6_addr
*)(s
.h_addr_list
[0]);
689 debugs(14,1, HERE
<< "Discarded IPv6 Address. Protocol disabled.");
691 // FIXME see if there is another address in the list that might be usable ??
698 IASSERT("false",false);
705 IpAddress::IpAddress(const struct addrinfo
&s
)
711 bool IpAddress::operator =(const struct addrinfo
&s
)
714 struct sockaddr_in
* ipv4
= NULL
;
716 struct sockaddr_in6
* ipv6
= NULL
;
719 // int ai_flags; /* input flags */
720 // int ai_family; /* protocol family for socket */
721 // int ai_socktype; /* socket type */
722 // int ai_protocol; /* protocol for socket */
723 // socklen_t ai_addrlen; /* length of socket-address */
724 // struct sockaddr *ai_addr; /* socket-address for socket */
725 // char *ai_canonname; /* canonical name for service location */
726 // struct addrinfo *ai_next; /* pointer to next in list */
729 switch (s
.ai_family
) {
732 ipv4
= (sockaddr_in
*)(s
.ai_addr
);
739 ipv6
= (sockaddr_in6
*)(s
.ai_addr
);
746 debugs(14,1, HERE
<< "Discarded IPv6 Address. Protocol disabled.");
748 // see if there is another address in the list that might be usable ??
751 return operator=(*s
.ai_next
);
760 // attempt to handle partially initialised addrinfo.
761 // such as those where data only comes from getsockopt()
762 if (s
.ai_addr
!= NULL
) {
764 if (s
.ai_addrlen
== sizeof(struct sockaddr_in6
)) {
765 operator=(*((struct sockaddr_in6
*)s
.ai_addr
));
769 if (s
.ai_addrlen
== sizeof(struct sockaddr_in
)) {
770 operator=(*((struct sockaddr_in
*)s
.ai_addr
));
780 void IpAddress::GetAddrInfo(struct addrinfo
*&dst
, int force
) const
786 memset(dst
, 0, sizeof(struct addrinfo
));
789 dst
->ai_flags
= AI_NUMERICHOST
;
791 if (dst
->ai_socktype
== 0)
792 dst
->ai_socktype
= SOCK_STREAM
;
794 if (dst
->ai_socktype
== SOCK_STREAM
// implies TCP
795 && dst
->ai_protocol
== 0)
796 dst
->ai_protocol
= IPPROTO_TCP
;
798 if (dst
->ai_socktype
== SOCK_DGRAM
// implies UDP
799 && dst
->ai_protocol
== 0)
800 dst
->ai_protocol
= IPPROTO_UDP
;
803 if ( force
== AF_INET6
|| (force
== AF_UNSPEC
&& IsIPv6()) ) {
804 dst
->ai_addr
= (struct sockaddr
*)new sockaddr_in6
;
806 memset(dst
->ai_addr
,0,sizeof(struct sockaddr_in6
));
808 GetSockAddr(*((struct sockaddr_in6
*)dst
->ai_addr
));
810 dst
->ai_addrlen
= sizeof(struct sockaddr_in6
);
812 dst
->ai_family
= ((struct sockaddr_in6
*)dst
->ai_addr
)->sin6_family
;
816 * Enable only if you must and please report to squid-dev if you find a need for this.
818 * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY).
819 * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx
820 * Linux appears to only do some things when its present.
822 * FreeBSD dies horribly when using dual-stack with it set.
823 * (43) Protocol not supported
825 dst
->ai_protocol
= IPPROTO_IPV6
;
830 if ( force
== AF_INET
|| (force
== AF_UNSPEC
&& IsIPv4()) ) {
832 dst
->ai_addr
= (struct sockaddr
*)new sockaddr_in
;
834 memset(dst
->ai_addr
,0,sizeof(struct sockaddr_in
));
836 GetSockAddr(*((struct sockaddr_in
*)dst
->ai_addr
));
838 dst
->ai_addrlen
= sizeof(struct sockaddr_in
);
840 dst
->ai_family
= ((struct sockaddr_in
*)dst
->ai_addr
)->sin_family
;
842 IASSERT("false",false);
846 void IpAddress::InitAddrInfo(struct addrinfo
*&ai
) const
850 memset(ai
,0,sizeof(struct addrinfo
));
853 // remove any existing data.
854 if (ai
->ai_addr
) delete ai
->ai_addr
;
856 ai
->ai_addr
= (struct sockaddr
*)new sockaddr_in6
;
857 memset(ai
->ai_addr
, 0, sizeof(struct sockaddr_in6
));
859 ai
->ai_addrlen
= sizeof(struct sockaddr_in6
);
863 void IpAddress::FreeAddrInfo(struct addrinfo
*&ai
) const
865 if (ai
== NULL
) return;
867 if (ai
->ai_addr
) delete ai
->ai_addr
;
873 // NP: name fields are NOT allocated at present.
879 int IpAddress::matchIPAddr(const IpAddress
&rhs
) const
882 uint8_t *l
= (uint8_t*)m_SocketAddr
.sin6_addr
.s6_addr
;
883 uint8_t *r
= (uint8_t*)rhs
.m_SocketAddr
.sin6_addr
.s6_addr
;
885 uint8_t *l
= (uint8_t*)&m_SocketAddr
.sin_addr
.s_addr
;
886 uint8_t *r
= (uint8_t*)&rhs
.m_SocketAddr
.sin_addr
.s_addr
;
889 // loop a byte-wise compare
890 // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR
891 // expected difference on CIDR is gt/eq or lt/eq ONLY.
892 for (unsigned int i
= 0 ; i
< sizeof(m_SocketAddr
.sin6_addr
) ; i
++) {
904 bool IpAddress::operator ==(const IpAddress
&s
) const
906 return (0 == matchIPAddr(s
));
909 bool IpAddress::operator !=(const IpAddress
&s
) const
911 return ! ( operator==(s
) );
914 bool IpAddress::operator <=(const IpAddress
&rhs
) const
916 if (IsAnyAddr() && !rhs
.IsAnyAddr())
919 return (matchIPAddr(rhs
) <= 0);
922 bool IpAddress::operator >=(const IpAddress
&rhs
) const
924 if (IsNoAddr() && !rhs
.IsNoAddr())
927 return ( matchIPAddr(rhs
) >= 0);
930 bool IpAddress::operator >(const IpAddress
&rhs
) const
932 if (IsNoAddr() && !rhs
.IsNoAddr())
935 return ( matchIPAddr(rhs
) > 0);
938 bool IpAddress::operator <(const IpAddress
&rhs
) const
940 if (IsNoAddr() && !rhs
.IsNoAddr())
943 return ( matchIPAddr(rhs
) < 0);
946 u_short
IpAddress::GetPort() const
948 return ntohs( m_SocketAddr
.sin6_port
);
951 u_short
IpAddress::SetPort(u_short prt
)
953 m_SocketAddr
.sin6_port
= htons(prt
);
959 * NtoA Given a buffer writes a readable ascii version of the IPA and/or port stored
961 * Buffer must be of a size large enough to hold the converted address.
962 * This size is provided in the form of a global defined variable MAX_IPSTRLEN
963 * Should a buffer shorter be provided the string result will be truncated
964 * at the length of the available buffer.
966 * A copy of the buffer is also returned for simple immediate display.
968 char* IpAddress::NtoA(char* buf
, const unsigned int blen
, int force
) const
970 // Ensure we have a buffer.
975 /* some external code may have blindly memset a parent. */
976 /* thats okay, our default is known */
979 memcpy(buf
,"::\0", min((const unsigned int)3,blen
));
981 memcpy(buf
,"0.0.0.0\0", min((const unsigned int)8,blen
));
986 memset(buf
,0,blen
); // clear buffer before write
988 /* Pure-IPv6 CANNOT be displayed in IPv4 format. */
989 /* However IPv4 CAN. */
990 if ( force
== AF_INET
&& !IsIPv4() ) {
992 memcpy(buf
, "{!IPv4}\0", min((const unsigned int)8,blen
));
998 if ( force
== AF_INET6
|| (force
== AF_UNSPEC
&& IsIPv6()) ) {
1000 xinet_ntop(AF_INET6
, &m_SocketAddr
.sin6_addr
, buf
, blen
);
1002 } else if ( force
== AF_INET
|| (force
== AF_UNSPEC
&& IsIPv4()) ) {
1006 xinet_ntop(AF_INET
, &tmp
, buf
, blen
);
1008 if ( force
== AF_UNSPEC
|| (force
== AF_INET
&& IsIPv4()) ) {
1009 xinet_ntop(AF_INET
, &m_SocketAddr
.sin_addr
, buf
, blen
);
1012 debugs(14,0,"WARNING: Corrupt IP Address details OR required to display in unknown format (" <<
1013 force
<< "). accepted={" << AF_UNSPEC
<< "," << AF_INET
<< "," << AF_INET6
<< "}");
1014 fprintf(stderr
,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ",
1015 force
, AF_UNSPEC
, AF_INET
, AF_INET6
);
1016 memcpy(buf
,"dead:beef::\0", min((const unsigned int)13,blen
));
1023 unsigned int IpAddress::ToHostname(char *buf
, const unsigned int blen
) const
1027 if (IsIPv6() && blen
> 0) {
1032 /* 7 being space for [,], and port */
1034 NtoA(p
, blen
-7, AF_INET6
);
1036 NtoA(p
, blen
-7, AF_INET
);
1038 // find the end of the new string
1039 while (*p
!= '\0' && p
< buf
+blen
)
1042 if (IsIPv6() && p
< (buf
+blen
-1) ) {
1047 /* terminate just in case. */
1050 /* return size of buffer now used */
1054 char* IpAddress::ToURL(char* buf
, unsigned int blen
) const
1058 // Ensure we have a buffer.
1064 p
+= ToHostname(p
, blen
);
1066 if (m_SocketAddr
.sin6_port
> 0 && p
< (buf
+blen
-6) ) {
1067 /* 6 is max length of expected ':port' (short int) */
1068 snprintf(p
, 6,":%d", GetPort() );
1071 // force a null-terminated string
1077 void IpAddress::GetSockAddr(struct sockaddr_storage
&addr
, const int family
) const
1079 struct sockaddr_in
*sin
= NULL
;
1081 if ( family
== AF_INET
&& !IsIPv4()) {
1082 // FIXME INET6: caller using the wrong socket type!
1083 debugs(14, DBG_CRITICAL
, HERE
<< "IpAddress::GetSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this);
1088 if ( family
== AF_INET6
|| (family
== AF_UNSPEC
&& IsIPv6()) ) {
1089 struct sockaddr_in6
*ss6
= (struct sockaddr_in6
*)&addr
;
1091 } else if ( family
== AF_INET
|| (family
== AF_UNSPEC
&& IsIPv4()) ) {
1092 sin
= (struct sockaddr_in
*)&addr
;
1095 IASSERT("false",false);
1097 #else /* not USE_IPV6 */
1098 sin
= (struct sockaddr_in
*)&addr
;
1100 #endif /* USE_IPV6 */
1103 void IpAddress::GetSockAddr(struct sockaddr_in
&buf
) const
1108 buf
.sin_family
= AF_INET
;
1109 buf
.sin_port
= m_SocketAddr
.sin6_port
;
1110 Map6to4( m_SocketAddr
.sin6_addr
, buf
.sin_addr
);
1112 debugs(14, DBG_CRITICAL
, HERE
<< "IpAddress::GetSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this );
1114 memset(&buf
,0xFFFFFFFF,sizeof(struct sockaddr_in
));
1120 memcpy(&buf
, &m_SocketAddr
, sizeof(struct sockaddr_in
));
1122 if (buf
.sin_family
== 0) {
1123 buf
.sin_family
= AF_INET
;
1128 #if HAVE_SIN_LEN_IN_SAI
1129 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
1130 buf
.sin_len
= sizeof(struct sockaddr_in
);
1137 void IpAddress::GetSockAddr(struct sockaddr_in6
&buf
) const
1139 memcpy(&buf
, &m_SocketAddr
, sizeof(struct sockaddr_in6
));
1140 /* maintain address family. It may have changed inside us. */
1141 buf
.sin6_family
= AF_INET6
;
1143 #if HAVE_SIN6_LEN_IN_SAI
1144 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
1145 buf
.sin6_len
= sizeof(struct sockaddr_in6
);
1153 void IpAddress::Map4to6(const struct in_addr
&in
, struct in6_addr
&out
) const
1155 /* check for special cases */
1157 if ( in
.s_addr
== 0x00000000) {
1160 memset(&out
, 0, sizeof(struct in6_addr
));
1161 } else if ( in
.s_addr
== 0xFFFFFFFF) {
1164 out
.s6_addr32
[0] = 0xFFFFFFFF;
1165 out
.s6_addr32
[1] = 0xFFFFFFFF;
1166 out
.s6_addr32
[2] = 0xFFFFFFFF;
1167 out
.s6_addr32
[3] = 0xFFFFFFFF;
1172 memset(&out
, 0, sizeof(struct in6_addr
));
1173 out
.s6_addr32
[2] = htonl(0xFFFF);
1174 out
.s6_addr32
[3] = in
.s_addr
;
1178 void IpAddress::Map6to4(const struct in6_addr
&in
, struct in_addr
&out
) const
1184 memset(&out
, 0, sizeof(struct in_addr
));
1185 out
.s_addr
= in
.s6_addr32
[3];
1191 void IpAddress::GetInAddr(in6_addr
&buf
) const
1193 memcpy(&buf
, &m_SocketAddr
.sin6_addr
, sizeof(struct in6_addr
));
1198 bool IpAddress::GetInAddr(struct in_addr
&buf
) const
1203 Map6to4((const in6_addr
)m_SocketAddr
.sin6_addr
, buf
);
1209 memcpy(&buf
, &m_SocketAddr
.sin_addr
, sizeof(struct in_addr
));
1215 // non-compatible IPv6 Pure Address
1217 debugs(14,1, HERE
<< "IpAddress::GetInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this);
1218 memset(&buf
,0xFFFFFFFF,sizeof(struct in_addr
));