PcapPlusPlus  23.09
IPReassembly.h
Go to the documentation of this file.
1 #ifndef PACKETPP_IP_REASSEMBLY
2 #define PACKETPP_IP_REASSEMBLY
3 
4 #include "Packet.h"
5 #include "LRUList.h"
6 #include "IpAddress.h"
7 #include "PointerVector.h"
8 #include <map>
9 
44 namespace pcpp
45 {
46 
50  #define PCPP_IP_REASSEMBLY_DEFAULT_MAX_PACKETS_TO_STORE 500000
51 
64  {
65  public:
66 
72  class PacketKey
73  {
74  public:
75 
79  virtual ~PacketKey() = default;
80 
84  virtual uint32_t getHashValue() const = 0;
85 
89  virtual ProtocolType getProtocolType() const = 0;
90 
94  virtual PacketKey* clone() const = 0;
95 
96  protected:
97  // private c'tor
98  PacketKey() = default;
99 
100  // private copy c'tor
101  PacketKey(const PacketKey& other) = default;
102  };
103 
104 
109  class IPv4PacketKey : public PacketKey
110  {
111  public:
112 
116  IPv4PacketKey() : m_IpID(0), m_SrcIP(IPv4Address::Zero), m_DstIP(IPv4Address::Zero) { }
117 
124  IPv4PacketKey(uint16_t ipid, IPv4Address srcip, IPv4Address dstip) : m_IpID(ipid), m_SrcIP(srcip), m_DstIP(dstip) { }
125 
130  IPv4PacketKey(const IPv4PacketKey& other) : PacketKey(other), m_IpID(other.m_IpID), m_SrcIP(other.m_SrcIP), m_DstIP(other.m_DstIP) { }
131 
137  {
138  m_IpID = other.m_IpID;
139  m_SrcIP = other.m_SrcIP;
140  m_DstIP = other.m_DstIP;
141  return *this;
142  }
143 
147  uint16_t getIpID() const { return m_IpID; }
148 
152  IPv4Address getSrcIP() const { return m_SrcIP; }
153 
157  IPv4Address getDstIP() const { return m_DstIP; }
158 
163  void setIpID(uint16_t ipID) { m_IpID = ipID; }
164 
169  void setSrcIP(const IPv4Address& srcIP) { m_SrcIP = srcIP; }
170 
175  void setDstIP(const IPv4Address& dstIP) { m_DstIP = dstIP; }
176 
177 
178  // implement abstract methods
179 
180  uint32_t getHashValue() const;
181 
185  ProtocolType getProtocolType() const { return IPv4; }
186 
187  PacketKey* clone() const { return new IPv4PacketKey(*this); }
188 
189  private:
190  uint16_t m_IpID;
191  IPv4Address m_SrcIP;
192  IPv4Address m_DstIP;
193  };
194 
195 
201  class IPv6PacketKey : public PacketKey
202  {
203  public:
204 
208  IPv6PacketKey() : m_FragmentID(0), m_SrcIP(IPv6Address::Zero), m_DstIP(IPv6Address::Zero) { }
209 
216  IPv6PacketKey(uint32_t fragmentID, IPv6Address srcip, IPv6Address dstip) : m_FragmentID(fragmentID), m_SrcIP(srcip), m_DstIP(dstip) { }
217 
222  IPv6PacketKey(const IPv6PacketKey& other) : PacketKey(other), m_FragmentID(other.m_FragmentID), m_SrcIP(other.m_SrcIP), m_DstIP(other.m_DstIP) { }
223 
229  {
230  m_FragmentID = other.m_FragmentID;
231  m_SrcIP = other.m_SrcIP;
232  m_DstIP = other.m_DstIP;
233  return *this;
234  }
235 
239  uint32_t getFragmentID() const { return m_FragmentID; }
240 
244  IPv6Address getSrcIP() const { return m_SrcIP; }
245 
249  IPv6Address getDstIP() const { return m_DstIP; }
250 
255  void setFragmentID(uint32_t fragID) { m_FragmentID = fragID; }
256 
261  void setSrcIP(const IPv6Address& srcIP) { m_SrcIP = srcIP; }
262 
267  void setDstIP(const IPv6Address& dstIP) { m_DstIP = dstIP; }
268 
269 
270  // implement abstract methods
271 
272  uint32_t getHashValue() const;
273 
277  ProtocolType getProtocolType() const { return IPv6; }
278 
279  PacketKey* clone() const { return new IPv6PacketKey(*this); }
280 
281  private:
282  uint32_t m_FragmentID;
283  IPv6Address m_SrcIP;
284  IPv6Address m_DstIP;
285  };
286 
287 
296  typedef void (*OnFragmentsClean)(const PacketKey* key, void* userCookie);
297 
302  {
306  NON_FRAGMENT = 0x01,
310  FRAGMENT = 0x04,
316  REASSEMBLED = 0x20
317  };
318 
327  explicit IPReassembly(OnFragmentsClean onFragmentsCleanCallback = NULL, void *callbackUserCookie = NULL, size_t maxPacketsToStore = PCPP_IP_REASSEMBLY_DEFAULT_MAX_PACKETS_TO_STORE)
328  : m_PacketLRU(maxPacketsToStore), m_OnFragmentsCleanCallback(onFragmentsCleanCallback), m_CallbackUserCookie(callbackUserCookie) {}
329 
333  ~IPReassembly();
334 
364  Packet* processPacket(Packet* fragment, ReassemblyStatus& status, ProtocolType parseUntil = UnknownProtocol, OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
365 
398 
408  Packet* getCurrentPacket(const PacketKey& key);
409 
415  void removePacket(const PacketKey& key);
416 
420  size_t getMaxCapacity() const { return m_PacketLRU.getMaxSize(); }
421 
425  size_t getCurrentCapacity() const { return m_FragmentMap.size(); }
426 
427  private:
428 
429  struct IPFragment
430  {
431  uint16_t fragmentOffset;
432  bool lastFragment;
433  uint8_t* fragmentData;
434  size_t fragmentDataLen;
435  IPFragment() { fragmentOffset = 0; lastFragment = false; fragmentData = NULL; fragmentDataLen = 0; }
436  ~IPFragment() { delete [] fragmentData; }
437  };
438 
439  struct IPFragmentData
440  {
441  uint16_t currentOffset;
442  RawPacket* data;
443  bool deleteData;
444  uint32_t fragmentID;
445  PacketKey* packetKey;
446  PointerVector<IPFragment> outOfOrderFragments;
447  IPFragmentData(PacketKey* pktKey, uint32_t fragId) { currentOffset = 0; data = NULL; deleteData = true; fragmentID = fragId; packetKey = pktKey; }
448  ~IPFragmentData() { delete packetKey; if (deleteData && data != NULL) { delete data; } }
449  };
450 
451  LRUList<uint32_t> m_PacketLRU;
452  std::map<uint32_t, IPFragmentData*> m_FragmentMap;
453  OnFragmentsClean m_OnFragmentsCleanCallback;
454  void* m_CallbackUserCookie;
455 
456  void addNewFragment(uint32_t hash, IPFragmentData* fragData);
457  bool matchOutOfOrderFragments(IPFragmentData* fragData);
458  };
459 
460 } // namespace pcpp
461 
462 #endif // PACKETPP_IP_REASSEMBLY
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
IPv4Address getDstIP() const
Definition: IPReassembly.h:157
virtual ProtocolType getProtocolType() const =0
void setSrcIP(const IPv6Address &srcIP)
Definition: IPReassembly.h:261
Definition: IPReassembly.h:316
PacketKey * clone() const
Definition: IPReassembly.h:187
IPv4PacketKey(const IPv4PacketKey &other)
Definition: IPReassembly.h:130
Definition: IPReassembly.h:306
Definition: Packet.h:26
size_t getCurrentCapacity() const
Definition: IPReassembly.h:425
Definition: RawPacket.h:254
void removePacket(const PacketKey &key)
IPv4PacketKey & operator=(const IPv4PacketKey &other)
Definition: IPReassembly.h:136
IPv6Address getSrcIP() const
Definition: IPReassembly.h:244
const ProtocolType UnknownProtocol
Definition: ProtocolType.h:23
IPv6PacketKey(const IPv6PacketKey &other)
Definition: IPReassembly.h:222
Definition: ProtocolType.h:350
IPv6PacketKey & operator=(const IPv6PacketKey &other)
Definition: IPReassembly.h:228
uint64_t ProtocolType
Definition: ProtocolType.h:18
void(* OnFragmentsClean)(const PacketKey *key, void *userCookie)
Definition: IPReassembly.h:296
void setDstIP(const IPv6Address &dstIP)
Definition: IPReassembly.h:267
Definition: IPReassembly.h:201
void setFragmentID(uint32_t fragID)
Definition: IPReassembly.h:255
Definition: IPReassembly.h:304
uint16_t getIpID() const
Definition: IPReassembly.h:147
Definition: IPReassembly.h:72
Definition: IPReassembly.h:109
Definition: IpAddress.h:42
Definition: IpAddress.h:192
Definition: IPReassembly.h:310
virtual uint32_t getHashValue() const =0
Definition: IPReassembly.h:312
uint32_t getFragmentID() const
Definition: IPReassembly.h:239
#define PCPP_IP_REASSEMBLY_DEFAULT_MAX_PACKETS_TO_STORE
Definition: IPReassembly.h:50
Definition: IPReassembly.h:63
PacketKey * clone() const
Definition: IPReassembly.h:279
ProtocolType getProtocolType() const
Definition: IPReassembly.h:185
IPv4PacketKey()
Definition: IPReassembly.h:116
ReassemblyStatus
Definition: IPReassembly.h:301
virtual PacketKey * clone() const =0
Definition: IPReassembly.h:308
virtual ~PacketKey()=default
const ProtocolType IPv6
Definition: ProtocolType.h:38
IPv6PacketKey(uint32_t fragmentID, IPv6Address srcip, IPv6Address dstip)
Definition: IPReassembly.h:216
size_t getMaxCapacity() const
Definition: IPReassembly.h:420
IPv4PacketKey(uint16_t ipid, IPv4Address srcip, IPv4Address dstip)
Definition: IPReassembly.h:124
void setDstIP(const IPv4Address &dstIP)
Definition: IPReassembly.h:175
ProtocolType getProtocolType() const
Definition: IPReassembly.h:277
IPReassembly(OnFragmentsClean onFragmentsCleanCallback=NULL, void *callbackUserCookie=NULL, size_t maxPacketsToStore=500000)
Definition: IPReassembly.h:327
const ProtocolType IPv4
Definition: ProtocolType.h:33
Packet * processPacket(Packet *fragment, ReassemblyStatus &status, ProtocolType parseUntil=UnknownProtocol, OsiModelLayer parseUntilLayer=OsiModelLayerUnknown)
void setIpID(uint16_t ipID)
Definition: IPReassembly.h:163
void setSrcIP(const IPv4Address &srcIP)
Definition: IPReassembly.h:169
IPv6Address getDstIP() const
Definition: IPReassembly.h:249
IPv4Address getSrcIP() const
Definition: IPReassembly.h:152
IPv6PacketKey()
Definition: IPReassembly.h:208
Definition: IPReassembly.h:314
Packet * getCurrentPacket(const PacketKey &key)