const int echo_reply_timeout_in_sec,
const IcmpPacketDistributorItem distributor
) :
+ PacketDistributor( distributor ),
DestinationEndpoint(),
Protocol( protocol ),
IcmpPacketReceiveTimer( *io_serv ),
ReplyReceived( false ),
EchoReplyTimeoutInSec( echo_reply_timeout_in_sec ),
PingerStatus( PingStatus_NotSent ),
- PingDoneCallback(),
- PacketDistributor( distributor )
+ PingDoneCallback()
{
// Create "unique" identifier
boost::uuids::random_generator random_gen;
<< ": fail sending ping data. " << ex.what() << endl;
}
+ ReplyReceived = false;
schedule_timeout_echo_reply();
return (bytes_sent > 0);
void IcmpPinger::schedule_timeout_echo_reply()
{
// Wait up to N seconds for a reply.
- ReplyReceived = false;
(void) IcmpPacketReceiveTimer.expires_at(
TimeSent + seconds( EchoReplyTimeoutInSec )
);
bool IcmpPinger::handle_receive_icmp_packet(const IcmpPacketItem icmp_packet,
const size_t bytes_transferred )
{
+ bool does_match = false;
+
if ( ReplyReceived )
+ {
// continue, might be an old packet
// or return false right away, do not want packet anyway...
- return false;
+ GlobalLogger.info()
+ << DestinationEndpoint.address().to_string()
+ << ": Not interested in packets since we already got a reply"
+ << endl;
+ return does_match;
+ }
// We can receive all ICMP packets received by the host, so we need to
// filter out only the echo replies that match our identifier,
// expected sequence number, and destination host address (receive just
// the ICMP packets from the host we had ping).
- bool does_match = false;
if ( icmp_packet->match_echo_reply(
Identifier, SequenceNumber,
DestinationEndpoint.address() ) )
set_ping_status( PingStatus_SuccessReply );
- IcmpPacketReceiveTimer.cancel(); //lint !e534
+ IcmpPacketReceiveTimer.cancel(); //lint !e534
}
else if ( icmp_packet->match_destination_unreachable(
Identifier, SequenceNumber,
set_ping_status( PingStatus_FailureDestinationUnreachable );
- IcmpPacketReceiveTimer.cancel(); //lint !e534
+ IcmpPacketReceiveTimer.cancel(); //lint !e534
}
- // Unknown ICMP reply, start another receive till timeout
else
{
GlobalLogger.info()
const std::string &network_interface,
const IoServiceItem io_serv ):
Protocol( protocol ),
+ Socket( new icmp::socket(*io_serv, protocol) ),
ReplyBuffer(),
PingerList()
{
- Socket = SocketItem( new icmp::socket(*io_serv, protocol) );
NetworkInterface<icmp::socket, boost::asio::ip::icmp>
NetInterface( network_interface, *Socket );
GlobalLogger.warning() << "Ignoring broken ICMP packet"
<< std::endl;
}
-
- // check which pinger wants this packet
- bool packet_matches = false;
- BOOST_FOREACH( IcmpPingerItem pinger, PingerList )
+ else
{
- packet_matches |= pinger->handle_receive_icmp_packet(icmp_packet,
- bytes_transferred);
- if (packet_matches)
- break;
+ // check which pinger wants this packet
+ bool packet_matches = false;
+ BOOST_FOREACH( IcmpPingerItem pinger, PingerList )
+ {
+ packet_matches |= pinger->handle_receive_icmp_packet(
+ icmp_packet, bytes_transferred);
+ if (packet_matches)
+ break;
+ }
+ if (!packet_matches)
+ GlobalLogger.warning() << "Packet did not match any pinger"
+ << std::endl;
}
- if (!packet_matches)
- GlobalLogger.warning() << "Packet did not match any pinger"
- << std::endl;
}
catch ( ... )
{