From d7520c15fd5009bfd4dba727fb4978417e62dde4 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Mon, 18 Apr 2022 13:11:02 +0200 Subject: [PATCH] store amount of rules in eeprom --- SourceCode/arduino/lib/Firewall/Firewall.cpp | 74 +++++++++++++++----- SourceCode/arduino/lib/Firewall/Firewall.h | 10 ++- SourceCode/arduino/src/main.cpp | 5 +- 3 files changed, 66 insertions(+), 23 deletions(-) diff --git a/SourceCode/arduino/lib/Firewall/Firewall.cpp b/SourceCode/arduino/lib/Firewall/Firewall.cpp index a6fc841..ef56373 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.cpp +++ b/SourceCode/arduino/lib/Firewall/Firewall.cpp @@ -2,6 +2,7 @@ ESPFirewall::ESPFirewall(int port) { + this->setup_eeprom(); log_i("Starting Firewall-API on %i", port); this->firewall_api = new AsyncWebServer(port); this->setup_routing(); @@ -53,23 +54,57 @@ firewall_target_t ESPFirewall::string_to_target(String &target) return FW_ACCEPT; } -void ESPFirewall::write_rule_to_eeprom(firewall_rule_t *rule) +void ESPFirewall::setup_eeprom() { - EEPROM.put(this->eeprom_address, rule->source); - this->eeprom_address + IP4ADDR_STRLEN_MAX; - EEPROM.put(this->eeprom_address, rule->destination); - this->eeprom_address + IP4ADDR_STRLEN_MAX; - EEPROM.put(this->eeprom_address, rule->protocol); - this->eeprom_address + sizeof(rule->protocol); - EEPROM.put(this->eeprom_address, rule->target); - this->eeprom_address + sizeof(rule->target); + EEPROM.begin(this->eeprom_size); + this->eeprom_settings_head = 0; + this->amount_of_rules = EEPROM.readUChar(this->eeprom_settings_head); + this->eeprom_rules_head = sizeof(this->amount_of_rules); + log_i("Amount of Rules %i", this->amount_of_rules); } -void ESPFirewall::get_rules_from_eeprom() +void ESPFirewall::eeprom_write_settings() { - int temp_eeprom_address = 0; - char source[IP4ADDR_STRLEN_MAX]; - EEPROM.get(temp_eeprom_address, source); + EEPROM.writeUChar(this->eeprom_settings_head, this->amount_of_rules); + EEPROM.commit(); +} + +void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr) +{ + EEPROM.writeUChar(this->eeprom_settings_head, this->amount_of_rules); + EEPROM.writeString(this->eeprom_rules_head, rule_ptr->source); + this->eeprom_rules_head += IP4ADDR_STRLEN_MAX; + EEPROM.writeString(this->eeprom_rules_head, rule_ptr->destination); + this->eeprom_rules_head += IP4ADDR_STRLEN_MAX; + EEPROM.writeUChar(this->eeprom_rules_head, rule_ptr->protocol); + this->eeprom_rules_head += sizeof(firewall_protocol_t); + EEPROM.writeUChar(this->eeprom_rules_head, rule_ptr->target); + this->eeprom_rules_head += sizeof(firewall_target_t); + EEPROM.commit(); + // eeprom_read_firewall_rules(); +} + +void ESPFirewall::eeprom_read_firewall_rules() +{ + int eeprom_address = sizeof(this->amount_of_rules); + firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); + + strcpy(rule_ptr->source, EEPROM.readString(eeprom_address).c_str()); + eeprom_address += IP4ADDR_STRLEN_MAX; + strcpy(rule_ptr->destination, EEPROM.readString(eeprom_address).c_str()); + eeprom_address += IP4ADDR_STRLEN_MAX; + // rule_ptr->protocol = firewall_protocol_t(EEPROM.readUChar(eeprom_address)); + // eeprom_address += sizeof(rule_ptr->protocol); + // rule_ptr->target = firewall_target_t(EEPROM.readUChar(eeprom_address)); + // eeprom_address += sizeof(rule_ptr->target); + log_i("Amount: %i, Source: %s, Destination: %s, Protocol: %s, Target: %s", + this->amount_of_rules, + rule_ptr->source, + rule_ptr->destination, + rule_ptr->protocol, + rule_ptr->target); + + free(rule_ptr); } void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule) @@ -114,7 +149,7 @@ firewall_rule_t *ESPFirewall::get_rule_from_firewall(int key) bool ESPFirewall::delete_rule_from_firewall(int key) { - if (head == NULL) + if (this->head == NULL) { return false; } @@ -133,10 +168,10 @@ bool ESPFirewall::delete_rule_from_firewall(int key) current_rule_ptr = current_rule_ptr->next; } } - if (current_rule_ptr == head) + if (current_rule_ptr == this->head) { - head = head->next; - temp = head; + this->head = head->next; + temp = this->head; } else { @@ -149,7 +184,8 @@ bool ESPFirewall::delete_rule_from_firewall(int key) temp = temp->next; } free(current_rule_ptr); - amount_of_rules--; + this->amount_of_rules--; + this->eeprom_write_settings(); return true; } @@ -191,7 +227,6 @@ String ESPFirewall::construct_json_firewall() // Size for max 12 Rules StaticJsonDocument<2048> doc; String response; - doc["amount"] = amount_of_rules; JsonArray rules = doc.createNestedArray("rules"); while (rule_ptr != NULL) { @@ -256,6 +291,7 @@ void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request) rule_ptr->target = string_to_target(target); add_rule_to_firewall(rule_ptr); + eeprom_write_firewall_rule(rule_ptr); request->send(200, "application/json", construct_json_firewall_rule(rule_ptr)); } else diff --git a/SourceCode/arduino/lib/Firewall/Firewall.h b/SourceCode/arduino/lib/Firewall/Firewall.h index 41ee6e6..1398a82 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.h +++ b/SourceCode/arduino/lib/Firewall/Firewall.h @@ -40,8 +40,10 @@ typedef struct firewall_rule class ESPFirewall { + uint16_t eeprom_size = 512; uint8_t amount_of_rules = 0; - int eeprom_address = 0; + int eeprom_settings_head; + int eeprom_rules_head; struct firewall_rule *head = NULL; AsyncWebServer *firewall_api; @@ -53,8 +55,10 @@ class ESPFirewall firewall_target_t string_to_target(String &); // EEPROM - void write_rule_to_eeprom(firewall_rule_t *rule); - void get_rules_from_eeprom(); + void setup_eeprom(); + void eeprom_write_settings(); + void eeprom_write_firewall_rule(firewall_rule_t *rule); + void eeprom_read_firewall_rules(); // Firewall Actions void add_rule_to_firewall(firewall_rule_t *); diff --git a/SourceCode/arduino/src/main.cpp b/SourceCode/arduino/src/main.cpp index 618f54f..f01a7ad 100644 --- a/SourceCode/arduino/src/main.cpp +++ b/SourceCode/arduino/src/main.cpp @@ -8,12 +8,15 @@ ESPFirewall *firewall; void setup_wifi() { + uint8_t max_retries = 20; + uint8_t retries = 1; log_i("Attempting to connect to WPA SSID: %s", ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, psk); - while (WiFi.status() != WL_CONNECTED) + while (WiFi.status() != WL_CONNECTED && retries <= max_retries) { delay(1000); + log_i("Connecting... (%i/%i)", retries++, max_retries); } esp_ip_address = WiFi.localIP().toString().c_str(); log_i("Connected, IP Address: %s", esp_ip_address);