bugfixed erase and re-set of TTLs; changed time warp thresh from 24h to 10mins
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 19 Jun 2015 15:13:03 +0000 (17:13 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 19 Jun 2015 15:13:03 +0000 (17:13 +0200)
increased revision to 0.6r4

CMakeLists.txt
src/dns/dnscache.cpp
src/dns/dnscache.h
src/dns/timetolive.cpp

index 590831a..2c44abe 100644 (file)
@@ -1,7 +1,7 @@
 # project: definitions
 project(pingcheck)
 set(VERSION 0.6)
-set(VERSION_REVISION 2)
+set(VERSION_REVISION 4)
 set(TARGET ${PROJECT_NAME})
 
 # cmake: build options
index f607112..e418c7d 100644 (file)
@@ -47,7 +47,7 @@ using I2n::Logger::GlobalLogger;
 namespace Config
 {
     const int SAVE_TIMER_SECONDS = 60;
-    const int CACHE_TIME_WARP_THRESH_HOURS = 24;
+    const int CACHE_TIME_WARP_THRESH_MINS = 10;
     const int CACHE_REMOVE_OUTDATED_DAYS = 60;
 }
 
@@ -213,7 +213,7 @@ bool DnsCache::check_timestamps(const boost::posix_time::ptime &cache_save_time)
     // check if CacheSaveTime is in the future
     boost::posix_time::ptime now
              = boost::posix_time::second_clock::universal_time()
-             + boost::posix_time::hours( Config::CACHE_TIME_WARP_THRESH_HOURS );
+             + boost::posix_time::minutes(Config::CACHE_TIME_WARP_THRESH_MINS);
     if (now > cache_save_time)
     {   // Cache was saved in the past -- everything alright
         return false;
@@ -223,18 +223,19 @@ bool DnsCache::check_timestamps(const boost::posix_time::ptime &cache_save_time)
                            << cache_save_time << ")! Resetting all TTLs to 0.";
 
     // reset TTLs in IP cache
-    BOOST_FOREACH( const ip_map_type::value_type key_and_ip, IpCache )
+    BOOST_FOREACH( ip_map_type::value_type &key_and_ip, IpCache )
     {
-        HostAddressVec addr_reset;
-        BOOST_FOREACH( HostAddress address, key_and_ip.second )
-            addr_reset.push_back( HostAddress( address.get_ip(), 0 ) );
-        IpCache[key_and_ip.first] = addr_reset;
+        BOOST_FOREACH( HostAddress &address, key_and_ip.second )
+            address.get_ttl().set_value(0);
     }
 
     // reset TTLs in CNAME cache
-    BOOST_FOREACH( const cname_map_type::value_type key_and_cname, CnameCache )
-        CnameCache[key_and_cname.first] = Cname( key_and_cname.second.Host,
-                                                 TimeToLive(0) );
+    BOOST_FOREACH( cname_map_type::value_type &key_and_cname, CnameCache )
+        key_and_cname.second.Ttl.set_value(0);
+
+    HasChanged = true;
+
+    //debug_print();
 
     return true;
 }
@@ -252,35 +253,47 @@ void DnsCache::remove_old_entries()
              - boost::gregorian::days( Config::CACHE_REMOVE_OUTDATED_DAYS );
 
     // IP cache
-    bool some_ip_up_to_date;
-    BOOST_FOREACH( const ip_map_type::value_type key_and_ip, IpCache )
     {
-        some_ip_up_to_date = false;
-        BOOST_FOREACH( HostAddress address, key_and_ip.second )
+        ip_map_type::iterator it = IpCache.begin();
+        ip_map_type::iterator it_end = IpCache.end();
+        bool some_ip_up_to_date;
+        while (it != it_end)
         {
-            if ( ! address.get_ttl().was_set_before(thresh) )
+            some_ip_up_to_date = false;
+            BOOST_FOREACH( const HostAddress &address, (*it).second )
             {
-                some_ip_up_to_date = true;
-                break;
+                if ( ! address.get_ttl().was_set_before(thresh) )
+                {
+                    some_ip_up_to_date = true;
+                    break;
+                }
             }
-        }
 
-        if ( ! some_ip_up_to_date )
-        {
-            GlobalLogger.debug() << "DnsCache: Removing empty/outdated IP list "
-                                 << "for " << key_and_ip.first.first;
-            IpCache.erase( key_and_ip.first );
+            if ( ! some_ip_up_to_date )
+            {
+                GlobalLogger.debug() << "DnsCache: Removing empty/outdated IP "
+                                     << "list for " << (*it).first.first;
+                IpCache.erase( (*it++).first );
+            }
+            else
+                ++it;
         }
     }
 
     // CNAME cache
-    BOOST_FOREACH( const cname_map_type::value_type key_and_cname, CnameCache )
     {
-        if ( key_and_cname.second.Ttl.was_set_before( thresh ) )
+        cname_map_type::iterator it = CnameCache.begin();
+        cname_map_type::iterator it_end = CnameCache.end();
+        while (it != it_end)
         {
-            GlobalLogger.debug() << "DnsCache: Removing outdated CNAME for "
-                                 << key_and_cname.first;
-            CnameCache.erase( key_and_cname.first );
+            if ( (*it).second.Ttl.was_set_before( thresh ) )
+            {
+                GlobalLogger.debug() << "DnsCache: Removing outdated CNAME for "
+                                     << (*it).first;
+                CnameCache.erase( (*it++).first );
+            }
+            else
+                ++it;
         }
     }
 }
@@ -622,3 +635,31 @@ std::string DnsCache::get_cname_chain_str(const std::string &hostname)
     }
     return temp.str();
 }
+
+
+// -----------------------------------------------------------------------------
+// OTHER
+// -----------------------------------------------------------------------------
+void DnsCache::debug_print() const
+{
+    GlobalLogger.debug() << "DnsCache: IP Cache contents:";
+    stringstream log_temp;
+    BOOST_FOREACH( const ip_map_type::value_type &key_and_ip, IpCache )
+    {
+        // write IPs into one log line
+        log_temp.str("");
+        log_temp << "DnsCache: " << key_and_ip.first.first << ": \t "
+                 << key_and_ip.second.size() << "-list ";
+        BOOST_FOREACH( const HostAddress &ip, key_and_ip.second )
+            log_temp << ip.get_ip() << "+" << ip.get_ttl().get_updated_value()
+                     << "s, ";
+        GlobalLogger.debug() << log_temp.str();
+    }
+
+    GlobalLogger.debug() << "DnsCache: CNAME Cache contents:";
+    BOOST_FOREACH( const cname_map_type::value_type &key_and_cname, CnameCache )
+        GlobalLogger.debug() << "DnsCache: " << key_and_cname.first << ": \t "
+                             << key_and_cname.second.Host << "+"
+                             << key_and_cname.second.Ttl.get_updated_value()
+                             << "s";
+}
index 83292bc..91c1429 100644 (file)
@@ -98,6 +98,7 @@ private:
     cname_map_key_type key_for_cname(const std::string &hostname) const;
     bool check_timestamps(const boost::posix_time::ptime &cache_save_time);
     void remove_old_entries();
+    void debug_print() const;
 
 };
 
index 87232df..3ad31a6 100644 (file)
@@ -52,7 +52,7 @@ uint32_t TimeToLive::get_value() const
  */
 void TimeToLive::set_value( const uint32_t ttl )
 {
-    BOOST_ASSERT( 0 < ttl );
+    BOOST_ASSERT( 0 <= ttl );
 
     Ttl = ttl;
     TtlSetTime = microsec_clock::universal_time();