From 0caefeeb0dfdb4427e35808af95693611ebde308 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Thu, 5 Mar 2015 11:15:38 +0100 Subject: [PATCH] check if file is really open after opening; changed return codes and log levels --- src/tools/feed_packet_data.cpp | 29 +++++++++++++++++++---------- 1 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/tools/feed_packet_data.cpp b/src/tools/feed_packet_data.cpp index 9c2d2aa..eb75725 100644 --- a/src/tools/feed_packet_data.cpp +++ b/src/tools/feed_packet_data.cpp @@ -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) -- 1.7.1