libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
flow.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_FLOW_H
31 #define TINS_TCP_IP_FLOW_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 <stdint.h>
43 #include "../macros.h"
44 #include "ack_tracker.h"
45 #include "../hw_address.h"
46 
47 namespace Tins {
48 
49 class PDU;
50 class TCP;
51 class IPv4Address;
52 class IPv6Address;
53 
54 namespace TCPIP {
55 
70 class TINS_API Flow {
71 public:
79  enum State {
80  UNKNOWN,
81  SYN_SENT,
82  ESTABLISHED,
83  FIN_SENT,
84  RST_SENT
85  };
86 
90  typedef std::vector<uint8_t> payload_type;
91 
95  typedef std::map<uint32_t, payload_type> buffered_payload_type;
96 
100  typedef std::function<void(Flow&)> data_available_callback_type;
101 
108  typedef std::function<void(Flow&,
109  uint32_t,
111 
119  Flow(const IPv4Address& dst_address, uint16_t dst_port,
120  uint32_t sequence_number);
121 
129  Flow(const IPv6Address& dst_address, uint16_t dst_port,
130  uint32_t sequence_number);
131 
140  void data_callback(const data_available_callback_type& callback);
141 
150  void out_of_order_callback(const flow_packet_callback_type& callback);
151 
166  void process_packet(PDU& pdu);
167 
171  bool is_v6() const;
172 
179  bool is_finished() const;
180 
189  bool packet_belongs(const PDU& packet) const;
190 
196  IPv4Address dst_addr_v4() const;
197 
203  IPv6Address dst_addr_v6() const;
204 
208  uint16_t dport() const;
209 
213  const payload_type& payload() const;
214 
218  payload_type& payload();
219 
223  State state() const;
224 
228  uint32_t sequence_number() const;
229 
233  const buffered_payload_type& buffered_payload() const;
234 
238  buffered_payload_type& buffered_payload();
239 
243  uint32_t total_buffered_bytes() const;
244 
250  void state(State new_state);
251 
258  void ignore_data_packets();
259 
265  int mss() const;
266 
270  bool sack_permitted() const;
271 
279  void enable_ack_tracking();
280 
284  bool ack_tracking_enabled() const;
285 
286  #ifdef TINS_HAVE_ACK_TRACKER
287 
290  const AckTracker& ack_tracker() const;
291 
295  AckTracker& ack_tracker();
296  #endif // TINS_HAVE_ACK_TRACKER
297 private:
298  // Compress all flags into just one struct using bitfields
299  struct flags {
300  flags() : is_v6(0), ignore_data_packets(0), sack_permitted(0), ack_tracking(0) {
301 
302  }
303 
304  uint32_t is_v6:1,
305  ignore_data_packets:1,
306  sack_permitted:1,
307  ack_tracking:1;
308  };
309 
310  void store_payload(uint32_t seq, payload_type payload);
311  buffered_payload_type::iterator erase_iterator(buffered_payload_type::iterator iter);
312  void update_state(const TCP& tcp);
313  void initialize();
314 
315  payload_type payload_;
316  buffered_payload_type buffered_payload_;
317  uint32_t seq_number_;
318  uint32_t total_buffered_bytes_;
319  std::array<uint8_t, 16> dest_address_;
320  uint16_t dest_port_;
321  data_available_callback_type on_data_callback_;
322  flow_packet_callback_type on_out_of_order_callback_;
323  State state_;
324  int mss_;
325  flags flags_;
326  #ifdef TINS_HAVE_ACK_TRACKER
327  AckTracker ack_tracker_;
328  #endif // TINS_HAVE_ACK_TRACKER
329 };
330 
331 } // TCPIP
332 } // TINS
333 
334 #endif // TINS_IS_CXX11
335 #endif // TINS_TCP_IP_FLOW_H
336 
Represents a TCP PDU.
Definition: tcp.h:79
std::function< void(Flow &, uint32_t, const payload_type &)> flow_packet_callback_type
The type used to store the callback called when data is buffered.
Definition: flow.h:110
std::function< void(Flow &)> data_available_callback_type
Definition: flow.h:100
State
Enum that indicates the state of this flow.
Definition: flow.h:79
std::map< uint32_t, payload_type > buffered_payload_type
Definition: flow.h:95
Allows tracking acknowledged intervals in a TCP stream.
Definition: ack_tracker.h:93
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