DnsCache::DnsCache(const IoServiceItem &io_serv,
const std::string &cache_file)
- : DataCache()
+ : IpCache()
+ , CnameCache()
, SaveTimer( *io_serv )
, CacheFile( cache_file )
, HasChanged( false )
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";
}
{
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;
}
{
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;
}
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;
}
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)