first tests are working

This commit is contained in:
Florian Hoss 2022-04-24 14:34:56 +02:00
parent 912165d9ee
commit ffee834ccb
3 changed files with 21 additions and 8 deletions

View file

@ -5,6 +5,8 @@ namespace fw
Rules::Rules() Rules::Rules()
{ {
this->amount_of_rules = retrieve_settings_value("amount_of_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++) for (uint8_t i = 1; i <= this->amount_of_rules; i++)
{ {
firewall_rule_t *rule_ptr = retrieve_firewall_rule(i); firewall_rule_t *rule_ptr = retrieve_firewall_rule(i);

View file

@ -42,8 +42,14 @@ namespace fw
uint16_t Storage::eeprom_rule_position(uint8_t key) 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 // 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) uint8_t Storage::retrieve_settings_value(const char *key)
@ -57,12 +63,12 @@ namespace fw
return value; return value;
#elif defined(ESP8266) #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 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 amount_of_rules;
return 0; return 0;
} }
@ -77,10 +83,10 @@ namespace fw
this->memory.putUChar(key, new_amount); this->memory.putUChar(key, new_amount);
this->memory.end(); this->memory.end();
#elif defined(ESP8266) #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, this->security_number);
EEPROM.write(this->eeprom_settings_head + 1, new_amount); EEPROM.write(this->eeprom_amout_of_rules_head, new_amount);
EEPROM.commit(); EEPROM.commit();
} }
#endif #endif
@ -103,8 +109,12 @@ namespace fw
#elif defined(ESP8266) #elif defined(ESP8266)
uint16_t eespom_position = eeprom_rule_position(key); 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<firewall_protocol_t>(EEPROM.read(eespom_position)); rule_ptr->protocol = static_cast<firewall_protocol_t>(EEPROM.read(eespom_position));
eespom_position += sizeof(firewall_protocol_t); eespom_position += sizeof(rule_ptr->protocol);
rule_ptr->target = static_cast<firewall_target_t>(EEPROM.read(eespom_position)); rule_ptr->target = static_cast<firewall_target_t>(EEPROM.read(eespom_position));
#endif #endif
return rule_ptr; return rule_ptr;
@ -137,7 +147,7 @@ namespace fw
#elif defined(ESP8266) #elif defined(ESP8266)
uint16_t eespom_position = eeprom_rule_position(rule_ptr->key); uint16_t eespom_position = eeprom_rule_position(rule_ptr->key);
EEPROM.write(this->eeprom_rules_head, rule_ptr->protocol); 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.write(this->eeprom_rules_head, rule_ptr->target);
EEPROM.commit(); EEPROM.commit();
#endif #endif

View file

@ -21,6 +21,7 @@ namespace fw
const uint16_t eeprom_size = 1000; const uint16_t eeprom_size = 1000;
const uint8_t security_number = 93; const uint8_t security_number = 93;
const uint16_t eeprom_settings_head = 0; 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; const uint16_t eeprom_rules_head = 10;
#endif #endif
ok_t mount_spiffs(); ok_t mount_spiffs();