Implement delete
This commit is contained in:
parent
2df60b71eb
commit
7a87f6fe34
3 changed files with 83 additions and 0 deletions
|
@ -12,6 +12,7 @@
|
|||
#define HANDLER_INVRULE 2
|
||||
#define HANDLER_ILLEGALARG 3
|
||||
#define HANDLER_FOUND 4
|
||||
#define HANDLER_DELETEDRULE 5
|
||||
|
||||
int handle_add_rule(char *data, size_t len) {
|
||||
// Add rule should provide a rule as per exercise 1 definitions.
|
||||
|
@ -34,6 +35,29 @@ end:
|
|||
return retval;
|
||||
}
|
||||
|
||||
int handle_delete_rule(char *data, size_t len) {
|
||||
int retval = HANDLER_OK;
|
||||
struct Rule *parse_res = (struct Rule*)malloc(sizeof(struct Rule));
|
||||
|
||||
char *eptr = parse_rule(data, parse_res);
|
||||
if (eptr == NULL) {
|
||||
retval = HANDLER_INVRULE;
|
||||
goto end;
|
||||
} else if ((eptr - data) < len) {
|
||||
// This means we've not consumed everything which means that there's extraneous data?
|
||||
retval = HANDLER_INVRULE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (delete_frame(parse_res) == 1) {
|
||||
retval = HANDLER_DELETEDRULE;
|
||||
}
|
||||
|
||||
end:
|
||||
free_rule(parse_res);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int handle_check_rule(char *data, size_t len) {
|
||||
// Check if the specified IP and port is matched by any IP and port
|
||||
struct IP ip;
|
||||
|
@ -60,6 +84,7 @@ int handle_check_rule(char *data, size_t len) {
|
|||
return HANDLER_OK;
|
||||
}
|
||||
|
||||
// Note: this function does not follow the usual return value conventions.
|
||||
int handle_list_rules(int sd) {
|
||||
return list_rules(sd);
|
||||
}
|
|
@ -203,4 +203,51 @@ int list_rules(int sd) {
|
|||
end:
|
||||
pthread_rwlock_unlock(&persist_lock);
|
||||
return status;
|
||||
}
|
||||
|
||||
// Returns 1 if something has been removed, 0 otherwise.
|
||||
int delete_frame(struct Rule* rule) {
|
||||
pthread_rwlock_wrlock(&persist_lock);
|
||||
|
||||
struct Frame *previous = NULL;
|
||||
struct Frame *frame_cursor = persist_head;
|
||||
int hasChanged = 0;
|
||||
|
||||
while (frame_cursor != NULL && hasChanged == 0) {
|
||||
struct Frame *next = frame_cursor->next;
|
||||
|
||||
if (compare_rule(rule, &frame_cursor->rule) == 0) {
|
||||
struct LogFrame *log_cursor = frame_cursor->log;
|
||||
while (log_cursor != NULL) {
|
||||
struct LogFrame *next = log_cursor->next;
|
||||
free(log_cursor);
|
||||
log_cursor = next;
|
||||
}
|
||||
|
||||
if (previous == NULL) {
|
||||
// If the thing we're deleting is the first
|
||||
persist_head = next;
|
||||
} else {
|
||||
previous->next = next;
|
||||
}
|
||||
|
||||
if (frame_cursor->rule.ip.end != NULL) {
|
||||
free(frame_cursor->rule.ip.end);
|
||||
}
|
||||
|
||||
if (frame_cursor->rule.port.end != NULL) {
|
||||
free(frame_cursor->rule.port.end);
|
||||
}
|
||||
|
||||
free(frame_cursor);
|
||||
|
||||
hasChanged = 1;
|
||||
}
|
||||
|
||||
previous = frame_cursor;
|
||||
frame_cursor = next;
|
||||
}
|
||||
|
||||
pthread_rwlock_unlock(&persist_lock);
|
||||
return hasChanged;
|
||||
}
|
|
@ -146,6 +146,17 @@ void handle_connection(int *sdptr) {
|
|||
}
|
||||
break;
|
||||
}
|
||||
case 'D': {
|
||||
int status = handle_delete_rule(data, message_length);
|
||||
if (status == HANDLER_INVRULE) {
|
||||
response = "Rule invalid";
|
||||
} else if (status == HANDLER_DELETEDRULE) {
|
||||
response = "Rule deleted";
|
||||
} else {
|
||||
response = "Rule not found";
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
response = "Illegal request";
|
||||
break;
|
||||
|
|
Reference in a new issue