86 lines
3.3 KiB
C++
86 lines
3.3 KiB
C++
#include "esp32Storage.hpp"
|
|
|
|
namespace firewall
|
|
{
|
|
Storage::Storage()
|
|
{
|
|
}
|
|
|
|
Storage::~Storage()
|
|
{
|
|
}
|
|
|
|
uint8_t Storage::retrieve_amount_of_firewall_rules()
|
|
{
|
|
uint8_t amount_of_rules;
|
|
this->preferences.begin("settings", false);
|
|
amount_of_rules = preferences.getUChar("amount_of_rules", 0);
|
|
this->preferences.end();
|
|
return amount_of_rules;
|
|
}
|
|
|
|
void Storage::store_amount_of_firewall_rules(const uint8_t new_amount)
|
|
{
|
|
this->preferences.begin("settings", false);
|
|
this->preferences.putUChar("amount_of_rules", new_amount);
|
|
this->preferences.end();
|
|
}
|
|
|
|
firewall_rule_t *Storage::retrieve_firewall_rule(const uint8_t key)
|
|
{
|
|
char rulename[12];
|
|
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
|
sprintf(rulename, "fw_rule_%i", key);
|
|
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", FW_ALL));
|
|
rule_ptr->target = static_cast<firewall_target_t>(this->preferences.getUChar("target", FW_ACCEPT));
|
|
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();
|
|
}
|
|
|
|
httpsserver::SSLCert *Storage::retrieve_certificate()
|
|
{
|
|
unsigned char *pk_data;
|
|
uint16_t pk_length;
|
|
unsigned char *cert_data;
|
|
uint16_t cert_length;
|
|
this->preferences.begin("certificate", false);
|
|
pk_length = this->preferences.getUInt("pk_length", 0);
|
|
cert_length = this->preferences.getUInt("pk_length", 0);
|
|
this->preferences.getBytes("pk_data", pk_data, pk_length);
|
|
this->preferences.getBytes("cert_data", cert_data, cert_length);
|
|
this->preferences.end();
|
|
httpsserver::SSLCert *certificate = new httpsserver::SSLCert(cert_data, cert_length, pk_data, pk_length);
|
|
return certificate;
|
|
}
|
|
|
|
void Storage::store_certificate(httpsserver::SSLCert *certificate)
|
|
{
|
|
unsigned char *pk_data = certificate->getPKData();
|
|
uint16_t pk_length = certificate->getPKLength();
|
|
unsigned char *cert_data = certificate->getCertData();
|
|
uint16_t cert_length = certificate->getCertLength();
|
|
this->preferences.begin("certificate", false);
|
|
this->preferences.putBytes("pk_data", pk_data, pk_length);
|
|
this->preferences.putUInt("pk_length", pk_length);
|
|
this->preferences.putBytes("cert_data", cert_data, cert_length);
|
|
this->preferences.putUInt("pk_length", cert_length);
|
|
this->preferences.end();
|
|
}
|
|
}
|