PcapPlusPlus  Next
IPReassembly.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Packet.h"
4 #include "LRUList.h"
5 #include "IpAddress.h"
6 #include "PointerVector.h"
7 #include <unordered_map>
8 
47 namespace pcpp
48 {
49 
53 #define PCPP_IP_REASSEMBLY_DEFAULT_MAX_PACKETS_TO_STORE 500000
54 
69  {
70  public:
76  class PacketKey
77  {
78  public:
82  virtual ~PacketKey() = default;
83 
87  virtual uint32_t getHashValue() const = 0;
88 
92  virtual ProtocolType getProtocolType() const = 0;
93 
97  virtual PacketKey* clone() const = 0;
98 
99  protected:
100  // private c'tor
101  PacketKey() = default;
102 
103  // private copy c'tor
104  PacketKey(const PacketKey& other) = default;
105  };
106 
112  class IPv4PacketKey : public PacketKey
113  {
114  public:
118  IPv4PacketKey() : m_IpID(0), m_SrcIP(IPv4Address::Zero), m_DstIP(IPv4Address::Zero)
119  {}
120 
127  IPv4PacketKey(uint16_t ipid, IPv4Address srcip, IPv4Address dstip)
128  : m_IpID(ipid), m_SrcIP(srcip), m_DstIP(dstip)
129  {}
130 
136  : PacketKey(other), m_IpID(other.m_IpID), m_SrcIP(other.m_SrcIP), m_DstIP(other.m_DstIP)
137  {}
138 
144  {
145  m_IpID = other.m_IpID;
146  m_SrcIP = other.m_SrcIP;
147  m_DstIP = other.m_DstIP;
148  return *this;
149  }
150 
154  uint16_t getIpID() const
155  {
156  return m_IpID;
157  }
158 
163  {
164  return m_SrcIP;
165  }
166 
171  {
172  return m_DstIP;
173  }
174 
179  void setIpID(uint16_t ipID)
180  {
181  m_IpID = ipID;
182  }
183 
188  void setSrcIP(const IPv4Address& srcIP)
189  {
190  m_SrcIP = srcIP;
191  }
192 
197  void setDstIP(const IPv4Address& dstIP)
198  {
199  m_DstIP = dstIP;
200  }
201 
202  // implement abstract methods
203 
204  uint32_t getHashValue() const override;
205 
209  ProtocolType getProtocolType() const override
210  {
211  return IPv4;
212  }
213 
214  PacketKey* clone() const override
215  {
216  return new IPv4PacketKey(*this);
217  }
218 
219  private:
220  uint16_t m_IpID;
221  IPv4Address m_SrcIP;
222  IPv4Address m_DstIP;
223  };
224 
230  class IPv6PacketKey : public PacketKey
231  {
232  public:
236  IPv6PacketKey() : m_FragmentID(0), m_SrcIP(IPv6Address::Zero), m_DstIP(IPv6Address::Zero)
237  {}
238 
245  IPv6PacketKey(uint32_t fragmentID, IPv6Address srcip, IPv6Address dstip)
246  : m_FragmentID(fragmentID), m_SrcIP(srcip), m_DstIP(dstip)
247  {}
248 
254  : PacketKey(other), m_FragmentID(other.m_FragmentID), m_SrcIP(other.m_SrcIP), m_DstIP(other.m_DstIP)
255  {}
256 
262  {
263  m_FragmentID = other.m_FragmentID;
264  m_SrcIP = other.m_SrcIP;
265  m_DstIP = other.m_DstIP;
266  return *this;
267  }
268 
272  uint32_t getFragmentID() const
273  {
274  return m_FragmentID;
275  }
276 
281  {
282  return m_SrcIP;
283  }
284 
289  {
290  return m_DstIP;
291  }
292 
297  void setFragmentID(uint32_t fragID)
298  {
299  m_FragmentID = fragID;
300  }
301 
306  void setSrcIP(const IPv6Address& srcIP)
307  {
308  m_SrcIP = srcIP;
309  }
310 
315  void setDstIP(const IPv6Address& dstIP)
316  {
317  m_DstIP = dstIP;
318  }
319 
320  // implement abstract methods
321 
322  uint32_t getHashValue() const override;
323 
327  ProtocolType getProtocolType() const override
328  {
329  return IPv6;
330  }
331 
332  PacketKey* clone() const override
333  {
334  return new IPv6PacketKey(*this);
335  }
336 
337  private:
338  uint32_t m_FragmentID;
339  IPv6Address m_SrcIP;
340  IPv6Address m_DstIP;
341  };
342 
353  typedef void (*OnFragmentsClean)(const PacketKey* key, void* userCookie);
354 
359  {
363  NON_FRAGMENT = 0x01,
367  FRAGMENT = 0x04,
374  REASSEMBLED = 0x20
375  };
376 
387  explicit IPReassembly(OnFragmentsClean onFragmentsCleanCallback = nullptr, void* callbackUserCookie = nullptr,
388  size_t maxPacketsToStore = PCPP_IP_REASSEMBLY_DEFAULT_MAX_PACKETS_TO_STORE)
389  : m_PacketLRU(maxPacketsToStore), m_OnFragmentsCleanCallback(onFragmentsCleanCallback),
390  m_CallbackUserCookie(callbackUserCookie)
391  {}
392 
397 
435  OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
436 
477  OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
478 
490 
496  void removePacket(const PacketKey& key);
497 
501  size_t getMaxCapacity() const
502  {
503  return m_PacketLRU.getMaxSize();
504  }
505 
509  size_t getCurrentCapacity() const
510  {
511  return m_FragmentMap.size();
512  }
513 
514  private:
515  struct IPFragment
516  {
517  uint16_t fragmentOffset;
518  bool lastFragment;
519  uint8_t* fragmentData;
520  size_t fragmentDataLen;
521  IPFragment()
522  {
523  fragmentOffset = 0;
524  lastFragment = false;
525  fragmentData = nullptr;
526  fragmentDataLen = 0;
527  }
528  ~IPFragment()
529  {
530  delete[] fragmentData;
531  }
532  };
533 
534  struct IPFragmentData
535  {
536  uint16_t currentOffset;
537  RawPacket* data;
538  bool deleteData;
539  uint32_t fragmentID;
540  PacketKey* packetKey;
541  PointerVector<IPFragment> outOfOrderFragments;
542  IPFragmentData(PacketKey* pktKey, uint32_t fragId)
543  {
544  currentOffset = 0;
545  data = nullptr;
546  deleteData = true;
547  fragmentID = fragId;
548  packetKey = pktKey;
549  }
550  ~IPFragmentData()
551  {
552  delete packetKey;
553  if (deleteData && data != nullptr)
554  {
555  delete data;
556  }
557  }
558  };
559 
560  LRUList<uint32_t> m_PacketLRU;
561  std::unordered_map<uint32_t, IPFragmentData*> m_FragmentMap;
562  OnFragmentsClean m_OnFragmentsCleanCallback;
563  void* m_CallbackUserCookie;
564 
565  void addNewFragment(uint32_t hash, IPFragmentData* fragData);
566  bool matchOutOfOrderFragments(IPFragmentData* fragData);
567  };
568 
569 } // namespace pcpp
#define PCPP_IP_REASSEMBLY_DEFAULT_MAX_PACKETS_TO_STORE
Definition: IPReassembly.h:53
Definition: IPReassembly.h:113
uint32_t getHashValue() const override
void setDstIP(const IPv4Address &dstIP)
Definition: IPReassembly.h:197
IPv4PacketKey & operator=(const IPv4PacketKey &other)
Definition: IPReassembly.h:143
uint16_t getIpID() const
Definition: IPReassembly.h:154
ProtocolType getProtocolType() const override
Definition: IPReassembly.h:209
void setSrcIP(const IPv4Address &srcIP)
Definition: IPReassembly.h:188
IPv4PacketKey(uint16_t ipid, IPv4Address srcip, IPv4Address dstip)
Definition: IPReassembly.h:127
IPv4Address getSrcIP() const
Definition: IPReassembly.h:162
IPv4Address getDstIP() const
Definition: IPReassembly.h:170
IPv4PacketKey()
Definition: IPReassembly.h:118
void setIpID(uint16_t ipID)
Definition: IPReassembly.h:179
PacketKey * clone() const override
Definition: IPReassembly.h:214
IPv4PacketKey(const IPv4PacketKey &other)
Definition: IPReassembly.h:135
Definition: IPReassembly.h:231
IPv6PacketKey()
Definition: IPReassembly.h:236
IPv6PacketKey(uint32_t fragmentID, IPv6Address srcip, IPv6Address dstip)
Definition: IPReassembly.h:245
void setFragmentID(uint32_t fragID)
Definition: IPReassembly.h:297
void setSrcIP(const IPv6Address &srcIP)
Definition: IPReassembly.h:306
uint32_t getHashValue() const override
ProtocolType getProtocolType() const override
Definition: IPReassembly.h:327
uint32_t getFragmentID() const
Definition: IPReassembly.h:272
IPv6PacketKey & operator=(const IPv6PacketKey &other)
Definition: IPReassembly.h:261
void setDstIP(const IPv6Address &dstIP)
Definition: IPReassembly.h:315
IPv6PacketKey(const IPv6PacketKey &other)
Definition: IPReassembly.h:253
IPv6Address getDstIP() const
Definition: IPReassembly.h:288
PacketKey * clone() const override
Definition: IPReassembly.h:332
IPv6Address getSrcIP() const
Definition: IPReassembly.h:280
Definition: IPReassembly.h:77
virtual ~PacketKey()=default
virtual ProtocolType getProtocolType() const =0
virtual PacketKey * clone() const =0
virtual uint32_t getHashValue() const =0
Definition: IPReassembly.h:69
Packet * getCurrentPacket(const PacketKey &key)
size_t getMaxCapacity() const
Definition: IPReassembly.h:501
void removePacket(const PacketKey &key)
IPReassembly(OnFragmentsClean onFragmentsCleanCallback=nullptr, void *callbackUserCookie=nullptr, size_t maxPacketsToStore=500000)
Definition: IPReassembly.h:387
void(* OnFragmentsClean)(const PacketKey *key, void *userCookie)
Definition: IPReassembly.h:353
Packet * processPacket(Packet *fragment, ReassemblyStatus &status, ProtocolType parseUntil=UnknownProtocol, OsiModelLayer parseUntilLayer=OsiModelLayerUnknown)
Packet * processPacket(RawPacket *fragment, ReassemblyStatus &status, ProtocolType parseUntil=UnknownProtocol, OsiModelLayer parseUntilLayer=OsiModelLayerUnknown)
ReassemblyStatus
Definition: IPReassembly.h:359
@ FIRST_FRAGMENT
Definition: IPReassembly.h:365
@ FRAGMENT
Definition: IPReassembly.h:367
@ OUT_OF_ORDER_FRAGMENT
Definition: IPReassembly.h:369
@ REASSEMBLED
Definition: IPReassembly.h:374
@ MALFORMED_FRAGMENT
Definition: IPReassembly.h:372
@ NON_FRAGMENT
Definition: IPReassembly.h:363
@ NON_IP_PACKET
Definition: IPReassembly.h:361
size_t getCurrentCapacity() const
Definition: IPReassembly.h:509
Definition: IpAddress.h:32
Definition: IpAddress.h:199
size_t getMaxSize() const
Definition: LRUList.h:121
Definition: Packet.h:27
Definition: RawPacket.h:269
The main namespace for the PcapPlusPlus lib.
uint8_t ProtocolType
Definition: ProtocolType.h:17
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelLayerUnknown
Definition: ProtocolType.h:380
const ProtocolType IPv4
Definition: ProtocolType.h:38
const ProtocolType IPv6
Definition: ProtocolType.h:43
const ProtocolType UnknownProtocol
Definition: ProtocolType.h:28