rename files to be generic

This commit is contained in:
Florian Hoss 2022-04-24 00:23:37 +02:00
parent 2cde69a34a
commit bda65724aa
3 changed files with 2 additions and 2 deletions

View file

@ -0,0 +1,169 @@
#include "Storage.hpp"
namespace fw
{
Storage::Storage()
{
#ifdef ESP32
if (this->mount_spiffs() == ERROR)
endless_loop();
#endif
}
Storage::~Storage()
{
}
ok_t Storage::mount_spiffs()
{
#ifdef ESP32
if (!SPIFFS.begin(false))
{
if (!SPIFFS.begin(true))
{
Serial.println("SPIFFS cannot be mounted");
return ERROR;
};
}
return SUCCESS;
#elif defined(ESP8266)
return NO_ACTION;
#endif
}
uint8_t Storage::retrieve_settings_value(const char *key)
{
#ifdef ESP32
uint8_t amount_of_rules;
this->memory.begin("settings", true);
amount_of_rules = memory.getUChar(key, 0);
this->memory.end();
return amount_of_rules;
#elif defined(ESP8266)
return 0;
#endif
}
void Storage::store_settings_value(const char *key, const uint8_t new_amount)
{
#ifdef ESP32
this->memory.begin("settings", false);
this->memory.putUChar(key, new_amount);
this->memory.end();
#endif
}
firewall_rule_t *Storage::retrieve_firewall_rule(const uint8_t key)
{
#ifdef ESP32
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);
strncpy(rule_ptr->source, this->memory.getString("source", "0.0.0.0").c_str(), sizeof(rule_ptr->source));
strncpy(rule_ptr->destination, this->memory.getString("destination", "0.0.0.0").c_str(), sizeof(rule_ptr->source));
rule_ptr->protocol = static_cast<firewall_protocol_t>(this->memory.getUChar("protocol", PROTOCOL_ALL));
rule_ptr->target = static_cast<firewall_target_t>(this->memory.getUChar("target", TARGET_REJECT));
this->memory.end();
return rule_ptr;
#elif defined(ESP8266)
return NULL;
#endif
}
void Storage::store_all_firewall_rules(firewall_rule_t *head)
{
#ifdef ESP32
firewall_rule_t *temp = head;
while (temp != NULL)
{
store_firewall_rule(temp);
temp = temp->next;
}
#endif
}
void Storage::store_firewall_rule(firewall_rule_t *rule_ptr)
{
#ifdef ESP32
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();
#endif
}
// httpsserver::SSLCert *Storage::retrieve_certificate()
// {
// File keyFile = SPIFFS.open("/key.der");
// File certFile = SPIFFS.open("/cert.der");
// if (!keyFile || !certFile || keyFile.size() == 0 || certFile.size() == 0)
// {
// log_e("No server-certificate found in SPIFFS");
// return NULL;
// }
// size_t keySize = keyFile.size();
// size_t certSize = certFile.size();
// uint8_t *keyBuffer = new uint8_t[keySize];
// if (keyBuffer == NULL)
// {
// log_w("Not enough memory to load private key");
// return NULL;
// }
// uint8_t *certBuffer = new uint8_t[certSize];
// if (certBuffer == NULL)
// {
// delete[] keyBuffer;
// log_w("Not enough memory to load server-certificate");
// return NULL;
// }
// keyFile.read(keyBuffer, keySize);
// certFile.read(certBuffer, certSize);
// keyFile.close();
// certFile.close();
// return new httpsserver::SSLCert(certBuffer, certSize, keyBuffer, keySize);
// }
// void Storage::store_certificate(httpsserver::SSLCert *certificate)
// {
// File keyFile = SPIFFS.open("/key.der");
// File certFile = SPIFFS.open("/cert.der");
// bool failure = false;
// keyFile = SPIFFS.open("/key.der", FILE_WRITE);
// if (!keyFile || !keyFile.write(certificate->getPKData(), certificate->getPKLength()))
// {
// log_w("Cannot write /key.der");
// failure = true;
// }
// if (keyFile)
// keyFile.close();
// certFile = SPIFFS.open("/cert.der", FILE_WRITE);
// if (!certFile || !certFile.write(certificate->getCertData(), certificate->getCertLength()))
// {
// log_w("Cannot write /cert.der");
// failure = true;
// }
// if (certFile)
// certFile.close();
// if (failure)
// {
// log_w("Server-certificate could not be stored permanently, generating new certificate on reboot...");
// }
// }
}