rule can be written to eeprom

This commit is contained in:
Florian Hoss 2022-04-18 17:35:52 +02:00
parent d7520c15fd
commit d017f28254
3 changed files with 33 additions and 42 deletions

View file

@ -57,54 +57,44 @@ firewall_target_t ESPFirewall::string_to_target(String &target)
void ESPFirewall::setup_eeprom() void ESPFirewall::setup_eeprom()
{ {
EEPROM.begin(this->eeprom_size); EEPROM.begin(this->eeprom_size);
this->eeprom_settings_head = 0; this->amount_of_rules = 0;
this->amount_of_rules = EEPROM.readUChar(this->eeprom_settings_head); // this->amount_of_rules = EEPROM.readUChar(this->eeprom_settings_head);
this->eeprom_rules_head = sizeof(this->amount_of_rules);
log_i("Amount of Rules %i", this->amount_of_rules); log_i("Amount of Rules %i", this->amount_of_rules);
} }
void ESPFirewall::eeprom_write_settings()
{
EEPROM.writeUChar(this->eeprom_settings_head, this->amount_of_rules);
EEPROM.commit();
}
void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr) void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr)
{ {
EEPROM.writeUChar(this->eeprom_settings_head, this->amount_of_rules); EEPROM.write(this->eeprom_settings_head, this->amount_of_rules);
EEPROM.writeString(this->eeprom_rules_head, rule_ptr->source); EEPROM.writeString(this->eeprom_rules_head, rule_ptr->source);
this->eeprom_rules_head += IP4ADDR_STRLEN_MAX; this->eeprom_rules_head += IP4ADDR_STRLEN_MAX;
EEPROM.writeString(this->eeprom_rules_head, rule_ptr->destination); EEPROM.writeString(this->eeprom_rules_head, rule_ptr->destination);
this->eeprom_rules_head += IP4ADDR_STRLEN_MAX; this->eeprom_rules_head += IP4ADDR_STRLEN_MAX;
EEPROM.writeUChar(this->eeprom_rules_head, rule_ptr->protocol); EEPROM.write(this->eeprom_rules_head, rule_ptr->protocol);
this->eeprom_rules_head += sizeof(firewall_protocol_t); this->eeprom_rules_head += sizeof(firewall_protocol_t);
EEPROM.writeUChar(this->eeprom_rules_head, rule_ptr->target); EEPROM.write(this->eeprom_rules_head, rule_ptr->target);
this->eeprom_rules_head += sizeof(firewall_target_t); this->eeprom_rules_head += sizeof(firewall_target_t);
EEPROM.commit(); EEPROM.commit();
// eeprom_read_firewall_rules();
} }
void ESPFirewall::eeprom_read_firewall_rules() void ESPFirewall::eeprom_read_firewall_rules()
{ {
int eeprom_address = sizeof(this->amount_of_rules); int eeprom_address = eeprom_start_firewall_rules;
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); char source[IP4ADDR_STRLEN_MAX];
char destination[IP4ADDR_STRLEN_MAX];
strcpy(rule_ptr->source, EEPROM.readString(eeprom_address).c_str()); strcpy(source, EEPROM.readString(eeprom_address).c_str());
eeprom_address += IP4ADDR_STRLEN_MAX; eeprom_address += IP4ADDR_STRLEN_MAX;
strcpy(rule_ptr->destination, EEPROM.readString(eeprom_address).c_str()); strcpy(destination, EEPROM.readString(eeprom_address).c_str());
eeprom_address += IP4ADDR_STRLEN_MAX; eeprom_address += IP4ADDR_STRLEN_MAX;
// rule_ptr->protocol = firewall_protocol_t(EEPROM.readUChar(eeprom_address)); firewall_protocol_t protocol = static_cast<firewall_protocol_t>(EEPROM.read(eeprom_address));
// eeprom_address += sizeof(rule_ptr->protocol); eeprom_address += sizeof(firewall_protocol_t);
// rule_ptr->target = firewall_target_t(EEPROM.readUChar(eeprom_address)); firewall_protocol_t target = static_cast<firewall_protocol_t>(EEPROM.read(eeprom_address));
// eeprom_address += sizeof(rule_ptr->target); eeprom_address += sizeof(firewall_target_t);
log_i("Amount: %i, Source: %s, Destination: %s, Protocol: %s, Target: %s", log_i("Amount: %i, Source: %s, Destination: %s, Protocol: %u, Target: %u",
this->amount_of_rules, this->amount_of_rules,
rule_ptr->source, source,
rule_ptr->destination, destination,
rule_ptr->protocol, protocol,
rule_ptr->target); target);
free(rule_ptr);
} }
void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule) void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule)
@ -185,7 +175,6 @@ bool ESPFirewall::delete_rule_from_firewall(int key)
} }
free(current_rule_ptr); free(current_rule_ptr);
this->amount_of_rules--; this->amount_of_rules--;
this->eeprom_write_settings();
return true; return true;
} }
@ -263,6 +252,7 @@ void ESPFirewall::get_firewall_rule_handler(AsyncWebServerRequest *request)
void ESPFirewall::get_firewall_rules_handler(AsyncWebServerRequest *request) void ESPFirewall::get_firewall_rules_handler(AsyncWebServerRequest *request)
{ {
eeprom_read_firewall_rules();
String response = construct_json_firewall(); String response = construct_json_firewall();
request->send(200, "application/json", response); request->send(200, "application/json", response);
} }

View file

@ -14,18 +14,20 @@
#endif #endif
#include "ESPAsyncWebServer.h" #include "ESPAsyncWebServer.h"
typedef enum #define eeprom_start_firewall_rules 4
typedef enum firewall_target : uint8_t
{ {
FW_REJECT = 0b0001, FW_REJECT = 0,
FW_DROP = 0b0010, FW_DROP = 1,
FW_ACCEPT = 0b0011, FW_ACCEPT = 2,
} firewall_target_t; } firewall_target_t;
typedef enum typedef enum firewall_protocol : uint8_t
{ {
FW_TCP = 0b0001, FW_TCP = 0,
FW_UDP = 0b0010, FW_UDP = 1,
FW_ALL = 0b1111, FW_ALL = 255,
} firewall_protocol_t; } firewall_protocol_t;
typedef struct firewall_rule typedef struct firewall_rule
@ -42,8 +44,8 @@ class ESPFirewall
{ {
uint16_t eeprom_size = 512; uint16_t eeprom_size = 512;
uint8_t amount_of_rules = 0; uint8_t amount_of_rules = 0;
int eeprom_settings_head; int eeprom_settings_head = 0;
int eeprom_rules_head; int eeprom_rules_head = eeprom_start_firewall_rules;
struct firewall_rule *head = NULL; struct firewall_rule *head = NULL;
AsyncWebServer *firewall_api; AsyncWebServer *firewall_api;
@ -56,7 +58,6 @@ class ESPFirewall
// EEPROM // EEPROM
void setup_eeprom(); void setup_eeprom();
void eeprom_write_settings();
void eeprom_write_firewall_rule(firewall_rule_t *rule); void eeprom_write_firewall_rule(firewall_rule_t *rule);
void eeprom_read_firewall_rules(); void eeprom_read_firewall_rules();

View file

@ -8,7 +8,7 @@ ESPFirewall *firewall;
void setup_wifi() void setup_wifi()
{ {
uint8_t max_retries = 20; uint8_t max_retries = 10;
uint8_t retries = 1; uint8_t retries = 1;
log_i("Attempting to connect to WPA SSID: %s", ssid); log_i("Attempting to connect to WPA SSID: %s", ssid);
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);