From 66dffbc2fc55d7cdd2236a9eeadfcdf496a0e6b1 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Mon, 18 Apr 2022 17:55:34 +0200 Subject: [PATCH] create firewall rule set on startup --- SourceCode/arduino/lib/Firewall/Firewall.cpp | 30 ++++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/SourceCode/arduino/lib/Firewall/Firewall.cpp b/SourceCode/arduino/lib/Firewall/Firewall.cpp index de0cd1a..5e18928 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.cpp +++ b/SourceCode/arduino/lib/Firewall/Firewall.cpp @@ -3,6 +3,7 @@ ESPFirewall::ESPFirewall(int port) { this->setup_eeprom(); + this->eeprom_read_firewall_rules(); log_i("Starting Firewall-API on %i", port); this->firewall_api = new AsyncWebServer(port); this->setup_routing(); @@ -57,9 +58,6 @@ firewall_target_t ESPFirewall::string_to_target(String &target) void ESPFirewall::setup_eeprom() { EEPROM.begin(this->eeprom_size); - this->amount_of_rules = 0; - // this->amount_of_rules = EEPROM.readUChar(this->eeprom_settings_head); - log_i("Amount of Rules %i", this->amount_of_rules); } void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr) @@ -87,15 +85,18 @@ void ESPFirewall::eeprom_read_firewall_rule(uint8_t &eeprom_address) eeprom_address += sizeof(firewall_protocol_t); rule_ptr->target = static_cast(EEPROM.read(eeprom_address)); eeprom_address += sizeof(firewall_target_t); - log_i("Source: %s, Destination: %s, Protocol: %u, Target: %u", + add_rule_to_firewall(rule_ptr); + log_i("%s, %s, %s, %s", rule_ptr->source, rule_ptr->destination, - rule_ptr->protocol, - rule_ptr->target); + protocol_to_string(rule_ptr->protocol), + target_to_string(rule_ptr->target)); } void ESPFirewall::eeprom_read_firewall_rules() { + this->amount_of_rules = EEPROM.readUChar(this->eeprom_settings_head); + log_i("Amount of existing Rules %i", this->amount_of_rules); uint8_t eeprom_address = eeprom_start_firewall_rules; for (uint8_t i = 0; i < this->amount_of_rules; i++) { @@ -103,29 +104,29 @@ void ESPFirewall::eeprom_read_firewall_rules() } } -void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule) +void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule_ptr) { firewall_rule_t *temp; - if (head == NULL) + if (this->head == NULL) { - head = rule; - rule->next = NULL; + this->head = rule_ptr; + rule_ptr->next = NULL; return; } - temp = head; + temp = this->head; while (temp->next != NULL) { temp = temp->next; } - temp->next = rule; - rule->next = NULL; + temp->next = rule_ptr; + rule_ptr->next = NULL; return; } firewall_rule_t *ESPFirewall::get_rule_from_firewall(int key) { firewall_rule_t *rule_ptr = this->head; - if (head == NULL) + if (this->head == NULL) { return NULL; } @@ -258,7 +259,6 @@ void ESPFirewall::get_firewall_rule_handler(AsyncWebServerRequest *request) void ESPFirewall::get_firewall_rules_handler(AsyncWebServerRequest *request) { - eeprom_read_firewall_rules(); String response = construct_json_firewall(); request->send(200, "application/json", response); }