From 079d19ab4b0ac1261f2d76a93e5904a418e482d2 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Fri, 7 Nov 2014 16:52:49 +0100 Subject: [PATCH] converted 3 constants into configuration variables, removing corresponding TODOs; minor change in variable orders to make compiler happier --- src/CMakeLists.txt | 3 + src/config/configuration.cpp | 33 ++++++++++ src/config/configuration.h | 20 +++++- src/config/configurationoptions.cpp | 13 ++++ .../option/maxaddressresolutionattemptsoption.cpp | 65 ++++++++++++++++++++ .../option/maxaddressresolutionattemptsoption.h | 49 +++++++++++++++ src/config/option/pingreplytimeoutoption.cpp | 65 ++++++++++++++++++++ src/config/option/pingreplytimeoutoption.h | 49 +++++++++++++++ src/config/option/resolvedipttlthresholdoption.cpp | 65 ++++++++++++++++++++ src/config/option/resolvedipttlthresholdoption.h | 49 +++++++++++++++ src/dns/dnsresolver.cpp | 10 ++-- src/dns/dnsresolver.h | 5 +- src/dns/dnsresolverfactory.cpp | 7 ++- src/dns/dnsresolverfactory.h | 1 + src/host/pingerfactory.cpp | 20 +++--- src/host/pingerfactory.h | 7 ++- src/host/pingrotate.cpp | 8 ++- src/host/pingrotate.h | 6 ++ src/host/pingscheduler.cpp | 14 +++- src/host/pingscheduler.h | 17 +++--- src/main.cpp | 6 ++ test/CMakeLists.test_configurationcommandline.txt | 3 + test/CMakeLists.test_configurationfile.txt | 3 + test/CMakeLists.test_configurationoptions.txt | 3 + 24 files changed, 485 insertions(+), 36 deletions(-) create mode 100644 src/config/option/maxaddressresolutionattemptsoption.cpp create mode 100644 src/config/option/maxaddressresolutionattemptsoption.h create mode 100644 src/config/option/pingreplytimeoutoption.cpp create mode 100644 src/config/option/pingreplytimeoutoption.h create mode 100644 src/config/option/resolvedipttlthresholdoption.cpp create mode 100644 src/config/option/resolvedipttlthresholdoption.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8d67551..ef00cfc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -58,6 +58,9 @@ set(SOURCES config/option/statusnotifiercmdoption.cpp config/option/versionoption.cpp config/option/ratiorandomhostsoption.cpp + config/option/pingreplytimeoutoption.cpp + config/option/maxaddressresolutionattemptsoption.cpp + config/option/resolvedipttlthresholdoption.cpp dns/dnsresolver.cpp dns/dnsresolverfactory.cpp dns/hostaddress.cpp diff --git a/src/config/configuration.cpp b/src/config/configuration.cpp index 2c29eeb..21fa226 100644 --- a/src/config/configuration.cpp +++ b/src/config/configuration.cpp @@ -57,6 +57,9 @@ Configuration::Configuration() : LinkDownIntervalInMin( 0 ), MinStableLinkIntervalInMin( 0 ), MaxStableLinkIntervalInMin( 60 ), + PingReplyTimeout( 30 ), + MaxAddressResolutionAttempts( 10 ), + ResolvedIpTtlThreshold( 10 ), Hosts(), RatioRandomHosts( 1.0f ), RandomNumberGenerator( boost::numeric_cast(time(0)) ) @@ -199,6 +202,36 @@ void Configuration::set_link_down_interval_in_min( LinkDownIntervalInMin = link_down_interval_in_min; } +int Configuration::get_ping_reply_timeout() const +{ + return PingReplyTimeout; +} +void Configuration::set_ping_reply_timeout( const int ping_reply_timeout ) +{ + BOOST_ASSERT(ping_reply_timeout > 0 ); + PingReplyTimeout = ping_reply_timeout; +} + +int Configuration::get_max_address_resolution_attempts() const +{ + return MaxAddressResolutionAttempts; +} +void Configuration::set_max_address_resolution_attempts( const int max_address_resolution_attempts ) +{ + BOOST_ASSERT(max_address_resolution_attempts > 0 ); + MaxAddressResolutionAttempts = max_address_resolution_attempts; +} + +int Configuration::get_resolved_ip_ttl_threshold() const +{ + return ResolvedIpTtlThreshold; +} +void Configuration::set_resolved_ip_ttl_threshold( const int resolved_ip_ttl_threshold ) +{ + BOOST_ASSERT(resolved_ip_ttl_threshold > 0 ); + ResolvedIpTtlThreshold = resolved_ip_ttl_threshold; +} + float Configuration::get_ratio_random_hosts() const { return RatioRandomHosts; diff --git a/src/config/configuration.h b/src/config/configuration.h index a041ce5..ebce5c2 100644 --- a/src/config/configuration.h +++ b/src/config/configuration.h @@ -84,16 +84,27 @@ public: int get_link_down_interval_in_min() const; void set_link_down_interval_in_min( const int link_down_interval_in_min ); + int get_ping_reply_timeout() const; + void set_ping_reply_timeout( const int ping_reply_timeout ); + + int get_max_address_resolution_attempts() const; + void set_max_address_resolution_attempts( + const int max_address_resolution_attempts ); + + int get_resolved_ip_ttl_threshold() const; + void set_resolved_ip_ttl_threshold( + const int resolved_ip_ttl_threshold ); + float get_ratio_random_hosts() const; void set_ratio_random_hosts( const float ratio ); HostList get_hosts() const; void set_hosts( const HostList &hosts_list ); - bool randomize_hosts(); - int get_random_number(const int lowest, const int highest); + bool randomize_hosts(); + private: bool Daemon; I2n::Logger::LogLevel LoggingLevel; @@ -112,8 +123,11 @@ private: int LinkDownIntervalInMin; int MinStableLinkIntervalInMin; int MaxStableLinkIntervalInMin; - float RatioRandomHosts; + int PingReplyTimeout; + int MaxAddressResolutionAttempts; + int ResolvedIpTtlThreshold; HostList Hosts; + float RatioRandomHosts; rand_gen_type RandomNumberGenerator; }; diff --git a/src/config/configurationoptions.cpp b/src/config/configurationoptions.cpp index 4377700..80501d1 100644 --- a/src/config/configurationoptions.cpp +++ b/src/config/configurationoptions.cpp @@ -45,6 +45,9 @@ #include "config/option/statusnotifiercmdoption.h" #include "config/option/versionoption.h" #include "config/option/ratiorandomhostsoption.h" +#include "config/option/pingreplytimeoutoption.h" +#include "config/option/maxaddressresolutionattemptsoption.h" +#include "config/option/resolvedipttlthresholdoption.h" using namespace std; using boost::program_options::option_description; @@ -104,6 +107,16 @@ ConfigurationOptions::ConfigurationOptions() : ConfigurationOptionItem status_notifier_cmd( new StatusNotifierCmdOption ); ConfigOptions.push_back( status_notifier_cmd ); + PingReplyTimeoutOption(); + ConfigurationOptionItem ping_reply_timeout( new PingReplyTimeoutOption ); + ConfigOptions.push_back( ping_reply_timeout ); + + ConfigurationOptionItem max_address_resolution_attempts( new MaxAddressResolutionAttemptsOption ); + ConfigOptions.push_back( max_address_resolution_attempts ); + + ConfigurationOptionItem resolved_ip_ttl_threshold( new ResolvedIpTtlThresholdOption ); + ConfigOptions.push_back( resolved_ip_ttl_threshold ); + ConfigurationOptionItem ratio_random_hosts( new RatioRandomHostsOption ); ConfigOptions.push_back( ratio_random_hosts ); diff --git a/src/config/option/maxaddressresolutionattemptsoption.cpp b/src/config/option/maxaddressresolutionattemptsoption.cpp new file mode 100644 index 0000000..b1acc91 --- /dev/null +++ b/src/config/option/maxaddressresolutionattemptsoption.cpp @@ -0,0 +1,65 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#include "config/option/maxaddressresolutionattemptsoption.h" + +#include + +using namespace std; +using boost::program_options::value; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// MaxAddressResolutionAttemptsOption +//----------------------------------------------------------------------------- + +MaxAddressResolutionAttemptsOption::MaxAddressResolutionAttemptsOption() : + ConfigurationOption( + "max-address-resolution-attempts", + value()->default_value( 10 ), + "Number of attempts at DNS resolution until target is assumed offline" + ) +{ +} + +MaxAddressResolutionAttemptsOption::~MaxAddressResolutionAttemptsOption() +{ +} + +bool MaxAddressResolutionAttemptsOption::parse( + const variables_map& vm, + Configuration *configuration +) +{ + // default-source-network-interface + if ( 1 <= vm.count( get_command_string() ) ) + { + int max_address_resolution_attempts = vm[ get_command_string() ].as (); + configuration->set_max_address_resolution_attempts( max_address_resolution_attempts ); + + GlobalLogger.info() << get_command_string() << "=" + << max_address_resolution_attempts << endl; + + return true; + } + + return false; +} diff --git a/src/config/option/maxaddressresolutionattemptsoption.h b/src/config/option/maxaddressresolutionattemptsoption.h new file mode 100644 index 0000000..748550f --- /dev/null +++ b/src/config/option/maxaddressresolutionattemptsoption.h @@ -0,0 +1,49 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#ifndef MAX_ADDRESS_RESOLUTION_ATTEMPTS_H +#define MAX_ADDRESS_RESOLUTION_ATTEMPTS_H + +#include + +#include "config/option/configurationoption.h" + +//----------------------------------------------------------------------------- +// MaxAddressResolutionAttemptsOption +//----------------------------------------------------------------------------- + +/** + * @brief This class represents the "max-address-resolution-attempts" + * configuration option. + */ +class MaxAddressResolutionAttemptsOption : public ConfigurationOption +{ +public: + MaxAddressResolutionAttemptsOption(); + virtual ~MaxAddressResolutionAttemptsOption(); + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // MAX_ADDRESS_RESOLUTION_ATTEMPTS_H diff --git a/src/config/option/pingreplytimeoutoption.cpp b/src/config/option/pingreplytimeoutoption.cpp new file mode 100644 index 0000000..d56d24b --- /dev/null +++ b/src/config/option/pingreplytimeoutoption.cpp @@ -0,0 +1,65 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#include "config/option/pingreplytimeoutoption.h" + +#include + +using namespace std; +using boost::program_options::value; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// PingReplyTimeoutOption +//----------------------------------------------------------------------------- + +PingReplyTimeoutOption::PingReplyTimeoutOption() : + ConfigurationOption( + "ping-reply-timeout", + value()->default_value( 30 ), + "Time in seconds to wait for reply from pings" + ) +{ +} + +PingReplyTimeoutOption::~PingReplyTimeoutOption() +{ +} + +bool PingReplyTimeoutOption::parse( + const variables_map& vm, + Configuration *configuration +) +{ + // default-source-network-interface + if ( 1 <= vm.count( get_command_string() ) ) + { + int ping_reply_timeout = vm[ get_command_string() ].as (); + configuration->set_ping_reply_timeout( ping_reply_timeout ); + + GlobalLogger.info() << get_command_string() << "=" + << ping_reply_timeout << endl; + + return true; + } + + return false; +} diff --git a/src/config/option/pingreplytimeoutoption.h b/src/config/option/pingreplytimeoutoption.h new file mode 100644 index 0000000..cda642c --- /dev/null +++ b/src/config/option/pingreplytimeoutoption.h @@ -0,0 +1,49 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#ifndef PING_REPLY_TIMEOUT_OPTION_H +#define PING_REPLY_TIMEOUT_OPTION_H + +#include + +#include "config/option/configurationoption.h" + +//----------------------------------------------------------------------------- +// PingReplyTimeoutOption +//----------------------------------------------------------------------------- + +/** + * @brief This class represents the "ping-reply-timeout" + * configuration option. + */ +class PingReplyTimeoutOption : public ConfigurationOption +{ +public: + PingReplyTimeoutOption(); + virtual ~PingReplyTimeoutOption(); + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // PING_REPLY_TIMEOUT_OPTION_H diff --git a/src/config/option/resolvedipttlthresholdoption.cpp b/src/config/option/resolvedipttlthresholdoption.cpp new file mode 100644 index 0000000..a8f9672 --- /dev/null +++ b/src/config/option/resolvedipttlthresholdoption.cpp @@ -0,0 +1,65 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#include "config/option/resolvedipttlthresholdoption.h" + +#include + +using namespace std; +using boost::program_options::value; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// ResolvedIpTtlThresholdOption +//----------------------------------------------------------------------------- + +ResolvedIpTtlThresholdOption::ResolvedIpTtlThresholdOption() : + ConfigurationOption( + "resolved-ip-ttl-threshold", + value()->default_value( 10 ), + "Minimum time in milliseconds that IP has to be valid when testing for refresh" + ) +{ +} + +ResolvedIpTtlThresholdOption::~ResolvedIpTtlThresholdOption() +{ +} + +bool ResolvedIpTtlThresholdOption::parse( + const variables_map& vm, + Configuration *configuration +) +{ + // default-source-network-interface + if ( 1 <= vm.count( get_command_string() ) ) + { + int resolved_ip_ttl_threshold = vm[ get_command_string() ].as (); + configuration->set_resolved_ip_ttl_threshold( resolved_ip_ttl_threshold ); + + GlobalLogger.info() << get_command_string() << "=" + << resolved_ip_ttl_threshold << endl; + + return true; + } + + return false; +} diff --git a/src/config/option/resolvedipttlthresholdoption.h b/src/config/option/resolvedipttlthresholdoption.h new file mode 100644 index 0000000..c045c19 --- /dev/null +++ b/src/config/option/resolvedipttlthresholdoption.h @@ -0,0 +1,49 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#ifndef RESOLVED_IP_TTL_THRESHOLD_OPTION_H +#define RESOLVED_IP_TTL_THRESHOLD_OPTION_H + +#include + +#include "config/option/configurationoption.h" + +//----------------------------------------------------------------------------- +// ResolvedIpTtlThresholdOption +//----------------------------------------------------------------------------- + +/** + * @brief This class represents the "resolved-ip-ttl-threshold" + * configuration option. + */ +class ResolvedIpTtlThresholdOption : public ConfigurationOption +{ +public: + ResolvedIpTtlThresholdOption(); + virtual ~ResolvedIpTtlThresholdOption(); + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // RESOLVED_IP_TTL_THRESHOLD_OPTION_H diff --git a/src/dns/dnsresolver.cpp b/src/dns/dnsresolver.cpp index 6e44343..f189930 100644 --- a/src/dns/dnsresolver.cpp +++ b/src/dns/dnsresolver.cpp @@ -61,12 +61,14 @@ using I2n::Logger::GlobalLogger; DnsResolver::DnsResolver( const string &dns_address, const string &nameserver, - const type_t address_resource_record_type + const type_t address_resource_record_type, + const int resolved_ip_ttl_threshold ) : ResolvedHostAddressList(), HostDnsAddress( dns_address ), NameServer( nameserver ), - AddressResourceRecord( address_resource_record_type ) + AddressResourceRecord( address_resource_record_type ), + ResolvedIpTtlThreshold( resolved_ip_ttl_threshold ) { } @@ -169,12 +171,10 @@ string DnsResolver::get_next_ip() */ bool DnsResolver::expired_resolved_ip() const { - const uint32_t threshold = 10; // TODO configurable - BOOST_FOREACH( const HostAddress &host_address, ResolvedHostAddressList ) { uint32_t ttl = host_address.get_ttl().get_updated_value(); - if ( ttl <= threshold ) + if ( ttl <= ResolvedIpTtlThreshold ) { return true; } diff --git a/src/dns/dnsresolver.h b/src/dns/dnsresolver.h index 74f1b70..b79d68c 100644 --- a/src/dns/dnsresolver.h +++ b/src/dns/dnsresolver.h @@ -40,7 +40,8 @@ public: DnsResolver( const std::string &dns_address, const std::string &nameserver, - const boost::net::dns::type_t address_resource_record_type + const boost::net::dns::type_t address_resource_record_type, + const int resolved_ip_ttl_threshold ); ~DnsResolver(); @@ -80,6 +81,8 @@ private: const std::string NameServer; /// 'A' resource records for the IPv4 addresses, or 'AAAA' resource records for the IPv6 addresses boost::net::dns::type_t AddressResourceRecord; + /// Minimum time that IP has to be valid before creating new dns request + const int ResolvedIpTtlThreshold; }; diff --git a/src/dns/dnsresolverfactory.cpp b/src/dns/dnsresolverfactory.cpp index 12f8da2..900cbf3 100644 --- a/src/dns/dnsresolverfactory.cpp +++ b/src/dns/dnsresolverfactory.cpp @@ -54,6 +54,7 @@ DnsResolverFactory::~DnsResolverFactory() DnsResolverItem DnsResolverFactory::createResolver( const string &destination_address, const string &nameserver, + const int resolved_ip_ttl_threshold, const PingProtocol ping_protocol ) { @@ -62,12 +63,14 @@ DnsResolverItem DnsResolverFactory::createResolver( case PingProtocol_ICMP: case PingProtocol_TCP: return DnsResolverItem( - new DnsResolver( destination_address, nameserver, boost::net::dns::type_a ) + new DnsResolver( destination_address, nameserver, boost::net::dns::type_a, + resolved_ip_ttl_threshold) ); case PingProtocol_ICMPv6: case PingProtocol_TCP_IPv6: return DnsResolverItem( - new DnsResolver( destination_address, nameserver, boost::net::dns::type_a6 ) + new DnsResolver( destination_address, nameserver, boost::net::dns::type_a6, + resolved_ip_ttl_threshold) ); default: BOOST_ASSERT( !"Try to create a DNS from an invalid address resource record" ); //lint !e506 diff --git a/src/dns/dnsresolverfactory.h b/src/dns/dnsresolverfactory.h index ff698be..91e92d6 100644 --- a/src/dns/dnsresolverfactory.h +++ b/src/dns/dnsresolverfactory.h @@ -36,6 +36,7 @@ public: static DnsResolverItem createResolver( const std::string &destination_address, const std::string &nameserver, + const int resolved_ip_ttl_threshold, const PingProtocol ping_protocol ); diff --git a/src/host/pingerfactory.cpp b/src/host/pingerfactory.cpp index 561d170..9203b42 100644 --- a/src/host/pingerfactory.cpp +++ b/src/host/pingerfactory.cpp @@ -67,34 +67,32 @@ PingerFactory::~PingerFactory() PingerItem PingerFactory::createPinger( const PingProtocol protocol, io_service &io_serv, - const string &network_interface + const string &network_interface, + const int ping_reply_timeout ) { BOOST_ASSERT( /*( PingProtocol_First <= protocol ) && */( protocol <= PingProtocol_Last ) ); BOOST_ASSERT( !network_interface.empty() ); - // Ping reply timeout. Could be made a configuration variable - const int ping_reply_timeout_in_sec = 30; // TODO - try { switch ( protocol ) { case PingProtocol_ICMP: return PingerItem( - new IcmpPinger( io_serv, icmp::v4(), network_interface, ping_reply_timeout_in_sec ) + new IcmpPinger( io_serv, icmp::v4(), network_interface, ping_reply_timeout ) ); case PingProtocol_ICMPv6: return PingerItem( - new IcmpPinger( io_serv, icmp::v6(), network_interface, ping_reply_timeout_in_sec ) + new IcmpPinger( io_serv, icmp::v6(), network_interface, ping_reply_timeout ) ); case PingProtocol_TCP: return PingerItem( - new TcpPinger( io_serv, tcp_raw_protocol::v4(), network_interface, ping_reply_timeout_in_sec ) + new TcpPinger( io_serv, tcp_raw_protocol::v4(), network_interface, ping_reply_timeout ) ); case PingProtocol_TCP_IPv6: return PingerItem( - new TcpPinger( io_serv, tcp_raw_protocol::v6(), network_interface, ping_reply_timeout_in_sec ) + new TcpPinger( io_serv, tcp_raw_protocol::v6(), network_interface, ping_reply_timeout ) ); default: BOOST_ASSERT( !"Try to create a pinger from an invalid protocol" ); //lint !e506 @@ -131,7 +129,9 @@ PingRotateItem PingerFactory::createPinger( const string &network_interface, const string &destination_address, const uint16_t destination_port, - const string &nameserver + const string &nameserver, + const int resolved_ip_ttl_threshold, + const int ping_reply_timeout ) { BOOST_ASSERT( 0 < protocol_list.size() ); @@ -146,6 +146,8 @@ PingRotateItem PingerFactory::createPinger( destination_address, destination_port, nameserver, + resolved_ip_ttl_threshold, + ping_reply_timeout, protocol_list ) ); } diff --git a/src/host/pingerfactory.h b/src/host/pingerfactory.h index 05d3415..5e6e918 100644 --- a/src/host/pingerfactory.h +++ b/src/host/pingerfactory.h @@ -40,7 +40,8 @@ public: static PingerItem createPinger( const PingProtocol protocol, boost::asio::io_service &io_serv, - const std::string &network_interface + const std::string &network_interface, + const int ping_reply_timeout ); static PingRotateItem createPinger( @@ -49,7 +50,9 @@ public: const std::string &network_interface, const std::string &destination_address, const uint16_t destination_port, - const std::string &nameserver + const std::string &nameserver, + const int resolved_ip_ttl_threshold, + const int ping_reply_timeout ); private: diff --git a/src/host/pingrotate.cpp b/src/host/pingrotate.cpp index 3fbf9ee..b000658 100644 --- a/src/host/pingrotate.cpp +++ b/src/host/pingrotate.cpp @@ -53,6 +53,8 @@ PingRotate::PingRotate( const string &destination_address, const uint16_t destination_port, const string &nameserver, + const int resolved_ip_ttl_threshold, + const int ping_reply_timeout, const PingProtocolList &protocol_list ) : IoService( io_serv ), @@ -61,6 +63,8 @@ PingRotate::PingRotate( DestinationAddess( destination_address ), DestinationPort( destination_port ), Nameserver( nameserver ), + ResolvedIpTtlThreshold( resolved_ip_ttl_threshold ), + PingReplyTimeout( ping_reply_timeout ), ProtocolList( protocol_list ), Ping(), PingDoneCallback() @@ -151,7 +155,7 @@ void PingRotate::get_next_ping_protocol() ProtocolList.pop_front(); - Ping = PingerFactory::createPinger( ping_protocol, IoService, NetworkInterfaceName ); + Ping = PingerFactory::createPinger( ping_protocol, IoService, NetworkInterfaceName, PingReplyTimeout ); update_dns_resolver( ping_protocol ); } @@ -166,7 +170,7 @@ bool PingRotate::can_change_ping_protocol() const void PingRotate::update_dns_resolver( PingProtocol current_protocol ) { - IpList = DnsResolverFactory::createResolver( DestinationAddess, Nameserver, current_protocol ); + IpList = DnsResolverFactory::createResolver( DestinationAddess, Nameserver, ResolvedIpTtlThreshold, current_protocol ); // when the protocol change, there is a chance that the IP version required by the new // protocol differ from the previous one, thus it is required to resolve the address again. diff --git a/src/host/pingrotate.h b/src/host/pingrotate.h index 0ac2f76..e5a68c9 100644 --- a/src/host/pingrotate.h +++ b/src/host/pingrotate.h @@ -51,6 +51,8 @@ public: const std::string &destination_address, const uint16_t destination_port, const std::string &nameserver, + const int resolved_ip_ttl_threshold, + const int ping_reply_timeout, const PingProtocolList &protocol_list ); virtual ~PingRotate(); @@ -92,6 +94,10 @@ private: uint16_t DestinationPort; /// Server to resolve the addresses std::string Nameserver; + /// time threshold for address resolution + int ResolvedIpTtlThreshold; + /// timeout for ping reply + const int PingReplyTimeout; /// The list of protocols to ping PingProtocolList ProtocolList; /// Internal boost pinger object diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index 3da3c0b..9117704 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -67,6 +67,9 @@ PingScheduler::PingScheduler( const long ping_interval_in_sec, const int ping_fail_percentage_limit, const string &nameserver, + const int max_address_resolution_attempts, + const int ping_reply_timeout, + const int resolved_ip_ttl_threshold, LinkStatusItem link_analyzer, const int first_delay @@ -80,6 +83,7 @@ PingScheduler::PingScheduler( HostAnalyzer( destination_address, ping_fail_percentage_limit, link_analyzer ), FirstDelay( first_delay ), AddressResolutionAttempts( 0 ), + MaxAddressResolutionAttempts( max_address_resolution_attempts ), EverHadAnyIP( false ), Ping(), Thread() @@ -97,7 +101,9 @@ PingScheduler::PingScheduler( network_interface, destination_address, destination_port, - nameserver + nameserver, + resolved_ip_ttl_threshold, + ping_reply_timeout ); } @@ -198,7 +204,7 @@ void PingScheduler::resolve_and_ping() else { // no IPs --> try again later, possibly report offline before AddressResolutionAttempts++; - if (AddressResolutionAttempts >= MAX_ADDRESS_RESOLUTION_ATTEMPTS) + if (AddressResolutionAttempts >= MaxAddressResolutionAttempts) { GlobalLogger.notice() << "No IPs after " << AddressResolutionAttempts << " DNS queries!"; HostAnalyzer.report_dns_resolution_failure(); @@ -225,8 +231,8 @@ void PingScheduler::ping() void PingScheduler::ping_done_handler( const bool ping_success ) { // post-processing - // TODO you must call these 3 methods exactly in this order - // Fix this method, once it has a semantic dependency with the + // You must call these 3 methods exactly in this order + // TODO Fix this method, once it has a semantic dependency with the // update_ping_statistics method, because it depends on the PingAnalyzer // statistics to update the exceeded_ping_failed_limit HostAnalyzer.update_ping_statistics( ping_success ); diff --git a/src/host/pingscheduler.h b/src/host/pingscheduler.h index cacc599..8192a9e 100644 --- a/src/host/pingscheduler.h +++ b/src/host/pingscheduler.h @@ -40,8 +40,6 @@ on this file might be covered by the GNU General Public License. // PingScheduler //----------------------------------------------------------------------------- -const int MAX_ADDRESS_RESOLUTION_ATTEMPTS = 3; - /** * @brief This class is responsible to control whether to ping a host. It * schedules periodic pings to a host. @@ -58,6 +56,9 @@ public: const long ping_interval_in_sec, const int ping_fail_percentage_limit, const std::string &nameserver, + const int max_address_resolution_attempts, + const int ping_reply_timeout, + const int resolved_ip_ttl_threshold, LinkStatusItem link_analyzer, const int first_delay ); @@ -102,18 +103,18 @@ private: PingInterval AddressResolveIntervalInSec; /// Object responsible to evaluate the status of the host HostStatus HostAnalyzer; - /// Internal boost pinger object - PingRotateItem Ping; - /// Thread object - boost::thread Thread; /// delay for very first ping to avoid lots of simultaneous pings int FirstDelay; /// number of attempts at Address Resolution int AddressResolutionAttempts; - /// flat whether any DNS lookup succeeded + int MaxAddressResolutionAttempts; + /// flag whether any DNS lookup succeeded // (comparing get_ip_count to 0 might violate assertion in dnsresolver.cpp) bool EverHadAnyIP; - + /// Internal boost pinger object + PingRotateItem Ping; + /// Thread object + boost::thread Thread; }; //----------------------------------------------------------------------------- diff --git a/src/main.cpp b/src/main.cpp index e6c50f7..979beef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -182,6 +182,9 @@ void init_pingers( string default_network_interface = configuration->get_source_network_interface(); string nameserver = configuration->get_nameserver(); int ping_fail_limit = configuration->get_ping_fail_limit(); + int max_address_resolution_attempts = configuration->get_max_address_resolution_attempts(); + int ping_reply_timeout = configuration->get_ping_reply_timeout(); + int resolved_ip_ttl_threshold = configuration->get_resolved_ip_ttl_threshold(); // remove some hosts at random configuration->randomize_hosts(); @@ -219,6 +222,9 @@ void init_pingers( ping_interval_in_sec, ping_fail_limit, nameserver, + max_address_resolution_attempts, + ping_reply_timeout, + resolved_ip_ttl_threshold, status_notifier, current_delay ) diff --git a/test/CMakeLists.test_configurationcommandline.txt b/test/CMakeLists.test_configurationcommandline.txt index a50b1b5..0567483 100644 --- a/test/CMakeLists.test_configurationcommandline.txt +++ b/test/CMakeLists.test_configurationcommandline.txt @@ -26,6 +26,9 @@ add_executable(test_configurationcommandline ${CMAKE_SOURCE_DIR}/src/config/option/statusnotifiercmdoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/versionoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/ratiorandomhostsoption.cpp + ${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/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 62f9adc..ee71cb1 100644 --- a/test/CMakeLists.test_configurationfile.txt +++ b/test/CMakeLists.test_configurationfile.txt @@ -26,6 +26,9 @@ add_executable(test_configurationfile ${CMAKE_SOURCE_DIR}/src/config/option/statusnotifiercmdoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/versionoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/ratiorandomhostsoption.cpp + ${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/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 c54cfc4..38f251b 100644 --- a/test/CMakeLists.test_configurationoptions.txt +++ b/test/CMakeLists.test_configurationoptions.txt @@ -24,6 +24,9 @@ add_executable(test_configurationoptions ${CMAKE_SOURCE_DIR}/src/config/option/statusnotifiercmdoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/versionoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/ratiorandomhostsoption.cpp + ${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/host/loglevel.cpp ${CMAKE_SOURCE_DIR}/src/host/logoutput.cpp ${CMAKE_SOURCE_DIR}/src/host/pingprotocol.cpp -- 1.7.1