libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
ip_reassembler.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_IP_REASSEMBLER_H
31 #define TINS_IP_REASSEMBLER_H
32 
33 #include <vector>
34 #include <map>
35 #include "pdu.h"
36 #include "macros.h"
37 #include "ip_address.h"
38 
39 namespace Tins {
40 
44 class IP;
45 namespace Internals {
46 class IPv4Fragment {
47 public:
48  typedef PDU::serialization_type payload_type;
49 
50  IPv4Fragment() : offset_() { }
51 
52  template<typename T>
53  IPv4Fragment(T* pdu, uint16_t offset)
54  : payload_(pdu->serialize()), offset_(offset) {
55 
56  }
57 
58  const payload_type& payload() const {
59  return payload_;
60  }
61 
62  uint16_t offset() const {
63  return offset_;
64  }
65 private:
66  payload_type payload_;
67  uint16_t offset_;
68 };
69 
70 class TINS_API IPv4Stream {
71 public:
72  IPv4Stream();
73 
74  void add_fragment(IP* ip);
75  bool is_complete() const;
76  PDU* allocate_pdu() const;
77 private:
78  typedef std::vector<IPv4Fragment> fragments_type;
79 
80  uint16_t extract_offset(const IP* ip);
81  bool extract_more_frag(const IP* ip);
82 
83  fragments_type fragments_;
84  bool received_end_;
85  uint8_t transport_proto_;
86  size_t received_size_, total_size_;
87 };
88 } // namespace Internals
89 
118 class TINS_API IPv4Reassembler {
119 public:
126  REASSEMBLED
127  };
128 
129  TINS_DEPRECATED(typedef PacketStatus packet_status);
130 
136  NONE
137  };
138 
142  IPv4Reassembler();
143 
150  IPv4Reassembler(OverlappingTechnique technique);
151 
166  PacketStatus process(PDU& pdu);
167 
171  void clear_streams();
172 
183  void remove_stream(uint16_t id, IPv4Address addr1, IPv4Address addr2);
184 private:
185  typedef std::pair<IPv4Address, IPv4Address> address_pair;
186  typedef std::pair<uint16_t, address_pair> key_type;
187  typedef std::map<key_type, Internals::IPv4Stream> streams_type;
188 
189  key_type make_key(const IP* ip) const;
190  address_pair make_address_pair(IPv4Address addr1, IPv4Address addr2) const;
191 
192  streams_type streams_;
193  OverlappingTechnique technique_;
194 };
195 
199 template<typename Functor>
201 public:
207  IPv4ReassemblerProxy(Functor func)
208  : functor_(func) {
209 
210  }
211 
220  bool operator()(PDU& pdu) {
221  // Forward it unless it's fragmented.
222  if (reassembler_.process(pdu) != IPv4Reassembler::FRAGMENTED) {
223  return functor_(pdu);
224  }
225  else {
226  return true;
227  }
228  }
229 private:
230  IPv4Reassembler reassembler_;
231  Functor functor_;
232 };
233 
240 template<typename Functor>
242  return IPv4ReassemblerProxy<Functor>(func);
243 }
244 
245 } // Tins
246 
247 #endif // TINS_IP_REASSEMBLER_H
Class that represents an IP PDU.
Definition: ip.h:63
OverlappingTechnique
Definition: ip_reassembler.h:135
PacketStatus
Definition: ip_reassembler.h:123
IPv4ReassemblerProxy(Functor func)
Definition: ip_reassembler.h:207
bool operator()(PDU &pdu)
Tries to reassemble the packet and forwards it to the functor.
Definition: ip_reassembler.h:220
Definition: ip_reassembler.h:200
IPv4ReassemblerProxy< Functor > make_ipv4_reassembler_proxy(Functor func)
Definition: ip_reassembler.h:241
The given packet is not fragmented.
Definition: ip_reassembler.h:124
Abstraction of an IPv4 address.
Definition: ip_address.h:44
PacketStatus process(PDU &pdu)
Processes a PDU and tries to reassemble it.
Definition: ip_reassembler.cpp:106
byte_array serialization_type
Definition: pdu.h:112
The given packet is fragmented and can't be reassembled yet.
Definition: ip_reassembler.h:125
Base class for protocol data units.
Definition: pdu.h:107
Reassembles fragmented IP packets.
Definition: ip_reassembler.h:118