//-----------------------------------------------------------------------------
Configuration::Configuration() :
- config_file_name( "" ),
- limit_to_notify( 0 ),
- host( "" ),
- MIN_LIMIT_TO_NOTIFY( 0 ),
- MAX_LIMIT_TO_NOTIFY( 50 )
+ ConfigFileName( "" ),
+ LimitToNotify( 0 ),
+ Host_( "" ),
+ MinLimitToNotify( 0 ),
+ MaxLimitToNotify( 50 )
{
}
string Configuration::get_config_file_name() const
{
- return config_file_name;
+ return ConfigFileName;
}
void Configuration::set_config_file_name( const std::string& config_file_name )
{
BOOST_ASSERT( !config_file_name.empty() );
- this->config_file_name = config_file_name;
+ this->ConfigFileName = config_file_name;
}
uint32_t Configuration::get_limit_to_notify() const
{
- return limit_to_notify;
+ return LimitToNotify;
}
void Configuration::set_limit_to_notify( const uint32_t limit_to_notify )
{
- BOOST_ASSERT( ( MIN_LIMIT_TO_NOTIFY <= limit_to_notify ) && ( limit_to_notify <= MAX_LIMIT_TO_NOTIFY) );
+ BOOST_ASSERT( ( MinLimitToNotify <= limit_to_notify ) && ( limit_to_notify <= MaxLimitToNotify) );
- this->limit_to_notify = limit_to_notify;
+ this->LimitToNotify = limit_to_notify;
}
Host Configuration::get_host() const
{
- return host;
+ return Host_;
}
void Configuration::set_host( const Host& host )
{
- this->host = host;
+ this->Host_ = host;
}
void set_host( const Host& host );
private:
- std::string config_file_name;
- uint32_t limit_to_notify;
- Host host;
+ std::string ConfigFileName;
+ uint32_t LimitToNotify;
+ Host Host_;
- const uint32_t MIN_LIMIT_TO_NOTIFY;
- const uint32_t MAX_LIMIT_TO_NOTIFY;
+ const uint32_t MinLimitToNotify;
+ const uint32_t MaxLimitToNotify;
};
//-----------------------------------------------------------------------------
ConfigurationReader::ConfigurationReader() :
- configuration(),
- VERSION_CMD_STR( "version" ),
- VERSION_CMD_DESC( "Print the version string and exit." ),
- HELP_CMD_STR( "help" ),
- HELP_CMD_DESC( "Print this help and exit." ),
- DEFAULT_CONFIG_FILE_NAME( "pingcheck.cfg" ),
- CONFIG_FILE_CMD_STR( "config-file" ),
- CONFIG_FILE_CMD_DESC( "Name of the configuration file." ),
- DEFAULT_LIMIT_TO_NOTIFY( 4 ),
- LIMIT_TO_NOTIFY_CMD_STR( "limit-to-notify" ),
- LIMIT_TO_NOTIFY_CMD_DESC( "Limit of host that have to be down in order to notify." ),
- HOST_NAME_CMD_STR( "host.name" ),
- HOST_NAME_CMD_DESC( "Host address" )
+ Config(),
+ VersionCmdStr( "version" ),
+ VersionCmdDesc( "Print the version string and exit." ),
+ HelpCmdStr( "help" ),
+ HelpCmdDesc( "Print this help and exit." ),
+ DefaultConfigFileName( "pingcheck.cfg" ),
+ ConfigFileCmdStr( "config-file" ),
+ ConfigFileCmdDesc( "Name of the configuration file." ),
+ DefaultLimitToNotify( 4 ),
+ LimitToNotifyCmdStr( "limit-to-notify" ),
+ LimitToNotifyCmdDesc( "Limit of host that have to be down in order to notify." ),
+ HostNameCmdStr( "host.name" ),
+ HostNameCmdDesc( "Host address" )
{
}
{
options_description options( "Generic options" );
options.add_options()
- ( VERSION_CMD_STR.c_str(), VERSION_CMD_DESC.c_str() )
- ( HELP_CMD_STR.c_str(), HELP_CMD_DESC.c_str() )
- ( CONFIG_FILE_CMD_STR.c_str(), value<string>()->default_value( DEFAULT_CONFIG_FILE_NAME ), CONFIG_FILE_CMD_DESC.c_str() );
+ ( VersionCmdStr.c_str(), VersionCmdDesc.c_str() )
+ ( HelpCmdStr.c_str(), HelpCmdDesc.c_str() )
+ ( ConfigFileCmdStr.c_str(), value<string>()->default_value( DefaultConfigFileName ), ConfigFileCmdDesc.c_str() );
return options;
}
{
options_description options( "Configuration" );
options.add_options()
- ( LIMIT_TO_NOTIFY_CMD_STR.c_str(), value<int>()->default_value( DEFAULT_LIMIT_TO_NOTIFY ), LIMIT_TO_NOTIFY_CMD_DESC.c_str() )
- ( HOST_NAME_CMD_STR.c_str(), value< string >(), HOST_NAME_CMD_DESC.c_str() );
+ ( LimitToNotifyCmdStr.c_str(), value<int>()->default_value( DefaultLimitToNotify ), LimitToNotifyCmdDesc.c_str() )
+ ( HostNameCmdStr.c_str(), value< string >(), HostNameCmdDesc.c_str() );
return options;
}
visible.add( generic ).add( config );
positional_options_description p;
- p.add( HOST_NAME_CMD_STR.c_str(), -1 );
+ p.add( HostNameCmdStr.c_str(), -1 );
store( command_line_parser( argc, argv ).
options( cmdline_options ).
positional( p ).run(), vm );
notify( vm );
- if ( vm.count( HELP_CMD_STR ) )
+ if ( vm.count( HelpCmdStr ) )
{
cout << visible << endl;
return false;
}
- if ( vm.count( VERSION_CMD_STR ) )
+ if ( vm.count( VersionCmdStr ) )
{
cout << PROJECT_NAME << " version " << VERSION_STRING << endl;
return false;
{
string config_file_name = "";
if ( fill_configuration( vm ) )
- config_file_name = configuration.get_config_file_name();
+ config_file_name = Config.get_config_file_name();
return parse_configuration_file( config_file_name, vm );
}
bool ConfigurationReader::fill_configuration( const variables_map& vm )
{
- if ( vm.count( CONFIG_FILE_CMD_STR ) )
+ if ( vm.count( ConfigFileCmdStr ) )
{
- string config_file_name = vm[ CONFIG_FILE_CMD_STR ].as<string> ();
- configuration.set_config_file_name( config_file_name );
+ string config_file_name = vm[ ConfigFileCmdStr ].as<string> ();
+ Config.set_config_file_name( config_file_name );
- cout << CONFIG_FILE_CMD_STR << "=" << config_file_name << endl;
+ cout << ConfigFileCmdStr << "=" << config_file_name << endl;
}
- if ( vm.count( LIMIT_TO_NOTIFY_CMD_STR ) )
+ if ( vm.count( LimitToNotifyCmdStr ) )
{
- uint32_t limit_to_notify = vm[ LIMIT_TO_NOTIFY_CMD_STR ].as<int> ();
- configuration.set_limit_to_notify( limit_to_notify );
+ uint32_t limit_to_notify = vm[ LimitToNotifyCmdStr ].as<int> ();
+ Config.set_limit_to_notify( limit_to_notify );
- cout << LIMIT_TO_NOTIFY_CMD_STR << "=" << limit_to_notify << endl;
+ cout << LimitToNotifyCmdStr << "=" << limit_to_notify << endl;
}
- if ( vm.count( HOST_NAME_CMD_STR ) )
+ if ( vm.count( HostNameCmdStr ) )
{
- string host_name = vm[ HOST_NAME_CMD_STR ].as<string> ();
- Host host = configuration.get_host();
+ string host_name = vm[ HostNameCmdStr ].as<string> ();
+ Host host = Config.get_host();
host.set_address( host_name );
- configuration.set_host( host );
+ Config.set_host( host );
- cout << HOST_NAME_CMD_STR << "=" << host_name << endl;
+ cout << HostNameCmdStr << "=" << host_name << endl;
}
return true;
Configuration ConfigurationReader::get_configuration() const
{
- return configuration;
+ return Config;
}
bool fill_configuration( const boost::program_options::variables_map& vm );
private:
- Configuration configuration;
-
- const std::string VERSION_CMD_STR;
- const std::string VERSION_CMD_DESC;
- const std::string HELP_CMD_STR;
- const std::string HELP_CMD_DESC;
- const std::string DEFAULT_CONFIG_FILE_NAME;
- const std::string CONFIG_FILE_CMD_STR;
- const std::string CONFIG_FILE_CMD_DESC;
- const uint32_t DEFAULT_LIMIT_TO_NOTIFY;
- const std::string LIMIT_TO_NOTIFY_CMD_STR;
- const std::string LIMIT_TO_NOTIFY_CMD_DESC;
- const std::string HOST_NAME_CMD_STR;
- const std::string HOST_NAME_CMD_DESC;
+ Configuration Config;
+
+ const std::string VersionCmdStr;
+ const std::string VersionCmdDesc;
+ const std::string HelpCmdStr;
+ const std::string HelpCmdDesc;
+ const std::string DefaultConfigFileName;
+ const std::string ConfigFileCmdStr;
+ const std::string ConfigFileCmdDesc;
+ const uint32_t DefaultLimitToNotify;
+ const std::string LimitToNotifyCmdStr;
+ const std::string LimitToNotifyCmdDesc;
+ const std::string HostNameCmdStr;
+ const std::string HostNameCmdDesc;
};
BoostPinger::BoostPinger(
boost::asio::io_service& io_service
) :
- resolver( io_service ),
- destination_endpoint(),
- socket( io_service, icmp::v4() ),
- timer( io_service ),
- sequence_number( 0 ),
- time_sent( posix_time::microsec_clock::universal_time() ),
- reply_buffer(),
- num_replies( 0 )
+ Resolver( io_service ),
+ DestinationEndpoint(),
+ Socket( io_service, icmp::v4() ),
+ Timer( io_service ),
+ SequenceNumber( 0 ),
+ TimeSent( posix_time::microsec_clock::universal_time() ),
+ ReplyBuffer(),
+ NumReplies( 0 )
{
#if 0
try
std::string ip = "172.16.1.149";
uint32_t port = 21;
icmp::endpoint ep( address::from_string( ip ), port );
- socket.bind( ep );
+ Socket.bind( ep );
}
catch (boost::system::system_error &e)
{
// TODO what about ping multiple hosts?
icmp::resolver::query query( icmp::v4(), destination, "" );
- destination_endpoint = *resolver.resolve( query );
+ DestinationEndpoint = *Resolver.resolve( query );
start_send();
start_receive();
echo_request.type( IcmpHeader::EchoRequest );
echo_request.code( 0 );
echo_request.identifier( get_identifier() );
- sequence_number++;
- echo_request.sequence_number( sequence_number );
+ SequenceNumber++;
+ echo_request.sequence_number( SequenceNumber );
compute_checksum( echo_request, body.begin(), body.end() );
// Encode the request packet.
os << echo_request << body;
// Send the request.
- time_sent = posix_time::microsec_clock::universal_time();
- socket.send_to( request_buffer.data(), destination_endpoint );
+ TimeSent = posix_time::microsec_clock::universal_time();
+ Socket.send_to( request_buffer.data(), DestinationEndpoint );
// Wait up to five seconds for a reply.
- num_replies = 0;
- timer.expires_at( time_sent + posix_time::seconds( 5 ) );
- timer.async_wait( boost::bind( &BoostPinger::handle_timeout, this ) );
+ NumReplies = 0;
+ Timer.expires_at( TimeSent + posix_time::seconds( 5 ) );
+ Timer.async_wait( boost::bind( &BoostPinger::handle_timeout, this ) );
}
void BoostPinger::handle_timeout()
{
- if ( num_replies == 0 )
+ if ( NumReplies == 0 )
std::cout << "Request timed out" << std::endl;
// Requests must be sent no less than one second apart.
- timer.expires_at( time_sent + posix_time::seconds( 1 ) );
- timer.async_wait( boost::bind( &BoostPinger::start_send, this ) );
+ Timer.expires_at( TimeSent + posix_time::seconds( 1 ) );
+ Timer.async_wait( boost::bind( &BoostPinger::start_send, this ) );
}
void BoostPinger::start_receive()
{
// Discard any data already in the buffer.
- reply_buffer.consume( reply_buffer.size() );
+ ReplyBuffer.consume( ReplyBuffer.size() );
// Wait for a reply. We prepare the buffer to receive up to 64KB.
- socket.async_receive(
- reply_buffer.prepare( 65536 ),
+ Socket.async_receive(
+ ReplyBuffer.prepare( 65536 ),
boost::bind( &BoostPinger::handle_receive, this, _2 )
);
}
{
// The actual number of bytes received is committed to the buffer so that we
// can extract it using a std::istream object.
- reply_buffer.commit( length );
+ ReplyBuffer.commit( length );
// Decode the reply packet.
- std::istream is( &reply_buffer );
+ std::istream is( &ReplyBuffer );
Ipv4Header ipv4_hdr;
IcmpHeader icmp_hdr;
is >> ipv4_hdr >> icmp_hdr;
// expected sequence number.
if ( is && icmp_hdr.type() == IcmpHeader::EchoReply
&& icmp_hdr.identifier() == get_identifier()
- && icmp_hdr.sequence_number() == sequence_number )
+ && icmp_hdr.sequence_number() == SequenceNumber )
{
// If this is the first reply, interrupt the five second timeout.
- if ( num_replies++ == 0 )
- timer.cancel();
+ if ( NumReplies++ == 0 )
+ Timer.cancel();
// Print out some information about the reply packet.
posix_time::ptime now = posix_time::microsec_clock::universal_time();
<< ipv4_hdr.source_address() << ": icmp_seq="
<< icmp_hdr.sequence_number() << ", ttl="
<< ipv4_hdr.time_to_live() << ", time="
- << (now - time_sent).total_milliseconds() << " ms" << std::endl;
+ << (now - TimeSent).total_milliseconds() << " ms" << std::endl;
}
start_receive();
static uint16_t get_identifier();
private:
- icmp::resolver resolver;
- icmp::endpoint destination_endpoint;
- icmp::socket socket;
- deadline_timer timer;
- uint16_t sequence_number;
- boost::posix_time::ptime time_sent;
- boost::asio::streambuf reply_buffer;
- std::size_t num_replies;
+ icmp::resolver Resolver;
+ icmp::endpoint DestinationEndpoint;
+ icmp::socket Socket;
+ deadline_timer Timer;
+ uint16_t SequenceNumber;
+ boost::posix_time::ptime TimeSent;
+ boost::asio::streambuf ReplyBuffer;
+ std::size_t NumReplies;
};
//-----------------------------------------------------------------------------
Host::Host( std::string address ) :
- address( address ),
- port( 0 ),
- interval( 0 ),
- options()
+ Address( address ),
+ Port( 0 ),
+ Interval( 0 ),
+ Options()
{
}
std::string Host::get_address() const
{
- return address;
+ return Address;
}
void Host::set_address( const std::string& address )
{
BOOST_ASSERT( !address.empty() );
- this->address = address;
+ this->Address = address;
}
uint16_t Host::get_port() const
{
- return port;
+ return Port;
}
void Host::set_port( const uint16_t port )
{
BOOST_ASSERT( ( 0 < port ) && ( port < USHRT_MAX ) );
- this->port = port;
+ this->Port = port;
}
uint32_t Host::get_interval() const
{
- return interval;
+ return Interval;
}
void Host::set_interval( const uint32_t interval )
{
BOOST_ASSERT( ( 0 < interval ) && ( interval < UINT_MAX ) );
- this->interval = interval;
+ this->Interval = interval;
}
std::vector<std::string> Host::get_options() const
{
- return options;
+ return Options;
}
void Host::set_options( const std::vector<std::string>& options )
{
- this->options = options;
+ this->Options = options;
}
void set_options( const std::vector<std::string>& options );
private:
- std::string address;
- uint16_t port;
- uint32_t interval;
- std::vector<std::string> options;
+ std::string Address;
+ uint16_t Port;
+ uint32_t Interval;
+ std::vector<std::string> Options;
};
IcmpHeader::IcmpHeader()
{
- std::fill( rep, rep + sizeof(rep), 0 );
+ std::fill( Rep, Rep + sizeof(Rep), 0 );
}
unsigned char IcmpHeader::type() const
{
- return rep[ 0 ];
+ return Rep[ 0 ];
}
unsigned char IcmpHeader::code() const
{
- return rep[ 1 ];
+ return Rep[ 1 ];
}
unsigned short IcmpHeader::checksum() const
void IcmpHeader::type( unsigned char n )
{
- rep[ 0 ] = n;
+ Rep[ 0 ] = n;
}
void IcmpHeader::code( unsigned char n )
{
- rep[ 1 ] = n;
+ Rep[ 1 ] = n;
}
void IcmpHeader::checksum( unsigned short n )
std::istream& operator>>( std::istream& is, IcmpHeader& header )
{
- return is.read( reinterpret_cast<char*> ( header.rep ), 8 );
+ return is.read( reinterpret_cast<char*> ( header.Rep ), 8 );
}
std::ostream& operator<<( std::ostream& os, const IcmpHeader& header )
{
- return os.write( reinterpret_cast<const char*> ( header.rep ), 8 );
+ return os.write( reinterpret_cast<const char*> ( header.Rep ), 8 );
}
unsigned short IcmpHeader::decode( int a, int b ) const
{
- return (rep[ a ] << 8) + rep[ b ];
+ return (Rep[ a ] << 8) + Rep[ b ];
}
void IcmpHeader::encode( int a, int b, unsigned short n )
{
- rep[ a ] = static_cast<unsigned char> ( n >> 8 );
- rep[ b ] = static_cast<unsigned char> ( n & 0xFF );
+ Rep[ a ] = static_cast<unsigned char> ( n >> 8 );
+ Rep[ b ] = static_cast<unsigned char> ( n & 0xFF );
}
unsigned short decode( int a, int b ) const;
void encode( int a, int b, unsigned short n );
- unsigned char rep[ 8 ];
+ unsigned char Rep[ 8 ];
};
+//-----------------------------------------------------------------------------
+// compute_checksum
+//-----------------------------------------------------------------------------
+
template<typename Iterator>
void compute_checksum(
IcmpHeader& header,
Ipv4Header::Ipv4Header()
{
- std::fill( rep, rep + sizeof(rep), 0 );
+ std::fill( Rep, Rep + sizeof(Rep), 0 );
}
unsigned char Ipv4Header::version() const
{
- return (rep[ 0 ] >> 4) & 0xF;
+ return (Rep[ 0 ] >> 4) & 0xF;
}
unsigned short Ipv4Header::header_length() const
{
- return (rep[ 0 ] & 0xF) * 4;
+ return (Rep[ 0 ] & 0xF) * 4;
}
unsigned char Ipv4Header::type_of_service() const
{
- return rep[ 1 ];
+ return Rep[ 1 ];
}
unsigned short Ipv4Header::total_length() const
bool Ipv4Header::dont_fragment() const
{
- return (rep[ 6 ] & 0x40) != 0;
+ return (Rep[ 6 ] & 0x40) != 0;
}
bool Ipv4Header::more_fragments() const
{
- return (rep[ 6 ] & 0x20) != 0;
+ return (Rep[ 6 ] & 0x20) != 0;
}
unsigned short Ipv4Header::fragment_offset() const
unsigned int Ipv4Header::time_to_live() const
{
- return rep[ 8 ];
+ return Rep[ 8 ];
}
unsigned char Ipv4Header::protocol() const
{
- return rep[ 9 ];
+ return Rep[ 9 ];
}
unsigned short Ipv4Header::header_checksum() const
boost::asio::ip::address_v4 Ipv4Header::source_address() const
{
boost::asio::ip::address_v4::bytes_type bytes = { {
- rep[ 12 ],
- rep[ 13 ],
- rep[ 14 ],
- rep[ 15 ] } };
+ Rep[ 12 ],
+ Rep[ 13 ],
+ Rep[ 14 ],
+ Rep[ 15 ] } };
return boost::asio::ip::address_v4( bytes );
}
boost::asio::ip::address_v4 Ipv4Header::destination_address() const
{
boost::asio::ip::address_v4::bytes_type bytes = { {
- rep[ 16 ],
- rep[ 17 ],
- rep[ 18 ],
- rep[ 19 ] } };
+ Rep[ 16 ],
+ Rep[ 17 ],
+ Rep[ 18 ],
+ Rep[ 19 ] } };
return boost::asio::ip::address_v4( bytes );
}
std::istream& operator>>( std::istream& is, Ipv4Header& header )
{
- is.read( reinterpret_cast<char*> ( header.rep ), 20 );
- if (header.version() != 4)
+ is.read( reinterpret_cast<char*> ( header.Rep ), 20 );
+ if ( header.version() != 4 )
is.setstate( std::ios::failbit );
std::streamsize options_length = header.header_length() - 20;
- if (options_length < 0 || options_length > 40)
+ if ( options_length < 0 || options_length > 40 )
is.setstate( std::ios::failbit );
else
- is.read( reinterpret_cast<char*> ( header.rep ) + 20, options_length );
+ is.read( reinterpret_cast<char*> ( header.Rep ) + 20, options_length );
return is;
}
unsigned short Ipv4Header::decode( int a, int b ) const
{
- return (rep[ a ] << 8) + rep[ b ];
+ return ((Rep[ a ] << 8) + Rep[ b ]);
}
private:
unsigned short decode( int a, int b ) const;
- unsigned char rep[ 60 ];
+ unsigned char Rep[ 60 ];
};
#endif // IPV4_HEADER_HPP