dumping package data works
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 25 Feb 2015 12:54:08 +0000 (13:54 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 25 Feb 2015 12:54:08 +0000 (13:54 +0100)
src/icmp/icmppacketfactory.cpp

index 4b2bf62..9ebd2cf 100644 (file)
 #include "icmp/icmppacketfactory.h"
 
 #include <stdlib.h>
+#include <iostream>
+#include <sstream>
 
 #include <logfunc.hpp>
 
-#include <boost/scoped_array.hpp>
 #include "boost_assert_handler.h"
 #include "icmp/icmpchecksum.h"
 #include "icmp/icmpdata.h"
@@ -76,21 +77,24 @@ IcmpPacketItem IcmpPacketFactory::create_icmp_packet(
     }
 
     IcmpPacket::ReadReturnCode return_code;
-    is.seekg(0, is.end);
-    int length = is.tellg();
-    is.seekg(0, is.beg);
-    boost::scoped_array<char> all_data( new char[length] );
-    if (dump_broken_packets)
-    {
-        // read all data into a string
-        is.read(all_data.get(), length);
 
-        GlobalLogger.debug() << "Copied " << length
-            << " chars before reading" << endl;
+    // create buffer for saving data in it
+    stringbuf data_backup;
+    size_t data_size;
 
-        // create a copy to read from again
-        std::stringbuf strbuf(all_data.get(), std::ios::in);
-        istream is_2(&strbuf);
+    // read packet from stream, possibly copying data first
+    if (dump_broken_packets)
+    {
+        // read all data into backup
+        ostream backup_filler(&data_backup);
+        backup_filler << is.rdbuf();
+        data_size = data_backup.str().size();
+        GlobalLogger.debug() << "Copied ICMP raw data (" << data_size
+             << " bytes) for dumping in case of broken packet" << endl;
+
+        // create a new stream from backup buffer
+        //   and use that in read
+        istream is_2(&data_backup);
         return_code = icmp_packet->read( is_2 );
     }
     else
@@ -118,7 +122,9 @@ IcmpPacketItem IcmpPacketFactory::create_icmp_packet(
             GlobalLogger.warning() << "Failed to create temp file!" << endl;
         else
         {
-            write(fd, all_data.get(), length);
+            // write data
+            write(fd, data_backup.str().c_str(), data_size);
+            // to check od -t u1
             close(fd);
             GlobalLogger.debug() << "Dumped a copy of the data into "
                                  << secure_filename << endl;