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
|
|
|
{
|
2022-04-20 21:17:34 +02:00
|
|
|
Firewall::Firewall()
|
2022-04-20 13:08:12 +02:00
|
|
|
{
|
2022-04-20 21:17:34 +02:00
|
|
|
this->amount_of_rules = retrieve_settings_value("amount_of_rules");
|
|
|
|
for (uint8_t i = 0; i < this->amount_of_rules; i++)
|
2022-04-20 13:08:12 +02:00
|
|
|
{
|
2022-04-20 21:17:34 +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
|
|
|
}
|
2022-04-18 18:40:23 +02:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:17:34 +02:00
|
|
|
Firewall::~Firewall()
|
2022-04-20 13:08:12 +02:00
|
|
|
{
|
|
|
|
}
|
2022-04-18 17:46:17 +02:00
|
|
|
|
2022-04-20 21:17:34 +02:00
|
|
|
void Firewall::add_rule_to_firewall(firewall_rule_t *rule_ptr)
|
2022-04-11 11:47:50 +02:00
|
|
|
{
|
2022-04-20 21:17:34 +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
|
|
|
}
|
|
|
|
|
2022-04-20 21:17:34 +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-20 21:17:34 +02:00
|
|
|
bool 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-11 22:17:55 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
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--;
|
|
|
|
return true;
|
2022-04-11 22:17:55 +02:00
|
|
|
}
|
2022-04-20 21:17:34 +02:00
|
|
|
|
|
|
|
String Firewall::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 Firewall::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 Firewall::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 Firewall::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;
|
|
|
|
}
|
2022-04-11 22:17:55 +02:00
|
|
|
}
|