2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 #include "host/pingerfactory.h"
27 #include <boost/asio/ip/tcp_raw_protocol.hpp>
28 #include <boost/system/system_error.hpp>
30 #include <logfunc.hpp>
32 #include "boost_assert_handler.h"
33 #include "icmp/icmppinger.h"
34 #include "tcp/tcppinger.h"
37 using boost::shared_ptr;
38 using boost::asio::ip::icmp;
39 using boost::asio::ip::tcp_raw_protocol;
40 using boost::system::system_error;
41 using I2n::Logger::GlobalLogger;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
48 * @brief Default constructor.
50 PingerFactory::PingerFactory()
57 PingerFactory::~PingerFactory()
62 * @brief Create a Pinger object suitable to the given protocol.
64 * @param protocol One of the available ping protocols.
65 * @param io_serv The one @c io_service object that controls async processing
66 * @param network_interface The network interface name from where the ping
67 * packet will be sent.
68 * @return a Pinger object to able to ping using the given protocol.
70 PingerItem PingerFactory::createPinger(
71 const PingProtocol protocol,
72 const IoServiceItem io_serv,
73 const string &network_interface,
74 const int ping_reply_timeout
77 BOOST_ASSERT( /*( PingProtocol_First <= protocol ) && */( protocol <= PingProtocol_Last ) );
78 BOOST_ASSERT( !network_interface.empty() );
82 if (protocol == PingProtocol_ICMP)
84 PingerItem new_pinger = IcmpPinger::create( io_serv, icmp::v4(),
89 else if (protocol == PingProtocol_ICMPv6)
91 PingerItem new_pinger = IcmpPinger::create( io_serv, icmp::v6(),
96 else if (protocol == PingProtocol_TCP)
98 PingerItem new_pinger = TcpPinger::create( io_serv, tcp_raw_protocol::v4(),
100 ping_reply_timeout );
103 else if (protocol == PingProtocol_TCP_IPv6)
105 PingerItem new_pinger = TcpPinger::create( io_serv, tcp_raw_protocol::v6(),
107 ping_reply_timeout );
112 BOOST_ASSERT( !"Try to create a pinger from an invalid protocol" ); //lint !e506
113 return PingerItem(); //lint !e527
116 catch ( const system_error &ex )
118 // Raw sockets are locked down by Unix operating systems to prevent
119 // malware and security holes, thus it requires root access to use it.
120 GlobalLogger.error() << "Pingcheck must run as root (" << ex.what() << ")" << endl;
121 exit( EXIT_FAILURE );
124 return PingerItem(); //lint !e527
128 * @brief Create a Pinger object suitable to the given list of protocols.
130 * @param protocol_list A list of the available ping protocols.
131 * @param io_serv The @c io_service object.
132 * @param network_interface The network interface name from where the ping
133 * packet will be sent.
134 * @param destination_address The remote address to ping.
135 * @param destination_port The remote port to ping.
137 * @return a Pinger object to able to ping using the given list of protocols.
139 PingRotateItem PingerFactory::createPinger(
140 const PingProtocolList &protocol_list,
141 const IoServiceItem io_serv,
142 const string &network_interface,
143 const string &destination_address,
144 const uint16_t destination_port,
145 const int resolved_ip_ttl_threshold,
146 const int ping_reply_timeout
149 BOOST_ASSERT( 0 < protocol_list.size() );
150 BOOST_ASSERT( !network_interface.empty() );
151 BOOST_ASSERT( !destination_address.empty() );
152 BOOST_ASSERT( 0 < destination_port );
154 return PingRotateItem( new PingRotate(
159 resolved_ip_ttl_threshold,