From: Christian Herdtweck Date: Wed, 25 Feb 2015 12:54:08 +0000 (+0100) Subject: dumping package data works X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=7238e8769018b31be241716f21f3d890febdf2a3;p=pingcheck dumping package data works --- diff --git a/src/icmp/icmppacketfactory.cpp b/src/icmp/icmppacketfactory.cpp index 4b2bf62..9ebd2cf 100644 --- a/src/icmp/icmppacketfactory.cpp +++ b/src/icmp/icmppacketfactory.cpp @@ -21,10 +21,11 @@ #include "icmp/icmppacketfactory.h" #include +#include +#include #include -#include #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 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;