diff --git a/ESPFirewall/lib/Firewall/src/API.cpp b/ESPFirewall/lib/Firewall/src/API.cpp index ab7c63a..b2610e8 100644 --- a/ESPFirewall/lib/Firewall/src/API.cpp +++ b/ESPFirewall/lib/Firewall/src/API.cpp @@ -116,13 +116,9 @@ namespace fw int rule_number = atoi(param.c_str()); firewall_rule_t *rule_ptr = get_rule_from_firewall(rule_number); if (rule_ptr == NULL) - { this->json_message_response("rule not found", 404); - } else - { this->json_generic_response(construct_json_firewall_rule(rule_ptr), 200); - } } void API::get_firewall_rules_handler() @@ -138,20 +134,11 @@ namespace fw return; if (request_has_firewall_parameter()) { - firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); - rule_ptr->key = ++amount_of_rules; - - String source = this->server->arg("source"); - strncpy(rule_ptr->source, source.c_str(), sizeof(rule_ptr->source)); - String destination = this->server->arg("destination"); - strncpy(rule_ptr->destination, destination.c_str(), sizeof(rule_ptr->destination)); - - String protocol = this->server->arg("protocol"); - rule_ptr->protocol = string_to_protocol(protocol); - String target = this->server->arg("target"); - rule_ptr->target = string_to_target(target); - - add_rule_to_firewall(rule_ptr); + firewall_rule_t *rule_ptr = add_rule_to_firewall( + this->server->arg("source"), + this->server->arg("destination"), + this->server->arg("protocol"), + this->server->arg("target")); this->json_generic_response(this->construct_json_firewall_rule(rule_ptr), 200); } else @@ -167,21 +154,15 @@ namespace fw String param = this->server->pathArg(0); int rule_number = atoi(param.c_str()); if (delete_rule_from_firewall(rule_number) == SUCCESS) - { this->json_message_response("firewall rule deleted", 200); - } else - { this->json_message_response("cannot delete firewall rule", 500); - } } bool API::request_has_firewall_parameter() { if (!this->server->args()) - { return false; - } else { return this->server->hasArg("source") || diff --git a/ESPFirewall/lib/Firewall/src/Rules.cpp b/ESPFirewall/lib/Firewall/src/Rules.cpp index f2bfbe1..e2f804e 100644 --- a/ESPFirewall/lib/Firewall/src/Rules.cpp +++ b/ESPFirewall/lib/Firewall/src/Rules.cpp @@ -36,7 +36,21 @@ namespace fw } temp->next = rule_ptr; rule_ptr->next = NULL; - return; + } + + firewall_rule_t *Rules::add_rule_to_firewall(String source, String destination, String protocol, String target) + { + firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); + rule_ptr->key = ++amount_of_rules; + + strncpy(rule_ptr->source, source.c_str(), sizeof(rule_ptr->source)); + strncpy(rule_ptr->destination, destination.c_str(), sizeof(rule_ptr->destination)); + + rule_ptr->protocol = string_to_protocol(protocol); + rule_ptr->target = string_to_target(target); + + add_rule_to_firewall(rule_ptr); + return rule_ptr; } firewall_rule_t *Rules::get_rule_from_firewall(uint8_t key) diff --git a/ESPFirewall/lib/Firewall/src/Rules.hpp b/ESPFirewall/lib/Firewall/src/Rules.hpp index ef5be67..1656573 100644 --- a/ESPFirewall/lib/Firewall/src/Rules.hpp +++ b/ESPFirewall/lib/Firewall/src/Rules.hpp @@ -13,6 +13,7 @@ namespace fw firewall_rule_t *rule_head = NULL; void add_rule_to_firewall(firewall_rule_t *); + firewall_rule_t *add_rule_to_firewall(String source, String destination, String protocol, String target); firewall_rule_t *get_rule_from_firewall(uint8_t); ok_t delete_rule_from_firewall(uint8_t);