#include "esp32Storage.hpp" namespace firewall { Storage::Storage() { 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); } 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(this->memory.getUChar("protocol", FW_ALL)); rule_ptr->target = static_cast(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, 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(); } }