Create unique identifier for every ping
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 3 May 2011 09:56:41 +0000 (11:56 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 3 May 2011 09:56:41 +0000 (11:56 +0200)
src/host/boostpinger.cpp
src/host/boostpinger.h

index 96c6578..c8af357 100644 (file)
@@ -8,6 +8,9 @@
 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/date_time/posix_time/posix_time_types.hpp>
 
+#include <boost/uuid/uuid.hpp>
+#include <boost/uuid/uuid_generators.hpp>
+
 #include <logfunc.hpp>
 
 #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<uint16_t> ( ::getpid() );
-}
-
 /**
  * Avoid the socket to drop to another network interface if the destination
  * is unreachable through the binded interface. Packets are sent only from
index 42740cf..4beadc9 100644 (file)
@@ -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