diff --git a/.gitmodules b/.gitmodules index 94f4ebc..57e0e93 100644 --- a/.gitmodules +++ b/.gitmodules @@ -25,3 +25,6 @@ [submodule "libs/libSocks5"] path = libs/libSocks5 url = https://github.com/maxDcb/libSocks5.git +[submodule "libs/libSocketHandler"] + path = libs/libSocketHandler + url = https://github.com/maxDcb/libSocketHandler.git diff --git a/core b/core index b4a6ade..2bcc8ec 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit b4a6ade9b4ff4c09839ca3d55671d4f90ede88b7 +Subproject commit 2bcc8ecd0cfa7ff9c4080c20b35500395678aa5b diff --git a/libs/CMakeLists.txt b/libs/CMakeLists.txt index 1856c0c..271d468 100644 --- a/libs/CMakeLists.txt +++ b/libs/CMakeLists.txt @@ -1,4 +1,4 @@ -add_subdirectory(libWindowsSocketHandler) +add_subdirectory(libSocketHandler) add_subdirectory(libPipeHandler) add_subdirectory(libDns) add_subdirectory(libSocks5) diff --git a/libs/libSocketHandler b/libs/libSocketHandler new file mode 160000 index 0000000..3d9300c --- /dev/null +++ b/libs/libSocketHandler @@ -0,0 +1 @@ +Subproject commit 3d9300c78377ef8331c50716d9a315252cd2ff6f diff --git a/libs/libWindowsSocketHandler/.vscode/settings.json b/libs/libWindowsSocketHandler/.vscode/settings.json deleted file mode 100644 index d90974a..0000000 --- a/libs/libWindowsSocketHandler/.vscode/settings.json +++ /dev/null @@ -1,53 +0,0 @@ -{ - "files.associations": { - "functional": "cpp", - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "chrono": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "ratio": "cpp", - "string": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "fstream": "cpp", - "initializer_list": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "new": "cpp", - "ostream": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "thread": "cpp", - "typeinfo": "cpp" - } -} \ No newline at end of file diff --git a/libs/libWindowsSocketHandler/CMakeLists.txt b/libs/libWindowsSocketHandler/CMakeLists.txt deleted file mode 100644 index 7b78257..0000000 --- a/libs/libWindowsSocketHandler/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -cmake_minimum_required(VERSION 3.5) -project(SocketHandler VERSION 1.0.0 LANGUAGES CXX) - -set(DEFAULT_BUILD_TYPE "Release") -set(CMAKE_CXX_STANDARD 14) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -set(SOURCE_FILES -src/SocketHandler.cpp -) - -include_directories(../src) - -add_library(${PROJECT_NAME} STATIC ${SOURCE_FILES}) -set_property(TARGET ${PROJECT_NAME} PROPERTY - MSVC_RUNTIME_LIBRARY "MultiThreaded") -target_link_libraries(${PROJECT_NAME}) -target_include_directories(${PROJECT_NAME} PUBLIC src) - -add_subdirectory(tests) \ No newline at end of file diff --git a/libs/libWindowsSocketHandler/README.md b/libs/libWindowsSocketHandler/README.md deleted file mode 100644 index 50d1ce1..0000000 --- a/libs/libWindowsSocketHandler/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# WindowsSocketHandler - -## Requirement - -Client can connect and disconnect at any time. - -Server can connect and disconnect at any time. - -Multiple client should be able to connect to the same server. - -Client and Server are monothread. diff --git a/libs/libWindowsSocketHandler/src/SocketHandler.cpp b/libs/libWindowsSocketHandler/src/SocketHandler.cpp deleted file mode 100644 index ccb963a..0000000 --- a/libs/libWindowsSocketHandler/src/SocketHandler.cpp +++ /dev/null @@ -1,406 +0,0 @@ -#include "SocketHandler.hpp" - -#pragma comment(lib, "ws2_32.lib") - - -#define SPDLOG_LEVEL_TRACE 0 -#define SPDLOG_LEVEL_DEBUG 1 -#define SPDLOG_LEVEL_INFO 2 -#define SPDLOG_LEVEL_WARN 3 -#define SPDLOG_LEVEL_ERROR 4 -#define SPDLOG_LEVEL_CRITICAL 5 -#define SPDLOG_LEVEL_OFF 6 - -#if defined(BUILD_TEAMSERVER) || defined(BUILD_TESTS) - - #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG - - #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE - #define SPDLOG_TRACE(...) printf(__VA_ARGS__) - #else - #define SPDLOG_TRACE(...) (void)0 - #endif - - #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG - #define SPDLOG_DEBUG(...) printf(__VA_ARGS__) - #else - #define SPDLOG_DEBUG(...) (void)0 - #endif - - #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO - #define SPDLOG_INFO(...) printf(__VA_ARGS__) - #else - #define SPDLOG_INFO(...) (void)0 - #endif - - #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN - #define SPDLOG_WARN(...) printf(__VA_ARGS__) - #else - #define SPDLOG_WARN(...) (void)0 - #endif - - #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR - #define SPDLOG_ERROR(...) printf(__VA_ARGS__) - #else - #define SPDLOG_ERROR(...) (void)0 - #endif - - #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL - #define SPDLOG_CRITICAL(...) printf(__VA_ARGS__) - #else - #define SPDLOG_CRITICAL(...) (void)0 - #endif - -#else - - #define SPDLOG_TRACE(...) (void)0 - #define SPDLOG_DEBUG(...) (void)0 - #define SPDLOG_INFO(...) (void)0 - #define SPDLOG_WARN(...) (void)0 - #define SPDLOG_ERROR(...) (void)0 - #define SPDLOG_CRITICAL(...) (void)0 - -#endif - -using namespace SocketHandler; - - -// https://learn.microsoft.com/en-us/windows/win32/winsock/complete-server-code -Server::Server(int port) -{ - m_port = port; - - WSADATA wsaData; - int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); - if (iResult != 0) - { - SPDLOG_DEBUG("WSAStartup failed with error: {0}", iResult); - return; - } -} - - -Server::~Server() -{ - closeConnection(); - WSACleanup(); -} - - -void Server::initServer() -{ - SPDLOG_DEBUG("initServer"); - - int iResult; - - m_listenSocket = INVALID_SOCKET; - m_clientSocket = INVALID_SOCKET; - - struct addrinfo hints; - ZeroMemory(&hints, sizeof(hints)); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - hints.ai_flags = AI_PASSIVE; - - std::string sPort = std::to_string(m_port); - - result = NULL; - iResult = getaddrinfo(NULL, sPort.c_str(), &hints, &result); - if ( iResult != 0 ) - { - SPDLOG_DEBUG("getaddrinfo failed with error: {0}", iResult); - return ; - } - - m_listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); - if (m_listenSocket == INVALID_SOCKET) - { - SPDLOG_DEBUG("socket failed with error: {0}", WSAGetLastError()); - freeaddrinfo(result); - return ; - } - - iResult = bind( m_listenSocket, result->ai_addr, (int)result->ai_addrlen); - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("bind failed with error: {0}", WSAGetLastError()); - freeaddrinfo(result); - closesocket(m_listenSocket); - return ; - } - - freeaddrinfo(result); - - iResult = listen(m_listenSocket, SOMAXCONN); - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("listen failed with error: {0}", WSAGetLastError()); - closesocket(m_listenSocket); - return ; - } - - m_clientSocket = accept(m_listenSocket, NULL, NULL); - if (m_clientSocket == INVALID_SOCKET) - { - SPDLOG_DEBUG("accept failed with error: {0}", WSAGetLastError()); - closesocket(m_listenSocket); - return ; - } - - linger lingerOpt = { 1, 0 }; - setsockopt(m_listenSocket, SOL_SOCKET, SO_LINGER, (char*)&lingerOpt, sizeof(lingerOpt)); - - closesocket(m_listenSocket); - - int timeOutMs = 5000; - unsigned int sz = sizeof(timeOutMs); - setsockopt(m_clientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeOutMs, sz); -} - - -bool Server::sendData(std::string& data) -{ - SPDLOG_DEBUG("sendData"); - - int nbBytes = data.size(); - int iResult = send(m_clientSocket, (char*)&nbBytes, (int) sizeof(int), 0); - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("send failed: {0}", WSAGetLastError()); - closesocket(m_clientSocket); - return false; - } - - if(nbBytes!=0) - { - iResult = send(m_clientSocket, (char*)&data[0], (int) nbBytes, 0); - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("send failed: {0}", WSAGetLastError()); - closesocket(m_clientSocket); - return false; - } - } - - return true; -} - - -bool Server::receive(std::string& data) -{ - SPDLOG_DEBUG("receive"); - - int nbBytes=0; - int iResult = recv(m_clientSocket, (char*)&nbBytes, sizeof(int), 0); - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("recv failed: {0}", WSAGetLastError()); - closesocket(m_clientSocket); - return false; - } - data.resize(nbBytes); - - if(nbBytes!=0) - { - int nbBytesReceived=0; - while(nbBytesReceived!=nbBytes) - { - iResult = recv(m_clientSocket, &data[nbBytesReceived], nbBytes-nbBytesReceived, 0); - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("recv failed: {0}", WSAGetLastError()); - closesocket(m_clientSocket); - return false; - } - nbBytesReceived+=iResult; - } - } - - return true; -} - - -void Server::closeConnection() -{ - SPDLOG_DEBUG("closeConnection"); - - int iResult = shutdown(m_clientSocket, SD_BOTH); - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("shutdown failed with error: {0}", WSAGetLastError()); - closesocket(m_clientSocket); - WSACleanup(); - return; - } - - linger lingerOpt = { 1, 0 }; - setsockopt(m_clientSocket, SOL_SOCKET, SO_LINGER, (char*)&lingerOpt, sizeof(lingerOpt)); - - closesocket(m_clientSocket); - WSACleanup(); -} - - -// https://learn.microsoft.com/en-us/windows/win32/winsock/complete-client-code -Client::Client(std::string& ip, int port) -{ - m_ipServer = ip; - m_port = port; -} - - -Client::~Client() -{ - closeConnection(); -} - - -bool Client::initConnection() -{ - SPDLOG_DEBUG("initConnection"); - - WSADATA wsaData; - int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); - if (iResult != 0) - { - SPDLOG_DEBUG("WSAStartup failed with error: {0}", iResult); - return false; - } - - m_connectSocket = INVALID_SOCKET; - - struct addrinfo hints; - ZeroMemory( &hints, sizeof(hints) ); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - - result = NULL; - std::string sPort = std::to_string(m_port); - iResult = getaddrinfo(m_ipServer.c_str(), sPort.c_str(), &hints, &result); - if ( iResult != 0 ) - { - SPDLOG_DEBUG("getaddrinfo failed with error: {0}", iResult); - WSACleanup(); - return false; - } - - struct addrinfo *ptr = NULL; - for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) - { - m_connectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); - if (m_connectSocket == INVALID_SOCKET) - { - SPDLOG_DEBUG("socket failed with error: {0}", WSAGetLastError()); - freeaddrinfo(result); - WSACleanup(); - return false; - } - - iResult = connect( m_connectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); - if (iResult == SOCKET_ERROR) - { - closesocket(m_connectSocket); - m_connectSocket = INVALID_SOCKET; - freeaddrinfo(result); - WSACleanup(); - return false; - } - break; - } - - freeaddrinfo(result); - - int timeOutMs = 1000; - unsigned int sz = sizeof(timeOutMs); - setsockopt(m_connectSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeOutMs, sz); - - return true; -} - - -bool Client::sendData(std::string& data) -{ - SPDLOG_DEBUG("sendData"); - - int nbBytes = data.size(); - int iResult = send(m_connectSocket, (char*)&nbBytes, (int) sizeof(int), 0); - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("send failed: {0}", WSAGetLastError()); - closesocket(m_connectSocket); - return false; - } - - if(nbBytes!=0) - { - iResult = send(m_connectSocket, (char*)&data[0], (int) nbBytes, 0); - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("send failed: {0}", WSAGetLastError()); - closesocket(m_connectSocket); - return false; - } - } - - return true; -} - - -bool Client::receive(std::string& data) -{ - SPDLOG_DEBUG("receive"); - - int nbBytes=0; - int iResult = recv(m_connectSocket, (char*)&nbBytes, sizeof(int), 0); - - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("recv failed: {0}", WSAGetLastError()); - closesocket(m_connectSocket); - return false; - } - data.resize(nbBytes); - - if(nbBytes!=0) - { - int nbBytesReceived=0; - while(nbBytesReceived!=nbBytes) - { - iResult = recv(m_connectSocket, &data[nbBytesReceived], nbBytes-nbBytesReceived, 0); - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("recv failed: {0}", WSAGetLastError()); - closesocket(m_connectSocket); - return false; - } - nbBytesReceived+=iResult; - } - - } - - return true; -} - - -void Client::closeConnection() -{ - SPDLOG_DEBUG("closeConnection"); - - int iResult = shutdown(m_connectSocket, SD_BOTH); - if (iResult == SOCKET_ERROR) - { - SPDLOG_DEBUG("shutdown failed with error: {0}", WSAGetLastError()); - closesocket(m_connectSocket); - WSACleanup(); - return ; - } - - linger lingerOpt = { 1, 0 }; - setsockopt(m_connectSocket, SOL_SOCKET, SO_LINGER, (char*)&lingerOpt, sizeof(lingerOpt)); - - closesocket(m_connectSocket); - WSACleanup(); -} - diff --git a/libs/libWindowsSocketHandler/src/SocketHandler.hpp b/libs/libWindowsSocketHandler/src/SocketHandler.hpp deleted file mode 100644 index 74b74a5..0000000 --- a/libs/libWindowsSocketHandler/src/SocketHandler.hpp +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once - -#include -#include -#include - -#include -#include -#include - - -namespace SocketHandler -{ - -class Server -{ -public: - Server(int port); - ~Server(); - - void initServer(); - void closeConnection(); - - bool sendData(std::string& data); - bool receive(std::string& data); - -private: - int m_port; - - struct addrinfo *result; - SOCKET m_listenSocket; - SOCKET m_clientSocket; -}; - - - -class Client -{ -public: - Client(std::string& ip, int port); - ~Client(); - - bool initConnection(); - void closeConnection(); - - bool sendData(std::string& data); - bool receive(std::string& data); - -private: - std::string m_ipServer; - int m_port; - - struct addrinfo *result; - SOCKET m_connectSocket; -}; - -} diff --git a/libs/libWindowsSocketHandler/tests/CMakeLists.txt b/libs/libWindowsSocketHandler/tests/CMakeLists.txt deleted file mode 100644 index 56aafa0..0000000 --- a/libs/libWindowsSocketHandler/tests/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -add_executable(SocketHandlerClientTest "testClient.cpp" ) -if(WIN32) - set_property(TARGET SocketHandlerClientTest PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded") - target_link_libraries(SocketHandlerClientTest SocketHandler) -else() -endif() - - -add_executable(SocketHandlerServerTest "testServer.cpp" ) -if(WIN32) - set_property(TARGET SocketHandlerServerTest PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded") - target_link_libraries(SocketHandlerServerTest SocketHandler) -else() -endif() diff --git a/libs/libWindowsSocketHandler/tests/testClient.cpp b/libs/libWindowsSocketHandler/tests/testClient.cpp deleted file mode 100644 index 9b207b7..0000000 --- a/libs/libWindowsSocketHandler/tests/testClient.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "SocketHandler.hpp" - -#include - - -using namespace std; -using namespace SocketHandler; - -int main() -{ - int remotePort=4444; - string remoteHost="127.0.0.1"; - - Client* client=new Client(remoteHost, remotePort); - - while(1) - { - while(!client->initConnection()) - { - Sleep(1000); - } - - string out="Hello from the client."; - bool res = client->sendData(out); - if(res) - { - string ret; - res = client->receive(ret); - - if(res) - { - std::cout << "Client received - " << ret << std::endl; - } - else - std::cout << "Client receive failed" << std::endl; - } - else - std::cout << "Client send failed" << std::endl; - - client->closeConnection(); - - Sleep(1000); - } - - delete client; -} \ No newline at end of file diff --git a/libs/libWindowsSocketHandler/tests/testServer.cpp b/libs/libWindowsSocketHandler/tests/testServer.cpp deleted file mode 100644 index cbcf369..0000000 --- a/libs/libWindowsSocketHandler/tests/testServer.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "SocketHandler.hpp" - -#include - - -using namespace std; -using namespace SocketHandler; - -int main() -{ - int port=4444; - Server* server = new Server(port); - - while(1) - { - server->initServer(); - - string ret; - bool res = server->receive(ret); - if(res) - { - std::cout << "Server received - " << ret << std::endl; - - string out="Hello from the server."; - res = server->sendData(out); - if(res) - { - } - else - std::cout << "Server send failed" << std::endl; - } - else - std::cout << "Server receive failed" << std::endl; - - server->closeConnection(); - } - - delete server; -} \ No newline at end of file