libtins  4.0
flow.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_FLOW_H
31 #define TINS_TCP_IP_FLOW_H
32 
33 #include <tins/config.h>
34 
35 #ifdef TINS_HAVE_TCPIP
36 
37 #include <array>
38 #include <functional>
39 #include <stdint.h>
40 #include <tins/macros.h>
41 #include <tins/tcp_ip/ack_tracker.h>
42 #include <tins/tcp_ip/data_tracker.h>
43 
44 namespace Tins {
45 
46 class PDU;
47 class TCP;
48 class IPv4Address;
49 class IPv6Address;
50 
51 namespace TCPIP {
52 
67 class TINS_API Flow {
68 public:
76  enum State {
77  UNKNOWN,
78  SYN_SENT,
79  ESTABLISHED,
80  FIN_SENT,
81  RST_SENT
82  };
83 
87  typedef DataTracker::payload_type payload_type;
88 
92  typedef DataTracker::buffered_payload_type buffered_payload_type;
93 
97  typedef std::function<void(Flow&)> data_available_callback_type;
98 
105  typedef std::function<void(Flow&,
106  uint32_t,
107  const payload_type&)> flow_packet_callback_type;
108 
116  Flow(const IPv4Address& dst_address, uint16_t dst_port,
117  uint32_t sequence_number);
118 
126  Flow(const IPv6Address& dst_address, uint16_t dst_port,
127  uint32_t sequence_number);
128 
137  void data_callback(const data_available_callback_type& callback);
138 
147  void out_of_order_callback(const flow_packet_callback_type& callback);
148 
163  void process_packet(PDU& pdu);
164 
181  void advance_sequence(uint32_t seq);
182 
186  bool is_v6() const;
187 
194  bool is_finished() const;
195 
204  bool packet_belongs(const PDU& packet) const;
205 
211  IPv4Address dst_addr_v4() const;
212 
218  IPv6Address dst_addr_v6() const;
219 
223  uint16_t dport() const;
224 
228  const payload_type& payload() const;
229 
233  payload_type& payload();
234 
238  State state() const;
239 
243  uint32_t sequence_number() const;
244 
248  const buffered_payload_type& buffered_payload() const;
249 
253  buffered_payload_type& buffered_payload();
254 
258  uint32_t total_buffered_bytes() const;
259 
265  void state(State new_state);
266 
273  void ignore_data_packets();
274 
280  int mss() const;
281 
285  bool sack_permitted() const;
286 
294  void enable_ack_tracking();
295 
299  bool ack_tracking_enabled() const;
300 
301  #ifdef TINS_HAVE_ACK_TRACKER
302 
305  const AckTracker& ack_tracker() const;
306 
310  AckTracker& ack_tracker();
311  #endif // TINS_HAVE_ACK_TRACKER
312 private:
313  // Compress all flags into just one struct using bitfields
314  struct flags {
315  flags() : is_v6(0), ignore_data_packets(0), sack_permitted(0), ack_tracking(0) {
316 
317  }
318 
319  uint32_t is_v6:1,
320  ignore_data_packets:1,
321  sack_permitted:1,
322  ack_tracking:1;
323  };
324 
325  void update_state(const TCP& tcp);
326  void initialize();
327 
328  DataTracker data_tracker_;
329  std::array<uint8_t, 16> dest_address_;
330  uint16_t dest_port_;
331  data_available_callback_type on_data_callback_;
332  flow_packet_callback_type on_out_of_order_callback_;
333  State state_;
334  int mss_;
335  flags flags_;
336  #ifdef TINS_HAVE_ACK_TRACKER
337  AckTracker ack_tracker_;
338  #endif // TINS_HAVE_ACK_TRACKER
339 };
340 
341 } // TCPIP
342 } // TINS
343 
344 #endif // TINS_HAVE_TCPIP
345 #endif // TINS_TCP_IP_FLOW_H
346 
The Tins namespace.
Definition: address_range.h:38