#include "icmp/icmppacketfactory.h"
// for dumping packets
-#include <stdlib.h>
+#include <cstring>
+#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
int fd = mkstemps(secure_filename.get(), 5); // 5 = ".pcap".length
if (fd == -1)
{
- GlobalLogger.warning() << "Failed to create temp file!" << endl;
+ GlobalLogger.warning() << "Failed to create temp file "
+ << secure_filename.get() << ": " << strerror(errno) << "!" << endl;
+ // maybe create containing directory if errno == ENOENT?
return;
}
fclose(fp);
close(fd);
GlobalLogger.debug() << "Dumped a copy of the data into "
- << secure_filename << endl;
+ << secure_filename.get() << endl;
}
#pragma pack(pop) // restore old value
// IcmpPacketFactory
//-----------------------------------------------------------------------------
-bool dump_broken_packets = true;
-bool dump_all_packets = false;
-
/**
* @brief Creates an ICMP packet from the input stream @c std::istream.
*
* @param protocol The packet's network layer protocol, IPv4 or IPv6.
* @param is The input stream.
+ * @param dump_mode: 0 for no dumping of packet data, 1 for dump if packet
+ * creation failed and 2 for dumping always
*
* @return An ICMP Packet object.
*/
IcmpPacketItem IcmpPacketFactory::create_icmp_packet(
const icmp::socket::protocol_type &protocol,
- istream &is
+ istream &is,
+ int dump_mode
)
{
IcmpPacketItem icmp_packet;
stringbuf data_backup;
// read packet from stream, possibly copying data first
- if (dump_broken_packets | dump_all_packets)
+ if (dump_mode > 0)
{
// read all data into backup
ostream backup_filler(&data_backup);
icmp_packet.reset(); // --> (!icmp_packet) is true
}
- // dump data if had trouble with packet
- if ( (dump_broken_packets && !icmp_packet) | dump_all_packets )
+ // dump data if had trouble with packet or dumping is set to always
+ if ( dump_mode == 2 || ( dump_mode==1 && !icmp_packet ) )
dump_packet(data_backup.str());
return icmp_packet;