From 0ad810a19fb85a6c5cfbe88204cec5862d13d8c4 Mon Sep 17 00:00:00 2001 From: Gregory <56975502+Trimutex@users.noreply.github.com> Date: Wed, 4 Oct 2023 01:10:24 -0500 Subject: [PATCH] Finished project on Windows --- .gitignore | 1 + src/CMakeLists.txt | 4 ++++ src/network.cpp | 37 +++++++++++++++++++++++++++++++++---- src/network.hpp | 8 ++++++++ src/proxy.cpp | 12 ++++++++++++ src/web_server.cpp | 24 +++++++++++------------- 6 files changed, 69 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index a047907..3d45f7f 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,4 @@ # Extras build test/web_server +.vscode diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d6e4384..c8d4184 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,3 +8,7 @@ add_executable(web_server ./network.cpp ) +if(WIN32) + target_link_libraries(proxy wsock32 ws2_32) + target_link_libraries(web_server wsock32 ws2_32) +endif() diff --git a/src/network.cpp b/src/network.cpp index f63dc8d..0752c3c 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -1,5 +1,7 @@ #include "network.hpp" +#ifdef UNIX #include +#endif #include #include // Needed for exit() #include @@ -35,8 +37,14 @@ void PipeSockets(int sender_s, int receiver_s) { send(sender_s, in_buf, strlen(in_buf), 0); strcpy(in_buf, MESS_403); send(sender_s, in_buf, strlen(in_buf), 0); +#ifdef WIN + closesocket(sender_s); + closesocket(receiver_s); +#endif +#ifdef UNIX close(sender_s); close(receiver_s); +#endif return; } @@ -72,8 +80,14 @@ void ProxySockets(int sender_s, int receiver_s) { send(receiver_s, out_buf, strlen(out_buf), 0); strcpy(out_buf, MESS_403); send(receiver_s, out_buf, strlen(out_buf), 0); +#ifdef WIN + closesocket(sender_s); + closesocket(receiver_s); +#endif +#ifdef UNIX close(sender_s); close(receiver_s); +#endif return; } @@ -83,8 +97,14 @@ void ProxySockets(int sender_s, int receiver_s) { std::cout << "LOG (info) - proxy packet send size: " << buf_len << '\n'; } while ((buf_len == BUF_SIZE) || (buf_len == 40)); std::cout << "Sent to browser" << std::endl; +#ifdef WIN + closesocket(sender_s); + closesocket(receiver_s); +#endif +#ifdef UNIX close(sender_s); close(receiver_s); +#endif } Client::Client(void) { @@ -116,9 +136,13 @@ bool Client::ConnectTo(int portNumber) { Server::Server(int portNumber) { std::cout << "Opening the server" << std::endl; +#ifdef WIN socketFD = socket(AF_INET, SOCK_STREAM, 0); - if (socketFD == -1) - { +#endif +#ifdef UNIX + socketFD = socket(AF_INET, SOCK_STREAM, 0); +#endif + if (socketFD == -1) { std::cerr << "ERROR - Unable to create socket on server" << std::endl; exit(1); } @@ -126,8 +150,7 @@ Server::Server(int portNumber) { address.sin_port = htons(portNumber); address.sin_addr.s_addr = htonl(INADDR_ANY); // Create a socket, fill-in address information, and then bind it - if (bind(socketFD, (struct sockaddr *)&address, sizeof(address)) == -1) - { + if (bind(socketFD, (struct sockaddr *)&address, sizeof(address)) == -1) { std::cerr << "ERROR - Unable to bind socket" << std::endl; exit(1); } @@ -138,5 +161,11 @@ Server::Server(int portNumber) { void Server::Close(void) { std::cout << "Closing the server" << std::endl; +#ifdef WIN + closesocket(socketFD); + WSACleanup(); +#endif +#ifdef UNIX close(socketFD); +#endif } diff --git a/src/network.hpp b/src/network.hpp index f3c6ac3..cb291fd 100644 --- a/src/network.hpp +++ b/src/network.hpp @@ -1,7 +1,15 @@ #ifndef NETWORK_HPP #define NETWORK_HPP +#define WIN // WIN for Windows environment, UNIX for BSD or LINUX env. + +#ifdef UNIX #include +#endif +#ifdef WIN +#include +#include +#endif #include #define BUF_SIZE 4096 // Buffer size (big enough for a GET) diff --git a/src/proxy.cpp b/src/proxy.cpp index 3e60e83..1f76144 100644 --- a/src/proxy.cpp +++ b/src/proxy.cpp @@ -8,6 +8,18 @@ int main(void) { + /* FOR WIN ------------------------------------------------------------- */ +#ifdef WIN + WORD wVersionRequested = MAKEWORD(1, 1); // Stuff for WSA functions + WSADATA wsaData; + int wsResult; // Stuff for WSA functions + wsResult = WSAStartup(wVersionRequested, &wsaData); + if (wsResult != 0) { + printf("WSAStartup failed: %d\n", wsResult); + return 1; + } +#endif + /* --------------------------------------------------------------------- */ strcpy(hazardous_contents_CS_01, "password.txt"); strcpy(hazardous_contents_CS_02, "admin.config"); std::vector> pending_futures; diff --git a/src/web_server.cpp b/src/web_server.cpp index 1df925c..1001240 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -28,7 +28,6 @@ //= History: KJC (12/29/00) - Genesis (from server.c) = //= HF (01/25/01) - Ported to multi-platform environment = //============================================================================= -#define UNIX // WIN for Windows environment, UNIX for BSD or LINUX env. //----- Include files --------------------------------------------------------- #include // Needed for strcpy() and strlen() @@ -51,13 +50,7 @@ /* FOR WIN ---------------------------------------------------------------- */ #ifdef WIN -#include // Needed for _threadid -#include // Needed for _beginthread() and _endthread() #include // Needed for open(), close(), and eof() -#include -#include // Needed for all Winsock stuff -// Lazy struct fixes -typedef int32_t socklen_t; #endif /* ------------------------------------------------------------------------ */ @@ -82,18 +75,13 @@ int main(void) #ifdef WIN WORD wVersionRequested = MAKEWORD(1, 1); // Stuff for WSA functions WSADATA wsaData; // Stuff for WSA functions + WSAStartup(wVersionRequested, &wsaData); #endif /* --------------------------------------------------------------------- */ std::vector> pending_futures; Server webserver(kWebserverPort); Client proxy; - /* FOR WIN ------------------------------------------------------------- */ -#ifdef WIN - // Initialize winsock - WSAStartup(wVersionRequested, &wsaData); -#endif - /* --------------------------------------------------------------------- */ // Main loop to listen, accept, and then spin-off a thread to handle the GET while (1) @@ -168,9 +156,19 @@ void ClientRequest(int client_s) { std::cout << "LOG (info) - webserver send size: " << buf_len << '\n'; } while (buf_len == BUF_SIZE); std::cout << std::endl; +#ifdef WIN + closesocket(fh); +#endif +#ifdef UNIX close(fh); +#endif } // Close the client socket and end the thread +#ifdef WIN + closesocket(client_s); +#endif +#ifdef UNIX close(client_s); +#endif }