This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
esp-firewall/SourceCode/arduino/lib/Firewall/esp32Storage.cpp
2022-04-20 19:54:46 +02:00

95 lines
3.2 KiB
C++

#include "esp32Storage.hpp"
namespace firewall
{
Storage::Storage()
{
}
Storage::~Storage()
{
}
uint8_t Storage::retrieve_settings_value(const char *key)
{
uint8_t amount_of_rules;
this->memory.begin("settings", true);
amount_of_rules = memory.getUChar(key, 0);
this->memory.end();
return amount_of_rules;
}
void Storage::store_settings_value(const char *key, const uint8_t new_amount)
{
this->memory.begin("settings", false);
this->memory.putUChar(key, new_amount);
this->memory.end();
}
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;
char rulename[9]; // fwRule99\n
sprintf(rulename, "fwRule%i", key);
this->memory.begin(rulename, true);
strcpy(rule_ptr->source, this->memory.getString("source", "0.0.0.0").c_str());
strcpy(rule_ptr->destination, this->memory.getString("destination", "0.0.0.0").c_str());
rule_ptr->protocol = static_cast<firewall_protocol_t>(this->memory.getUChar("protocol", FW_ALL));
rule_ptr->target = static_cast<firewall_target_t>(this->memory.getUChar("target", FW_REJECT));
this->memory.end();
return rule_ptr;
}
void Storage::store_firewall_rule(firewall_rule_t *rule_ptr)
{
char rulename[9]; // fwRule99\n
sprintf(rulename, "fwRule%i", rule_ptr->key);
this->memory.begin(rulename, false);
this->memory.putString("source", rule_ptr->source);
this->memory.putString("destination", rule_ptr->destination);
this->memory.putUChar("protocol", rule_ptr->protocol);
this->memory.putUChar("target", rule_ptr->target);
this->memory.end();
}
httpsserver::SSLCert *Storage::retrieve_certificate()
{
this->memory.begin("certificate", true);
uint16_t cert_data_length = this->memory.getBytesLength("cert_data");
unsigned char cert_data[cert_data_length];
this->memory.getBytes("cert_data", cert_data, cert_data_length);
uint16_t pk_data_length = this->memory.getBytesLength("pk_data");
unsigned char pk_data[pk_data_length];
this->memory.getBytes("pk_data", pk_data, pk_data_length);
uint16_t pk_length = this->memory.getUInt("pk_length", 0);
uint16_t cert_length = this->memory.getUInt("pk_length", 0);
this->memory.end();
return new httpsserver::SSLCert(cert_data, cert_length, pk_data, pk_length);
}
void Storage::store_certificate(httpsserver::SSLCert *certificate)
{
unsigned char *pk_data = certificate->getPKData();
unsigned char *cert_data = certificate->getCertData();
uint16_t pk_length = certificate->getPKLength();
uint16_t cert_length = certificate->getCertLength();
this->memory.begin("certificate", false);
this->memory.putBytes("pk_data", pk_data, sizeof(pk_data));
this->memory.putBytes("cert_data", cert_data, sizeof(cert_data));
this->memory.putUInt("pk_length", pk_length);
this->memory.putUInt("cert_length", cert_length);
this->memory.end();
}
}