Added protocol version to TCP header, also some launch args

This commit is contained in:
2025-11-09 02:26:44 +01:00
parent 098afeb4d8
commit 2cbcf70c56
11 changed files with 3124 additions and 10 deletions

View File

@@ -9,6 +9,7 @@
#include <columnlynx/common/panic_handler.hpp>
#include <columnlynx/client/net/tcp/tcp_client.hpp>
#include <columnlynx/client/net/udp/udp_client.hpp>
#include <cxxopts/cxxopts.hpp>
using asio::ip::tcp;
using namespace ColumnLynx::Utils;
@@ -32,6 +33,22 @@ int main(int argc, char** argv) {
PanicHandler::init();
cxxopts::Options options("columnlynx_client", "ColumnLynx Client Application");
options.add_options()
("h,help", "Print help")
("s,server", "Server address", cxxopts::value<std::string>()->default_value("127.0.0.1"))
("p,port", "Server port", cxxopts::value<uint16_t>()->default_value(std::to_string(serverPort())));
auto result = options.parse(argc, argv);
if (result.count("help")) {
std::cout << options.help() << std::endl;
return 0;
}
auto host = result["server"].as<std::string>();
auto port = std::to_string(result["port"].as<uint16_t>());
try {
log("ColumnLynx Client, Version " + getVersion());
log("This software is licensed under the GPLv3. See LICENSE for details.");
@@ -42,8 +59,8 @@ int main(int argc, char** argv) {
uint64_t sessionID = 0;
asio::io_context io;
auto client = std::make_shared<ColumnLynx::Net::TCP::TCPClient>(io, "127.0.0.1", std::to_string(serverPort()), &sodiumWrapper, &aesKey, &sessionID);
auto udpClient = std::make_shared<ColumnLynx::Net::UDP::UDPClient>(io, "127.0.0.1", std::to_string(serverPort()), &aesKey, &sessionID);
auto client = std::make_shared<ColumnLynx::Net::TCP::TCPClient>(io, host, port, &sodiumWrapper, &aesKey, &sessionID);
auto udpClient = std::make_shared<ColumnLynx::Net::UDP::UDPClient>(io, host, port, &aesKey, &sessionID);
client->start();
udpClient->start();
@@ -54,7 +71,7 @@ int main(int argc, char** argv) {
});
ioThread.detach();
log("Client connected to 127.0.0.1:" + std::to_string(serverPort()));
log("Client connected to " + host + ":" + port);
// Client is running
while (!done) {
@@ -71,6 +88,7 @@ int main(int argc, char** argv) {
}
}
log("Client shutting down.");
udpClient->stop();
client->disconnect();
io.stop();
ioThread.join();

View File

@@ -44,6 +44,10 @@ namespace ColumnLynx::Utils {
return 48042;
}
unsigned char protocolVersion() {
return 1;
}
std::string bytesToHexString(const uint8_t* bytes, size_t length) {
const char hexChars[] = "0123456789ABCDEF";
std::string hexString;

View File

@@ -65,11 +65,12 @@ namespace ColumnLynx::Net::UDP {
// Update recv counter
const_cast<SessionState*>(session.get())->recv_ctr.fetch_add(1, std::memory_order_relaxed);
// TODO: Process the packet payload
// For now, just log the decrypted payload
std::string payloadStr(plaintext.begin(), plaintext.end());
Utils::log("UDP: Received packet from " + mRemoteEndpoint.address().to_string() + " - Payload: " + payloadStr);
// TODO: Process the packet payload, for now just echo back
mSendData(sessionID, std::string(plaintext.begin(), plaintext.end()));
} catch (...) {
Utils::warn("UDP: Failed to decrypt payload from " + mRemoteEndpoint.address().to_string());
return;
@@ -78,6 +79,44 @@ namespace ColumnLynx::Net::UDP {
void UDPServer::mSendData(const uint64_t sessionID, const std::string& data) {
// TODO: Implement
// Find the IPv4/IPv6 endpoint for the session
std::shared_ptr<const SessionState> session = SessionRegistry::getInstance().get(sessionID);
if (!session) {
Utils::warn("UDP: Cannot send data, unknown session ID " + std::to_string(sessionID));
return;
}
asio::ip::udp::endpoint endpoint = session->udpEndpoint;
if (endpoint.address().is_unspecified()) {
Utils::warn("UDP: Cannot send data, session ID " + std::to_string(sessionID) + " has no known UDP endpoint.");
return;
}
// Prepare packet
UDPPacketHeader hdr{};
randombytes_buf(hdr.nonce.data(), hdr.nonce.size());
auto encryptedPayload = Utils::LibSodiumWrapper::encryptMessage(
reinterpret_cast<const uint8_t*>(data.data()), data.size(),
session->aesKey, hdr.nonce, "udp-data"
);
std::vector<uint8_t> packet;
packet.reserve(sizeof(UDPPacketHeader) + sizeof(uint64_t) + encryptedPayload.size());
packet.insert(packet.end(),
reinterpret_cast<uint8_t*>(&hdr),
reinterpret_cast<uint8_t*>(&hdr) + sizeof(UDPPacketHeader)
);
packet.insert(packet.end(),
reinterpret_cast<const uint8_t*>(&sessionID),
reinterpret_cast<const uint8_t*>(&sessionID) + sizeof(sessionID)
);
packet.insert(packet.end(), encryptedPayload.begin(), encryptedPayload.end());
// Send packet
mSocket.send_to(asio::buffer(packet), endpoint);
Utils::log("UDP: Sent packet of size " + std::to_string(packet.size()) + " to " + std::to_string(sessionID) + " (" + endpoint.address().to_string() + ":" + std::to_string(endpoint.port()) + ")");
}
void UDPServer::stop() {