storing and reading works

This commit is contained in:
Florian Hoss 2022-04-20 19:42:30 +02:00
parent d457949a76
commit 89886a65e3
3 changed files with 87 additions and 53 deletions

View file

@ -4,83 +4,117 @@ 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_amount_of_firewall_rules()
uint8_t Storage::retrieve_settings_value(const char *key)
{
uint8_t amount_of_rules;
this->preferences.begin("settings", false);
amount_of_rules = preferences.getUChar("amount_of_rules", 0);
this->preferences.end();
this->memory.begin("settings", true);
amount_of_rules = memory.getUChar(key, 0);
this->memory.end();
return amount_of_rules;
}
void Storage::store_amount_of_firewall_rules(const uint8_t new_amount)
void Storage::store_settings_value(const char *key, const uint8_t new_amount)
{
this->preferences.begin("settings", false);
this->preferences.putUChar("amount_of_rules", new_amount);
this->preferences.end();
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)
{
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();
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(const uint8_t &new_amount, firewall_rule_t *rule_ptr)
void Storage::store_firewall_rule(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();
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()
{
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;
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();
uint16_t pk_length = certificate->getPKLength();
unsigned char *cert_data = certificate->getCertData();
uint16_t pk_length = certificate->getPKLength();
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();
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();
}
}

View file

@ -11,13 +11,15 @@ namespace firewall
class Storage
{
private:
Preferences preferences;
Preferences memory;
protected:
uint8_t retrieve_amount_of_firewall_rules();
void store_amount_of_firewall_rules(const uint8_t);
uint8_t retrieve_settings_value(const char *);
void store_settings_value(const char *, const uint8_t);
firewall_rule_t *retrieve_firewall_rule(const uint8_t);
void store_firewall_rule(const uint8_t &, firewall_rule_t *);
void store_firewall_rule(firewall_rule_t *);
httpsserver::SSLCert *retrieve_certificate();
void store_certificate(httpsserver::SSLCert *certificate);

View file

@ -15,8 +15,7 @@ framework = arduino
monitor_speed = 115200
build_flags =
-DCORE_DEBUG_LEVEL=3
lib_deps =
bblanchon/ArduinoJson@^6.19.4
lib_deps = bblanchon/ArduinoJson@^6.19.4
[env:esp32-dev]
platform = espressif32
@ -25,5 +24,4 @@ framework = arduino
monitor_speed = 115200
build_flags =
-DCORE_DEBUG_LEVEL=3
lib_deps =
bblanchon/ArduinoJson@^6.19.4
lib_deps = bblanchon/ArduinoJson@^6.19.4