This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
esp-firewall/src/Storage.cpp

118 lines
4 KiB
C++
Raw Permalink Normal View History

2022-07-29 10:27:39 +02:00
#include "Storage.hpp"
namespace fw
{
Storage::Storage()
{
2022-07-29 17:25:56 +02:00
#ifdef ESP8266
2022-07-29 10:27:39 +02:00
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);
2022-07-29 17:25:56 +02:00
#endif
2022-07-29 10:27:39 +02:00
}
Storage::~Storage()
{
}
2022-07-29 17:25:56 +02:00
#ifdef ESP8266
2022-07-29 10:27:39 +02:00
uint16_t Storage::eeprom_rule_position(uint8_t key)
{
return eeprom_rules_head + (key - 1) * sizeof(firewall_rule_t);
}
2022-07-29 17:25:56 +02:00
#endif
2022-07-29 10:27:39 +02:00
uint8_t Storage::retrieve_amount_of_rules()
{
2022-07-29 17:25:56 +02:00
#ifdef ESP8266
2022-07-29 10:27:39 +02:00
uint8_t amount_of_rules = EEPROM.read(this->eeprom_amount_of_rules);
if (amount_of_rules > this->max_rules)
return 0;
2022-07-29 17:25:56 +02:00
#else
this->memory.begin("settings", true);
const uint8_t amount_of_rules = memory.getUChar("amount_of_rules", 0);
this->memory.end();
#endif
2022-07-29 10:27:39 +02:00
return amount_of_rules;
}
void Storage::store_amount_of_rules(const uint8_t new_amount)
{
2022-07-29 17:25:56 +02:00
#ifdef ESP8266
2022-07-29 10:27:39 +02:00
EEPROM.put(this->eeprom_amount_of_rules, new_amount);
EEPROM.commit();
2022-07-29 17:25:56 +02:00
#else
this->memory.begin("settings", false);
this->memory.putUChar("amount_of_rules", new_amount);
this->memory.end();
#endif
2022-07-29 10:27:39 +02:00
}
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 17:25:56 +02:00
#ifdef ESP8266
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 17:25:56 +02:00
#else
char rulename[10]; // fwRule99\n
sprintf(rulename, "fwRule%i", key);
this->memory.begin(rulename, true);
strncpy(rule_ptr->ip, this->memory.getString(firewall_fields[IP], "0.0.0.0").c_str(), sizeof(rule_ptr->ip));
rule_ptr->port_from = this->memory.getUShort(firewall_fields[PORT_FROM], 0);
rule_ptr->port_to = this->memory.getUShort(firewall_fields[PORT_TO], 0);
rule_ptr->protocol = static_cast<firewall_protocol_t>(this->memory.getUChar(firewall_fields[PROTOCOL], PROTOCOL_ALL));
rule_ptr->target = static_cast<firewall_target_t>(this->memory.getUChar(firewall_fields[TARGET], TARGET_ACCEPT));
this->memory.end();
#endif
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 17:25:56 +02:00
#ifdef ESP8266
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 17:25:56 +02:00
#else
char rulename[10]; // fwRule99\n
sprintf(rulename, "fwRule%i", rule_ptr->key);
this->memory.begin(rulename, false);
this->memory.putString(firewall_fields[IP], rule_ptr->ip);
this->memory.putUShort(firewall_fields[PORT_FROM], rule_ptr->port_from);
this->memory.putUShort(firewall_fields[PORT_TO], rule_ptr->port_to);
this->memory.putUChar(firewall_fields[PROTOCOL], rule_ptr->protocol);
this->memory.putUChar(firewall_fields[TARGET], rule_ptr->target);
this->memory.end();
#endif
2022-07-29 10:27:39 +02:00
}
}