libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
stream.h
1 /*
2  * Copyright (c) 2017, 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 <tins/config.h>
34 
35 #ifdef TINS_HAVE_TCPIP
36 
37 #include <vector>
38 #include <array>
39 #include <map>
40 #include <functional>
41 #include <chrono>
42 #include <stdint.h>
43 #include <tins/macros.h>
44 #include <tins/hw_address.h>
45 #include <tins/config.h>
46 #include <tins/tcp_ip/flow.h>
47 #ifdef TINS_HAVE_TCP_STREAM_CUSTOM_DATA
48  #include <boost/any.hpp>
49 #endif
50 
51 namespace Tins {
52 
53 class PDU;
54 class TCP;
55 class IPv4Address;
56 class IPv6Address;
57 
58 namespace TCPIP {
59 
73 class TINS_API Stream {
74 public:
78  typedef Flow::payload_type payload_type;
79 
83  typedef std::chrono::microseconds timestamp_type;
84 
88  typedef std::function<void(Stream&)> stream_callback_type;
89 
98  typedef std::function<void(Stream&,
99  uint32_t,
100  const payload_type&)> stream_packet_callback_type;
101 
105  typedef HWAddress<6> hwaddress_type;
106 
107 
114  Stream(PDU& initial_packet, const timestamp_type& ts = timestamp_type());
115 
125  void process_packet(PDU& packet, const timestamp_type& ts);
126 
135  void process_packet(PDU& packet);
136 
140  Flow& client_flow();
141 
145  const Flow& client_flow() const;
146 
150  Flow& server_flow();
151 
155  const Flow& server_flow() const;
156 
163  bool is_finished() const;
164 
168  bool is_v6() const;
169 
175  IPv4Address client_addr_v4() const;
176 
182  IPv6Address client_addr_v6() const;
183 
192  const hwaddress_type& client_hw_addr() const;
193 
202  const hwaddress_type& server_hw_addr() const;
203 
209  IPv4Address server_addr_v4() const;
210 
216  IPv6Address server_addr_v6() const;
217 
221  uint16_t client_port() const;
222 
226  uint16_t server_port() const;
227 
231  const payload_type& client_payload() const;
232 
236  payload_type& client_payload();
237 
241  const payload_type& server_payload() const;
242 
246  payload_type& server_payload();
247 
251  const timestamp_type& create_time() const;
252 
256  const timestamp_type& last_seen() const;
257 
263  void stream_closed_callback(const stream_callback_type& callback);
264 
271  void client_data_callback(const stream_callback_type& callback);
272 
279  void server_data_callback(const stream_callback_type& callback);
280 
288  void client_out_of_order_callback(const stream_packet_callback_type& callback);
289 
297  void server_out_of_order_callback(const stream_packet_callback_type& callback);
298 
305  void ignore_client_data();
306 
313  void ignore_server_data();
314 
321  void setup_flows_callbacks();
322 
342  void auto_cleanup_payloads(bool value);
343 
350  void auto_cleanup_client_data(bool value);
351 
358  void auto_cleanup_server_data(bool value);
359 
365  void enable_ack_tracking();
366 
370  bool ack_tracking_enabled() const;
371 
372  #ifdef TINS_HAVE_TCP_STREAM_CUSTOM_DATA
373 
384  template<typename T>
385  T& user_data() {
386  if (user_data_.empty()) {
387  user_data_ = T();
388  };
389  return boost::any_cast<T&>(user_data_);
390  }
391  #endif // TINS_HAVE_TCP_STREAM_CUSTOM_DATA
392 
396  bool is_partial_stream() const;
397 
417  void enable_recovery_mode(uint32_t recovery_window);
418 
425  bool is_recovery_mode_enabled() const;
426 private:
427  static Flow extract_client_flow(const PDU& packet);
428  static Flow extract_server_flow(const PDU& packet);
429 
430  void on_client_flow_data(const Flow& flow);
431  void on_server_flow_data(const Flow& flow);
432  void on_client_out_of_order(const Flow& flow,
433  uint32_t seq,
434  const payload_type& payload);
435  void on_server_out_of_order(const Flow& flow,
436  uint32_t seq,
437  const payload_type& payload);
438  static void client_recovery_mode_handler(Stream& stream, uint32_t sequence_number,
439  const payload_type& payload,
440  uint32_t recovery_sequence_number_end,
441  const stream_packet_callback_type& original_callback);
442  static void server_recovery_mode_handler(Stream& stream, uint32_t sequence_number,
443  const payload_type& payload,
444  uint32_t recovery_sequence_number_end,
445  const stream_packet_callback_type& original_callback);
446  static bool recovery_mode_handler(Flow& flow, uint32_t sequence_number,
447  uint32_t recovery_sequence_number_end);
448 
449  Flow client_flow_;
450  Flow server_flow_;
451  stream_callback_type on_stream_closed_;
452  stream_callback_type on_client_data_callback_;
453  stream_callback_type on_server_data_callback_;
454  stream_packet_callback_type on_client_out_of_order_callback_;
455  stream_packet_callback_type on_server_out_of_order_callback_;
456  hwaddress_type client_hw_addr_;
457  hwaddress_type server_hw_addr_;
458  timestamp_type create_time_;
459  timestamp_type last_seen_;
460  bool auto_cleanup_client_;
461  bool auto_cleanup_server_;
462  bool is_partial_stream_;
463  unsigned directions_recovery_mode_enabled_;
464 
465  #ifdef TINS_HAVE_TCP_STREAM_CUSTOM_DATA
466  boost::any user_data_;
467  #endif // TINS_HAVE_TCP_STREAM_CUSTOM_DATA
468 };
469 
470 } // TCPIP
471 } // Tins
472 
473 #endif // TINS_HAVE_TCPIP
474 
475 #endif // TINS_TCP_IP_STREAM_H