libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
stream.h
1 /*
2  * Copyright (c) 2016, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_TCP_IP_STREAM_H
31 #define TINS_TCP_IP_STREAM_H
32 
33 #include "../cxxstd.h"
34 
35 // This classes use C++11 features
36 #if TINS_IS_CXX11
37 
38 #include <vector>
39 #include <array>
40 #include <map>
41 #include <functional>
42 #include <chrono>
43 #include <stdint.h>
44 #include "../macros.h"
45 #include "../hw_address.h"
46 #include "flow.h"
47 
48 namespace Tins {
49 
50 class PDU;
51 class TCP;
52 class IPv4Address;
53 class IPv6Address;
54 
55 namespace TCPIP {
56 
70 class TINS_API Stream {
71 public:
76 
80  typedef std::chrono::microseconds timestamp_type;
81 
85  typedef std::function<void(Stream&)> stream_callback_type;
86 
92  typedef std::function<void(Stream&,
93  uint32_t,
95 
100 
101 
108  Stream(PDU& initial_packet, const timestamp_type& ts = timestamp_type());
109 
119  void process_packet(PDU& packet, const timestamp_type& ts);
120 
129  void process_packet(PDU& packet);
130 
134  Flow& client_flow();
135 
139  const Flow& client_flow() const;
140 
144  Flow& server_flow();
145 
149  const Flow& server_flow() const;
150 
157  bool is_finished() const;
158 
162  bool is_v6() const;
163 
169  IPv4Address client_addr_v4() const;
170 
176  IPv6Address client_addr_v6() const;
177 
186  const hwaddress_type& client_hw_addr() const;
187 
196  const hwaddress_type& server_hw_addr() const;
197 
203  IPv4Address server_addr_v4() const;
204 
210  IPv6Address server_addr_v6() const;
211 
215  uint16_t client_port() const;
216 
220  uint16_t server_port() const;
221 
225  const payload_type& client_payload() const;
226 
230  payload_type& client_payload();
231 
235  const payload_type& server_payload() const;
236 
240  payload_type& server_payload();
241 
245  const timestamp_type& create_time() const;
246 
250  const timestamp_type& last_seen() const;
251 
257  void stream_closed_callback(const stream_callback_type& callback);
258 
265  void client_data_callback(const stream_callback_type& callback);
266 
273  void server_data_callback(const stream_callback_type& callback);
274 
282  void client_out_of_order_callback(const stream_packet_callback_type& callback);
283 
291  void server_out_of_order_callback(const stream_packet_callback_type& callback);
292 
299  void ignore_client_data();
300 
307  void ignore_server_data();
308 
315  void setup_flows_callbacks();
316 
336  void auto_cleanup_payloads(bool value);
337 
344  void auto_cleanup_client_data(bool value);
345 
352  void auto_cleanup_server_data(bool value);
353 
359  void enable_ack_tracking();
360 
364  bool ack_tracking_enabled() const;
365 private:
366  static Flow extract_client_flow(const PDU& packet);
367  static Flow extract_server_flow(const PDU& packet);
368 
369  void on_client_flow_data(const Flow& flow);
370  void on_server_flow_data(const Flow& flow);
371  void on_client_out_of_order(const Flow& flow,
372  uint32_t seq,
373  const payload_type& payload);
374  void on_server_out_of_order(const Flow& flow,
375  uint32_t seq,
376  const payload_type& payload);
377 
378  Flow client_flow_;
379  Flow server_flow_;
380  stream_callback_type on_stream_closed_;
381  stream_callback_type on_client_data_callback_;
382  stream_callback_type on_server_data_callback_;
383  stream_packet_callback_type on_client_out_of_order_callback_;
384  stream_packet_callback_type on_server_out_of_order_callback_;
385  hwaddress_type client_hw_addr_;
386  hwaddress_type server_hw_addr_;
387  timestamp_type create_time_;
388  timestamp_type last_seen_;
389  bool auto_cleanup_client_;
390  bool auto_cleanup_server_;
391 };
392 
393 } // TCPIP
394 } // Tins
395 
396 #endif // TINS_IS_CXX11
397 
398 #endif // TINS_TCP_IP_STREAM_H
HWAddress< 6 > hwaddress_type
Definition: stream.h:99
Represents a TCP stream.
Definition: stream.h:70
Flow::payload_type payload_type
Definition: stream.h:75
std::function< void(Stream &, uint32_t, const payload_type &)> stream_packet_callback_type
Definition: stream.h:94
std::function< void(Stream &)> stream_callback_type
Definition: stream.h:85
std::chrono::microseconds timestamp_type
Definition: stream.h:80
Abstraction of an IPv4 address.
Definition: ip_address.h:44
std::vector< uint8_t > payload_type
Definition: flow.h:90
Definition: ipv6_address.h:44
Base class for protocol data units.
Definition: pdu.h:108
Represents an unidirectional TCP flow between 2 endpoints.
Definition: flow.h:70