From 0cb50a6ebec24a6ea7cb51bb43478853b5fec04a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Mon, 20 Jun 2022 15:26:21 +0200 Subject: [PATCH 01/12] Working on new TransportTuple.hash implementation This branch should be applied on `webrtcserver` branch or in `v3` once the former is merged. Not happy at all yet because: - `SetHash()` implementation in this branch is too simple. We should consider local IP:port and protocol as well, but we cannot just "sum" those numbers since there is a chance to collide. - And if we collide (if we have 2 tuples with same `hash` in the same `WebRtcServer`) then we'll have an assertion problem: ``` RTC::WebRtcServer::OnWebRtcTransportTransportTupleRemoved() | failed assertion `this->mapTupleWebRtcTransport.find(tuple->hash) != this->mapTupleWebRtcTransport.end()': tuple not handled ``` - So IMHO we need `hash` to be `int64_t` and we should concatenate remote IP:port first then local IP:port plus protocol somewhere. But summing protocol cannot be just about doing + 1 or +2. imagine this: ``` - Client A connects from udp:1.2.3.4:5001 to udp:9.9.9.9:9000. - Client B connects from tcp:1.2.3.4:5000 to tcp:9.9.9.9:9000. - If tcp protocol just means "adding 1 to the hash" then both hashes will be the same (BUMPP!). --- worker/include/RTC/TransportTuple.hpp | 20 ++-------------- worker/include/RTC/WebRtcServer.hpp | 2 +- worker/src/RTC/TransportTuple.cpp | 34 +++++++++++++++++++++++++++ worker/src/RTC/WebRtcServer.cpp | 3 +++ 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/worker/include/RTC/TransportTuple.hpp b/worker/include/RTC/TransportTuple.hpp index eacee1a2da..5731fb1fd7 100644 --- a/worker/include/RTC/TransportTuple.hpp +++ b/worker/include/RTC/TransportTuple.hpp @@ -131,26 +131,10 @@ namespace RTC } private: - void SetHash() - { - const std::string protocol = this->protocol == Protocol::UDP ? "udp" : "tcp"; - int family; - std::string localIp; - uint16_t localPort; - std::string remoteIp; - uint16_t remotePort; - - Utils::IP::GetAddressInfo(GetLocalAddress(), family, localIp, localPort); - Utils::IP::GetAddressInfo(GetRemoteAddress(), family, remoteIp, remotePort); - - std::string id = remoteIp + std::to_string(remotePort) + localIp + std::to_string(localPort) + - std::to_string(family) + protocol; - - this->hash = std::hash{}(id); - } + void SetHash(); public: - size_t hash; + uint32_t hash{ 0u }; private: // Passed by argument. diff --git a/worker/include/RTC/WebRtcServer.hpp b/worker/include/RTC/WebRtcServer.hpp index 504ff18f81..e43da91984 100644 --- a/worker/include/RTC/WebRtcServer.hpp +++ b/worker/include/RTC/WebRtcServer.hpp @@ -100,7 +100,7 @@ namespace RTC // Map of WebRtcTransports indexed by local ICE usernameFragment. absl::flat_hash_map mapLocalIceUsernameFragmentWebRtcTransport; // Map of WebRtcTransports indexed by TransportTuple.hash. - absl::flat_hash_map mapTupleWebRtcTransport; + absl::flat_hash_map mapTupleWebRtcTransport; }; } // namespace RTC diff --git a/worker/src/RTC/TransportTuple.cpp b/worker/src/RTC/TransportTuple.cpp index ab8dce24b2..e54f695a9d 100644 --- a/worker/src/RTC/TransportTuple.cpp +++ b/worker/src/RTC/TransportTuple.cpp @@ -82,4 +82,38 @@ namespace RTC MS_DUMP(""); } + + // TODO: Move to .hpp once done. + void TransportTuple::SetHash() + { + const struct sockaddr* localSockAddr = GetLocalAddress(); + const struct sockaddr* remoteSockAddr = GetRemoteAddress(); + + switch (localSockAddr->sa_family) + { + case AF_INET: + { + auto* localSockAddrIn = reinterpret_cast(localSockAddr); + auto* remoteSockAddrIn = reinterpret_cast(remoteSockAddr); + + this->hash += (ntohl(remoteSockAddrIn->sin_addr.s_addr) << 16); + this->hash += ntohs(remoteSockAddrIn->sin_port); + + break; + } + + case AF_INET6: + { + auto* localSockAddrIn6 = reinterpret_cast(localSockAddr); + auto* remoteSockAddrIn6 = reinterpret_cast(remoteSockAddr); + + auto* a = reinterpret_cast(std::addressof(remoteSockAddrIn6->sin6_addr)); + + this->hash += a[0] ^ a[1] ^ a[2] ^ a[3]; + this->hash += ntohs(remoteSockAddrIn6->sin6_port); + + break; + } + } + } } // namespace RTC diff --git a/worker/src/RTC/WebRtcServer.cpp b/worker/src/RTC/WebRtcServer.cpp index 9ae25f2c61..34cd8dd54f 100644 --- a/worker/src/RTC/WebRtcServer.cpp +++ b/worker/src/RTC/WebRtcServer.cpp @@ -459,6 +459,9 @@ namespace RTC { MS_TRACE(); + // TODO: REMOVE + MS_ERROR("--- hash: %" PRIu32, tuple->hash); + this->mapTupleWebRtcTransport[tuple->hash] = webRtcTransport; } From bc6b80307ae414ef465c71b2ae1a410e0282e6b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Mon, 20 Jun 2022 17:29:52 +0200 Subject: [PATCH 02/12] foo --- worker/include/RTC/TransportTuple.hpp | 2 +- worker/include/RTC/WebRtcServer.hpp | 2 +- worker/src/RTC/TransportTuple.cpp | 23 +++++++++++++++++++++-- worker/src/RTC/WebRtcServer.cpp | 3 --- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/worker/include/RTC/TransportTuple.hpp b/worker/include/RTC/TransportTuple.hpp index 5731fb1fd7..a1dd586e05 100644 --- a/worker/include/RTC/TransportTuple.hpp +++ b/worker/include/RTC/TransportTuple.hpp @@ -134,7 +134,7 @@ namespace RTC void SetHash(); public: - uint32_t hash{ 0u }; + uint64_t hash{ 0u }; private: // Passed by argument. diff --git a/worker/include/RTC/WebRtcServer.hpp b/worker/include/RTC/WebRtcServer.hpp index e43da91984..cb53233c4f 100644 --- a/worker/include/RTC/WebRtcServer.hpp +++ b/worker/include/RTC/WebRtcServer.hpp @@ -100,7 +100,7 @@ namespace RTC // Map of WebRtcTransports indexed by local ICE usernameFragment. absl::flat_hash_map mapLocalIceUsernameFragmentWebRtcTransport; // Map of WebRtcTransports indexed by TransportTuple.hash. - absl::flat_hash_map mapTupleWebRtcTransport; + absl::flat_hash_map mapTupleWebRtcTransport; }; } // namespace RTC diff --git a/worker/src/RTC/TransportTuple.cpp b/worker/src/RTC/TransportTuple.cpp index e54f695a9d..0b3130c749 100644 --- a/worker/src/RTC/TransportTuple.cpp +++ b/worker/src/RTC/TransportTuple.cpp @@ -96,7 +96,9 @@ namespace RTC auto* localSockAddrIn = reinterpret_cast(localSockAddr); auto* remoteSockAddrIn = reinterpret_cast(remoteSockAddr); - this->hash += (ntohl(remoteSockAddrIn->sin_addr.s_addr) << 16); + this->hash += ntohl(remoteSockAddrIn->sin_addr.s_addr); + // TODO: I'd like that port (8 bits) is most significant bits from 1..8 + // (bit 0 is for protocol, see below). this->hash += ntohs(remoteSockAddrIn->sin_port); break; @@ -106,14 +108,31 @@ namespace RTC { auto* localSockAddrIn6 = reinterpret_cast(localSockAddr); auto* remoteSockAddrIn6 = reinterpret_cast(remoteSockAddr); - auto* a = reinterpret_cast(std::addressof(remoteSockAddrIn6->sin6_addr)); this->hash += a[0] ^ a[1] ^ a[2] ^ a[3]; + // TODO: I'd like that port (8 bits) is most significant bits from 1..8 + // (bit 0 is for protocol, see below). this->hash += ntohs(remoteSockAddrIn6->sin6_port); break; } } + + MS_ERROR("--- temp hash : %" PRIu64, this->hash); + + // Override most significant bit with protocol information: + // - If UDP, start with 0. + // - If TCP, start with 1. + if (this->protocol == Protocol::UDP) + { + this->hash &= 0b0111111111111111111111111111111101111111111111111111111111111111; + } + else + { + this->hash |= 0b1000000000000000000000000000000000000000000000000000000000000000; + } + + MS_ERROR("--- final hash : %" PRIu64, this->hash); } } // namespace RTC diff --git a/worker/src/RTC/WebRtcServer.cpp b/worker/src/RTC/WebRtcServer.cpp index 34cd8dd54f..9ae25f2c61 100644 --- a/worker/src/RTC/WebRtcServer.cpp +++ b/worker/src/RTC/WebRtcServer.cpp @@ -459,9 +459,6 @@ namespace RTC { MS_TRACE(); - // TODO: REMOVE - MS_ERROR("--- hash: %" PRIu32, tuple->hash); - this->mapTupleWebRtcTransport[tuple->hash] = webRtcTransport; } From b2f9d00cc91a95298328497c85d93f2dc96a852e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Tue, 21 Jun 2022 10:10:53 +0200 Subject: [PATCH 03/12] TransportTuple hash --- worker/src/RTC/TransportTuple.cpp | 34 ++++++++++++++++++------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/worker/src/RTC/TransportTuple.cpp b/worker/src/RTC/TransportTuple.cpp index 0b3130c749..36bf368271 100644 --- a/worker/src/RTC/TransportTuple.cpp +++ b/worker/src/RTC/TransportTuple.cpp @@ -93,46 +93,52 @@ namespace RTC { case AF_INET: { - auto* localSockAddrIn = reinterpret_cast(localSockAddr); auto* remoteSockAddrIn = reinterpret_cast(remoteSockAddr); - this->hash += ntohl(remoteSockAddrIn->sin_addr.s_addr); - // TODO: I'd like that port (8 bits) is most significant bits from 1..8 - // (bit 0 is for protocol, see below). - this->hash += ntohs(remoteSockAddrIn->sin_port); + const auto address = ntohl(remoteSockAddrIn->sin_addr.s_addr); + const uint64_t port = (ntohs(remoteSockAddrIn->sin_port)); + + std::cout << "address " << std::bitset<64>{address} << std::endl; + std::cout << "port " << std::bitset<64>{port} << std::endl; + + this->hash = address; + this->hash |= port << 32; break; } case AF_INET6: { - auto* localSockAddrIn6 = reinterpret_cast(localSockAddr); auto* remoteSockAddrIn6 = reinterpret_cast(remoteSockAddr); auto* a = reinterpret_cast(std::addressof(remoteSockAddrIn6->sin6_addr)); - this->hash += a[0] ^ a[1] ^ a[2] ^ a[3]; - // TODO: I'd like that port (8 bits) is most significant bits from 1..8 - // (bit 0 is for protocol, see below). - this->hash += ntohs(remoteSockAddrIn6->sin6_port); + const auto address = a[0] ^ a[1] ^ a[2] ^ a[3]; + const uint64_t port = ntohs(remoteSockAddrIn6->sin6_port); + + std::cout << "address " << std::bitset<64>{address} << std::endl; + std::cout << "port " << std::bitset<64>{port} << std::endl; + + this->hash = address; + this->hash |= port << 32; break; } } - MS_ERROR("--- temp hash : %" PRIu64, this->hash); + std::cout << "tmp hash " << std::bitset<64>{this->hash} << std::endl; // Override most significant bit with protocol information: // - If UDP, start with 0. // - If TCP, start with 1. if (this->protocol == Protocol::UDP) { - this->hash &= 0b0111111111111111111111111111111101111111111111111111111111111111; + this->hash |= 1LL << 63; } else { - this->hash |= 0b1000000000000000000000000000000000000000000000000000000000000000; + this->hash |= 0LL << 63; } - MS_ERROR("--- final hash : %" PRIu64, this->hash); + std::cout << "fin hash " << std::bitset<64>{this->hash} << std::endl; } } // namespace RTC From a8eadf88139d56b7b31658864e9101af03f51cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Tue, 21 Jun 2022 10:18:15 +0200 Subject: [PATCH 04/12] format --- worker/src/RTC/TransportTuple.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/worker/src/RTC/TransportTuple.cpp b/worker/src/RTC/TransportTuple.cpp index 36bf368271..37522aa47d 100644 --- a/worker/src/RTC/TransportTuple.cpp +++ b/worker/src/RTC/TransportTuple.cpp @@ -95,11 +95,11 @@ namespace RTC { auto* remoteSockAddrIn = reinterpret_cast(remoteSockAddr); - const auto address = ntohl(remoteSockAddrIn->sin_addr.s_addr); + const auto address = ntohl(remoteSockAddrIn->sin_addr.s_addr); const uint64_t port = (ntohs(remoteSockAddrIn->sin_port)); - std::cout << "address " << std::bitset<64>{address} << std::endl; - std::cout << "port " << std::bitset<64>{port} << std::endl; + std::cout << "address " << std::bitset<64>{ address } << std::endl; + std::cout << "port " << std::bitset<64>{ port } << std::endl; this->hash = address; this->hash |= port << 32; @@ -112,11 +112,11 @@ namespace RTC auto* remoteSockAddrIn6 = reinterpret_cast(remoteSockAddr); auto* a = reinterpret_cast(std::addressof(remoteSockAddrIn6->sin6_addr)); - const auto address = a[0] ^ a[1] ^ a[2] ^ a[3]; + const auto address = a[0] ^ a[1] ^ a[2] ^ a[3]; const uint64_t port = ntohs(remoteSockAddrIn6->sin6_port); - std::cout << "address " << std::bitset<64>{address} << std::endl; - std::cout << "port " << std::bitset<64>{port} << std::endl; + std::cout << "address " << std::bitset<64>{ address } << std::endl; + std::cout << "port " << std::bitset<64>{ port } << std::endl; this->hash = address; this->hash |= port << 32; @@ -125,7 +125,7 @@ namespace RTC } } - std::cout << "tmp hash " << std::bitset<64>{this->hash} << std::endl; + std::cout << "tmp hash " << std::bitset<64>{ this->hash } << std::endl; // Override most significant bit with protocol information: // - If UDP, start with 0. @@ -139,6 +139,6 @@ namespace RTC this->hash |= 0LL << 63; } - std::cout << "fin hash " << std::bitset<64>{this->hash} << std::endl; + std::cout << "fin hash " << std::bitset<64>{ this->hash } << std::endl; } } // namespace RTC From 801fe691fd660de1accaa2af9bc0bfdce4095373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Tue, 21 Jun 2022 10:23:01 +0200 Subject: [PATCH 05/12] tmp: include bitset header --- worker/src/RTC/TransportTuple.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/worker/src/RTC/TransportTuple.cpp b/worker/src/RTC/TransportTuple.cpp index 37522aa47d..c816f9ae6f 100644 --- a/worker/src/RTC/TransportTuple.cpp +++ b/worker/src/RTC/TransportTuple.cpp @@ -4,6 +4,7 @@ #include "RTC/TransportTuple.hpp" #include "Logger.hpp" #include +#include // TMP: for logging purposes in SetHash. namespace RTC { From 8c9486bfdb440d4221e0ba9943ff881dd6daded6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Tue, 21 Jun 2022 10:55:41 +0200 Subject: [PATCH 06/12] format --- worker/src/RTC/TransportTuple.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/worker/src/RTC/TransportTuple.cpp b/worker/src/RTC/TransportTuple.cpp index c816f9ae6f..340e119284 100644 --- a/worker/src/RTC/TransportTuple.cpp +++ b/worker/src/RTC/TransportTuple.cpp @@ -3,8 +3,8 @@ #include "RTC/TransportTuple.hpp" #include "Logger.hpp" -#include #include // TMP: for logging purposes in SetHash. +#include namespace RTC { From 8859238f158e722b42047f6636205850b37f95c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Tue, 21 Jun 2022 17:10:14 +0200 Subject: [PATCH 07/12] small cleanup --- worker/src/RTC/TransportTuple.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/worker/src/RTC/TransportTuple.cpp b/worker/src/RTC/TransportTuple.cpp index 340e119284..196f873f76 100644 --- a/worker/src/RTC/TransportTuple.cpp +++ b/worker/src/RTC/TransportTuple.cpp @@ -87,10 +87,9 @@ namespace RTC // TODO: Move to .hpp once done. void TransportTuple::SetHash() { - const struct sockaddr* localSockAddr = GetLocalAddress(); const struct sockaddr* remoteSockAddr = GetRemoteAddress(); - switch (localSockAddr->sa_family) + switch (remoteSockAddr->sa_family) { case AF_INET: { @@ -133,11 +132,11 @@ namespace RTC // - If TCP, start with 1. if (this->protocol == Protocol::UDP) { - this->hash |= 1LL << 63; + this->hash |= 0LL << 63; } else { - this->hash |= 0LL << 63; + this->hash |= 1LL << 63; } std::cout << "fin hash " << std::bitset<64>{ this->hash } << std::endl; From 58146dcdd631759c371fdfd917c74c5c960c448e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Tue, 21 Jun 2022 19:16:19 +0200 Subject: [PATCH 08/12] work on tuple hash --- worker/src/RTC/TransportTuple.cpp | 50 ++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/worker/src/RTC/TransportTuple.cpp b/worker/src/RTC/TransportTuple.cpp index 196f873f76..f65097ccc1 100644 --- a/worker/src/RTC/TransportTuple.cpp +++ b/worker/src/RTC/TransportTuple.cpp @@ -85,6 +85,25 @@ namespace RTC } // TODO: Move to .hpp once done. + /* + * Hash for IPv4 + * + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | PORT | IP | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | IP | |F|P| + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * Hash for IPv6 + * + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | PORT | IP[0] ^ IP[1] ^ IP[2] ^ IP[3]| + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + |IP[0] ^ IP[1] ^ IP[2] ^ IP[3] | IP[0] >> 16 |F|P| + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ void TransportTuple::SetHash() { const struct sockaddr* remoteSockAddr = GetRemoteAddress(); @@ -95,14 +114,15 @@ namespace RTC { auto* remoteSockAddrIn = reinterpret_cast(remoteSockAddr); - const auto address = ntohl(remoteSockAddrIn->sin_addr.s_addr); - const uint64_t port = (ntohs(remoteSockAddrIn->sin_port)); + const uint64_t address = ntohl(remoteSockAddrIn->sin_addr.s_addr); + const uint64_t port = (ntohs(remoteSockAddrIn->sin_port)); - std::cout << "address " << std::bitset<64>{ address } << std::endl; std::cout << "port " << std::bitset<64>{ port } << std::endl; + std::cout << "address " << std::bitset<64>{ address } << std::endl; - this->hash = address; - this->hash |= port << 32; + this->hash = port << 48; + this->hash |= address << 16; + this->hash |= 0x0000; // AF_INET. break; } @@ -112,14 +132,22 @@ namespace RTC auto* remoteSockAddrIn6 = reinterpret_cast(remoteSockAddr); auto* a = reinterpret_cast(std::addressof(remoteSockAddrIn6->sin6_addr)); - const auto address = a[0] ^ a[1] ^ a[2] ^ a[3]; + const auto address1 = a[0] ^ a[1] ^ a[2] ^ a[3]; + const auto address2 = a[0]; const uint64_t port = ntohs(remoteSockAddrIn6->sin6_port); - std::cout << "address " << std::bitset<64>{ address } << std::endl; + std::cout << "a0 " << std::bitset<64>{ a[0] } << std::endl; + std::cout << "a1 " << std::bitset<64>{ a[1] } << std::endl; + std::cout << "a2 " << std::bitset<64>{ a[2] } << std::endl; + std::cout << "a3 " << std::bitset<64>{ a[3] } << std::endl; std::cout << "port " << std::bitset<64>{ port } << std::endl; + std::cout << "address1 " << std::bitset<64>{ address1 } << std::endl; + std::cout << "address2 " << std::bitset<64>{ address2 } << std::endl; - this->hash = address; - this->hash |= port << 32; + this->hash = port << 48; + this->hash |= static_cast(address1) << 16; + this->hash |= address2 >> 16 & 0xFFFC; + this->hash |= 0x0002; // AF_INET6. break; } @@ -132,11 +160,11 @@ namespace RTC // - If TCP, start with 1. if (this->protocol == Protocol::UDP) { - this->hash |= 0LL << 63; + this->hash |= 0x0000; } else { - this->hash |= 1LL << 63; + this->hash |= 0x0001; } std::cout << "fin hash " << std::bitset<64>{ this->hash } << std::endl; From cda403db0f642859abe243f348a47d6dd0314805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Tue, 21 Jun 2022 19:22:31 +0200 Subject: [PATCH 09/12] remove logs --- worker/src/RTC/TransportTuple.cpp | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/worker/src/RTC/TransportTuple.cpp b/worker/src/RTC/TransportTuple.cpp index f65097ccc1..968beb5f06 100644 --- a/worker/src/RTC/TransportTuple.cpp +++ b/worker/src/RTC/TransportTuple.cpp @@ -3,7 +3,6 @@ #include "RTC/TransportTuple.hpp" #include "Logger.hpp" -#include // TMP: for logging purposes in SetHash. #include namespace RTC @@ -117,9 +116,6 @@ namespace RTC const uint64_t address = ntohl(remoteSockAddrIn->sin_addr.s_addr); const uint64_t port = (ntohs(remoteSockAddrIn->sin_port)); - std::cout << "port " << std::bitset<64>{ port } << std::endl; - std::cout << "address " << std::bitset<64>{ address } << std::endl; - this->hash = port << 48; this->hash |= address << 16; this->hash |= 0x0000; // AF_INET. @@ -136,14 +132,6 @@ namespace RTC const auto address2 = a[0]; const uint64_t port = ntohs(remoteSockAddrIn6->sin6_port); - std::cout << "a0 " << std::bitset<64>{ a[0] } << std::endl; - std::cout << "a1 " << std::bitset<64>{ a[1] } << std::endl; - std::cout << "a2 " << std::bitset<64>{ a[2] } << std::endl; - std::cout << "a3 " << std::bitset<64>{ a[3] } << std::endl; - std::cout << "port " << std::bitset<64>{ port } << std::endl; - std::cout << "address1 " << std::bitset<64>{ address1 } << std::endl; - std::cout << "address2 " << std::bitset<64>{ address2 } << std::endl; - this->hash = port << 48; this->hash |= static_cast(address1) << 16; this->hash |= address2 >> 16 & 0xFFFC; @@ -153,8 +141,6 @@ namespace RTC } } - std::cout << "tmp hash " << std::bitset<64>{ this->hash } << std::endl; - // Override most significant bit with protocol information: // - If UDP, start with 0. // - If TCP, start with 1. @@ -166,7 +152,5 @@ namespace RTC { this->hash |= 0x0001; } - - std::cout << "fin hash " << std::bitset<64>{ this->hash } << std::endl; } } // namespace RTC From ff8499c8f204fc46e0e05253d5c4930db784dd6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Tue, 21 Jun 2022 19:33:12 +0200 Subject: [PATCH 10/12] move SetHash to .hpp --- worker/include/RTC/TransportTuple.hpp | 71 ++++++++++++++++++++++++++- worker/src/RTC/TransportTuple.cpp | 71 --------------------------- 2 files changed, 70 insertions(+), 72 deletions(-) diff --git a/worker/include/RTC/TransportTuple.hpp b/worker/include/RTC/TransportTuple.hpp index a1dd586e05..b6f98895dd 100644 --- a/worker/include/RTC/TransportTuple.hpp +++ b/worker/include/RTC/TransportTuple.hpp @@ -131,7 +131,76 @@ namespace RTC } private: - void SetHash(); + + /* + * Hash for IPv4 + * + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | PORT | IP | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | IP | |F|P| + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * Hash for IPv6 + * + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | PORT | IP[0] ^ IP[1] ^ IP[2] ^ IP[3]| + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + |IP[0] ^ IP[1] ^ IP[2] ^ IP[3] | IP[0] >> 16 |F|P| + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ + void SetHash() + { + const struct sockaddr* remoteSockAddr = GetRemoteAddress(); + + switch (remoteSockAddr->sa_family) + { + case AF_INET: + { + auto* remoteSockAddrIn = reinterpret_cast(remoteSockAddr); + + const uint64_t address = ntohl(remoteSockAddrIn->sin_addr.s_addr); + const uint64_t port = (ntohs(remoteSockAddrIn->sin_port)); + + this->hash = port << 48; + this->hash |= address << 16; + this->hash |= 0x0000; // AF_INET. + + break; + } + + case AF_INET6: + { + auto* remoteSockAddrIn6 = reinterpret_cast(remoteSockAddr); + auto* a = reinterpret_cast(std::addressof(remoteSockAddrIn6->sin6_addr)); + + const auto address1 = a[0] ^ a[1] ^ a[2] ^ a[3]; + const auto address2 = a[0]; + const uint64_t port = ntohs(remoteSockAddrIn6->sin6_port); + + this->hash = port << 48; + this->hash |= static_cast(address1) << 16; + this->hash |= address2 >> 16 & 0xFFFC; + this->hash |= 0x0002; // AF_INET6. + + break; + } + } + + // Override most significant bit with protocol information: + // - If UDP, start with 0. + // - If TCP, start with 1. + if (this->protocol == Protocol::UDP) + { + this->hash |= 0x0000; + } + else + { + this->hash |= 0x0001; + } + } public: uint64_t hash{ 0u }; diff --git a/worker/src/RTC/TransportTuple.cpp b/worker/src/RTC/TransportTuple.cpp index 968beb5f06..ab8dce24b2 100644 --- a/worker/src/RTC/TransportTuple.cpp +++ b/worker/src/RTC/TransportTuple.cpp @@ -82,75 +82,4 @@ namespace RTC MS_DUMP(""); } - - // TODO: Move to .hpp once done. - /* - * Hash for IPv4 - * - 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | PORT | IP | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | IP | |F|P| - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * - * Hash for IPv6 - * - 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | PORT | IP[0] ^ IP[1] ^ IP[2] ^ IP[3]| - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - |IP[0] ^ IP[1] ^ IP[2] ^ IP[3] | IP[0] >> 16 |F|P| - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - */ - void TransportTuple::SetHash() - { - const struct sockaddr* remoteSockAddr = GetRemoteAddress(); - - switch (remoteSockAddr->sa_family) - { - case AF_INET: - { - auto* remoteSockAddrIn = reinterpret_cast(remoteSockAddr); - - const uint64_t address = ntohl(remoteSockAddrIn->sin_addr.s_addr); - const uint64_t port = (ntohs(remoteSockAddrIn->sin_port)); - - this->hash = port << 48; - this->hash |= address << 16; - this->hash |= 0x0000; // AF_INET. - - break; - } - - case AF_INET6: - { - auto* remoteSockAddrIn6 = reinterpret_cast(remoteSockAddr); - auto* a = reinterpret_cast(std::addressof(remoteSockAddrIn6->sin6_addr)); - - const auto address1 = a[0] ^ a[1] ^ a[2] ^ a[3]; - const auto address2 = a[0]; - const uint64_t port = ntohs(remoteSockAddrIn6->sin6_port); - - this->hash = port << 48; - this->hash |= static_cast(address1) << 16; - this->hash |= address2 >> 16 & 0xFFFC; - this->hash |= 0x0002; // AF_INET6. - - break; - } - } - - // Override most significant bit with protocol information: - // - If UDP, start with 0. - // - If TCP, start with 1. - if (this->protocol == Protocol::UDP) - { - this->hash |= 0x0000; - } - else - { - this->hash |= 0x0001; - } - } } // namespace RTC From 9aa2a641b6f7c948c4bbc110f5600337b57b74a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Tue, 21 Jun 2022 20:05:39 +0200 Subject: [PATCH 11/12] cosmetic --- worker/include/RTC/TransportTuple.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/worker/include/RTC/TransportTuple.hpp b/worker/include/RTC/TransportTuple.hpp index b6f98895dd..c1430513f4 100644 --- a/worker/include/RTC/TransportTuple.hpp +++ b/worker/include/RTC/TransportTuple.hpp @@ -189,7 +189,7 @@ namespace RTC } } - // Override most significant bit with protocol information: + // Override least significant bit with protocol information: // - If UDP, start with 0. // - If TCP, start with 1. if (this->protocol == Protocol::UDP) From 691e7899103b95ec95a2eb1bba3e50448c5c4e43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Tue, 21 Jun 2022 21:13:51 +0200 Subject: [PATCH 12/12] lint --- worker/include/RTC/TransportTuple.hpp | 117 +++++++++++++------------- 1 file changed, 58 insertions(+), 59 deletions(-) diff --git a/worker/include/RTC/TransportTuple.hpp b/worker/include/RTC/TransportTuple.hpp index c1430513f4..535d221a68 100644 --- a/worker/include/RTC/TransportTuple.hpp +++ b/worker/include/RTC/TransportTuple.hpp @@ -131,76 +131,75 @@ namespace RTC } private: - - /* - * Hash for IPv4 - * - 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | PORT | IP | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | IP | |F|P| - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * - * Hash for IPv6 - * - 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | PORT | IP[0] ^ IP[1] ^ IP[2] ^ IP[3]| - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - |IP[0] ^ IP[1] ^ IP[2] ^ IP[3] | IP[0] >> 16 |F|P| - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - */ - void SetHash() - { - const struct sockaddr* remoteSockAddr = GetRemoteAddress(); - - switch (remoteSockAddr->sa_family) - { - case AF_INET: + /* + * Hash for IPv4 + * + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | PORT | IP | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | IP | |F|P| + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * Hash for IPv6 + * + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | PORT | IP[0] ^ IP[1] ^ IP[2] ^ IP[3]| + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + |IP[0] ^ IP[1] ^ IP[2] ^ IP[3] | IP[0] >> 16 |F|P| + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ + void SetHash() + { + const struct sockaddr* remoteSockAddr = GetRemoteAddress(); + + switch (remoteSockAddr->sa_family) { - auto* remoteSockAddrIn = reinterpret_cast(remoteSockAddr); + case AF_INET: + { + auto* remoteSockAddrIn = reinterpret_cast(remoteSockAddr); - const uint64_t address = ntohl(remoteSockAddrIn->sin_addr.s_addr); - const uint64_t port = (ntohs(remoteSockAddrIn->sin_port)); + const uint64_t address = ntohl(remoteSockAddrIn->sin_addr.s_addr); + const uint64_t port = (ntohs(remoteSockAddrIn->sin_port)); - this->hash = port << 48; - this->hash |= address << 16; - this->hash |= 0x0000; // AF_INET. + this->hash = port << 48; + this->hash |= address << 16; + this->hash |= 0x0000; // AF_INET. - break; - } + break; + } - case AF_INET6: - { - auto* remoteSockAddrIn6 = reinterpret_cast(remoteSockAddr); - auto* a = reinterpret_cast(std::addressof(remoteSockAddrIn6->sin6_addr)); + case AF_INET6: + { + auto* remoteSockAddrIn6 = reinterpret_cast(remoteSockAddr); + auto* a = reinterpret_cast(std::addressof(remoteSockAddrIn6->sin6_addr)); - const auto address1 = a[0] ^ a[1] ^ a[2] ^ a[3]; - const auto address2 = a[0]; - const uint64_t port = ntohs(remoteSockAddrIn6->sin6_port); + const auto address1 = a[0] ^ a[1] ^ a[2] ^ a[3]; + const auto address2 = a[0]; + const uint64_t port = ntohs(remoteSockAddrIn6->sin6_port); - this->hash = port << 48; - this->hash |= static_cast(address1) << 16; - this->hash |= address2 >> 16 & 0xFFFC; - this->hash |= 0x0002; // AF_INET6. + this->hash = port << 48; + this->hash |= static_cast(address1) << 16; + this->hash |= address2 >> 16 & 0xFFFC; + this->hash |= 0x0002; // AF_INET6. - break; + break; + } } - } - // Override least significant bit with protocol information: - // - If UDP, start with 0. - // - If TCP, start with 1. - if (this->protocol == Protocol::UDP) - { - this->hash |= 0x0000; - } - else - { - this->hash |= 0x0001; + // Override least significant bit with protocol information: + // - If UDP, start with 0. + // - If TCP, start with 1. + if (this->protocol == Protocol::UDP) + { + this->hash |= 0x0000; + } + else + { + this->hash |= 0x0001; + } } - } public: uint64_t hash{ 0u };