diff --git a/CMakeLists.txt b/CMakeLists.txt index bca8dbd..efa5898 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.16) # If MAJOR is 0, and MINOR > 0, Version is BETA project(ColumnLynx - VERSION 0.1.0 + VERSION 0.2.0 LANGUAGES CXX ) diff --git a/include/columnlynx/common/net/virtual_interface.hpp b/include/columnlynx/common/net/virtual_interface.hpp index dca574d..87cfaec 100644 --- a/include/columnlynx/common/net/virtual_interface.hpp +++ b/include/columnlynx/common/net/virtual_interface.hpp @@ -44,6 +44,8 @@ namespace ColumnLynx::Net { bool configureIP(uint32_t clientIP, uint32_t serverIP, uint8_t prefixLen, uint16_t mtu); + void resetIP(); + std::vector readPacket(); void writePacket(const std::vector& packet); diff --git a/include/columnlynx/server/net/tcp/tcp_connection.hpp b/include/columnlynx/server/net/tcp/tcp_connection.hpp index 4b93140..212a470 100644 --- a/include/columnlynx/server/net/tcp/tcp_connection.hpp +++ b/include/columnlynx/server/net/tcp/tcp_connection.hpp @@ -42,7 +42,7 @@ namespace ColumnLynx::Net::TCP { // Set callback for disconnects void setDisconnectCallback(std::function)> cb); // Disconnect the client - void disconnect(); + void disconnect(bool echo = true); // Get the assigned session ID uint64_t getSessionID() const; diff --git a/src/client/net/tcp/tcp_client.cpp b/src/client/net/tcp/tcp_client.cpp index ef03e3a..d2b05e3 100644 --- a/src/client/net/tcp/tcp_client.cpp +++ b/src/client/net/tcp/tcp_client.cpp @@ -269,6 +269,12 @@ namespace ColumnLynx::Net::TCP { disconnect(false); } break; + case ServerMessageType::KILL_CONNECTION: + Utils::warn("Server is killing the connection: " + data); + if (mConnected) { + disconnect(false); + } + break; default: Utils::log("Received unknown message type from server."); break; diff --git a/src/common/utils.cpp b/src/common/utils.cpp index 73c7c51..c7ec622 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -49,7 +49,7 @@ namespace ColumnLynx::Utils { } std::string getVersion() { - return "b0.1"; + return "b0.2"; } unsigned short serverPort() { diff --git a/src/common/virtual_interface.cpp b/src/common/virtual_interface.cpp index e2a860e..01da960 100644 --- a/src/common/virtual_interface.cpp +++ b/src/common/virtual_interface.cpp @@ -237,7 +237,38 @@ namespace ColumnLynx::Net { return false; #endif } - + + void VirtualInterface::resetIP() { + #if defined(__linux__) + char cmd[512]; + snprintf(cmd, sizeof(cmd), + "ip addr flush dev %s", + mIfName.c_str() + ); + system(cmd); + #elif defined(__APPLE__) + char cmd[512]; + snprintf(cmd, sizeof(cmd), + "ifconfig %s inet 0.0.0.0 delete", + mIfName.c_str() + ); + system(cmd); + + snprintf(cmd, sizeof(cmd), + "ifconfig %s inet6 :: delete", + mIfName.c_str() + ); + system(cmd); + #elif defined(_WIN32) + char cmd[256]; + snprintf(cmd, sizeof(cmd), + "netsh interface ip set address name=\"%s\" dhcp", + mIfName.c_str() + ); + system(cmd); + #endif + } + // ------------------------------------------------------------ // Linux // ------------------------------------------------------------ diff --git a/src/server/net/tcp/tcp_connection.cpp b/src/server/net/tcp/tcp_connection.cpp index 34d7094..f6b28f6 100644 --- a/src/server/net/tcp/tcp_connection.cpp +++ b/src/server/net/tcp/tcp_connection.cpp @@ -32,14 +32,23 @@ namespace ColumnLynx::Net::TCP { mOnDisconnect = std::move(cb); } - void TCPConnection::disconnect() { + void TCPConnection::disconnect(bool echo) { std::string ip = mHandler->socket().remote_endpoint().address().to_string(); - mHandler->sendMessage(ServerMessageType::GRACEFUL_DISCONNECT, "Server initiated disconnect."); + if (echo) { + mHandler->sendMessage(ServerMessageType::GRACEFUL_DISCONNECT, "Server initiated disconnect."); + } mHeartbeatTimer.cancel(); asio::error_code ec; mHandler->socket().shutdown(asio::ip::tcp::socket::shutdown_both, ec); + if (ec) { + Utils::error("Error during socket shutdown: " + ec.message()); + } + mHandler->socket().close(ec); + if (ec) { + Utils::error("Error during socket close: " + ec.message()); + } SessionRegistry::getInstance().erase(mConnectionSessionID); SessionRegistry::getInstance().deallocIP(mConnectionSessionID); @@ -272,6 +281,11 @@ namespace ColumnLynx::Net::TCP { disconnect(); break; } + case ClientMessageType::KILL_CONNECTION: { + Utils::warn("Received KILL_CONNECTION from " + reqAddr + ": " + data); + disconnect(); + break; + } default: Utils::warn("Unhandled message type from " + reqAddr); break;