libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
stream_follower.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_FOLLOWER_H
31 #define TINS_TCP_IP_STREAM_FOLLOWER_H
32 
33 #include "../cxxstd.h"
34 
35 // This classes use C++11 features
36 #if TINS_IS_CXX11
37 
38 #include <map>
39 #include "stream.h"
40 #include "stream_identifier.h"
41 
42 namespace Tins {
43 
44 class PDU;
45 class TCP;
46 class IPv4Address;
47 class IPv6Address;
48 class Packet;
49 
50 namespace TCPIP {
51 
76 class TINS_API StreamFollower {
77 public:
82 
87 
94  SACKED_SEGMENTS
95  };
96 
102  typedef std::function<void(Stream&, TerminationReason)> stream_termination_callback_type;
103 
107  StreamFollower();
108 
118  void process_packet(PDU& packet);
119 
129  void process_packet(Packet& packet);
130 
139  void new_stream_callback(const stream_callback_type& callback);
140 
152  void stream_termination_callback(const stream_termination_callback_type& callback);
153 
160  template <typename Rep, typename Period>
161  void stream_keep_alive(const std::chrono::duration<Rep, Period>& keep_alive) {
162  stream_keep_alive_ = keep_alive;
163  }
164 
173  Stream& find_stream(const IPv4Address& client_addr, uint16_t client_port,
174  const IPv4Address& server_addr, uint16_t server_port);
175 
184  Stream& find_stream(const IPv6Address& client_addr, uint16_t client_port,
185  const IPv6Address& server_addr, uint16_t server_port);
186 private:
187  typedef Stream::timestamp_type timestamp_type;
188 
189  static const size_t DEFAULT_MAX_BUFFERED_CHUNKS;
190  static const size_t DEFAULT_MAX_SACKED_INTERVALS;
191  static const uint32_t DEFAULT_MAX_BUFFERED_BYTES;
192  static const timestamp_type DEFAULT_KEEP_ALIVE;
193 
194  typedef std::map<stream_id, Stream> streams_type;
195 
196  Stream& find_stream(const stream_id& id);
197  void process_packet(PDU& packet, const timestamp_type& ts);
198  void cleanup_streams(const timestamp_type& now);
199 
200  streams_type streams_;
201  stream_callback_type on_new_connection_;
202  stream_termination_callback_type on_stream_termination_;
203  size_t max_buffered_chunks_;
204  uint32_t max_buffered_bytes_;
205  timestamp_type last_cleanup_;
206  timestamp_type stream_keep_alive_;
207  bool attach_to_flows_;
208 };
209 
210 } // TCPIP
211 } // Tins
212 
213 #endif // TINS_IS_CXX11
214 
215 #endif // TINS_TCP_IP_STREAM_FOLLOWER_H
Definition: packet.h:131
void stream_keep_alive(const std::chrono::duration< Rep, Period > &keep_alive)
Sets the maximum time a stream will be followed without capturing packets that belong to it...
Definition: stream_follower.h:161
Represents a TCP stream.
Definition: stream.h:70
Uniquely identifies a stream.
Definition: stream_identifier.h:55
The stream was terminated due to a timeout.
Definition: stream_follower.h:92
Represents a class that follows TCP and reassembles streams.
Definition: stream_follower.h:76
TerminationReason
Definition: stream_follower.h:91
StreamIdentifier stream_id
Definition: stream_follower.h:86
std::function< void(Stream &, TerminationReason)> stream_termination_callback_type
The type used for stream termination callbacks.
Definition: stream_follower.h:102
The stream was terminated because it had too much buffered data.
Definition: stream_follower.h:93
std::function< void(Stream &)> stream_callback_type
Definition: stream.h:85
std::chrono::microseconds timestamp_type
Definition: stream.h:80
Stream::stream_callback_type stream_callback_type
Definition: stream_follower.h:81
Abstraction of an IPv4 address.
Definition: ip_address.h:44
Definition: ipv6_address.h:44
Base class for protocol data units.
Definition: pdu.h:108