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/esp32Firewall.cpp
2022-04-20 13:08:12 +02:00

211 lines
6.2 KiB
C++

#include "esp32Firewall.hpp"
namespace firewall
{
esp32Firewall::esp32Firewall()
{
this->setup_eeprom();
}
esp32Firewall::~esp32Firewall()
{
}
String esp32Firewall::protocol_to_string(firewall_protocol_t &protocol)
{
switch (protocol)
{
case FW_TCP:
return "TCP";
case FW_UDP:
return "UDP";
default:
return "ALL";
}
}
firewall_protocol_t esp32Firewall::string_to_protocol(std::string &protocol)
{
if (protocol.compare("TCP") == 0)
return FW_TCP;
else if (protocol.compare("UDP") == 0)
return FW_UDP;
else
return FW_ALL;
}
String esp32Firewall::target_to_string(firewall_target_t &target)
{
switch (target)
{
case FW_REJECT:
return "REJECT";
case FW_DROP:
return "DROP";
default:
return "ACCEPT";
}
}
firewall_target_t esp32Firewall::string_to_target(std::string &target)
{
if (target.compare("REJECT") == 0)
return FW_REJECT;
else if (target.compare("DROP") == 0)
return FW_DROP;
else
return FW_ACCEPT;
}
void esp32Firewall::setup_eeprom()
{
EEPROM.begin(this->eeprom_size);
this->amount_of_rules = EEPROM.read(this->eeprom_settings_head);
uint8_t security_number = EEPROM.read(this->eeprom_settings_head + 1);
if (this->amount_of_rules > 50 || security_number != this->security_number)
{
this->amount_of_rules = 0;
EEPROM.write(this->eeprom_settings_head, this->amount_of_rules);
EEPROM.write(this->eeprom_settings_head + 1, this->security_number);
EEPROM.commit();
}
log_i("Amount of existing Rules %i", this->amount_of_rules);
this->eeprom_read_firewall_rules();
}
void esp32Firewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr)
{
EEPROM.write(this->eeprom_settings_head, this->amount_of_rules);
EEPROM.writeString(this->eeprom_rules_head, rule_ptr->source);
this->eeprom_rules_head += IPV4ADDRESS_LENGTH;
EEPROM.writeString(this->eeprom_rules_head, rule_ptr->destination);
this->eeprom_rules_head += IPV4ADDRESS_LENGTH;
EEPROM.write(this->eeprom_rules_head, rule_ptr->protocol);
this->eeprom_rules_head += sizeof(firewall_protocol_t);
EEPROM.write(this->eeprom_rules_head, rule_ptr->target);
this->eeprom_rules_head += sizeof(firewall_target_t);
EEPROM.commit();
}
void esp32Firewall::eeprom_write_firewall_rules()
{
this->eeprom_rules_head = eeprom_start_firewall_rules;
firewall_rule_t *rule_ptr = this->head;
while (rule_ptr != NULL)
{
this->eeprom_write_firewall_rule(rule_ptr);
rule_ptr = rule_ptr->next;
}
}
void esp32Firewall::eeprom_read_firewall_rule(uint8_t &eeprom_address, uint8_t &rule_nr)
{
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
rule_ptr->key = rule_nr;
strcpy(rule_ptr->source, EEPROM.readString(eeprom_address).c_str());
eeprom_address += IPV4ADDRESS_LENGTH;
strcpy(rule_ptr->destination, EEPROM.readString(eeprom_address).c_str());
eeprom_address += IPV4ADDRESS_LENGTH;
rule_ptr->protocol = static_cast<firewall_protocol_t>(EEPROM.read(eeprom_address));
eeprom_address += sizeof(firewall_protocol_t);
rule_ptr->target = static_cast<firewall_target_t>(EEPROM.read(eeprom_address));
eeprom_address += sizeof(firewall_target_t);
add_rule_to_firewall(rule_ptr);
log_i("%s, %s, %s, %s",
rule_ptr->source,
rule_ptr->destination,
protocol_to_string(rule_ptr->protocol),
target_to_string(rule_ptr->target));
}
void esp32Firewall::eeprom_read_firewall_rules()
{
uint8_t eeprom_address = eeprom_start_firewall_rules;
for (uint8_t i = 1; i <= this->amount_of_rules; i++)
{
eeprom_read_firewall_rule(eeprom_address, i);
}
}
void esp32Firewall::add_rule_to_firewall(firewall_rule_t *rule_ptr)
{
firewall_rule_t *temp;
if (this->head == NULL)
{
this->head = rule_ptr;
rule_ptr->next = NULL;
return;
}
temp = this->head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = rule_ptr;
rule_ptr->next = NULL;
return;
}
firewall_rule_t *esp32Firewall::get_rule_from_firewall(uint8_t key)
{
firewall_rule_t *rule_ptr = this->head;
if (this->head == NULL)
{
return NULL;
}
while (rule_ptr->key != key)
{
if (rule_ptr->next == NULL)
{
return NULL;
}
else
{
rule_ptr = rule_ptr->next;
}
}
return rule_ptr;
}
bool esp32Firewall::delete_rule_from_firewall(uint8_t key)
{
if (this->head == NULL)
{
return false;
}
firewall_rule_t *current_rule_ptr = this->head;
firewall_rule_t *previous_rule_ptr = NULL;
firewall_rule_t *temp = NULL;
while (current_rule_ptr->key != key)
{
if (current_rule_ptr->next == NULL)
{
return false;
}
else
{
previous_rule_ptr = current_rule_ptr;
current_rule_ptr = current_rule_ptr->next;
}
}
if (current_rule_ptr == this->head)
{
this->head = head->next;
temp = this->head;
}
else
{
previous_rule_ptr->next = current_rule_ptr->next;
temp = previous_rule_ptr->next;
}
while (temp != NULL)
{
temp->key--;
temp = temp->next;
}
free(current_rule_ptr);
this->amount_of_rules--;
this->eeprom_write_firewall_rules();
return true;
}
}