Add new rules to the frame thing magic watsit
This commit is contained in:
parent
591549bcff
commit
2f8ab6c8e3
4 changed files with 81 additions and 2 deletions
|
@ -4,7 +4,7 @@ import sys
|
|||
import time
|
||||
|
||||
sys.stdout.write(b"A".decode())
|
||||
x = "hello world!"
|
||||
x = "65.108.144.151 443"
|
||||
sys.stdout.write(struct.pack("b", len(x)).decode())
|
||||
sys.stdout.write(x)
|
||||
sys.stdout.flush()
|
||||
|
|
|
@ -2,10 +2,31 @@
|
|||
// Created by akp on 11/11/23.
|
||||
//
|
||||
|
||||
#include "../../firewall_parser.c"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "persist.c"
|
||||
|
||||
// Functions in this file return zero if successful or non-zero otherwise.
|
||||
|
||||
int handle_add_rule(char *data, size_t len) {
|
||||
// Add rule should provide a rule as per exercise 1 definitions.
|
||||
|
||||
struct Rule *parse_res = (struct Rule*)malloc(sizeof(struct Rule));
|
||||
|
||||
char *eptr = parse_rule(data, parse_res);
|
||||
if (eptr == NULL) {
|
||||
goto fail;
|
||||
} else if ((eptr - data) != len) {
|
||||
// This means we've not consumed everything which means that there's extraneous data?
|
||||
goto fail;
|
||||
}
|
||||
|
||||
add_frame(parse_res);
|
||||
|
||||
free_rule(parse_res);
|
||||
return 0;
|
||||
fail:
|
||||
free_rule(parse_res);
|
||||
return 1;
|
||||
}
|
49
firewall-server/server_includes/persist.c
Normal file
49
firewall-server/server_includes/persist.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// Created by akp on 11/11/23.
|
||||
//
|
||||
|
||||
#include "../../firewall_types.c"
|
||||
#include <string.h>
|
||||
|
||||
struct LogFrame {
|
||||
struct LogFrame *next;
|
||||
struct IP *ip;
|
||||
Port port;
|
||||
};
|
||||
|
||||
struct Frame {
|
||||
struct Frame *next;
|
||||
struct Rule rule;
|
||||
struct LogFrame *log;
|
||||
};
|
||||
|
||||
struct Frame* head = NULL;
|
||||
|
||||
struct Frame** get_last_frame() {
|
||||
struct Frame** cursor = &head;
|
||||
while (1) {
|
||||
if (*cursor == NULL) {
|
||||
break;
|
||||
}
|
||||
if ((*cursor)->next == NULL) {
|
||||
break;
|
||||
}
|
||||
cursor = &((*cursor)->next);
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
|
||||
void add_frame(struct Rule* rule) {
|
||||
struct Frame** last_frame = get_last_frame();
|
||||
|
||||
struct Frame* new_frame = (struct Frame*) malloc(sizeof(struct Frame));
|
||||
memset(new_frame, 0, sizeof(struct Frame));
|
||||
|
||||
if (*last_frame == NULL) {
|
||||
*last_frame = new_frame;
|
||||
} else {
|
||||
(*last_frame)->next = new_frame;
|
||||
}
|
||||
|
||||
new_frame->rule = *rule;
|
||||
}
|
|
@ -10,6 +10,7 @@
|
|||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include "handlers.c"
|
||||
|
||||
int open_socket(const char *ip, const char *port, int *res_sd) {
|
||||
// get address info
|
||||
|
@ -112,6 +113,14 @@ void handle_connection(int *sdptr) {
|
|||
|
||||
fprintf(stderr, "[%d] Data: %s\n", sd, data);
|
||||
|
||||
switch (operation) {
|
||||
case 'A': {
|
||||
handle_add_rule(data, message_length);
|
||||
// TODO: proper error handling
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (send(sd, "lmao cool\n", 10, 0) == -1) {
|
||||
perror("warning: failed to send data to remote host");
|
||||
goto data_cleanup;
|
||||
|
@ -122,7 +131,7 @@ data_cleanup:
|
|||
cleanup:
|
||||
free(leader);
|
||||
|
||||
printf("[%d] end\n", sd);
|
||||
fprintf(stderr, "[%d] end\n", sd);
|
||||
|
||||
if (shutdown(sd, 2) != 0) {
|
||||
perror("warning: shutdown connection in thread failed");
|
||||
|
|
Reference in a new issue