made nicer static variables of packet dump mode and location
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 4 May 2015 16:27:31 +0000 (18:27 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 4 May 2015 16:27:31 +0000 (18:27 +0200)
src/icmp/icmppacketfactory.cpp
src/icmp/icmppacketfactory.h
src/icmp/icmppinger.cpp
src/tools/feed_packet_data.cpp
test/test_icmppacket.cpp

index 03e14dc..0f01c67 100644 (file)
@@ -49,7 +49,7 @@ void dump_packet(const std::string &data)
 {
     // create unique file name
     std::stringstream temp_name;
-    temp_name << "/datastore/pingcheck.broken/icmp_";
+    temp_name << IcmpPacketFactory::DumpFilePrefix;
     time_t capture_time = time(0);
     temp_name << capture_time;
     temp_name << "_XXXXXX.pcap";
@@ -83,20 +83,21 @@ void dump_packet(const std::string &data)
 // IcmpPacketFactory
 //-----------------------------------------------------------------------------
 
+// set default value
+DumpMode    IcmpPacketFactory::PacketDumpMode = DUMP_IF_ERROR;
+std::string IcmpPacketFactory::DumpFilePrefix = "/tmp/icmp_";
+
 /**
  * @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,
-        int dump_mode
+        istream &is
 )
 {
     IcmpPacketItem icmp_packet;
@@ -121,7 +122,7 @@ IcmpPacketItem IcmpPacketFactory::create_icmp_packet(
     try
     {
         // read packet from stream, possibly copying data first
-        if (dump_mode > 0)
+        if (PacketDumpMode != DUMP_NEVER)
         {
             // read all data into backup
             ostream backup_filler(&data_backup);
@@ -163,13 +164,14 @@ IcmpPacketItem IcmpPacketFactory::create_icmp_packet(
     }
 
     // dump data if had trouble with packet or dumping is set to always
-    if ( dump_mode == 2 || ( dump_mode==1 && !icmp_packet ) )
+    if ( PacketDumpMode == DUMP_ALWAYS ||
+       ( PacketDumpMode == DUMP_IF_ERROR && !icmp_packet ) )
     {
         if ( have_backup )
             dump_packet(data_backup.str());
         else
             GlobalLogger.warning() << "Would like to dump packet but "
-                << "exception occured before backup was created!";
+                << "trouble occured before backup was created!";
     }
 
     if (icmp_packet)
index 3c481f7..384126b 100644 (file)
 
 void dump_packet(const std::string &data);
 
+enum DumpMode {
+    DUMP_NEVER = 0,
+    DUMP_IF_ERROR = 1,
+    DUMP_ALWAYS = 2
+};
+
 //-----------------------------------------------------------------------------
 // IcmpPacketFactory
 //-----------------------------------------------------------------------------
@@ -50,10 +56,14 @@ void dump_packet(const std::string &data);
 class IcmpPacketFactory
 {
 public:
+    /// directory and file name start used to dump packets;
+    /// will be concatenated with time and pattern that ensures unique file name
+    static std::string DumpFilePrefix;
+    static DumpMode PacketDumpMode;
+
     static IcmpPacketItem create_icmp_packet(
             const boost::asio::ip::icmp::socket::protocol_type &protocol,
-            std::istream &is,
-            int dump_mode=1
+            std::istream &is
     );
     static IcmpPacketItem create_icmp_packet_echo_request(
             const boost::asio::ip::icmp::socket::protocol_type &protocol,
index 788e44b..49a16d0 100644 (file)
@@ -293,18 +293,10 @@ bool IcmpPinger::handle_receive_icmp_packet(const IcmpPacketItem icmp_packet,
     {
         // continue, might be an old packet
         // or return false right away, do not want packet anyway...
-        GlobalLogger.debug()
-           << DestinationEndpoint.address().to_string()
-           << ": Not interested in packets since we already got a reply"
-           << endl;
         return does_match;
     }
     else if ( DestinationEndpoint.address() == address() )
     {   // we have no IP set yet
-        GlobalLogger.debug()
-           << DestinationEndpoint.address().to_string()
-           << ": Not interested in packets since have no Destination yet"
-           << endl;
         return does_match;
     }
 
index d11ab10..5a0733d 100644 (file)
@@ -67,8 +67,7 @@ void increase_verbosity()
  * @returns positive error code or negated number of packets created
  */
 int read_packets( std::istream &input_stream, const bool is_icmp,
-                                              const bool is_v4,
-                                              const int dump_mode )
+                                              const bool is_v4)
 {
     // peek at start of stream to see if there is a pcap header
     bool is_pcap = check_for_pcap_header(input_stream);
@@ -126,14 +125,14 @@ int read_packets( std::istream &input_stream, const bool is_icmp,
                 GlobalLogger.info() << "Trying to read ICMP v4 packet"
                                     << std::endl;
                 packet = IcmpPacketFactory::create_icmp_packet(
-                        boost::asio::ip::icmp::v4(), input_stream, dump_mode);
+                                     boost::asio::ip::icmp::v4(), input_stream);
             }
             else  // v6
             {
                 GlobalLogger.info() << "Trying to read ICMP v6 packet"
                                     << std::endl;
                 packet = IcmpPacketFactory::create_icmp_packet(
-                        boost::asio::ip::icmp::v6(), input_stream, dump_mode);
+                                     boost::asio::ip::icmp::v6(), input_stream);
             }
 
             if (packet)
@@ -235,7 +234,7 @@ int main(int argc, char *argv[])
     int current_return, return_val = 0;
     int packet_count_good = 0;
     int packet_count_bad = 0;
-    int dump_mode = 0;  // never dump
+    DumpMode dump_mode = DUMP_NEVER;
 
     if (argc == 1)
     {
@@ -246,6 +245,8 @@ int main(int argc, char *argv[])
         return 1;
     }
 
+    IcmpPacketFactory::PacketDumpMode = dump_mode;
+
     // convert arguments to vector of strings and loop over them
     std::vector<std::string> args(argv+1, argv + argc);
     BOOST_FOREACH (const std::string &arg, args)
@@ -267,7 +268,7 @@ int main(int argc, char *argv[])
         else if (arg == "--")   // read input from stdin
         {
             GlobalLogger.info() << "Trying to read from stdin" << std::endl;
-            current_return = read_packets(std::cin, is_icmp, is_v4, dump_mode);
+            current_return = read_packets(std::cin, is_icmp, is_v4);
         }
         else   // assume is file name
         {
@@ -283,8 +284,7 @@ int main(int argc, char *argv[])
             }
             else
             {
-                current_return = read_packets( file_stream, is_icmp, is_v4,
-                                               dump_mode);
+                current_return = read_packets( file_stream, is_icmp, is_v4);
                 file_stream.close();
             }
         }
index 692586e..6dd6f4d 100644 (file)
 #include "tools/pcap.h"
 
 //------------------------------------------------------------------------------
-//  helper function and consts
+//  helper function
 //------------------------------------------------------------------------------
 
-const int DUMP_MODE_NO_DUMP = 0;
-
 IcmpPacketItem read_packet(const std::string &file_name);
 
 
@@ -60,8 +58,10 @@ IcmpPacketItem read_packet(const std::string &file_name)
         BOOST_FAIL( "Failed to recognize/consume pcap header!" );
     }
 
+    IcmpPacketFactory::PacketDumpMode = DUMP_NEVER;
+
     IcmpPacketItem packet = IcmpPacketFactory::create_icmp_packet(
-            boost::asio::ip::icmp::v4(), file_stream, DUMP_MODE_NO_DUMP);
+                                      boost::asio::ip::icmp::v4(), file_stream);
     file_stream.close();
 
     // managed to create packet from rest of file contents?