]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ip/Address.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 14 IP Storage and Handling */
12 #include "compat/getaddrinfo.h"
13 #include "compat/inet_ntop.h"
15 #include "ip/Address.h"
23 #include <arpa/inet.h>
26 // Windows IPv6 definitions
30 // some OS (ie WIndows) define IN6_ADDR_EQUAL instead
31 #if !defined(IN6_ARE_ADDR_EQUAL) && _SQUID_WINDOWS_
32 #define IN6_ARE_ADDR_EQUAL IN6_ADDR_EQUAL
35 /* Debugging only. Dump the address content when a fatal assert is encountered. */
36 #define IASSERT(a,b) \
37 if(!(b)){ printf("assert \"%s\" at line %d\n", a, __LINE__); \
38 printf("Ip::Address invalid? with isIPv4()=%c, isIPv6()=%c\n",(isIPv4()?'T':'F'),(isIPv6()?'T':'F')); \
40 for(unsigned int i = 0; i < sizeof(mSocketAddr_.sin6_addr); ++i) { \
41 printf(" %x", mSocketAddr_.sin6_addr.s6_addr[i]); \
42 } printf("\n"); assert(b); \
46 Ip::Address::cidr() const
51 const uint8_t *ptr
= mSocketAddr_
.sin6_addr
.s6_addr
;
53 /* Let's scan all the bits from Most Significant to Least */
54 /* Until we find an "0" bit. Then, we return */
57 /* return IPv4 CIDR for any Mapped address */
58 /* Thus only check the mapped bit */
64 for (; shift
<sizeof(mSocketAddr_
.sin6_addr
) ; ++shift
) {
69 continue ; /* A short-cut */
72 for (caught
= 0 , bit
= 7 ; !caught
&& (bit
<= 7); --bit
) {
73 caught
= ((ipbyte
& 0x80) == 0x00); /* Found a '0' at 'bit' ? */
82 break; /* We have found the most significant "0" bit. */
89 Ip::Address::applyMask(Ip::Address
const &mask_addr
)
91 uint32_t *p1
= (uint32_t*)(&mSocketAddr_
.sin6_addr
);
92 uint32_t const *p2
= (uint32_t const *)(&mask_addr
.mSocketAddr_
.sin6_addr
);
93 unsigned int blen
= sizeof(mSocketAddr_
.sin6_addr
)/sizeof(uint32_t);
94 unsigned int changes
= 0;
96 for (unsigned int i
= 0; i
< blen
; ++i
) {
97 if ((p1
[i
] & p2
[i
]) != p1
[i
])
107 Ip::Address::applyMask(const unsigned int cidrMask
, int mtype
)
109 uint8_t clearbits
= 0;
112 // validation and short-cuts.
116 if (cidrMask
> 32 && mtype
== AF_INET
)
120 /* CIDR /0 is NoAddr regardless of the IPv4/IPv6 protocol */
125 clearbits
= (uint8_t)( (mtype
==AF_INET6
?128:32) - cidrMask
);
131 p
= (uint8_t*)(&mSocketAddr_
.sin6_addr
) + 15;
133 for (; clearbits
>0 && p
>= (uint8_t*)&mSocketAddr_
.sin6_addr
; --p
) {
135 *p
&= ((0xFF << clearbits
) & 0xFF);
147 Ip::Address::isSockAddr() const
149 return (mSocketAddr_
.sin6_port
!= 0);
153 Ip::Address::isIPv4() const
155 return IN6_IS_ADDR_V4MAPPED( &mSocketAddr_
.sin6_addr
);
159 Ip::Address::isIPv6() const
165 Ip::Address::isAnyAddr() const
167 return IN6_IS_ADDR_UNSPECIFIED(&mSocketAddr_
.sin6_addr
) || IN6_ARE_ADDR_EQUAL(&mSocketAddr_
.sin6_addr
, &v4_anyaddr
);
170 /// NOTE: Does NOT clear the Port stored. Ony the Address and Type.
172 Ip::Address::setAnyAddr()
174 memset(&mSocketAddr_
.sin6_addr
, 0, sizeof(struct in6_addr
) );
177 /// NOTE: completely empties the Ip::Address structure. Address, Port, Type, everything.
179 Ip::Address::setEmpty()
181 memset(&mSocketAddr_
, 0, sizeof(mSocketAddr_
) );
185 // Bug 2885 comment 78 explains.
186 // In short AIX has a different netinet/in.h union definition
187 const struct in6_addr
Ip::Address::v4_localhost
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x7f000001 }}};
188 const struct in6_addr
Ip::Address::v4_anyaddr
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0x00000000 }}};
189 const struct in6_addr
Ip::Address::v4_noaddr
= {{{ 0x00000000, 0x00000000, 0x0000ffff, 0xffffffff }}};
190 const struct in6_addr
Ip::Address::v6_noaddr
= {{{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }}};
192 const struct in6_addr
Ip::Address::v4_localhost
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
193 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 }}
195 const struct in6_addr
Ip::Address::v4_anyaddr
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
196 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }}
198 const struct in6_addr
Ip::Address::v4_noaddr
= {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
199 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
201 const struct in6_addr
Ip::Address::v6_noaddr
= {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
202 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}
207 Ip::Address::setIPv4()
209 if ( isLocalhost() ) {
210 mSocketAddr_
.sin6_addr
= v4_localhost
;
215 mSocketAddr_
.sin6_addr
= v4_anyaddr
;
220 mSocketAddr_
.sin6_addr
= v4_noaddr
;
227 // anything non-IPv4 and non-convertable is BAD.
232 Ip::Address::isLocalhost() const
234 return IN6_IS_ADDR_LOOPBACK( &mSocketAddr_
.sin6_addr
) || IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v4_localhost
);
238 Ip::Address::setLocalhost()
240 if (Ip::EnableIpv6
) {
241 mSocketAddr_
.sin6_addr
= in6addr_loopback
;
242 mSocketAddr_
.sin6_family
= AF_INET6
;
244 mSocketAddr_
.sin6_addr
= v4_localhost
;
245 mSocketAddr_
.sin6_family
= AF_INET
;
250 Ip::Address::isSiteLocal6() const
252 // RFC 4193 the site-local allocated range is fc00::/7
253 // with fd00::/8 as the only currently allocated range (so we test it first).
254 // BUG: as of 2010-02 Linux and BSD define IN6_IS_ADDR_SITELOCAL() to check for fec::/10
255 return mSocketAddr_
.sin6_addr
.s6_addr
[0] == static_cast<uint8_t>(0xfd) ||
256 mSocketAddr_
.sin6_addr
.s6_addr
[0] == static_cast<uint8_t>(0xfc);
260 Ip::Address::isSiteLocalAuto() const
262 return mSocketAddr_
.sin6_addr
.s6_addr
[11] == static_cast<uint8_t>(0xff) &&
263 mSocketAddr_
.sin6_addr
.s6_addr
[12] == static_cast<uint8_t>(0xfe);
267 Ip::Address::isNoAddr() const
269 // IFF the address == 0xff..ff (all ones)
270 return IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v6_noaddr
)
271 || IN6_ARE_ADDR_EQUAL( &mSocketAddr_
.sin6_addr
, &v4_noaddr
);
275 Ip::Address::setNoAddr()
277 memset(&mSocketAddr_
.sin6_addr
, 0xFF, sizeof(struct in6_addr
) );
278 mSocketAddr_
.sin6_family
= AF_INET6
;
282 Ip::Address::getReverseString6(char buf
[MAX_IPSTRLEN
], const struct in6_addr
&dat
) const
285 unsigned char const *r
= dat
.s6_addr
;
288 /* 4321:0:1:2:3:4:567:89ab */
290 /* 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. */
292 /* Work from the binary field. Anything else may have representation changes. */
293 /* The sin6_port and sin6_addr members shall be in network byte order. */
295 /* Compile Err: 'Too many arguments for format. */
297 for (int i
= 15; i
>= 0; --i
, p
+=4) {
298 snprintf(p
, 5, "%x.%x.", ((r
[i
])&0xf), (((r
[i
])>>4)&0xf) );
302 /* ip6.int is now deprecated TLD, use ip6.arpa instead. */
303 snprintf(p
,10,"ip6.arpa.");
309 Ip::Address::getReverseString4(char buf
[MAX_IPSTRLEN
], const struct in_addr
&dat
) const
311 unsigned int i
= (unsigned int) ntohl(dat
.s_addr
);
312 snprintf(buf
, 32, "%u.%u.%u.%u.in-addr.arpa.",
321 Ip::Address::getReverseString(char buf
[MAX_IPSTRLEN
], int show_type
) const
324 if (show_type
== AF_UNSPEC
) {
325 show_type
= isIPv6() ? AF_INET6
: AF_INET
;
328 if (show_type
== AF_INET
&& isIPv4()) {
329 struct in_addr
* tmp
= (struct in_addr
*)&mSocketAddr_
.sin6_addr
.s6_addr
[12];
330 return getReverseString4(buf
, *tmp
);
331 } else if ( show_type
== AF_INET6
&& isIPv6() ) {
332 return getReverseString6(buf
, mSocketAddr_
.sin6_addr
);
335 debugs(14, DBG_CRITICAL
, "Unable to convert '" << toStr(buf
,MAX_IPSTRLEN
) << "' to the rDNS type requested.");
343 Ip::Address::operator =(const Ip::Address
&s
)
345 memcpy(this, &s
, sizeof(Ip::Address
));
349 Ip::Address::Address(const char*s
)
352 lookupHostIP(s
, true);
356 Ip::Address::operator =(const char* s
)
358 return lookupHostIP(s
, true);
362 Ip::Address::GetHostByName(const char* s
)
364 return lookupHostIP(s
, false);
368 Ip::Address::lookupHostIP(const char *s
, bool nodns
)
370 struct addrinfo want
;
371 memset(&want
, 0, sizeof(struct addrinfo
));
373 want
.ai_flags
= AI_NUMERICHOST
; // prevent actual DNS lookups!
377 struct addrinfo
*res
= NULL
;
378 if ( (err
= getaddrinfo(s
, NULL
, &want
, &res
)) != 0) {
379 debugs(14,3, HERE
<< "Given Non-IP '" << s
<< "': " << gai_strerror(err
) );
380 /* free the memory getaddrinfo() dynamically allocated. */
387 * NP: =(sockaddr_*) may alter the port. we don't want that.
388 * all we have been given as input was an IPA.
390 short portSaved
= port();
394 /* free the memory getaddrinfo() dynamically allocated. */
399 Ip::Address::Address(struct sockaddr_in
const &s
)
406 Ip::Address::operator =(struct sockaddr_in
const &s
)
408 map4to6((const in_addr
)s
.sin_addr
, mSocketAddr_
.sin6_addr
);
409 mSocketAddr_
.sin6_port
= s
.sin_port
;
410 mSocketAddr_
.sin6_family
= AF_INET6
;
415 Ip::Address::operator =(const struct sockaddr_storage
&s
)
417 /* some AF_* magic to tell socket types apart and what we need to do */
418 if (s
.ss_family
== AF_INET6
) {
419 memcpy(&mSocketAddr_
, &s
, sizeof(struct sockaddr_in6
));
420 } else { // convert it to our storage mapping.
421 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&s
;
422 mSocketAddr_
.sin6_port
= sin
->sin_port
;
423 map4to6( sin
->sin_addr
, mSocketAddr_
.sin6_addr
);
428 Ip::Address::Address(struct sockaddr_in6
const &s
)
435 Ip::Address::operator =(struct sockaddr_in6
const &s
)
437 memcpy(&mSocketAddr_
, &s
, sizeof(struct sockaddr_in6
));
442 Ip::Address::Address(struct in_addr
const &s
)
449 Ip::Address::operator =(struct in_addr
const &s
)
451 map4to6((const in_addr
)s
, mSocketAddr_
.sin6_addr
);
452 mSocketAddr_
.sin6_family
= AF_INET6
;
456 Ip::Address::Address(struct in6_addr
const &s
)
463 Ip::Address::operator =(struct in6_addr
const &s
)
466 memcpy(&mSocketAddr_
.sin6_addr
, &s
, sizeof(struct in6_addr
));
467 mSocketAddr_
.sin6_family
= AF_INET6
;
472 Ip::Address::Address(const Ip::Address
&s
)
478 Ip::Address::Address(const struct hostent
&s
)
485 Ip::Address::operator =(const struct hostent
&s
)
488 struct in_addr
* ipv4
= NULL
;
490 struct in6_addr
* ipv6
= NULL
;
493 // char *h_name; /* official name of host */
494 // char **h_aliases; /* alias list */
495 // int h_addrtype; /* host address type */
496 // int h_length; /* length of address */
497 // char **h_addr_list; /* list of addresses */
500 switch (s
.h_addrtype
) {
503 ipv4
= (in_addr
*)(s
.h_addr_list
[0]);
509 ipv6
= (in6_addr
*)(s
.h_addr_list
[0]);
515 IASSERT("false",false);
522 Ip::Address::Address(const struct addrinfo
&s
)
529 Ip::Address::operator =(const struct addrinfo
&s
)
532 struct sockaddr_in
* ipv4
= NULL
;
534 struct sockaddr_in6
* ipv6
= NULL
;
537 // int ai_flags; /* input flags */
538 // int ai_family; /* protocol family for socket */
539 // int ai_socktype; /* socket type */
540 // int ai_protocol; /* protocol for socket */
541 // socklen_t ai_addrlen; /* length of socket-address */
542 // struct sockaddr *ai_addr; /* socket-address for socket */
543 // char *ai_canonname; /* canonical name for service location */
544 // struct addrinfo *ai_next; /* pointer to next in list */
547 switch (s
.ai_family
) {
550 ipv4
= (sockaddr_in
*)(s
.ai_addr
);
557 ipv6
= (sockaddr_in6
*)(s
.ai_addr
);
565 // attempt to handle partially initialised addrinfo.
566 // such as those where data only comes from getsockopt()
567 if (s
.ai_addr
!= NULL
) {
568 if (s
.ai_addrlen
== sizeof(struct sockaddr_in6
)) {
569 operator=(*((struct sockaddr_in6
*)s
.ai_addr
));
571 } else if (s
.ai_addrlen
== sizeof(struct sockaddr_in
)) {
572 operator=(*((struct sockaddr_in
*)s
.ai_addr
));
583 Ip::Address::getAddrInfo(struct addrinfo
*&dst
, int force
) const
589 memset(dst
, 0, sizeof(struct addrinfo
));
592 // Mac OS X does not emit a flag indicating the output is numeric (IP address)
596 dst
->ai_flags
= AI_NUMERICHOST
;
599 if (dst
->ai_socktype
== 0)
600 dst
->ai_socktype
= SOCK_STREAM
;
602 if (dst
->ai_socktype
== SOCK_STREAM
// implies TCP
603 && dst
->ai_protocol
== 0)
604 dst
->ai_protocol
= IPPROTO_TCP
;
606 if (dst
->ai_socktype
== SOCK_DGRAM
// implies UDP
607 && dst
->ai_protocol
== 0)
608 dst
->ai_protocol
= IPPROTO_UDP
;
610 if (force
== AF_INET6
|| (force
== AF_UNSPEC
&& Ip::EnableIpv6
&& isIPv6()) ) {
611 dst
->ai_addr
= (struct sockaddr
*)new sockaddr_in6
;
613 memset(dst
->ai_addr
,0,sizeof(struct sockaddr_in6
));
615 getSockAddr(*((struct sockaddr_in6
*)dst
->ai_addr
));
617 dst
->ai_addrlen
= sizeof(struct sockaddr_in6
);
619 dst
->ai_family
= ((struct sockaddr_in6
*)dst
->ai_addr
)->sin6_family
;
623 * Enable only if you must and please report to squid-dev if you find a need for this.
625 * Vista may need this to cope with dual-stack (unsetting IP6_V6ONLY).
626 * http://msdn.microsoft.com/en-us/library/ms738574(VS.85).aspx
627 * Linux appears to only do some things when its present.
629 * FreeBSD dies horribly when using dual-stack with it set.
630 * (43) Protocol not supported
632 dst
->ai_protocol
= IPPROTO_IPV6
;
635 } else if ( force
== AF_INET
|| (force
== AF_UNSPEC
&& isIPv4()) ) {
637 dst
->ai_addr
= (struct sockaddr
*)new sockaddr_in
;
639 memset(dst
->ai_addr
,0,sizeof(struct sockaddr_in
));
641 getSockAddr(*((struct sockaddr_in
*)dst
->ai_addr
));
643 dst
->ai_addrlen
= sizeof(struct sockaddr_in
);
645 dst
->ai_family
= ((struct sockaddr_in
*)dst
->ai_addr
)->sin_family
;
647 IASSERT("false",false);
652 Ip::Address::InitAddrInfo(struct addrinfo
*&ai
)
656 memset(ai
,0,sizeof(struct addrinfo
));
659 // remove any existing data.
660 if (ai
->ai_addr
) delete ai
->ai_addr
;
662 ai
->ai_addr
= (struct sockaddr
*)new sockaddr_in6
;
663 memset(ai
->ai_addr
, 0, sizeof(struct sockaddr_in6
));
665 ai
->ai_addrlen
= sizeof(struct sockaddr_in6
);
670 Ip::Address::FreeAddrInfo(struct addrinfo
*&ai
)
672 if (ai
== NULL
) return;
674 if (ai
->ai_addr
) delete ai
->ai_addr
;
680 // NP: name fields are NOT allocated at present.
687 Ip::Address::matchIPAddr(const Ip::Address
&rhs
) const
689 uint8_t *l
= (uint8_t*)mSocketAddr_
.sin6_addr
.s6_addr
;
690 uint8_t *r
= (uint8_t*)rhs
.mSocketAddr_
.sin6_addr
.s6_addr
;
692 // loop a byte-wise compare
693 // NP: match MUST be R-to-L : L-to-R produces inconsistent gt/lt results at varying CIDR
694 // expected difference on CIDR is gt/eq or lt/eq ONLY.
695 for (unsigned int i
= 0 ; i
< sizeof(mSocketAddr_
.sin6_addr
) ; ++i
) {
708 Ip::Address::compareWhole(const Ip::Address
&rhs
) const
710 return memcmp(this, &rhs
, sizeof(*this));
714 Ip::Address::operator ==(const Ip::Address
&s
) const
716 return (0 == matchIPAddr(s
));
720 Ip::Address::operator !=(const Ip::Address
&s
) const
722 return ! ( operator==(s
) );
726 Ip::Address::operator <=(const Ip::Address
&rhs
) const
728 if (isAnyAddr() && !rhs
.isAnyAddr())
731 return (matchIPAddr(rhs
) <= 0);
735 Ip::Address::operator >=(const Ip::Address
&rhs
) const
737 if (isNoAddr() && !rhs
.isNoAddr())
740 return ( matchIPAddr(rhs
) >= 0);
744 Ip::Address::operator >(const Ip::Address
&rhs
) const
746 if (isNoAddr() && !rhs
.isNoAddr())
749 return ( matchIPAddr(rhs
) > 0);
753 Ip::Address::operator <(const Ip::Address
&rhs
) const
755 if (isAnyAddr() && !rhs
.isAnyAddr())
758 return ( matchIPAddr(rhs
) < 0);
762 Ip::Address::port() const
764 return ntohs( mSocketAddr_
.sin6_port
);
768 Ip::Address::port(unsigned short prt
)
770 mSocketAddr_
.sin6_port
= htons(prt
);
776 * toStr Given a buffer writes a readable ascii version of the IPA and/or port stored
778 * Buffer must be of a size large enough to hold the converted address.
779 * This size is provided in the form of a global defined variable MAX_IPSTRLEN
780 * Should a buffer shorter be provided the string result will be truncated
781 * at the length of the available buffer.
783 * A copy of the buffer is also returned for simple immediate display.
786 Ip::Address::toStr(char* buf
, const unsigned int blen
, int force
) const
788 // Ensure we have a buffer.
793 /* some external code may have blindly memset a parent. */
794 /* thats okay, our default is known */
797 memcpy(buf
,"::\0", min(static_cast<unsigned int>(3),blen
));
799 memcpy(buf
,"0.0.0.0\0", min(static_cast<unsigned int>(8),blen
));
803 memset(buf
,0,blen
); // clear buffer before write
805 /* Pure-IPv6 CANNOT be displayed in IPv4 format. */
806 /* However IPv4 CAN. */
807 if ( force
== AF_INET
&& !isIPv4() ) {
809 memcpy(buf
, "{!IPv4}\0", min(static_cast<unsigned int>(8),blen
));
814 if ( force
== AF_INET6
|| (force
== AF_UNSPEC
&& isIPv6()) ) {
816 inet_ntop(AF_INET6
, &mSocketAddr_
.sin6_addr
, buf
, blen
);
818 } else if ( force
== AF_INET
|| (force
== AF_UNSPEC
&& isIPv4()) ) {
822 inet_ntop(AF_INET
, &tmp
, buf
, blen
);
824 debugs(14, DBG_CRITICAL
, "WARNING: Corrupt IP Address details OR required to display in unknown format (" <<
825 force
<< "). accepted={" << AF_UNSPEC
<< "," << AF_INET
<< "," << AF_INET6
<< "}");
826 fprintf(stderr
,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ",
827 force
, AF_UNSPEC
, AF_INET
, AF_INET6
);
828 memcpy(buf
,"dead:beef::\0", min(static_cast<unsigned int>(13),blen
));
836 Ip::Address::toHostStr(char *buf
, const unsigned int blen
) const
840 if (isIPv6() && blen
> 0) {
845 /* 8 being space for [ ] : and port digits */
847 toStr(p
, blen
-8, AF_INET6
);
849 toStr(p
, blen
-8, AF_INET
);
851 // find the end of the new string
852 while (*p
!= '\0' && p
< buf
+blen
)
855 if (isIPv6() && p
< (buf
+blen
-1) ) {
860 /* terminate just in case. */
863 /* return size of buffer now used */
868 Ip::Address::toUrl(char* buf
, unsigned int blen
) const
872 // Ensure we have a buffer.
878 p
+= toHostStr(p
, blen
);
880 if (mSocketAddr_
.sin6_port
> 0 && p
<= (buf
+blen
-7) ) {
881 // ':port' (short int) needs at most 6 bytes plus 1 for 0-terminator
882 snprintf(p
, 7, ":%d", port() );
885 // force a null-terminated string
892 Ip::Address::getSockAddr(struct sockaddr_storage
&addr
, const int family
) const
894 struct sockaddr_in
*sin
= NULL
;
896 if ( family
== AF_INET
&& !isIPv4()) {
897 // FIXME INET6: caller using the wrong socket type!
898 debugs(14, DBG_CRITICAL
, HERE
<< "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this);
902 if ( family
== AF_INET6
|| (family
== AF_UNSPEC
&& isIPv6()) ) {
903 struct sockaddr_in6
*ss6
= (struct sockaddr_in6
*)&addr
;
905 } else if ( family
== AF_INET
|| (family
== AF_UNSPEC
&& isIPv4()) ) {
906 sin
= (struct sockaddr_in
*)&addr
;
909 IASSERT("false",false);
914 Ip::Address::getSockAddr(struct sockaddr_in
&buf
) const
917 buf
.sin_family
= AF_INET
;
918 buf
.sin_port
= mSocketAddr_
.sin6_port
;
919 map6to4( mSocketAddr_
.sin6_addr
, buf
.sin_addr
);
921 debugs(14, DBG_CRITICAL
, HERE
<< "Ip::Address::getSockAddr : Cannot convert non-IPv4 to IPv4. from " << *this );
923 memset(&buf
,0xFFFFFFFF,sizeof(struct sockaddr_in
));
927 #if HAVE_SIN_LEN_IN_SAI
928 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
929 buf
.sin_len
= sizeof(struct sockaddr_in
);
934 Ip::Address::getSockAddr(struct sockaddr_in6
&buf
) const
936 memcpy(&buf
, &mSocketAddr_
, sizeof(struct sockaddr_in6
));
937 /* maintain address family. It may have changed inside us. */
938 buf
.sin6_family
= AF_INET6
;
940 #if HAVE_SIN6_LEN_IN_SAI
941 /* not all OS have this field, BUT when they do it can be a problem if set wrong */
942 buf
.sin6_len
= sizeof(struct sockaddr_in6
);
947 Ip::Address::map4to6(const struct in_addr
&in
, struct in6_addr
&out
) const
949 /* check for special cases */
951 if ( in
.s_addr
== 0x00000000) {
954 } else if ( in
.s_addr
== 0xFFFFFFFF) {
960 out
.s6_addr
[12] = ((uint8_t *)&in
.s_addr
)[0];
961 out
.s6_addr
[13] = ((uint8_t *)&in
.s_addr
)[1];
962 out
.s6_addr
[14] = ((uint8_t *)&in
.s_addr
)[2];
963 out
.s6_addr
[15] = ((uint8_t *)&in
.s_addr
)[3];
968 Ip::Address::map6to4(const struct in6_addr
&in
, struct in_addr
&out
) const
974 memset(&out
, 0, sizeof(struct in_addr
));
975 ((uint8_t *)&out
.s_addr
)[0] = in
.s6_addr
[12];
976 ((uint8_t *)&out
.s_addr
)[1] = in
.s6_addr
[13];
977 ((uint8_t *)&out
.s_addr
)[2] = in
.s6_addr
[14];
978 ((uint8_t *)&out
.s_addr
)[3] = in
.s6_addr
[15];
982 Ip::Address::getInAddr(struct in6_addr
&buf
) const
984 memcpy(&buf
, &mSocketAddr_
.sin6_addr
, sizeof(struct in6_addr
));
988 Ip::Address::getInAddr(struct in_addr
&buf
) const
991 map6to4(mSocketAddr_
.sin6_addr
, buf
);
996 // non-compatible IPv6 Pure Address
998 debugs(14, DBG_IMPORTANT
, HERE
<< "Ip::Address::getInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this);
999 memset(&buf
,0xFFFFFFFF,sizeof(struct in_addr
));