bool ConfigurationReader::is_generic_options( const variables_map &vm ) const
{
- bool is_help = vm.count( HelpCmdStr );
- bool is_version = vm.count( VersionCmdStr );
+ bool is_help = ( vm.count( HelpCmdStr ) > 0 );
+ bool is_version = ( vm.count( VersionCmdStr ) > 0 );
bool is_a_generic_option = is_help || is_version;
return is_a_generic_option;
}
// [host] name
- int hosts_names_count = 0;
+ size_t hosts_names_count = 0;
if ( vm.count( HostNameCmdStr ) )
{
vector<HostItem> hosts_list;
hosts_names_count = hosts_names.size();
- BOOST_ASSERT( hosts_names_count >= host_down_limit );
+ BOOST_ASSERT( hosts_names_count >= static_cast<size_t>( host_down_limit ) );
}
// [host] interval
- int hosts_interval_count = 0;
+ size_t hosts_interval_count = 0;
if ( vm.count( HostIntervalCmdStr ) )
{
vector<HostItem> hosts_list = Config.get_hosts();
visible.add( generic ).add( config );
positional_options_description p;
- p.add( HostNameCmdStr.c_str(), -1 );
- //p.add( HostIntervalCmdStr.c_str(), -1 );
+ (void) p.add( HostNameCmdStr.c_str(), -1 );
parsed_options parsed_opt = command_line_parser( argc, argv ).
options( cmdline_options ).positional( p ).run();
if ( is_generic_options( vm ) )
{
- parse_generic_options( vm, visible );
+ (void) parse_generic_options( vm, visible );
return false;
}
ifstream ifs( config_file_name.c_str() );
if ( !ifs )
{
- cerr << "Error: could not open " << config_file_name << " file\n"
+ cerr << "Error: could not open " << config_file_name << " file."
<< endl;
return false;
}
{
BOOST_ASSERT( !HostDnsAddress.empty() );
- cerr << "Resolved IP(s) for host : " << HostDnsAddress << endl;
+ cout << "Resolved IP(s) for host : " << HostDnsAddress << endl;
try
{
resolved_host.set_ip( dest_ip );
ResolvedHostAddressList.push_back( resolved_host );
- cerr << "- " << dest_ip << endl;
+ cout << "- " << dest_ip << endl;
++it_first;
}
*/
int DnsResolver::get_resolved_ip_count() const
{
- int resolved_ip_count = ResolvedHostAddressList.size();
+ size_t resolved_ip_count = ResolvedHostAddressList.size();
BOOST_ASSERT( 1 <= resolved_ip_count );
*/
string DnsResolver::get_next_ip()
{
- int list_size_before = ResolvedHostAddressList.size();
+ size_t list_size_before = ResolvedHostAddressList.size();
HostAddress host_address = ResolvedHostAddressList.front();
ResolvedHostAddressList.pop_front();
string destination_ip = host_address.get_ip();
ResolvedHostAddressList.push_back( host_address );
- int list_size_after = ResolvedHostAddressList.size();
+ size_t list_size_after = ResolvedHostAddressList.size();
BOOST_ASSERT( list_size_before == list_size_after );
uint8_t code,
uint16_t identifier,
uint16_t sequence_number
- );
+ ) const;
private:
Iterator BodyBegin;
uint8_t code,
uint16_t identifier,
uint16_t sequence_number
- )
+ ) const
{
uint32_t sum = (type << 8) + code + identifier + sequence_number;
// IcmpChecksumCalculator
//-----------------------------------------------------------------------------
-typedef ChecksumCalculator<IcmpData::iterator> IcmpChecksumCalculator;
+typedef ChecksumCalculator<IcmpData::const_iterator> IcmpChecksumCalculator;
#endif /* ICMPCHECKSUMCALCULATOR_H */
#include "icmp/ipv4header.h"
+using namespace std;
+using namespace boost::asio::ip;
+
//-----------------------------------------------------------------------------
// Ipv4Header
//-----------------------------------------------------------------------------
Ipv4Header::Ipv4Header()
{
- std::fill( Rep, Rep + sizeof(Rep), 0 );
+ fill( Rep, Rep + sizeof(Rep), 0 );
}
uint8_t Ipv4Header::get_version() const
{
- return (Rep[ 0 ] >> 4) & 0xF;
+ return ( Rep[ 0 ] >> 4 ) & 0xF;
}
uint16_t Ipv4Header::get_header_length() const
{
- return (Rep[ 0 ] & 0xF) * 4;
+ return ( Rep[ 0 ] & 0xF ) * 4;
}
uint8_t Ipv4Header::get_type_of_service() const
return decode( 10, 11 );
}
-boost::asio::ip::address_v4 Ipv4Header::get_source_address() const
+address_v4 Ipv4Header::get_source_address() const
{
- boost::asio::ip::address_v4::bytes_type bytes = { {
+ address_v4::bytes_type bytes = { {
Rep[ 12 ],
Rep[ 13 ],
Rep[ 14 ],
Rep[ 15 ] } };
- return boost::asio::ip::address_v4( bytes );
+ return address_v4( bytes );
}
-boost::asio::ip::address_v4 Ipv4Header::get_destination_address() const
+address_v4 Ipv4Header::get_destination_address() const
{
- boost::asio::ip::address_v4::bytes_type bytes = { {
+ address_v4::bytes_type bytes = { {
Rep[ 16 ],
Rep[ 17 ],
Rep[ 18 ],
Rep[ 19 ] } };
- return boost::asio::ip::address_v4( bytes );
+ return address_v4( bytes );
}
-std::istream& operator>>(
- std::istream &is,
+istream &operator>>(
+ istream &is,
Ipv4Header &header
)
{
- is.read( reinterpret_cast<char*> ( header.Rep ), 20 );
+ (void) is.read( reinterpret_cast<char*> ( header.Rep ), 20 );
if ( header.get_version() != 4 )
- is.setstate( std::ios::failbit );
- std::streamsize options_length = header.get_header_length() - 20;
- if ( options_length < 0 || options_length > 40 )
- is.setstate( std::ios::failbit );
+ is.setstate( ios::failbit );
+ streamsize options_length = (streamsize) header.get_header_length() - 20;
+ if ( options_length < 0 || 40 < options_length )
+ {
+ is.setstate( ios::failbit );
+ }
else
- is.read( reinterpret_cast<char*> ( header.Rep ) + 20, options_length );
+ {
+ (void) is.read(
+ reinterpret_cast<char*> ( header.Rep ) + 20,
+ options_length
+ );
+ }
+
return is;
}
boost::asio::ip::address_v4 get_source_address() const;
boost::asio::ip::address_v4 get_destination_address() const;
- friend std::istream& operator>>(
+ friend std::istream &operator>>(
std::istream &is,
Ipv4Header &header
);
#include <vector>
+#include <iostream>
#include <boost/asio.hpp>
#include <boost/foreach.hpp>
// TODO ping_loop()
BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list )
{
- scheduler->start_pinging();
+ bool started = scheduler->start_pinging();
+ if ( !started )
+ {
+ cerr << "Error: could not start pinger." << endl;
+ }
}
// Main loop to handle scheduled ping requests
- io_serv.run();
+ (void) io_serv.run();
}
return 0;
{
if ( HostsDownList.count( host_address ) > 0 )
{
- HostsDownList.erase( host_address );
+ size_t erased_host_count = HostsDownList.erase( host_address );
+
+ BOOST_ASSERT( erased_host_count == 1 );
}
}
void LinkStatusAnalyzer::add_host_down( const string &host_address )
{
- HostsDownList.insert( host_address );
+ (void) HostsDownList.insert( host_address );
}
bool LinkStatusAnalyzer::exceeded_host_down_limit() const
BOOST_ASSERT( CurrentLinkStatus == LinkStatus_Up );
BOOST_ASSERT( CurrentNotificationStatus == NotificationStatus_NotReported );
- StatusNotifierCmd.set_token_value(
+ bool replaced = StatusNotifierCmd.set_token_value(
StatusNotifierCommand::StatusToken,
"up"
);
- StatusNotifierCmd.execute();
+ bool executed = StatusNotifierCmd.execute();
- CurrentNotificationStatus = NotificationStatus_Reported;
+ if ( replaced && executed )
+ {
+ CurrentNotificationStatus = NotificationStatus_Reported;
+ }
}
}
BOOST_ASSERT( CurrentLinkStatus == LinkStatus_Down );
BOOST_ASSERT( CurrentNotificationStatus == NotificationStatus_NotReported );
- StatusNotifierCmd.set_token_value(
+ bool replaced = StatusNotifierCmd.set_token_value(
StatusNotifierCommand::StatusToken,
"down"
);
- StatusNotifierCmd.execute();
+ bool executed = StatusNotifierCmd.execute();
- CurrentNotificationStatus = NotificationStatus_Reported;
+ if ( replaced && executed )
+ {
+ CurrentNotificationStatus = NotificationStatus_Reported;
+ }
}
}
}
const size_t token_size = token.length();
- CommandStr.replace( token_begin_pos, token_size, value );
+ (void) CommandStr.replace( token_begin_pos, token_size, value );
// assert the token is no longer within the command string
BOOST_ASSERT( CommandStr.find( token ) == string::npos );
start_send();
start_receive();
- IoService.run();
+ (void) IoService.run();
}
void BoostPinger::stop_pinger()
const uint16_t sequence_number
) const
{
- IcmpData icmp_data( "ping-message" );
+ const IcmpData icmp_data( "ping-message" );
IcmpHeader::IcmpType type = IcmpHeader::IcmpType_EchoRequest;
uint8_t code = 0;
// Send the request.
string dest_address_string = DestinationEndpoint.address().to_string();
BOOST_ASSERT( !dest_address_string.empty() );
- Socket.send_to( request_buffer.data(), DestinationEndpoint );
+
+ const_buffers_1 data = request_buffer.data();
+ size_t bytes_sent = Socket.send_to( data, DestinationEndpoint );
+ if ( bytes_sent != buffer_size( data ) )
+ {
+ cerr << "Error: fail sending ping data." << endl;
+ }
schedule_timeout_echo_reply();
}
{
// Wait up to N seconds for a reply.
RepliesCount = 0;
- Timer.expires_at( TimeSent + seconds( EchoReplyTimeoutInSec ) );
+ (void) Timer.expires_at( TimeSent + seconds( EchoReplyTimeoutInSec ) );
Timer.async_wait(
boost::bind( &BoostPinger::handle_timeout_echo_reply, this )
);
{
// Requests must be sent no less than one second apart.
const int echo_request_interval_in_sec = 1;
- Timer.expires_at( TimeSent + seconds( echo_request_interval_in_sec ) );
+ (void) Timer.expires_at( TimeSent + seconds( echo_request_interval_in_sec ) );
Timer.async_wait( boost::bind( &BoostPinger::start_send, this ) );
}
// If this is the first reply, interrupt the echo reply timeout.
if ( RepliesCount == 0 )
{
- Timer.cancel();
+ (void) Timer.cancel();
}
++RepliesCount;
HostStatusAnalyzer::HostStatusAnalyzer(
const string &host_address,
const int ping_fail_percentage_limit,
- shared_ptr<LinkStatusAnalyzer> link_analyzer
+ const shared_ptr<LinkStatusAnalyzer> link_analyzer
) :
HostAddress( host_address ),
LinkAnalyzer( link_analyzer ),
HostStatusAnalyzer(
const std::string &host_address,
const int ping_fail_percentage_limit,
- boost::shared_ptr<LinkStatusAnalyzer> link_analyzer
+ const boost::shared_ptr<LinkStatusAnalyzer> link_analyzer
);
virtual ~HostStatusAnalyzer();
private:
std::string HostAddress;
- boost::shared_ptr<LinkStatusAnalyzer> LinkAnalyzer;
+ const boost::shared_ptr<LinkStatusAnalyzer> LinkAnalyzer;
int PingFailPercentageLimit;
int ResolvedIpCount;
int PingsPerformedCount;
schedule_next_ping();
}
-bool PingScheduler::ping( const string &destination )
+bool PingScheduler::ping( const string &destination_ip ) const
{
- BOOST_ASSERT( !destination.empty() );
+ BOOST_ASSERT( !destination_ip.empty() );
- io_service io_service;
+ io_service io_serv;
int echo_reply_timeout_in_sec = 5; // TODO configurable: this is the timeout to WAIT FOR the ping before considering a timeout
- BoostPinger pinger( io_service, echo_reply_timeout_in_sec );
+ BoostPinger pinger( io_serv, echo_reply_timeout_in_sec );
- return pinger.ping( destination );
+ return pinger.ping( destination_ip );
}
void PingScheduler::schedule_next_ping()
{
BOOST_ASSERT( 0 < PingIntervalInSec );
- Timer.expires_from_now( seconds( PingIntervalInSec ) );
+ (void) Timer.expires_from_now( seconds( PingIntervalInSec ) );
Timer.async_wait( bind( &PingScheduler::handle_next_ping, this ) );
}
private:
bool resolve_ping_address();
void setup_ping();
- bool ping( const std::string &destination );
+ bool ping( const std::string &destination_ip ) const;
void schedule_next_ping();
void handle_next_ping();
void update_ping_statistics( const bool ping_success );