fix: truncate empty bytes before using messages

This commit is contained in:
Kevin J. 2024-08-06 22:21:38 +02:00
parent 4fc6665e44
commit 40daff5c44
3 changed files with 50 additions and 31 deletions

View File

@ -11,4 +11,4 @@ clean:
rm -f proxy *.o rm -f proxy *.o
tar: tar:
tar -cvzf ass1.tgz proxy.c README Makefile proxy_parse.c proxy_parse.h tar -cvzf proxy.tgz proxy.c structs.h README Makefile

75
proxy.c
View File

@ -11,7 +11,7 @@
#include "structs.h" #include "structs.h"
int debug = 1; int debug = 1;
struct request *child_msg; struct request *clt_data;
regex_t preg; regex_t preg;
regmatch_t pmatch[REGEX_MATCHN]; regmatch_t pmatch[REGEX_MATCHN];
@ -20,7 +20,9 @@ int statem;
int clt_sock = -1; int clt_sock = -1;
int srv_sock = -1; int srv_sock = -1;
char *clt_msg = NULL; char *clt_msg = NULL;
int clt_msg_len = 0;
char *srv_msg = NULL; char *srv_msg = NULL;
int srv_msg_len = 0;
void *extractsub(const char *msg, regmatch_t match) { void *extractsub(const char *msg, regmatch_t match) {
int buflen = match.rm_eo - match.rm_so; int buflen = match.rm_eo - match.rm_so;
@ -58,16 +60,15 @@ int parse_header(char *msgbuff) {
.value = value .value = value
}; };
int last_index = child_msg->header_num; int last_index = clt_data->header_num;
child_msg->header_num++; clt_data->header_num++;
child_msg->headers = (void *) realloc(child_msg->headers, clt_data->headers = (void *) realloc(clt_data->headers,
child_msg->header_num*sizeof(struct header)); clt_data->header_num*sizeof(struct header));
child_msg->headers[last_index] = new_header; clt_data->headers[last_index] = new_header;
_ok: _ok:
regfree(&preg); regfree(&preg);
return 0; return 0;
@ -87,16 +88,16 @@ int parse_title(char *msgbuff) {
if (ret != 0) if (ret != 0)
goto _err; goto _err;
child_msg->method = extractsub(msgbuff, pmatch[1]); clt_data->method = extractsub(msgbuff, pmatch[1]);
if (child_msg->method == NULL) if (clt_data->method == NULL)
goto _err; goto _err;
child_msg->uri = extractsub(msgbuff, pmatch[2]); clt_data->uri = extractsub(msgbuff, pmatch[2]);
if (child_msg->uri == NULL) if (clt_data->uri == NULL)
goto _err; goto _err;
child_msg->ver = extractsub(msgbuff, pmatch[3]); clt_data->ver = extractsub(msgbuff, pmatch[3]);
if (child_msg->ver == NULL) if (clt_data->ver == NULL)
goto _err; goto _err;
regfree(&preg); regfree(&preg);
@ -109,24 +110,24 @@ _err:
} }
void free_title() { void free_title() {
free(child_msg->method); free(clt_data->method);
free(child_msg->uri); free(clt_data->uri);
free(child_msg->ver); free(clt_data->ver);
} }
void free_headers() { void free_headers() {
for (int i = 0; i < child_msg->header_num; i++) { for (int i = 0; i < clt_data->header_num; i++) {
struct header *header = &child_msg->headers[i]; struct header *header = &clt_data->headers[i];
free(header->key); free(header->key);
free(header->value); free(header->value);
} }
free(child_msg->headers); free(clt_data->headers);
} }
void free_message() { void free_message() {
free_title(); free_title();
free_headers(); free_headers();
free(child_msg); free(clt_data);
} }
int parse_line(char *line, int line_count) { int parse_line(char *line, int line_count) {
@ -143,8 +144,8 @@ int parse_line(char *line, int line_count) {
char *getheader(char *key) { char *getheader(char *key) {
char *ret = NULL; char *ret = NULL;
for (int i = 0; i < child_msg->header_num; i++) { for (int i = 0; i < clt_data->header_num; i++) {
struct header *hdr = &child_msg->headers[i]; struct header *hdr = &clt_data->headers[i];
if (strcmp(hdr->key, key)) if (strcmp(hdr->key, key))
continue; continue;
@ -162,8 +163,8 @@ void do_err(void) {
int do_fwd_clt(void) { int do_fwd_clt(void) {
int bytes = 0; int bytes = 0;
do { do {
bytes += send(clt_sock, srv_msg+bytes, PROXY_MAX_MSGLEN-bytes, 0); bytes += send(clt_sock, srv_msg+bytes, srv_msg_len-bytes, 0);
} while (bytes < PROXY_MAX_MSGLEN); } while (bytes < srv_msg_len);
return 0; return 0;
} }
@ -179,6 +180,8 @@ int do_rcv_srv(void) {
if (ret < 0) if (ret < 0)
return -1; return -1;
srv_msg_len = ret;
if (debug) { if (debug) {
fprintf(stdout, "[%d] Received server message: %s\n", statem, clt_msg); fprintf(stdout, "[%d] Received server message: %s\n", statem, clt_msg);
} }
@ -224,8 +227,8 @@ int do_fwd_srv(void) {
int bytes = 0; int bytes = 0;
do { do {
bytes += send(srv_sock, clt_msg+bytes, PROXY_MAX_MSGLEN-bytes, 0); bytes += send(srv_sock, clt_msg+bytes, clt_msg_len-bytes, 0);
} while (bytes < PROXY_MAX_MSGLEN); } while (bytes < clt_msg_len);
return 0; return 0;
} }
@ -257,6 +260,8 @@ int do_rcv_clt(void) {
if (ret < 0) if (ret < 0)
return -1; return -1;
clt_msg_len = ret;
if (debug) { if (debug) {
fprintf(stdout, "[%d] Received client message: %s\n", statem, clt_msg); fprintf(stdout, "[%d] Received client message: %s\n", statem, clt_msg);
} }
@ -273,13 +278,22 @@ int do_alloc(void) {
if (!srv_msg) if (!srv_msg)
return -1; return -1;
child_msg = (struct request *) calloc(1, sizeof(struct request)); clt_data = (struct request *) calloc(1, sizeof(struct request));
if (!child_msg) if (!clt_data)
return -1; return -1;
return 0; return 0;
} }
void do_clear(void) {
memset(clt_msg, 0, PROXY_MAX_MSGLEN);
memset(srv_msg, 0, PROXY_MAX_MSGLEN);
memset(clt_data, 0, sizeof(struct request));
clt_msg_len = 0;
srv_msg_len = 0;
}
void dostatem() { void dostatem() {
int ret = do_alloc(); int ret = do_alloc();
if (ret < 0) { if (ret < 0) {
@ -322,11 +336,16 @@ void dostatem() {
if (statem & STATEM_FWD_CLT) { if (statem & STATEM_FWD_CLT) {
statem = STATEM_RCV_CLT; statem = STATEM_RCV_CLT;
do_clear();
continue; continue;
} }
statem <<= 1; statem <<= 1;
} }
free_message();
free(clt_msg);
free(srv_msg);
} }
void dohelp() { void dohelp() {

View File

@ -1,14 +1,14 @@
#ifndef STRUCTS_H #ifndef STRUCTS_H
#define STRUCTS_H #define STRUCTS_H
#define MAX_BOUND 100 #define MAX_BOUND 10000
#define SERVER_MODE "server" #define SERVER_MODE "server"
#define CLIENT_MODE "client" #define CLIENT_MODE "client"
#define PROXY_PORT 2020 #define PROXY_PORT 2020
#define PROXY_CONN 20 #define PROXY_CONN 20
#define PROXY_MAX_MSGLEN 1000*1024 #define PROXY_MAX_MSGLEN 10000*1024
#define REGEX_MATCHN 4 #define REGEX_MATCHN 4
#define REGEX_TITLE "^([A-Z]+)[ ]+([a-zA-Z0-9\\:/_.,-]+)"\ #define REGEX_TITLE "^([A-Z]+)[ ]+([a-zA-Z0-9\\:/_.,-]+)"\