added a count of good/bad packets to feed_packet_data tool
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 13 Mar 2015 14:13:03 +0000 (15:13 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 13 Mar 2015 14:13:03 +0000 (15:13 +0100)
src/tools/feed_packet_data.cpp

index eb75725..e5a8c3a 100644 (file)
@@ -82,7 +82,7 @@ bool check_for_pcap_header(std::istream &input_stream)
     for (peek_idx=0; peek_idx<4; ++peek_idx)
     {
         int val = input_stream.get();
-        GlobalLogger.debug() << "Peeked value " << std::hex << val;
+        //GlobalLogger.debug() << "Peeked value " << std::hex << val;
         if (val != pcap_magic_bytes[3-peek_idx])
         {
             is_pcap = false;
@@ -119,6 +119,11 @@ void consume_pcap_packet_header(std::istream &input_stream)
 }
 
 
+/**
+ * @brief read packets from given input stream
+ *
+ * @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 )
@@ -126,6 +131,7 @@ int read_packets( std::istream &input_stream, const bool is_icmp,
     // peek at start of stream to see if there is a pcap header
     bool is_pcap = check_for_pcap_header(input_stream);
     int next_val;
+    int packet_count = 0;
     if (is_pcap)
         consume_pcap_file_header(input_stream);
     if ( !input_stream )
@@ -168,7 +174,10 @@ int read_packets( std::istream &input_stream, const bool is_icmp,
             }
 
             if (packet)
+            {
                 GlobalLogger.notice() << "Succesfully created ICMP packet";
+                GlobalLogger.info() << packet->to_string();
+            }
             else
             {
                 GlobalLogger.notice() << "ICMP packet creation failed";
@@ -210,6 +219,10 @@ int read_packets( std::istream &input_stream, const bool is_icmp,
             return 6;
         }
 
+        // if reached this point, created a packet, otherwise will have
+        // returned with error code
+        ++packet_count;
+
         // peek 1 byte to check whether we are at the end of stream
         next_val = input_stream.get();
         if ( input_stream.eof() )
@@ -220,7 +233,7 @@ int read_packets( std::istream &input_stream, const bool is_icmp,
         input_stream.unget();
     } //eo: while (input_stream)
 
-    return 0;
+    return (-1) * packet_count;
 } // eo: function read_packets
 
 //------------------------------------------------------------------------------
@@ -236,7 +249,9 @@ int main(int argc, char *argv[])
 
     bool is_icmp = default_is_icmp;
     bool is_v4 = default_is_v4;
-    int temp_val, return_val = 0;
+    int current_return, return_val = 0;
+    int packet_count_good = 0;
+    int packet_count_bad = 0;
     int dump_mode = 0;  // never dump
 
     if (argc == 1)
@@ -253,7 +268,7 @@ int main(int argc, char *argv[])
     BOOST_FOREACH (const std::string &arg, args)
     {
         GlobalLogger.debug() << "Parsing next arg: " << arg;
-        temp_val = 0;
+        current_return = 0;
 
         // check if is some specification of data format
         if (arg == "-i" || arg == "-icmp" || arg == "i" || arg == "icmp")
@@ -269,7 +284,7 @@ int main(int argc, char *argv[])
         else if (arg == "--")   // read input from stdin
         {
             GlobalLogger.info() << "Trying to read from stdin" << std::endl;
-            temp_val = read_packets( std::cin, is_icmp, is_v4, dump_mode );
+            current_return = read_packets(std::cin, is_icmp, is_v4, dump_mode);
         }
         else   // assume is file name
         {
@@ -281,22 +296,31 @@ int main(int argc, char *argv[])
             {
                 GlobalLogger.notice() << "Failed to open file " << arg
                     << " for reading!";
-                temp_val = 2;
+                current_return = 2;
             }
             else
             {
-                temp_val = read_packets( file_stream, is_icmp, is_v4, dump_mode);
+                current_return = read_packets( file_stream, is_icmp, is_v4,
+                                               dump_mode);
                 file_stream.close();
             }
         }
 
-        if (temp_val != 0)
+        if (current_return > 0)
         {
-            GlobalLogger.debug() << "Remember error value " << temp_val;
-            return_val = temp_val;
+            GlobalLogger.debug() << "Remember error value " << current_return;
+            return_val = current_return;
+            ++packet_count_bad;
         }
+        else // returned the number of packets created * (-1)
+            packet_count_good -= current_return;
     } //eo: loop over input-files
 
+    GlobalLogger.notice() << "Created (at least) " << packet_count_good
+        << " packets successfully and failed for " << packet_count_bad;
+    // ("at least" because if we get an error code from read_packets, then
+    //  there might have been successfull reads earlier in same stream)
+
     GlobalLogger.debug() << "End program with return value " << return_val;
     return return_val;