Add functional rule add code
This commit is contained in:
parent
2f8ab6c8e3
commit
930108dec2
5 changed files with 38 additions and 22 deletions
12
firewall-server/form_message.py
Normal file
12
firewall-server/form_message.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
#!/usr/bin/env python3
|
||||
import struct
|
||||
import sys
|
||||
import time
|
||||
|
||||
op = sys.argv[1]
|
||||
rest = " ".join(sys.argv[2:])
|
||||
|
||||
sys.stdout.write(op.decode())
|
||||
sys.stdout.write(struct.pack("b", len(rest)).decode())
|
||||
sys.stdout.write(rest)
|
||||
sys.stdout.flush()
|
|
@ -1,10 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
import struct
|
||||
import sys
|
||||
import time
|
||||
|
||||
sys.stdout.write(b"A".decode())
|
||||
x = "65.108.144.151 443"
|
||||
sys.stdout.write(struct.pack("b", len(x)).decode())
|
||||
sys.stdout.write(x)
|
||||
sys.stdout.flush()
|
|
@ -7,26 +7,27 @@
|
|||
#include <stdio.h>
|
||||
#include "persist.c"
|
||||
|
||||
#define EINVALIDRULE 2
|
||||
|
||||
// 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.
|
||||
|
||||
int retval = 0;
|
||||
struct Rule *parse_res = (struct Rule*)malloc(sizeof(struct Rule));
|
||||
|
||||
char *eptr = parse_rule(data, parse_res);
|
||||
if (eptr == NULL) {
|
||||
goto fail;
|
||||
retval = EINVALIDRULE;
|
||||
goto end;
|
||||
} else if ((eptr - data) != len) {
|
||||
// This means we've not consumed everything which means that there's extraneous data?
|
||||
goto fail;
|
||||
retval = EINVALIDRULE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
add_frame(parse_res);
|
||||
|
||||
end:
|
||||
free_rule(parse_res);
|
||||
return 0;
|
||||
fail:
|
||||
free_rule(parse_res);
|
||||
return 1;
|
||||
return retval;
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include "../../firewall_types.c"
|
||||
#include <string.h>
|
||||
#include "pthread.h"
|
||||
|
||||
struct LogFrame {
|
||||
struct LogFrame *next;
|
||||
|
@ -18,6 +19,7 @@ struct Frame {
|
|||
};
|
||||
|
||||
struct Frame* head = NULL;
|
||||
pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
|
||||
|
||||
struct Frame** get_last_frame() {
|
||||
struct Frame** cursor = &head;
|
||||
|
@ -34,6 +36,7 @@ struct Frame** get_last_frame() {
|
|||
}
|
||||
|
||||
void add_frame(struct Rule* rule) {
|
||||
pthread_rwlock_wrlock(&lock);
|
||||
struct Frame** last_frame = get_last_frame();
|
||||
|
||||
struct Frame* new_frame = (struct Frame*) malloc(sizeof(struct Frame));
|
||||
|
@ -46,4 +49,5 @@ void add_frame(struct Rule* rule) {
|
|||
}
|
||||
|
||||
new_frame->rule = *rule;
|
||||
pthread_rwlock_unlock(&lock);
|
||||
}
|
|
@ -113,19 +113,28 @@ void handle_connection(int *sdptr) {
|
|||
|
||||
fprintf(stderr, "[%d] Data: %s\n", sd, data);
|
||||
|
||||
char *response = NULL;
|
||||
switch (operation) {
|
||||
case 'A': {
|
||||
handle_add_rule(data, message_length);
|
||||
// TODO: proper error handling
|
||||
int status = handle_add_rule(data, message_length);
|
||||
if (status != 0) {
|
||||
response = "Invalid rule";
|
||||
} else {
|
||||
response = "Rule added";
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
response = "Illegal request";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (send(sd, "lmao cool\n", 10, 0) == -1) {
|
||||
if (send(sd, response, strlen(response), 0) == -1) {
|
||||
perror("warning: failed to send data to remote host");
|
||||
goto data_cleanup;
|
||||
}
|
||||
|
||||
|
||||
data_cleanup:
|
||||
free(data);
|
||||
cleanup:
|
||||
|
|
Reference in a new issue