// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
-#include "host/boostpinger.h"
+#include "host/icmppinger.h"
#include <istream>
#include <ostream>
#include "icmp/icmppacket.h"
#include "icmp/icmptype.h"
#include "icmp/ipv4header.h"
-#include "boostpinger.h"
using namespace std;
using boost::asio::const_buffers_1;
using I2n::Logger::GlobalLogger;
//-----------------------------------------------------------------------------
-// BoostPinger
+// IcmpPinger
//-----------------------------------------------------------------------------
-BoostPinger::BoostPinger(
+IcmpPinger::IcmpPinger(
boost::asio::io_service &io_service,
string source_network_interface,
const int echo_reply_timeout_in_sec
) :
- IoService(io_service),
+ IoService( io_service ),
DestinationEndpoint(),
Socket( IoService, icmp::v4() ),
IcmpPacketReceiveTimer( IoService ),
- Identifier(0),
+ Identifier( 0 ),
SequenceNumber( 0 ),
TimeSent( microsec_clock::universal_time() ),
ReplyBuffer(),
memcpy(&Identifier, random_tag.data, sizeof(Identifier));
}
-BoostPinger::~BoostPinger()
+IcmpPinger::~IcmpPinger()
{
}
* @param destination_ip The address of the host to ping.
* @param done_handler Done handler will be called on successful ping or timeout
*/
-void BoostPinger::ping( const string &destination_ip, boost::function< void(bool) > ping_done_callback )
+void IcmpPinger::ping(
+ const string &destination_ip,
+ boost::function< void(bool) > ping_done_callback
+)
{
BOOST_ASSERT( !destination_ip.empty() );
start_receive();
}
-void BoostPinger::set_destination_endpoint( const string &destination_ip )
+void IcmpPinger::set_destination_endpoint( const string &destination_ip )
{
BOOST_ASSERT( !destination_ip.empty() );
DestinationEndpoint = icmp::endpoint( destination_address, port );
}
-void BoostPinger::start_send()
+void IcmpPinger::start_send()
{
++SequenceNumber;
send_echo_request( icmp_echo_request_packet );
}
-IcmpPacket BoostPinger::create_echo_request(
+IcmpPacket IcmpPinger::create_echo_request(
const uint16_t sequence_number
) const
{
return IcmpPacket( icmp_header, icmp_data );
}
-void BoostPinger::send_echo_request( const IcmpPacket &icmp_packet )
+void IcmpPinger::send_echo_request( const IcmpPacket &icmp_packet )
{
boost::asio::streambuf request_buffer;
ostream os( &request_buffer );
schedule_timeout_echo_reply();
}
-void BoostPinger::schedule_timeout_echo_reply()
+void IcmpPinger::schedule_timeout_echo_reply()
{
// Wait up to N seconds for a reply.
ReceivedReply = false;
TimeSent + seconds( EchoReplyTimeoutInSec )
);
IcmpPacketReceiveTimer.async_wait(
- boost::bind( &BoostPinger::handle_ping_done, this )
+ boost::bind( &IcmpPinger::handle_ping_done, this )
);
}
-void BoostPinger::start_receive()
+void IcmpPinger::start_receive()
{
// Discard any data already in the buffer.
ReplyBuffer.consume( ReplyBuffer.size() );
// Waiting for a reply. We prepare the buffer to receive up to 64KB.
Socket.async_receive(
ReplyBuffer.prepare( 65536 ),
- boost::bind( &BoostPinger::handle_receive_icmp_packet, this, _2 )
+ boost::bind( &IcmpPinger::handle_receive_icmp_packet, this, _2 )
);
}
*
* @return void
**/
-void BoostPinger::handle_ping_done()
+void IcmpPinger::handle_ping_done()
{
// Check ReceivedReply as the timer handler
// is also called by Timer.cancel();
* @param bytes_transferred Number of bytes transferred
* @return void
**/
-void BoostPinger::handle_receive_icmp_packet( const size_t &bytes_transferred )
+void IcmpPinger::handle_receive_icmp_packet( const size_t &bytes_transferred )
{
// The actual number of bytes received is committed to the buffer so that we
// can extract it using a std::istream object.
}
}
-void BoostPinger::print_echo_reply(
+void IcmpPinger::print_echo_reply(
const IcmpPacket &icmp_packet,
const size_t &bytes_transferred
) const
<< " time=" << elapsed_time << " ms" << endl;
}
-void BoostPinger::print_destination_unreachable(
+void IcmpPinger::print_destination_unreachable(
const IcmpPacket &icmp_packet
) const
{
<< " Destination Net Unreachable" << endl;
}
-void BoostPinger::set_ping_status( BoostPinger::PingStatus ping_status )
+void IcmpPinger::set_ping_status( IcmpPinger::PingStatus ping_status )
{
PingerStatus = ping_status;
}
*
* @return false if the bind failed.
*/
-bool BoostPinger::select_source_network_interface(
+bool IcmpPinger::select_source_network_interface(
const string &source_network_interface
)
{
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
-#ifndef BOOSTPINGER_H
-#define BOOSTPINGER_H
+#ifndef ICMPPINGER_H
+#define ICMPPINGER_H
#include <boost/asio.hpp>
#include <boost/function.hpp>
class IcmpPacket;
//-----------------------------------------------------------------------------
-// BoostPinger
+// IcmpPinger
//-----------------------------------------------------------------------------
/**
* @brief This class performs ping to host using Boost Asio.
* Scope: one object per host.
*/
-class BoostPinger
+class IcmpPinger
{
public:
- BoostPinger(
+ IcmpPinger(
boost::asio::io_service &io_service,
std::string source_network_interface,
const int echo_reply_timeout_in_sec
);
- virtual ~BoostPinger();
+ virtual ~IcmpPinger();
- void ping( const std::string &destination_ip, boost::function< void(bool) > ping_done_callback);
+ void ping(
+ const std::string &destination_ip,
+ boost::function< void(bool) > ping_done_callback
+ );
private:
enum PingStatus
const IcmpPacket &icmp_packet
) const;
- void set_ping_status( BoostPinger::PingStatus ping_status );
+ void set_ping_status( IcmpPinger::PingStatus ping_status );
uint16_t get_identifier() const;
/// the amount of time to wait for the reply
int EchoReplyTimeoutInSec;
/// the status of the pinger
- BoostPinger::PingStatus PingerStatus;
+ IcmpPinger::PingStatus PingerStatus;
/// Callback to notify when the ping is done (got reply/timeout)
boost::function< void(bool) > PingDoneCallback;
};
-#endif /* BOOSTPINGER_H */
+#endif /* ICMPPINGER_H */