virtual void ping(
const std::string &destination_ip,
+ const int destination_port,
boost::function<void(bool)> ping_done_callback
) = 0;
#include "host/pingscheduler.h"
#include <iostream>
+#include <limits>
#include <boost/bind.hpp>
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,
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()
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()
}
string destination_ip = IpList.get_next_ip();
- ping( destination_ip);
+ ping( destination_ip, DestinationPort );
}
void PingScheduler::ping_done_handler( bool ping_success )
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,
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();
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;
};
* @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
)
{
void ping(
const std::string &destination_ip,
+ const int destination_port,
boost::function< void(bool) > ping_done_callback
);
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,
#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>
* @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();
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 );
}
// 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,
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(