117 lines
4 KiB
C++
117 lines
4 KiB
C++
#include "Storage.hpp"
|
|
|
|
namespace fw
|
|
{
|
|
Storage::Storage()
|
|
{
|
|
#ifdef ESP8266
|
|
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);
|
|
#endif
|
|
}
|
|
|
|
Storage::~Storage()
|
|
{
|
|
}
|
|
|
|
#ifdef ESP8266
|
|
uint16_t Storage::eeprom_rule_position(uint8_t key)
|
|
{
|
|
return eeprom_rules_head + (key - 1) * sizeof(firewall_rule_t);
|
|
}
|
|
#endif
|
|
|
|
uint8_t Storage::retrieve_amount_of_rules()
|
|
{
|
|
#ifdef ESP8266
|
|
uint8_t amount_of_rules = EEPROM.read(this->eeprom_amount_of_rules);
|
|
|
|
if (amount_of_rules > this->max_rules)
|
|
return 0;
|
|
#else
|
|
this->memory.begin("settings", true);
|
|
const uint8_t amount_of_rules = memory.getUChar("amount_of_rules", 0);
|
|
this->memory.end();
|
|
#endif
|
|
return amount_of_rules;
|
|
}
|
|
|
|
void Storage::store_amount_of_rules(const uint8_t new_amount)
|
|
{
|
|
#ifdef ESP8266
|
|
EEPROM.put(this->eeprom_amount_of_rules, new_amount);
|
|
EEPROM.commit();
|
|
#else
|
|
this->memory.begin("settings", false);
|
|
this->memory.putUChar("amount_of_rules", new_amount);
|
|
this->memory.end();
|
|
#endif
|
|
}
|
|
|
|
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;
|
|
#ifdef ESP8266
|
|
uint16_t eeprom_position = eeprom_rule_position(key);
|
|
|
|
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);
|
|
#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
|
|
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)
|
|
{
|
|
#ifdef ESP8266
|
|
uint16_t eeprom_position = eeprom_rule_position(rule_ptr->key);
|
|
|
|
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);
|
|
|
|
EEPROM.commit();
|
|
#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
|
|
}
|
|
}
|