simplify
This commit is contained in:
parent
a40b81b45d
commit
ef86c4f8f6
3 changed files with 21 additions and 25 deletions
|
@ -116,13 +116,9 @@ namespace fw
|
||||||
int rule_number = atoi(param.c_str());
|
int rule_number = atoi(param.c_str());
|
||||||
firewall_rule_t *rule_ptr = get_rule_from_firewall(rule_number);
|
firewall_rule_t *rule_ptr = get_rule_from_firewall(rule_number);
|
||||||
if (rule_ptr == NULL)
|
if (rule_ptr == NULL)
|
||||||
{
|
|
||||||
this->json_message_response("rule not found", 404);
|
this->json_message_response("rule not found", 404);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
this->json_generic_response(construct_json_firewall_rule(rule_ptr), 200);
|
this->json_generic_response(construct_json_firewall_rule(rule_ptr), 200);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void API::get_firewall_rules_handler()
|
void API::get_firewall_rules_handler()
|
||||||
|
@ -138,20 +134,11 @@ namespace fw
|
||||||
return;
|
return;
|
||||||
if (request_has_firewall_parameter())
|
if (request_has_firewall_parameter())
|
||||||
{
|
{
|
||||||
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
firewall_rule_t *rule_ptr = add_rule_to_firewall(
|
||||||
rule_ptr->key = ++amount_of_rules;
|
this->server->arg("source"),
|
||||||
|
this->server->arg("destination"),
|
||||||
String source = this->server->arg("source");
|
this->server->arg("protocol"),
|
||||||
strncpy(rule_ptr->source, source.c_str(), sizeof(rule_ptr->source));
|
this->server->arg("target"));
|
||||||
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);
|
|
||||||
this->json_generic_response(this->construct_json_firewall_rule(rule_ptr), 200);
|
this->json_generic_response(this->construct_json_firewall_rule(rule_ptr), 200);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -167,21 +154,15 @@ namespace fw
|
||||||
String param = this->server->pathArg(0);
|
String param = this->server->pathArg(0);
|
||||||
int rule_number = atoi(param.c_str());
|
int rule_number = atoi(param.c_str());
|
||||||
if (delete_rule_from_firewall(rule_number) == SUCCESS)
|
if (delete_rule_from_firewall(rule_number) == SUCCESS)
|
||||||
{
|
|
||||||
this->json_message_response("firewall rule deleted", 200);
|
this->json_message_response("firewall rule deleted", 200);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
this->json_message_response("cannot delete firewall rule", 500);
|
this->json_message_response("cannot delete firewall rule", 500);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool API::request_has_firewall_parameter()
|
bool API::request_has_firewall_parameter()
|
||||||
{
|
{
|
||||||
if (!this->server->args())
|
if (!this->server->args())
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return this->server->hasArg("source") ||
|
return this->server->hasArg("source") ||
|
||||||
|
|
|
@ -36,7 +36,21 @@ namespace fw
|
||||||
}
|
}
|
||||||
temp->next = rule_ptr;
|
temp->next = rule_ptr;
|
||||||
rule_ptr->next = NULL;
|
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)
|
firewall_rule_t *Rules::get_rule_from_firewall(uint8_t key)
|
||||||
|
|
|
@ -13,6 +13,7 @@ namespace fw
|
||||||
firewall_rule_t *rule_head = NULL;
|
firewall_rule_t *rule_head = NULL;
|
||||||
|
|
||||||
void add_rule_to_firewall(firewall_rule_t *);
|
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);
|
firewall_rule_t *get_rule_from_firewall(uint8_t);
|
||||||
ok_t delete_rule_from_firewall(uint8_t);
|
ok_t delete_rule_from_firewall(uint8_t);
|
||||||
|
|
||||||
|
|
Reference in a new issue