]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ip/Address.cc
2 * DEBUG: section 14 IP Storage and Handling
5 #include "compat/getaddrinfo.h"
6 #include "compat/inet_ntop.h"
8 #include "ip/Address.h"
16 #include <arpa/inet.h>
19 // Windows IPv6 definitions
23 // some OS (ie WIndows) define IN6_ADDR_EQUAL instead
24 #if !defined(IN6_ARE_ADDR_EQUAL) && _SQUID_WINDOWS_
25 #define IN6_ARE_ADDR_EQUAL IN6_ADDR_EQUAL
28 /* Debugging only. Dump the address content when a fatal assert is encountered. */
29 #define IASSERT(a,b) \
30 if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \
31 printf("Ip::Address invalid? with isIPv4()=%c, isIPv6()=%c\n",(isIPv4()?'T':'F'),(isIPv6()?'T':'F')); \
33 for(unsigned int i = 0; i < sizeof(mSocketAddr_.sin6_addr); ++i) { \
34 printf(" %x", mSocketAddr_.sin6_addr.s6_addr[i]); \
35 } printf("\n"); assert(b); \
39 Ip::Address::cidr() const
44 const uint8_t *ptr
= mSocketAddr_
.sin6_addr
.s6_addr
;
46 /* Let's scan all the bits from Most Significant to Least */
47 /* Until we find an "0" bit. Then, we return */
50 /* return IPv4 CIDR for any Mapped address */
51 /* Thus only check the mapped bit */
57 for (; shift
<sizeof(mSocketAddr_
.sin6_addr
) ; ++shift
) {
62 continue ; /* A short-cut */
65 for (caught
= 0 , bit
= 7 ; !caught
&& (bit
<= 7); --bit
) {
66 caught
= ((ipbyte
& 0x80) == 0x00); /* Found a '0' at 'bit' ? */
75 break; /* We have found the most significant "0" bit. */
82 Ip::Address::applyMask(Ip::Address
const &mask_addr
)
84 uint32_t *p1
= (uint32_t*)(&mSocketAddr_
.sin6_addr
);
85 uint32_t const *p2
= (uint32_t const *)(&mask_addr
.mSocketAddr_
.sin6_addr
);
86 unsigned int blen
= sizeof(mSocketAddr_
.sin6_addr
)/sizeof(uint32_t);
87 unsigned int changes
= 0;
89 for (unsigned int i
= 0; i
< blen
; ++i
) {
90 if ((p1
[i
] & p2
[i
]) != p1
[i
])
100 Ip::Address::applyMask(const unsigned int cidrMask
, int mtype
)
102 uint8_t clearbits
= 0;
105 // validation and short-cuts.
109 if (cidrMask
> 32 && mtype
== AF_INET
)
113 /* CIDR /0 is NoAddr regardless of the IPv4/IPv6 protocol */
118 clearbits
= (uint8_t)( (mtype
==AF_INET6
?128:32) - cidrMask
);
124 p
= (uint8_t*)(&mSocketAddr_
.sin6_addr
) + 15;
126 for (; clearbits
>0 && p
>= (uint8_t*)&mSocketAddr_
.sin6_addr
; --p
) {
128 *p
&= ((0xFF << clearbits
) & 0xFF);
140 Ip::Address::isSockAddr() const
142 return (mSocketAddr_
.sin6_port
!= 0);
146 Ip::Address::isIPv4() const
148 return IN6_IS_ADDR_V4MAPPED( &mSocketAddr_
.sin6_addr
);
152 Ip::Address::isIPv6() const
158 Ip::Address::isAnyAddr() const
160 return IN6_IS_ADDR_UNSPECIFIED(&mSocketAddr_
.sin6_addr
) || IN6_ARE_ADDR_EQUAL(&mSocketAddr_
.sin6_addr
, &v4_anyaddr
);
163 /// NOTE: Does NOT clear the Port stored. Ony the Address and Type.
165 Ip::Address::setAnyAddr()
167 memset(&mSocketAddr_
.sin6_addr
, 0, sizeof(struct in6_addr
) );
170 /// NOTE: completely empties the Ip::Address structure. Address, Port, Type, everything.
172 Ip::Address::setEmpty()
174 memset(&mSocketAddr_
, 0, sizeof(mSocketAddr_
) );
178 // Bug 2885 comment 78 explains.
179 // In short AIX has a different netinet/in.h union definition
180 const struct in6_addr
Ip::Address::v4_localhost
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x7f000001 }}};
181 const struct in6_addr
Ip::Address::v4_anyaddr
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x00000000 }}};
182 const struct in6_addr
Ip::Address::v4_noaddr
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0xffffffff }}};
183 const struct in6_addr
Ip::Address::v6_noaddr
= {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}};
185 const struct in6_addr
Ip::Address::v4_localhost
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
186 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 }}
188 const struct in6_addr
Ip::Address::v4_anyaddr
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }}
191 const struct in6_addr
Ip::Address::v4_noaddr
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
194 const struct in6_addr
Ip::Address::v6_noaddr
= {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
195 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
200 Ip::Address::setIPv4()
202 if ( isLocalhost() ) {
203 mSocketAddr_
.sin6_addr
= v4_localhost
;
208 mSocketAddr_
.sin6_addr
= v4_anyaddr
;
213 mSocketAddr_
.sin6_addr
= v4_noaddr
;
220 // anything non-IPv4 and non-convertable is BAD.
225 Ip::Address::isLocalhost() const
227 return IN6_IS_ADDR_LOOPBACK( &mSocketAddr_
.sin6_addr
) || IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v4_localhost
);
231 Ip::Address::setLocalhost()
233 if (Ip::EnableIpv6
) {
234 mSocketAddr_
.sin6_addr
= in6addr_loopback
;
235 mSocketAddr_
.sin6_family
= AF_INET6
;
237 mSocketAddr_
.sin6_addr
= v4_localhost
;
238 mSocketAddr_
.sin6_family
= AF_INET
;
243 Ip::Address::isSiteLocal6() const
245 // RFC 4193 the site-local allocated range is fc00::/7
246 // with fd00::/8 as the only currently allocated range (so we test it first).
247 // BUG: as of 2010-02 Linux and BSD define IN6_IS_ADDR_SITELOCAL() to check for fec::/10
248 return mSocketAddr_
.sin6_addr
.s6_addr
[0] == static_cast<uint8_t>(0xfd) ||
249 mSocketAddr_
.sin6_addr
.s6_addr
[0] == static_cast<uint8_t>(0xfc);
253 Ip::Address::isSiteLocalAuto() const
255 return mSocketAddr_
.sin6_addr
.s6_addr
[11] == static_cast<uint8_t>(0xff) &&
256 mSocketAddr_
.sin6_addr
.s6_addr
[12] == static_cast<uint8_t>(0xfe);
260 Ip::Address::isNoAddr() const
262 // IFF the address == 0xff..ff (all ones)
263 return IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v6_noaddr
)
264 || IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v4_noaddr
);
268 Ip::Address::setNoAddr()
270 memset(&mSocketAddr_
.sin6_addr
, 0xFF, sizeof(struct in6_addr
) );
271 mSocketAddr_
.sin6_family
= AF_INET6
;
275 Ip::Address::getReverseString6(char buf
[MAX_IPSTRLEN
], const struct in6_addr
&dat
) const
278 unsigned char const *r
= dat
.s6_addr
;
281 /* 4321:0:1:2:3:4:567:89ab */
283 /* 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. */
285 /* Work from the binary field. Anything else may have representation changes. */
286 /* The sin6_port and sin6_addr members shall be in network byte order. */
288 /* Compile Err: 'Too many arguments for format. */
290 for (int i
= 15; i
>= 0; --i
, p
+=4) {
291 snprintf(p
, 5, "%x.%x.", ((r
[i
])&0xf), (((r
[i
])>>4)&0xf) );
295 /* ip6.int is now deprecated TLD, use ip6.arpa instead. */
296 snprintf(p
,10,"ip6.arpa.");
302 Ip::Address::getReverseString4(char buf
[MAX_IPSTRLEN
], const struct in_addr
&dat
) const
304 unsigned int i
= (unsigned int) ntohl(dat
.s_addr
);
305 snprintf(buf
, 32, "%u.%u.%u.%u.in-addr.arpa.",
314 Ip::Address::getReverseString(char buf
[MAX_IPSTRLEN
], int show_type
) const
317 if (show_type
== AF_UNSPEC
) {
318 show_type
= isIPv6() ? AF_INET6
: AF_INET
;
321 if (show_type
== AF_INET
&& isIPv4()) {
322 struct in_addr
* tmp
= (struct in_addr
*)&mSocketAddr_
.sin6_addr
.s6_addr
[12];
323 return getReverseString4(buf
, *tmp
);
324 } else if ( show_type
== AF_INET6
&& isIPv6() ) {
325 return getReverseString6(buf
, mSocketAddr_
.sin6_addr
);
328 debugs(14, DBG_CRITICAL
, "Unable to convert '" << toStr(buf
,MAX_IPSTRLEN
) << "' to the rDNS type requested.");
336 Ip::Address::operator =(const Ip::Address
&s
)
338 memcpy(this, &s
, sizeof(Ip::Address
));
342 Ip::Address::Address(const char*s
)
345 lookupHostIP(s
, true);
349 Ip::Address::operator =(const char* s
)
351 return lookupHostIP(s
, true);
355 Ip::Address::GetHostByName(const char* s
)
357 return lookupHostIP(s
, false);
361 Ip::Address::lookupHostIP(const char *s
, bool nodns
)
363 struct addrinfo want
;
364 memset(&want
, 0, sizeof(struct addrinfo
));
366 want
.ai_flags
= AI_NUMERICHOST
; // prevent actual DNS lookups!
370 struct addrinfo
*res
= NULL
;
371 if ( (err
= getaddrinfo(s
, NULL
, &want
, &res
)) != 0) {
372 debugs(14,3, HERE
<< "Given Non-IP '" << s
<< "': " << gai_strerror(err
) );
373 /* free the memory getaddrinfo() dynamically allocated. */
380 * NP: =(sockaddr_*) may alter the port. we don't want that.
381 * all we have been given as input was an IPA.
383 short portSaved
= port();
387 /* free the memory getaddrinfo() dynamically allocated. */
392 Ip::Address::Address(struct sockaddr_in
const &s
)
399 Ip::Address::operator =(struct sockaddr_in
const &s
)
401 map4to6((const in_addr
)s
.sin_addr
, mSocketAddr_
.sin6_addr
);
402 mSocketAddr_
.sin6_port
= s
.sin_port
;
403 mSocketAddr_
.sin6_family
= AF_INET6
;
408 Ip::Address::operator =(const struct sockaddr_storage
&s
)
410 /* some AF_* magic to tell socket types apart and what we need to do */
411 if (s
.ss_family
== AF_INET6
) {
412 memcpy(&mSocketAddr_
, &s
, sizeof(struct sockaddr_in6
));
413 } else { // convert it to our storage mapping.
414 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&s
;
415 mSocketAddr_
.sin6_port
= sin
->sin_port
;
416 map4to6( sin
->sin_addr
, mSocketAddr_
.sin6_addr
);
421 Ip::Address::Address(struct sockaddr_in6
const &s
)
428 Ip::Address::operator =(struct sockaddr_in6
const &s
)
430 memcpy(&mSocketAddr_
, &s
, sizeof(struct sockaddr_in6
));
435 Ip::Address::Address(struct in_addr
const &s
)
442 Ip::Address::operator =(struct in_addr
const &s
)
444 map4to6((const in_addr
)s
, mSocketAddr_
.sin6_addr
);
445 mSocketAddr_
.sin6_family
= AF_INET6
;
449 Ip::Address::Address(struct in6_addr
const &s
)
456 Ip::Address::operator =(struct in6_addr
const &s
)
459 memcpy(&mSocketAddr_
.sin6_addr
, &s
, sizeof(struct in6_addr
));
460 mSocketAddr_
.sin6_family
= AF_INET6
;
465 Ip::Address::Address(const Ip::Address
&s
)
471 Ip::Address::Address(const struct hostent
&s
)
478 Ip::Address::operator =(const struct hostent
&s
)
481 struct in_addr
* ipv4
= NULL
;
483 struct in6_addr
* ipv6
= NULL
;
486 // char *h_name; /* official name of host */
487 // char **h_aliases; /* alias list */
488 // int h_addrtype; /* host address type */
489 // int h_length; /* length of address */
490 // char **h_addr_list; /* list of addresses */
493 switch (s
.h_addrtype
) {
496 ipv4
= (in_addr
*)(s
.h_addr_list
[0]);
502 ipv6
= (in6_addr
*)(s
.h_addr_list
[0]);
508 IASSERT("false",false);
515 Ip::Address::Address(const struct addrinfo
&s
)
522 Ip::Address::operator =(const struct addrinfo
&s
)
525 struct sockaddr_in
* ipv4
= NULL
;
527 struct sockaddr_in6
* ipv6
= NULL
;
530 // int ai_flags; /* input flags */
531 // int ai_family; /* protocol family for socket */
532 // int ai_socktype; /* socket type */
533 // int ai_protocol; /* protocol for socket */
534 // socklen_t ai_addrlen; /* length of socket-address */
535 // struct sockaddr *ai_addr; /* socket-address for socket */
536 // char *ai_canonname; /* canonical name for service location */
537 // struct addrinfo *ai_next; /* pointer to next in list */
540 switch (s
.ai_family
) {
543 ipv4
= (sockaddr_in
*)(s
.ai_addr
);
550 ipv6
= (sockaddr_in6
*)(s
.ai_addr
);
558 // attempt to handle partially initialised addrinfo.
559 // such as those where data only comes from getsockopt()
560 if (s
.ai_addr
!= NULL
) {
561 if (s
.ai_addrlen
== sizeof(struct sockaddr_in6
)) {
562 operator=(*((struct sockaddr_in6
*)s
.ai_addr
));
564 } else if (s
.ai_addrlen
== sizeof(struct sockaddr_in
)) {
565 operator=(*((struct sockaddr_in
*)s
.ai_addr
));
576 Ip::Address::getAddrInfo(struct addrinfo
*&dst
, int force
) const
582 memset(dst
, 0, sizeof(struct addrinfo
));
585 // Mac OS X does not emit a flag indicating the output is numeric (IP address)
589 dst
->ai_flags
= AI_NUMERICHOST
;
592 if (dst
->ai_socktype
== 0)
593 dst
->ai_socktype
= SOCK_STREAM
;
595 if (dst
->ai_socktype
== SOCK_STREAM
// implies TCP
596 && dst
->ai_protocol
== 0)
597 dst
->ai_protocol
= IPPROTO_TCP
;
599 if (dst
->ai_socktype
== SOCK_DGRAM
// implies UDP
600 && dst
->ai_protocol
== 0)
601 dst
->ai_protocol
= IPPROTO_UDP
;
603 if (force
== AF_INET6
|| (force
== AF_UNSPEC
&& Ip::EnableIpv6
&& isIPv6()) ) {
604 dst
->ai_addr
= (struct sockaddr
*)new sockaddr_in6
;
606 memset(dst
->ai_addr
,0,sizeof(struct sockaddr_in6
));
608 getSockAddr(*((struct sockaddr_in6
*)dst
->ai_addr
));
610 dst
->ai_addrlen
= sizeof(struct sockaddr_in6
);
612 dst
->ai_family
= ((struct sockaddr_in6
*)dst
->ai_addr
)->sin6_family
;
616 * Enable only if you must and please report to squid-dev if you find a need for this.
618 * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY).
619 * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx
620 * Linux appears to only do some things when its present.
622 * FreeBSD dies horribly when using dual-stack with it set.
623 * (43) Protocol not supported
625 dst
->ai_protocol
= IPPROTO_IPV6
;
628 } else if ( force
== AF_INET
|| (force
== AF_UNSPEC
&& isIPv4()) ) {
630 dst
->ai_addr
= (struct sockaddr
*)new sockaddr_in
;
632 memset(dst
->ai_addr
,0,sizeof(struct sockaddr_in
));
634 getSockAddr(*((struct sockaddr_in
*)dst
->ai_addr
));
636 dst
->ai_addrlen
= sizeof(struct sockaddr_in
);
638 dst
->ai_family
= ((struct sockaddr_in
*)dst
->ai_addr
)->sin_family
;
640 IASSERT("false",false);
645 Ip::Address::InitAddrInfo(struct addrinfo
*&ai
)
649 memset(ai
,0,sizeof(struct addrinfo
));
652 // remove any existing data.
653 if (ai
->ai_addr
) delete ai
->ai_addr
;
655 ai
->ai_addr
= (struct sockaddr
*)new sockaddr_in6
;
656 memset(ai
->ai_addr
, 0, sizeof(struct sockaddr_in6
));
658 ai
->ai_addrlen
= sizeof(struct sockaddr_in6
);
663 Ip::Address::FreeAddrInfo(struct addrinfo
*&ai
)
665 if (ai
== NULL
) return;
667 if (ai
->ai_addr
) delete ai
->ai_addr
;
673 // NP: name fields are NOT allocated at present.
680 Ip::Address::matchIPAddr(const Ip::Address
&rhs
) const
682 uint8_t *l
= (uint8_t*)mSocketAddr_
.sin6_addr
.s6_addr
;
683 uint8_t *r
= (uint8_t*)rhs
.mSocketAddr_
.sin6_addr
.s6_addr
;
685 // loop a byte-wise compare
686 // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR
687 // expected difference on CIDR is gt/eq or lt/eq ONLY.
688 for (unsigned int i
= 0 ; i
< sizeof(mSocketAddr_
.sin6_addr
) ; ++i
) {
701 Ip::Address::compareWhole(const Ip::Address
&rhs
) const
703 return memcmp(this, &rhs
, sizeof(*this));
707 Ip::Address::operator ==(const Ip::Address
&s
) const
709 return (0 == matchIPAddr(s
));
713 Ip::Address::operator !=(const Ip::Address
&s
) const
715 return ! ( operator==(s
) );
719 Ip::Address::operator <=(const Ip::Address
&rhs
) const
721 if (isAnyAddr() && !rhs
.isAnyAddr())
724 return (matchIPAddr(rhs
) <= 0);
728 Ip::Address::operator >=(const Ip::Address
&rhs
) const
730 if (isNoAddr() && !rhs
.isNoAddr())
733 return ( matchIPAddr(rhs
) >= 0);
737 Ip::Address::operator >(const Ip::Address
&rhs
) const
739 if (isNoAddr() && !rhs
.isNoAddr())
742 return ( matchIPAddr(rhs
) > 0);
746 Ip::Address::operator <(const Ip::Address
&rhs
) const
748 if (isAnyAddr() && !rhs
.isAnyAddr())
751 return ( matchIPAddr(rhs
) < 0);
755 Ip::Address::port() const
757 return ntohs( mSocketAddr_
.sin6_port
);
761 Ip::Address::port(unsigned short prt
)
763 mSocketAddr_
.sin6_port
= htons(prt
);
769 * toStr Given a buffer writes a readable ascii version of the IPA and/or port stored
771 * Buffer must be of a size large enough to hold the converted address.
772 * This size is provided in the form of a global defined variable MAX_IPSTRLEN
773 * Should a buffer shorter be provided the string result will be truncated
774 * at the length of the available buffer.
776 * A copy of the buffer is also returned for simple immediate display.
779 Ip::Address::toStr(char* buf
, const unsigned int blen
, int force
) const
781 // Ensure we have a buffer.
786 /* some external code may have blindly memset a parent. */
787 /* thats okay, our default is known */
790 memcpy(buf
,"::\0", min(static_cast<unsigned int>(3),blen
));
792 memcpy(buf
,"0.0.0.0\0", min(static_cast<unsigned int>(8),blen
));
796 memset(buf
,0,blen
); // clear buffer before write
798 /* Pure-IPv6 CANNOT be displayed in IPv4 format. */
799 /* However IPv4 CAN. */
800 if ( force
== AF_INET
&& !isIPv4() ) {
802 memcpy(buf
, "{!IPv4}\0", min(static_cast<unsigned int>(8),blen
));
807 if ( force
== AF_INET6
|| (force
== AF_UNSPEC
&& isIPv6()) ) {
809 inet_ntop(AF_INET6
, &mSocketAddr_
.sin6_addr
, buf
, blen
);
811 } else if ( force
== AF_INET
|| (force
== AF_UNSPEC
&& isIPv4()) ) {
815 inet_ntop(AF_INET
, &tmp
, buf
, blen
);
817 debugs(14, DBG_CRITICAL
, "WARNING: Corrupt IP Address details OR required to display in unknown format (" <<
818 force
<< "). accepted={" << AF_UNSPEC
<< "," << AF_INET
<< "," << AF_INET6
<< "}");
819 fprintf(stderr
,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ",
820 force
, AF_UNSPEC
, AF_INET
, AF_INET6
);
821 memcpy(buf
,"dead:beef::\0", min(static_cast<unsigned int>(13),blen
));
829 Ip::Address::toHostStr(char *buf
, const unsigned int blen
) const
833 if (isIPv6() && blen
> 0) {
838 /* 8 being space for [ ] : and port digits */
840 toStr(p
, blen
-8, AF_INET6
);
842 toStr(p
, blen
-8, AF_INET
);
844 // find the end of the new string
845 while (*p
!= '\0' && p
< buf
+blen
)
848 if (isIPv6() && p
< (buf
+blen
-1) ) {
853 /* terminate just in case. */
856 /* return size of buffer now used */
861 Ip::Address::toUrl(char* buf
, unsigned int blen
) const
865 // Ensure we have a buffer.
871 p
+= toHostStr(p
, blen
);
873 if (mSocketAddr_
.sin6_port
> 0 && p
<= (buf
+blen
-7) ) {
874 // ':port' (short int) needs at most 6 bytes plus 1 for 0-terminator
875 snprintf(p
, 7, ":%d", port() );
878 // force a null-terminated string
885 Ip::Address::getSockAddr(struct sockaddr_storage
&addr
, const int family
) const
887 struct sockaddr_in
*sin
= NULL
;
889 if ( family
== AF_INET
&& !isIPv4()) {
890 // FIXME INET6: caller using the wrong socket type!
891 debugs(14, DBG_CRITICAL
, HERE
<< "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this);
895 if ( family
== AF_INET6
|| (family
== AF_UNSPEC
&& isIPv6()) ) {
896 struct sockaddr_in6
*ss6
= (struct sockaddr_in6
*)&addr
;
898 } else if ( family
== AF_INET
|| (family
== AF_UNSPEC
&& isIPv4()) ) {
899 sin
= (struct sockaddr_in
*)&addr
;
902 IASSERT("false",false);
907 Ip::Address::getSockAddr(struct sockaddr_in
&buf
) const
910 buf
.sin_family
= AF_INET
;
911 buf
.sin_port
= mSocketAddr_
.sin6_port
;
912 map6to4( mSocketAddr_
.sin6_addr
, buf
.sin_addr
);
914 debugs(14, DBG_CRITICAL
, HERE
<< "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this );
916 memset(&buf
,0xFFFFFFFF,sizeof(struct sockaddr_in
));
920 #if HAVE_SIN_LEN_IN_SAI
921 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
922 buf
.sin_len
= sizeof(struct sockaddr_in
);
927 Ip::Address::getSockAddr(struct sockaddr_in6
&buf
) const
929 memcpy(&buf
, &mSocketAddr_
, sizeof(struct sockaddr_in6
));
930 /* maintain address family. It may have changed inside us. */
931 buf
.sin6_family
= AF_INET6
;
933 #if HAVE_SIN6_LEN_IN_SAI
934 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
935 buf
.sin6_len
= sizeof(struct sockaddr_in6
);
940 Ip::Address::map4to6(const struct in_addr
&in
, struct in6_addr
&out
) const
942 /* check for special cases */
944 if ( in
.s_addr
== 0x00000000) {
947 } else if ( in
.s_addr
== 0xFFFFFFFF) {
953 out
.s6_addr
[12] = ((uint8_t *)&in
.s_addr
)[0];
954 out
.s6_addr
[13] = ((uint8_t *)&in
.s_addr
)[1];
955 out
.s6_addr
[14] = ((uint8_t *)&in
.s_addr
)[2];
956 out
.s6_addr
[15] = ((uint8_t *)&in
.s_addr
)[3];
961 Ip::Address::map6to4(const struct in6_addr
&in
, struct in_addr
&out
) const
967 memset(&out
, 0, sizeof(struct in_addr
));
968 ((uint8_t *)&out
.s_addr
)[0] = in
.s6_addr
[12];
969 ((uint8_t *)&out
.s_addr
)[1] = in
.s6_addr
[13];
970 ((uint8_t *)&out
.s_addr
)[2] = in
.s6_addr
[14];
971 ((uint8_t *)&out
.s_addr
)[3] = in
.s6_addr
[15];
975 Ip::Address::getInAddr(struct in6_addr
&buf
) const
977 memcpy(&buf
, &mSocketAddr_
.sin6_addr
, sizeof(struct in6_addr
));
981 Ip::Address::getInAddr(struct in_addr
&buf
) const
984 map6to4(mSocketAddr_
.sin6_addr
, buf
);
989 // non-compatible IPv6 Pure Address
991 debugs(14, DBG_IMPORTANT
, HERE
<< "Ip::Address::getInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this);
992 memset(&buf
,0xFFFFFFFF,sizeof(struct in_addr
));