Added basic UDP handling - only sending from client supported.
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#include <columnlynx/common/net/tcp/tcp_message_handler.hpp>
|
||||
#include <columnlynx/common/utils.hpp>
|
||||
#include <columnlynx/common/libsodium_wrapper.hpp>
|
||||
#include <columnlynx/common/net/session_registry.hpp>
|
||||
|
||||
namespace ColumnLynx::Net::TCP {
|
||||
class TCPConnection : public std::enable_shared_from_this<TCPConnection> {
|
||||
@@ -70,6 +71,14 @@ namespace ColumnLynx::Net::TCP {
|
||||
mOnDisconnect(shared_from_this());
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t getSessionID() const {
|
||||
return mConnectionSessionID;
|
||||
}
|
||||
|
||||
std::array<uint8_t, 32> getAESKey() const {
|
||||
return mConnectionAESKey;
|
||||
}
|
||||
|
||||
private:
|
||||
TCPConnection(asio::ip::tcp::socket socket, Utils::LibSodiumWrapper* sodiumWrapper)
|
||||
@@ -140,6 +149,8 @@ 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
|
||||
std::vector<uint8_t> encryptedSessionID = Utils::LibSodiumWrapper::encryptMessage(
|
||||
@@ -149,6 +160,11 @@ namespace ColumnLynx::Net::TCP {
|
||||
|
||||
mHandler->sendMessage(ServerMessageType::HANDSHAKE_EXCHANGE_KEY_CONFIRM, Utils::uint8ArrayToString(encryptedSessionID.data(), encryptedSessionID.size()));
|
||||
|
||||
// Add to session registry
|
||||
Utils::log("Handshake with " + reqAddr + " completed successfully. Session ID assigned.");
|
||||
auto session = std::make_shared<SessionState>(mConnectionAESKey, std::chrono::hours(12));
|
||||
SessionRegistry::getInstance().put(mConnectionSessionID, std::move(session));
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
Utils::error("Failed to decrypt HANDSHAKE_EXCHANGE_KEY from " + reqAddr + ": " + e.what());
|
||||
disconnect();
|
||||
|
||||
30
include/columnlynx/server/net/udp/udp_server.hpp
Normal file
30
include/columnlynx/server/net/udp/udp_server.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
// udp_server.hpp - UDP Server for ColumnLynx
|
||||
// Copyright (C) 2025 DcruBro
|
||||
// Distributed under the GPLv3 license. See LICENSE for details.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <asio/asio.hpp>
|
||||
#include <columnlynx/common/net/udp/udp_message_type.hpp>
|
||||
#include <columnlynx/common/utils.hpp>
|
||||
#include <array>
|
||||
|
||||
namespace ColumnLynx::Net::UDP {
|
||||
class UDPServer {
|
||||
public:
|
||||
UDPServer(asio::io_context& ioContext, uint16_t port)
|
||||
: mSocket(ioContext, asio::ip::udp::endpoint(asio::ip::udp::v4(), port))
|
||||
{
|
||||
Utils::log("Started UDP server on port " + std::to_string(port));
|
||||
mStartReceive();
|
||||
}
|
||||
|
||||
private:
|
||||
void mStartReceive();
|
||||
void mHandlePacket(std::size_t bytes);
|
||||
void mSendData(const uint64_t sessionID, const std::string& data);
|
||||
asio::ip::udp::socket mSocket;
|
||||
asio::ip::udp::endpoint mRemoteEndpoint;
|
||||
std::array<uint8_t, 2048> mRecvBuffer; // Adjust size as needed
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user