PcapPlusPlus  19.12
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  uint16_t getIpID() const { return m_IpID; }
135 
139  IPv4Address getSrcIP() const { return m_SrcIP; }
140 
144  IPv4Address getDstIP() const { return m_DstIP; }
145 
150  void setIpID(uint16_t ipID) { m_IpID = ipID; }
151 
156  void setSrcIP(const IPv4Address& srcIP) { m_SrcIP = srcIP; }
157 
162  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  uint32_t getFragmentID() const { return m_FragmentID; }
214 
218  IPv6Address getSrcIP() const { return m_SrcIP; }
219 
223  IPv6Address getDstIP() const { return m_DstIP; }
224 
229  void setFragmentID(uint32_t fragID) { m_FragmentID = fragID; }
230 
235  void setSrcIP(const IPv6Address& srcIP) { m_SrcIP = srcIP; }
236 
241  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  : m_PacketLRU(maxPacketsToStore), m_OnFragmentsCleanCallback(onFragmentsCleanCallback), m_CallbackUserCookie(callbackUserCookie) {}
303 
307  ~IPReassembly();
308 
336  Packet* processPacket(Packet* fragment, ReassemblyStatus& status, ProtocolType parseUntil = UnknownProtocol, OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
337 
367 
377  Packet* getCurrentPacket(const PacketKey& key);
378 
384  void removePacket(const PacketKey& key);
385 
389  size_t getMaxCapacity() const { return m_PacketLRU.getMaxSize(); }
390 
394  size_t getCurrentCapacity() const { return m_FragmentMap.size(); }
395 
396  private:
397 
398  struct IPFragment
399  {
400  uint16_t fragmentOffset;
401  bool lastFragment;
402  uint8_t* fragmentData;
403  size_t fragmentDataLen;
404  IPFragment() { fragmentOffset = 0; lastFragment = false; fragmentData = NULL; fragmentDataLen = 0; }
405  ~IPFragment() { delete [] fragmentData; }
406  };
407 
408  struct IPFragmentData
409  {
410  uint16_t currentOffset;
411  RawPacket* data;
412  bool deleteData;
413  uint32_t fragmentID;
414  PacketKey* packetKey;
415  PointerVector<IPFragment> outOfOrderFragments;
416  IPFragmentData(PacketKey* pktKey, uint32_t fragId) { currentOffset = 0; data = NULL; deleteData = true; fragmentID = fragId; packetKey = pktKey; }
417  ~IPFragmentData() { delete packetKey; if (deleteData && data != NULL) { delete data; } }
418  };
419 
420  LRUList<uint32_t> m_PacketLRU;
421  std::map<uint32_t, IPFragmentData*> m_FragmentMap;
422  OnFragmentsClean m_OnFragmentsCleanCallback;
423  void* m_CallbackUserCookie;
424 
425  void addNewFragment(uint32_t hash, IPFragmentData* fragData);
426  bool matchOutOfOrderFragments(IPFragmentData* fragData);
427  };
428 
429 } // namespace pcpp
430 
431 #endif // PACKETPP_IP_REASSEMBLY
pcpp::IPReassembly::IPv6PacketKey::setSrcIP
void setSrcIP(const IPv6Address &srcIP)
Definition: IPReassembly.h:235
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:224
pcpp::IPv6
Definition: ProtocolType.h:36
pcpp::IPv4Address
Definition: IpAddress.h:119
pcpp::IPv6Address
Definition: IpAddress.h:229
pcpp::Packet
Definition: Packet.h:26
pcpp::IPReassembly::IPv6PacketKey
Definition: IPReassembly.h:188
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:213
pcpp::IPReassembly::NON_FRAGMENT
Definition: IPReassembly.h:280
pcpp::IPReassembly::MALFORMED_FRAGMENT
Definition: IPReassembly.h:288
pcpp::IPReassembly::removePacket
void removePacket(const PacketKey &key)
pcpp::IPReassembly::IPv6PacketKey::clone
PacketKey * clone() const
Definition: IPReassembly.h:253
pcpp::IPReassembly::IPv4PacketKey
Definition: IPReassembly.h:109
pcpp::IPReassembly::getCurrentCapacity
size_t getCurrentCapacity() const
Definition: IPReassembly.h:394
pcpp::IPReassembly::OUT_OF_ORDER_FRAGMENT
Definition: IPReassembly.h:286
pcpp::LRUList::getMaxSize
size_t getMaxSize() const
Definition: LRUList.h:120
pcpp::IPReassembly::OnFragmentsClean
void(* OnFragmentsClean)(const PacketKey *key, void *userCookie)
Definition: IPReassembly.h:270
pcpp::IPReassembly::IPv4PacketKey::clone
PacketKey * clone() const
Definition: IPReassembly.h:174
pcpp::IPReassembly::IPv4PacketKey::IPv4PacketKey
IPv4PacketKey(const IPv4PacketKey &other)
Definition: IPReassembly.h:129
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:150
pcpp::IPReassembly::PacketKey::getHashValue
virtual uint32_t getHashValue() const =0
pcpp::IPReassembly::IPv6PacketKey::getProtocolType
ProtocolType getProtocolType() const
Definition: IPReassembly.h:251
pcpp::IPReassembly::FIRST_FRAGMENT
Definition: IPReassembly.h:282
pcpp::ProtocolType
ProtocolType
Definition: ProtocolType.h:16
pcpp::IPReassembly::IPv6PacketKey::IPv6PacketKey
IPv6PacketKey(const IPv6PacketKey &other)
Definition: IPReassembly.h:208
pcpp::OsiModelLayerUnknown
Definition: ProtocolType.h:241
pcpp::IPReassembly::IPv4PacketKey::getIpID
uint16_t getIpID() const
Definition: IPReassembly.h:134
pcpp::IPReassembly::IPv6PacketKey::IPv6PacketKey
IPv6PacketKey(uint32_t fragmentID, IPv6Address srcip, IPv6Address dstip)
Definition: IPReassembly.h:203
pcpp::IPReassembly::IPv6PacketKey::IPv6PacketKey
IPv6PacketKey()
Definition: IPReassembly.h:195
pcpp::IPReassembly::~IPReassembly
~IPReassembly()
pcpp::IPReassembly::IPv4PacketKey::setDstIP
void setDstIP(const IPv4Address &dstIP)
Definition: IPReassembly.h:162
PointerVector.h
pcpp::IPReassembly::IPv6PacketKey::getHashValue
uint32_t getHashValue() const
pcpp::IPv4
Definition: ProtocolType.h:31
pcpp::IPReassembly::PacketKey::clone
virtual PacketKey * clone() const =0
pcpp::IPReassembly::ReassemblyStatus
ReassemblyStatus
Definition: IPReassembly.h:275
pcpp::IPReassembly::IPv4PacketKey::getHashValue
uint32_t getHashValue() const
pcpp::IPReassembly::IPv4PacketKey::getDstIP
IPv4Address getDstIP() const
Definition: IPReassembly.h:144
IpAddress.h
pcpp
The main namespace for the PcapPlusPlus lib.
LRUList.h
pcpp::IPReassembly::IPv4PacketKey::getProtocolType
ProtocolType getProtocolType() const
Definition: IPReassembly.h:172
pcpp::UnknownProtocol
Definition: ProtocolType.h:21
pcpp::IPReassembly::REASSEMBLED
Definition: IPReassembly.h:290
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::IPReassembly::IPv6PacketKey::setDstIP
void setDstIP(const IPv6Address &dstIP)
Definition: IPReassembly.h:241
pcpp::IPReassembly::IPv6PacketKey::getSrcIP
IPv6Address getSrcIP() const
Definition: IPReassembly.h:218
Packet.h
pcpp::IPReassembly::PacketKey::~PacketKey
virtual ~PacketKey()
Definition: IPReassembly.h:79
pcpp::IPReassembly::getMaxCapacity
size_t getMaxCapacity() const
Definition: IPReassembly.h:389
pcpp::IPReassembly::FRAGMENT
Definition: IPReassembly.h:284
pcpp::IPReassembly::IPv6PacketKey::getDstIP
IPv6Address getDstIP() const
Definition: IPReassembly.h:223
pcpp::IPReassembly::NON_IP_PACKET
Definition: IPReassembly.h:278
pcpp::IPReassembly::IPv6PacketKey::setFragmentID
void setFragmentID(uint32_t fragID)
Definition: IPReassembly.h:229
pcpp::IPReassembly::IPReassembly
IPReassembly(OnFragmentsClean onFragmentsCleanCallback=NULL, void *callbackUserCookie=NULL, size_t maxPacketsToStore=500000)
Definition: IPReassembly.h:301
pcpp::IPReassembly::getCurrentPacket
Packet * getCurrentPacket(const PacketKey &key)
pcpp::IPReassembly
Definition: IPReassembly.h:63
pcpp::RawPacket
Definition: RawPacket.h:219
pcpp::IPReassembly::IPv4PacketKey::setSrcIP
void setSrcIP(const IPv4Address &srcIP)
Definition: IPReassembly.h:156
pcpp::IPReassembly::IPv4PacketKey::getSrcIP
IPv4Address getSrcIP() const
Definition: IPReassembly.h:139