#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"
}
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
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;