created and passed first unit tests for DNS; finished recovery from PingScheduler...
[pingcheck] / src / dns / dnscache.cpp
index d306fd1..4b8df17 100644 (file)
@@ -42,6 +42,7 @@ using boost::bind;
 using boost::posix_time::seconds;
 using I2n::Logger::GlobalLogger;
 
+
 namespace Config
 {
     int SaveTimerSeconds = 60;
@@ -63,6 +64,9 @@ Cname::Cname(const std::string &host, const TimeToLive &ttl)
     , Ttl( ttl )
 {}
 
+
+const string DnsCache::DoNotUseCacheFile = "do not use cache file!";
+
 DnsCache::DnsCache(const IoServiceItem &io_serv,
                    const std::string &cache_file)
     : IpCache()
@@ -126,7 +130,7 @@ void DnsCache::save_to_cachefile()
     else if (CacheFile.empty())
         GlobalLogger.warning()
                            << "DnsCache: skip saving because file name empty!";
-    else if (CacheFile == DO_NOT_USE_CACHE_FILE)
+    else if (CacheFile == DoNotUseCacheFile)
         GlobalLogger.info() << "DnsCache: configured not to use cache file";
     else
     {
@@ -138,7 +142,8 @@ void DnsCache::save_to_cachefile()
             //oa << boost::serialization::make_nvp("CnameCache", CnameCache);
             oa & BOOST_SERIALIZATION_NVP(IpCache);
             oa & BOOST_SERIALIZATION_NVP(CnameCache);
-            GlobalLogger.info() << "DnsCache: saved to cache file " << CacheFile;
+            GlobalLogger.info() << "DnsCache: saved to cache file "
+                                << CacheFile;
 
             HasChanged = false;
         }
@@ -153,8 +158,8 @@ void DnsCache::load_from_cachefile()
 {
     if (CacheFile.empty())
         GlobalLogger.warning()
-                  << "DnsCache: cannot load because cache file name is empty!";
-    else if (CacheFile == DO_NOT_USE_CACHE_FILE)
+                   << "DnsCache: cannot load because cache file name is empty!";
+    else if (CacheFile == DoNotUseCacheFile)
         GlobalLogger.info() << "DnsCache: configured not to use cache file";
     else if ( !I2n::file_exists(CacheFile) )
         GlobalLogger.warning() << "DnsCache: cannot load because cache file "
@@ -172,8 +177,9 @@ void DnsCache::load_from_cachefile()
         }
         catch (boost::archive::archive_exception &exc)
         {
-            GlobalLogger.warning() << "DnsCache: archive exception loading from "
-                                   << CacheFile << ": " << exc.what();
+            GlobalLogger.warning()
+                << "DnsCache: archive exception loading from " << CacheFile
+                << ": " << exc.what();
         }
         catch (std::exception &exc)
         {
@@ -316,10 +322,11 @@ HostAddressVec DnsCache::get_ips_recursive(const std::string &hostname,
     while ( result.empty() )
     {
         current_cname = get_cname(current_host, check_up_to_date);
-        current_host = key_for_hostname(current_cname.Host);
-        if (current_host.empty())
+        if (current_cname.Host.empty())
             break;
-        else if (++n_recursions >= Config::MaxRetrievalRecursions)
+
+        current_host = key_for_hostname(current_cname.Host);
+        if (++n_recursions >= Config::MaxRetrievalRecursions)
         {
             GlobalLogger.warning() << "DnsCache: reached recursion limit of "
                 << n_recursions << " in recursive IP retrieval!";
@@ -344,7 +351,10 @@ HostAddressVec DnsCache::get_ips_recursive(const std::string &hostname,
         BOOST_FOREACH( HostAddress &addr, result )
         {
             if (addr.get_ttl().get_value() > min_cname_ttl)
+            {
+                GlobalLogger.debug() << "DnsCache: using shorter CNAME TTL";
                 addr.set_ttl(cname_ttl);
+            }
         }
     }
 
@@ -366,7 +376,7 @@ HostAddressVec DnsCache::get_ips_recursive(const std::string &hostname,
 std::string DnsCache::get_first_outdated_cname(const std::string &hostname,
                                                const uint32_t ttl_thresh)
 {
-    std::string up_to_date_host = hostname;
+    std::string first_outdated = hostname;
     Cname cname;
     int n_recursions = 0;
     while (true)
@@ -378,18 +388,20 @@ std::string DnsCache::get_first_outdated_cname(const std::string &hostname,
             break;
         }
 
-        cname = get_cname(up_to_date_host);
-        if (key_for_hostname(cname.Host).empty())
+        cname = get_cname(first_outdated);
+        if (cname.Host.empty())
             // reached end of cname list
             break;
         else if (cname.Ttl.get_updated_value() > ttl_thresh)
             // cname is up to date --> continue looking
-            up_to_date_host = cname.Host;
+            first_outdated = cname.Host;
         else
-            // cname is out of date --> return that
+        {   // cname is out of date --> return its target
+            first_outdated = cname.Host;
             break;
+        }
     }
-    return up_to_date_host;
+    return first_outdated;
 }
 
 // (created using vim -- the world's best text editor)