started copying icmp data before parsing it and dumping it into temp files if parsing...
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 20 Feb 2015 17:36:50 +0000 (18:36 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 20 Feb 2015 17:36:50 +0000 (18:36 +0100)
does compile but causes problems in data parsing

src/icmp/icmppacketfactory.cpp

index c9042bd..4b2bf62 100644 (file)
 
 #include "icmp/icmppacketfactory.h"
 
+#include <stdlib.h>
+
 #include <logfunc.hpp>
 
+#include <boost/scoped_array.hpp>
 #include "boost_assert_handler.h"
 #include "icmp/icmpchecksum.h"
 #include "icmp/icmpdata.h"
@@ -39,6 +42,8 @@ using I2n::Logger::GlobalLogger;
 // IcmpPacketFactory
 //-----------------------------------------------------------------------------
 
+bool dump_broken_packets = true;
+
 /**
  * @brief Creates an ICMP packet from the input stream @c std::istream.
  *
@@ -70,7 +75,27 @@ IcmpPacketItem IcmpPacketFactory::create_icmp_packet(
         return icmp_packet;
     }
 
-    IcmpPacket::ReadReturnCode return_code = icmp_packet->read( is );
+    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 a copy to read from again
+        std::stringbuf strbuf(all_data.get(), std::ios::in);
+        istream is_2(&strbuf);
+        return_code = icmp_packet->read( is_2 );
+    }
+    else
+        return_code = icmp_packet->read( is );
+
     if ( return_code != IcmpPacket::ReadReturnCode_OK )
     {
         GlobalLogger.warning() << "ICMP packet creation failed: "
@@ -85,6 +110,21 @@ IcmpPacketItem IcmpPacketFactory::create_icmp_packet(
         icmp_packet.reset();    // --> (!icmp_packet) is true
     }
 
+    if (dump_broken_packets && !icmp_packet)
+    {
+        char secure_filename[] = "/datastore/pingcheck.broken/icmp_XXXXXX";
+        int fd = mkstemp(secure_filename);
+        if (fd == -1)
+            GlobalLogger.warning() << "Failed to create temp file!" << endl;
+        else
+        {
+            write(fd, all_data.get(), length);
+            close(fd);
+            GlobalLogger.debug() << "Dumped a copy of the data into "
+                                 << secure_filename << endl;
+        }
+    }
+
     return icmp_packet;
 }