From 1309d0e48ffd31fa5137891d383da35895528115 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Tue, 23 Aug 2011 00:33:07 -0300 Subject: [PATCH] Pings the host at a specific port (available only to TCP ping) --- src/host/pinger.h | 1 + src/host/pingscheduler.cpp | 17 ++++++++++++++--- src/host/pingscheduler.h | 24 +++++++++++++++--------- src/icmp/icmppinger.cpp | 6 +++++- src/icmp/icmppinger.h | 1 + src/main.cpp | 2 ++ src/tcp/tcppinger.cpp | 27 ++++++++++++++++++++++----- src/tcp/tcppinger.h | 9 ++++++++- 8 files changed, 68 insertions(+), 19 deletions(-) diff --git a/src/host/pinger.h b/src/host/pinger.h index 859a5ae..27c9d1d 100644 --- a/src/host/pinger.h +++ b/src/host/pinger.h @@ -37,6 +37,7 @@ public: virtual void ping( const std::string &destination_ip, + const int destination_port, boost::function ping_done_callback ) = 0; diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index baaf831..bcb103b 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -20,6 +20,7 @@ on this file might be covered by the GNU General Public License. #include "host/pingscheduler.h" #include +#include #include @@ -47,6 +48,7 @@ using I2n::Logger::GlobalLogger; PingScheduler::PingScheduler( const string &network_interface, const string &destination_address, + const int destination_port, const PingProtocol protocol, const long ping_interval_in_sec, const int ping_fail_percentage_limit, @@ -60,6 +62,7 @@ PingScheduler::PingScheduler( TimeSentLastPing( microsec_clock::universal_time() ), PingIntervalInSec( ping_interval_in_sec ), IpList( destination_address, nameserver ), + DestinationPort( destination_port ), HostAnalyzer( destination_address, ping_fail_percentage_limit, link_analyzer ), Ping(), Thread() @@ -139,11 +142,19 @@ bool PingScheduler::resolve_ping_address() return true; } -void PingScheduler::ping( const string &destination_ip ) +void PingScheduler::ping( + const string &destination_ip, + const int destination_port +) { BOOST_ASSERT( !destination_ip.empty() ); + BOOST_ASSERT( ( 0 <= destination_port ) && ( destination_port < numeric_limits::max() ) ); - Ping->ping( destination_ip, boost::bind(&PingScheduler::ping_done_handler, this, _1) ); + Ping->ping( + destination_ip, + destination_port, + boost::bind(&PingScheduler::ping_done_handler, this, _1) + ); } void PingScheduler::setup_next_ping() @@ -163,7 +174,7 @@ void PingScheduler::setup_next_ping() } string destination_ip = IpList.get_next_ip(); - ping( destination_ip); + ping( destination_ip, DestinationPort ); } void PingScheduler::ping_done_handler( bool ping_success ) diff --git a/src/host/pingscheduler.h b/src/host/pingscheduler.h index 1bd3556..feed1d3 100644 --- a/src/host/pingscheduler.h +++ b/src/host/pingscheduler.h @@ -51,6 +51,7 @@ public: PingScheduler( const std::string &network_interface, const std::string &destination_address, + const int destination_port, const PingProtocol protocol, const long ping_interval_in_sec, const int ping_fail_percentage_limit, @@ -68,7 +69,10 @@ private: void stop_pinging(); bool resolve_ping_address(); - void ping( const std::string &destination_ip); + void ping( + const std::string &destination_ip, + const int destination_port + ); void ping_done_handler(bool ping_success); void setup_next_ping(); @@ -79,23 +83,25 @@ private: void update_ping_elapsed_time(); private: - /// service object, which has the event loop + /// Service object, which has the event loop boost::asio::io_service IoService; - /// name of the network device used to send the packets + /// Name of the network device used to send the packets std::string LocalNetworkInterfaceName; - /// timer to trigger the next ping + /// Timer to trigger the next ping boost::asio::deadline_timer NextPingTimer; - /// keeps track of the time when the last ping was send + /// Keeps track of the time when the last ping was send boost::posix_time::ptime TimeSentLastPing; - /// interval between each ping to the same host + /// Interval between each ping to the same host PingInterval PingIntervalInSec; - /// the list of IPs which are aliases to the host DNS + /// The list of IPs which are aliases to the host DNS DnsResolver IpList; - /// object responsible to evaluate the status of the host + /// The port to ping at destination host + int DestinationPort; + /// Object responsible to evaluate the status of the host HostStatusAnalyzer HostAnalyzer; /// Internal boost pinger object boost::shared_ptr Ping; - /// thread object + /// Thread object boost::thread Thread; }; diff --git a/src/icmp/icmppinger.cpp b/src/icmp/icmppinger.cpp index fcfc161..2cdd223 100644 --- a/src/icmp/icmppinger.cpp +++ b/src/icmp/icmppinger.cpp @@ -85,10 +85,14 @@ IcmpPinger::~IcmpPinger() * @brief Ping a destination address from an available local source. * * @param destination_ip The address of the host to ping. - * @param done_handler Done handler will be called on successful ping or timeout + * @param destination_port The port at the destination host to ping. + * @param done_handler Done handler will be called on successful ping or timeout. + * + * @return void. */ void IcmpPinger::ping( const string &destination_ip, + const int /*destination_port*/, // the ICMP protocol does not use ports function< void(bool) > ping_done_callback ) { diff --git a/src/icmp/icmppinger.h b/src/icmp/icmppinger.h index 69a92e4..d0a09c6 100644 --- a/src/icmp/icmppinger.h +++ b/src/icmp/icmppinger.h @@ -38,6 +38,7 @@ public: void ping( const std::string &destination_ip, + const int destination_port, boost::function< void(bool) > ping_done_callback ); diff --git a/src/main.cpp b/src/main.cpp index 281d754..0040f57 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -103,11 +103,13 @@ void init_pingers( BOOST_FOREACH( HostItem host, hosts ) { string destination_address = host->get_address(); + int destination_port = host->get_port(); int ping_interval_in_sec = host->get_interval_in_sec(); PingSchedulerItem scheduler( new PingScheduler( local_interface, destination_address, + destination_port, protocol, ping_interval_in_sec, ping_fail_limit, diff --git a/src/tcp/tcppinger.cpp b/src/tcp/tcppinger.cpp index fe4eee5..1e9cc19 100644 --- a/src/tcp/tcppinger.cpp +++ b/src/tcp/tcppinger.cpp @@ -26,11 +26,13 @@ on this file might be covered by the GNU General Public License. #include #include +#include #include #include #include #include +#include #include #include @@ -93,21 +95,26 @@ TcpPinger::~TcpPinger() * @brief Ping a destination address from an available local source. * * @param destination_ip The address of the host to ping. - * @param done_handler Done handler will be called on successful ping or timeout + * @param destination_port The port at the destination host to ping. + * @param done_handler Done handler will be called on successful ping or timeout. + * + * @return void. */ void TcpPinger::ping( const string &destination_ip, + const int destination_port, function ping_done_callback ) { BOOST_ASSERT( !destination_ip.empty() ); + BOOST_ASSERT( ( 0 <= destination_port ) && ( destination_port < numeric_limits::max() ) ); PingDoneCallback = ping_done_callback; // Prepare ping set_ping_status( PingStatus_NotSent ); - set_destination_endpoint( destination_ip ); + set_destination_endpoint( destination_ip, destination_port ); start_send(); start_receive(); @@ -126,12 +133,22 @@ uint32_t TcpPinger::get_destination_address() const return destination_ipv4_address; } -void TcpPinger::set_destination_endpoint( const string &destination_ip ) +uint16_t TcpPinger::get_destination_port() const +{ + return DestinationEndpoint.port(); +} + +void TcpPinger::set_destination_endpoint( + const string &destination_ip, + const int destination_port +) { BOOST_ASSERT( !destination_ip.empty() ); + BOOST_ASSERT( ( 0 <= destination_port ) && ( destination_port <= numeric_limits::max() ) ); + BOOST_STATIC_ASSERT( sizeof(uint16_t) <= sizeof(destination_port) ); - uint16_t port = 0; address destination_address = address::from_string( destination_ip ); + uint16_t port = static_cast( destination_port ); DestinationEndpoint = tcp_raw_protocol::endpoint( destination_address, port ); } @@ -141,7 +158,7 @@ void TcpPinger::start_send() // Create an TCP header for an ACK request. uint16_t source_port = static_cast( ( random() % 16383 ) + 49152 ); // same as random() % 65536; - uint16_t destination_port = 80; + uint16_t destination_port = get_destination_port(); TcpHeader tcp_header = create_ack_request( source_port, destination_port, diff --git a/src/tcp/tcppinger.h b/src/tcp/tcppinger.h index 51382ba..a52904d 100644 --- a/src/tcp/tcppinger.h +++ b/src/tcp/tcppinger.h @@ -52,13 +52,20 @@ public: void ping( const std::string &destination_ip, + const int destination_port, boost::function ping_done_callback ); private: uint32_t get_source_address(); uint32_t get_destination_address() const; - void set_destination_endpoint( const std::string &destination_ip ); + + uint16_t get_destination_port() const; + + void set_destination_endpoint( + const std::string &destination_ip, + const int destination_port + ); void start_send(); TcpHeader create_ack_request( -- 1.7.1