#include "host/pingerfactory.h"
+#include <cstdlib>
+
+#include <iostream>
+
#include <boost/asio/ip/tcp_raw_protocol.hpp>
+#include <boost/system/system_error.hpp>
#include "icmp/icmppinger.h"
#include "tcp/tcppinger.h"
using boost::asio::io_service;
using boost::asio::ip::icmp;
using boost::asio::ip::tcp_raw_protocol;
+using boost::system::system_error;
//-----------------------------------------------------------------------------
// PingerFactory
// Ping reply timeout. Could be made a configuration variable
const int ping_reply_timeout_in_sec = 30; // TODO
- switch ( protocol )
+ try
{
- case PingProtocol_ICMP:
- return shared_ptr<Pinger>(
- new IcmpPinger( io_serv, icmp::v4(), network_interface, ping_reply_timeout_in_sec )
- );
- case PingProtocol_ICMPv6:
- return shared_ptr<Pinger>(
- new IcmpPinger( io_serv, icmp::v6(), network_interface, ping_reply_timeout_in_sec )
- );
- case PingProtocol_TCP:
- return shared_ptr<Pinger>(
- new TcpPinger( io_serv, tcp_raw_protocol::v4(), network_interface, ping_reply_timeout_in_sec )
- );
- case PingProtocol_TCP_IPv6:
- return shared_ptr<Pinger>(
- new TcpPinger( io_serv, tcp_raw_protocol::v6(), network_interface, ping_reply_timeout_in_sec )
- );
- default:
- BOOST_ASSERT( !"Try to create a pinger from an invalid protocol" );
- return shared_ptr<Pinger>();
+ switch ( protocol )
+ {
+ case PingProtocol_ICMP:
+ return shared_ptr<Pinger>(
+ new IcmpPinger( io_serv, icmp::v4(), network_interface, ping_reply_timeout_in_sec )
+ );
+ case PingProtocol_ICMPv6:
+ return shared_ptr<Pinger>(
+ new IcmpPinger( io_serv, icmp::v6(), network_interface, ping_reply_timeout_in_sec )
+ );
+ case PingProtocol_TCP:
+ return shared_ptr<Pinger>(
+ new TcpPinger( io_serv, tcp_raw_protocol::v4(), network_interface, ping_reply_timeout_in_sec )
+ );
+ case PingProtocol_TCP_IPv6:
+ return shared_ptr<Pinger>(
+ new TcpPinger( io_serv, tcp_raw_protocol::v6(), network_interface, ping_reply_timeout_in_sec )
+ );
+ default:
+ BOOST_ASSERT( !"Try to create a pinger from an invalid protocol" );
+ return shared_ptr<Pinger>();
+ }
}
+ catch ( const system_error &ex )
+ {
+ // Raw sockets are locked down by Unix operating systems to prevent
+ // malware and security holes, thus it requires root access to use it.
+ cerr << "Error: pingcheck must run as root (" << ex.what() << ")" << endl;
+ exit( EXIT_FAILURE );
+ }
+
+ return shared_ptr<Pinger>();
}