2024-07-21 17:43:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2024-07-21 20:41:14 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2024-07-21 17:43:17 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2024-07-31 22:11:41 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
2024-07-21 17:43:17 +00:00
|
|
|
#include <netdb.h>
|
2024-07-31 14:14:50 +00:00
|
|
|
#include <regex.h>
|
2024-07-21 20:41:14 +00:00
|
|
|
#include "structs.h"
|
2024-07-21 17:43:17 +00:00
|
|
|
|
2024-07-21 20:41:14 +00:00
|
|
|
struct http_msg *child_msg;
|
2024-07-31 14:14:50 +00:00
|
|
|
regex_t preg;
|
|
|
|
regmatch_t pmatch[REGEX_MATCHN];
|
|
|
|
|
|
|
|
void *extractsub(const char *msg, regmatch_t match) {
|
|
|
|
int buflen = match.rm_eo - match.rm_so;
|
|
|
|
void *buf = (void *) calloc(1, buflen);
|
|
|
|
if (buf == NULL)
|
|
|
|
goto _return;
|
|
|
|
|
|
|
|
sprintf(buf, "%.*s", buflen, &msg[match.rm_so]);
|
|
|
|
|
|
|
|
_return:
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_header(char *msgbuff) {
|
2024-07-31 22:11:41 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = regcomp(&preg, REGEX_HEADER, REG_EXTENDED);
|
|
|
|
if (ret != 0)
|
|
|
|
goto _err;
|
|
|
|
|
|
|
|
ret = regexec(&preg, msgbuff, REGEX_MATCHN, pmatch, 0);
|
|
|
|
if (ret != 0)
|
|
|
|
goto _err;
|
|
|
|
|
|
|
|
char *key = extractsub(msgbuff, pmatch[1]);
|
|
|
|
if (key == NULL)
|
|
|
|
goto _err;
|
|
|
|
|
|
|
|
char *value = extractsub(msgbuff, pmatch[2]);
|
|
|
|
if (value == NULL)
|
|
|
|
goto _err;
|
|
|
|
|
|
|
|
struct header new_header = {
|
|
|
|
.key = key,
|
|
|
|
.value = value
|
|
|
|
};
|
|
|
|
|
|
|
|
int last_index = child_msg->header_num;
|
|
|
|
|
|
|
|
child_msg->header_num++;
|
|
|
|
child_msg->headers = (void *) realloc(child_msg->headers,
|
|
|
|
child_msg->header_num*sizeof(struct header));
|
|
|
|
|
|
|
|
child_msg->headers[last_index] = new_header;
|
|
|
|
|
|
|
|
regfree(&preg);
|
2024-07-31 14:14:50 +00:00
|
|
|
return 0;
|
2024-07-31 22:11:41 +00:00
|
|
|
|
|
|
|
_err:
|
|
|
|
regfree(&preg);
|
|
|
|
return -1;
|
2024-07-31 14:14:50 +00:00
|
|
|
}
|
2024-07-21 20:41:14 +00:00
|
|
|
|
|
|
|
int parse_title(char *msgbuff) {
|
2024-07-31 14:14:50 +00:00
|
|
|
int ret;
|
2024-07-21 20:41:14 +00:00
|
|
|
|
2024-07-31 14:14:50 +00:00
|
|
|
ret = regcomp(&preg, REGEX_TITLE, REG_EXTENDED);
|
|
|
|
if (ret != 0)
|
|
|
|
goto _err;
|
|
|
|
|
|
|
|
ret = regexec(&preg, msgbuff, REGEX_MATCHN, pmatch, 0);
|
|
|
|
if (ret != 0)
|
|
|
|
goto _err;
|
|
|
|
|
|
|
|
child_msg->method = extractsub(msgbuff, pmatch[1]);
|
|
|
|
if (child_msg->method == NULL)
|
|
|
|
goto _err;
|
2024-07-21 20:41:14 +00:00
|
|
|
|
2024-07-31 14:14:50 +00:00
|
|
|
child_msg->uri = extractsub(msgbuff, pmatch[2]);
|
|
|
|
if (child_msg->uri == NULL)
|
|
|
|
goto _err;
|
|
|
|
|
|
|
|
child_msg->ver = extractsub(msgbuff, pmatch[3]);
|
|
|
|
if (child_msg->ver == NULL)
|
|
|
|
goto _err;
|
|
|
|
|
2024-07-31 22:11:41 +00:00
|
|
|
regfree(&preg);
|
|
|
|
return 0;
|
2024-07-31 14:14:50 +00:00
|
|
|
|
|
|
|
_err:
|
|
|
|
regfree(&preg);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_line(char *line, int line_count) {
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (line_count == 0) {
|
|
|
|
ret = parse_title(line);
|
|
|
|
} else {
|
|
|
|
ret = parse_header(line);
|
2024-07-21 20:41:14 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 14:14:50 +00:00
|
|
|
return ret;
|
2024-07-21 20:41:14 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 22:11:41 +00:00
|
|
|
int test(char *line, int num) {
|
|
|
|
fprintf(stdout, "line %s and num %d\n", line, num);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int par_line = 0;
|
|
|
|
|
2024-07-21 20:41:14 +00:00
|
|
|
void handle_request(int sockfd) {
|
|
|
|
int ret;
|
|
|
|
int id = getpid();
|
2024-07-31 22:11:41 +00:00
|
|
|
|
|
|
|
char *msgbuff = (char *) calloc(1, PROXY_MAX_MSGLEN);
|
|
|
|
if (msgbuff == NULL) {
|
|
|
|
fprintf(stderr, "Not enough dynamic memory\n");
|
|
|
|
goto end_sock;
|
|
|
|
}
|
2024-07-21 20:41:14 +00:00
|
|
|
|
2024-07-31 22:11:41 +00:00
|
|
|
ret = recv(sockfd, msgbuff, PROXY_MAX_MSGLEN, 0);
|
2024-07-21 20:41:14 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "[CHILD %d] Failed to receive data from client\n", id);
|
|
|
|
goto end_sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stdout, "[CHILD %d] Received data from client: %s\n", id, msgbuff);
|
|
|
|
|
|
|
|
// prepare structs
|
|
|
|
child_msg = (struct http_msg *) calloc(1, sizeof(struct http_msg));
|
|
|
|
if (child_msg == NULL) {
|
2024-07-31 22:11:41 +00:00
|
|
|
fprintf(stderr, "[CHILD %d] Failed to allocate memory for client"
|
|
|
|
"structs\n", id);
|
|
|
|
goto end_sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ln = strdup(msgbuff);
|
|
|
|
if (!ln) {
|
|
|
|
fprintf(stdout, "Not enough dynamic memory\n");
|
2024-07-21 20:41:14 +00:00
|
|
|
goto end_sock;
|
|
|
|
}
|
|
|
|
|
2024-07-31 22:11:41 +00:00
|
|
|
ln = strtok(ln, "\n");
|
|
|
|
|
|
|
|
while (ln) {
|
2024-07-31 14:14:50 +00:00
|
|
|
parse_line(ln, par_line);
|
2024-07-31 22:11:41 +00:00
|
|
|
par_line++;
|
|
|
|
ln = strtok(NULL, "\n");
|
2024-07-31 14:14:50 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 22:11:41 +00:00
|
|
|
fprintf(stdout, "I am done here.... zzzz \n");
|
|
|
|
|
|
|
|
for (;;);
|
2024-07-21 20:41:14 +00:00
|
|
|
|
2024-07-31 14:14:50 +00:00
|
|
|
//end_structs:
|
2024-07-31 22:11:41 +00:00
|
|
|
//free(child_msg);
|
2024-07-21 20:41:14 +00:00
|
|
|
|
|
|
|
end_sock:
|
|
|
|
close(sockfd);
|
|
|
|
}
|
2024-07-21 17:43:17 +00:00
|
|
|
|
2024-07-31 22:11:41 +00:00
|
|
|
void dohelp() {
|
|
|
|
printf(
|
|
|
|
"+====================+\n"
|
|
|
|
"| HTTP/1.0 PROXY |\n"
|
|
|
|
"+=====@0xdeadbeer====+\n"
|
|
|
|
"usage:\n"
|
|
|
|
" ./proxy [mode]\n"
|
|
|
|
"mode:\n"
|
|
|
|
" * server -> start listening as proxy\n"
|
|
|
|
" * client -> send test requests to server\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
int doserver(void) {
|
|
|
|
int ret, server_socket;
|
2024-07-21 17:43:17 +00:00
|
|
|
|
2024-07-31 22:11:41 +00:00
|
|
|
ret = server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2024-07-21 17:43:17 +00:00
|
|
|
if (ret < 0) {
|
2024-07-31 22:11:41 +00:00
|
|
|
fprintf(stderr, "Failed to create a socket to listen on\n");
|
|
|
|
return -1;
|
2024-07-21 17:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in serv_addr;
|
2024-07-31 22:11:41 +00:00
|
|
|
|
2024-07-21 17:43:17 +00:00
|
|
|
memset(&serv_addr, 0, sizeof(serv_addr));
|
2024-07-31 22:11:41 +00:00
|
|
|
|
2024-07-21 17:43:17 +00:00
|
|
|
serv_addr.sin_family = AF_INET;
|
|
|
|
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
serv_addr.sin_port = htons(PROXY_PORT);
|
|
|
|
|
2024-07-31 22:11:41 +00:00
|
|
|
ret = bind(server_socket, (struct sockaddr *) &serv_addr,
|
|
|
|
sizeof(serv_addr));
|
2024-07-21 17:43:17 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed to bind to port %d\n", PROXY_PORT);
|
2024-07-31 22:11:41 +00:00
|
|
|
return -1;
|
2024-07-21 17:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = listen(server_socket, PROXY_CONN);
|
|
|
|
if (ret < 0) {
|
2024-07-31 22:11:41 +00:00
|
|
|
fprintf(stderr, "Failed to listen on port %d\n", PROXY_PORT);
|
|
|
|
return -1;
|
2024-07-21 17:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stdout, "Listening on port %d\n", PROXY_PORT);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct sockaddr_in client_addr;
|
2024-07-31 22:11:41 +00:00
|
|
|
socklen_t client_addrlen = sizeof(client_addr);
|
2024-07-21 17:43:17 +00:00
|
|
|
int client_socket;
|
|
|
|
|
2024-07-31 22:11:41 +00:00
|
|
|
ret = client_socket = accept(server_socket, (struct sockaddr *)
|
|
|
|
&client_addr, &client_addrlen);
|
2024-07-21 17:43:17 +00:00
|
|
|
if (ret < 0) {
|
2024-07-31 22:11:41 +00:00
|
|
|
fprintf(stderr, "Failed to establish socket connection with"
|
|
|
|
"client\n");
|
|
|
|
return -1;
|
2024-07-21 17:43:17 +00:00
|
|
|
}
|
|
|
|
|
2024-07-21 20:41:14 +00:00
|
|
|
ret = fork();
|
|
|
|
switch (ret) {
|
|
|
|
case -1:
|
2024-07-31 22:11:41 +00:00
|
|
|
fprintf(stderr, "[CLIENT SOCKET %d] Failed to fork child process to"
|
|
|
|
"handle the request\n", client_socket);
|
|
|
|
return -1;
|
2024-07-21 20:41:14 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
handle_request(client_socket);
|
|
|
|
break;
|
|
|
|
default:
|
2024-07-31 22:11:41 +00:00
|
|
|
fprintf(stdout, "[PROGRAM] Successfully forked a new child process"
|
|
|
|
"with PID %d\n", ret);
|
2024-07-21 20:41:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-07-21 17:43:17 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 22:11:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int doclient(void) {
|
|
|
|
int ret = 0;
|
|
|
|
int client_socket;
|
|
|
|
struct sockaddr_in serv_addr;
|
|
|
|
|
|
|
|
ret = client_socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed creating socket\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&serv_addr, '0', sizeof(serv_addr));
|
|
|
|
|
|
|
|
serv_addr.sin_family = AF_INET;
|
|
|
|
serv_addr.sin_port = htons(PROXY_PORT);
|
|
|
|
|
|
|
|
ret = inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
|
|
|
|
if(ret <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Inet_pton error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = connect(client_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed connecting to remote server\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bytes = 0;
|
|
|
|
do {
|
|
|
|
bytes += send(client_socket, CLIENT_MESSAGE, sizeof(CLIENT_MESSAGE), 0);
|
|
|
|
} while (bytes != sizeof(CLIENT_MESSAGE));
|
|
|
|
|
|
|
|
fprintf(stdout, "Sent %d bytes to server\n", bytes);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int ret;
|
|
|
|
if (argc != 2) {
|
|
|
|
dohelp();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *mode = argv[1];
|
|
|
|
ret = strcmp(mode, SERVER_MODE);
|
|
|
|
if (ret == 0)
|
|
|
|
return doserver();
|
|
|
|
|
|
|
|
ret = strcmp(mode, CLIENT_MODE);
|
|
|
|
if (ret == 0)
|
|
|
|
return doclient();
|
|
|
|
|
|
|
|
fprintf(stderr, "Unknown proxy mode\n");
|
2024-07-21 17:43:17 +00:00
|
|
|
}
|