Skip to content

Commit

Permalink
more relay stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Siarhei Dziki committed Mar 2, 2024
1 parent fad4be1 commit 73e0b10
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
43 changes: 35 additions & 8 deletions src/relay/relay.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
bool relay::init()
{
m_socket = udpsocketFactory::createUdpSocket();
if (!m_socket->isValid())
if (m_socket == nullptr || !m_socket->isValid())
{
LOG(Error, "Failed to create socket!");
return false;
Expand All @@ -26,14 +26,24 @@ bool relay::init()
return true;
}

channel& relay::createChannel(const guid& inGuid)
{
channel newChannel{};
newChannel.m_guid = inGuid;

LOG(Display, "Created channel with guid: {0}", inGuid.toString());

return *m_channels.emplace_after(m_channels.before_begin(), newChannel);
}

bool relay::run()
{
if (!init())
return false;

LOG(Display, "Relay initialized and running on port {0}", mainPort);

std::unique_ptr<internetaddr> recvAddr = udpsocketFactory::createInternetAddrUnique();
std::shared_ptr<internetaddr> recvAddr = udpsocketFactory::createInternetAddrUnique();

std::array<uint8_t, 1024> buffer{};
m_running = true;
Expand All @@ -54,20 +64,37 @@ bool relay::run()
// continue;
//}

if (bytesRead == 1024)
if (bytesRead == 1024) // const handshake packet size
{
const handshake_header* header = reinterpret_cast<const handshake_header*>(buffer.data());

LOG(Verbose, "Received header: type: {0}, length: {1} from {2}", header->type, header->length, recvAddr->toString());
LOG(Verbose, "Received header: type: {0}, length: {1} from {2}", header->m_type, header->m_length, recvAddr->toString());

auto nthType = NETWORK_TO_HOST_16(header->type);
auto nthLength = NETWORK_TO_HOST_16(header->length);
auto nthType = NETWORK_TO_HOST_16(header->m_type);
auto nthLength = NETWORK_TO_HOST_16(header->m_length);

LOG(Verbose, "Swapped header: type: {0}, length: {1} from {2}", nthType, nthLength, recvAddr->toString());

if (nthType == 0x01 && nthLength == 1024)
if (nthType == 0x01 && nthLength == 992) // const handshake packet values
{
LOG(Display, "you win!");
const auto& recvGuid = header->m_id;
const guid htnGuid = guid(NETWORK_TO_HOST_16(recvGuid.m_a), NETWORK_TO_HOST_16(recvGuid.m_b), NETWORK_TO_HOST_16(recvGuid.m_c), NETWORK_TO_HOST_16(recvGuid.m_d));

auto guidChannel = m_guidMappedChannels.find(htnGuid);
if (guidChannel == m_guidMappedChannels.end())
{
channel& newChannel = createChannel(htnGuid);
newChannel.m_peerA = recvAddr;
}
else if (*guidChannel->second.m_peerA != *recvAddr)
{
guidChannel->second.m_peerB = recvAddr;

LOG(Display, "Creating relay for session: {0} with peers: {1}, {2}.", htnGuid.toString(), guidChannel->second.m_peerA, guidChannel->second.m_peerB);

m_addressMappedChannels.emplace(guidChannel->second.m_peerA, guidChannel->second);
m_addressMappedChannels.emplace(guidChannel->second.m_peerB, guidChannel->second);
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/relay/relay.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public:
private:
bool init();

channel& createChannel(const guid& inGuid);

std::unique_ptr<udpsocket> m_socket{};

std::forward_list<channel> m_channels{};

std::unordered_map<guid, const channel&> m_guidMappedChannels{};
std::unordered_map<guid, channel&> m_guidMappedChannels{};

std::unordered_map<std::shared_ptr<internetaddr>, const channel&> m_addressMappedChannels{};

Expand Down
30 changes: 23 additions & 7 deletions src/relay/types.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,35 @@

struct guid
{
int32_t a{};
int32_t b{};
int32_t c{};
int32_t d{};
guid() = default;
guid(int32_t a, int32_t b, int32_t c, int32_t d)
: m_a{a}
, m_b{b}
, m_c{c}
, m_d{d}
{
}

int32_t m_a{};
int32_t m_b{};
int32_t m_c{};
int32_t m_d{};

bool operator<=>(const guid&) const = default;

std::string toString() const
{
char buffer[36]{};
std::snprintf(buffer, sizeof(buffer), "%08x-%04x-%04x-%04x-%04x%08x", m_a, m_b >> 16, m_b & 0xFFFF, m_c >> 16, m_c & 0xFFFF, m_d);
return std::string(buffer);
}
};

struct handshake_header
{
uint16_t type{};
uint16_t length{};
guid id{};
uint16_t m_type{};
uint16_t m_length{};
guid m_id{};
};

// custom hash for guid
Expand Down

0 comments on commit 73e0b10

Please sign in to comment.