ESP32/ESP8266 Firewall 1.0.0
a software firewall for ESP23 or ESP8266
Storage.cpp
1#include "Storage.hpp"
2
3namespace fw
4{
5 Storage::Storage()
6 {
7#ifdef ESP8266
8 this->max_rules = 15;
9 this->eeprom_amount_of_rules = 0;
10 this->eeprom_rules_head = 1;
11 this->eeprom_size = this->max_rules * sizeof(firewall_rule_t) + eeprom_rules_head;
12 EEPROM.begin(this->eeprom_size);
13#endif
14 }
15
16 Storage::~Storage()
17 {
18 }
19
20#ifdef ESP8266
21 uint16_t Storage::eeprom_rule_position(uint8_t key)
22 {
23 return eeprom_rules_head + (key - 1) * sizeof(firewall_rule_t);
24 }
25#endif
26
27 uint8_t Storage::retrieve_amount_of_rules()
28 {
29#ifdef ESP8266
30 uint8_t amount_of_rules = EEPROM.read(this->eeprom_amount_of_rules);
31
32 if (amount_of_rules > this->max_rules)
33 return 0;
34#else
35 this->memory.begin("settings", true);
36 const uint8_t amount_of_rules = memory.getUChar("amount_of_rules", 0);
37 this->memory.end();
38#endif
39 return amount_of_rules;
40 }
41
42 void Storage::store_amount_of_rules(const uint8_t new_amount)
43 {
44#ifdef ESP8266
45 EEPROM.put(this->eeprom_amount_of_rules, new_amount);
46 EEPROM.commit();
47#else
48 this->memory.begin("settings", false);
49 this->memory.putUChar("amount_of_rules", new_amount);
50 this->memory.end();
51#endif
52 }
53
54 firewall_rule_t *Storage::retrieve_firewall_rule(const uint8_t key)
55 {
56 firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
57 rule_ptr->key = key;
58#ifdef ESP8266
59 uint16_t eeprom_position = eeprom_rule_position(key);
60
61 EEPROM.get(eeprom_position, rule_ptr->ip);
62 EEPROM.get(eeprom_position += sizeof(rule_ptr->ip), rule_ptr->port_from);
63 EEPROM.get(eeprom_position += sizeof(rule_ptr->port_from), rule_ptr->port_to);
64 EEPROM.get(eeprom_position += sizeof(rule_ptr->port_to), rule_ptr->protocol);
65 EEPROM.get(eeprom_position += sizeof(rule_ptr->protocol), rule_ptr->target);
66#else
67 char rulename[10]; // fwRule99\n
68 sprintf(rulename, "fwRule%i", key);
69
70 this->memory.begin(rulename, true);
71 strncpy(rule_ptr->ip, this->memory.getString(firewall_fields[IP], "0.0.0.0").c_str(), sizeof(rule_ptr->ip));
72 rule_ptr->port_from = this->memory.getUShort(firewall_fields[PORT_FROM], 0);
73 rule_ptr->port_to = this->memory.getUShort(firewall_fields[PORT_TO], 0);
74 rule_ptr->protocol = static_cast<firewall_protocol_t>(this->memory.getUChar(firewall_fields[PROTOCOL], PROTOCOL_ALL));
75 rule_ptr->target = static_cast<firewall_target_t>(this->memory.getUChar(firewall_fields[TARGET], TARGET_ACCEPT));
76 this->memory.end();
77#endif
78 return rule_ptr;
79 }
80
81 void Storage::store_all_firewall_rules(firewall_rule_t *rule_head)
82 {
83 firewall_rule_t *temp = rule_head;
84 while (temp != NULL)
85 {
86 store_firewall_rule(temp);
87 temp = temp->next;
88 }
89 }
90
91 void Storage::store_firewall_rule(firewall_rule_t *rule_ptr)
92 {
93#ifdef ESP8266
94 uint16_t eeprom_position = eeprom_rule_position(rule_ptr->key);
95
96 EEPROM.put(eeprom_position, rule_ptr->ip);
97 EEPROM.put(eeprom_position += sizeof(rule_ptr->ip), rule_ptr->port_from);
98 EEPROM.put(eeprom_position += sizeof(rule_ptr->port_from), rule_ptr->port_to);
99 EEPROM.put(eeprom_position += sizeof(rule_ptr->port_to), rule_ptr->protocol);
100 EEPROM.put(eeprom_position += sizeof(rule_ptr->protocol), rule_ptr->target);
101
102 EEPROM.commit();
103#else
104 char rulename[10]; // fwRule99\n
105 sprintf(rulename, "fwRule%i", rule_ptr->key);
106
107 this->memory.begin(rulename, false);
108 this->memory.putString(firewall_fields[IP], rule_ptr->ip);
109 this->memory.putUShort(firewall_fields[PORT_FROM], rule_ptr->port_from);
110 this->memory.putUShort(firewall_fields[PORT_TO], rule_ptr->port_to);
111 this->memory.putUChar(firewall_fields[PROTOCOL], rule_ptr->protocol);
112 this->memory.putUChar(firewall_fields[TARGET], rule_ptr->target);
113
114 this->memory.end();
115#endif
116 }
117}