From: Thomas Jarosch Date: Tue, 3 May 2011 09:56:41 +0000 (+0200) Subject: Create unique identifier for every ping X-Git-Tag: v1.0~35 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=559780894dd8693180a7c907f9654b3e2194605b;p=pingcheck Create unique identifier for every ping --- diff --git a/src/host/boostpinger.cpp b/src/host/boostpinger.cpp index 96c6578..c8af357 100644 --- a/src/host/boostpinger.cpp +++ b/src/host/boostpinger.cpp @@ -8,6 +8,9 @@ #include #include +#include +#include + #include #include "icmp/icmpchecksumcalculator.h" @@ -41,6 +44,7 @@ BoostPinger::BoostPinger( DestinationEndpoint(), Socket( io_serv, icmp::v4() ), IcmpPacketReceiveTimer( io_serv ), + Identifier(0), SequenceNumber( 0 ), TimeSent( microsec_clock::universal_time() ), ReplyBuffer(), @@ -55,6 +59,12 @@ BoostPinger::BoostPinger( "with the local interface." << ::strerror( errno ) << endl; } + // Create "unique" identifier + boost::uuids::random_generator random_gen; + boost::uuids::uuid random_tag = random_gen(); + + BOOST_ASSERT(sizeof(Identifier) <= random_tag.size()); + memcpy(&Identifier, random_tag.data, sizeof(Identifier)); } BoostPinger::~BoostPinger() @@ -132,13 +142,12 @@ IcmpPacket BoostPinger::create_echo_request( IcmpType type = IcmpType_EchoRequest; uint8_t code = 0; - uint16_t identifier = get_identifier(); IcmpChecksumCalculator calculator( icmp_data.begin(), icmp_data.end() ); uint16_t checksum = calculator.compute( - type, code, identifier, sequence_number + type, code, Identifier, sequence_number ); IcmpHeader icmp_header( - type, code, checksum, identifier, sequence_number + type, code, checksum, Identifier, sequence_number ); return IcmpPacket( icmp_header, icmp_data ); @@ -243,7 +252,7 @@ void BoostPinger::handle_receive_icmp_packet( const size_t &bytes_transferred ) // expected sequence number, and destination host address (receive just the // ICMP packets from the host we had ping). if ( icmp_packet.match( - IcmpType_EchoReply, get_identifier(), SequenceNumber, + IcmpType_EchoReply, Identifier, SequenceNumber, DestinationEndpoint.address() ) ) { @@ -260,7 +269,7 @@ void BoostPinger::handle_receive_icmp_packet( const size_t &bytes_transferred ) set_ping_status( PingStatus_SuccessReply ); } else if ( icmp_packet.match( - IcmpType_DestinationUnreachable, get_identifier(), SequenceNumber, + IcmpType_DestinationUnreachable, Identifier, SequenceNumber, DestinationEndpoint.address() ) ) { @@ -332,11 +341,6 @@ void BoostPinger::set_ping_status( BoostPinger::PingStatus ping_status ) PingerStatus = ping_status; } -uint16_t BoostPinger::get_identifier() const -{ - return static_cast ( ::getpid() ); -} - /** * Avoid the socket to drop to another network interface if the destination * is unreachable through the binded interface. Packets are sent only from diff --git a/src/host/boostpinger.h b/src/host/boostpinger.h index 42740cf..4beadc9 100644 --- a/src/host/boostpinger.h +++ b/src/host/boostpinger.h @@ -76,6 +76,8 @@ private: /// the timer of ICMP packet receive, triggers the timeout to avoid infinite /// wait boost::asio::deadline_timer IcmpPacketReceiveTimer; + /// ICMP packet identifier + uint16_t Identifier; /// ICMP packet sequence_number uint16_t SequenceNumber; /// the time when the last ICMP packet was sent