From: Christian Herdtweck Date: Fri, 20 Feb 2015 17:36:50 +0000 (+0100) Subject: started copying icmp data before parsing it and dumping it into temp files if parsing... X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=3d5eac0b56ad6644ac3c50d322e9a37453974c7f;p=pingcheck started copying icmp data before parsing it and dumping it into temp files if parsing fails does compile but causes problems in data parsing --- diff --git a/src/icmp/icmppacketfactory.cpp b/src/icmp/icmppacketfactory.cpp index c9042bd..4b2bf62 100644 --- a/src/icmp/icmppacketfactory.cpp +++ b/src/icmp/icmppacketfactory.cpp @@ -20,8 +20,11 @@ #include "icmp/icmppacketfactory.h" +#include + #include +#include #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 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; }