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/ESPFirewall/lib/Firewall/src/esp32Firewall.cpp

101 lines
2.6 KiB
C++
Raw Normal View History

2022-04-19 21:26:43 +02:00
#include "esp32Firewall.hpp"
2022-04-10 20:46:54 +02:00
2022-04-20 13:08:12 +02:00
namespace firewall
2022-04-10 20:46:54 +02:00
{
Firewall::Firewall()
2022-04-20 13:08:12 +02:00
{
this->amount_of_rules = retrieve_settings_value("amount_of_rules");
2022-04-20 21:35:36 +02:00
for (uint8_t i = 1; i <= this->amount_of_rules; i++)
2022-04-20 13:08:12 +02:00
{
firewall_rule_t *rule_ptr = retrieve_firewall_rule(i);
add_rule_to_firewall(rule_ptr);
2022-04-20 13:08:12 +02:00
}
}
Firewall::~Firewall()
2022-04-20 13:08:12 +02:00
{
}
void Firewall::add_rule_to_firewall(firewall_rule_t *rule_ptr)
2022-04-11 11:47:50 +02:00
{
store_settings_value("amount_of_rules", this->amount_of_rules);
store_firewall_rule(rule_ptr);
2022-04-20 13:08:12 +02:00
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;
2022-04-18 17:55:34 +02:00
rule_ptr->next = NULL;
2022-04-11 17:02:58 +02:00
return;
2022-04-11 11:47:50 +02:00
}
firewall_rule_t *Firewall::get_rule_from_firewall(uint8_t key)
2022-04-11 21:27:27 +02:00
{
2022-04-20 13:08:12 +02:00
firewall_rule_t *rule_ptr = this->head;
if (this->head == NULL)
2022-04-11 21:27:27 +02:00
{
return NULL;
}
2022-04-20 13:08:12 +02:00
while (rule_ptr->key != key)
2022-04-11 21:27:27 +02:00
{
2022-04-20 13:08:12 +02:00
if (rule_ptr->next == NULL)
{
return NULL;
}
else
{
rule_ptr = rule_ptr->next;
}
2022-04-11 21:27:27 +02:00
}
2022-04-20 13:08:12 +02:00
return rule_ptr;
2022-04-11 17:02:58 +02:00
}
2022-04-11 21:27:27 +02:00
2022-04-21 15:16:54 +02:00
ok_t Firewall::delete_rule_from_firewall(uint8_t key)
2022-04-11 22:17:55 +02:00
{
2022-04-20 13:08:12 +02:00
if (this->head == NULL)
2022-04-21 15:16:54 +02:00
return NO_ACTION;
2022-04-20 13:08:12 +02:00
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)
2022-04-21 15:16:54 +02:00
return NO_ACTION;
2022-04-20 13:08:12 +02:00
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;
}
2022-04-11 22:17:55 +02:00
else
{
2022-04-20 13:08:12 +02:00
previous_rule_ptr->next = current_rule_ptr->next;
temp = previous_rule_ptr->next;
2022-04-11 22:17:55 +02:00
}
2022-04-20 13:08:12 +02:00
while (temp != NULL)
{
temp->key--;
temp = temp->next;
}
free(current_rule_ptr);
this->amount_of_rules--;
2022-04-20 21:35:36 +02:00
store_settings_value("amount_of_rules", this->amount_of_rules);
if (this->amount_of_rules != 0)
store_all_firewall_rules(head);
2022-04-21 15:16:54 +02:00
return SUCCESS;
}
2022-04-11 22:17:55 +02:00
}