30 #ifndef TINS_TCP_STREAM_H 31 #define TINS_TCP_STREAM_H 38 #include <tins/macros.h> 41 #include <tins/ip_address.h> 42 #include <tins/utils/pdu_utils.h> 46 #include <tins/sniffer.h> 56 class TINS_API TCPStream {
62 IPv4Address client_addr, server_addr;
63 uint16_t client_port, server_port;
65 StreamInfo() : client_port(0), server_port(0) {}
67 StreamInfo(IPv4Address client, IPv4Address server,
68 uint16_t cport, uint16_t sport);
70 bool operator<(
const StreamInfo& rhs)
const;
76 typedef std::vector<uint8_t> payload_type;
84 TCPStream(IP* ip, TCP* tcp, uint64_t identifier);
89 TCPStream(
const TCPStream& rhs);
94 TCPStream& operator=(
const TCPStream& rhs);
108 const payload_type& client_payload()
const {
109 return client_payload_;
119 payload_type& client_payload() {
120 return client_payload_;
130 const payload_type& server_payload()
const {
131 return server_payload_;
141 payload_type& server_payload() {
142 return server_payload_;
149 uint64_t id()
const {
157 const StreamInfo& stream_info()
const {
169 bool is_finished()
const {
183 bool update(IP* ip, TCP* tcp);
185 typedef std::map<uint32_t, RawPDU*> fragments_type;
187 static void free_fragments(fragments_type& frags);
188 static fragments_type clone_fragments(
const fragments_type& frags);
190 bool generic_process(uint32_t& my_seq, uint32_t& other_seq,
191 payload_type& pload, fragments_type& frags, TCP* tcp);
193 void safe_insert(fragments_type& frags, uint32_t seq, RawPDU* raw);
196 uint32_t client_seq_, server_seq_;
198 uint64_t identifier_;
199 payload_type client_payload_, server_payload_;
200 fragments_type client_frags_, server_frags_;
201 bool syn_ack_sent_, fin_sent_;
209 class TINS_API TCPStreamFollower {
228 template<
typename DataFunctor,
typename EndFunctor>
229 void follow_streams(BaseSniffer& sniffer, DataFunctor data_fun, EndFunctor end_fun);
252 template<
typename ForwardIterator,
typename DataFunctor,
typename EndFunctor>
253 void follow_streams(ForwardIterator start, ForwardIterator end,
254 DataFunctor data_fun, EndFunctor end_fun);
267 template<
typename DataFunctor>
268 void follow_streams(BaseSniffer& sniffer, DataFunctor data_fun);
289 template<
typename ForwardIterator,
typename DataFunctor>
290 void follow_streams(ForwardIterator start, ForwardIterator end,
291 DataFunctor data_fun);
293 typedef std::map<TCPStream::StreamInfo, TCPStream> sessions_type;
295 template<
typename DataFunctor,
typename EndFunctor>
296 struct proxy_caller {
297 bool callback(PDU& pdu) {
298 return stream->callback(pdu, data_fun, end_fun);
301 TCPStreamFollower* stream;
302 DataFunctor data_fun;
306 template<
typename DataFunctor,
typename EndFunctor>
307 bool callback(PDU& pdu,
const DataFunctor& fun,
const EndFunctor& end_fun);
308 static void dummy_function(TCPStream&) { }
310 sessions_type sessions_;
311 uint64_t last_identifier_;
314 template<
typename DataFunctor,
typename EndFunctor>
315 void TCPStreamFollower::follow_streams(BaseSniffer& sniffer,
316 DataFunctor data_fun,
317 EndFunctor end_fun) {
318 typedef proxy_caller<DataFunctor, EndFunctor> proxy_type;
319 proxy_type proxy = {
this, data_fun, end_fun };
320 sniffer.sniff_loop(make_sniffer_handler(&proxy, &proxy_type::callback));
323 template<
typename ForwardIterator,
typename DataFunctor,
typename EndFunctor>
324 void TCPStreamFollower::follow_streams(ForwardIterator start,
326 DataFunctor data_fun,
327 EndFunctor end_fun) {
328 while(start != end) {
336 template<
typename DataFunctor>
337 void TCPStreamFollower::follow_streams(BaseSniffer& sniffer, DataFunctor data_fun) {
338 return follow_streams(sniffer, data_fun, dummy_function);
341 template<
typename ForwardIterator,
typename DataFunctor>
342 void TCPStreamFollower::follow_streams(ForwardIterator start,
344 DataFunctor data_fun) {
345 follow_streams(start, end, data_fun, dummy_function);
348 template<
typename DataFunctor,
typename EndFunctor>
349 bool TCPStreamFollower::callback(PDU& pdu,
350 const DataFunctor& data_fun,
351 const EndFunctor& end_fun) {
352 IP* ip = pdu.find_pdu<IP>();
353 TCP* tcp = pdu.find_pdu<TCP>();
357 TCPStream::StreamInfo info(
358 ip->src_addr(), ip->dst_addr(),
359 tcp->sport(), tcp->dport()
361 sessions_type::iterator it = sessions_.find(info);
362 if (it == sessions_.end()) {
363 std::swap(info.client_addr, info.server_addr);
364 std::swap(info.client_port, info.server_port);
365 if ((it = sessions_.find(info)) == sessions_.end()) {
366 if (tcp->get_flag(TCP::SYN) && !tcp->get_flag(TCP::ACK)) {
370 TCPStream(ip, tcp, last_identifier_++)
377 if (it->second.update(ip, tcp)) {
378 data_fun(it->second);
381 if (it->second.is_finished()) {
390 #endif // TINS_HAVE_PCAP 392 #endif // TINS_TCP_STREAM_H PDU & dereference_until_pdu(PDU &pdu)
Definition: pdu_utils.h:61
The Tins namespace.
Definition: address_range.h:38