libtins 4.5
Loading...
Searching...
No Matches
stream_follower.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_FOLLOWER_H
31#define TINS_TCP_IP_STREAM_FOLLOWER_H
32
33#include <tins/config.h>
34
35#ifdef TINS_HAVE_TCPIP
36
37#include <map>
38#include <tins/tcp_ip/stream.h>
39#include <tins/tcp_ip/stream_identifier.h>
40
41namespace Tins {
42
43class PDU;
44class TCP;
45class IPv4Address;
46class IPv6Address;
47class Packet;
48
49namespace TCPIP {
50
78class TINS_API StreamFollower {
79public:
83 typedef Stream::stream_callback_type stream_callback_type;
84
88 typedef StreamIdentifier stream_id;
89
93 enum TerminationReason {
94 TIMEOUT,
95 BUFFERED_DATA,
96 SACKED_SEGMENTS
97 };
98
104 typedef std::function<void(Stream&, TerminationReason)> stream_termination_callback_type;
105
109 StreamFollower();
110
120 void process_packet(PDU& packet);
121
131 void process_packet(Packet& packet);
132
141 void new_stream_callback(const stream_callback_type& callback);
142
154 void stream_termination_callback(const stream_termination_callback_type& callback);
155
162 template <typename Rep, typename Period>
163 void stream_keep_alive(const std::chrono::duration<Rep, Period>& keep_alive) {
164 stream_keep_alive_ = keep_alive;
165 }
166
175 Stream& find_stream(const IPv4Address& client_addr, uint16_t client_port,
176 const IPv4Address& server_addr, uint16_t server_port);
177
186 Stream& find_stream(const IPv6Address& client_addr, uint16_t client_port,
187 const IPv6Address& server_addr, uint16_t server_port);
188
207 void follow_partial_streams(bool value);
208private:
209 typedef Stream::timestamp_type timestamp_type;
210
211 static const size_t DEFAULT_MAX_BUFFERED_CHUNKS;
212 static const size_t DEFAULT_MAX_SACKED_INTERVALS;
213 static const uint32_t DEFAULT_MAX_BUFFERED_BYTES;
214 static const timestamp_type DEFAULT_KEEP_ALIVE;
215
216 typedef std::map<stream_id, Stream> streams_type;
217
218 Stream& find_stream(const stream_id& id);
219 void process_packet(PDU& packet, const timestamp_type& ts);
220 void cleanup_streams(const timestamp_type& now);
221
222 streams_type streams_;
223 stream_callback_type on_new_connection_;
224 stream_termination_callback_type on_stream_termination_;
225 size_t max_buffered_chunks_;
226 uint32_t max_buffered_bytes_;
227 timestamp_type last_cleanup_;
228 timestamp_type stream_keep_alive_;
229 bool attach_to_flows_;
230};
231
232} // TCPIP
233} // Tins
234
235#endif // TINS_HAVE_TCPIP
236
237#endif // TINS_TCP_IP_STREAM_FOLLOWER_H
The Tins namespace.
Definition address_range.h:38