]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ip/Address.cc
2 * DEBUG: section 14 IP Storage and Handling
3 * AUTHOR: Amos Jeffries
4 * COPYRIGHT: GPL version 2, (C)2007-2013 Treehouse Networks Ltd.
7 #include "compat/inet_ntop.h"
8 #include "compat/getaddrinfo.h"
10 #include "ip/Address.h"
22 #include <arpa/inet.h>
25 /* Debugging only. Dump the address content when a fatal assert is encountered. */
26 #define IASSERT(a,b) \
27 if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \
28 printf("Ip::Address invalid? with isIPv4()=%c, isIPv6()=%c\n",(isIPv4()?'T':'F'),(isIPv6()?'T':'F')); \
30 for(unsigned int i = 0; i < sizeof(mSocketAddr_.sin6_addr); ++i) { \
31 printf(" %x", mSocketAddr_.sin6_addr.s6_addr[i]); \
32 } printf("\n"); assert(b); \
36 Ip::Address::cidr() const
41 const uint8_t *ptr
= mSocketAddr_
.sin6_addr
.s6_addr
;
43 /* Let's scan all the bits from Most Significant to Least */
44 /* Until we find an "0" bit. Then, we return */
47 /* return IPv4 CIDR for any Mapped address */
48 /* Thus only check the mapped bit */
54 for (; shift
<sizeof(mSocketAddr_
.sin6_addr
) ; ++shift
) {
59 continue ; /* A short-cut */
62 for (caught
= 0 , bit
= 7 ; !caught
&& (bit
<= 7); --bit
) {
63 caught
= ((byte
& 0x80) == 0x00); /* Found a '0' at 'bit' ? */
72 break; /* We have found the most significant "0" bit. */
79 Ip::Address::applyMask(Ip::Address
const &mask_addr
)
81 uint32_t *p1
= (uint32_t*)(&mSocketAddr_
.sin6_addr
);
82 uint32_t const *p2
= (uint32_t const *)(&mask_addr
.mSocketAddr_
.sin6_addr
);
83 unsigned int blen
= sizeof(mSocketAddr_
.sin6_addr
)/sizeof(uint32_t);
84 unsigned int changes
= 0;
86 for (unsigned int i
= 0; i
< blen
; ++i
) {
87 if ((p1
[i
] & p2
[i
]) != p1
[i
])
97 Ip::Address::applyMask(const unsigned int cidrMask
, int mtype
)
99 uint8_t clearbits
= 0;
102 // validation and short-cuts.
106 if (cidrMask
> 32 && mtype
== AF_INET
)
110 /* CIDR /0 is NoAddr regardless of the IPv4/IPv6 protocol */
115 clearbits
= (uint8_t)( (mtype
==AF_INET6
?128:32) - cidrMask
);
121 p
= (uint8_t*)(&mSocketAddr_
.sin6_addr
) + 15;
123 for (; clearbits
>0 && p
>= (uint8_t*)&mSocketAddr_
.sin6_addr
; --p
) {
125 *p
&= ((0xFF << clearbits
) & 0xFF);
137 Ip::Address::isSockAddr() const
139 return (mSocketAddr_
.sin6_port
!= 0);
143 Ip::Address::isIPv4() const
145 return IN6_IS_ADDR_V4MAPPED( &mSocketAddr_
.sin6_addr
);
149 Ip::Address::isIPv6() const
155 Ip::Address::isAnyAddr() const
157 return IN6_IS_ADDR_UNSPECIFIED(&mSocketAddr_
.sin6_addr
) || IN6_ARE_ADDR_EQUAL(&mSocketAddr_
.sin6_addr
, &v4_anyaddr
);
160 /// NOTE: Does NOT clear the Port stored. Ony the Address and Type.
162 Ip::Address::setAnyAddr()
164 memset(&mSocketAddr_
.sin6_addr
, 0, sizeof(struct in6_addr
) );
167 /// NOTE: completely empties the Ip::Address structure. Address, Port, Type, everything.
169 Ip::Address::setEmpty()
171 memset(&mSocketAddr_
, 0, sizeof(mSocketAddr_
) );
175 // Bug 2885 comment 78 explains.
176 // In short AIX has a different netinet/in.h union definition
177 const struct in6_addr
Ip::Address::v4_localhost
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x7f000001 }}};
178 const struct in6_addr
Ip::Address::v4_anyaddr
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x00000000 }}};
179 const struct in6_addr
Ip::Address::v4_noaddr
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0xffffffff }}};
180 const struct in6_addr
Ip::Address::v6_noaddr
= {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}};
182 const struct in6_addr
Ip::Address::v4_localhost
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
183 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 }}
185 const struct in6_addr
Ip::Address::v4_anyaddr
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
186 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }}
188 const struct in6_addr
Ip::Address::v4_noaddr
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
191 const struct in6_addr
Ip::Address::v6_noaddr
= {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
192 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
197 Ip::Address::setIPv4()
199 if ( isLocalhost() ) {
200 mSocketAddr_
.sin6_addr
= v4_localhost
;
205 mSocketAddr_
.sin6_addr
= v4_anyaddr
;
210 mSocketAddr_
.sin6_addr
= v4_noaddr
;
217 // anything non-IPv4 and non-convertable is BAD.
222 Ip::Address::isLocalhost() const
224 return IN6_IS_ADDR_LOOPBACK( &mSocketAddr_
.sin6_addr
) || IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v4_localhost
);
228 Ip::Address::setLocalhost()
230 if (Ip::EnableIpv6
) {
231 mSocketAddr_
.sin6_addr
= in6addr_loopback
;
232 mSocketAddr_
.sin6_family
= AF_INET6
;
234 mSocketAddr_
.sin6_addr
= v4_localhost
;
235 mSocketAddr_
.sin6_family
= AF_INET
;
240 Ip::Address::isSiteLocal6() const
242 // RFC 4193 the site-local allocated range is fc00::/7
243 // with fd00::/8 as the only currently allocated range (so we test it first).
244 // BUG: as of 2010-02 Linux and BSD define IN6_IS_ADDR_SITELOCAL() to check for fec::/10
245 return mSocketAddr_
.sin6_addr
.s6_addr
[0] == static_cast<uint8_t>(0xfd) ||
246 mSocketAddr_
.sin6_addr
.s6_addr
[0] == static_cast<uint8_t>(0xfc);
250 Ip::Address::isSiteLocalAuto() const
252 return mSocketAddr_
.sin6_addr
.s6_addr
[10] == static_cast<uint8_t>(0xff) &&
253 mSocketAddr_
.sin6_addr
.s6_addr
[11] == static_cast<uint8_t>(0xfe);
257 Ip::Address::isNoAddr() const
259 // IFF the address == 0xff..ff (all ones)
260 return IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v6_noaddr
)
261 || IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v4_noaddr
);
265 Ip::Address::setNoAddr()
267 memset(&mSocketAddr_
.sin6_addr
, 0xFF, sizeof(struct in6_addr
) );
268 mSocketAddr_
.sin6_family
= AF_INET6
;
272 Ip::Address::getReverseString6(char buf
[MAX_IPSTRLEN
], const struct in6_addr
&dat
) const
275 unsigned char const *r
= dat
.s6_addr
;
278 /* 4321:0:1:2:3:4:567:89ab */
280 /* 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. */
282 /* Work from the binary field. Anything else may have representation changes. */
283 /* The sin6_port and sin6_addr members shall be in network byte order. */
285 /* Compile Err: 'Too many arguments for format. */
287 for (int i
= 15; i
>= 0; --i
, p
+=4) {
288 snprintf(p
, 5, "%x.%x.", ((r
[i
])&0xf), (((r
[i
])>>4)&0xf) );
292 /* ip6.int is now deprecated TLD, use ip6.arpa instead. */
293 snprintf(p
,10,"ip6.arpa.");
299 Ip::Address::getReverseString4(char buf
[MAX_IPSTRLEN
], const struct in_addr
&dat
) const
301 unsigned int i
= (unsigned int) ntohl(dat
.s_addr
);
302 snprintf(buf
, 32, "%u.%u.%u.%u.in-addr.arpa.",
311 Ip::Address::getReverseString(char buf
[MAX_IPSTRLEN
], int show_type
) const
314 if (show_type
== AF_UNSPEC
) {
315 show_type
= isIPv6() ? AF_INET6
: AF_INET
;
318 if (show_type
== AF_INET
&& isIPv4()) {
319 struct in_addr
* tmp
= (struct in_addr
*)&mSocketAddr_
.sin6_addr
.s6_addr
[12];
320 return getReverseString4(buf
, *tmp
);
321 } else if ( show_type
== AF_INET6
&& isIPv6() ) {
322 return getReverseString6(buf
, mSocketAddr_
.sin6_addr
);
325 debugs(14, DBG_CRITICAL
, "Unable to convert '" << toStr(buf
,MAX_IPSTRLEN
) << "' to the rDNS type requested.");
333 Ip::Address::operator =(const Ip::Address
&s
)
335 memcpy(this, &s
, sizeof(Ip::Address
));
339 Ip::Address::Address(const char*s
)
342 lookupHostIP(s
, true);
346 Ip::Address::operator =(const char* s
)
348 return lookupHostIP(s
, true);
352 Ip::Address::GetHostByName(const char* s
)
354 return lookupHostIP(s
, false);
358 Ip::Address::lookupHostIP(const char *s
, bool nodns
)
360 struct addrinfo want
;
361 memset(&want
, 0, sizeof(struct addrinfo
));
363 want
.ai_flags
= AI_NUMERICHOST
; // prevent actual DNS lookups!
367 struct addrinfo
*res
= NULL
;
368 if ( (err
= getaddrinfo(s
, NULL
, &want
, &res
)) != 0) {
369 debugs(14,3, HERE
<< "Given Non-IP '" << s
<< "': " << gai_strerror(err
) );
370 /* free the memory getaddrinfo() dynamically allocated. */
377 * NP: =(sockaddr_*) may alter the port. we don't want that.
378 * all we have been given as input was an IPA.
380 short portSaved
= port();
384 /* free the memory getaddrinfo() dynamically allocated. */
389 Ip::Address::Address(struct sockaddr_in
const &s
)
396 Ip::Address::operator =(struct sockaddr_in
const &s
)
398 map4to6((const in_addr
)s
.sin_addr
, mSocketAddr_
.sin6_addr
);
399 mSocketAddr_
.sin6_port
= s
.sin_port
;
400 mSocketAddr_
.sin6_family
= AF_INET6
;
405 Ip::Address::operator =(const struct sockaddr_storage
&s
)
407 /* some AF_* magic to tell socket types apart and what we need to do */
408 if (s
.ss_family
== AF_INET6
) {
409 memcpy(&mSocketAddr_
, &s
, sizeof(struct sockaddr_in6
));
410 } else { // convert it to our storage mapping.
411 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&s
;
412 mSocketAddr_
.sin6_port
= sin
->sin_port
;
413 map4to6( sin
->sin_addr
, mSocketAddr_
.sin6_addr
);
418 Ip::Address::Address(struct sockaddr_in6
const &s
)
425 Ip::Address::operator =(struct sockaddr_in6
const &s
)
427 memcpy(&mSocketAddr_
, &s
, sizeof(struct sockaddr_in6
));
432 Ip::Address::Address(struct in_addr
const &s
)
439 Ip::Address::operator =(struct in_addr
const &s
)
441 map4to6((const in_addr
)s
, mSocketAddr_
.sin6_addr
);
442 mSocketAddr_
.sin6_family
= AF_INET6
;
446 Ip::Address::Address(struct in6_addr
const &s
)
453 Ip::Address::operator =(struct in6_addr
const &s
)
456 memcpy(&mSocketAddr_
.sin6_addr
, &s
, sizeof(struct in6_addr
));
457 mSocketAddr_
.sin6_family
= AF_INET6
;
462 Ip::Address::Address(const Ip::Address
&s
)
468 Ip::Address::Address(const struct hostent
&s
)
475 Ip::Address::operator =(const struct hostent
&s
)
478 struct in_addr
* ipv4
= NULL
;
480 struct in6_addr
* ipv6
= NULL
;
483 // char *h_name; /* official name of host */
484 // char **h_aliases; /* alias list */
485 // int h_addrtype; /* host address type */
486 // int h_length; /* length of address */
487 // char **h_addr_list; /* list of addresses */
490 switch (s
.h_addrtype
) {
493 ipv4
= (in_addr
*)(s
.h_addr_list
[0]);
499 ipv6
= (in6_addr
*)(s
.h_addr_list
[0]);
505 IASSERT("false",false);
512 Ip::Address::Address(const struct addrinfo
&s
)
519 Ip::Address::operator =(const struct addrinfo
&s
)
522 struct sockaddr_in
* ipv4
= NULL
;
524 struct sockaddr_in6
* ipv6
= NULL
;
527 // int ai_flags; /* input flags */
528 // int ai_family; /* protocol family for socket */
529 // int ai_socktype; /* socket type */
530 // int ai_protocol; /* protocol for socket */
531 // socklen_t ai_addrlen; /* length of socket-address */
532 // struct sockaddr *ai_addr; /* socket-address for socket */
533 // char *ai_canonname; /* canonical name for service location */
534 // struct addrinfo *ai_next; /* pointer to next in list */
537 switch (s
.ai_family
) {
540 ipv4
= (sockaddr_in
*)(s
.ai_addr
);
547 ipv6
= (sockaddr_in6
*)(s
.ai_addr
);
555 // attempt to handle partially initialised addrinfo.
556 // such as those where data only comes from getsockopt()
557 if (s
.ai_addr
!= NULL
) {
558 if (s
.ai_addrlen
== sizeof(struct sockaddr_in6
)) {
559 operator=(*((struct sockaddr_in6
*)s
.ai_addr
));
561 } else if (s
.ai_addrlen
== sizeof(struct sockaddr_in
)) {
562 operator=(*((struct sockaddr_in
*)s
.ai_addr
));
573 Ip::Address::getAddrInfo(struct addrinfo
*&dst
, int force
) const
579 memset(dst
, 0, sizeof(struct addrinfo
));
582 // Mac OS X does not emit a flag indicating the output is numeric (IP address)
586 dst
->ai_flags
= AI_NUMERICHOST
;
589 if (dst
->ai_socktype
== 0)
590 dst
->ai_socktype
= SOCK_STREAM
;
592 if (dst
->ai_socktype
== SOCK_STREAM
// implies TCP
593 && dst
->ai_protocol
== 0)
594 dst
->ai_protocol
= IPPROTO_TCP
;
596 if (dst
->ai_socktype
== SOCK_DGRAM
// implies UDP
597 && dst
->ai_protocol
== 0)
598 dst
->ai_protocol
= IPPROTO_UDP
;
600 if (force
== AF_INET6
|| (force
== AF_UNSPEC
&& Ip::EnableIpv6
&& isIPv6()) ) {
601 dst
->ai_addr
= (struct sockaddr
*)new sockaddr_in6
;
603 memset(dst
->ai_addr
,0,sizeof(struct sockaddr_in6
));
605 getSockAddr(*((struct sockaddr_in6
*)dst
->ai_addr
));
607 dst
->ai_addrlen
= sizeof(struct sockaddr_in6
);
609 dst
->ai_family
= ((struct sockaddr_in6
*)dst
->ai_addr
)->sin6_family
;
613 * Enable only if you must and please report to squid-dev if you find a need for this.
615 * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY).
616 * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx
617 * Linux appears to only do some things when its present.
619 * FreeBSD dies horribly when using dual-stack with it set.
620 * (43) Protocol not supported
622 dst
->ai_protocol
= IPPROTO_IPV6
;
625 } else if ( force
== AF_INET
|| (force
== AF_UNSPEC
&& isIPv4()) ) {
627 dst
->ai_addr
= (struct sockaddr
*)new sockaddr_in
;
629 memset(dst
->ai_addr
,0,sizeof(struct sockaddr_in
));
631 getSockAddr(*((struct sockaddr_in
*)dst
->ai_addr
));
633 dst
->ai_addrlen
= sizeof(struct sockaddr_in
);
635 dst
->ai_family
= ((struct sockaddr_in
*)dst
->ai_addr
)->sin_family
;
637 IASSERT("false",false);
642 Ip::Address::InitAddrInfo(struct addrinfo
*&ai
)
646 memset(ai
,0,sizeof(struct addrinfo
));
649 // remove any existing data.
650 if (ai
->ai_addr
) delete ai
->ai_addr
;
652 ai
->ai_addr
= (struct sockaddr
*)new sockaddr_in6
;
653 memset(ai
->ai_addr
, 0, sizeof(struct sockaddr_in6
));
655 ai
->ai_addrlen
= sizeof(struct sockaddr_in6
);
660 Ip::Address::FreeAddrInfo(struct addrinfo
*&ai
)
662 if (ai
== NULL
) return;
664 if (ai
->ai_addr
) delete ai
->ai_addr
;
670 // NP: name fields are NOT allocated at present.
677 Ip::Address::matchIPAddr(const Ip::Address
&rhs
) const
679 uint8_t *l
= (uint8_t*)mSocketAddr_
.sin6_addr
.s6_addr
;
680 uint8_t *r
= (uint8_t*)rhs
.mSocketAddr_
.sin6_addr
.s6_addr
;
682 // loop a byte-wise compare
683 // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR
684 // expected difference on CIDR is gt/eq or lt/eq ONLY.
685 for (unsigned int i
= 0 ; i
< sizeof(mSocketAddr_
.sin6_addr
) ; ++i
) {
698 Ip::Address::compareWhole(const Ip::Address
&rhs
) const
700 return memcmp(this, &rhs
, sizeof(*this));
704 Ip::Address::operator ==(const Ip::Address
&s
) const
706 return (0 == matchIPAddr(s
));
710 Ip::Address::operator !=(const Ip::Address
&s
) const
712 return ! ( operator==(s
) );
716 Ip::Address::operator <=(const Ip::Address
&rhs
) const
718 if (isAnyAddr() && !rhs
.isAnyAddr())
721 return (matchIPAddr(rhs
) <= 0);
725 Ip::Address::operator >=(const Ip::Address
&rhs
) const
727 if (isNoAddr() && !rhs
.isNoAddr())
730 return ( matchIPAddr(rhs
) >= 0);
734 Ip::Address::operator >(const Ip::Address
&rhs
) const
736 if (isNoAddr() && !rhs
.isNoAddr())
739 return ( matchIPAddr(rhs
) > 0);
743 Ip::Address::operator <(const Ip::Address
&rhs
) const
745 if (isAnyAddr() && !rhs
.isAnyAddr())
748 return ( matchIPAddr(rhs
) < 0);
752 Ip::Address::port() const
754 return ntohs( mSocketAddr_
.sin6_port
);
758 Ip::Address::port(unsigned short prt
)
760 mSocketAddr_
.sin6_port
= htons(prt
);
766 * toStr Given a buffer writes a readable ascii version of the IPA and/or port stored
768 * Buffer must be of a size large enough to hold the converted address.
769 * This size is provided in the form of a global defined variable MAX_IPSTRLEN
770 * Should a buffer shorter be provided the string result will be truncated
771 * at the length of the available buffer.
773 * A copy of the buffer is also returned for simple immediate display.
776 Ip::Address::toStr(char* buf
, const unsigned int blen
, int force
) const
778 // Ensure we have a buffer.
783 /* some external code may have blindly memset a parent. */
784 /* thats okay, our default is known */
787 memcpy(buf
,"::\0", min(static_cast<unsigned int>(3),blen
));
789 memcpy(buf
,"0.0.0.0\0", min(static_cast<unsigned int>(8),blen
));
793 memset(buf
,0,blen
); // clear buffer before write
795 /* Pure-IPv6 CANNOT be displayed in IPv4 format. */
796 /* However IPv4 CAN. */
797 if ( force
== AF_INET
&& !isIPv4() ) {
799 memcpy(buf
, "{!IPv4}\0", min(static_cast<unsigned int>(8),blen
));
804 if ( force
== AF_INET6
|| (force
== AF_UNSPEC
&& isIPv6()) ) {
806 inet_ntop(AF_INET6
, &mSocketAddr_
.sin6_addr
, buf
, blen
);
808 } else if ( force
== AF_INET
|| (force
== AF_UNSPEC
&& isIPv4()) ) {
812 inet_ntop(AF_INET
, &tmp
, buf
, blen
);
814 debugs(14, DBG_CRITICAL
, "WARNING: Corrupt IP Address details OR required to display in unknown format (" <<
815 force
<< "). accepted={" << AF_UNSPEC
<< "," << AF_INET
<< "," << AF_INET6
<< "}");
816 fprintf(stderr
,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ",
817 force
, AF_UNSPEC
, AF_INET
, AF_INET6
);
818 memcpy(buf
,"dead:beef::\0", min(static_cast<unsigned int>(13),blen
));
826 Ip::Address::toHostStr(char *buf
, const unsigned int blen
) const
830 if (isIPv6() && blen
> 0) {
835 /* 8 being space for [ ] : and port digits */
837 toStr(p
, blen
-8, AF_INET6
);
839 toStr(p
, blen
-8, AF_INET
);
841 // find the end of the new string
842 while (*p
!= '\0' && p
< buf
+blen
)
845 if (isIPv6() && p
< (buf
+blen
-1) ) {
850 /* terminate just in case. */
853 /* return size of buffer now used */
858 Ip::Address::toUrl(char* buf
, unsigned int blen
) const
862 // Ensure we have a buffer.
868 p
+= toHostStr(p
, blen
);
870 if (mSocketAddr_
.sin6_port
> 0 && p
<= (buf
+blen
-7) ) {
871 // ':port' (short int) needs at most 6 bytes plus 1 for 0-terminator
872 snprintf(p
, 7, ":%d", port() );
875 // force a null-terminated string
882 Ip::Address::getSockAddr(struct sockaddr_storage
&addr
, const int family
) const
884 struct sockaddr_in
*sin
= NULL
;
886 if ( family
== AF_INET
&& !isIPv4()) {
887 // FIXME INET6: caller using the wrong socket type!
888 debugs(14, DBG_CRITICAL
, HERE
<< "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this);
892 if ( family
== AF_INET6
|| (family
== AF_UNSPEC
&& isIPv6()) ) {
893 struct sockaddr_in6
*ss6
= (struct sockaddr_in6
*)&addr
;
895 } else if ( family
== AF_INET
|| (family
== AF_UNSPEC
&& isIPv4()) ) {
896 sin
= (struct sockaddr_in
*)&addr
;
899 IASSERT("false",false);
904 Ip::Address::getSockAddr(struct sockaddr_in
&buf
) const
907 buf
.sin_family
= AF_INET
;
908 buf
.sin_port
= mSocketAddr_
.sin6_port
;
909 map6to4( mSocketAddr_
.sin6_addr
, buf
.sin_addr
);
911 debugs(14, DBG_CRITICAL
, HERE
<< "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this );
913 memset(&buf
,0xFFFFFFFF,sizeof(struct sockaddr_in
));
917 #if HAVE_SIN_LEN_IN_SAI
918 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
919 buf
.sin_len
= sizeof(struct sockaddr_in
);
924 Ip::Address::getSockAddr(struct sockaddr_in6
&buf
) const
926 memcpy(&buf
, &mSocketAddr_
, sizeof(struct sockaddr_in6
));
927 /* maintain address family. It may have changed inside us. */
928 buf
.sin6_family
= AF_INET6
;
930 #if HAVE_SIN6_LEN_IN_SAI
931 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
932 buf
.sin6_len
= sizeof(struct sockaddr_in6
);
937 Ip::Address::map4to6(const struct in_addr
&in
, struct in6_addr
&out
) const
939 /* check for special cases */
941 if ( in
.s_addr
== 0x00000000) {
944 } else if ( in
.s_addr
== 0xFFFFFFFF) {
950 out
.s6_addr
[12] = ((uint8_t *)&in
.s_addr
)[0];
951 out
.s6_addr
[13] = ((uint8_t *)&in
.s_addr
)[1];
952 out
.s6_addr
[14] = ((uint8_t *)&in
.s_addr
)[2];
953 out
.s6_addr
[15] = ((uint8_t *)&in
.s_addr
)[3];
958 Ip::Address::map6to4(const struct in6_addr
&in
, struct in_addr
&out
) const
964 memset(&out
, 0, sizeof(struct in_addr
));
965 ((uint8_t *)&out
.s_addr
)[0] = in
.s6_addr
[12];
966 ((uint8_t *)&out
.s_addr
)[1] = in
.s6_addr
[13];
967 ((uint8_t *)&out
.s_addr
)[2] = in
.s6_addr
[14];
968 ((uint8_t *)&out
.s_addr
)[3] = in
.s6_addr
[15];
972 Ip::Address::getInAddr(struct in6_addr
&buf
) const
974 memcpy(&buf
, &mSocketAddr_
.sin6_addr
, sizeof(struct in6_addr
));
978 Ip::Address::getInAddr(struct in_addr
&buf
) const
981 map6to4(mSocketAddr_
.sin6_addr
, buf
);
986 // non-compatible IPv6 Pure Address
988 debugs(14, DBG_IMPORTANT
, HERE
<< "Ip::Address::getInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this);
989 memset(&buf
,0xFFFFFFFF,sizeof(struct in_addr
));