1#if defined(HAVE_CONFIG_H)
15#include <netinet/in.h>
16#if defined(__APPLE__) && !defined(s6_addr16)
17#define s6_addr16 __u6_addr.__u6_addr16
33#define RESIPROCATE_SUBSYSTEM Subsystem::DNS
38 onlyUseExistingConnection(false),
41 sockaddr_in* addr4 = (sockaddr_in*)&
mSockaddr;
42 memset(addr4, 0,
sizeof(sockaddr_in));
47 const Data& targetDomain) :
50 onlyUseExistingConnection(false),
52 mTargetDomain(targetDomain)
62 const Data& targetDomain,
66 onlyUseExistingConnection(false),
68 mTargetDomain(targetDomain),
77 if (printableAddr.
empty())
79 m_anonv4.sin_addr.s_addr = htonl(INADDR_ANY);
89 memset(&m_anonv6, 0,
sizeof(m_anonv6));
90 m_anonv6.sin6_family = AF_INET6;
91 m_anonv6.sin6_port = htons(port);
92 if (printableAddr.
empty())
94 m_anonv6.sin6_addr = in6addr_any;
107 m_anonv4.sin_addr.s_addr = htonl(INADDR_ANY);
115 const Data& targetDomain,
119 onlyUseExistingConnection(false),
120 mTransportType(ptype),
121 mTargetDomain(targetDomain),
135 memset(&m_anonv6, 0,
sizeof(m_anonv6));
137 m_anonv6.sin6_family = AF_INET6;
138 m_anonv6.sin6_port = htons(port);
149 m_anonv4.sin_addr.s_addr = htonl(INADDR_ANY);
156 const Data& targetDomain,
160 onlyUseExistingConnection(false),
161 mTransportType(ptype),
162 mTargetDomain(targetDomain),
165 memset(&
m_anonv4, 0,
sizeof(sockaddr_in));
175 const Data& targetDomaina,
179 onlyUseExistingConnection(false),
180 mTransportType(ptype),
181 mTargetDomain(targetDomaina),
184 memset(&m_anonv6, 0,
sizeof(sockaddr_in6));
185 m_anonv6.sin6_addr = ipv6;
186 m_anonv6.sin6_port = htons(port);
187 m_anonv6.sin6_family = AF_INET6;
193 const Data& targetDomain) :
196 onlyUseExistingConnection(false),
198 mTransportType(ptype),
199 mTargetDomain(targetDomain)
201 if (addr.sa_family == AF_INET)
206 else if (addr.sa_family == AF_INET6)
208 m_anonv6 = (sockaddr_in6&)(addr);
222 if (sa->sa_family == AF_INET)
224 ((sockaddr_in*)sa)->sin_port = 0;
227 else if (sa->sa_family == AF_INET6)
229 ((sockaddr_in6*)sa)->sin6_port = 0;
248 m_anonv6 = addr.v6Address;
259# define TOKEN_IP_ADDRESS_OFFSET 4
262# define TOKEN_IP_ADDRESS_OFFSET 3
281 rawToken[2] += (tuple.
getPort() << 16);
284 rawToken[2] += (tuple.
getType() << 8);
289 rawToken[2] += 0x00000010;
293 rawToken[3] = NetNs::getNetNsId(tuple.
getNetNs());
300 rawToken[2] += 0x00000001;
301 in6_addr address =
reinterpret_cast<const sockaddr_in6&
>(tuple.
getSockaddr()).sin6_addr;
308 in_addr address =
reinterpret_cast<const sockaddr_in&
>(tuple.
getSockaddr()).sin_addr;
321 ms << container << salt;
332 if(binaryFlowToken.
size()<16)
337 DebugLog(<<
"binary flow token was too small: " << binaryFlowToken.
size());
348 bool isRealFlow = (rawToken[2] & 0x00000010 ? true :
false);
353 DebugLog(<<
"Garbage transport type in flow token: " << temp );
366 DebugLog(<<
"Binary flow token is the wrong size for its IP version.");
377 ms << flowTokenLessHMAC << salt;
378 if(ms.
getHex() != flowTokenHMAC)
380 DebugLog(<<
"Binary flow token has invalid HMAC, not our token");
387 int netNsId = rawToken[3];
390 netNs = NetNs::getNetNsName(netNsId);
392 catch (
const NetNs::Exception& e)
394 ErrLog(<<
"Tuple binary token contained netns id: " << netNsId <<
"which does not exist."
433 else if (IN6_IS_ADDR_V4MAPPED(&m_anonv6.sin6_addr))
436 (
reinterpret_cast<const unsigned char*
>(&m_anonv6.sin6_addr) + 12))));
458 m_anonv6.sin6_port = htons(port);
475 return ntohs(m_anonv6.sin6_port);
489 return m_anonv4.sin_addr.s_addr == htonl(INADDR_ANY);
491#if defined (USE_IPV6)
494 return memcmp(&m_anonv6.sin6_addr, &in6addr_any,
sizeof(in6_addr)) == 0;
512#if defined(__linux__) || defined(__APPLE__) || defined(WIN32)
513 return IN6_IS_ADDR_LOOPBACK(&(m_anonv6.sin6_addr)) != 0;
515 return ((*(
const __uint32_t *)(
const void *)(&(m_anonv6.sin6_addr.s6_addr[0])) == 0) &&
516 (*(
const __uint32_t *)(
const void *)(&(m_anonv6.sin6_addr.s6_addr[4])) == 0) &&
517 (*(
const __uint32_t *)(
const void *)(&(m_anonv6.sin6_addr.s6_addr[8])) == 0) &&
518 (*(
const __uint32_t *)(
const void *)(&(m_anonv6.sin6_addr.s6_addr[12])) == ntohl(1)));
659 return sizeof(sockaddr_in);
662 else if (
mSockaddr.sa_family == AF_INET6)
664 return sizeof(sockaddr_in6);
689 memcmp(&
m_anonv4.sin_addr, &compare.
m_anonv4.sin_addr,
sizeof(in_addr)) == 0 &&
690 compare.mNetNs == mNetNs);
695 return ((ignorePort || m_anonv6.sin6_port == compare.m_anonv6.sin6_port) &&
696 memcmp(&m_anonv6.sin6_addr, &compare.m_anonv6.sin6_addr,
sizeof(in6_addr)) == 0 &&
697 compare.mNetNs == mNetNs);
713 if (mTransportType < rhs.mTransportType)
717 else if (mTransportType > rhs.mTransportType)
727 else if(mNetNs < rhs.mNetNs)
731 else if(mNetNs > rhs.mNetNs)
761 else if (
mSockaddr.sa_family == AF_INET6 &&
764 int c = memcmp(&m_anonv6.sin6_addr,
765 &rhs.m_anonv6.sin6_addr,
775 else if (m_anonv6.sin6_port < rhs.m_anonv6.sin6_port)
784 else if (
mSockaddr.sa_family == AF_INET6 &&
789 else if (
mSockaddr.sa_family == AF_INET &&
809 if (tuple.
mSockaddr.sa_family == AF_INET6)
815 if (tuple.
mSockaddr.sa_family == AF_INET)
826 if (!tuple.mTargetDomain.
empty())
828 ostrm <<
" targetDomain=" << tuple.mTargetDomain;
833 ostrm <<
" flowKey=" << tuple.
mFlowKey;
842 ostrm <<
" mNetNs=" << tuple.mNetNs;
859 const std::vector<AddressMask>& denied)
const
864 if (entry.mAddress.isEqualWithMask(*
this, entry.mMaskBits,
true ,
true ))
871 if (!allowed.empty())
875 if (entry.mAddress.isEqualWithMask(*
this, entry.mMaskBits,
true ,
true ))
893 const sockaddr_in6& in6 =
894 reinterpret_cast<const sockaddr_in6&
>(
mSockaddr);
896 return size_t(
Data(
Data::Share, (
const char *)&in6.sin6_addr.s6_addr,
sizeof(in6.sin6_addr.s6_addr)).
hash() +
906 const sockaddr_in& in4 =
907 reinterpret_cast<const sockaddr_in&
>(
mSockaddr);
909 return size_t(in4.sin_addr.s_addr +
944 const sockaddr_in6& addr =
reinterpret_cast<const sockaddr_in6&
>(tuple.
getSockaddr());
950 const sockaddr_in& addr =
reinterpret_cast<const sockaddr_in&
>(tuple.
getSockaddr());
962 sockaddr_in* addr1 = (sockaddr_in*)&
mSockaddr;
963 sockaddr_in* addr2 = (sockaddr_in*)&compare.
getSockaddr();
965 return ((ignorePort || addr1->sin_port == addr2->sin_port) &&
966 (addr1->sin_addr.s_addr & htonl((0xFFFFFFFF << (32 - mask)))) ==
967 (addr2->sin_addr.s_addr & htonl((0xFFFFFFFF << (32 - mask)))));
972 sockaddr_in6* addr1 = (sockaddr_in6*)&
mSockaddr;
973 sockaddr_in6* addr2 = (sockaddr_in6*)&compare.
getSockaddr();
975 if(ignorePort || addr1->sin6_port == addr2->sin6_port)
980 for(
int i = 3; i >= 0; i--)
991 mask6part = 0xffffffff;
995 mask6part = 0xffffffff << (32 - temp);
999 if((*((
unsigned long*)&addr1->sin6_addr.u.Word[i*2]) & htonl(mask6part)) !=
1000 (*((
unsigned long*)&addr2->sin6_addr.u.Word[i*2]) & htonl(mask6part)))
1003 if((*((
unsigned long*)&addr1->sin6_addr._S6_un._S6_u32[i]) & htonl(mask6part)) !=
1004 (*((
unsigned long*)&addr2->sin6_addr._S6_un._S6_u32[i]) & htonl(mask6part)))
1005#elif defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
1007 if((*((
unsigned long*)&addr1->sin6_addr.__u6_addr.__u6_addr32[i]) & htonl(mask6part)) !=
1008 (*((
unsigned long*)&addr2->sin6_addr.__u6_addr.__u6_addr32[i]) & htonl(mask6part)))
1010 if((*((
uint32_t*)&addr1->sin6_addr.s6_addr16[i*2]) & htonl(mask6part)) !=
1011 (*((
uint32_t*)&addr2->sin6_addr.s6_addr16[i*2]) & htonl(mask6part)))
1032 const Tuple& rhs)
const
1034 if (lhs.mTransportType < rhs.mTransportType)
1038 else if (lhs.mTransportType > rhs.mTransportType)
1054 else if (lhs.
mSockaddr.sa_family == AF_INET6 &&
1057 if (lhs.m_anonv6.sin6_port < rhs.m_anonv6.sin6_port)
1066 else if (lhs.
mSockaddr.sa_family == AF_INET6 &&
1071 else if (lhs.
mSockaddr.sa_family == AF_INET &&
1085 const Tuple& rhs)
const
1087 if (lhs.mTransportType < rhs.mTransportType)
1091 else if (lhs.mTransportType > rhs.mTransportType)
1099 int c = memcmp(&lhs.
m_anonv4.sin_addr,
1113 else if (lhs.
mSockaddr.sa_family == AF_INET6 &&
1116 int c = memcmp(&lhs.m_anonv6.sin6_addr,
1117 &rhs.m_anonv6.sin6_addr,
1128 else if (lhs.
mSockaddr.sa_family == AF_INET6 &&
1133 else if (lhs.
mSockaddr.sa_family == AF_INET &&
1141 if(lhs.mNetNs < rhs.mNetNs)
1146 else if(rhs.mNetNs < lhs.mNetNs)
1159 const Tuple& rhs)
const
1190 const Tuple& rhs)
const
1192 if (lhs.mTransportType < rhs.mTransportType)
1196 else if (lhs.mTransportType > rhs.mTransportType)
1201 else if (lhs.
mSockaddr.sa_family == AF_INET6 &&
1206 else if (lhs.
mSockaddr.sa_family == AF_INET &&
Wraps your platform's implementation of hash_map, hash_multimap, and hash_set.
#define HashValueImp(type, ret)
#define resip_assert(x)
This file needs to work for both C and C++ code.
static Tuple v4documentation1("192.0.2.0", 0, UNKNOWN_TRANSPORT)
static Tuple v6linkedscopedunicast("fe80::", 0, UNKNOWN_TRANSPORT)
static Tuple v4documentation2("198.51.100.0", 0, UNKNOWN_TRANSPORT)
static Tuple v66to4("2002::", 0, UNKNOWN_TRANSPORT)
static Tuple v4multicast("224.0.0.0", 0, UNKNOWN_TRANSPORT)
static Tuple v4broadcast("255.255.255.255", 0, UNKNOWN_TRANSPORT)
static Tuple v4benchmarking("198.18.0.0", 0, UNKNOWN_TRANSPORT)
static Tuple v4privateaddrbase1("10.0.0.0", 0, UNKNOWN_TRANSPORT)
static Tuple v6privateaddrbase("fc00::", 0, UNKNOWN_TRANSPORT)
static Tuple v4privateaddrbase2("172.16.0.0", 0, UNKNOWN_TRANSPORT)
static Tuple v6multicast("ff00::", 0, UNKNOWN_TRANSPORT)
static Tuple v6discardonly("100::", 0, UNKNOWN_TRANSPORT)
static Tuple v4sharedaddrbase1("100.64.0.0", 0, UNKNOWN_TRANSPORT)
static Tuple v4ietfprotocol("192.0.0.0", 0, UNKNOWN_TRANSPORT)
static Tuple v4privateaddrbase3("192.168.0.0", 0, UNKNOWN_TRANSPORT)
static Tuple v4documentation3("203.0.113.0", 0, UNKNOWN_TRANSPORT)
static Tuple v4llinklocal("169.254.0.0", 0, UNKNOWN_TRANSPORT)
#define TOKEN_IP_ADDRESS_OFFSET
static Tuple v6documentation("2001:db8::", 0, UNKNOWN_TRANSPORT)
static Tuple v6v4mappedaddress("::ffff:0:0", 0, UNKNOWN_TRANSPORT)
static Tuple v46to5relayanycast("192.88.99.0", 0, UNKNOWN_TRANSPORT)
static Tuple v6orchid("2001:10::", 0, UNKNOWN_TRANSPORT)
static Tuple v6benchmarking("2001:2::", 0, UNKNOWN_TRANSPORT)
static Tuple v64to6translation("64:ff9b::", 0, UNKNOWN_TRANSPORT)
static Tuple v6uniquelocal("fc00::", 0, UNKNOWN_TRANSPORT)
static Tuple loopbackv4("127.0.0.1", 0, UNKNOWN_TRANSPORT)
static Tuple v6unspecified("::", 0, UNKNOWN_TRANSPORT)
static Tuple v4thishostonthisnetwork("0.0.0.0", 0, UNKNOWN_TRANSPORT)
static Tuple v6ietfprotocol("2001::", 0, UNKNOWN_TRANSPORT)
static Tuple v4reserved("240.0.0.0", 0, UNKNOWN_TRANSPORT)
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
static const Data Empty
Constant that represents a zero-length data.
@ Share
The Data instance will use the buffer in a read-only mode.
Data & append(const char *str, size_type len)
Appends the specified number of bytes to the end of this Data.
bool empty() const noexcept
Checks whether the Data is empty.
size_type size() const noexcept
Returns the number of bytes in this Data.
size_t hash() const
Creates a 32-bit hash based on the contents of this Data.
Data & clear()
Clears the contents of this Data.
const char * data() const noexcept
Returns a pointer to the contents of this Data.
void reserve(size_type capacity)
Guarantees that the underlying buffer used by the Data is at least the number of bytes specified.
Used to accumlate data written to the stream in a DigestBuffer and convert the data to Digest.
Data getHex()
Calls flush() on itself and returns the Digest data in hex format.
static bool isIpV6Address(const Data &ipAddress)
static int inet_pton(const Data &printableIp, struct in_addr &dst)
Convert from the presentation format of the IPv4 address to struct in_addr.
static Data inet_ntop(const struct in_addr &addr)
Converts from the network format to presentation format.
static bool isIpV4Address(const Data &ipAddress)
bool operator()(const Tuple &x, const Tuple &y) const
bool operator()(const Tuple &x, const Tuple &y) const
bool operator()(const Tuple &x, const Tuple &y) const
bool operator()(const Tuple &x, const Tuple &y) const
Represents a network address.
IpVersion ipVersion() const
Returns V4 or V6 as appropriate.
Data presentationFormat() const
Wrapper around the inet_top() method.
static Tuple makeTupleFromBinaryToken(const Data &binaryToken, const Data &salt=Data::Empty)
size_t hash() const
Creates a 32-bit hash based on the contents of this Tuple.
static const Data & toData(TransportType)
Converts the TransportType to a string representation of the transport type, e.g.
bool operator==(const Tuple &rhs) const
Compares TransportType, the binary address, port, and address family of the Tuple.
const Data & getNetNs() const
Get the netns for this Tuple.
GenericIPAddress toGenericIPAddress() const
bool isAnyInterface() const
TRUE if this address is equal to the "INADDR_ANY" value for this address family.
bool onlyUseExistingConnection
bool isPrivateAddress() const
FlowKey mFlowKey
This is a (largely) opaque key that subclasses of Transport will use to help record/find flows.
bool isAddressAllowed(const std::vector< AddressMask > &allowed, const std::vector< AddressMask > &denied) const
Tests whether this address is permitted by the supplied allow/deny address lists.
static const Data & toDataLower(TransportType type)
static void writeBinaryToken(const Tuple &tuple, Data &container, const Data &salt=Data::Empty)
bool isEqual(const Tuple &tuple, bool ignorePort=false, bool ignoreTransport=false) const
compares this tuple with the one passed in for family, port and address equality.
void copySockaddrAnyPort(sockaddr *sa)
Get a copy of the socket address including the interface and not the port number.
bool isEqualWithMask(const Tuple &tuple, short mask, bool ignorePort=false, bool ignoreTransport=false) const
compares this tuple with the one passed in for family, port and address equality using the passed in ...
TransportType getType() const
static Data inet_ntop(const Tuple &tuple)
Converts the binary socket address to presentation format, via the DnsUtil::inet_ntop() method.
const sockaddr & getSockaddr() const
Retrieve a const binary representation of the socket address for this tuple.
static TransportType toTransport(const Data &)
Converts a string representation of transport type, i.e.
bool isSpecialPurposeAddress() const
void setSockaddr(const GenericIPAddress &)
Set the internal binary representation of the socket address from the GenericIPAddress.
TransportKey mTransportKey
bool operator<(const Tuple &rhs) const
Compares TransportType, the binary address, port, and address family of the Tuple.
This file is used to handle compatibility fixes/tweaks so reSIProcate can function on multiple platfo...
dcm! – add UnusedChecking(_enum) below;
const resip::Data & toData(const TransportType typeEnum)
TransportType toTransportType(const resip::Data &transportName)
const resip::Data & toDataLower(const TransportType typeEnum)
unsigned long TransportKey
TransportType
An enumeration of transport protocols.
IpVersion
An enumeration of IP versions.
EncodeStream & operator<<(EncodeStream &strm, const BaseException &e)
An IP address paired with a CIDR prefix length (number of mask bits), for use in address-based access...
Represents an IP-address and port (V4 or V6).