use preferences instead of eeprom
This commit is contained in:
parent
e5c061ff32
commit
d05ccef834
2 changed files with 46 additions and 69 deletions
|
@ -2,69 +2,55 @@
|
||||||
|
|
||||||
namespace firewall
|
namespace firewall
|
||||||
{
|
{
|
||||||
Storage::Storage(const uint16_t eeprom_size)
|
Storage::Storage()
|
||||||
{
|
{
|
||||||
this->get_eeprom_hash();
|
|
||||||
this->eeprom_size = eeprom_size;
|
|
||||||
EEPROM.begin(this->eeprom_size);
|
|
||||||
this->amount_of_rules = EEPROM.read(this->settings_head);
|
|
||||||
log_i("Amount of Rules: %i", this->amount_of_rules);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Storage::~Storage()
|
Storage::~Storage()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Storage::clear_eeprom()
|
uint8_t Storage::retrieve_amount_of_firewall_rules()
|
||||||
{
|
{
|
||||||
|
uint8_t amount_of_rules;
|
||||||
for (int i = 0; i < this->eeprom_size; i++)
|
this->preferences.begin("settings", false);
|
||||||
{
|
amount_of_rules = preferences.getUChar("amount_of_rules", 0);
|
||||||
EEPROM.write(i, 0);
|
this->preferences.end();
|
||||||
}
|
return amount_of_rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Storage::get_eeprom_hash()
|
void Storage::store_amount_of_firewall_rules(const uint8_t new_amount)
|
||||||
{
|
{
|
||||||
char buffer[this->eeprom_size] = {0};
|
this->preferences.begin("settings", false);
|
||||||
for (int i = 0; i < this->eeprom_size; i++)
|
this->preferences.putUChar("amount_of_rules", new_amount);
|
||||||
{
|
this->preferences.end();
|
||||||
buffer[i] = EEPROM.readChar(i);
|
}
|
||||||
}
|
|
||||||
unsigned char *hashedPayload = get_hash(buffer);
|
firewall_rule_t *Storage::retrieve_firewall_rule(const uint8_t key)
|
||||||
for (int i = 0; i < sizeof(hashedPayload); i++)
|
{
|
||||||
{
|
char rulename[12];
|
||||||
char str[3];
|
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
||||||
sprintf(str, "%02x", (int)hashedPayload[i]);
|
sprintf(rulename, "fw_rule_%i", key);
|
||||||
Serial.print(str);
|
this->preferences.begin(rulename, false);
|
||||||
}
|
rule_ptr->key = key;
|
||||||
|
strcpy(rule_ptr->source, this->preferences.getString("source").c_str());
|
||||||
|
strcpy(rule_ptr->destination, this->preferences.getString("destination").c_str());
|
||||||
|
rule_ptr->protocol = static_cast<firewall_protocol_t>(this->preferences.getUChar("protocol"));
|
||||||
|
rule_ptr->target = static_cast<firewall_target_t>(this->preferences.getUChar("target"));
|
||||||
|
this->preferences.end();
|
||||||
|
return rule_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Storage::store_firewall_rule(const uint8_t &new_amount, firewall_rule_t *rule_ptr)
|
||||||
|
{
|
||||||
|
this->store_amount_of_firewall_rules(new_amount);
|
||||||
|
char rulename[12];
|
||||||
|
sprintf(rulename, "fw_rule_%i", rule_ptr->key);
|
||||||
|
this->preferences.begin(rulename, false);
|
||||||
|
this->preferences.putString("source", rule_ptr->source);
|
||||||
|
this->preferences.putString("destination", rule_ptr->destination);
|
||||||
|
this->preferences.putUChar("protocol", rule_ptr->protocol);
|
||||||
|
this->preferences.putUChar("target", rule_ptr->target);
|
||||||
|
this->preferences.end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *Storage::get_hash(const char *payload)
|
|
||||||
{
|
|
||||||
unsigned char hashedPayload[32];
|
|
||||||
mbedtls_md_context_t ctx;
|
|
||||||
mbedtls_md_type_t md_type = MBEDTLS_MD_MD5;
|
|
||||||
const size_t payloadLength = strlen(payload);
|
|
||||||
|
|
||||||
mbedtls_md_init(&ctx);
|
|
||||||
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 0);
|
|
||||||
mbedtls_md_starts(&ctx);
|
|
||||||
mbedtls_md_update(&ctx, (const unsigned char *)payload, payloadLength);
|
|
||||||
mbedtls_md_finish(&ctx, hashedPayload);
|
|
||||||
mbedtls_md_free(&ctx);
|
|
||||||
|
|
||||||
return hashedPayload;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t Storage::get_amount_of_firewall_rules()
|
|
||||||
{
|
|
||||||
return this->amount_of_rules;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Storage::set_amount_of_firewall_rules(const uint8_t new_amount)
|
|
||||||
{
|
|
||||||
this->amount_of_rules = new_amount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef ESP32_STORAGE_HPP
|
#ifndef ESP32_STORAGE_HPP
|
||||||
#define ESP32_STORAGE_HPP
|
#define ESP32_STORAGE_HPP
|
||||||
|
|
||||||
#include "EEPROM.h"
|
#include "Preferences.h"
|
||||||
#include "FirewallTypes.h"
|
#include "FirewallTypes.h"
|
||||||
#include "mbedtls/md.h"
|
#include "mbedtls/md.h"
|
||||||
|
|
||||||
|
@ -10,25 +10,16 @@ namespace firewall
|
||||||
class Storage
|
class Storage
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
uint16_t eeprom_size;
|
Preferences preferences;
|
||||||
uint16_t settings_start = 0;
|
|
||||||
uint16_t settings_head = settings_start;
|
|
||||||
uint16_t rules_start = 100;
|
|
||||||
uint16_t rules_head = rules_start;
|
|
||||||
uint16_t certificate_start = 800;
|
|
||||||
uint16_t certificate_head = certificate_start;
|
|
||||||
|
|
||||||
void clear_eeprom();
|
|
||||||
void get_eeprom_hash();
|
|
||||||
unsigned char *Storage::get_hash(const char *);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint8_t amount_of_rules;
|
uint8_t retrieve_amount_of_firewall_rules();
|
||||||
uint8_t get_amount_of_firewall_rules();
|
void store_amount_of_firewall_rules(const uint8_t);
|
||||||
void set_amount_of_firewall_rules(const uint8_t);
|
firewall_rule_t *retrieve_firewall_rule(const uint8_t);
|
||||||
|
void store_firewall_rule(const uint8_t &, firewall_rule_t *);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Storage(const uint16_t = 1000);
|
Storage();
|
||||||
~Storage();
|
~Storage();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue