Pings the host at a specific port (available only to TCP ping)
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 23 Aug 2011 03:33:07 +0000 (00:33 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 23 Aug 2011 03:33:07 +0000 (00:33 -0300)
src/host/pinger.h
src/host/pingscheduler.cpp
src/host/pingscheduler.h
src/icmp/icmppinger.cpp
src/icmp/icmppinger.h
src/main.cpp
src/tcp/tcppinger.cpp
src/tcp/tcppinger.h

index 859a5ae..27c9d1d 100644 (file)
@@ -37,6 +37,7 @@ public:
 
     virtual void ping(
             const std::string &destination_ip,
+            const int destination_port,
             boost::function<void(bool)> ping_done_callback
     ) = 0;
 
index baaf831..bcb103b 100644 (file)
@@ -20,6 +20,7 @@ on this file might be covered by the GNU General Public License.
 #include "host/pingscheduler.h"
 
 #include <iostream>
+#include <limits>
 
 #include <boost/bind.hpp>
 
@@ -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<uint16_t>::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 )
index 1bd3556..feed1d3 100644 (file)
@@ -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<Pinger> Ping;
-    /// thread object
+    /// Thread object
     boost::thread Thread;
 
 };
index fcfc161..2cdd223 100644 (file)
@@ -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
 )
 {
index 69a92e4..d0a09c6 100644 (file)
@@ -38,6 +38,7 @@ public:
 
     void ping(
             const std::string &destination_ip,
+            const int destination_port,
             boost::function< void(bool) > ping_done_callback
     );
 
index 281d754..0040f57 100644 (file)
@@ -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,
index fe4eee5..1e9cc19 100644 (file)
@@ -26,11 +26,13 @@ on this file might be covered by the GNU General Public License.
 
 #include <istream>
 #include <ostream>
+#include <limits>
 
 #include <boost/assert.hpp>
 #include <boost/bind.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/date_time/posix_time/posix_time_types.hpp>
+#include <boost/static_assert.hpp>
 #include <boost/uuid/uuid.hpp>
 #include <boost/uuid/uuid_generators.hpp>
 
@@ -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<void(bool)> ping_done_callback
 )
 {
     BOOST_ASSERT( !destination_ip.empty() );
+    BOOST_ASSERT( ( 0 <= destination_port ) && ( destination_port < numeric_limits<uint16_t>::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<uint16_t>::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<uint16_t>( 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<uint16_t>( ( 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,
index 51382ba..a52904d 100644 (file)
@@ -52,13 +52,20 @@ public:
 
     void ping(
             const std::string &destination_ip,
+            const int destination_port,
             boost::function<void(bool)> 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(