#include "esp32Firewall.hpp" namespace firewall { Firewall::Firewall() { this->amount_of_rules = retrieve_settings_value("amount_of_rules"); for (uint8_t i = 0; i < this->amount_of_rules; i++) { firewall_rule_t *rule_ptr = retrieve_firewall_rule(i); add_rule_to_firewall(rule_ptr); } } Firewall::~Firewall() { } void Firewall::add_rule_to_firewall(firewall_rule_t *rule_ptr) { store_settings_value("amount_of_rules", this->amount_of_rules); store_firewall_rule(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 *Firewall::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 Firewall::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--; return true; } 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; } }