From 174abc69fa7030a52e15d5e96a8116607c22caf4 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Wed, 4 Mar 2015 17:28:20 +0100 Subject: [PATCH] added a dump_option to IcmpPacketFactory creation function; better error message if temp file creation fails --- src/icmp/icmppacketfactory.cpp | 23 +++++++++++++---------- src/icmp/icmppacketfactory.h | 3 ++- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/icmp/icmppacketfactory.cpp b/src/icmp/icmppacketfactory.cpp index 9428dc1..6e9624d 100644 --- a/src/icmp/icmppacketfactory.cpp +++ b/src/icmp/icmppacketfactory.cpp @@ -25,7 +25,8 @@ #include "icmp/icmppacketfactory.h" // for dumping packets -#include +#include +#include #include #include #include @@ -86,7 +87,9 @@ void dump_packet(const std::string &data) 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; } @@ -120,7 +123,7 @@ void dump_packet(const std::string &data) 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 @@ -128,20 +131,20 @@ void dump_packet(const std::string &data) // 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; @@ -162,7 +165,7 @@ IcmpPacketItem IcmpPacketFactory::create_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); @@ -190,8 +193,8 @@ IcmpPacketItem IcmpPacketFactory::create_icmp_packet( 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; diff --git a/src/icmp/icmppacketfactory.h b/src/icmp/icmppacketfactory.h index 556f012..f954ed3 100644 --- a/src/icmp/icmppacketfactory.h +++ b/src/icmp/icmppacketfactory.h @@ -49,7 +49,8 @@ class IcmpPacketFactory public: static IcmpPacketItem create_icmp_packet( const boost::asio::ip::icmp::socket::protocol_type &protocol, - std::istream &is + std::istream &is, + int dump_mode=1 ); static IcmpPacketItem create_icmp_packet_echo_request( const boost::asio::ip::icmp::socket::protocol_type &protocol, -- 1.7.1