PcapPlusPlus  20.08
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) : 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) : 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
pcpp::IPReassembly::IPv6PacketKey::setSrcIP
void setSrcIP(const IPv6Address &srcIP)
Definition: IPReassembly.h:261
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:233
pcpp::IPv4Address
Definition: IpAddress.h:26
pcpp::IPv6Address
Definition: IpAddress.h:140
pcpp::Packet
Definition: Packet.h:26
pcpp::IPReassembly::IPv6PacketKey
Definition: IPReassembly.h:201
pcpp::IPReassembly::processPacket
Packet * processPacket(Packet *fragment, ReassemblyStatus &status, ProtocolType parseUntil=UnknownProtocol, OsiModelLayer parseUntilLayer=OsiModelLayerUnknown)
pcpp::IPReassembly::IPv6PacketKey::getFragmentID
uint32_t getFragmentID() const
Definition: IPReassembly.h:239
pcpp::IPReassembly::NON_FRAGMENT
@ NON_FRAGMENT
Definition: IPReassembly.h:306
pcpp::IPReassembly::MALFORMED_FRAGMENT
@ MALFORMED_FRAGMENT
Definition: IPReassembly.h:314
pcpp::IPReassembly::removePacket
void removePacket(const PacketKey &key)
pcpp::IPReassembly::IPv6PacketKey::clone
PacketKey * clone() const
Definition: IPReassembly.h:279
pcpp::IPReassembly::IPv4PacketKey
Definition: IPReassembly.h:109
pcpp::IPReassembly::getCurrentCapacity
size_t getCurrentCapacity() const
Definition: IPReassembly.h:420
pcpp::IPReassembly::OUT_OF_ORDER_FRAGMENT
@ OUT_OF_ORDER_FRAGMENT
Definition: IPReassembly.h:312
pcpp::LRUList::getMaxSize
size_t getMaxSize() const
Definition: LRUList.h:120
pcpp::IPReassembly::OnFragmentsClean
void(* OnFragmentsClean)(const PacketKey *key, void *userCookie)
Definition: IPReassembly.h:296
pcpp::IPReassembly::IPv4PacketKey::clone
PacketKey * clone() const
Definition: IPReassembly.h:187
pcpp::IPReassembly::IPv4PacketKey::IPv4PacketKey
IPv4PacketKey(const IPv4PacketKey &other)
Definition: IPReassembly.h:130
pcpp::IPReassembly::PacketKey
Definition: IPReassembly.h:72
pcpp::IPReassembly::IPv4PacketKey::IPv4PacketKey
IPv4PacketKey(uint16_t ipid, IPv4Address srcip, IPv4Address dstip)
Definition: IPReassembly.h:124
pcpp::IPReassembly::PacketKey::getProtocolType
virtual ProtocolType getProtocolType() const =0
pcpp::IPReassembly::IPv4PacketKey::setIpID
void setIpID(uint16_t ipID)
Definition: IPReassembly.h:163
pcpp::IPReassembly::PacketKey::getHashValue
virtual uint32_t getHashValue() const =0
pcpp::IPReassembly::IPv4PacketKey::operator=
IPv4PacketKey & operator=(const IPv4PacketKey &other)
Definition: IPReassembly.h:136
pcpp::IPReassembly::IPv6PacketKey::getProtocolType
ProtocolType getProtocolType() const
Definition: IPReassembly.h:277
pcpp::IPReassembly::FIRST_FRAGMENT
@ FIRST_FRAGMENT
Definition: IPReassembly.h:308
pcpp::ProtocolType
uint64_t ProtocolType
Definition: ProtocolType.h:18
pcpp::IPReassembly::IPv6PacketKey::IPv6PacketKey
IPv6PacketKey(const IPv6PacketKey &other)
Definition: IPReassembly.h:222
pcpp::OsiModelLayerUnknown
@ OsiModelLayerUnknown
Definition: ProtocolType.h:250
pcpp::IPReassembly::IPv4PacketKey::getIpID
uint16_t getIpID() const
Definition: IPReassembly.h:147
pcpp::IPReassembly::IPv6PacketKey::IPv6PacketKey
IPv6PacketKey(uint32_t fragmentID, IPv6Address srcip, IPv6Address dstip)
Definition: IPReassembly.h:216
pcpp::UnknownProtocol
const ProtocolType UnknownProtocol
Definition: ProtocolType.h:23
pcpp::IPReassembly::IPv6PacketKey::IPv6PacketKey
IPv6PacketKey()
Definition: IPReassembly.h:208
pcpp::IPReassembly::~IPReassembly
~IPReassembly()
pcpp::IPReassembly::IPv4PacketKey::setDstIP
void setDstIP(const IPv4Address &dstIP)
Definition: IPReassembly.h:175
PointerVector.h
pcpp::IPReassembly::IPv6PacketKey::getHashValue
uint32_t getHashValue() const
pcpp::IPReassembly::PacketKey::clone
virtual PacketKey * clone() const =0
pcpp::IPReassembly::ReassemblyStatus
ReassemblyStatus
Definition: IPReassembly.h:301
pcpp::IPReassembly::IPv4PacketKey::getHashValue
uint32_t getHashValue() const
pcpp::IPReassembly::IPv4PacketKey::getDstIP
IPv4Address getDstIP() const
Definition: IPReassembly.h:157
IpAddress.h
pcpp
The main namespace for the PcapPlusPlus lib.
LRUList.h
pcpp::IPReassembly::IPv4PacketKey::getProtocolType
ProtocolType getProtocolType() const
Definition: IPReassembly.h:185
pcpp::IPReassembly::REASSEMBLED
@ REASSEMBLED
Definition: IPReassembly.h:316
PCPP_IP_REASSEMBLY_DEFAULT_MAX_PACKETS_TO_STORE
#define PCPP_IP_REASSEMBLY_DEFAULT_MAX_PACKETS_TO_STORE
Definition: IPReassembly.h:50
pcpp::IPReassembly::IPv4PacketKey::IPv4PacketKey
IPv4PacketKey()
Definition: IPReassembly.h:116
pcpp::IPv4
const ProtocolType IPv4
Definition: ProtocolType.h:33
pcpp::IPReassembly::IPv6PacketKey::setDstIP
void setDstIP(const IPv6Address &dstIP)
Definition: IPReassembly.h:267
pcpp::IPv6
const ProtocolType IPv6
Definition: ProtocolType.h:38
pcpp::IPReassembly::IPv6PacketKey::getSrcIP
IPv6Address getSrcIP() const
Definition: IPReassembly.h:244
Packet.h
pcpp::IPReassembly::PacketKey::~PacketKey
virtual ~PacketKey()
Definition: IPReassembly.h:79
pcpp::IPReassembly::getMaxCapacity
size_t getMaxCapacity() const
Definition: IPReassembly.h:415
pcpp::IPReassembly::FRAGMENT
@ FRAGMENT
Definition: IPReassembly.h:310
pcpp::IPReassembly::IPv6PacketKey::getDstIP
IPv6Address getDstIP() const
Definition: IPReassembly.h:249
pcpp::IPReassembly::NON_IP_PACKET
@ NON_IP_PACKET
Definition: IPReassembly.h:304
pcpp::IPReassembly::IPv6PacketKey::setFragmentID
void setFragmentID(uint32_t fragID)
Definition: IPReassembly.h:255
pcpp::IPReassembly::IPReassembly
IPReassembly(OnFragmentsClean onFragmentsCleanCallback=NULL, void *callbackUserCookie=NULL, size_t maxPacketsToStore=500000)
Definition: IPReassembly.h:327
pcpp::IPReassembly::getCurrentPacket
Packet * getCurrentPacket(const PacketKey &key)
pcpp::IPReassembly
Definition: IPReassembly.h:63
pcpp::RawPacket
Definition: RawPacket.h:252
pcpp::IPReassembly::IPv6PacketKey::operator=
IPv6PacketKey & operator=(const IPv6PacketKey &other)
Definition: IPReassembly.h:228
pcpp::IPReassembly::IPv4PacketKey::setSrcIP
void setSrcIP(const IPv4Address &srcIP)
Definition: IPReassembly.h:169
pcpp::IPReassembly::IPv4PacketKey::getSrcIP
IPv4Address getSrcIP() const
Definition: IPReassembly.h:152