|
|
|
@ -1,12 +1,12 @@ |
|
|
|
|
#include "proxy.h" |
|
|
|
|
|
|
|
|
|
void proxy_startListener(unsigned int port){ |
|
|
|
|
// https://www.geeksforgeeks.org/socket-programming-cc/
|
|
|
|
|
|
|
|
|
|
int server_fd, new_socket; |
|
|
|
|
struct sockaddr_in address; |
|
|
|
|
int opt = 1; |
|
|
|
|
int addrlen = sizeof(address); |
|
|
|
|
char line[1024] = {0}; |
|
|
|
|
char *response; |
|
|
|
|
|
|
|
|
|
// Creating socket file descriptor
|
|
|
|
|
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { |
|
|
|
@ -15,19 +15,20 @@ void proxy_startListener(unsigned int port){ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
address.sin_family = AF_INET; |
|
|
|
|
address.sin_addr.s_addr = htonl(INADDR_ANY); |
|
|
|
|
address.sin_addr.s_addr = INADDR_ANY; |
|
|
|
|
address.sin_port = htons( port ); |
|
|
|
|
|
|
|
|
|
// Forcefully attaching socket to the port 8080
|
|
|
|
|
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) { |
|
|
|
|
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) != 0) { |
|
|
|
|
perror("bind failed"); |
|
|
|
|
return ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (listen(server_fd, 3) < 0) { |
|
|
|
|
if (listen(server_fd, 3) != 0) { |
|
|
|
|
perror("listen"); |
|
|
|
|
return ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
while ( true ){ |
|
|
|
|
printf("Listening\n"); |
|
|
|
|
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
|
|
|
|
@ -37,6 +38,17 @@ void proxy_startListener(unsigned int port){ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Request *request = newRequestFromSocket(new_socket); |
|
|
|
|
|
|
|
|
|
if ( strcmp( request->host, "" ) == 0 ){ |
|
|
|
|
response = webserverGetResponse(request); |
|
|
|
|
} else { |
|
|
|
|
printf("This is a proxy request\n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
send(new_socket , response, strlen(response) , 0 ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//close(new_socket);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|