From 46fffdd606fd3e8ab2afe9e2223123f27a653dd1 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Fri, 21 May 2010 10:09:06 +0200 Subject: [PATCH] Implemented logic for ability to activate webcheck through signals. --- config/bpdyndnsd.conf | 3 ++- src/config.cpp | 30 ++++++++++++++++++++++++++++++ src/config.h | 5 +++++ src/ip_addr_helper.cpp | 8 ++++---- src/ip_addr_helper.h | 2 +- src/main.cpp | 5 +++++ src/updater.cpp | 2 +- 7 files changed, 48 insertions(+), 7 deletions(-) diff --git a/config/bpdyndnsd.conf b/config/bpdyndnsd.conf index 8d0bd2e..cc9fa3e 100644 --- a/config/bpdyndnsd.conf +++ b/config/bpdyndnsd.conf @@ -1,5 +1,6 @@ daemon_mode=1 loglevel=0 syslog=1 -# Uncomment following line if located behind a NAT router. +# Uncomment following two lines if located behind a NAT router. +#webcheck_enabled=1 #webcheck_url=http://checkip.dyndns.com diff --git a/src/config.cpp b/src/config.cpp index efce38b..7afc166 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -53,6 +53,7 @@ Config::Config() , ExternalWarningLog("") , ExternalWarningLevel(0) , StartOffline(false) + , WebcheckEnabled(false) { // Available service description config options po::options_description opt_desc_service("Service description options"); @@ -82,6 +83,7 @@ Config::Config() ("loglevel",po::value()->default_value(0),"Loglevel.") ("syslog",po::value()->default_value(false),"Use syslog facility.") ("enable_ipv6",po::value()->default_value(false),"Try to use IPv6.") + ("webcheck_enabled",po::value()->default_value(false),"Use webcheck url to determine actual IP address.") ("webcheck_url",po::value()->default_value(""),"Use this URL to determine IP.") ("webcheck_url_alt",po::value()->default_value(""),"Use this alternative URL to determine IP.") ("webcheck_interval",po::value()->default_value(10),"The webcheck interval in minutes.") @@ -124,6 +126,7 @@ Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder) , ExternalWarningLog("") , ExternalWarningLevel(0) , StartOffline(false) + , WebcheckEnabled(false) { // Available service description config options po::options_description opt_desc_service("Service description options"); @@ -153,6 +156,7 @@ Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder) ("loglevel",po::value()->default_value(0),"Loglevel.") ("syslog",po::value()->default_value(false),"Use syslog facility.") ("enable_ipv6",po::value()->default_value(false),"Try to use IPv6.") + ("webcheck_enabled",po::value()->default_value(false),"Use webcheck url to determine actual IP address.") ("webcheck_url",po::value()->default_value(""),"Use this URL to determine IP.") ("webcheck_url_alt",po::value()->default_value(""),"Use this alternative URL to determine IP.") ("webcheck_interval",po::value()->default_value(10),"The webcheck interval in minutes.") @@ -278,6 +282,9 @@ int Config::parse_cmd_line(int argc, char *argv[]) if ( VariablesMap.count("enable_ipv6") ) EnableIPv6 = VariablesMap["enable_ipv6"].as(); + if ( VariablesMap.count("webcheck_enabled") ) + WebcheckEnabled = VariablesMap["webcheck_enabled"].as(); + if ( VariablesMap.count("webcheck_url") ) WebcheckIpUrl = VariablesMap["webcheck_url"].as(); @@ -532,6 +539,9 @@ int Config::load_main_config_file(const string& full_filename) if ( VariablesMap.count("enable_ipv6") ) EnableIPv6 = VariablesMap["enable_ipv6"].as(); + if ( VariablesMap.count("webcheck_enabled") ) + WebcheckEnabled = VariablesMap["webcheck_enabled"].as(); + if ( VariablesMap.count("webcheck_url") ) WebcheckIpUrl = VariablesMap["webcheck_url"].as(); @@ -711,6 +721,26 @@ bool Config::get_enable_ipv6() const /** + * Getter for member WebcheckEnabled + * @return Is webcheck enabled by default. + */ +bool Config::get_webcheck_enabled() const +{ + return WebcheckEnabled; +} + + +/** + * Setter for member WebcheckEnabled + * @return Is webcheck enabled by default. + */ +void Config::set_webcheck_enabled( bool webcheck_enabled ) +{ + WebcheckEnabled = webcheck_enabled; +} + + +/** * Getter for member WebcheckIpUrl * @return The primary IP Webcheck URL */ diff --git a/src/config.h b/src/config.h index d6e8d45..b282c6e 100644 --- a/src/config.h +++ b/src/config.h @@ -47,6 +47,7 @@ private: std::string ExternalWarningLog; int ExternalWarningLevel; bool StartOffline; + bool WebcheckEnabled; Service::Ptr create_service(const std::string& protocol, const std::string& server, const std::string& hostname, const std::string& login, const std::string& password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl); int load_main_config_file(const std::string& full_filename); @@ -84,6 +85,10 @@ public: int get_proxy_port() const; + bool get_webcheck_enabled() const; + + void set_webcheck_enabled( bool webcheck_enabled ); + std::string get_webcheck_ip_url() const; std::string get_webcheck_ip_url_alt() const; diff --git a/src/ip_addr_helper.cpp b/src/ip_addr_helper.cpp index 5070a94..280257c 100644 --- a/src/ip_addr_helper.cpp +++ b/src/ip_addr_helper.cpp @@ -181,14 +181,14 @@ bool IPAddrHelper::is_local_ipv4(const string ip) const * Get the actual IP of this host through a conventional DNS query or through a IP webcheck URL if configured so. * @return A string representation of the actual IP in dotted format or an empty string if something went wrong. */ -string IPAddrHelper::get_actual_ip() +string IPAddrHelper::get_actual_ip( bool use_webcheck ) { string ip; - if ( WebcheckIpUrl.empty() ) - ip = get_local_wan_nic_ip(); - else + if ( !WebcheckIpUrl.empty() && use_webcheck ) ip = webcheck_ip(); + else + ip = get_local_wan_nic_ip(); return ip; } diff --git a/src/ip_addr_helper.h b/src/ip_addr_helper.h index f817854..72eb1fe 100644 --- a/src/ip_addr_helper.h +++ b/src/ip_addr_helper.h @@ -61,7 +61,7 @@ public: std::string dns_query(const std::string& _hostname) const; - std::string get_actual_ip(); + std::string get_actual_ip( bool use_webcheck ); std::string get_local_wan_nic_ip() const; diff --git a/src/main.cpp b/src/main.cpp index e2d7ed9..2b42217 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -310,12 +310,17 @@ int main(int argc, char *argv[]) // Should we start in offline mode? is_online = !updater->get_config()->get_start_offline(); + webcheck_enabled = updater->get_config()->get_webcheck_enabled(); // service processing starts here do { if ( is_online == true ) { + // Check if webcheck_enabled differs due to caught singnal then set it in config correspondingly + if ( updater->get_config()->get_webcheck_enabled() != webcheck_enabled ) + updater->get_config()->set_webcheck_enabled(webcheck_enabled); + // update all configured services updater->update_services(); } diff --git a/src/updater.cpp b/src/updater.cpp index 74ae13f..648baef 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -219,7 +219,7 @@ void Updater::update_services() const list services = ServiceHolder->get_services(); // Get the actual IP of this host. - string ip_host = IPAddrHelp->get_actual_ip(); + string ip_host = IPAddrHelp->get_actual_ip(Conf->get_webcheck_enabled()); if ( !ip_host.empty() ) { -- 1.7.1