From: Bjoern Sikora Date: Mon, 31 Jan 2011 12:45:44 +0000 (+0100) Subject: Fixed burnt IP detection issue. Moved burnt IP detection into update_allowed(). X-Git-Tag: v1.1~18 X-Git-Url: http://developer.intra2net.com/git/?p=bpdyndnsd;a=commitdiff_plain;h=08e6f3395589f88f8bfe62e73470be12e3c7df3a Fixed burnt IP detection issue. Moved burnt IP detection into update_allowed(). --- diff --git a/src/logger.cpp b/src/logger.cpp index dc344b4..040b999 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -1958,22 +1958,28 @@ void Logger::print_msg( const string& msg ) const * @param last_updates Map with the last updates in it. * @param hostname The service hostname. */ -void Logger::print_last_updates( const std::string& ip_host, const int max_equal_updates_in_succession, const std::map& last_updates, const std::string& hostname ) const +void Logger::print_last_updates( const std::string& ip_host, const time_t current_time, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const std::map& last_updates, const std::string& servicename ) const { int level = 1; if ( level <= Loglevel ) { ostringstream msg; - msg << "Last updates for " << hostname << " : "; + msg << "Last updates for " << servicename << " : "; int i = 0; + for ( std::map::reverse_iterator r_iter; (r_iter != last_updates.rend()) && (i < max_equal_updates_in_succession); r_iter++ ) { msg << r_iter->first << "->" << r_iter->second; i++; } - msg << ". Actual internet IP: " << ip_host << endl; + + msg << ". Actual internet IP: " << ip_host + << " Current time: " << current_time + << " Update interval: " << update_interval + << " Max updates within interval: " << max_updates_within_interval + << " Max equal updates in succession: " << max_equal_updates_in_succession << endl; log_notice(msg.str()); } diff --git a/src/logger.hpp b/src/logger.hpp index 5ba1594..d3dca9d 100644 --- a/src/logger.hpp +++ b/src/logger.hpp @@ -276,7 +276,7 @@ public: void print_msg( const std::string& msg ) const; - void print_last_updates( const std::string& ip_host, const int max_equal_updates_in_succession, const std::map& last_updates, const std::string& hostname ) const; + void print_last_updates( const std::string& ip_host, const time_t current_time, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const std::map& last_updates, const std::string& servicename ) const; void print_ip_burnt( const std::string& ip_host, const std::string& hostname ) const; }; diff --git a/src/service.cpp b/src/service.cpp index 3c1cd25..e798a1d 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -206,13 +206,16 @@ bool Service::operator!= (const Service& other) const /** - * Checks if update will exceed max update interval. + * Checks if update will exceed max update interval or if the IP address is burnt. * @param current_time Current time. * @param changed_to_online True if we just changed to online, false if we were already online * @return True if update is allowed, false if update would exceed max update interval. */ -bool Service::update_allowed(const time_t current_time, bool changed_to_online) +bool Service::update_allowed(const time_t current_time, bool changed_to_online, const std::string& ip_host) { + Log->print_last_updates(ip_host,current_time,UpdateInterval,MaxUpdatesWithinInterval,MaxEqualUpdatesInSuccession,LastUpdates,get_service_name()); + + // Check for update interval overstepping. int i = 0; for ( std::map::reverse_iterator r_iter = LastUpdates.rbegin(); (r_iter != LastUpdates.rend()) && ( i < MaxUpdatesWithinInterval ); r_iter++) { @@ -223,6 +226,34 @@ bool Service::update_allowed(const time_t current_time, bool changed_to_online) } i++; } + + // Check for burnt IP. + // Only check for burnt IP address if there are at least max_equal_updates_in_succession entries in the last_updates map. + if ( (MaxEqualUpdatesInSuccession != 0) && ((int)LastUpdates.size() >= MaxEqualUpdatesInSuccession) ) + { + bool ip_burnt = true; + i = 0; + // Reverse iterate "last_updates" list so the latest update + // will be the first entry (map key is the unix timestamp) + for ( std::map::reverse_iterator r_iter = LastUpdates.rbegin(); + ( r_iter != LastUpdates.rend() ) && ( i < MaxEqualUpdatesInSuccession ); r_iter++ ) + { + if ( ip_host != r_iter->second ) + { + ip_burnt = false; + break; + } + i++; + } + + if ( ip_burnt ) + { + // IP Address is burnt. Too many updates in succession with the same IP. + Log->print_ip_burnt(ip_host,get_service_name()); + return false; + } + } + return true; } @@ -245,7 +276,7 @@ void Service::update(const string& ip, const time_t current_time, bool changed_t } // test if update is permitted by UpdateInterval Status - if ( update_allowed(current_time, changed_to_online) ) + if ( update_allowed(current_time, changed_to_online, ip) ) { Log->print_update_service(service_name); diff --git a/src/service.hpp b/src/service.hpp index e0410f1..5037883 100644 --- a/src/service.hpp +++ b/src/service.hpp @@ -73,7 +73,7 @@ public: void update(const std::string& ip, const time_t current_time, bool changed_to_online); - bool update_allowed(const time_t current_time, bool changed_to_online); + bool update_allowed(const time_t current_time, bool changed_to_online, const std::string& ip_host); void set_last_update(const time_t current_time, const std::string& ip); diff --git a/src/updater.cpp b/src/updater.cpp index cad0ca2..712200d 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -244,45 +244,8 @@ void Updater::update_services(bool changed_to_online) const string hostname = service->get_hostname(); time_t current_time = time(NULL); - // Try to get the last update time of the service if there is one. - // And check for burnt IP, too. - std::map last_updates = service->get_last_updates(); /*lint !e1793 */ - time_t lastupdated = 0; - - int last_updates_size = last_updates.size(); - int max_equal_updates_in_succession = service->get_max_equal_updates_in_succession(); - - Log->print_last_updates(ip_host,max_equal_updates_in_succession,last_updates,service->get_hostname()); - - // Only check for burnt IP address if there are at least max_equal_updates_in_succession entries in the last_updates map. - if ( (max_equal_updates_in_succession != 0) && (last_updates_size >= max_equal_updates_in_succession) ) - { - bool ip_burnt = true; - int i = 0; - // Reverse iterate "last_updates" list so the latest update - // will be the first entry (map key is the unix timestamp) - for ( std::map::reverse_iterator r_iter = last_updates.rbegin(); - (r_iter != last_updates.rend()) && (i < max_equal_updates_in_succession); r_iter++ ) - { - if ( i == 0 ) - lastupdated = r_iter->first; - - if ( ip_host != r_iter->second ) - { - ip_burnt = false; - break; - } - - i++; - } - - if ( ip_burnt ) - { - // IP Address is burnt. Too many updates in succession with the same IP. - Log->print_ip_burnt(ip_host,service->get_hostname()); - continue; - } - } + // Get the last update time of the service. + time_t lastupdated = service->get_last_update_time(); Log->print_check_service_update(hostname, current_time, lastupdated);