Add new rules to the frame thing magic watsit

This commit is contained in:
akp 2023-11-11 13:52:16 +00:00
parent 591549bcff
commit 2f8ab6c8e3
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
4 changed files with 81 additions and 2 deletions

View file

@ -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()

View file

@ -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;
}

View 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;
}

View file

@ -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");