Fixed burnt IP detection issue. Moved burnt IP detection into update_allowed().
[bpdyndnsd] / src / service.cpp
index 3c1cd25..e798a1d 100644 (file)
@@ -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<time_t,std::string>::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<time_t,std::string>::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);