Commit | Line | Data |
---|---|---|
cd395966 GMF |
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 | */ | |
f5c0f0d0 GMF |
20 | #ifndef TCP_PINGER_H |
21 | #define TCP_PINGER_H | |
cd395966 | 22 | |
238da857 GMF |
23 | #include <stdint.h> |
24 | ||
cd395966 GMF |
25 | #include <string> |
26 | ||
84525f34 | 27 | #include <boost/asio.hpp> |
f8a8d9dc | 28 | #include <boost/asio/ip/tcp_raw_protocol.hpp> |
cd395966 GMF |
29 | #include <boost/function.hpp> |
30 | ||
3b668576 | 31 | #include "host/networkinterface.hpp" |
cd395966 | 32 | #include "host/pinger.h" |
f5c0f0d0 | 33 | #include "host/pingstatus.h" |
3596e4f4 | 34 | #include "tcp/tcpsegment.h" |
cd395966 GMF |
35 | |
36 | //----------------------------------------------------------------------------- | |
37 | // TcpPinger | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
f8a8d9dc GMF |
40 | /** |
41 | * @brief This class performs a TCP ping to host using Boost Asio. | |
42 | * Scope: one object per host. | |
43 | */ | |
cd395966 GMF |
44 | class TcpPinger : public Pinger |
45 | { | |
46 | public: | |
cd395966 GMF |
47 | virtual ~TcpPinger(); |
48 | ||
9a34f743 | 49 | virtual void ping( |
23f51766 | 50 | const boost::asio::ip::address &destination_ip, |
238da857 | 51 | const uint16_t destination_port, |
9c0dcf33 | 52 | boost::function<void(PingStatus,long)> ping_done_callback |
cd395966 GMF |
53 | ); |
54 | ||
5a9bc2d1 CH |
55 | virtual void stop_pinging(); |
56 | ||
4606d2b1 CH |
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 | ||
f8a8d9dc | 64 | private: |
4606d2b1 CH |
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 | ||
6fd0993e | 72 | boost::asio::ip::address get_source_address() const; |
865e078a | 73 | boost::asio::ip::address get_destination_address() const; |
1309d0e4 | 74 | |
6f6db388 | 75 | uint16_t get_source_port() const; |
1309d0e4 GMF |
76 | uint16_t get_destination_port() const; |
77 | ||
78 | void set_destination_endpoint( | |
23f51766 | 79 | const boost::asio::ip::address &destination_ip, |
238da857 | 80 | const uint16_t destination_port |
1309d0e4 | 81 | ); |
f8a8d9dc GMF |
82 | |
83 | void start_send(); | |
3596e4f4 | 84 | void send_ack_request( const TcpSegmentItem tcp_segment ); |
81adfb7b | 85 | void schedule_timeout_rst_reply(); |
bd66be8f | 86 | void handle_ping_done(); |
f8a8d9dc GMF |
87 | |
88 | void start_receive(); | |
89 | void handle_receive_tcp_segment( const std::size_t &bytes_transferred ); | |
90 | ||
f5c0f0d0 | 91 | void set_ping_status( PingStatus ping_status ); |
f8a8d9dc | 92 | |
f8a8d9dc | 93 | private: |
e58d7507 | 94 | /// The destination host |
f8a8d9dc | 95 | boost::asio::ip::tcp_raw_protocol::endpoint DestinationEndpoint; |
3b6a0314 GMF |
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 | |
f8a8d9dc | 99 | boost::asio::ip::tcp_raw_protocol::socket Socket; |
3b668576 | 100 | /// This object represents the network interface |
d14043de | 101 | NetworkInterface<boost::asio::ip::tcp_raw_protocol::socket, boost::asio::ip::tcp_raw_protocol> NetInterface; |
3b6a0314 | 102 | /// The timer of TCP segment receive, triggers the timeout to avoid infinite |
f8a8d9dc GMF |
103 | /// wait |
104 | boost::asio::deadline_timer TcpSegmentReceiveTimer; | |
6f6db388 | 105 | /// TCP packet identifier |
f8a8d9dc | 106 | uint16_t Identifier; |
6f6db388 | 107 | /// TCP packet sequence_number |
f8a8d9dc | 108 | uint16_t SequenceNumber; |
3b6a0314 | 109 | /// The time when the last ICMP packet was sent |
f8a8d9dc | 110 | boost::posix_time::ptime TimeSent; |
3b6a0314 | 111 | /// The buffer where the data received will be placed |
f8a8d9dc | 112 | boost::asio::streambuf ReplyBuffer; |
3b6a0314 | 113 | /// A flag to indicate if we got a reply or not |
f8a8d9dc | 114 | bool ReceivedReply; |
e58d7507 | 115 | /// The amount of time to wait for the reply |
c2a393ee | 116 | int RstReplyTimeoutInSec; |
e58d7507 | 117 | /// The status of the pinger |
f5c0f0d0 | 118 | PingStatus PingerStatus; |
f8a8d9dc | 119 | /// Callback to notify when the ping is done (got reply/timeout) |
9c0dcf33 | 120 | boost::function< void(PingStatus,long) > PingDoneCallback; |
cd395966 GMF |
121 | }; |
122 | ||
f5c0f0d0 | 123 | #endif // TCP_PINGER_H |