From 3c498f794e2065f43215cb71f297e75aef15d1c3 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 15 Sep 2016 01:49:32 +0200 Subject: [PATCH] Add a setting to allow the program to try and delete any pre-existing rule. --- keepupnpup.yml | 1 + src/main.cpp | 18 ++++++++++++++---- src/settings.cpp | 16 ++++++++++++++++ src/settings.hpp | 1 + 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/keepupnpup.yml b/keepupnpup.yml index 01162ac..c3aab3f 100644 --- a/keepupnpup.yml +++ b/keepupnpup.yml @@ -6,3 +6,4 @@ redirect: udp: - 49164 desc: RoutArm redir + autoremove: true diff --git a/src/main.cpp b/src/main.cpp index f26f93a..9fbd9c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,7 +26,8 @@ namespace { enum CurrentMappingType { NoMapping, PresentWithSameIP, - PresentWithDifferentIP + PresentWithDifferentIP, + IgnoreMapping }; CurrentMappingType has_mapping (const std::vector& parRedirs, const std::string& parAddr, uint16_t parPort, kuu::Protocol parProtocol) { @@ -37,7 +38,13 @@ namespace { return NoMapping; } - void add_bindings (kuu::UPNP& parUpnp, const std::string& parHost, const kuu::RedirectSetting::PortMappingsList& parPortMappings, const std::string& parDesc) { + void add_bindings ( + kuu::UPNP& parUpnp, + const std::string& parHost, + const kuu::RedirectSetting::PortMappingsList& parPortMappings, + const std::string& parDesc, + bool parTryForcing + ) { const std::vector redirs; // = upnp.redirections(); for (auto& mapping : parPortMappings) { @@ -46,8 +53,10 @@ namespace { try { const CurrentMappingType curr_mapping = has_mapping(redirs, parHost, mapping.port, mapping.protocol); + const CurrentMappingType decided_mapping = + (PresentWithDifferentIP == curr_mapping and not parTryForcing ? IgnoreMapping : curr_mapping); - switch (curr_mapping) { + switch (decided_mapping) { case PresentWithDifferentIP: parUpnp.remove_port_mapping(mapping.port, parHost, mapping.protocol); @@ -56,6 +65,7 @@ namespace { break; case PresentWithSameIP: + case IgnoreMapping: break; }; } @@ -70,7 +80,7 @@ namespace { kuu::RedirectSetting settings = kuu::load_redirect_settings("keepupnpup.yml", upnp.lanaddr()); - add_bindings(upnp, settings.host, settings.port_mappings, settings.desc); + add_bindings(upnp, settings.host, settings.port_mappings, settings.desc, settings.autoremove_old); } } //unnamed namespace diff --git a/src/settings.cpp b/src/settings.cpp index 53300b3..b6b81a4 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -19,6 +19,7 @@ #include #include #include +#include namespace kuu { namespace { @@ -41,6 +42,16 @@ namespace kuu { } } } + + bool to_boolean (const std::string& parValue) { + using boost::iequals; + + return iequals(parValue, std::string("on")) or + iequals(parValue, std::string("true")) or + parValue == "1" or + iequals(parValue, std::string("enabled")) + ; + } } //unnamed namespace RedirectSetting load_redirect_settings (const std::string& parPath, const std::string& parLanAddr) { @@ -71,6 +82,11 @@ namespace kuu { redir_settings.desc = redirect_node["desc"].as(); } + if (redirect_node["autoremove"]) + redir_settings.autoremove_old = to_boolean(redirect_node["autoremove"].as()); + else + redir_settings.autoremove_old = false; + return redir_settings; } } //namespace kuu diff --git a/src/settings.hpp b/src/settings.hpp index 65a257b..90b3c63 100644 --- a/src/settings.hpp +++ b/src/settings.hpp @@ -41,6 +41,7 @@ namespace kuu { std::string host; std::string desc; PortMappingsList port_mappings; + bool autoremove_old; }; RedirectSetting load_redirect_settings (const std::string& parPath, const std::string& parLanAddr);