rename...

This commit is contained in:
Florian Hoss 2022-04-20 14:37:21 +02:00
parent beddd88653
commit e5c061ff32
4 changed files with 81 additions and 32 deletions

View file

@ -1,26 +0,0 @@
#include "esp32Eeprom.hpp"
namespace firewall
{
Storage::Storage(const uint16_t eeprom_size)
{
this->eeprom_size = eeprom_size;
EEPROM.begin(this->eeprom_size);
set_amount_of_firewall_rules(EEPROM.read(this->settings_head));
log_i("Amount of Rules: %i", get_amount_of_firewall_rules());
}
Storage::~Storage()
{
}
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;
}
}

View file

@ -0,0 +1,70 @@
#include "esp32Storage.hpp"
namespace firewall
{
Storage::Storage(const uint16_t eeprom_size)
{
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()
{
}
void Storage::clear_eeprom()
{
for (int i = 0; i < this->eeprom_size; i++)
{
EEPROM.write(i, 0);
}
}
void Storage::get_eeprom_hash()
{
char buffer[this->eeprom_size] = {0};
for (int i = 0; i < this->eeprom_size; i++)
{
buffer[i] = EEPROM.readChar(i);
}
unsigned char *hashedPayload = get_hash(buffer);
for (int i = 0; i < sizeof(hashedPayload); i++)
{
char str[3];
sprintf(str, "%02x", (int)hashedPayload[i]);
Serial.print(str);
}
}
}
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;
}
}

View file

@ -1,8 +1,9 @@
#ifndef ESP32_EEPROM_HPP #ifndef ESP32_STORAGE_HPP
#define ESP32_EEPROM_HPP #define ESP32_STORAGE_HPP
#include "EEPROM.h" #include "EEPROM.h"
#include "FirewallTypes.h" #include "FirewallTypes.h"
#include "mbedtls/md.h"
namespace firewall namespace firewall
{ {
@ -12,18 +13,22 @@ namespace firewall
uint16_t eeprom_size; uint16_t eeprom_size;
uint16_t settings_start = 0; uint16_t settings_start = 0;
uint16_t settings_head = settings_start; uint16_t settings_head = settings_start;
uint16_t rules_start = 1000; uint16_t rules_start = 100;
uint16_t rules_head = rules_start; uint16_t rules_head = rules_start;
uint16_t certificate_start = 3000; uint16_t certificate_start = 800;
uint16_t certificate_head = certificate_start; 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 amount_of_rules;
uint8_t get_amount_of_firewall_rules(); uint8_t get_amount_of_firewall_rules();
void set_amount_of_firewall_rules(const uint8_t); void set_amount_of_firewall_rules(const uint8_t);
public: public:
Storage(const uint16_t = 4000); Storage(const uint16_t = 1000);
~Storage(); ~Storage();
}; };
} }

View file

@ -2,7 +2,7 @@
#include "WiFi.h" #include "WiFi.h"
#include "esp32FirewallAPI.hpp" #include "esp32FirewallAPI.hpp"
#include "esp32Eeprom.hpp" #include "esp32Storage.hpp"
firewall::esp32FirewallApi *firewall_api; firewall::esp32FirewallApi *firewall_api;
firewall::Storage *storage; firewall::Storage *storage;