05e9586bb3baac2be25cc4f426411263b3bcd7db
[pingcheck] / src / tcp / tcppinger.h
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
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.
13
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.
16
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.
19 */
20 #ifndef TCP_PINGER_H
21 #define TCP_PINGER_H
22
23 #include <stdint.h>
24
25 #include <string>
26
27 #include <boost/asio.hpp>
28 #include <boost/asio/ip/tcp_raw_protocol.hpp>
29 #include <boost/function.hpp>
30
31 #include "host/networkinterface.hpp"
32 #include "host/pinger.h"
33 #include "host/pingstatus.h"
34 #include "tcp/tcpsegment.h"
35
36 //-----------------------------------------------------------------------------
37 // TcpPinger
38 //-----------------------------------------------------------------------------
39
40 /**
41  * @brief This class performs a TCP ping to host using Boost Asio.
42  * Scope: one object per host.
43  */
44 class TcpPinger : public Pinger
45 {
46 public:
47     virtual ~TcpPinger();
48
49     virtual void ping(
50             const boost::asio::ip::address &destination_ip,
51             const uint16_t destination_port,
52             boost::function<void(PingStatus)> ping_done_callback
53     );
54
55     virtual void stop_pinging();
56
57     static PingerItem create(
58             const IoServiceItem io_serv,
59             const boost::asio::ip::tcp_raw_protocol::socket::protocol_type &protocol,
60             const std::string &source_network_interface_name,
61             const int rst_reply_timeout_in_sec
62     );
63
64 private:
65     TcpPinger(
66             const IoServiceItem io_serv,
67             const boost::asio::ip::tcp_raw_protocol::socket::protocol_type &protocol,
68             const std::string &source_network_interface_name,
69             const int rst_reply_timeout_in_sec
70     );
71
72     boost::asio::ip::address get_source_address() const;
73     boost::asio::ip::address get_destination_address() const;
74
75     uint16_t get_source_port() const;
76     uint16_t get_destination_port() const;
77
78     void set_destination_endpoint(
79             const boost::asio::ip::address &destination_ip,
80             const uint16_t destination_port
81     );
82
83     void start_send();
84     void send_ack_request( const TcpSegmentItem tcp_segment );
85     void schedule_timeout_rst_reply();
86     void handle_ping_done();
87
88     void start_receive();
89     void handle_receive_tcp_segment( const std::size_t &bytes_transferred );
90
91     void set_ping_status( PingStatus ping_status );
92
93 private:
94     /// The destination host
95     boost::asio::ip::tcp_raw_protocol::endpoint DestinationEndpoint;
96     /// Network layer protocol used to ping, IPv4 or IPv6
97     boost::asio::ip::tcp_raw_protocol::socket::protocol_type Protocol;
98     /// The socket object
99     boost::asio::ip::tcp_raw_protocol::socket Socket;
100     /// This object represents the network interface
101     NetworkInterface<boost::asio::ip::tcp_raw_protocol::socket, boost::asio::ip::tcp_raw_protocol> NetInterface;
102     /// The timer of TCP segment receive, triggers the timeout to avoid infinite
103     /// wait
104     boost::asio::deadline_timer TcpSegmentReceiveTimer;
105     /// TCP packet identifier
106     uint16_t Identifier;
107     /// TCP packet sequence_number
108     uint16_t SequenceNumber;
109     /// The time when the last ICMP packet was sent
110     boost::posix_time::ptime TimeSent;
111     /// The buffer where the data received will be placed
112     boost::asio::streambuf ReplyBuffer;
113     /// A flag to indicate if we got a reply or not
114     bool ReceivedReply;
115     /// The amount of time to wait for the reply
116     int RstReplyTimeoutInSec;
117     /// The status of the pinger
118     PingStatus PingerStatus;
119     /// Callback to notify when the ping is done (got reply/timeout)
120     boost::function< void(PingStatus) > PingDoneCallback;
121 };
122
123 #endif // TCP_PINGER_H