2023-09-30 14:28:48 -05:00
|
|
|
//----- Include files ---------------------------------------------------------
|
|
|
|
#include <stdio.h> // Needed for printf()
|
|
|
|
#include <stdlib.h> // Needed for exit()
|
|
|
|
#include <string.h> // Needed for strcpy() and strlen()
|
|
|
|
#include <fcntl.h> // Needed for file i/o constants
|
|
|
|
#include <sys/stat.h> // Needed for file i/o constants
|
|
|
|
#include <future>
|
|
|
|
|
|
|
|
#include <sys/types.h> //
|
|
|
|
#include <netinet/in.h> //
|
|
|
|
#include <sys/socket.h> //
|
|
|
|
#include <arpa/inet.h> //
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
|
|
//----- HTTP response messages ----------------------------------------------
|
|
|
|
#define OK_IMAGE "HTTP/1.0 200 OK\nContent-Type:image/gif\n\n"
|
|
|
|
#define OK_TEXT "HTTP/1.0 200 OK\nContent-Type:text/html\n\n"
|
|
|
|
#define NOTOK_404 "HTTP/1.0 404 Not Found\nContent-Type:text/html\n\n"
|
|
|
|
#define MESS_404 "<html><body><h1>FILE NOT FOUND</h1></body></html>"
|
|
|
|
|
|
|
|
//----- Defines -------------------------------------------------------------
|
|
|
|
#define BUF_SIZE 4096 // Buffer size (big enough for a GET)
|
2023-09-30 17:13:09 -05:00
|
|
|
#define PORT_NUM 9080 // Port number for a Web server
|
2023-09-30 14:28:48 -05:00
|
|
|
|
|
|
|
//----- Function prototypes -------------------------------------------------
|
2023-09-30 17:13:09 -05:00
|
|
|
void BrowserToProxy(int server_s, int client_s);
|
|
|
|
void ProxyToServer(int server_s, int client_s);
|
2023-09-30 14:28:48 -05:00
|
|
|
|
|
|
|
//===== modeule main ========================================================
|
|
|
|
int main(void)
|
|
|
|
{
|
2023-09-30 17:13:09 -05:00
|
|
|
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
|
2023-09-30 14:28:48 -05:00
|
|
|
socklen_t addr_len; // Internet address length
|
|
|
|
|
|
|
|
// Create a socket, fill-in address information, and then bind it
|
2023-09-30 17:13:09 -05:00
|
|
|
proxy_s = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (proxy_s == -1)
|
2023-09-30 14:28:48 -05:00
|
|
|
{
|
|
|
|
printf("ERROR - Unable to create socket on server\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-09-30 17:13:09 -05:00
|
|
|
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));
|
2023-09-30 14:28:48 -05:00
|
|
|
if (bindRet == -1)
|
|
|
|
{
|
|
|
|
printf("ERROR - Unable to bind socket\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
// Listen for connections and then accept
|
2023-09-30 17:13:09 -05:00
|
|
|
listen(proxy_s, 100);
|
2023-09-30 14:28:48 -05:00
|
|
|
|
|
|
|
// Main loop to listen, accept, and then spin-off a thread to handle the GET
|
|
|
|
while (1)
|
|
|
|
{
|
2023-09-30 17:13:09 -05:00
|
|
|
addr_len = sizeof(browser_addr);
|
2023-09-30 14:28:48 -05:00
|
|
|
|
2023-09-30 17:13:09 -05:00
|
|
|
browser_s = accept(proxy_s, (struct sockaddr *)&browser_addr, &addr_len);
|
|
|
|
if (browser_s == -1)
|
2023-09-30 14:28:48 -05:00
|
|
|
{
|
|
|
|
perror("ERROR - Unable to create socket to client\n");
|
|
|
|
continue;
|
|
|
|
}
|
2023-09-30 17:13:09 -05:00
|
|
|
unsigned int server_s = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
connect(server_s, (struct sockaddr *)&server_addr, addr_len);
|
2023-09-30 14:28:48 -05:00
|
|
|
|
2023-09-30 17:13:09 -05:00
|
|
|
std::async(std::launch::async, BrowserToProxy, browser_s, server_s);
|
|
|
|
std::async(std::launch::async, ProxyToServer, server_s, browser_s);
|
2023-09-30 14:28:48 -05:00
|
|
|
|
|
|
|
}
|
2023-09-30 17:13:09 -05:00
|
|
|
close(browser_s);
|
2023-09-30 14:28:48 -05:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2023-09-30 17:13:09 -05:00
|
|
|
void BrowserToProxy(int server_s, int client_s) {
|
2023-09-30 14:28:48 -05:00
|
|
|
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
|
2023-09-30 17:13:09 -05:00
|
|
|
ssize_t buf_len; // Buffer length for file reads
|
2023-09-30 14:28:48 -05:00
|
|
|
unsigned int retcode; // Return code
|
|
|
|
|
|
|
|
// Receive the GET request from the Web browser
|
2023-09-30 17:13:09 -05:00
|
|
|
do {
|
|
|
|
buf_len = recv(server_s, in_buf, BUF_SIZE, 0);
|
|
|
|
send(client_s, in_buf, buf_len, 0);
|
|
|
|
} while (buf_len != 0);
|
2023-09-30 14:28:48 -05:00
|
|
|
}
|
|
|
|
|
2023-09-30 17:13:09 -05:00
|
|
|
void ProxyToServer(int server_s, int client_s) {
|
|
|
|
/*
|
|
|
|
|
|
|
|
loop {
|
|
|
|
recv from browser
|
|
|
|
send to server
|
|
|
|
}
|
2023-09-30 14:28:48 -05:00
|
|
|
|
2023-09-30 17:13:09 -05:00
|
|
|
*/
|
2023-09-30 14:28:48 -05:00
|
|
|
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
|
2023-09-30 17:13:09 -05:00
|
|
|
ssize_t buf_len; // Buffer length for file reads
|
|
|
|
unsigned int retcode; // Return code
|
2023-09-30 14:28:48 -05:00
|
|
|
|
|
|
|
// Receive the GET request from the Web browser
|
2023-09-30 17:13:09 -05:00
|
|
|
do {
|
|
|
|
buf_len = recv(server_s, in_buf, BUF_SIZE, 0);
|
|
|
|
send(client_s, in_buf, buf_len, 0);
|
|
|
|
} while (buf_len != 0);
|
|
|
|
close(server_s);
|
|
|
|
close(client_s);
|
2023-09-30 14:28:48 -05:00
|
|
|
}
|