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

121 lines
4.2 KiB
C++
Raw Normal View History

2022-04-20 14:37:21 +02:00
#include "esp32Storage.hpp"
namespace firewall
{
2022-04-20 15:43:02 +02:00
Storage::Storage()
2022-04-20 14:37:21 +02:00
{
2022-04-20 19:42:30 +02:00
store_settings_value("amount_of_rules", 21);
log_i("Amount: %i", retrieve_settings_value("amount_of_rules"));
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
rule_ptr->key = 0;
strcpy(rule_ptr->source, "192.168.0.12");
strcpy(rule_ptr->destination, "192.168.0.12");
rule_ptr->protocol = FW_TCP;
rule_ptr->target = FW_DROP;
store_firewall_rule(rule_ptr);
free(rule_ptr);
rule_ptr = retrieve_firewall_rule(0);
log_i("%s, %s, %i, %i",
rule_ptr->source,
rule_ptr->destination,
rule_ptr->protocol,
rule_ptr->target);
free(rule_ptr);
rule_ptr = retrieve_firewall_rule(1);
log_i("%s, %s, %i, %i",
rule_ptr->source,
rule_ptr->destination,
rule_ptr->protocol,
rule_ptr->target);
free(rule_ptr);
2022-04-20 14:37:21 +02:00
}
Storage::~Storage()
{
}
2022-04-20 19:42:30 +02:00
uint8_t Storage::retrieve_settings_value(const char *key)
2022-04-20 14:37:21 +02:00
{
2022-04-20 15:43:02 +02:00
uint8_t amount_of_rules;
2022-04-20 19:42:30 +02:00
this->memory.begin("settings", true);
amount_of_rules = memory.getUChar(key, 0);
this->memory.end();
2022-04-20 15:43:02 +02:00
return amount_of_rules;
2022-04-20 14:37:21 +02:00
}
2022-04-20 19:42:30 +02:00
void Storage::store_settings_value(const char *key, const uint8_t new_amount)
2022-04-20 14:37:21 +02:00
{
2022-04-20 19:42:30 +02:00
this->memory.begin("settings", false);
this->memory.putUChar(key, new_amount);
this->memory.end();
2022-04-20 14:37:21 +02:00
}
2022-04-20 15:43:02 +02:00
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;
2022-04-20 19:42:30 +02:00
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();
2022-04-20 15:43:02 +02:00
return rule_ptr;
}
2022-04-20 14:37:21 +02:00
2022-04-20 19:42:30 +02:00
void Storage::store_firewall_rule(firewall_rule_t *rule_ptr)
2022-04-20 15:43:02 +02:00
{
2022-04-20 19:42:30 +02:00
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();
2022-04-20 15:43:02 +02:00
}
2022-04-20 15:53:54 +02:00
2022-04-20 16:02:10 +02:00
httpsserver::SSLCert *Storage::retrieve_certificate()
{
2022-04-20 19:42:30 +02:00
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);
2022-04-20 16:02:10 +02:00
}
2022-04-20 15:53:54 +02:00
void Storage::store_certificate(httpsserver::SSLCert *certificate)
{
unsigned char *pk_data = certificate->getPKData();
unsigned char *cert_data = certificate->getCertData();
2022-04-20 19:42:30 +02:00
uint16_t pk_length = certificate->getPKLength();
2022-04-20 15:53:54 +02:00
uint16_t cert_length = certificate->getCertLength();
2022-04-20 19:42:30 +02:00
this->memory.begin("certificate", false);
this->memory.putBytes("pk_data", pk_data, pk_length);
this->memory.putBytes("cert_data", cert_data, cert_length);
this->memory.putUInt("pk_length", pk_length);
this->memory.putUInt("pk_length", cert_length);
this->memory.end();
2022-04-20 15:53:54 +02:00
}
2022-04-20 14:37:21 +02:00
}