/* The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING.GPL. As a special exception, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. */ #ifndef TCP_PINGER_H #define TCP_PINGER_H #include #include #include #include #include #include "host/networkinterface.hpp" #include "host/pinger.h" #include "host/pingstatus.h" #include "tcp/tcpsegment.h" //----------------------------------------------------------------------------- // TcpPinger //----------------------------------------------------------------------------- /** * @brief This class performs a TCP ping to host using Boost Asio. * Scope: one object per host. */ class TcpPinger : public Pinger { public: virtual ~TcpPinger(); virtual void ping( const boost::asio::ip::address &destination_ip, const uint16_t destination_port, boost::function ping_done_callback ); virtual void stop_pinging(); static PingerItem create( const IoServiceItem io_serv, const boost::asio::ip::tcp_raw_protocol::socket::protocol_type &protocol, const std::string &source_network_interface_name, const int rst_reply_timeout_in_sec ); private: TcpPinger( const IoServiceItem io_serv, const boost::asio::ip::tcp_raw_protocol::socket::protocol_type &protocol, const std::string &source_network_interface_name, const int rst_reply_timeout_in_sec ); boost::asio::ip::address get_source_address() const; boost::asio::ip::address get_destination_address() const; uint16_t get_source_port() const; uint16_t get_destination_port() const; void set_destination_endpoint( const boost::asio::ip::address &destination_ip, const uint16_t destination_port ); void start_send(); void send_ack_request( const TcpSegmentItem tcp_segment ); void schedule_timeout_rst_reply(); void handle_ping_done(); void start_receive(); void handle_receive_tcp_segment( const std::size_t &bytes_transferred ); void set_ping_status( PingStatus ping_status ); private: /// The destination host boost::asio::ip::tcp_raw_protocol::endpoint DestinationEndpoint; /// Network layer protocol used to ping, IPv4 or IPv6 boost::asio::ip::tcp_raw_protocol::socket::protocol_type Protocol; /// The socket object boost::asio::ip::tcp_raw_protocol::socket Socket; /// This object represents the network interface NetworkInterface NetInterface; /// The timer of TCP segment receive, triggers the timeout to avoid infinite /// wait boost::asio::deadline_timer TcpSegmentReceiveTimer; /// TCP packet identifier uint16_t Identifier; /// TCP packet sequence_number uint16_t SequenceNumber; /// The time when the last ICMP packet was sent boost::posix_time::ptime TimeSent; /// The buffer where the data received will be placed boost::asio::streambuf ReplyBuffer; /// A flag to indicate if we got a reply or not bool ReceivedReply; /// The amount of time to wait for the reply int RstReplyTimeoutInSec; /// The status of the pinger PingStatus PingerStatus; /// Callback to notify when the ping is done (got reply/timeout) boost::function< void(PingStatus,long) > PingDoneCallback; }; #endif // TCP_PINGER_H