PcapPlusPlus  21.05
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() {}
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() {}
99 
100  // private copy c'tor
101  PacketKey(const PacketKey& other) {}
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_SrcIP = 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  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 
362  Packet* processPacket(Packet* fragment, ReassemblyStatus& status, ProtocolType parseUntil = UnknownProtocol, OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
363 
393 
403  Packet* getCurrentPacket(const PacketKey& key);
404 
410  void removePacket(const PacketKey& key);
411 
415  size_t getMaxCapacity() const { return m_PacketLRU.getMaxSize(); }
416 
420  size_t getCurrentCapacity() const { return m_FragmentMap.size(); }
421 
422  private:
423 
424  struct IPFragment
425  {
426  uint16_t fragmentOffset;
427  bool lastFragment;
428  uint8_t* fragmentData;
429  size_t fragmentDataLen;
430  IPFragment() { fragmentOffset = 0; lastFragment = false; fragmentData = NULL; fragmentDataLen = 0; }
431  ~IPFragment() { delete [] fragmentData; }
432  };
433 
434  struct IPFragmentData
435  {
436  uint16_t currentOffset;
437  RawPacket* data;
438  bool deleteData;
439  uint32_t fragmentID;
440  PacketKey* packetKey;
441  PointerVector<IPFragment> outOfOrderFragments;
442  IPFragmentData(PacketKey* pktKey, uint32_t fragId) { currentOffset = 0; data = NULL; deleteData = true; fragmentID = fragId; packetKey = pktKey; }
443  ~IPFragmentData() { delete packetKey; if (deleteData && data != NULL) { delete data; } }
444  };
445 
446  LRUList<uint32_t> m_PacketLRU;
447  std::map<uint32_t, IPFragmentData*> m_FragmentMap;
448  OnFragmentsClean m_OnFragmentsCleanCallback;
449  void* m_CallbackUserCookie;
450 
451  void addNewFragment(uint32_t hash, IPFragmentData* fragData);
452  bool matchOutOfOrderFragments(IPFragmentData* fragData);
453  };
454 
455 } // namespace pcpp
456 
457 #endif // PACKETPP_IP_REASSEMBLY
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:253
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:420
Definition: RawPacket.h:252
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:270
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:26
Definition: IpAddress.h:160
Definition: IPReassembly.h:310
virtual uint32_t getHashValue() const =0
Definition: IPReassembly.h:312
uint32_t getFragmentID() const
Definition: IPReassembly.h:239
virtual ~PacketKey()
Definition: IPReassembly.h:79
#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
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:415
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)