diff --git a/src/CWP.cpp b/src/CWP.cpp index 94459e7..f4d3451 100644 --- a/src/CWP.cpp +++ b/src/CWP.cpp @@ -1,6 +1,8 @@ #include "CWP.h" void ProtocolThread() { + DebugPrint(DEBUG_TYPE::INFO_MSG, "Started CWP protocol worker thread."); + int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock < 0) { @@ -13,5 +15,27 @@ void ProtocolThread() { saIn.sin_family = AF_INET; inet_pton(AF_INET, LHOST, &saIn.sin_addr.s_addr); - + if (bind(sock, (const struct sockaddr*)&saIn, sizeof(saIn)) < 0) { + DebugPrint(DEBUG_TYPE::ERROR_MSG, std::string("Error binding protocol TCP socket on port: ") + std::to_string(PROTOCOL_PORT)); + exit(1); + } + + if (listen(sock, MAX_CONN) < 0) { + DebugPrint(DEBUG_TYPE::ERROR_MSG, "Error starting TCP listened"); + exit(1); + } + + DebugPrint(DEBUG_TYPE::INFO_MSG, std::string("WCP Protocol listening on port: ") + std::to_string(PROTOCOL_PORT)); + + while (true) { + sockaddr_in clientAddr = { }; + socklen_t clientSize = sizeof(sockaddr_in); + int client = accept(sock, (sockaddr*)&clientAddr, &clientSize); + + char clientIp[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET, &clientAddr.sin_addr.s_addr, clientIp, sizeof(clientIp)); + UINT clientPort = ntohs(clientAddr.sin_port); + + DebugPrint(DEBUG_TYPE::INFO_MSG, std::string("(CWP) New connection from: ") + std::string(clientIp) + ':' + std::to_string(clientPort)); + } } \ No newline at end of file diff --git a/src/CWP.h b/src/CWP.h index 6f392c5..9b208fe 100644 --- a/src/CWP.h +++ b/src/CWP.h @@ -1,4 +1,5 @@ #pragma once + #ifdef WIN32 #include #include @@ -11,9 +12,12 @@ typedef unsigned int UINT; #endif +#include + #include "ConsoleUtils.h" #define LHOST "127.0.0.1" #define PROTOCOL_PORT 18450 +#define MAX_CONN 10 void ProtocolThread(); \ No newline at end of file diff --git a/src/ConsoleUtils.h b/src/ConsoleUtils.h index 61c05cf..e27bdc8 100644 --- a/src/ConsoleUtils.h +++ b/src/ConsoleUtils.h @@ -29,5 +29,5 @@ inline void DebugPrint(DEBUG_TYPE type, std::string msg) { break; } - std::cout << printedMsg << std::endl; + std::cout << printedMsg + "\n"; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f7d96bb..c2406e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ typedef unsigned int UINT; #include "ConsoleUtils.h" #include "BlockList.h" +#include "CWP.h" #define VERSION 0.1 @@ -29,6 +30,8 @@ char PRIMARY_DNS[] = "8.8.8.8"; BlockList* g_blockList = nullptr; +std::thread g_cwpThread; + int main() { std::cout << "CleanWave. The most powerful DNS for privacy and ad blocking. Version: " << VERSION << std::endl; std::cout << "Made by Preciado" << std::endl << std::endl; @@ -43,7 +46,10 @@ int main() { } #endif - DebugPrint(DEBUG_TYPE::INFO_MSG, "Initializing socket"); + g_cwpThread = std::thread(ProtocolThread); + g_cwpThread.detach(); + + DebugPrint(DEBUG_TYPE::INFO_MSG, "Initializing DNS server socket"); int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock < 0) { DebugPrint(DEBUG_TYPE::FATAL_MSG, "Error creating the socket");