From: Christian Herdtweck Date: Fri, 10 Apr 2015 15:30:21 +0000 (+0200) Subject: moved new dns code from temp dir to dns subdir; changed serialization; added destruct... X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=c5b4902df2b24b6d09c58cba925e21939d056c50;p=pingcheck moved new dns code from temp dir to dns subdir; changed serialization; added destructors; added boost libs --> can link now! --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0289bdb..34a7eaf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,7 +5,7 @@ include(FindPkgConfig) set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED OFF) set(Boost_USE_STATIC_RUNTIME OFF) -find_package(Boost 1.44 COMPONENTS filesystem program_options system REQUIRED) +find_package(Boost 1.44 COMPONENTS filesystem program_options system serialization date_time REQUIRED) include_directories(${Boost_INCLUDE_DIRS}) link_directories(${Boost_LIBRARY_DIRS}) @@ -15,9 +15,6 @@ include_directories(${CMAKE_SOURCE_DIR}/lib/boost-custom) # package: boost-net-dns include_directories(${CMAKE_SOURCE_DIR}/lib/boost-net-dns) -# package: boost-signal_handler -include_directories(${CMAKE_SOURCE_DIR}/lib/boost-signal_handler) - # package: libi2ncommon pkg_check_modules(I2NCOMMON REQUIRED libi2ncommon) include_directories(${I2NCOMMON_INCLUDE_DIRS}) @@ -62,14 +59,12 @@ set(SOURCES config/option/maxaddressresolutionattemptsoption.cpp config/option/resolvedipttlthresholdoption.cpp config/option/dnscachefileoption.cpp - #dns/dnsresolver.cpp - #dns/dnsresolverfactory.cpp - dns/hostaddress.cpp dns/timetolive.cpp - dns_neww/dnscache.cpp - dns_neww/resolverbase.cpp - dns_neww/dnsresolver.cpp - dns_neww/dnsmaster.cpp + dns/hostaddress.cpp + dns/dnscache.cpp + dns/resolverbase.cpp + dns/dnsresolver.cpp + dns/dnsmaster.cpp host/hoststatus.cpp host/loglevel.cpp host/logoutput.cpp diff --git a/src/config/configuration.h b/src/config/configuration.h index 45752ff..1bb4895 100644 --- a/src/config/configuration.h +++ b/src/config/configuration.h @@ -95,6 +95,9 @@ public: void set_resolved_ip_ttl_threshold( const int resolved_ip_ttl_threshold ); + std::string get_dns_cache_file() const; + void set_dns_cache_file(const std::string &dns_cache_file); + float get_ratio_random_hosts() const; void set_ratio_random_hosts( const float ratio ); @@ -106,9 +109,6 @@ public: void set_print_version( const bool do_print ); bool get_print_version(); - std::string get_dns_cache_file() const; - void set_dns_cache_file(const std::string &dns_cache_file); - bool randomize_hosts(); private: @@ -132,10 +132,10 @@ private: int PingReplyTimeout; int MaxAddressResolutionAttempts; int ResolvedIpTtlThreshold; + std::string DnsCacheFile; HostList Hosts; float RatioRandomHosts; bool PrintVersion; - std::string DnsCacheFile; rand_gen_type RandomNumberGenerator; }; diff --git a/src/dns/dnscache.cpp b/src/dns/dnscache.cpp index 6e7fcca..23f2152 100644 --- a/src/dns/dnscache.cpp +++ b/src/dns/dnscache.cpp @@ -20,7 +20,7 @@ Christian Herdtweck, Intra2net AG 2015 */ -#include "dns_neww/dnscache.h" +#include "dns/dnscache.h" #include #include diff --git a/src/dns/dnsmaster.cpp b/src/dns/dnsmaster.cpp index 7553eef..31814e4 100644 --- a/src/dns/dnsmaster.cpp +++ b/src/dns/dnsmaster.cpp @@ -20,14 +20,14 @@ Christian Herdtweck, Intra2net AG 2015 */ -#include "dns_neww/dnsmaster.h" +#include "dns/dnsmaster.h" #include #include #include -#include "dns_neww/ippseudoresolver.h" -#include "dns_neww/dnsresolver.h" +#include "dns/ippseudoresolver.h" +#include "dns/dnsresolver.h" using boost::bind; using I2n::Logger::GlobalLogger; @@ -82,6 +82,21 @@ DnsMasterItem& DnsMaster::get_instance() return TheOnlyInstance; } +DnsMaster::~DnsMaster() +{ + GlobalLogger.info() << "DnsMaster is being destructed"; + + if (DnsMaster::TheOnlyInstance) + { // just to be sure ... + GlobalLogger.warning() << "DnsMaster is being destructed that is not " + << "singleton instance TheOnlyInstance!"; + DnsMaster::TheOnlyInstance.reset(); + } + + // Items in ResolverMap and the DnsCache might still be referenced by + // Resolvers and are smart pointers, anyway --> nothing to do here +} + ResolverItem& DnsMaster::get_resolver_for( const std::string &hostname, diff --git a/src/dns/dnsmaster.h b/src/dns/dnsmaster.h index c8c5866..e431104 100644 --- a/src/dns/dnsmaster.h +++ b/src/dns/dnsmaster.h @@ -44,17 +44,17 @@ #include "host/pinger.h" // for IoserviceItem #include "host/pingprotocol.h" -#include "dns_neww/dnscache.h" -#include "dns_neww/resolverbase.h" +#include "dns/dnscache.h" +#include "dns/resolverbase.h" class DnsMaster; typedef boost::shared_ptr DnsMasterItem; typedef boost::net::dns::type_t DnsIpProtocol; -DnsIpProtocol DNS_IPv4 = boost::net::dns::type_a; -DnsIpProtocol DNS_IPv6 = boost::net::dns::type_a6; -DnsIpProtocol DNS_IPALL = boost::net::dns::type_all; +const DnsIpProtocol DNS_IPv4 = boost::net::dns::type_a; +const DnsIpProtocol DNS_IPv6 = boost::net::dns::type_a6; +const DnsIpProtocol DNS_IPALL = boost::net::dns::type_all; typedef std::pair resolver_key_type; typedef std::map resolver_map_type; diff --git a/src/dns/dnsresolver.cpp b/src/dns/dnsresolver.cpp index f1bbb6f..71a79dc 100644 --- a/src/dns/dnsresolver.cpp +++ b/src/dns/dnsresolver.cpp @@ -24,7 +24,9 @@ from https://github.com/softwareace/Boost.DNS */ -#include "dns_neww/dnsresolver.h" +#include "dns/dnsresolver.h" + +#include #include #include @@ -53,7 +55,7 @@ DnsResolver::DnsResolver(IoServiceItem &io_serv, const DnsCacheItem cache, const boost::asio::ip::address &name_server) : ResolverBase( io_serv, hostname, cache ) - , Socket( *io_serv, ip::udp::endpoint(ip::udp::v4(), 0) ) + , Socket( *io_serv ) , ReceiveBuffer() , Protocol( protocol ) , NameServer( name_server, Config::DNS_PORT ) @@ -65,13 +67,30 @@ DnsResolver::DnsResolver(IoServiceItem &io_serv, , IsResolving( false ) { } +DnsResolver::~DnsResolver() +{ + boost::system::error_code error; + Socket.shutdown(boost::asio::ip::udp::socket::shutdown_both, error); //both=send&receive + if ( error ) + GlobalLogger.warning() << "Received error " << error + << " when shutting down socket for DNS"; + // in IcmpPinger always gave an error system:9 + // (probably EBADF: Bad file descriptor) + + Socket.close(error); + if ( error ) + GlobalLogger.warning() << "Received error " << error + << " when closing socket for DNS"; +} + + //============================================================================== // ASYNC RESOLVE //============================================================================== /** - * copied here code from boost::net::dns::resolve.hpp, since want async + * copied here code from boost::net::dns::resolve.hpp, since want async * operation and that is used only internally, there */ void DnsResolver::do_resolve() @@ -399,8 +418,8 @@ bool DnsResolver::have_up_to_date_ip() HostAddressVec cached_data = ResolverBase::get_cached_results(); // get threshold - int resolved_ip_ttl_threshold = DnsMaster::get_instance() - ->get_resolved_ip_ttl_threshold(); + uint32_t resolved_ip_ttl_threshold = static_cast( + DnsMaster::get_instance()->get_resolved_ip_ttl_threshold() ); // loop over addresses BOOST_FOREACH( const HostAddress &addr, cached_data ) diff --git a/src/dns/dnsresolver.h b/src/dns/dnsresolver.h index d56df2a..d622c47 100644 --- a/src/dns/dnsresolver.h +++ b/src/dns/dnsresolver.h @@ -23,7 +23,7 @@ #ifndef DNS_RESOLVER_H #define DNS_RESOLVER_H -#include "dns_neww/dnsmaster.h" +#include "dns/dnsmaster.h" #include #include @@ -31,9 +31,9 @@ #include #include // dns_buffer_t -#include "dns_neww/resolverbase.h" -#include "dns_neww/dnsmaster.h" -#include "dns_neww/dnscache.h" +#include "dns/resolverbase.h" +#include "dns/dnsmaster.h" +#include "dns/dnscache.h" class DnsResolver : public ResolverBase @@ -63,6 +63,7 @@ public: protected: void do_resolve(); +// internal helper functions private: void handle_resolve_timeout(const boost::system::error_code &error); void handle_dns_result(const boost::system::error_code &error, @@ -77,6 +78,7 @@ private: void stop_trying(); void wait_timer_timeout_handler(const boost::system::error_code &error); +// variables private: boost::asio::ip::udp::socket Socket; boost::net::dns_buffer_t ReceiveBuffer; diff --git a/src/dns/hostaddress.cpp b/src/dns/hostaddress.cpp index 89b64b5..ebc01ad 100644 --- a/src/dns/hostaddress.cpp +++ b/src/dns/hostaddress.cpp @@ -20,7 +20,6 @@ on this file might be covered by the GNU General Public License. #include "dns/hostaddress.h" #include -#include #include "boost_assert_handler.h" @@ -73,10 +72,3 @@ void HostAddress::set_ttl( const TimeToLive &ttl ) { Ttl = ttl; } - -template -void HostAddress::serialize(Archive & ar, const unsigned int version) -{ - ar & Ip; - ar & Ttl; -} diff --git a/src/dns/hostaddress.h b/src/dns/hostaddress.h index 0c70827..2722228 100644 --- a/src/dns/hostaddress.h +++ b/src/dns/hostaddress.h @@ -26,6 +26,9 @@ on this file might be covered by the GNU General Public License. #include #include +#include +#include +#include #include "dns/timetolive.h" @@ -55,10 +58,47 @@ private: /// time-to-live of the host IP TimeToLive Ttl; - /// serialization + /// serialization: needs to be implemented here or will not compile + // since there is no serialize in ip::address nor ptime, + // need to save in other formats here friend class boost::serialization::access; template - void serialize(Archive & ar, const unsigned int version); + void save(Archive & ar, const unsigned int version) const + { + std::string ip = Ip.to_string(); + std::string now_str = boost::posix_time::to_iso_string( + boost::posix_time::second_clock::universal_time()); + uint32_t ttl_seconds_now = Ttl.get_updated_value(); + + ar & BOOST_SERIALIZATION_NVP(ip); + ar & BOOST_SERIALIZATION_NVP(now_str); + ar & BOOST_SERIALIZATION_NVP(ttl_seconds_now); + } + + template + void load(Archive & ar, const unsigned int version) + { + std::string ip; + std::string save_time_str; + uint32_t ttl_seconds_at_save; + ar & BOOST_SERIALIZATION_NVP(ip); + ar & BOOST_SERIALIZATION_NVP(save_time_str); + ar & BOOST_SERIALIZATION_NVP(ttl_seconds_at_save); + + // now convert to Ip and Ttl + Ip = boost::asio::ip::address::from_string(ip); + boost::posix_time::ptime save_time = + boost::posix_time::from_iso_string(save_time_str); + boost::posix_time::ptime now = + boost::posix_time::second_clock::universal_time(); + uint32_t elapsed_seconds_since_save = (save_time - now).total_seconds(); + if (elapsed_seconds_since_save > ttl_seconds_at_save) + Ttl = TimeToLive(0); + else + Ttl = TimeToLive(ttl_seconds_at_save - elapsed_seconds_since_save); + } + + BOOST_SERIALIZATION_SPLIT_MEMBER() }; //----------------------------------------------------------------------------- diff --git a/src/dns/ippseudoresolver.h b/src/dns/ippseudoresolver.h index dd7b792..c165511 100644 --- a/src/dns/ippseudoresolver.h +++ b/src/dns/ippseudoresolver.h @@ -25,8 +25,8 @@ #include #include "dns/hostaddress.h" -#include "dns_neww/resolverbase.h" -#include "dns_neww/dnsmaster.h" +#include "dns/resolverbase.h" +#include "dns/dnsmaster.h" namespace Config { diff --git a/src/dns/resolverbase.cpp b/src/dns/resolverbase.cpp index 2587e63..dc0a6a6 100644 --- a/src/dns/resolverbase.cpp +++ b/src/dns/resolverbase.cpp @@ -20,14 +20,14 @@ Christian Herdtweck, Intra2net AG 2015 */ -#include "dns_neww/resolverbase.h" -#include "dns_neww/dnsmaster.h" +#include "dns/resolverbase.h" +#include "dns/dnsmaster.h" #include + ResolverBase::~ResolverBase() -{ -} +{ } /** * callbacks should be of type diff --git a/src/dns/resolverbase.h b/src/dns/resolverbase.h index 3b2a1ab..d5f5e45 100644 --- a/src/dns/resolverbase.h +++ b/src/dns/resolverbase.h @@ -29,7 +29,7 @@ #include #include "host/pinger.h" #include "dns/hostaddress.h" -#include "dns_neww/dnscache.h" +#include "dns/dnscache.h" typedef boost::function callback_type; typedef std::queue callback_list_type; @@ -40,8 +40,6 @@ typedef std::queue callback_list_type; class ResolverBase { public: - virtual HostAddress get_next_ip() = 0; - virtual ~ResolverBase(); /** @@ -51,6 +49,7 @@ public: */ void async_resolve(const callback_type &callback); + virtual HostAddress get_next_ip() = 0; virtual bool have_up_to_date_ip() = 0; virtual int get_resolved_ip_count() = 0; diff --git a/src/dns/timetolive.cpp b/src/dns/timetolive.cpp index 62f61ca..1497042 100644 --- a/src/dns/timetolive.cpp +++ b/src/dns/timetolive.cpp @@ -21,8 +21,6 @@ on this file might be covered by the GNU General Public License. #include "boost_assert_handler.h" -#include - using boost::date_time::time_resolution_traits_adapted64_impl; using boost::posix_time::microsec_clock; using boost::posix_time::ptime; @@ -74,10 +72,3 @@ uint32_t TimeToLive::get_updated_value() const return remaining_seconds; } - -template -void TimeToLive::serialize(Archive & ar, const unsigned int version) -{ - ar & Ttl; - ar & TtlSetTime; -} diff --git a/src/dns/timetolive.h b/src/dns/timetolive.h index a73b2a3..cfd5168 100644 --- a/src/dns/timetolive.h +++ b/src/dns/timetolive.h @@ -23,7 +23,6 @@ on this file might be covered by the GNU General Public License. #include #include -#include //----------------------------------------------------------------------------- // TimeToLive @@ -46,12 +45,6 @@ private: /// the time when the time-to-live was set, so it is possible to know the /// elapsed time boost::posix_time::ptime TtlSetTime; - - - /// serialization - friend class boost::serialization::access; - template - void serialize(Archive & ar, const unsigned int version); }; #endif // TIME_TO_LIVE_H diff --git a/src/host/pingrotate.cpp b/src/host/pingrotate.cpp index 018e812..d3166a3 100644 --- a/src/host/pingrotate.cpp +++ b/src/host/pingrotate.cpp @@ -26,7 +26,7 @@ #include #include "boost_assert_handler.h" -#include "dns_neww/dnsmaster.h" +#include "dns/dnsmaster.h" #include "host/pingerfactory.h" using namespace std; diff --git a/src/host/pingrotate.h b/src/host/pingrotate.h index d70b653..389c450 100644 --- a/src/host/pingrotate.h +++ b/src/host/pingrotate.h @@ -30,7 +30,7 @@ #include #include -#include "dns_neww/resolverbase.h" +#include "dns/resolverbase.h" #include "host/pinger.h" #include "host/pingprotocol.h" diff --git a/src/main.cpp b/src/main.cpp index 0dbfaec..19f6f38 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,7 +41,7 @@ on this file might be covered by the GNU General Public License. #include "host/pingprotocol.h" #include "host/pingscheduler.h" #include "icmp/icmppinger.h" // contains IcmpPacketDistributor -#include "dns_neww/dnsmaster.h" +#include "dns/dnsmaster.h" using namespace std; diff --git a/test/CMakeLists.test_configurationcommandline.txt b/test/CMakeLists.test_configurationcommandline.txt index e8ffe65..0425095 100644 --- a/test/CMakeLists.test_configurationcommandline.txt +++ b/test/CMakeLists.test_configurationcommandline.txt @@ -30,6 +30,7 @@ add_executable(test_configurationcommandline ${CMAKE_SOURCE_DIR}/src/config/option/pingreplytimeoutoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/maxaddressresolutionattemptsoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/resolvedipttlthresholdoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/dnscachefileoption.cpp ${CMAKE_SOURCE_DIR}/src/host/loglevel.cpp ${CMAKE_SOURCE_DIR}/src/host/logoutput.cpp ${CMAKE_SOURCE_DIR}/src/host/pingprotocol.cpp diff --git a/test/CMakeLists.test_configurationfile.txt b/test/CMakeLists.test_configurationfile.txt index 3c95c94..da1dcad 100644 --- a/test/CMakeLists.test_configurationfile.txt +++ b/test/CMakeLists.test_configurationfile.txt @@ -30,6 +30,7 @@ add_executable(test_configurationfile ${CMAKE_SOURCE_DIR}/src/config/option/pingreplytimeoutoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/maxaddressresolutionattemptsoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/resolvedipttlthresholdoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/dnscachefileoption.cpp ${CMAKE_SOURCE_DIR}/src/host/loglevel.cpp ${CMAKE_SOURCE_DIR}/src/host/logoutput.cpp ${CMAKE_SOURCE_DIR}/src/host/pingprotocol.cpp diff --git a/test/CMakeLists.test_configurationoptions.txt b/test/CMakeLists.test_configurationoptions.txt index 94d69f6..65ac76c 100644 --- a/test/CMakeLists.test_configurationoptions.txt +++ b/test/CMakeLists.test_configurationoptions.txt @@ -28,6 +28,7 @@ add_executable(test_configurationoptions ${CMAKE_SOURCE_DIR}/src/config/option/pingreplytimeoutoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/maxaddressresolutionattemptsoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/resolvedipttlthresholdoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/dnscachefileoption.cpp ${CMAKE_SOURCE_DIR}/src/host/loglevel.cpp ${CMAKE_SOURCE_DIR}/src/host/logoutput.cpp ${CMAKE_SOURCE_DIR}/src/host/pingprotocol.cpp