check if file is really open after opening; changed return codes and log levels
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 5 Mar 2015 10:15:38 +0000 (11:15 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 13 Mar 2015 13:00:36 +0000 (14:00 +0100)
src/tools/feed_packet_data.cpp

index 9c2d2aa..eb75725 100644 (file)
@@ -64,14 +64,13 @@ void init_logger()
 {
     // set default: log at level NOTICE to stderr
     I2n::Logger::enable_stderr_log( true );
-    //I2n::Logger::set_log_level( I2n::Logger::LogLevel::Notice );
-    I2n::Logger::set_log_level( I2n::Logger::LogLevel::Debug );
+    I2n::Logger::set_log_level( I2n::Logger::LogLevel::Info );
     GlobalLogger.info() << "Logger initiated";
 }
 
 void increase_verbosity()
 {
-    I2n::Logger::set_log_level( I2n::Logger::LogLevel::Info );
+    I2n::Logger::set_log_level( I2n::Logger::LogLevel::Debug );
     GlobalLogger.info() << "Increased verbosity";
 }
 
@@ -132,7 +131,7 @@ int read_packets( std::istream &input_stream, const bool is_icmp,
     if ( !input_stream )
     {
         GlobalLogger.notice() << "Failure consuming pcap file header!";
-        return 2;
+        return 3;
     }
 
     // read from stream until it is empty
@@ -145,7 +144,7 @@ int read_packets( std::istream &input_stream, const bool is_icmp,
             if ( !input_stream )
             {
                 GlobalLogger.notice() <<"Failure consuming pcap packet header!";
-                return 3;
+                return 4;
             }
         }
 
@@ -173,7 +172,7 @@ int read_packets( std::istream &input_stream, const bool is_icmp,
             else
             {
                 GlobalLogger.notice() << "ICMP packet creation failed";
-                return 4;
+                return 5;
             }
         }
         else   // is tcp
@@ -201,14 +200,14 @@ int read_packets( std::istream &input_stream, const bool is_icmp,
             else
             {
                 GlobalLogger.notice() << "TCP segment creation failed";
-                return 4;
+                return 5;
             }
         }
 
         if ( !input_stream )
         {
             GlobalLogger.notice() << "Stream not good after reading!";
-            return 5;
+            return 6;
         }
 
         // peek 1 byte to check whether we are at the end of stream
@@ -254,6 +253,7 @@ int main(int argc, char *argv[])
     BOOST_FOREACH (const std::string &arg, args)
     {
         GlobalLogger.debug() << "Parsing next arg: " << arg;
+        temp_val = 0;
 
         // check if is some specification of data format
         if (arg == "-i" || arg == "-icmp" || arg == "i" || arg == "icmp")
@@ -277,8 +277,17 @@ int main(int argc, char *argv[])
                                 << std::endl;
             std::ifstream file_stream(arg.c_str(), std::ios::in |
                                                    std::ios::binary);
-            temp_val = read_packets( file_stream, is_icmp, is_v4, dump_mode);
-            file_stream.close();
+            if ( !file_stream )
+            {
+                GlobalLogger.notice() << "Failed to open file " << arg
+                    << " for reading!";
+                temp_val = 2;
+            }
+            else
+            {
+                temp_val = read_packets( file_stream, is_icmp, is_v4, dump_mode);
+                file_stream.close();
+            }
         }
 
         if (temp_val != 0)