Write some helpers to compare
This commit is contained in:
parent
8353443ea4
commit
b3d9c2a646
4 changed files with 14 additions and 13 deletions
|
@ -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)
|
||||
return true;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in a new issue