Commit 4580b452 authored by Franck Messaoudi's avatar Franck Messaoudi

Fix: update the parsing protocol

parent 2166c0fc
...@@ -10,13 +10,29 @@ ...@@ -10,13 +10,29 @@
#include "sdf_filter.h" #include "sdf_filter.h"
//--------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------
std::optional<int> SdfFilterParser::ParseProtocol(const std::string& protocol) { std::optional<uint16_t> SdfFilterParser::ParseProtocol(
static const std::unordered_map<std::string, uint8_t> protocolMap = { const std::string& protocol) {
{"icmp", 0}, {"ip", 1}, {"tcp", 2}, {"udp", 3}, {"icmp6", 4}}; static const std::unordered_map<std::string, uint8_t> allowedProtocolMap = {
// Standard IANA-assigned IP protocol numbers
std::string proto = (protocol == "58") ? "icmp6" : protocol; {"icmp", 1}, // IPv4 ICMP
auto it = protocolMap.find(proto); {"ip", 0}, // IP in IP
if (it != protocolMap.end()) { {"tcp", 6}, // TCP over IP
{"udp", 17}, // UDP over IP
{"icmp6", 58}, // IPv6 ICMP ?
/*
* We can also accept numbers to specify protocol:
* eg: permit out 6 from any to any
* So here: permit out tcp from any to any
*/
{"0", 0}, // icmp
{"1", 1}, // ip
{"6", 6}, // tcp
{"17", 17}, // udp
{"58", 58} // icmp6 ?
};
auto it = allowedProtocolMap.find(protocol);
if (it != allowedProtocolMap.end()) {
return it->second; return it->second;
} }
return std::nullopt; return std::nullopt;
...@@ -117,8 +133,9 @@ std::optional<uint16_t> SdfFilterParser::ParsePort(const std::string& str) { ...@@ -117,8 +133,9 @@ std::optional<uint16_t> SdfFilterParser::ParsePort(const std::string& str) {
std::optional<struct sdf_filtr> SdfFilterParser::ParseSdfFilter( std::optional<struct sdf_filtr> SdfFilterParser::ParseSdfFilter(
const std::string& flowDescription) { const std::string& flowDescription) {
std::regex re( const std::string flowRegexStr =
R"(^permit out (icmp|ip|tcp|udp|\d+) from (any|[\d.]+|[\da-fA-F:]+)(?:/(\d+))?(?:\s+(\d+|\d+-\d+))? to (assigned|any|[\d.]+|[\da-fA-F:]+)(?:/(\d+))?(?:\s+(\d+|\d+-\d+))?$)"); R"(^permit out (icmp|ip|tcp|udp|\d+) from (any|[\d.]+|[\da-fA-F:]+)(?:/(\d+))?(?:\s+(\d+|\d+-\d+))? to (assigned|any|[\d.]+|[\da-fA-F:]+)(?:/(\d+))?(?:\s+(\d+|\d+-\d+))?$)";
std::regex re(flowRegexStr);
// permit out (icmp|ip|tcp|udp|\d+) from // permit out (icmp|ip|tcp|udp|\d+) from
// (any|[\d.]+|[\da-fA-F:]+)(?:/(\d+))?(?: (\d+|\d+-\d+))? to // (any|[\d.]+|[\da-fA-F:]+)(?:/(\d+))?(?: (\d+|\d+-\d+))? to
// (assigned|any|[\d.]+|[\da-fA-F:]+)(?:/(\d+))?(?: (\d+|\d+-\d+))?$ // (assigned|any|[\d.]+|[\da-fA-F:]+)(?:/(\d+))?(?: (\d+|\d+-\d+))?$
...@@ -128,7 +145,8 @@ std::optional<struct sdf_filtr> SdfFilterParser::ParseSdfFilter( ...@@ -128,7 +145,8 @@ std::optional<struct sdf_filtr> SdfFilterParser::ParseSdfFilter(
if (!std::regex_match(flowDescription, match, re)) { if (!std::regex_match(flowDescription, match, re)) {
Logger::upf_app().error( Logger::upf_app().error(
"SDF Filter: bad formatting. Should be compatible with regex:"); "SDF Filter: bad formatting. Should be compatible with regex: %s",
flowRegexStr);
// throw std::runtime_error("SDF Filter: bad formatting"); // throw std::runtime_error("SDF Filter: bad formatting");
return std::nullopt; return std::nullopt;
} }
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
class SdfFilterParser { class SdfFilterParser {
public: public:
// SdfFilterParser(); // SdfFilterParser();
static std::optional<int> ParseProtocol(const std::string& protocol); static std::optional<uint16_t> ParseProtocol(const std::string& protocol);
static std::optional<struct ip_subnet> ParseCidrIp( static std::optional<struct ip_subnet> ParseCidrIp(
const std::string& ipStr, const std::string& maskStr); const std::string& ipStr, const std::string& maskStr);
static std::optional<struct port_range> ParsePortRange( static std::optional<struct port_range> ParsePortRange(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment