]>
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/getaddrinfo.h"
8 #include "compat/inet_ntop.h"
10 #include "ip/Address.h"
18 #include <arpa/inet.h>
21 /* Debugging only. Dump the address content when a fatal assert is encountered. */
22 #define IASSERT(a,b) \
23 if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \
24 printf("Ip::Address invalid? with isIPv4()=%c, isIPv6()=%c\n",(isIPv4()?'T':'F'),(isIPv6()?'T':'F')); \
26 for(unsigned int i = 0; i < sizeof(mSocketAddr_.sin6_addr); ++i) { \
27 printf(" %x", mSocketAddr_.sin6_addr.s6_addr[i]); \
28 } printf("\n"); assert(b); \
32 Ip::Address::cidr() const
37 const uint8_t *ptr
= mSocketAddr_
.sin6_addr
.s6_addr
;
39 /* Let's scan all the bits from Most Significant to Least */
40 /* Until we find an "0" bit. Then, we return */
43 /* return IPv4 CIDR for any Mapped address */
44 /* Thus only check the mapped bit */
50 for (; shift
<sizeof(mSocketAddr_
.sin6_addr
) ; ++shift
) {
55 continue ; /* A short-cut */
58 for (caught
= 0 , bit
= 7 ; !caught
&& (bit
<= 7); --bit
) {
59 caught
= ((byte
& 0x80) == 0x00); /* Found a '0' at 'bit' ? */
68 break; /* We have found the most significant "0" bit. */
75 Ip::Address::applyMask(Ip::Address
const &mask_addr
)
77 uint32_t *p1
= (uint32_t*)(&mSocketAddr_
.sin6_addr
);
78 uint32_t const *p2
= (uint32_t const *)(&mask_addr
.mSocketAddr_
.sin6_addr
);
79 unsigned int blen
= sizeof(mSocketAddr_
.sin6_addr
)/sizeof(uint32_t);
80 unsigned int changes
= 0;
82 for (unsigned int i
= 0; i
< blen
; ++i
) {
83 if ((p1
[i
] & p2
[i
]) != p1
[i
])
93 Ip::Address::applyMask(const unsigned int cidrMask
, int mtype
)
95 uint8_t clearbits
= 0;
98 // validation and short-cuts.
102 if (cidrMask
> 32 && mtype
== AF_INET
)
106 /* CIDR /0 is NoAddr regardless of the IPv4/IPv6 protocol */
111 clearbits
= (uint8_t)( (mtype
==AF_INET6
?128:32) - cidrMask
);
117 p
= (uint8_t*)(&mSocketAddr_
.sin6_addr
) + 15;
119 for (; clearbits
>0 && p
>= (uint8_t*)&mSocketAddr_
.sin6_addr
; --p
) {
121 *p
&= ((0xFF << clearbits
) & 0xFF);
133 Ip::Address::isSockAddr() const
135 return (mSocketAddr_
.sin6_port
!= 0);
139 Ip::Address::isIPv4() const
141 return IN6_IS_ADDR_V4MAPPED( &mSocketAddr_
.sin6_addr
);
145 Ip::Address::isIPv6() const
151 Ip::Address::isAnyAddr() const
153 return IN6_IS_ADDR_UNSPECIFIED(&mSocketAddr_
.sin6_addr
) || IN6_ARE_ADDR_EQUAL(&mSocketAddr_
.sin6_addr
, &v4_anyaddr
);
156 /// NOTE: Does NOT clear the Port stored. Ony the Address and Type.
158 Ip::Address::setAnyAddr()
160 memset(&mSocketAddr_
.sin6_addr
, 0, sizeof(struct in6_addr
) );
163 /// NOTE: completely empties the Ip::Address structure. Address, Port, Type, everything.
165 Ip::Address::setEmpty()
167 memset(&mSocketAddr_
, 0, sizeof(mSocketAddr_
) );
171 // Bug 2885 comment 78 explains.
172 // In short AIX has a different netinet/in.h union definition
173 const struct in6_addr
Ip::Address::v4_localhost
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x7f000001 }}};
174 const struct in6_addr
Ip::Address::v4_anyaddr
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x00000000 }}};
175 const struct in6_addr
Ip::Address::v4_noaddr
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0xffffffff }}};
176 const struct in6_addr
Ip::Address::v6_noaddr
= {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}};
178 const struct in6_addr
Ip::Address::v4_localhost
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 }}
181 const struct in6_addr
Ip::Address::v4_anyaddr
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
182 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }}
184 const struct in6_addr
Ip::Address::v4_noaddr
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
185 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
187 const struct in6_addr
Ip::Address::v6_noaddr
= {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
188 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
193 Ip::Address::setIPv4()
195 if ( isLocalhost() ) {
196 mSocketAddr_
.sin6_addr
= v4_localhost
;
201 mSocketAddr_
.sin6_addr
= v4_anyaddr
;
206 mSocketAddr_
.sin6_addr
= v4_noaddr
;
213 // anything non-IPv4 and non-convertable is BAD.
218 Ip::Address::isLocalhost() const
220 return IN6_IS_ADDR_LOOPBACK( &mSocketAddr_
.sin6_addr
) || IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v4_localhost
);
224 Ip::Address::setLocalhost()
226 if (Ip::EnableIpv6
) {
227 mSocketAddr_
.sin6_addr
= in6addr_loopback
;
228 mSocketAddr_
.sin6_family
= AF_INET6
;
230 mSocketAddr_
.sin6_addr
= v4_localhost
;
231 mSocketAddr_
.sin6_family
= AF_INET
;
236 Ip::Address::isSiteLocal6() const
238 // RFC 4193 the site-local allocated range is fc00::/7
239 // with fd00::/8 as the only currently allocated range (so we test it first).
240 // BUG: as of 2010-02 Linux and BSD define IN6_IS_ADDR_SITELOCAL() to check for fec::/10
241 return mSocketAddr_
.sin6_addr
.s6_addr
[0] == static_cast<uint8_t>(0xfd) ||
242 mSocketAddr_
.sin6_addr
.s6_addr
[0] == static_cast<uint8_t>(0xfc);
246 Ip::Address::isSiteLocalAuto() const
248 return mSocketAddr_
.sin6_addr
.s6_addr
[10] == static_cast<uint8_t>(0xff) &&
249 mSocketAddr_
.sin6_addr
.s6_addr
[11] == static_cast<uint8_t>(0xfe);
253 Ip::Address::isNoAddr() const
255 // IFF the address == 0xff..ff (all ones)
256 return IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v6_noaddr
)
257 || IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v4_noaddr
);
261 Ip::Address::setNoAddr()
263 memset(&mSocketAddr_
.sin6_addr
, 0xFF, sizeof(struct in6_addr
) );
264 mSocketAddr_
.sin6_family
= AF_INET6
;
268 Ip::Address::getReverseString6(char buf
[MAX_IPSTRLEN
], const struct in6_addr
&dat
) const
271 unsigned char const *r
= dat
.s6_addr
;
274 /* 4321:0:1:2:3:4:567:89ab */
276 /* 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. */
278 /* Work from the binary field. Anything else may have representation changes. */
279 /* The sin6_port and sin6_addr members shall be in network byte order. */
281 /* Compile Err: 'Too many arguments for format. */
283 for (int i
= 15; i
>= 0; --i
, p
+=4) {
284 snprintf(p
, 5, "%x.%x.", ((r
[i
])&0xf), (((r
[i
])>>4)&0xf) );
288 /* ip6.int is now deprecated TLD, use ip6.arpa instead. */
289 snprintf(p
,10,"ip6.arpa.");
295 Ip::Address::getReverseString4(char buf
[MAX_IPSTRLEN
], const struct in_addr
&dat
) const
297 unsigned int i
= (unsigned int) ntohl(dat
.s_addr
);
298 snprintf(buf
, 32, "%u.%u.%u.%u.in-addr.arpa.",
307 Ip::Address::getReverseString(char buf
[MAX_IPSTRLEN
], int show_type
) const
310 if (show_type
== AF_UNSPEC
) {
311 show_type
= isIPv6() ? AF_INET6
: AF_INET
;
314 if (show_type
== AF_INET
&& isIPv4()) {
315 struct in_addr
* tmp
= (struct in_addr
*)&mSocketAddr_
.sin6_addr
.s6_addr
[12];
316 return getReverseString4(buf
, *tmp
);
317 } else if ( show_type
== AF_INET6
&& isIPv6() ) {
318 return getReverseString6(buf
, mSocketAddr_
.sin6_addr
);
321 debugs(14, DBG_CRITICAL
, "Unable to convert '" << toStr(buf
,MAX_IPSTRLEN
) << "' to the rDNS type requested.");
329 Ip::Address::operator =(const Ip::Address
&s
)
331 memcpy(this, &s
, sizeof(Ip::Address
));
335 Ip::Address::Address(const char*s
)
338 lookupHostIP(s
, true);
342 Ip::Address::operator =(const char* s
)
344 return lookupHostIP(s
, true);
348 Ip::Address::GetHostByName(const char* s
)
350 return lookupHostIP(s
, false);
354 Ip::Address::lookupHostIP(const char *s
, bool nodns
)
356 struct addrinfo want
;
357 memset(&want
, 0, sizeof(struct addrinfo
));
359 want
.ai_flags
= AI_NUMERICHOST
; // prevent actual DNS lookups!
363 struct addrinfo
*res
= NULL
;
364 if ( (err
= getaddrinfo(s
, NULL
, &want
, &res
)) != 0) {
365 debugs(14,3, HERE
<< "Given Non-IP '" << s
<< "': " << gai_strerror(err
) );
366 /* free the memory getaddrinfo() dynamically allocated. */
373 * NP: =(sockaddr_*) may alter the port. we don't want that.
374 * all we have been given as input was an IPA.
376 short portSaved
= port();
380 /* free the memory getaddrinfo() dynamically allocated. */
385 Ip::Address::Address(struct sockaddr_in
const &s
)
392 Ip::Address::operator =(struct sockaddr_in
const &s
)
394 map4to6((const in_addr
)s
.sin_addr
, mSocketAddr_
.sin6_addr
);
395 mSocketAddr_
.sin6_port
= s
.sin_port
;
396 mSocketAddr_
.sin6_family
= AF_INET6
;
401 Ip::Address::operator =(const struct sockaddr_storage
&s
)
403 /* some AF_* magic to tell socket types apart and what we need to do */
404 if (s
.ss_family
== AF_INET6
) {
405 memcpy(&mSocketAddr_
, &s
, sizeof(struct sockaddr_in6
));
406 } else { // convert it to our storage mapping.
407 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&s
;
408 mSocketAddr_
.sin6_port
= sin
->sin_port
;
409 map4to6( sin
->sin_addr
, mSocketAddr_
.sin6_addr
);
414 Ip::Address::Address(struct sockaddr_in6
const &s
)
421 Ip::Address::operator =(struct sockaddr_in6
const &s
)
423 memcpy(&mSocketAddr_
, &s
, sizeof(struct sockaddr_in6
));
428 Ip::Address::Address(struct in_addr
const &s
)
435 Ip::Address::operator =(struct in_addr
const &s
)
437 map4to6((const in_addr
)s
, mSocketAddr_
.sin6_addr
);
438 mSocketAddr_
.sin6_family
= AF_INET6
;
442 Ip::Address::Address(struct in6_addr
const &s
)
449 Ip::Address::operator =(struct in6_addr
const &s
)
452 memcpy(&mSocketAddr_
.sin6_addr
, &s
, sizeof(struct in6_addr
));
453 mSocketAddr_
.sin6_family
= AF_INET6
;
458 Ip::Address::Address(const Ip::Address
&s
)
464 Ip::Address::Address(const struct hostent
&s
)
471 Ip::Address::operator =(const struct hostent
&s
)
474 struct in_addr
* ipv4
= NULL
;
476 struct in6_addr
* ipv6
= NULL
;
479 // char *h_name; /* official name of host */
480 // char **h_aliases; /* alias list */
481 // int h_addrtype; /* host address type */
482 // int h_length; /* length of address */
483 // char **h_addr_list; /* list of addresses */
486 switch (s
.h_addrtype
) {
489 ipv4
= (in_addr
*)(s
.h_addr_list
[0]);
495 ipv6
= (in6_addr
*)(s
.h_addr_list
[0]);
501 IASSERT("false",false);
508 Ip::Address::Address(const struct addrinfo
&s
)
515 Ip::Address::operator =(const struct addrinfo
&s
)
518 struct sockaddr_in
* ipv4
= NULL
;
520 struct sockaddr_in6
* ipv6
= NULL
;
523 // int ai_flags; /* input flags */
524 // int ai_family; /* protocol family for socket */
525 // int ai_socktype; /* socket type */
526 // int ai_protocol; /* protocol for socket */
527 // socklen_t ai_addrlen; /* length of socket-address */
528 // struct sockaddr *ai_addr; /* socket-address for socket */
529 // char *ai_canonname; /* canonical name for service location */
530 // struct addrinfo *ai_next; /* pointer to next in list */
533 switch (s
.ai_family
) {
536 ipv4
= (sockaddr_in
*)(s
.ai_addr
);
543 ipv6
= (sockaddr_in6
*)(s
.ai_addr
);
551 // attempt to handle partially initialised addrinfo.
552 // such as those where data only comes from getsockopt()
553 if (s
.ai_addr
!= NULL
) {
554 if (s
.ai_addrlen
== sizeof(struct sockaddr_in6
)) {
555 operator=(*((struct sockaddr_in6
*)s
.ai_addr
));
557 } else if (s
.ai_addrlen
== sizeof(struct sockaddr_in
)) {
558 operator=(*((struct sockaddr_in
*)s
.ai_addr
));
569 Ip::Address::getAddrInfo(struct addrinfo
*&dst
, int force
) const
575 memset(dst
, 0, sizeof(struct addrinfo
));
578 // Mac OS X does not emit a flag indicating the output is numeric (IP address)
582 dst
->ai_flags
= AI_NUMERICHOST
;
585 if (dst
->ai_socktype
== 0)
586 dst
->ai_socktype
= SOCK_STREAM
;
588 if (dst
->ai_socktype
== SOCK_STREAM
// implies TCP
589 && dst
->ai_protocol
== 0)
590 dst
->ai_protocol
= IPPROTO_TCP
;
592 if (dst
->ai_socktype
== SOCK_DGRAM
// implies UDP
593 && dst
->ai_protocol
== 0)
594 dst
->ai_protocol
= IPPROTO_UDP
;
596 if (force
== AF_INET6
|| (force
== AF_UNSPEC
&& Ip::EnableIpv6
&& isIPv6()) ) {
597 dst
->ai_addr
= (struct sockaddr
*)new sockaddr_in6
;
599 memset(dst
->ai_addr
,0,sizeof(struct sockaddr_in6
));
601 getSockAddr(*((struct sockaddr_in6
*)dst
->ai_addr
));
603 dst
->ai_addrlen
= sizeof(struct sockaddr_in6
);
605 dst
->ai_family
= ((struct sockaddr_in6
*)dst
->ai_addr
)->sin6_family
;
609 * Enable only if you must and please report to squid-dev if you find a need for this.
611 * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY).
612 * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx
613 * Linux appears to only do some things when its present.
615 * FreeBSD dies horribly when using dual-stack with it set.
616 * (43) Protocol not supported
618 dst
->ai_protocol
= IPPROTO_IPV6
;
621 } else if ( force
== AF_INET
|| (force
== AF_UNSPEC
&& isIPv4()) ) {
623 dst
->ai_addr
= (struct sockaddr
*)new sockaddr_in
;
625 memset(dst
->ai_addr
,0,sizeof(struct sockaddr_in
));
627 getSockAddr(*((struct sockaddr_in
*)dst
->ai_addr
));
629 dst
->ai_addrlen
= sizeof(struct sockaddr_in
);
631 dst
->ai_family
= ((struct sockaddr_in
*)dst
->ai_addr
)->sin_family
;
633 IASSERT("false",false);
638 Ip::Address::InitAddrInfo(struct addrinfo
*&ai
)
642 memset(ai
,0,sizeof(struct addrinfo
));
645 // remove any existing data.
646 if (ai
->ai_addr
) delete ai
->ai_addr
;
648 ai
->ai_addr
= (struct sockaddr
*)new sockaddr_in6
;
649 memset(ai
->ai_addr
, 0, sizeof(struct sockaddr_in6
));
651 ai
->ai_addrlen
= sizeof(struct sockaddr_in6
);
656 Ip::Address::FreeAddrInfo(struct addrinfo
*&ai
)
658 if (ai
== NULL
) return;
660 if (ai
->ai_addr
) delete ai
->ai_addr
;
666 // NP: name fields are NOT allocated at present.
673 Ip::Address::matchIPAddr(const Ip::Address
&rhs
) const
675 uint8_t *l
= (uint8_t*)mSocketAddr_
.sin6_addr
.s6_addr
;
676 uint8_t *r
= (uint8_t*)rhs
.mSocketAddr_
.sin6_addr
.s6_addr
;
678 // loop a byte-wise compare
679 // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR
680 // expected difference on CIDR is gt/eq or lt/eq ONLY.
681 for (unsigned int i
= 0 ; i
< sizeof(mSocketAddr_
.sin6_addr
) ; ++i
) {
694 Ip::Address::compareWhole(const Ip::Address
&rhs
) const
696 return memcmp(this, &rhs
, sizeof(*this));
700 Ip::Address::operator ==(const Ip::Address
&s
) const
702 return (0 == matchIPAddr(s
));
706 Ip::Address::operator !=(const Ip::Address
&s
) const
708 return ! ( operator==(s
) );
712 Ip::Address::operator <=(const Ip::Address
&rhs
) const
714 if (isAnyAddr() && !rhs
.isAnyAddr())
717 return (matchIPAddr(rhs
) <= 0);
721 Ip::Address::operator >=(const Ip::Address
&rhs
) const
723 if (isNoAddr() && !rhs
.isNoAddr())
726 return ( matchIPAddr(rhs
) >= 0);
730 Ip::Address::operator >(const Ip::Address
&rhs
) const
732 if (isNoAddr() && !rhs
.isNoAddr())
735 return ( matchIPAddr(rhs
) > 0);
739 Ip::Address::operator <(const Ip::Address
&rhs
) const
741 if (isAnyAddr() && !rhs
.isAnyAddr())
744 return ( matchIPAddr(rhs
) < 0);
748 Ip::Address::port() const
750 return ntohs( mSocketAddr_
.sin6_port
);
754 Ip::Address::port(unsigned short prt
)
756 mSocketAddr_
.sin6_port
= htons(prt
);
762 * toStr Given a buffer writes a readable ascii version of the IPA and/or port stored
764 * Buffer must be of a size large enough to hold the converted address.
765 * This size is provided in the form of a global defined variable MAX_IPSTRLEN
766 * Should a buffer shorter be provided the string result will be truncated
767 * at the length of the available buffer.
769 * A copy of the buffer is also returned for simple immediate display.
772 Ip::Address::toStr(char* buf
, const unsigned int blen
, int force
) const
774 // Ensure we have a buffer.
779 /* some external code may have blindly memset a parent. */
780 /* thats okay, our default is known */
783 memcpy(buf
,"::\0", min(static_cast<unsigned int>(3),blen
));
785 memcpy(buf
,"0.0.0.0\0", min(static_cast<unsigned int>(8),blen
));
789 memset(buf
,0,blen
); // clear buffer before write
791 /* Pure-IPv6 CANNOT be displayed in IPv4 format. */
792 /* However IPv4 CAN. */
793 if ( force
== AF_INET
&& !isIPv4() ) {
795 memcpy(buf
, "{!IPv4}\0", min(static_cast<unsigned int>(8),blen
));
800 if ( force
== AF_INET6
|| (force
== AF_UNSPEC
&& isIPv6()) ) {
802 inet_ntop(AF_INET6
, &mSocketAddr_
.sin6_addr
, buf
, blen
);
804 } else if ( force
== AF_INET
|| (force
== AF_UNSPEC
&& isIPv4()) ) {
808 inet_ntop(AF_INET
, &tmp
, buf
, blen
);
810 debugs(14, DBG_CRITICAL
, "WARNING: Corrupt IP Address details OR required to display in unknown format (" <<
811 force
<< "). accepted={" << AF_UNSPEC
<< "," << AF_INET
<< "," << AF_INET6
<< "}");
812 fprintf(stderr
,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ",
813 force
, AF_UNSPEC
, AF_INET
, AF_INET6
);
814 memcpy(buf
,"dead:beef::\0", min(static_cast<unsigned int>(13),blen
));
822 Ip::Address::toHostStr(char *buf
, const unsigned int blen
) const
826 if (isIPv6() && blen
> 0) {
831 /* 8 being space for [ ] : and port digits */
833 toStr(p
, blen
-8, AF_INET6
);
835 toStr(p
, blen
-8, AF_INET
);
837 // find the end of the new string
838 while (*p
!= '\0' && p
< buf
+blen
)
841 if (isIPv6() && p
< (buf
+blen
-1) ) {
846 /* terminate just in case. */
849 /* return size of buffer now used */
854 Ip::Address::toUrl(char* buf
, unsigned int blen
) const
858 // Ensure we have a buffer.
864 p
+= toHostStr(p
, blen
);
866 if (mSocketAddr_
.sin6_port
> 0 && p
<= (buf
+blen
-7) ) {
867 // ':port' (short int) needs at most 6 bytes plus 1 for 0-terminator
868 snprintf(p
, 7, ":%d", port() );
871 // force a null-terminated string
878 Ip::Address::getSockAddr(struct sockaddr_storage
&addr
, const int family
) const
880 struct sockaddr_in
*sin
= NULL
;
882 if ( family
== AF_INET
&& !isIPv4()) {
883 // FIXME INET6: caller using the wrong socket type!
884 debugs(14, DBG_CRITICAL
, HERE
<< "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this);
888 if ( family
== AF_INET6
|| (family
== AF_UNSPEC
&& isIPv6()) ) {
889 struct sockaddr_in6
*ss6
= (struct sockaddr_in6
*)&addr
;
891 } else if ( family
== AF_INET
|| (family
== AF_UNSPEC
&& isIPv4()) ) {
892 sin
= (struct sockaddr_in
*)&addr
;
895 IASSERT("false",false);
900 Ip::Address::getSockAddr(struct sockaddr_in
&buf
) const
903 buf
.sin_family
= AF_INET
;
904 buf
.sin_port
= mSocketAddr_
.sin6_port
;
905 map6to4( mSocketAddr_
.sin6_addr
, buf
.sin_addr
);
907 debugs(14, DBG_CRITICAL
, HERE
<< "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this );
909 memset(&buf
,0xFFFFFFFF,sizeof(struct sockaddr_in
));
913 #if HAVE_SIN_LEN_IN_SAI
914 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
915 buf
.sin_len
= sizeof(struct sockaddr_in
);
920 Ip::Address::getSockAddr(struct sockaddr_in6
&buf
) const
922 memcpy(&buf
, &mSocketAddr_
, sizeof(struct sockaddr_in6
));
923 /* maintain address family. It may have changed inside us. */
924 buf
.sin6_family
= AF_INET6
;
926 #if HAVE_SIN6_LEN_IN_SAI
927 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
928 buf
.sin6_len
= sizeof(struct sockaddr_in6
);
933 Ip::Address::map4to6(const struct in_addr
&in
, struct in6_addr
&out
) const
935 /* check for special cases */
937 if ( in
.s_addr
== 0x00000000) {
940 } else if ( in
.s_addr
== 0xFFFFFFFF) {
946 out
.s6_addr
[12] = ((uint8_t *)&in
.s_addr
)[0];
947 out
.s6_addr
[13] = ((uint8_t *)&in
.s_addr
)[1];
948 out
.s6_addr
[14] = ((uint8_t *)&in
.s_addr
)[2];
949 out
.s6_addr
[15] = ((uint8_t *)&in
.s_addr
)[3];
954 Ip::Address::map6to4(const struct in6_addr
&in
, struct in_addr
&out
) const
960 memset(&out
, 0, sizeof(struct in_addr
));
961 ((uint8_t *)&out
.s_addr
)[0] = in
.s6_addr
[12];
962 ((uint8_t *)&out
.s_addr
)[1] = in
.s6_addr
[13];
963 ((uint8_t *)&out
.s_addr
)[2] = in
.s6_addr
[14];
964 ((uint8_t *)&out
.s_addr
)[3] = in
.s6_addr
[15];
968 Ip::Address::getInAddr(struct in6_addr
&buf
) const
970 memcpy(&buf
, &mSocketAddr_
.sin6_addr
, sizeof(struct in6_addr
));
974 Ip::Address::getInAddr(struct in_addr
&buf
) const
977 map6to4(mSocketAddr_
.sin6_addr
, buf
);
982 // non-compatible IPv6 Pure Address
984 debugs(14, DBG_IMPORTANT
, HERE
<< "Ip::Address::getInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this);
985 memset(&buf
,0xFFFFFFFF,sizeof(struct in_addr
));