PcapPlusPlus
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 
129  IPv4PacketKey(const IPv4PacketKey& other) : m_IpID(other.m_IpID), m_SrcIP(other.m_SrcIP), m_DstIP(other.m_DstIP) { }
130 
134  inline uint16_t getIpID() { return m_IpID; }
135 
139  inline IPv4Address getSrcIP() { return m_SrcIP; }
140 
144  inline IPv4Address getDstIP() { return m_DstIP; }
145 
150  inline void setIpID(uint16_t ipID) { m_IpID = ipID; }
151 
156  inline void setSrcIP(const IPv4Address& srcIP) { m_SrcIP = srcIP; }
157 
162  inline void setDstIP(const IPv4Address& dstIP) { m_DstIP = dstIP; }
163 
164 
165  // implement abstract methods
166 
167  uint32_t getHashValue() const;
168 
172  ProtocolType getProtocolType() const { return IPv4; }
173 
174  PacketKey* clone() const { return new IPv4PacketKey(*this); }
175 
176  private:
177  uint16_t m_IpID;
178  IPv4Address m_SrcIP;
179  IPv4Address m_DstIP;
180  };
181 
182 
188  class IPv6PacketKey : public PacketKey
189  {
190  public:
191 
195  IPv6PacketKey() : m_FragmentID(0), m_SrcIP(IPv6Address::Zero), m_DstIP(IPv6Address::Zero) { }
196 
203  IPv6PacketKey(uint32_t fragmentID, IPv6Address srcip, IPv6Address dstip) : m_FragmentID(fragmentID), m_SrcIP(srcip), m_DstIP(dstip) { }
204 
208  IPv6PacketKey(const IPv6PacketKey& other) : m_FragmentID(other.m_FragmentID), m_SrcIP(other.m_SrcIP), m_DstIP(other.m_DstIP) { }
209 
213  inline uint32_t getFragmentID() { return m_FragmentID; }
214 
218  inline IPv6Address getSrcIP() { return m_SrcIP; }
219 
223  inline IPv6Address getDstIP() { return m_DstIP; }
224 
229  inline void setFragmentID(uint32_t fragID) { m_FragmentID = fragID; }
230 
235  inline void setSrcIP(const IPv6Address& srcIP) { m_SrcIP = srcIP; }
236 
241  inline void setDstIP(const IPv6Address& dstIP) { m_DstIP = dstIP; }
242 
243 
244  // implement abstract methods
245 
246  uint32_t getHashValue() const;
247 
251  ProtocolType getProtocolType() const { return IPv6; }
252 
253  PacketKey* clone() const { return new IPv6PacketKey(*this); }
254 
255  private:
256  uint32_t m_FragmentID;
257  IPv6Address m_SrcIP;
258  IPv6Address m_DstIP;
259  };
260 
261 
270  typedef void (*OnFragmentsClean)(const PacketKey* key, void* userCookie);
271 
276  {
280  NON_FRAGMENT = 0x01,
284  FRAGMENT = 0x04,
290  REASSEMBLED = 0x20
291  };
292 
301  IPReassembly(OnFragmentsClean onFragmentsCleanCallback = NULL, void* callbackUserCookie = NULL, size_t maxPacketsToStore = PCPP_IP_REASSEMBLY_DEFAULT_MAX_PACKETS_TO_STORE);
302 
306  ~IPReassembly();
307 
329  Packet* processPacket(Packet* fragment, ReassemblyStatus& status);
330 
353  Packet* processPacket(RawPacket* fragment, ReassemblyStatus& status);
354 
364  Packet* getCurrentPacket(const PacketKey& key);
365 
371  void removePacket(const PacketKey& key);
372 
376  inline size_t getMaxCapacity() { return (int)m_PacketLRU->getMaxSize(); }
377 
381  inline size_t getCurrentCapacity() { return m_FragmentMap.size(); }
382 
383  private:
384 
385  struct IPFragment
386  {
387  uint16_t fragmentOffset;
388  bool lastFragment;
389  uint8_t* fragmentData;
390  size_t fragmentDataLen;
391  IPFragment() { fragmentOffset = 0; lastFragment = false; fragmentData = NULL; fragmentDataLen = 0; }
392  ~IPFragment() { delete [] fragmentData; }
393  };
394 
395  struct IPFragmentData
396  {
397  uint16_t currentOffset;
398  RawPacket* data;
399  bool deleteData;
400  uint32_t fragmentID;
401  PacketKey* packetKey;
402  PointerVector<IPFragment> outOfOrderFragments;
403  IPFragmentData(PacketKey* pktKey, uint32_t fragId) { currentOffset = 0; data = NULL; deleteData = true; fragmentID = fragId; packetKey = pktKey; }
404  ~IPFragmentData() { delete packetKey; if (deleteData && data != NULL) { delete data; } }
405  };
406 
407  LRUList<uint32_t>* m_PacketLRU;
408  std::map<uint32_t, IPFragmentData*> m_FragmentMap;
409  OnFragmentsClean m_OnFragmentsCleanCallback;
410  void* m_CallbackUserCookie;
411 
412  void addNewFragment(uint32_t hash, IPFragmentData* fragData);
413  bool matchOutOfOrderFragments(IPFragmentData* fragData);
414  };
415 
416 } // namespace pcpp
417 
418 #endif // PACKETPP_IP_REASSEMBLY
Definition: IpAddress.h:212
IPv4Address getDstIP()
Definition: IPReassembly.h:144
Definition: IPReassembly.h:286
uint16_t getIpID()
Definition: IPReassembly.h:134
Definition: IPReassembly.h:282
void setFragmentID(uint32_t fragID)
Definition: IPReassembly.h:229
IPReassembly(OnFragmentsClean onFragmentsCleanCallback=NULL, void *callbackUserCookie=NULL, size_t maxPacketsToStore=500000)
IPv4PacketKey(uint16_t ipid, IPv4Address srcip, IPv4Address dstip)
Definition: IPReassembly.h:124
IPv4PacketKey(const IPv4PacketKey &other)
Definition: IPReassembly.h:129
void setIpID(uint16_t ipID)
Definition: IPReassembly.h:150
IPv6PacketKey()
Definition: IPReassembly.h:195
IPv4Address getSrcIP()
Definition: IPReassembly.h:139
void setSrcIP(const IPv6Address &srcIP)
Definition: IPReassembly.h:235
IPv6PacketKey(uint32_t fragmentID, IPv6Address srcip, IPv6Address dstip)
Definition: IPReassembly.h:203
Definition: RawPacket.h:220
Definition: Packet.h:26
virtual PacketKey * clone() const =0
virtual ProtocolType getProtocolType() const =0
The main namespace for the PcapPlusPlus lib.
Definition: IPReassembly.h:72
Definition: IPReassembly.h:290
IPv6PacketKey(const IPv6PacketKey &other)
Definition: IPReassembly.h:208
void removePacket(const PacketKey &key)
Definition: IpAddress.h:113
void(* OnFragmentsClean)(const PacketKey *key, void *userCookie)
Definition: IPReassembly.h:270
void setDstIP(const IPv4Address &dstIP)
Definition: IPReassembly.h:162
Definition: IPReassembly.h:278
Definition: IPReassembly.h:288
void setSrcIP(const IPv4Address &srcIP)
Definition: IPReassembly.h:156
Definition: IPReassembly.h:188
ReassemblyStatus
Definition: IPReassembly.h:275
Definition: IPReassembly.h:63
#define PCPP_IP_REASSEMBLY_DEFAULT_MAX_PACKETS_TO_STORE
Definition: IPReassembly.h:50
IPv4PacketKey()
Definition: IPReassembly.h:116
uint32_t getFragmentID()
Definition: IPReassembly.h:213
Definition: IPReassembly.h:109
size_t getMaxSize()
Definition: LRUList.h:106
virtual ~PacketKey()
Definition: IPReassembly.h:79
size_t getCurrentCapacity()
Definition: IPReassembly.h:381
ProtocolType getProtocolType() const
Definition: IPReassembly.h:251
virtual uint32_t getHashValue() const =0
ProtocolType
Definition: ProtocolType.h:16
IPv6Address getSrcIP()
Definition: IPReassembly.h:218
Definition: ProtocolType.h:31
Packet * getCurrentPacket(const PacketKey &key)
ProtocolType getProtocolType() const
Definition: IPReassembly.h:172
Definition: IPReassembly.h:280
Packet * processPacket(Packet *fragment, ReassemblyStatus &status)
PacketKey * clone() const
Definition: IPReassembly.h:253
void setDstIP(const IPv6Address &dstIP)
Definition: IPReassembly.h:241
IPv6Address getDstIP()
Definition: IPReassembly.h:223
Definition: ProtocolType.h:36
PacketKey * clone() const
Definition: IPReassembly.h:174
Definition: IPReassembly.h:284
size_t getMaxCapacity()
Definition: IPReassembly.h:376