Write some helpers to compare

This commit is contained in:
Florian Hoss 2022-06-12 14:58:24 +02:00
parent 8353443ea4
commit b3d9c2a646
4 changed files with 14 additions and 13 deletions

View file

@ -110,16 +110,15 @@ namespace fw
return SUCCESS;
}
bool Firewall::is_included_in_firewall(const char *ip, const uint16_t port)
bool Firewall::is_included_in_firewall(String &ip, const uint32_t &port)
{
firewall_rule_t *rule_ptr = this->rule_head;
while (rule_ptr != NULL)
{
if (strncmp(ip, rule_ptr->ip, IPV4ADDRESS_LENGTH) == 0)
{
if (rule_ptr->port_from <= port && port <= rule_ptr->port_to)
if (ip == String(rule_ptr->ip) &&
is_in_range(port, rule_ptr->port_from, rule_ptr->port_to) &&
rule_ptr->target != TARGET_ACCEPT)
return true;
}
rule_ptr = rule_ptr->next;
}
return false;
@ -127,12 +126,8 @@ namespace fw
bool Firewall::is_client_allowed(WiFiClient client)
{
const char *ip = client.remoteIP().toString().c_str();
const uint16_t port = client.remotePort();
Serial.print(client.remoteIP());
Serial.print(":");
Serial.println(client.remotePort());
String ip = client.remoteIP().toString();
const uint32_t port = client.remotePort();
return !is_included_in_firewall(ip, port);
}
}

View file

@ -19,7 +19,7 @@ namespace fw
firewall_rule_t *get_rule_from_firewall(const uint8_t key);
ok_t delete_rule_from_firewall(const uint8_t key);
bool is_included_in_firewall(const char *ip, const uint16_t port);
bool is_included_in_firewall(String &ip, const uint32_t &port);
bool is_client_allowed(WiFiClient client);
protected:

View file

@ -54,4 +54,9 @@ namespace fw
while (true)
delay(500);
}
bool is_in_range(const uint32_t number, const uint32_t lower, const uint32_t upper)
{
return lower <= number && number <= upper;
}
}

View file

@ -78,6 +78,7 @@ namespace fw
firewall_target_t string_to_target(String &target);
String response_code_to_string(const uint16_t response_code);
void endless_loop();
bool is_in_range(const uint32_t number, const uint32_t lower, const uint32_t upper);
}
#endif