From f40433694ce7363f1d6c858c0b6c00ddf97e4dd6 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sun, 1 Oct 2023 16:10:03 -0500 Subject: [PATCH 1/8] UPDATE - Still having error with PIPE --- src/proxy.cpp | 54 ++++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/src/proxy.cpp b/src/proxy.cpp index 7422c2e..367f9f6 100644 --- a/src/proxy.cpp +++ b/src/proxy.cpp @@ -1,10 +1,10 @@ //----- 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 // @@ -24,8 +24,8 @@ #define PORT_NUM 9080 // Port number for a Web server //----- Function prototypes ------------------------------------------------- -void BrowserToProxy(int server_s, int client_s); -void ProxyToServer(int server_s, int client_s); +void BrowserToServer(int browser_s, int server_s); +void ServerToBrowser(int server_s, int browser_s); //===== modeule main ======================================================== int main(void) @@ -44,7 +44,7 @@ int main(void) proxy_s = socket(AF_INET, SOCK_STREAM, 0); if (proxy_s == -1) { - printf("ERROR - Unable to create socket on server\n"); + std::cerr << "ERROR - Unable to create socket on server" << std::endl; exit(1); } proxy_addr.sin_family = AF_INET; @@ -53,7 +53,7 @@ int main(void) int bindRet = bind(proxy_s, (struct sockaddr *)&proxy_addr, sizeof(proxy_addr)); if (bindRet == -1) { - printf("ERROR - Unable to bind socket\n"); + std::cerr << "ERROR - Unable to bind socket" << std::endl; exit(1); } // Listen for connections and then accept @@ -67,56 +67,40 @@ int main(void) browser_s = accept(proxy_s, (struct sockaddr *)&browser_addr, &addr_len); if (browser_s == -1) { - perror("ERROR - Unable to create socket to client\n"); + std::cerr << "ERROR - Unable to create socket to client" << std::endl; continue; } + std::cout << "Got here" << std::endl; unsigned int server_s = socket(AF_INET, SOCK_STREAM, 0); connect(server_s, (struct sockaddr *)&server_addr, addr_len); - std::async(std::launch::async, BrowserToProxy, browser_s, server_s); - std::async(std::launch::async, ProxyToServer, server_s, browser_s); - + std::async(std::launch::async, BrowserToServer, browser_s, server_s); + std::async(std::launch::async, ServerToBrowser, server_s, browser_s); } - close(browser_s); + close(proxy_s); return (1); } -void BrowserToProxy(int server_s, int client_s) { +void BrowserToServer(int browser_s, int server_s) { char in_buf[BUF_SIZE]; // Input buffer for GET resquest - char out_buf[BUF_SIZE]; // Output buffer for HTML response - char *file_name; // File name - unsigned int fh; // File handle ssize_t buf_len; // Buffer length for file reads - unsigned int retcode; // Return code - // Receive the GET request from the Web browser + // Pass GET along from browser to server do { - buf_len = recv(server_s, in_buf, BUF_SIZE, 0); - send(client_s, in_buf, buf_len, 0); + buf_len = recv(browser_s, in_buf, BUF_SIZE, 0); + send(server_s, in_buf, buf_len, 0); } while (buf_len != 0); } -void ProxyToServer(int server_s, int client_s) { - /* - - loop { - recv from browser - send to server - } - - */ - char in_buf[BUF_SIZE]; // Input buffer for GET resquest +void ServerToBrowser(int server_s, int browser_s) { char out_buf[BUF_SIZE]; // Output buffer for HTML response - char *file_name; // File name - unsigned int fh; // File handle ssize_t buf_len; // Buffer length for file reads - unsigned int retcode; // Return code - // Receive the GET request from the Web browser + // Pass response along from server to browser do { - buf_len = recv(server_s, in_buf, BUF_SIZE, 0); - send(client_s, in_buf, buf_len, 0); + buf_len = recv(server_s, out_buf, BUF_SIZE, 0); + send(browser_s, out_buf, buf_len, 0); } while (buf_len != 0); close(server_s); - close(client_s); + close(browser_s); } -- 2.45.2 From 9e94f77a839da1a737c30fc6e4244eb47e40ee25 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sun, 1 Oct 2023 18:24:47 -0500 Subject: [PATCH 2/8] Abstracted code for ease of use --- src/CMakeLists.txt | 9 ++-- src/network.cpp | 80 ++++++++++++++++++++++++++++++++++ src/network.hpp | 32 ++++++++++++++ src/proxy.cpp | 104 +++++++++------------------------------------ 4 files changed, 137 insertions(+), 88 deletions(-) create mode 100644 src/network.cpp create mode 100644 src/network.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0636427..f6601ad 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,9 @@ +add_executable(proxy + ./proxy.cpp + ./network.cpp +) + add_executable(web_server ./web_server.cpp ) -add_executable(proxy - ./proxy.cpp -) - diff --git a/src/network.cpp b/src/network.cpp new file mode 100644 index 0000000..addff4d --- /dev/null +++ b/src/network.cpp @@ -0,0 +1,80 @@ +#include "network.hpp" +#include +#include // Needed for exit() +#include + +void HandleClient(int browser_s, int server_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 + do { + buf_len = recv(browser_s, in_buf, BUF_SIZE, 0); + send(server_s, in_buf, buf_len, 0); + } while (buf_len != 0); + + // Pass response along from server to browser + do { + buf_len = recv(server_s, out_buf, BUF_SIZE, 0); + send(browser_s, out_buf, buf_len, 0); + } while (buf_len != 0); + close(server_s); + close(browser_s); +} + +Client::Client(void) { + addressLength = sizeof(address); + address.sin_family = AF_INET; +} + +bool Client::ConnectFrom(int socketFD) { + socketFD = accept(socketFD, (struct sockaddr *)&address, &addressLength); + if (socketFD == -1) { return 1; } + return 0; +} + +bool Client::ConnectTo(int portNumber) { + std::cout << "Attempting to connect to " << kWebserverIP << " on " << portNumber << std::endl; + address.sin_port = htons(portNumber); + if (inet_pton(address.sin_family, kWebserverIP, &address.sin_addr) <= 0) { + std::cerr << "ERROR (info) - inet_pton" << std::endl; + return 1; + } + socketFD = socket(address.sin_family, SOCK_STREAM, 0); + if (connect(socketFD, (struct sockaddr *)&address, addressLength) == -1) { + std::cerr << "ERROR (info) - connect" << std::endl; + return 1; + } + std::cout << "Connection successful" << std::endl; + return 0; +} + +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); + if (socketFD == -1) + { + std::cerr << "ERROR - Unable to create socket on server" << std::endl; + exit(1); + } + if (bind(socketFD, (struct sockaddr *)&address, sizeof(address)) == -1) + { + std::cerr << "ERROR - Unable to bind socket" << std::endl; + exit(1); + } + // Listen for connections and then accept + listen(socketFD, 100); +} + +void Server::Close(void) { + std::cout << "Closing the server" << std::endl; + close(socketFD); +} diff --git a/src/network.hpp b/src/network.hpp new file mode 100644 index 0000000..7bae37e --- /dev/null +++ b/src/network.hpp @@ -0,0 +1,32 @@ +#ifndef NETWORK_HPP +#define NETWORK_HPP + +#include +#include + +#define BUF_SIZE 4096 // Buffer size (big enough for a GET) + +#define kProxyPort 9080 +#define kWebserverIP "127.0.0.1" +#define kWebserverPort 7080 + +void HandleClient(int browser_s, int server_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 ConnectTo(int portNumber); +}; + +struct Server { + unsigned int socketFD; // Server socket descriptor + sockaddr_in address; // Server Internet address + Server(int portNumber); + void Open(void); + void Close(void); +}; + +#endif diff --git a/src/proxy.cpp b/src/proxy.cpp index 367f9f6..31a0ed8 100644 --- a/src/proxy.cpp +++ b/src/proxy.cpp @@ -1,17 +1,9 @@ //----- Include files --------------------------------------------------------- -#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 // -#include // -#include // -#include // -#include - +#include +#include "network.hpp" //----- HTTP response messages ---------------------------------------------- #define OK_IMAGE "HTTP/1.0 200 OK\nContent-Type:image/gif\n\n" @@ -19,88 +11,32 @@ #define NOTOK_404 "HTTP/1.0 404 Not Found\nContent-Type:text/html\n\n" #define MESS_404 "

FILE NOT FOUND

" -//----- Defines ------------------------------------------------------------- -#define BUF_SIZE 4096 // Buffer size (big enough for a GET) -#define PORT_NUM 9080 // Port number for a Web server - -//----- Function prototypes ------------------------------------------------- -void BrowserToServer(int browser_s, int server_s); -void ServerToBrowser(int server_s, int browser_s); - -//===== modeule main ======================================================== int main(void) { - unsigned int proxy_s; // Server socket descriptor - struct sockaddr_in proxy_addr; // Server Internet address - unsigned int browser_s; // Client socket descriptor - struct sockaddr_in browser_addr; // Client Internet address - struct in_addr browser_ip_addr; // Client IP address - unsigned int server_s; // Client socket descriptor - struct sockaddr_in server_addr; // Client Internet address - struct in_addr server_ip_addr; // Client IP address - socklen_t addr_len; // Internet address length + std::vector> pending_futures; + Server proxy(kProxyPort); + Client browser; + Client webserver; - // Create a socket, fill-in address information, and then bind it - proxy_s = socket(AF_INET, SOCK_STREAM, 0); - if (proxy_s == -1) - { - std::cerr << "ERROR - Unable to create socket on server" << std::endl; - exit(1); - } - proxy_addr.sin_family = AF_INET; - proxy_addr.sin_port = htons(PORT_NUM); - proxy_addr.sin_addr.s_addr = htonl(INADDR_ANY); - int bindRet = bind(proxy_s, (struct sockaddr *)&proxy_addr, sizeof(proxy_addr)); - if (bindRet == -1) - { - std::cerr << "ERROR - Unable to bind socket" << std::endl; - exit(1); - } - // Listen for connections and then accept - listen(proxy_s, 100); + proxy.Open(); // Main loop to listen, accept, and then spin-off a thread to handle the GET while (1) { - addr_len = sizeof(browser_addr); - - browser_s = accept(proxy_s, (struct sockaddr *)&browser_addr, &addr_len); - if (browser_s == -1) - { + if (browser.ConnectFrom(proxy.socketFD) != 0) + { std::cerr << "ERROR - Unable to create socket to client" << std::endl; continue; - } - std::cout << "Got here" << std::endl; - unsigned int server_s = socket(AF_INET, SOCK_STREAM, 0); - connect(server_s, (struct sockaddr *)&server_addr, addr_len); - - std::async(std::launch::async, BrowserToServer, browser_s, server_s); - std::async(std::launch::async, ServerToBrowser, server_s, browser_s); + } + if (webserver.ConnectTo(kWebserverPort) != 0) + { + std::cerr << "ERROR - Unable to connect to webserver" << std::endl; + continue; + } + auto newThreadRequest = std::async(std::launch::async, HandleClient, browser.socketFD, webserver.socketFD); + pending_futures.push_back(std::move(newThreadRequest)); } - close(proxy_s); - return (1); + proxy.Close(); + return 0; } -void BrowserToServer(int browser_s, int server_s) { - char in_buf[BUF_SIZE]; // Input buffer for GET resquest - ssize_t buf_len; // Buffer length for file reads - - // Pass GET along from browser to server - do { - buf_len = recv(browser_s, in_buf, BUF_SIZE, 0); - send(server_s, in_buf, buf_len, 0); - } while (buf_len != 0); -} - -void ServerToBrowser(int server_s, int browser_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 - do { - buf_len = recv(server_s, out_buf, BUF_SIZE, 0); - send(browser_s, out_buf, buf_len, 0); - } while (buf_len != 0); - close(server_s); - close(browser_s); -} -- 2.45.2 From 5e0069871edad1cdf7b68608012064d6ec2aef85 Mon Sep 17 00:00:00 2001 From: Trimutex Date: Mon, 2 Oct 2023 13:10:33 -0500 Subject: [PATCH 3/8] Added some error checking --- src/network.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/network.cpp b/src/network.cpp index addff4d..237506b 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -1,6 +1,7 @@ #include "network.hpp" #include #include // Needed for exit() +#include // Needed for strcpy() and strlen() #include void HandleClient(int browser_s, int server_s) { @@ -9,16 +10,24 @@ void HandleClient(int browser_s, int server_s) { 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; do { buf_len = recv(browser_s, in_buf, BUF_SIZE, 0); + if (buf_len == -1) { + std::cout << "ERROR - recv returned -1" << std::endl; + std::terminate(); + } send(server_s, in_buf, buf_len, 0); } while (buf_len != 0); + std::cout << "Sent to server" << std::endl; // 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); + std::cout << "Sent to browser" << std::endl; close(server_s); close(browser_s); } -- 2.45.2 From 0ea28af44d35ca497a577ff7b082034ddf91e229 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Mon, 2 Oct 2023 16:47:48 -0500 Subject: [PATCH 4/8] 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); } } -- 2.45.2 From f8f03bb5eef11e5de6c3fc51bbeb1c83d7f33903 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Mon, 2 Oct 2023 16:48:15 -0500 Subject: [PATCH 5/8] Current progress on the proxy, currently getting stuck on sending back to browser --- src/proxy.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/proxy.cpp b/src/proxy.cpp index 31a0ed8..acc3dae 100644 --- a/src/proxy.cpp +++ b/src/proxy.cpp @@ -18,8 +18,6 @@ int main(void) Client browser; Client webserver; - proxy.Open(); - // Main loop to listen, accept, and then spin-off a thread to handle the GET while (1) { @@ -33,8 +31,11 @@ int main(void) std::cerr << "ERROR - Unable to connect to webserver" << std::endl; continue; } - auto newThreadRequest = std::async(std::launch::async, HandleClient, browser.socketFD, webserver.socketFD); - pending_futures.push_back(std::move(newThreadRequest)); + auto newThreadRequest1 = std::async(std::launch::async, PipeSockets, browser.socketFD, webserver.socketFD); + auto newThreadRequest2 = std::async(std::launch::async, ProxySockets, webserver.socketFD, browser.socketFD); + pending_futures.push_back(std::move(newThreadRequest1)); + pending_futures.push_back(std::move(newThreadRequest2)); + std::cout << "Returned from creating threads, continuing..." << std::endl; } proxy.Close(); return 0; -- 2.45.2 From 42bf316ccbd956a6d3b1ce8472b1972f9fc21e50 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Tue, 3 Oct 2023 13:04:22 -0500 Subject: [PATCH 6/8] Some progress may have been made, but its still blocking unexpectedly --- src/network.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++------- src/network.hpp | 1 + src/proxy.cpp | 5 +++++ 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/network.cpp b/src/network.cpp index b1b69e8..d313caa 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -4,6 +4,41 @@ #include // Needed for strcpy() and strlen() #include +void TestSockets(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 data to receiver" << std::endl; + do { + buf_len = recv(sender_s, in_buf, BUF_SIZE, 0); + if (buf_len == -1) { + std::cout << "ERROR (info) - recv" << std::endl; + } + if (buf_len == 0) { continue; } + 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; + + // Pass response along from server to browser + std::cout << "Attempting to send to browser" << std::endl; + do { + buf_len = recv(receiver_s, out_buf, BUF_SIZE, 0); + if (buf_len == -1) { + std::cout << "ERROR (info) - recv" << std::endl; + break; + } + if (buf_len == 0) { continue; } + send(sender_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(sender_s); + close(receiver_s); +} + void PipeSockets(int sender_s, int receiver_s) { char in_buf[BUF_SIZE]; // Input buffer for GET resquest ssize_t buf_len; // Buffer length for file reads @@ -12,13 +47,15 @@ void PipeSockets(int sender_s, int receiver_s) { std::cout << "Attempting to send data to receiver" << std::endl; do { buf_len = recv(sender_s, in_buf, BUF_SIZE, 0); + std::cout << "LOG (info) - pipe packet recv size: " << buf_len << std::endl; if (buf_len == -1) { std::cout << "ERROR (info) - recv" << std::endl; - std::terminate(); + return; } - send(receiver_s, in_buf, buf_len, 0); - std::cout << "LOG (info) - pipe packet size: " << buf_len << std::endl; - } while (buf_len > 0); + if (buf_len == 0) { break; } + buf_len = send(receiver_s, in_buf, buf_len, 0); + std::cout << "LOG (info) - pipe packet send size: " << buf_len << std::endl; + } while (buf_len < BUF_SIZE); std::cout << "Sent data to receiver" << std::endl; } @@ -31,13 +68,15 @@ void ProxySockets(int sender_s, int receiver_s) { std::cout << "Attempting to send to browser" << std::endl; do { buf_len = recv(sender_s, out_buf, BUF_SIZE, 0); + std::cout << "LOG (info) - proxy packet recv size: " << buf_len << std::endl; if (buf_len == -1) { std::cout << "ERROR (info) - recv" << std::endl; - std::terminate(); + return; } - send(receiver_s, out_buf, buf_len, 0); - std::cout << "LOG (info) - proxy packet size: " << buf_len << std::endl; - } while (buf_len > 0); + if (buf_len == 0) { break; } + buf_len = send(receiver_s, out_buf, buf_len, 0); + std::cout << "LOG (info) - proxy packet send size: " << buf_len << std::endl; + } while (buf_len < BUF_SIZE); std::cout << "Sent to browser" << std::endl; close(sender_s); close(receiver_s); diff --git a/src/network.hpp b/src/network.hpp index d5c2000..bcab3d9 100644 --- a/src/network.hpp +++ b/src/network.hpp @@ -10,6 +10,7 @@ #define kWebserverIP "127.0.0.1" #define kWebserverPort 7080 +void TestSockets(int sender_s, int receiver_s); void PipeSockets(int sender_s, int receiver_s); void ProxySockets(int sender_s, int receiver_s); diff --git a/src/proxy.cpp b/src/proxy.cpp index acc3dae..980c8f8 100644 --- a/src/proxy.cpp +++ b/src/proxy.cpp @@ -35,7 +35,12 @@ int main(void) auto newThreadRequest2 = std::async(std::launch::async, ProxySockets, webserver.socketFD, browser.socketFD); pending_futures.push_back(std::move(newThreadRequest1)); pending_futures.push_back(std::move(newThreadRequest2)); + /* + auto newThreadRequest = std::async(std::launch::async, TestSockets, browser.socketFD, webserver.socketFD); + pending_futures.push_back(std::move(newThreadRequest)); std::cout << "Returned from creating threads, continuing..." << std::endl; + */ + //TestSockets(browser.socketFD, webserver.socketFD); } proxy.Close(); return 0; -- 2.45.2 From abab69344a9f6f8e96045f095e09639bfe43dc3e Mon Sep 17 00:00:00 2001 From: Trimutex Date: Tue, 3 Oct 2023 18:34:21 -0500 Subject: [PATCH 7/8] Still having issues with loading site --- src/network.cpp | 34 ++++++++++++++----------- src/web_server.cpp | 62 ++++++++++++++++++++++------------------------ 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/network.cpp b/src/network.cpp index d313caa..65acf59 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -13,27 +13,30 @@ void TestSockets(int sender_s, int receiver_s) { std::cout << "Attempting to send data to receiver" << std::endl; do { buf_len = recv(sender_s, in_buf, BUF_SIZE, 0); + std::cout << "LOG (info) - pipe packet recv size: " << buf_len << std::endl; if (buf_len == -1) { std::cout << "ERROR (info) - recv" << std::endl; + return; } - if (buf_len == 0) { continue; } - send(receiver_s, in_buf, buf_len, 0); - std::cout << "LOG (info) - pipe packet size: " << buf_len << std::endl; - } while (buf_len > 0); + if (buf_len == 0) { break; } + buf_len = send(receiver_s, in_buf, buf_len, 0); + std::cout << "LOG (info) - pipe packet send size: " << buf_len << std::endl; + } while (buf_len == BUF_SIZE); std::cout << "Sent data to receiver" << std::endl; // Pass response along from server to browser std::cout << "Attempting to send to browser" << std::endl; do { - buf_len = recv(receiver_s, out_buf, BUF_SIZE, 0); + buf_len = recv(sender_s, out_buf, BUF_SIZE, 0); + std::cout << "LOG (info) - proxy packet recv size: " << buf_len << std::endl; if (buf_len == -1) { std::cout << "ERROR (info) - recv" << std::endl; - break; + return; } - if (buf_len == 0) { continue; } - send(sender_s, out_buf, buf_len, 0); - std::cout << "LOG (info) - proxy packet size: " << buf_len << std::endl; - } while (buf_len > 0); + if (buf_len == 0) { break; } + buf_len = send(receiver_s, out_buf, buf_len, 0); + std::cout << "LOG (info) - proxy packet send size: " << buf_len << std::endl; + } while (buf_len == BUF_SIZE); std::cout << "Sent to browser" << std::endl; close(sender_s); close(receiver_s); @@ -55,7 +58,7 @@ void PipeSockets(int sender_s, int receiver_s) { if (buf_len == 0) { break; } buf_len = send(receiver_s, in_buf, buf_len, 0); std::cout << "LOG (info) - pipe packet send size: " << buf_len << std::endl; - } while (buf_len < BUF_SIZE); + } while (buf_len == BUF_SIZE); std::cout << "Sent data to receiver" << std::endl; } @@ -70,13 +73,16 @@ void ProxySockets(int sender_s, int receiver_s) { buf_len = recv(sender_s, out_buf, BUF_SIZE, 0); std::cout << "LOG (info) - proxy packet recv size: " << buf_len << std::endl; if (buf_len == -1) { - std::cout << "ERROR (info) - recv" << std::endl; + std::cout << "ERROR (info) - recv -1" << std::endl; return; } - if (buf_len == 0) { break; } + if (buf_len == 0) { + std::cout << "ERROR (info) - recv 0" << std::endl; + return; + } buf_len = send(receiver_s, out_buf, buf_len, 0); std::cout << "LOG (info) - proxy packet send size: " << buf_len << std::endl; - } while (buf_len < BUF_SIZE); + } while (buf_len == BUF_SIZE); std::cout << "Sent to browser" << std::endl; close(sender_s); close(receiver_s); diff --git a/src/web_server.cpp b/src/web_server.cpp index 348766e..10e8365 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -121,46 +121,42 @@ void ClientRequest(int client_s) { // Receive the GET request from the Web browser retcode = recv(client_s, in_buf, BUF_SIZE, 0); + if (retcode == -1) { std::cerr << "ERROR (info) - recv" << std::endl; } // Handle the GET if there is one (see note #3 in the header) - if (retcode != -1) - { - // Parse out the filename from the GET request - strtok(in_buf, " "); - file_name = strtok(NULL, " "); + // Parse out the filename from the GET request + strtok(in_buf, " "); + file_name = strtok(NULL, " "); - // Open the requested file - // - Start at 2nd char to get rid of leading "\" + // Open the requested file + // - Start at 2nd char to get rid of leading "\" #ifdef WIN - fh = open(&file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE); + fh = open(&file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE); #endif #ifdef UNIX - fh = open(&file_name[1], O_RDONLY, S_IREAD | S_IWRITE); + fh = open(&file_name[1], O_RDONLY, S_IREAD | S_IWRITE); #endif - // Generate and send the response (404 if could not open the file) - if (fh == -1) - { - printf("File %s not found - sending an HTTP 404 \n", &file_name[1]); - strcpy(out_buf, NOTOK_404); - send(client_s, out_buf, strlen(out_buf), 0); - strcpy(out_buf, MESS_404); - send(client_s, out_buf, strlen(out_buf), 0); - } - else - { - printf("File %s is being sent \n", &file_name[1]); - if (strstr(file_name, ".gif") != NULL) - strcpy(out_buf, OK_IMAGE); - else - strcpy(out_buf, OK_TEXT); - send(client_s, out_buf, strlen(out_buf), 0); - do { - buf_len = read(fh, out_buf, BUF_SIZE); - send(client_s, out_buf, buf_len, 0); - } while (buf_len > 0); - close(fh); - } - } + // Generate and send the response (404 if could not open the file) + if (fh == -1) + { + printf("File %s not found - sending an HTTP 404 \n", &file_name[1]); + strcpy(out_buf, NOTOK_404); + send(client_s, out_buf, strlen(out_buf), 0); + strcpy(out_buf, MESS_404); + send(client_s, out_buf, strlen(out_buf), 0); + } else { + printf("File %s is being sent \n", &file_name[1]); + if (strstr(file_name, ".gif") != NULL) + strcpy(out_buf, OK_IMAGE); + else + strcpy(out_buf, OK_TEXT); + send(client_s, out_buf, strlen(out_buf), 0); + do { + buf_len = read(fh, out_buf, BUF_SIZE); + buf_len = send(client_s, out_buf, buf_len, 0); + } while (buf_len == BUF_SIZE); + close(fh); + } // Close the client socket and end the thread close(client_s); } -- 2.45.2 From b3c970beb46ba214e923bb71c588a4d31dd1adc3 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Tue, 3 Oct 2023 22:03:21 -0500 Subject: [PATCH 8/8] Fixed endless loading webpage --- src/network.cpp | 30 ++++++++++++++++++------------ src/web_server.cpp | 10 ++++++++++ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/network.cpp b/src/network.cpp index 65acf59..cc8ab8f 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -1,7 +1,8 @@ #include "network.hpp" #include +#include #include // Needed for exit() -#include // Needed for strcpy() and strlen() +#include #include void TestSockets(int sender_s, int receiver_s) { @@ -45,19 +46,24 @@ void TestSockets(int sender_s, int receiver_s) { void PipeSockets(int sender_s, int receiver_s) { char in_buf[BUF_SIZE]; // Input buffer for GET resquest ssize_t buf_len; // Buffer length for file reads - // + // Pass GET along from browser to server std::cout << "Attempting to send data to receiver" << std::endl; do { + // Receive buf_len = recv(sender_s, in_buf, BUF_SIZE, 0); - std::cout << "LOG (info) - pipe packet recv size: " << buf_len << std::endl; + std::cout << "LOG (info) - pipe packet recv size: " << buf_len << '\n'; if (buf_len == -1) { std::cout << "ERROR (info) - recv" << std::endl; + close(sender_s); + close(receiver_s); return; } - if (buf_len == 0) { break; } + if (buf_len == 0) { continue; } + + // Send buf_len = send(receiver_s, in_buf, buf_len, 0); - std::cout << "LOG (info) - pipe packet send size: " << buf_len << std::endl; + std::cout << "LOG (info) - pipe packet send size: " << buf_len << '\n'; } while (buf_len == BUF_SIZE); std::cout << "Sent data to receiver" << std::endl; @@ -70,19 +76,19 @@ void ProxySockets(int sender_s, int receiver_s) { // Pass response along from server to browser std::cout << "Attempting to send to browser" << std::endl; do { + // Receive buf_len = recv(sender_s, out_buf, BUF_SIZE, 0); - std::cout << "LOG (info) - proxy packet recv size: " << buf_len << std::endl; + std::cout << "LOG (info) - proxy packet recv size: " << buf_len << '\n'; if (buf_len == -1) { std::cout << "ERROR (info) - recv -1" << std::endl; return; } - if (buf_len == 0) { - std::cout << "ERROR (info) - recv 0" << std::endl; - return; - } + + // Send buf_len = send(receiver_s, out_buf, buf_len, 0); - std::cout << "LOG (info) - proxy packet send size: " << buf_len << std::endl; - } while (buf_len == BUF_SIZE); + if (buf_len == 96) { break; } + 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; close(sender_s); close(receiver_s); diff --git a/src/web_server.cpp b/src/web_server.cpp index 10e8365..595a8d5 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -34,6 +34,7 @@ #include // Needed for strcpy() and strlen() #include // Needed for file i/o constants #include // Needed for file i/o constants +#include #include #include #include @@ -154,7 +155,16 @@ void ClientRequest(int client_s) { do { buf_len = read(fh, out_buf, BUF_SIZE); buf_len = send(client_s, out_buf, buf_len, 0); + if (buf_len == -1) { + std::cerr << "ERROR (info) - send -1\n"; + std::cerr << "File causing error: " << &file_name[1] << '\n'; + std::cerr << "Errno: " << errno << '\n'; + std::cout << std::endl; + return; + } + std::cout << "LOG (info) - webserver send size: " << buf_len << '\n'; } while (buf_len == BUF_SIZE); + std::cout << std::endl; close(fh); } // Close the client socket and end the thread -- 2.45.2