diff --git a/SourceCode/arduino/lib/Firewall/Firewall.cpp b/SourceCode/arduino/lib/Firewall/Firewall.cpp index 45de01f..d397f8c 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.cpp +++ b/SourceCode/arduino/lib/Firewall/Firewall.cpp @@ -1,10 +1,17 @@ #include "Firewall.h" -ESPFirewall::ESPFirewall(int api_port) +ESPFirewall::ESPFirewall(const uint16_t api_port) { this->setup_eeprom(); this->setup_certificate(); - this->setup_firewall_api(api_port); + this->firewall_api = new HTTPSServer(this->certificate, api_port, 5); + this->setup_routing(); + log_i("Starting server..."); + this->firewall_api->start(); + if (this->firewall_api->isRunning()) + { + log_i("Server ready."); + } } void ESPFirewall::handle_firewall_api_clients() @@ -229,9 +236,8 @@ void ESPFirewall::setup_certificate() log_i("Creating the certificate was successful"); } -void ESPFirewall::setup_firewall_api(int api_port) +void ESPFirewall::setup_routing() { - this->firewall_api = new HTTPSServer(this->certificate, api_port, 5); ResourceNode *get_firewall_rule = new ResourceNode("/api/v1/firewall/*", "GET", std::bind(&ESPFirewall::get_firewall_rule_handler, this, std::placeholders::_1, std::placeholders::_2)); ResourceNode *get_firewall_rules = new ResourceNode("/api/v1/firewall", "GET", std::bind(&ESPFirewall::get_firewall_rules_handler, this, std::placeholders::_1, std::placeholders::_2)); ResourceNode *post_firewall = new ResourceNode("/api/v1/firewall", "POST", std::bind(&ESPFirewall::post_firewall_handler, this, std::placeholders::_1, std::placeholders::_2)); @@ -244,13 +250,6 @@ void ESPFirewall::setup_firewall_api(int api_port) this->firewall_api->registerNode(delete_firewall); this->firewall_api->setDefaultNode(restart_device); this->firewall_api->setDefaultNode(not_found); - - log_i("Starting server..."); - this->firewall_api->start(); - if (this->firewall_api->isRunning()) - { - log_i("Server ready."); - } } void ESPFirewall::json_generic_response(HTTPResponse *response, String serialized, int response_code) @@ -356,10 +355,10 @@ void ESPFirewall::post_firewall_handler(HTTPRequest *request, HTTPResponse *resp // carefully copying c-string that is shorter then the destination char-array length std::string source; params->getQueryParameter("source", source); - strcpy(rule_ptr->source, source.length() <= IP4ADDR_STRLEN_MAX ? source.c_str() : ""); + strcpy(rule_ptr->source, source.length() <= IPV4ADDRESS_LENGTH ? source.c_str() : ""); std::string destination; params->getQueryParameter("destination", destination); - strcpy(rule_ptr->destination, destination.length() <= IP4ADDR_STRLEN_MAX ? destination.c_str() : ""); + strcpy(rule_ptr->destination, destination.length() <= IPV4ADDRESS_LENGTH ? destination.c_str() : ""); std::string protocol; params->getQueryParameter("protocol", protocol); diff --git a/SourceCode/arduino/lib/Firewall/Firewall.h b/SourceCode/arduino/lib/Firewall/Firewall.h index 75cb181..0125967 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.h +++ b/SourceCode/arduino/lib/Firewall/Firewall.h @@ -10,33 +10,11 @@ #include "HTTPRequest.hpp" #include "HTTPResponse.hpp" +#include "FirewallTypes.h" + #define eeprom_start_firewall_rules 4 using namespace httpsserver; -typedef enum firewall_target : uint8_t -{ - FW_REJECT = 0, - FW_DROP = 1, - FW_ACCEPT = 2, -} firewall_target_t; - -typedef enum firewall_protocol : uint8_t -{ - FW_TCP = 0, - FW_UDP = 1, - FW_ALL = 255, -} firewall_protocol_t; - -typedef struct firewall_rule -{ - uint8_t key; - char source[IP4ADDR_STRLEN_MAX]; - char destination[IP4ADDR_STRLEN_MAX]; - firewall_protocol_t protocol; - firewall_target_t target; - struct firewall_rule *next; -} firewall_rule_t; - class ESPFirewall { uint16_t eeprom_size = 512; @@ -69,7 +47,7 @@ class ESPFirewall // Firewall-API Actions void setup_certificate(); - void setup_firewall_api(int); + void setup_routing(); void json_generic_response(HTTPResponse *, String, int); void json_message_response(HTTPResponse *, String, int); String construct_json_firewall_rule(firewall_rule_t *); @@ -83,7 +61,7 @@ class ESPFirewall void delete_firewall_handler(HTTPRequest *, HTTPResponse *); public: - ESPFirewall(int port = 8080); + ESPFirewall(const uint16_t = 8080); void handle_firewall_api_clients(); }; diff --git a/SourceCode/arduino/lib/Firewall/FirewallTypes.h b/SourceCode/arduino/lib/Firewall/FirewallTypes.h new file mode 100644 index 0000000..d89cc01 --- /dev/null +++ b/SourceCode/arduino/lib/Firewall/FirewallTypes.h @@ -0,0 +1,32 @@ +#ifndef FIREWALL_TYPES_H +#define FIREWALL_TYPES_H + +#include "stdint.h" + +static const uint8_t IPV4ADDRESS_LENGTH = 16; + +typedef enum firewall_target : uint8_t +{ + FW_REJECT = 0, + FW_DROP = 1, + FW_ACCEPT = 2, +} firewall_target_t; + +typedef enum firewall_protocol : uint8_t +{ + FW_TCP = 0, + FW_UDP = 1, + FW_ALL = 255, +} firewall_protocol_t; + +typedef struct firewall_rule +{ + uint8_t key; + char source[IPV4ADDRESS_LENGTH]; + char destination[IPV4ADDRESS_LENGTH]; + firewall_protocol_t protocol; + firewall_target_t target; + struct firewall_rule *next; +} firewall_rule_t; + +#endif