find a specific firewall rule in the linked list

This commit is contained in:
Florian Hoss 2022-04-11 18:40:57 +02:00
parent 945f1fd3b6
commit f1dbc95de5
2 changed files with 37 additions and 8 deletions

View file

@ -7,6 +7,15 @@ ESPFirewall::ESPFirewall(int port)
this->setup_routing();
}
void ESPFirewall::prepare_firewall_json(DynamicJsonDocument &json, firewall_rule_t *rule)
{
json["key"] = rule->key;
json["source"] = rule->source;
json["destination"] = rule->destination;
json["protocol"] = rule->protocol;
json["target"] = rule->target;
}
void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule)
{
firewall_rule_t *temp;
@ -52,10 +61,33 @@ void ESPFirewall::get_firewall_rule_handler(AsyncWebServerRequest *request)
int rule_number = request->pathArg(0).toInt();
DynamicJsonDocument json(1024);
String response;
json["message"] = "get firewall rule";
json["key"] = rule_number;
firewall_rule_t *current = this->head;
if (head == NULL)
{
json["message"] = "list is empty";
serializeJson(json, response);
request->send(404, "application/json", response);
request->send(500, "application/json", response);
return;
}
while (current->key != rule_number)
{
if (current->next == NULL)
{
json["message"] = "rule not found";
serializeJson(json, response);
request->send(500, "application/json", response);
return;
}
else
{
current = current->next;
}
}
prepare_firewall_json(json, current);
serializeJson(json, response);
request->send(200, "application/json", response);
}
void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request)
@ -82,11 +114,7 @@ void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request)
add_rule_to_firewall(rule);
json["key"] = rule->key;
json["source"] = rule->source;
json["destination"] = rule->destination;
json["protocol"] = rule->protocol;
json["target"] = rule->target;
prepare_firewall_json(json, rule);
response_code = 200;
}
else

View file

@ -32,6 +32,7 @@ class ESPFirewall
unsigned int amount_of_rules = 0;
struct firewall_rule *head = NULL;
void prepare_firewall_json(DynamicJsonDocument &json, firewall_rule_t *rule);
void add_rule_to_firewall(firewall_rule_t *rule);
void get_firewall_rules_handler(AsyncWebServerRequest *request);
void get_firewall_rule_handler(AsyncWebServerRequest *request);