{
// 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";
// 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;
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);
}
// 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)
void dump_packet(const std::string &data);
+enum DumpMode {
+ DUMP_NEVER = 0,
+ DUMP_IF_ERROR = 1,
+ DUMP_ALWAYS = 2
+};
+
//-----------------------------------------------------------------------------
// IcmpPacketFactory
//-----------------------------------------------------------------------------
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,
{
// 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;
}
* @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);
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)
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)
{
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)
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
{
}
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();
}
}
#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);
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?