From: Bjoern Sikora Date: Tue, 16 Jul 2013 15:41:40 +0000 (+0200) Subject: Enhanced update logic to respect not activated hostnames. If a hostname could not... X-Git-Tag: v1.1~7 X-Git-Url: http://developer.intra2net.com/git/?p=bpdyndnsd;a=commitdiff_plain;h=7335d7a7a9db007b02b47c30cc76148a9962e993 Enhanced update logic to respect not activated hostnames. If a hostname could not be resolved it could be due to offline or not activated state. Try to update at least once. If the server response indicated that the hostname could correctly be associated to an existing account, assume hostname as activated. --- diff --git a/src/config.cpp b/src/config.cpp index ff63d8e..0285ccc 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -223,7 +223,7 @@ int Config::parse_cmd_line(int argc, char *argv[]) if ( service ) { ServiceHolder->add_service(service); - Log->print_service_object("New Service object from command line options:", service->get_protocol(), service->get_hostname(), service->get_login() ,service->get_password(), service->get_update_interval(), service->get_max_updates_within_interval(), service->get_max_equal_updates_in_succession(), service->get_dns_cache_ttl() , service->get_actual_ip(), service->get_last_updates()); + Log->print_service_object("New Service object from command line options:", service->get_protocol(), service->get_hostname(), service->get_login() ,service->get_password(), service->get_update_interval(), service->get_max_updates_within_interval(), service->get_max_equal_updates_in_succession(), service->get_dns_cache_ttl() , service->get_actual_ip(), service->get_last_updates(), service->get_activated()); } else { @@ -460,7 +460,7 @@ int Config::load_service_config_file(const string& full_filename) if ( service ) { ServiceHolder->add_service(service); - Log->print_service_object("New Service object from config:", service->get_protocol(), service->get_hostname(), service->get_login() ,service->get_password(), service->get_update_interval(), service->get_max_updates_within_interval(), service->get_max_equal_updates_in_succession(), service->get_dns_cache_ttl() , service->get_actual_ip(), service->get_last_updates()); + Log->print_service_object("New Service object from config:", service->get_protocol(), service->get_hostname(), service->get_login() ,service->get_password(), service->get_update_interval(), service->get_max_updates_within_interval(), service->get_max_equal_updates_in_succession(), service->get_dns_cache_ttl() , service->get_actual_ip(), service->get_last_updates(), service->get_activated()); } else { diff --git a/src/logger.cpp b/src/logger.cpp index dec7077..19ef01b 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -815,8 +815,9 @@ void Logger::print_deserialized_objects_success() const * @param password Service's password. * @param actual_ip Service's actual_ip. * @param lastupdated Service's lastupdated. + * @param activated Service's activated. */ -void Logger::print_service_object(const string& message, const string& protocol, const string& hostname, const string& login, const string& password, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const int dns_cache_ttl , const string& actual_ip, std::map lastupdated) const +void Logger::print_service_object(const string& message, const string& protocol, const string& hostname, const string& login, const string& password, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const int dns_cache_ttl , const string& actual_ip, std::map lastupdated, const bool activated) const { int level = 1; if ( level <= Loglevel ) @@ -836,6 +837,7 @@ void Logger::print_service_object(const string& message, const string& protocol, { msg << "\t" << "Lastupdated: " << r_iter->first << " -> " << r_iter->second << endl; } + msg << "\t" << "Activated: " << activated << endl; log_notice(msg.str()); } } diff --git a/src/logger.hpp b/src/logger.hpp index d3dca9d..7202410 100644 --- a/src/logger.hpp +++ b/src/logger.hpp @@ -146,7 +146,7 @@ public: void print_deserialized_objects_success() const; - void print_service_object(const std::string& message, const std::string& protocol, const std::string& hostname, const std::string& login, const std::string& password, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const int dns_cache_ttl , const std::string& actual_ip, std::map lastupdates) const; + void print_service_object(const std::string& message, const std::string& protocol, const std::string& hostname, const std::string& login, const std::string& password, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const int dns_cache_ttl , const std::string& actual_ip, std::map lastupdates, const bool activated) const; void print_exception_serialize(const std::string& errMsg) const; diff --git a/src/service.cpp b/src/service.cpp index f8279ae..45d833e 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -25,6 +25,7 @@ Service::Service() , Password("NOT SERIALIZED") , ActualIP("0.0.0.0") , ActualIPIsBurnt(false) + , Activated(false) , UpdateInterval(15) , MaxUpdatesWithinInterval(3) , MaxEqualUpdatesInSuccession(2) @@ -297,6 +298,10 @@ void Service::update(const string& ip, const time_t current_time, bool changed_t UpdateErrorCode update_res = perform_update(ip); + // If update result is other than Generic or NotAuth, we assume that the hostname is activated. + if ( (update_res != GenericError) && (update_res != NotAuth) ) + set_activated(); + if (update_res == UpdateOk) { // if update was successful, we need to set the Lastupdated and ActualIP base member. @@ -483,3 +488,23 @@ void Service::set_dns_cache_ttl(const int _dns_cache_ttl) { DNSCacheTTL = _dns_cache_ttl; } + + +/** + * Get member Activated + * @return Activated + */ +bool Service::get_activated() const +{ + return Activated; +} + + +/** + * Set member Activated + * @param _activated Activated + */ +void Service::set_activated() +{ + Activated = true; +} diff --git a/src/service.hpp b/src/service.hpp index 09548f2..7d71d97 100644 --- a/src/service.hpp +++ b/src/service.hpp @@ -33,6 +33,7 @@ private: std::string ActualIP; bool ActualIPIsBurnt; + bool Activated; int UpdateInterval; int MaxUpdatesWithinInterval; @@ -58,15 +59,24 @@ private: ar & MaxUpdatesWithinInterval; ar & MaxEqualUpdatesInSuccession; ar & DNSCacheTTL; + ar & Activated; } Logger::Ptr Log; protected: - enum UpdateErrorCode { UpdateOk=0, GenericError=1, NoChange=2, Blocked=3 }; + enum UpdateErrorCode { UpdateOk=0, UpdateError=1, NoChange=2, Blocked=3, NotAuth=4, GenericError=4 }; virtual UpdateErrorCode perform_update(const std::string& ip) = 0; + // UpdateOk: Update was successful + // UpdateError: Unspecified error code returned from update server + // NoChange: IP was not changed_to_online + // Blocked: Client blocked due to abusive updates + // NotAuth: Authentication was not successful + // GenericError: Generic error. For example update server not reached, wrong hostname, not authenticated or network error. + // Generic error indicates that the update server couldn't associate the update request to any hostname. + public: typedef boost::shared_ptr Ptr; @@ -118,6 +128,9 @@ public: std::string get_service_name() const; + void set_activated(); + bool get_activated() const; + bool operator== (const Service& other) const; bool operator!= (const Service& other) const; }; diff --git a/src/service_dhs.cpp b/src/service_dhs.cpp index 27b9740..2e275c1 100644 --- a/src/service_dhs.cpp +++ b/src/service_dhs.cpp @@ -166,11 +166,13 @@ Service::UpdateErrorCode ServiceDhs::perform_update(const std::string& ip) else { get_logger()->print_update_failure(url,curl_data); + return UpdateError; } } else if ( http_status_code == 401 ) { get_logger()->print_service_not_authorized(url,get_login(),get_password()); + return NotAuth; } else { diff --git a/src/service_dyndns.cpp b/src/service_dyndns.cpp index 4dc4789..475d29e 100644 --- a/src/service_dyndns.cpp +++ b/src/service_dyndns.cpp @@ -146,10 +146,12 @@ Service::UpdateErrorCode ServiceDyndns::perform_update(const std::string& ip) else if ( curl_data == "badauth" ) { get_logger()->print_service_not_authorized(url,get_login(),get_password()); + return NotAuth; } else { get_logger()->print_update_failure(url, curl_data); + return UpdateError; } } else diff --git a/src/service_dyns.cpp b/src/service_dyns.cpp index b305637..c831296 100644 --- a/src/service_dyns.cpp +++ b/src/service_dyns.cpp @@ -127,10 +127,12 @@ Service::UpdateErrorCode ServiceDyns::perform_update(const std::string& ip) else if ( status_code == "401" ) { get_logger()->print_service_not_authorized(url,get_login(),get_password()); + return NotAuth; } else { get_logger()->print_update_failure(url,curl_data); + return UpdateError; } } else diff --git a/src/service_easydns.cpp b/src/service_easydns.cpp index a90d259..d598d64 100644 --- a/src/service_easydns.cpp +++ b/src/service_easydns.cpp @@ -210,10 +210,12 @@ Service::UpdateErrorCode ServiceEasydns::perform_update(const std::string& ip) else if ( status_code == "NOACCESS" ) { get_logger()->print_service_not_authorized(url,get_login(),get_password()); + return NotAuth; } else { get_logger()->print_update_failure(url, curl_data); + return UpdateError; } } else diff --git a/src/service_gnudip.cpp b/src/service_gnudip.cpp index 76b9420..934db17 100644 --- a/src/service_gnudip.cpp +++ b/src/service_gnudip.cpp @@ -319,10 +319,12 @@ Service::UpdateErrorCode ServiceGnudip::perform_update(const std::string& ip) else if ( update_return_code == "1" ) { get_logger()->print_service_not_authorized(url,get_login(),get_password()); + return NotAuth; } else { get_logger()->print_update_failure(url,update_return_code); + return UpdateError; } } else diff --git a/src/service_ods.cpp b/src/service_ods.cpp index 60d4354..d6a3bec 100644 --- a/src/service_ods.cpp +++ b/src/service_ods.cpp @@ -115,7 +115,7 @@ Service::UpdateErrorCode ServiceOds::perform_update(const std::string& ip) // Login failed get_logger()->print_service_not_authorized(UpdateServer,get_login(),get_password()); connection->close_connection(); /*lint !e534 */ - return GenericError; + return NotAuth; } else if ( status_code != "225" ) { @@ -177,7 +177,7 @@ Service::UpdateErrorCode ServiceOds::perform_update(const std::string& ip) { get_logger()->print_undefined_protocol_error("ODS",update_reply); connection->close_connection(); /*lint !e534 */ - return GenericError; + return UpdateError; } } diff --git a/src/service_tzo.cpp b/src/service_tzo.cpp index 402ff44..486bec7 100644 --- a/src/service_tzo.cpp +++ b/src/service_tzo.cpp @@ -132,10 +132,12 @@ Service::UpdateErrorCode ServiceTzo::perform_update(const std::string& ip) else if ( status_code == "401" ) { get_logger()->print_service_not_authorized(url,get_login(),get_password()); + return NotAuth; } else { get_logger()->print_update_failure(url,curl_data); + return UpdateError; } } else diff --git a/src/service_zoneedit.cpp b/src/service_zoneedit.cpp index 9641011..724e539 100644 --- a/src/service_zoneedit.cpp +++ b/src/service_zoneedit.cpp @@ -126,11 +126,13 @@ Service::UpdateErrorCode ServiceZoneedit::perform_update(const std::string& ip) else { get_logger()->print_update_failure(url,curl_data); + return UpdateError; } } else if ( http_status_code == 401 ) { get_logger()->print_service_not_authorized(url,get_login(),get_password()); + return NotAuth; } else { diff --git a/src/serviceholder.cpp b/src/serviceholder.cpp index e4a484b..e4041a8 100644 --- a/src/serviceholder.cpp +++ b/src/serviceholder.cpp @@ -151,14 +151,17 @@ int Serviceholder::deserialize_services() BOOST_FOREACH(Service::Ptr &old_service, _old_services) { OldServices.push_back(old_service); - Log->print_service_object("Deserialized following Service object:", old_service->get_protocol(), old_service->get_hostname(), old_service->get_login() ,old_service->get_password(), old_service->get_update_interval(), old_service->get_max_updates_within_interval(), old_service->get_max_equal_updates_in_succession(), old_service->get_dns_cache_ttl() , old_service->get_actual_ip(), old_service->get_last_updates()); + Log->print_service_object("Deserialized following Service object:", old_service->get_protocol(), old_service->get_hostname(), old_service->get_login() ,old_service->get_password(), old_service->get_update_interval(), old_service->get_max_updates_within_interval(), old_service->get_max_equal_updates_in_succession(), old_service->get_dns_cache_ttl() , old_service->get_actual_ip(), old_service->get_last_updates(), old_service->get_activated()); BOOST_FOREACH(Service::Ptr &service, Services) { if ( *service == *old_service ) { service->set_last_updates(old_service->get_last_updates()); service->set_actual_ip(old_service->get_actual_ip()); - Log->print_service_object("New Service object with adopted values:", service->get_protocol(), service->get_hostname(), service->get_login() ,service->get_password(), service->get_update_interval(), service->get_max_updates_within_interval(), service->get_max_equal_updates_in_succession(), service->get_dns_cache_ttl() , service->get_actual_ip(), service->get_last_updates()); + if ( old_service->get_activated() ) + service->set_activated(); + + Log->print_service_object("New Service object with adopted values:", service->get_protocol(), service->get_hostname(), service->get_login() ,service->get_password(), service->get_update_interval(), service->get_max_updates_within_interval(), service->get_max_equal_updates_in_succession(), service->get_dns_cache_ttl() , service->get_actual_ip(), service->get_last_updates(), service->get_activated()); // We have adopted the values of the old_service. Just set lastupdated and timeout to 0, so this old_service wont be serialized. old_service->set_update_interval(0); } diff --git a/src/updater.cpp b/src/updater.cpp index 712200d..28a58fe 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -255,7 +255,8 @@ void Updater::update_services(bool changed_to_online) const Log->print_cached_dns_entry(hostname, ip_dns_recheck, ip_last_update, ip_host); // Test if the DNS-Record could not be found. - if ( ip_dns_recheck.empty() ) + // If DNS-Record was not found but service was not activated, the hostname could be deactivated, offline or not existent. In this case try a ordinary update. + if ( ip_dns_recheck.empty() && service->get_activated() ) { Log->print_dns_lookup_failed(changed_to_online, hostname); continue; @@ -273,7 +274,16 @@ void Updater::update_services(bool changed_to_online) const if ( ip_last_update != ip_host ) { // Update - Log->print_update_service(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated); + if ( !ip_dns_recheck.empty() ) + { + Log->print_update_service(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated); + } + else + { + // Service gets updated the first time and no DNS-Record was found. This is either a initial update or the hostname is really not available. + Log->print_update_service(hostname, "", ip_last_update, ip_host, lastupdated); + } + service->update(ip_host, current_time, changed_to_online); } else