From ffee834ccb67f000ea1da1983303c11dc8cd3398 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Sun, 24 Apr 2022 14:34:56 +0200 Subject: [PATCH] first tests are working --- ESPFirewall/lib/Firewall/src/Rules.cpp | 2 ++ ESPFirewall/lib/Firewall/src/Storage.cpp | 26 ++++++++++++++++-------- ESPFirewall/lib/Firewall/src/Storage.hpp | 1 + 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ESPFirewall/lib/Firewall/src/Rules.cpp b/ESPFirewall/lib/Firewall/src/Rules.cpp index 3a40417..4e80232 100644 --- a/ESPFirewall/lib/Firewall/src/Rules.cpp +++ b/ESPFirewall/lib/Firewall/src/Rules.cpp @@ -5,6 +5,8 @@ namespace fw Rules::Rules() { this->amount_of_rules = retrieve_settings_value("amount_of_rules"); + Serial.print("Firewall Rules: "); + Serial.println(amount_of_rules); for (uint8_t i = 1; i <= this->amount_of_rules; i++) { firewall_rule_t *rule_ptr = retrieve_firewall_rule(i); diff --git a/ESPFirewall/lib/Firewall/src/Storage.cpp b/ESPFirewall/lib/Firewall/src/Storage.cpp index 679403f..7d95e48 100644 --- a/ESPFirewall/lib/Firewall/src/Storage.cpp +++ b/ESPFirewall/lib/Firewall/src/Storage.cpp @@ -42,8 +42,14 @@ namespace fw uint16_t Storage::eeprom_rule_position(uint8_t key) { + firewall_rule_t rule; + uint8_t total_space_needed = 0; + total_space_needed += sizeof(rule.source); + total_space_needed += sizeof(rule.destination); + total_space_needed += sizeof(rule.target); + total_space_needed += sizeof(rule.protocol); // key-1 because key will be in range 1-255, but we need 1 less for multiplication - return eeprom_rules_head + key - 1 * sizeof(firewall_rule_t); + return eeprom_rules_head + key - 1 * total_space_needed; } uint8_t Storage::retrieve_settings_value(const char *key) @@ -57,12 +63,12 @@ namespace fw return value; #elif defined(ESP8266) - if (strncmp("amount_of_rules", key, 16)) + if (strncmp("amount_of_rules", key, 16) == 0) { uint8_t security_number = EEPROM.read(this->eeprom_settings_head); - uint8_t amount_of_rules = EEPROM.read(this->eeprom_settings_head + 1); + uint8_t amount_of_rules = EEPROM.read(this->eeprom_amout_of_rules_head); - if (amount_of_rules > 50 || security_number != this->security_number) + if (amount_of_rules < 50 || security_number != this->security_number) return amount_of_rules; return 0; } @@ -77,10 +83,10 @@ namespace fw this->memory.putUChar(key, new_amount); this->memory.end(); #elif defined(ESP8266) - if (strncmp("amount_of_rules", key, 16)) + if (strncmp("amount_of_rules", key, 16) == 0) { EEPROM.write(this->eeprom_settings_head, this->security_number); - EEPROM.write(this->eeprom_settings_head + 1, new_amount); + EEPROM.write(this->eeprom_amout_of_rules_head, new_amount); EEPROM.commit(); } #endif @@ -103,8 +109,12 @@ namespace fw #elif defined(ESP8266) uint16_t eespom_position = eeprom_rule_position(key); + strncpy(rule_ptr->source, "test", sizeof(rule_ptr->source)); + eespom_position += sizeof(rule_ptr->source); + strncpy(rule_ptr->destination, "test de", sizeof(rule_ptr->destination)); + eespom_position += sizeof(rule_ptr->destination); rule_ptr->protocol = static_cast(EEPROM.read(eespom_position)); - eespom_position += sizeof(firewall_protocol_t); + eespom_position += sizeof(rule_ptr->protocol); rule_ptr->target = static_cast(EEPROM.read(eespom_position)); #endif return rule_ptr; @@ -137,7 +147,7 @@ namespace fw #elif defined(ESP8266) uint16_t eespom_position = eeprom_rule_position(rule_ptr->key); EEPROM.write(this->eeprom_rules_head, rule_ptr->protocol); - eespom_position += sizeof(firewall_protocol_t); + eespom_position += sizeof(rule_ptr->protocol); EEPROM.write(this->eeprom_rules_head, rule_ptr->target); EEPROM.commit(); #endif diff --git a/ESPFirewall/lib/Firewall/src/Storage.hpp b/ESPFirewall/lib/Firewall/src/Storage.hpp index 147b090..79599ac 100644 --- a/ESPFirewall/lib/Firewall/src/Storage.hpp +++ b/ESPFirewall/lib/Firewall/src/Storage.hpp @@ -21,6 +21,7 @@ namespace fw const uint16_t eeprom_size = 1000; const uint8_t security_number = 93; const uint16_t eeprom_settings_head = 0; + const uint16_t eeprom_amout_of_rules_head = eeprom_settings_head + 1; const uint16_t eeprom_rules_head = 10; #endif ok_t mount_spiffs();