Skip to content

Commit

Permalink
fix hash and cosmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc committed Apr 7, 2024
1 parent 5c48e0b commit 6ece7ac
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 170 deletions.
7 changes: 3 additions & 4 deletions node/src/Channel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as os from 'node:os';
import { Duplex } from 'node:stream';
import { info, warn } from 'node:console';
import * as flatbuffers from 'flatbuffers';
import { Logger } from './Logger';
import { EnhancedEventEmitter } from './EnhancedEventEmitter';
Expand Down Expand Up @@ -156,8 +157,7 @@ export class Channel extends EnhancedEventEmitter {
}

default: {
// eslint-disable-next-line no-console
console.warn(
warn(
`worker[pid:${pid}] unexpected data: ${payload.toString(
'utf8',
1
Expand Down Expand Up @@ -482,8 +482,7 @@ export class Channel extends EnhancedEventEmitter {

// 'X' (a dump log).
case 'X': {
// eslint-disable-next-line no-console
console.log(logData.slice(1));
info(logData.slice(1));

break;
}
Expand Down
19 changes: 9 additions & 10 deletions worker/include/RTC/PortManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace RTC
class PortManager
{
private:
enum class Transport : uint8_t
enum class Protocol : uint8_t
{
UDP = 1,
TCP
Expand All @@ -35,7 +35,7 @@ namespace RTC
public:
static uv_udp_t* BindUdp(std::string& ip, uint16_t port, RTC::Transport::SocketFlags& flags)
{
return reinterpret_cast<uv_udp_t*>(Bind(Transport::UDP, ip, port, flags));
return reinterpret_cast<uv_udp_t*>(Bind(Protocol::UDP, ip, port, flags));
}
static uv_udp_t* BindUdp(
std::string& ip,
Expand All @@ -44,11 +44,11 @@ namespace RTC
RTC::Transport::SocketFlags& flags,
uint64_t& hash)
{
return reinterpret_cast<uv_udp_t*>(Bind(Transport::UDP, ip, minPort, maxPort, flags, hash));
return reinterpret_cast<uv_udp_t*>(Bind(Protocol::UDP, ip, minPort, maxPort, flags, hash));
}
static uv_tcp_t* BindTcp(std::string& ip, uint16_t port, RTC::Transport::SocketFlags& flags)
{
return reinterpret_cast<uv_tcp_t*>(Bind(Transport::TCP, ip, port, flags));
return reinterpret_cast<uv_tcp_t*>(Bind(Protocol::TCP, ip, port, flags));
}
static uv_tcp_t* BindTcp(
std::string& ip,
Expand All @@ -57,26 +57,25 @@ namespace RTC
RTC::Transport::SocketFlags& flags,
uint64_t& hash)
{
return reinterpret_cast<uv_tcp_t*>(Bind(Transport::TCP, ip, minPort, maxPort, flags, hash));
return reinterpret_cast<uv_tcp_t*>(Bind(Protocol::TCP, ip, minPort, maxPort, flags, hash));
}
static void Unbind(uint64_t hash, uint16_t port);
static void Dump();

private:
static uv_handle_t* Bind(
Transport transport, std::string& ip, uint16_t port, RTC::Transport::SocketFlags& flags);
Protocol protocol, std::string& ip, uint16_t port, RTC::Transport::SocketFlags& flags);
static uv_handle_t* Bind(
Transport transport,
Protocol protocol,
std::string& ip,
uint16_t minPort,
uint16_t maxPort,
RTC::Transport::SocketFlags& flags,
uint64_t& hash);
static uint64_t GeneratePortRangeHash(
Transport transport, sockaddr_storage* bindAddr, uint16_t minPort, uint16_t maxPort);
Protocol protocol, sockaddr_storage* bindAddr, uint16_t minPort, uint16_t maxPort);
static PortRange& GetOrCreatePortRange(uint64_t hash, uint16_t minPort, uint16_t maxPort);
static uint8_t ConvertSocketFlags(
RTC::Transport::SocketFlags& flags, Transport transport, int family);
static uint8_t ConvertSocketFlags(RTC::Transport::SocketFlags& flags, Protocol protocol, int family);

private:
thread_local static absl::flat_hash_map<uint64_t, PortRange> mapPortRanges;
Expand Down
72 changes: 1 addition & 71 deletions worker/include/RTC/TransportTuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,77 +163,7 @@ 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:
{
const auto* remoteSockAddrIn = reinterpret_cast<const struct sockaddr_in*>(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:
{
const auto* remoteSockAddrIn6 =
reinterpret_cast<const struct sockaddr_in6*>(remoteSockAddr);
const auto* a =
reinterpret_cast<const uint32_t*>(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<uint64_t>(address1) << 16;
this->hash |= address2 >> 16 & 0xFFFC;
this->hash |= 0x0002; // AF_INET6.

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;
}
}
void SetHash();

public:
uint64_t hash{ 0u };
Expand Down
Loading

0 comments on commit 6ece7ac

Please sign in to comment.