From 81cf5681c031e8e386de85baa58660f94aa38c05 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Fri, 19 Jun 2015 17:13:03 +0200 Subject: [PATCH] bugfixed erase and re-set of TTLs; changed time warp thresh from 24h to 10mins increased revision to 0.6r4 --- CMakeLists.txt | 2 +- src/dns/dnscache.cpp | 97 ++++++++++++++++++++++++++++++++++-------------- src/dns/dnscache.h | 1 + src/dns/timetolive.cpp | 2 +- 4 files changed, 72 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 590831a..2c44abe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/dns/dnscache.cpp b/src/dns/dnscache.cpp index f607112..e418c7d 100644 --- a/src/dns/dnscache.cpp +++ b/src/dns/dnscache.cpp @@ -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"; +} diff --git a/src/dns/dnscache.h b/src/dns/dnscache.h index 83292bc..91c1429 100644 --- a/src/dns/dnscache.h +++ b/src/dns/dnscache.h @@ -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; }; diff --git a/src/dns/timetolive.cpp b/src/dns/timetolive.cpp index 87232df..3ad31a6 100644 --- a/src/dns/timetolive.cpp +++ b/src/dns/timetolive.cpp @@ -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(); -- 1.7.1