moved new dns code from temp dir to dns subdir; changed serialization; added destruct...
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 10 Apr 2015 15:30:21 +0000 (17:30 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 4 May 2015 14:57:57 +0000 (16:57 +0200)
20 files changed:
src/CMakeLists.txt
src/config/configuration.h
src/dns/dnscache.cpp
src/dns/dnsmaster.cpp
src/dns/dnsmaster.h
src/dns/dnsresolver.cpp
src/dns/dnsresolver.h
src/dns/hostaddress.cpp
src/dns/hostaddress.h
src/dns/ippseudoresolver.h
src/dns/resolverbase.cpp
src/dns/resolverbase.h
src/dns/timetolive.cpp
src/dns/timetolive.h
src/host/pingrotate.cpp
src/host/pingrotate.h
src/main.cpp
test/CMakeLists.test_configurationcommandline.txt
test/CMakeLists.test_configurationfile.txt
test/CMakeLists.test_configurationoptions.txt

index 0289bdb..34a7eaf 100644 (file)
@@ -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
index 45752ff..1bb4895 100644 (file)
@@ -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;
 };
 
index 6e7fcca..23f2152 100644 (file)
@@ -20,7 +20,7 @@
  Christian Herdtweck, Intra2net AG 2015
  */
 
-#include "dns_neww/dnscache.h"
+#include "dns/dnscache.h"
 
 #include <fstream>
 #include <logfunc.hpp>
index 7553eef..31814e4 100644 (file)
  Christian Herdtweck, Intra2net AG 2015
  */
 
-#include "dns_neww/dnsmaster.h"
+#include "dns/dnsmaster.h"
 
 #include <logfunc.hpp>
 #include <boost/bind.hpp>
 #include <boost/asio/placeholders.hpp>
 
-#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,
index c8c5866..e431104 100644 (file)
 
 #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<DnsMaster> 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<std::string, DnsIpProtocol> resolver_key_type;
 typedef std::map<resolver_key_type, ResolverItem> resolver_map_type;
index f1bbb6f..71a79dc 100644 (file)
@@ -24,7 +24,9 @@
     from https://github.com/softwareace/Boost.DNS
  */
 
-#include "dns_neww/dnsresolver.h"
+#include "dns/dnsresolver.h"
+
+#include <stdint.h>
 
 #include <boost/foreach.hpp>
 #include <boost/bind.hpp>
@@ -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<uint32_t>(
+                   DnsMaster::get_instance()->get_resolved_ip_ttl_threshold() );
 
     // loop over addresses
     BOOST_FOREACH( const HostAddress &addr, cached_data )
index d56df2a..d622c47 100644 (file)
@@ -23,7 +23,7 @@
 #ifndef DNS_RESOLVER_H
 #define DNS_RESOLVER_H
 
-#include "dns_neww/dnsmaster.h"
+#include "dns/dnsmaster.h"
 
 #include <boost/asio/deadline_timer.hpp>
 #include <boost/asio/ip/udp.hpp>
@@ -31,9 +31,9 @@
 #include <boost/system/error_code.hpp>
 #include <boost/net/network_array.hpp>   // 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;
index 89b64b5..ebc01ad 100644 (file)
@@ -20,7 +20,6 @@ on this file might be covered by the GNU General Public License.
 #include "dns/hostaddress.h"
 
 #include <boost/version.hpp>
-#include <boost/serialization/serialization.hpp>
 
 #include "boost_assert_handler.h"
 
@@ -73,10 +72,3 @@ void HostAddress::set_ttl( const TimeToLive &ttl )
 {
     Ttl = ttl;
 }
-
-template<class Archive>
-void HostAddress::serialize(Archive & ar, const unsigned int version)
-{
-    ar & Ip;
-    ar & Ttl;
-}
index 0c70827..2722228 100644 (file)
@@ -26,6 +26,9 @@ on this file might be covered by the GNU General Public License.
 
 #include <boost/asio.hpp>
 #include <boost/serialization/access.hpp>
+#include <boost/serialization/nvp.hpp>
+#include <boost/serialization/split_member.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
 
 #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<class Archive>
-    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<class Archive>
+    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()
 };
 
 //-----------------------------------------------------------------------------
index dd7b792..c165511 100644 (file)
@@ -25,8 +25,8 @@
 
 #include <boost/asio/ip/address.hpp>
 #include "dns/hostaddress.h"
-#include "dns_neww/resolverbase.h"
-#include "dns_neww/dnsmaster.h"
+#include "dns/resolverbase.h"
+#include "dns/dnsmaster.h"
 
 namespace Config
 {
index 2587e63..dc0a6a6 100644 (file)
  Christian Herdtweck, Intra2net AG 2015
  */
 
-#include "dns_neww/resolverbase.h"
-#include "dns_neww/dnsmaster.h"
+#include "dns/resolverbase.h"
+#include "dns/dnsmaster.h"
 
 #include <boost/bind.hpp>
 
+
 ResolverBase::~ResolverBase()
-{
-}
+{ }
 
 /**
  * callbacks should be of type
index 3b2a1ab..d5f5e45 100644 (file)
@@ -29,7 +29,7 @@
 #include <queue>
 #include "host/pinger.h"
 #include "dns/hostaddress.h"
-#include "dns_neww/dnscache.h"
+#include "dns/dnscache.h"
 
 typedef boost::function<void(const bool, const int)> callback_type;
 typedef std::queue<callback_type> callback_list_type;
@@ -40,8 +40,6 @@ typedef std::queue<callback_type> 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;
 
index 62f61ca..1497042 100644 (file)
@@ -21,8 +21,6 @@ on this file might be covered by the GNU General Public License.
 
 #include "boost_assert_handler.h"
 
-#include <boost/serialization/serialization.hpp>
-
 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<class Archive>
-void TimeToLive::serialize(Archive & ar, const unsigned int version)
-{
-    ar & Ttl;
-    ar & TtlSetTime;
-}
index a73b2a3..cfd5168 100644 (file)
@@ -23,7 +23,6 @@ on this file might be covered by the GNU General Public License.
 #include <stdint.h>
 
 #include <boost/asio.hpp>
-#include <boost/serialization/access.hpp>
 
 //-----------------------------------------------------------------------------
 // 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<class Archive>
-    void serialize(Archive & ar, const unsigned int version);
 };
 
 #endif // TIME_TO_LIVE_H
index 018e812..d3166a3 100644 (file)
@@ -26,7 +26,7 @@
 #include <boost/foreach.hpp>
 
 #include "boost_assert_handler.h"
-#include "dns_neww/dnsmaster.h"
+#include "dns/dnsmaster.h"
 #include "host/pingerfactory.h"
 
 using namespace std;
index d70b653..389c450 100644 (file)
@@ -30,7 +30,7 @@
 #include <boost/shared_ptr.hpp>
 #include <boost/circular_buffer.hpp>
 
-#include "dns_neww/resolverbase.h"
+#include "dns/resolverbase.h"
 #include "host/pinger.h"
 #include "host/pingprotocol.h"
 
index 0dbfaec..19f6f38 100644 (file)
@@ -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;
index e8ffe65..0425095 100644 (file)
@@ -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
index 3c95c94..da1dcad 100644 (file)
@@ -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
index 94d69f6..65ac76c 100644 (file)
@@ -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