From 022debfa5be737e66e63742db3ee8a1d9571efdb Mon Sep 17 00:00:00 2001 From: DcruBro Date: Sat, 22 Nov 2025 21:53:45 +0100 Subject: [PATCH] Fix byte order for sessionid, add 64-bit conversion helpers - damn you C standard for no htonl/ntohl for 64-bit :( --- include/columnlynx/common/utils.hpp | 19 +++++++++++++++++++ src/client/main.cpp | 4 ++-- src/client/net/tcp/tcp_client.cpp | 3 +++ src/server/server/net/tcp/tcp_connection.cpp | 6 +++--- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/include/columnlynx/common/utils.hpp b/include/columnlynx/common/utils.hpp index 46d77ea..d8026ba 100644 --- a/include/columnlynx/common/utils.hpp +++ b/include/columnlynx/common/utils.hpp @@ -38,4 +38,23 @@ namespace ColumnLynx::Utils { inline std::string uint8ArrayToString(const uint8_t* data, size_t length) { return std::string(reinterpret_cast(data), length); } + + inline constexpr uint64_t bswap64(uint64_t x) { + return ((x & 0x00000000000000FFULL) << 56) | + ((x & 0x000000000000FF00ULL) << 40) | + ((x & 0x0000000000FF0000ULL) << 24) | + ((x & 0x00000000FF000000ULL) << 8) | + ((x & 0x000000FF00000000ULL) >> 8) | + ((x & 0x0000FF0000000000ULL) >> 24) | + ((x & 0x00FF000000000000ULL) >> 40) | + ((x & 0xFF00000000000000ULL) >> 56); + } + + inline constexpr uint64_t htobe64(uint64_t x) { + return bswap64(x); // host -> big-endian (for little-endian hosts) + } + + inline constexpr uint64_t be64toh(uint64_t x) { + return bswap64(x); // big-endian -> host (for little-endian hosts) + } }; \ No newline at end of file diff --git a/src/client/main.cpp b/src/client/main.cpp index a331bf1..fb4a6d3 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -63,7 +63,7 @@ int main(int argc, char** argv) { WintunInitialize(); #endif - std::shared_ptr tun = std::make_shared("utun0"); + std::shared_ptr tun = std::make_shared("utun1"); log("Using virtual interface: " + tun->getName()); LibSodiumWrapper sodiumWrapper = LibSodiumWrapper(); @@ -111,4 +111,4 @@ int main(int argc, char** argv) { } catch (const std::exception& e) { error("Client error: " + std::string(e.what())); } -} \ No newline at end of file +} diff --git a/src/client/net/tcp/tcp_client.cpp b/src/client/net/tcp/tcp_client.cpp index c6185e7..b7dc53c 100644 --- a/src/client/net/tcp/tcp_client.cpp +++ b/src/client/net/tcp/tcp_client.cpp @@ -247,6 +247,9 @@ namespace ColumnLynx::Net::TCP { std::memcpy(&mConnectionSessionID, decrypted.data(), sizeof(mConnectionSessionID)); std::memcpy(&mTunConfig, decrypted.data() + sizeof(mConnectionSessionID), sizeof(Protocol::TunConfig)); + + mConnectionSessionID = Utils::be64toh(mConnectionSessionID); + Utils::log("Connection established with Session ID: " + std::to_string(mConnectionSessionID)); if (mSessionIDRef) { // Copy to the global reference diff --git a/src/server/server/net/tcp/tcp_connection.cpp b/src/server/server/net/tcp/tcp_connection.cpp index 4c0282f..dd902f6 100644 --- a/src/server/server/net/tcp/tcp_connection.cpp +++ b/src/server/server/net/tcp/tcp_connection.cpp @@ -173,8 +173,6 @@ namespace ColumnLynx::Net::TCP { // Make a Session ID randombytes_buf(&mConnectionSessionID, sizeof(mConnectionSessionID)); - // TODO: Make the session ID little-endian for network transmission - // Encrypt the Session ID with the established AES key (using symmetric encryption, nonce can be all zeros for this purpose) Nonce symNonce{}; // All zeros @@ -190,8 +188,10 @@ namespace ColumnLynx::Net::TCP { SessionRegistry::getInstance().lockIP(mConnectionSessionID, clientIP); + uint64_t sessionIDNet = Utils::htobe64(mConnectionSessionID); + std::vector payload(sizeof(uint64_t) + sizeof(tunConfig)); - std::memcpy(payload.data(), &mConnectionSessionID, sizeof(uint64_t)); + std::memcpy(payload.data(), &sessionIDNet, sizeof(uint64_t)); std::memcpy(payload.data() + sizeof(uint64_t), &tunConfig, sizeof(tunConfig)); std::vector encryptedPayload = Utils::LibSodiumWrapper::encryptMessage(