From c33414ade116cd1c3b901b54abcda1be6f4a4dab Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Mon, 11 Apr 2022 17:46:44 +0200 Subject: [PATCH] firewall routes can be created --- SourceCode/arduino/lib/Firewall/Firewall.cpp | 20 ++++++++++++++------ SourceCode/arduino/lib/Firewall/Firewall.h | 6 +++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/SourceCode/arduino/lib/Firewall/Firewall.cpp b/SourceCode/arduino/lib/Firewall/Firewall.cpp index 2ce6168..06d482c 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.cpp +++ b/SourceCode/arduino/lib/Firewall/Firewall.cpp @@ -7,11 +7,15 @@ ESPFirewall::ESPFirewall(int port) this->setup_routing(); } -void ESPFirewall::add_rule_to_firewall() +void ESPFirewall::add_rule_to_firewall(const char *source, const char *destination, const char *protocol, const char *target) { firewall_rule_t *temp; firewall_rule_t *link = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); link->key = ++amount_of_rules; + strcpy(link->source, source); + strcpy(link->destination, destination); + strcpy(link->protocol, protocol); + strcpy(link->target, target); if (head == NULL) { head = link; @@ -39,6 +43,10 @@ void ESPFirewall::get_firewall_handler(AsyncWebServerRequest *request) { JsonObject rule = rules.createNestedObject(); rule["key"] = ptr->key; + rule["source"] = ptr->source; + rule["destination"] = ptr->destination; + rule["protocol"] = ptr->protocol; + rule["target"] = ptr->target; ptr = ptr->next; } serializeJson(json, response); @@ -52,15 +60,15 @@ void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request) int response_code; if (request->hasArg("source") || request->hasArg("destination") || request->hasArg("protocol") || request->hasArg("target")) { - String source = request->arg("source"); - String destination = request->arg("destination"); - String protocol = request->arg("protocol"); - String target = request->arg("target"); + const char *source = request->arg("source").c_str(); + const char *destination = request->arg("destination").c_str(); + const char *protocol = request->arg("protocol").c_str(); + const char *target = request->arg("target").c_str(); json["source"] = source; json["destination"] = destination; json["protocol"] = protocol; json["target"] = target; - add_rule_to_firewall(); + add_rule_to_firewall(source, destination, protocol, target); response_code = 200; } else diff --git a/SourceCode/arduino/lib/Firewall/Firewall.h b/SourceCode/arduino/lib/Firewall/Firewall.h index 1eb1188..662d601 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.h +++ b/SourceCode/arduino/lib/Firewall/Firewall.h @@ -16,6 +16,10 @@ typedef struct firewall_rule { int key; + char source[IP4ADDR_STRLEN_MAX]; + char destination[IP4ADDR_STRLEN_MAX]; + char protocol[4]; + char target[7]; struct firewall_rule *next; } firewall_rule_t; @@ -25,7 +29,7 @@ class ESPFirewall unsigned int amount_of_rules = 0; struct firewall_rule *head = NULL; - void add_rule_to_firewall(); + void add_rule_to_firewall(const char *source, const char *destination, const char *protocol, const char *target); void get_firewall_handler(AsyncWebServerRequest *request); void post_firewall_handler(AsyncWebServerRequest *request); void not_found(AsyncWebServerRequest *request);