fixed possible reason for broken icmp packages: restricted read of icmp payload data...
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 9 Dec 2014 17:50:58 +0000 (18:50 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 9 Dec 2014 17:50:58 +0000 (18:50 +0100)
src/icmp/icmppinger.cpp
src/icmp/icmppinger.h
src/icmp/icmpv4packet.cpp

index 4734026..6309c09 100644 (file)
@@ -38,6 +38,9 @@ using I2n::Logger::GlobalLogger;
 // IcmpPinger
 //-----------------------------------------------------------------------------
 
+/// size of buffer used to read from socket in [bytes]
+static const std::size_t SOCKET_BUFFER_SIZE = 65536;   // 64kB
+
 /**
  * @brief Parameterized constructor.
  *
index 3387388..fc85a90 100644 (file)
 // IcmpPinger
 //-----------------------------------------------------------------------------
 
-
-/// size of buffer used to read from socket in [bytes]
-const std::size_t SOCKET_BUFFER_SIZE = 65536;   // 64kB
-
 /**
  * @brief This class performs an ICMP ping to host using Boost Asio.
  * Scope: one object per host.
index 05f1b50..02c06f2 100644 (file)
@@ -252,7 +252,25 @@ istream& operator>>(
     if (is.good())
         is >> packet.IcmpPayloadHeader;
     if (is.good())
-        is >> packet.IcmpPayloadData;
+    {
+        streamsize data_length = static_cast<streamsize>( packet.IpHeader.get_total_length() ) -
+                                 static_cast<streamsize>( packet.IpHeader.get_header_length() );
+
+        if ( data_length < 0 )
+        {
+            GlobalLogger.error() << "Error: invalid size for optional ICMP data: " << data_length << endl;
+            is.setstate( ios::failbit );
+        }
+        else if ( data_length > 0 )
+        {
+            size_t options_size = static_cast<size_t>( data_length );
+            scoped_array<uint8_t> scoped_data( new uint8_t[options_size] );
+            char *char_data = reinterpret_cast<char *>( scoped_data.get() );
+
+            (void) is.read( char_data, data_length );
+            packet.IcmpPayloadData = char_data;
+        }
+    }
 
     return is;
 }