added a dump_option to IcmpPacketFactory creation function; better error message...
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 4 Mar 2015 16:28:20 +0000 (17:28 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 13 Mar 2015 13:00:36 +0000 (14:00 +0100)
src/icmp/icmppacketfactory.cpp
src/icmp/icmppacketfactory.h

index 9428dc1..6e9624d 100644 (file)
@@ -25,7 +25,8 @@
 #include "icmp/icmppacketfactory.h"
 
 // for dumping packets
-#include <stdlib.h>
+#include <cstring>
+#include <cstdlib>
 #include <ctime>
 #include <iostream>
 #include <sstream>
@@ -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;
index 556f012..f954ed3 100644 (file)
@@ -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,