diff --git a/CMakeLists.txt b/CMakeLists.txt index c6a8903..c9c6b02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,6 +100,16 @@ endforeach() file(GLOB_RECURSE COMMON_SRC CONFIGURE_DEPENDS src/common/*.cpp) add_library(common STATIC ${COMMON_SRC}) target_link_libraries(common PUBLIC sodium cxxopts::cxxopts) + +if (WIN32) + target_link_libraries(common PUBLIC + ws2_32 + iphlpapi + advapi32 + mswsock + ) +endif() + target_include_directories(common PUBLIC ${PROJECT_SOURCE_DIR}/include ${sodium_SOURCE_DIR}/src/libsodium/include @@ -114,6 +124,11 @@ target_compile_definitions(common PUBLIC ASIO_STANDALONE) file(GLOB_RECURSE CLIENT_SRC CONFIGURE_DEPENDS src/client/*.cpp) add_executable(client ${CLIENT_SRC}) target_link_libraries(client PRIVATE common sodium cxxopts::cxxopts) +if (WIN32) + target_link_libraries(client PRIVATE + dbghelp + ) +endif() target_include_directories(client PRIVATE ${PROJECT_SOURCE_DIR}/include ${sodium_SOURCE_DIR}/src/libsodium/include @@ -129,6 +144,11 @@ set_target_properties(client PROPERTIES OUTPUT_NAME "columnlynx_client") file(GLOB_RECURSE SERVER_SRC CONFIGURE_DEPENDS src/server/*.cpp) add_executable(server ${SERVER_SRC}) target_link_libraries(server PRIVATE common sodium cxxopts::cxxopts) +if (WIN32) + target_link_libraries(server PRIVATE + dbghelp + ) +endif() target_include_directories(server PRIVATE ${PROJECT_SOURCE_DIR}/include ${sodium_SOURCE_DIR}/src/libsodium/include diff --git a/include/columnlynx/common/net/virtual_interface.hpp b/include/columnlynx/common/net/virtual_interface.hpp index 299bbe9..baecd11 100644 --- a/include/columnlynx/common/net/virtual_interface.hpp +++ b/include/columnlynx/common/net/virtual_interface.hpp @@ -29,6 +29,7 @@ #include #elif defined(_WIN32) #define WIN32_LEAN_AND_MEAN + #define WINTUN_STATIC #include #include #include diff --git a/src/client/main.cpp b/src/client/main.cpp index b18bba5..1f73ac6 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -27,11 +27,13 @@ void signalHandler(int signum) { int main(int argc, char** argv) { // Capture SIGINT and SIGTERM for graceful shutdown +#if !defined(_WIN32) struct sigaction action; memset(&action, 0, sizeof(struct sigaction)); action.sa_handler = signalHandler; sigaction(SIGINT, &action, nullptr); sigaction(SIGTERM, &action, nullptr); +#endif PanicHandler::init(); @@ -67,7 +69,7 @@ int main(int argc, char** argv) { log("This software is licensed under the GPLv2 only OR the GPLv3. See LICENSES/ for details."); #if defined(__WIN32__) - WintunInitialize(); + //WintunInitialize(); #endif std::shared_ptr tun = std::make_shared(optionsObj["interface"].as()); diff --git a/src/client/net/tcp/tcp_client.cpp b/src/client/net/tcp/tcp_client.cpp index d2b05e3..dd90faa 100644 --- a/src/client/net/tcp/tcp_client.cpp +++ b/src/client/net/tcp/tcp_client.cpp @@ -3,7 +3,7 @@ // Distributed under the terms of the GNU General Public License, either version 2 only or version 3. See LICENSES/ for details. #include -#include +//#include namespace ColumnLynx::Net::TCP { void TCPClient::start() { @@ -26,9 +26,13 @@ namespace ColumnLynx::Net::TCP { Utils::log("Sending handshake init to server."); // Check if hostname or IPv4/IPv6 - sockaddr_in addr4{}; - sockaddr_in6 addr6{}; - self->mIsHostDomain = inet_pton(AF_INET, mHost.c_str(), (void*)(&addr4)) != 1 && inet_pton(AF_INET6, mHost.c_str(), (void*)(&addr6)) != 1; // Voodoo black magic + try { + asio::ip::make_address(mHost); + self->mIsHostDomain = false; // IPv4 or IPv6 literal + } catch (const asio::system_error&) { + self->mIsHostDomain = true; // hostname / domain + } + std::vector payload; payload.reserve(1 + crypto_box_PUBLICKEYBYTES); diff --git a/src/common/virtual_interface.cpp b/src/common/virtual_interface.cpp index 800ec19..d2caf0f 100644 --- a/src/common/virtual_interface.cpp +++ b/src/common/virtual_interface.cpp @@ -10,37 +10,43 @@ static HMODULE gWintun = nullptr; -static WintunOpenAdapterFn WintunOpenAdapter; -static WintunStartSessionFn WintunStartSession; -static WintunEndSessionFn WintunEndSession; -static WintunGetReadWaitEventFn WintunGetReadWaitEvent; -static WintunReceivePacketFn WintunReceivePacket; -static WintunReleaseReceivePacketFn WintunReleaseReceivePacket; -static WintunAllocateSendPacketFn WintunAllocateSendPacket; -static WintunSendPacketFn WintunSendPacket; +static WINTUN_OPEN_ADAPTER_FUNC* pWintunOpenAdapter; +static WINTUN_START_SESSION_FUNC* pWintunStartSession; +static WINTUN_END_SESSION_FUNC* pWintunEndSession; +static WINTUN_GET_READ_WAIT_EVENT_FUNC* pWintunGetReadWaitEvent; +static WINTUN_RECEIVE_PACKET_FUNC* pWintunReceivePacket; +static WINTUN_RELEASE_RECEIVE_PACKET_FUNC* pWintunReleaseReceivePacket; +static WINTUN_ALLOCATE_SEND_PACKET_FUNC* pWintunAllocateSendPacket; +static WINTUN_SEND_PACKET_FUNC* pWintunSendPacket; static void InitializeWintun() { if (gWintun) return; - gWintun = LoadLibraryExW(L"wintun.dll", nullptr, - LOAD_LIBRARY_SEARCH_APPLICATION_DIR); + gWintun = LoadLibraryExW( + L"wintun.dll", + nullptr, + LOAD_LIBRARY_SEARCH_APPLICATION_DIR + ); + if (!gWintun) throw std::runtime_error("Failed to load wintun.dll"); -#define RESOLVE(name) \ - name = (name##Fn)GetProcAddress(gWintun, #name); \ - if (!name) throw std::runtime_error("Missing Wintun symbol: " #name); +#define RESOLVE(name, type) \ + p##name = reinterpret_cast( \ + GetProcAddress(gWintun, #name)); \ + if (!p##name) \ + throw std::runtime_error("Missing Wintun symbol: " #name); - RESOLVE(WintunOpenAdapter) - RESOLVE(WintunStartSession) - RESOLVE(WintunEndSession) - RESOLVE(WintunGetReadWaitEvent) - RESOLVE(WintunReceivePacket) - RESOLVE(WintunReleaseReceivePacket) - RESOLVE(WintunAllocateSendPacket) - RESOLVE(WintunSendPacket) + RESOLVE(WintunOpenAdapter, WINTUN_OPEN_ADAPTER_FUNC) + RESOLVE(WintunStartSession, WINTUN_START_SESSION_FUNC) + RESOLVE(WintunEndSession, WINTUN_END_SESSION_FUNC) + RESOLVE(WintunGetReadWaitEvent, WINTUN_GET_READ_WAIT_EVENT_FUNC) + RESOLVE(WintunReceivePacket, WINTUN_RECEIVE_PACKET_FUNC) + RESOLVE(WintunReleaseReceivePacket, WINTUN_RELEASE_RECEIVE_PACKET_FUNC) + RESOLVE(WintunAllocateSendPacket, WINTUN_ALLOCATE_SEND_PACKET_FUNC) + RESOLVE(WintunSendPacket, WINTUN_SEND_PACKET_FUNC) #undef RESOLVE } @@ -107,19 +113,18 @@ namespace ColumnLynx::Net { InitializeWintun(); - mAdapter = WintunOpenAdapter( - L"ColumnLynx", + mAdapter = pWintunOpenAdapter( std::wstring(ifName.begin(), ifName.end()).c_str() ); if (!mAdapter) throw std::runtime_error("Wintun adapter not found"); - mSession = WintunStartSession(mAdapter, 0x200000); + mSession = pWintunStartSession(mAdapter, 0x200000); if (!mSession) throw std::runtime_error("Failed to start Wintun session"); - mHandle = WintunGetReadWaitEvent(mSession); + mHandle = pWintunGetReadWaitEvent(mSession); mFd = -1; #else @@ -134,7 +139,7 @@ namespace ColumnLynx::Net { close(mFd); #elif defined(_WIN32) if (mSession) - WintunEndSession(mSession); + pWintunEndSession(mSession); #endif } @@ -200,14 +205,13 @@ namespace ColumnLynx::Net { #elif defined(_WIN32) - WINTUN_PACKET* packet = WintunReceivePacket(mSession, nullptr); + DWORD size = 0; + BYTE* packet = pWintunReceivePacket(mSession, &size); if (!packet) return {}; - std::vector buf(packet->Data, - packet->Data + packet->Length); - - WintunReleaseReceivePacket(mSession, packet); + std::vector buf(packet, packet + size); + pWintunReleaseReceivePacket(mSession, packet); return buf; #else @@ -252,15 +256,16 @@ namespace ColumnLynx::Net { #elif defined(_WIN32) - WINTUN_PACKET* tx = - WintunAllocateSendPacket(mSession, - static_cast(packet.size())); + BYTE* tx = pWintunAllocateSendPacket( + mSession, + static_cast(packet.size()) + ); if (!tx) throw std::runtime_error("WintunAllocateSendPacket failed"); - - memcpy(tx->Data, packet.data(), packet.size()); - WintunSendPacket(mSession, tx); + + memcpy(tx, packet.data(), packet.size()); + pWintunSendPacket(mSession, tx); #endif } diff --git a/src/server/main.cpp b/src/server/main.cpp index 3b6214e..080018b 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -32,11 +32,13 @@ void signalHandler(int signum) { int main(int argc, char** argv) { // Capture SIGINT and SIGTERM for graceful shutdown +#if !defined(_WIN32) struct sigaction action; memset(&action, 0, sizeof(struct sigaction)); action.sa_handler = signalHandler; sigaction(SIGINT, &action, nullptr); sigaction(SIGTERM, &action, nullptr); +#endif cxxopts::Options options("columnlynx_server", "ColumnLynx Server Application"); @@ -68,7 +70,7 @@ int main(int argc, char** argv) { log("This software is licensed under the GPLv2 only OR the GPLv3. See LICENSES/ for details."); #if defined(__WIN32__) - WintunInitialize(); + //WintunInitialize(); #endif std::unordered_map config = Utils::getConfigMap(optionsObj["config"].as());