2022-07-29 10:36:46 +02:00
|
|
|
#ifdef ESP8266
|
2022-07-29 10:27:39 +02:00
|
|
|
#include "Storage.hpp"
|
|
|
|
|
|
|
|
namespace fw
|
|
|
|
{
|
|
|
|
Storage::Storage()
|
|
|
|
{
|
|
|
|
this->max_rules = 15;
|
|
|
|
this->eeprom_amount_of_rules = 0;
|
|
|
|
this->eeprom_rules_head = 1;
|
|
|
|
this->eeprom_size = this->max_rules * sizeof(firewall_rule_t) + eeprom_rules_head;
|
|
|
|
EEPROM.begin(this->eeprom_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage::~Storage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t Storage::eeprom_rule_position(uint8_t key)
|
|
|
|
{
|
|
|
|
return eeprom_rules_head + (key - 1) * sizeof(firewall_rule_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t Storage::retrieve_amount_of_rules()
|
|
|
|
{
|
|
|
|
uint8_t amount_of_rules = EEPROM.read(this->eeprom_amount_of_rules);
|
|
|
|
|
|
|
|
if (amount_of_rules > this->max_rules)
|
|
|
|
return 0;
|
|
|
|
return amount_of_rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Storage::store_amount_of_rules(const uint8_t new_amount)
|
|
|
|
{
|
|
|
|
EEPROM.put(this->eeprom_amount_of_rules, new_amount);
|
|
|
|
EEPROM.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
firewall_rule_t *Storage::retrieve_firewall_rule(const uint8_t key)
|
|
|
|
{
|
|
|
|
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
|
|
|
rule_ptr->key = key;
|
2022-07-29 16:18:57 +02:00
|
|
|
uint16_t eeprom_position = eeprom_rule_position(key);
|
2022-07-29 10:27:39 +02:00
|
|
|
|
2022-07-29 16:18:57 +02:00
|
|
|
EEPROM.get(eeprom_position, rule_ptr->ip);
|
|
|
|
EEPROM.get(eeprom_position += sizeof(rule_ptr->ip), rule_ptr->port_from);
|
|
|
|
EEPROM.get(eeprom_position += sizeof(rule_ptr->port_from), rule_ptr->port_to);
|
|
|
|
EEPROM.get(eeprom_position += sizeof(rule_ptr->port_to), rule_ptr->protocol);
|
|
|
|
EEPROM.get(eeprom_position += sizeof(rule_ptr->protocol), rule_ptr->target);
|
2022-07-29 10:27:39 +02:00
|
|
|
return rule_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Storage::store_all_firewall_rules(firewall_rule_t *rule_head)
|
|
|
|
{
|
|
|
|
firewall_rule_t *temp = rule_head;
|
|
|
|
while (temp != NULL)
|
|
|
|
{
|
|
|
|
store_firewall_rule(temp);
|
|
|
|
temp = temp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Storage::store_firewall_rule(firewall_rule_t *rule_ptr)
|
|
|
|
{
|
2022-07-29 16:18:57 +02:00
|
|
|
uint16_t eeprom_position = eeprom_rule_position(rule_ptr->key);
|
2022-07-29 10:27:39 +02:00
|
|
|
|
2022-07-29 16:18:57 +02:00
|
|
|
EEPROM.put(eeprom_position, rule_ptr->ip);
|
|
|
|
EEPROM.put(eeprom_position += sizeof(rule_ptr->ip), rule_ptr->port_from);
|
|
|
|
EEPROM.put(eeprom_position += sizeof(rule_ptr->port_from), rule_ptr->port_to);
|
|
|
|
EEPROM.put(eeprom_position += sizeof(rule_ptr->port_to), rule_ptr->protocol);
|
|
|
|
EEPROM.put(eeprom_position += sizeof(rule_ptr->protocol), rule_ptr->target);
|
2022-07-29 10:27:39 +02:00
|
|
|
|
|
|
|
EEPROM.commit();
|
|
|
|
}
|
|
|
|
}
|
2022-07-29 10:36:46 +02:00
|
|
|
#endif
|