From b3d9c2a6468de1873f3eb1d63b1ca9168dd5a95c Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Sun, 12 Jun 2022 14:58:24 +0200 Subject: [PATCH] Write some helpers to compare --- ESPFirewall/lib/Firewall/src/Firewall.cpp | 19 +++++++------------ ESPFirewall/lib/Firewall/src/Firewall.hpp | 2 +- ESPFirewall/lib/Firewall/src/Utils.cpp | 5 +++++ ESPFirewall/lib/Firewall/src/Utils.hpp | 1 + 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/ESPFirewall/lib/Firewall/src/Firewall.cpp b/ESPFirewall/lib/Firewall/src/Firewall.cpp index c304219..45c6bf1 100644 --- a/ESPFirewall/lib/Firewall/src/Firewall.cpp +++ b/ESPFirewall/lib/Firewall/src/Firewall.cpp @@ -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); } } diff --git a/ESPFirewall/lib/Firewall/src/Firewall.hpp b/ESPFirewall/lib/Firewall/src/Firewall.hpp index eeb6b9c..47cc64d 100644 --- a/ESPFirewall/lib/Firewall/src/Firewall.hpp +++ b/ESPFirewall/lib/Firewall/src/Firewall.hpp @@ -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: diff --git a/ESPFirewall/lib/Firewall/src/Utils.cpp b/ESPFirewall/lib/Firewall/src/Utils.cpp index e6a2f75..9be85ba 100644 --- a/ESPFirewall/lib/Firewall/src/Utils.cpp +++ b/ESPFirewall/lib/Firewall/src/Utils.cpp @@ -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; + } } diff --git a/ESPFirewall/lib/Firewall/src/Utils.hpp b/ESPFirewall/lib/Firewall/src/Utils.hpp index 95631e4..db8e5d5 100644 --- a/ESPFirewall/lib/Firewall/src/Utils.hpp +++ b/ESPFirewall/lib/Firewall/src/Utils.hpp @@ -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