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;
}
+/**
+ * @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 )
// 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 )
}
if (packet)
+ {
GlobalLogger.notice() << "Succesfully created ICMP packet";
+ GlobalLogger.info() << packet->to_string();
+ }
else
{
GlobalLogger.notice() << "ICMP packet creation failed";
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() )
input_stream.unget();
} //eo: while (input_stream)
- return 0;
+ return (-1) * packet_count;
} // eo: function read_packets
//------------------------------------------------------------------------------
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)
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")
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
{
{
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;