From 0ea28af44d35ca497a577ff7b082034ddf91e229 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Mon, 2 Oct 2023 16:47:48 -0500 Subject: [PATCH] Cleaned up web server and makes it use network.cpp --- src/CMakeLists.txt | 1 + src/network.cpp | 59 +++++++++++++++++++++++++------------------- src/network.hpp | 6 ++--- src/web_server.cpp | 61 ++++++++++++---------------------------------- 4 files changed, 53 insertions(+), 74 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f6601ad..d6e4384 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,5 +5,6 @@ add_executable(proxy add_executable(web_server ./web_server.cpp + ./network.cpp ) diff --git a/src/network.cpp b/src/network.cpp index 237506b..b1b69e8 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -4,41 +4,52 @@ #include // Needed for strcpy() and strlen() #include -void HandleClient(int browser_s, int server_s) { +void PipeSockets(int sender_s, int receiver_s) { char in_buf[BUF_SIZE]; // Input buffer for GET resquest - char out_buf[BUF_SIZE]; // Output buffer for HTML response ssize_t buf_len; // Buffer length for file reads - + // // Pass GET along from browser to server - std::cout << "Attempting to send to server" << std::endl; + std::cout << "Attempting to send data to receiver" << std::endl; do { - buf_len = recv(browser_s, in_buf, BUF_SIZE, 0); + buf_len = recv(sender_s, in_buf, BUF_SIZE, 0); if (buf_len == -1) { - std::cout << "ERROR - recv returned -1" << std::endl; + std::cout << "ERROR (info) - recv" << std::endl; std::terminate(); } - send(server_s, in_buf, buf_len, 0); - } while (buf_len != 0); - std::cout << "Sent to server" << std::endl; + send(receiver_s, in_buf, buf_len, 0); + std::cout << "LOG (info) - pipe packet size: " << buf_len << std::endl; + } while (buf_len > 0); + std::cout << "Sent data to receiver" << std::endl; + +} + +void ProxySockets(int sender_s, int receiver_s) { + char out_buf[BUF_SIZE]; // Output buffer for HTML response + ssize_t buf_len; // Buffer length for file reads // Pass response along from server to browser std::cout << "Attempting to send to browser" << std::endl; do { - buf_len = recv(server_s, out_buf, BUF_SIZE, 0); - send(browser_s, out_buf, buf_len, 0); - } while (buf_len != 0); + buf_len = recv(sender_s, out_buf, BUF_SIZE, 0); + if (buf_len == -1) { + std::cout << "ERROR (info) - recv" << std::endl; + std::terminate(); + } + send(receiver_s, out_buf, buf_len, 0); + std::cout << "LOG (info) - proxy packet size: " << buf_len << std::endl; + } while (buf_len > 0); std::cout << "Sent to browser" << std::endl; - close(server_s); - close(browser_s); + close(sender_s); + close(receiver_s); } Client::Client(void) { addressLength = sizeof(address); - address.sin_family = AF_INET; + address.sin_family = AF_INET; } -bool Client::ConnectFrom(int socketFD) { - socketFD = accept(socketFD, (struct sockaddr *)&address, &addressLength); +bool Client::ConnectFrom(int serverFD) { + socketFD = accept(serverFD, (struct sockaddr *)&address, &addressLength); if (socketFD == -1) { return 1; } return 0; } @@ -60,20 +71,17 @@ bool Client::ConnectTo(int portNumber) { } Server::Server(int portNumber) { - address.sin_family = AF_INET; - address.sin_port = htons(portNumber); - address.sin_addr.s_addr = htonl(INADDR_ANY); -} - -void Server::Open(void) { std::cout << "Opening the server" << std::endl; - // Create a socket, fill-in address information, and then bind it - socketFD = socket(address.sin_family, SOCK_STREAM, 0); + socketFD = socket(AF_INET, SOCK_STREAM, 0); if (socketFD == -1) { std::cerr << "ERROR - Unable to create socket on server" << std::endl; exit(1); } + address.sin_family = AF_INET; + 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) { std::cerr << "ERROR - Unable to bind socket" << std::endl; @@ -81,6 +89,7 @@ void Server::Open(void) { } // Listen for connections and then accept listen(socketFD, 100); + std::cout << "Server is now open" << std::endl; } void Server::Close(void) { diff --git a/src/network.hpp b/src/network.hpp index 7bae37e..d5c2000 100644 --- a/src/network.hpp +++ b/src/network.hpp @@ -10,14 +10,15 @@ #define kWebserverIP "127.0.0.1" #define kWebserverPort 7080 -void HandleClient(int browser_s, int server_s); +void PipeSockets(int sender_s, int receiver_s); +void ProxySockets(int sender_s, int receiver_s); struct Client { unsigned int socketFD; // Client socket descriptor sockaddr_in address; // Client IP address socklen_t addressLength; // Internet address length Client(void); - bool ConnectFrom(int socketFD); + bool ConnectFrom(int serverFD); bool ConnectTo(int portNumber); }; @@ -25,7 +26,6 @@ struct Server { unsigned int socketFD; // Server socket descriptor sockaddr_in address; // Server Internet address Server(int portNumber); - void Open(void); void Close(void); }; diff --git a/src/web_server.cpp b/src/web_server.cpp index c09fe76..348766e 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -31,12 +31,13 @@ #define UNIX // WIN for Windows environment, UNIX for BSD or LINUX env. //----- Include files --------------------------------------------------------- -#include // Needed for printf() -#include // Needed for exit() #include // Needed for strcpy() and strlen() #include // Needed for file i/o constants #include // Needed for file i/o constants #include +#include +#include +#include "network.hpp" /* FOR BSD UNIX/LINUX ---------------------------------------------------- */ #ifdef UNIX @@ -84,19 +85,9 @@ int main(void) #endif /* --------------------------------------------------------------------- */ - unsigned int server_s; // Server socket descriptor - struct sockaddr_in server_addr; // Server Internet address - unsigned int client_s; // Client socket descriptor - struct sockaddr_in client_addr; // Client Internet address - struct in_addr client_ip_addr; // Client IP address - socklen_t addr_len; // Internet address length - - /* FOR UNIX/LINUX ------------------------------------------------------ */ -#ifdef UNIX - int nChild_proc_id; // New child process ID. -#endif - /* --------------------------------------------------------------------- */ - + std::vector> pending_futures; + Server webserver(kWebserverPort); + Client proxy; /* FOR WIN ------------------------------------------------------------- */ #ifdef WIN // Initialize winsock @@ -104,41 +95,19 @@ int main(void) #endif /* --------------------------------------------------------------------- */ - // Create a socket, fill-in address information, and then bind it - server_s = socket(AF_INET, SOCK_STREAM, 0); - if (server_s == -1) - { - printf("ERROR - Unable to create socket on server\n"); - exit(1); - } - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(PORT_NUM); - server_addr.sin_addr.s_addr = htonl(INADDR_ANY); - int bindRet = bind(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr)); - if (bindRet == -1) - { - printf("ERROR - Unable to bind socket\n"); - exit(1); - } - // Listen for connections and then accept - listen(server_s, 100); - // Main loop to listen, accept, and then spin-off a thread to handle the GET while (1) { - addr_len = sizeof(client_addr); - - client_s = accept(server_s, (struct sockaddr *)&client_addr, &addr_len); - if (client_s == -1) - { - perror("ERROR - Unable to create socket to client\n"); + if (proxy.ConnectFrom(webserver.socketFD) != 0) + { + std::cerr << "ERROR - Unable to create socket to client" << std::endl; continue; - } - - std::async(std::launch::async, ClientRequest, client_s); + } + auto newThreadRequest = std::async(std::launch::async, ClientRequest, proxy.socketFD); + pending_futures.push_back(std::move(newThreadRequest)); } - close(server_s); - return (1); + webserver.Close(); + return 0; } void ClientRequest(int client_s) { @@ -188,7 +157,7 @@ void ClientRequest(int client_s) { do { buf_len = read(fh, out_buf, BUF_SIZE); send(client_s, out_buf, buf_len, 0); - } while (buf_len != 0); + } while (buf_len > 0); close(fh); } }