changed how dns deals with cnames and recursion: remember cnames and implement recurs...
[pingcheck] / src / dns / dnscache.cpp
index d180a89..9e8d1e0 100644 (file)
@@ -48,7 +48,8 @@ namespace Config
 
 DnsCache::DnsCache(const IoServiceItem &io_serv,
                    const std::string &cache_file)
-    : DataCache()
+    : IpCache()
+    , CnameCache()
     , SaveTimer( *io_serv )
     , CacheFile( cache_file )
     , HasChanged( false )
@@ -82,13 +83,13 @@ void DnsCache::schedule_save(const boost::system::error_code &error)
 
     if ( error ==  boost::asio::error::operation_aborted )   // cancelled
     {
-        GlobalLogger.error() << "DNS Cache: SaveTimer was cancelled "
+        GlobalLogger.error() << "DnsCache: SaveTimer was cancelled "
                              << "--> no save and no re-schedule of saving!";
         return;
     }
     else if (error)
     {
-        GlobalLogger.error() << "DNS Cache: Received error " << error
+        GlobalLogger.error() << "DnsCache: Received error " << error
                              << " in schedule_save "
                              << "--> no save now but re-schedule saving";
     }
@@ -105,13 +106,13 @@ void DnsCache::save_to_cachefile()
 {
     if (!HasChanged)
     {
-        GlobalLogger.info() << "DNS Cache: skip saving because has not changed";
+        GlobalLogger.info() << "DnsCache: skip saving because has not changed";
         return;
     }
     else if (CacheFile.empty())
     {
         GlobalLogger.warning()
-                           << "DNS Cache: skip saving because file name empty!";
+                           << "DnsCache: skip saving because file name empty!";
         return;
     }
 
@@ -119,8 +120,9 @@ void DnsCache::save_to_cachefile()
     {
         std::ofstream ofs( CacheFile.c_str() );
         boost::archive::xml_oarchive oa(ofs);
-        oa << boost::serialization::make_nvp("DataCache", DataCache);
-        GlobalLogger.info() << "DNS Cache: saved to cache file " << CacheFile;
+        oa << boost::serialization::make_nvp("IpCache", IpCache);
+        oa << boost::serialization::make_nvp("CnameCache", CnameCache);
+        GlobalLogger.info() << "DnsCache: saved to cache file " << CacheFile;
 
         HasChanged = false;
     }
@@ -136,12 +138,12 @@ void DnsCache::load_from_cachefile()
     if (CacheFile.empty())
     {
         GlobalLogger.warning()
-                  << "DNS Cache: cannot load because cache file name is empty!";
+                  << "DnsCache: cannot load because cache file name is empty!";
         return;
     }
     else if ( !I2n::file_exists(CacheFile) )
     {
-        GlobalLogger.warning() << "DNS Cache: cannot load because cache file "
+        GlobalLogger.warning() << "DnsCache: cannot load because cache file "
                                << CacheFile << " does not exist!";
         return;
     }
@@ -152,43 +154,79 @@ void DnsCache::load_from_cachefile()
         std::ifstream ifs( CacheFile.c_str() );
         boost::archive::xml_iarchive ia(ifs);
 
-        ia >> boost::serialization::make_nvp("DataCache", cache);
-        GlobalLogger.info() << "DNS Cache: loaded from file " << CacheFile;
+        ia >> boost::serialization::make_nvp("IpCache", cache);
+        ia >> boost::serialization::make_nvp("CnameCache", cache);
+        GlobalLogger.info() << "DnsCache: loaded from file " << CacheFile;
     }
     catch (boost::archive::archive_exception &exc)
     {
-        GlobalLogger.warning() << "DNS Cache: archive exception loading from "
+        GlobalLogger.warning() << "DnsCache: archive exception loading from "
                                << CacheFile << ": " << exc.what();
     }
     catch (std::exception &exc)
     {
-        GlobalLogger.warning() << "DNS Cache: exception while loading from "
+        GlobalLogger.warning() << "DnsCache: exception while loading from "
                                << CacheFile << ": " << exc.what();
     }
 }
 
-/**
- * 
- * in case the cached set is equal to the given new one, the old one is kept
- *   with TTLs updated
- * @returns true if changed cache; returns false if new_data is same as cache
- */
 void DnsCache::update(const std::string &hostname,
                       const HostAddressVec &new_data)
 {
-    GlobalLogger.info() << "DNS Cache: update IPs for " << hostname
+    GlobalLogger.info() << "DnsCache: update IPs for " << hostname
                         << " to " << new_data.size() << "-list";
-    DataCache[hostname] = new_data;
+    IpCache[hostname] = new_data;
     HasChanged = true;
 }
 
 
-HostAddressVec& DnsCache::get_data(const std::string &hostname)
+void DnsCache::update(const std::string &hostname,
+                      const std::string &cname)
+{
+    GlobalLogger.info() << "DnsCache: update CNAME for " << hostname
+                        << " to " << cname;
+    CnameCache[hostname] = cname;
+    HasChanged = true;
+}
+
+
+void DnsCache::update_ttl(const std::string &hostname,
+                          const uint32_t new_ttl)
+{
+    GlobalLogger.info() << "DnsCache: ensure TTL for IPs for " << hostname
+                        << " is below " << new_ttl;
+    HostAddressVec ips = IpCache[hostname];
+    uint32_t current_ttl;
+    TimeToLive current_ttl;
+    BOOST_FOREACH( HostAddress &addr, ips )
+    {
+        current_ttl = addr.get_ttl();
+        if (current_ttl.get_value() > new_ttl)
+        {
+            current_ttl.set_value(new_ttl);
+            addr.set_ttl(current_ttl);
+            HasChanged = true;
+        }
+    }
+    IpCache[hostname] = ip;
+}
+
+
+HostAddressVec& DnsCache::get_ips(const std::string &hostname)
 {
-    GlobalLogger.info() << "DNS Cache: request IPs for " << hostname
-                        << " --> " << DataCache[hostname].size() << "-list";
-    return DataCache[hostname];
+    GlobalLogger.info() << "DnsCache: request IPs for " << hostname
+                        << " --> " << IpCache[hostname].size() << "-list";
+    return IpCache[hostname];
 }
 
+std::string& DnsCache::get_cname(const std::string &hostname)
+{
+    GlobalLogger.info() << "DnsCache: request CNAME for " << hostname
+                        << " --> " << CnameCache[hostname];
+    return CnameCache[hostname];
+}
+
+
+
 // (created using vim -- the world's best text editor)