PcapPlusPlus  24.09
TcpReassembly.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Packet.h"
4 #include "IpAddress.h"
5 #include "PointerVector.h"
6 #include <unordered_map>
7 #include <map>
8 #include <list>
9 #include <time.h>
10 
90 namespace pcpp
91 {
92 
98  {
104  uint16_t srcPort;
106  uint16_t dstPort;
108  uint32_t flowKey;
110  timeval startTime;
112  timeval endTime;
113 
118  {}
119 
124  void setStartTime(const timeval& startTimeValue)
125  {
126  startTime = startTimeValue;
127  }
128 
133  void setEndTime(const timeval& endTimeValue)
134  {
135  endTime = endTimeValue;
136  }
137  };
138 
139  class TcpReassembly;
140 
148  {
149  public:
158  TcpStreamData(const uint8_t* tcpData, size_t tcpDataLength, size_t missingBytes, const ConnectionData& connData,
159  timeval timestamp)
160  : m_Data(tcpData), m_DataLen(tcpDataLength), m_MissingBytes(missingBytes), m_Connection(connData),
161  m_Timestamp(timestamp)
162  {}
163 
168  const uint8_t* getData() const
169  {
170  return m_Data;
171  }
172 
177  size_t getDataLength() const
178  {
179  return m_DataLen;
180  }
181 
186  size_t getMissingByteCount() const
187  {
188  return m_MissingBytes;
189  }
190 
195  bool isBytesMissing() const
196  {
197  return getMissingByteCount() > 0;
198  }
199 
205  {
206  return m_Connection;
207  }
208 
213  timeval getTimeStamp() const
214  {
215  return m_Timestamp;
216  }
217 
218  private:
219  const uint8_t* m_Data;
220  size_t m_DataLen;
221  size_t m_MissingBytes;
222  const ConnectionData& m_Connection;
223  timeval m_Timestamp;
224  };
225 
231  {
234 
240 
245  uint32_t maxNumToClean;
246 
252 
256 
271  uint32_t maxNumToClean = 30, uint32_t maxOutOfOrderFragments = 0,
272  bool enableBaseBufferClearCondition = true)
276  {}
277  };
278 
285  {
286  public:
291  {
296  };
297 
302  {
365  };
366 
370  typedef std::unordered_map<uint32_t, ConnectionData> ConnectionInfoList;
371 
381  typedef void (*OnTcpMessageReady)(int8_t side, const TcpStreamData& tcpData, void* userCookie);
382 
390  typedef void (*OnTcpConnectionStart)(const ConnectionData& connectionData, void* userCookie);
391 
400  typedef void (*OnTcpConnectionEnd)(const ConnectionData& connectionData, ConnectionEndReason reason,
401  void* userCookie);
402 
415  explicit TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void* userCookie = nullptr,
416  OnTcpConnectionStart onConnectionStartCallback = nullptr,
417  OnTcpConnectionEnd onConnectionEndCallback = nullptr,
419 
429 
439 
446  void closeConnection(uint32_t flowKey);
447 
453 
460  {
461  return m_ConnectionInfo;
462  }
463 
470  int isConnectionOpen(const ConnectionData& connection) const;
471 
478  uint32_t purgeClosedConnections(uint32_t maxNumToClean = 0);
479 
480  private:
481  struct TcpFragment
482  {
483  uint32_t sequence;
484  size_t dataLength;
485  uint8_t* data;
486  timeval timestamp;
487 
488  TcpFragment() : sequence(0), dataLength(0), data(nullptr)
489  {}
490  ~TcpFragment()
491  {
492  delete[] data;
493  }
494  };
495 
496  struct TcpOneSideData
497  {
498  IPAddress srcIP;
499  uint16_t srcPort;
500  uint32_t sequence;
501  PointerVector<TcpFragment> tcpFragmentList;
502  bool gotFinOrRst;
503 
504  TcpOneSideData() : srcPort(0), sequence(0), gotFinOrRst(false)
505  {}
506  };
507 
508  struct TcpReassemblyData
509  {
510  bool closed;
511  int8_t numOfSides;
512  int8_t prevSide;
513  TcpOneSideData twoSides[2];
514  ConnectionData connData;
515 
516  TcpReassemblyData() : closed(false), numOfSides(0), prevSide(-1)
517  {}
518  };
519 
520  class OutOfOrderProcessingGuard
521  {
522  private:
523  bool& m_Flag;
524 
525  public:
526  explicit OutOfOrderProcessingGuard(bool& flag) : m_Flag(flag)
527  {
528  m_Flag = true;
529  }
530 
531  ~OutOfOrderProcessingGuard()
532  {
533  m_Flag = false;
534  }
535 
536  // Disable copy and move operations
537  OutOfOrderProcessingGuard(const OutOfOrderProcessingGuard&) = delete;
538  OutOfOrderProcessingGuard& operator=(const OutOfOrderProcessingGuard&) = delete;
539  };
540 
541  typedef std::unordered_map<uint32_t, TcpReassemblyData> ConnectionList;
542  typedef std::map<time_t, std::list<uint32_t>> CleanupList;
543 
544  OnTcpMessageReady m_OnMessageReadyCallback;
545  OnTcpConnectionStart m_OnConnStart;
546  OnTcpConnectionEnd m_OnConnEnd;
547  void* m_UserCookie;
548  ConnectionList m_ConnectionList;
549  ConnectionInfoList m_ConnectionInfo;
550  CleanupList m_CleanupList;
551  bool m_RemoveConnInfo;
552  uint32_t m_ClosedConnectionDelay;
553  uint32_t m_MaxNumToClean;
554  size_t m_MaxOutOfOrderFragments;
555  time_t m_PurgeTimepoint;
556  bool m_EnableBaseBufferClearCondition;
557  bool m_ProcessingOutOfOrder = false;
558 
559  void checkOutOfOrderFragments(TcpReassemblyData* tcpReassemblyData, int8_t sideIndex, bool cleanWholeFragList);
560 
561  void handleFinOrRst(TcpReassemblyData* tcpReassemblyData, int8_t sideIndex, uint32_t flowKey, bool isRst);
562 
563  void closeConnectionInternal(uint32_t flowKey, ConnectionEndReason reason);
564 
565  void insertIntoCleanupList(uint32_t flowKey);
566  };
567 
568 } // namespace pcpp
Definition: IpAddress.h:358
Definition: Packet.h:27
Definition: RawPacket.h:269
Definition: TcpReassembly.h:285
ReassemblyStatus reassemblePacket(RawPacket *tcpRawData)
int isConnectionOpen(const ConnectionData &connection) const
TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void *userCookie=nullptr, OnTcpConnectionStart onConnectionStartCallback=nullptr, OnTcpConnectionEnd onConnectionEndCallback=nullptr, const TcpReassemblyConfiguration &config=TcpReassemblyConfiguration())
ReassemblyStatus
Definition: TcpReassembly.h:302
@ NonIpPacket
Definition: TcpReassembly.h:353
@ FIN_RSTWithNoData
Definition: TcpReassembly.h:332
@ Error_PacketDoesNotMatchFlow
Definition: TcpReassembly.h:364
@ Ignore_PacketOfClosedFlow
Definition: TcpReassembly.h:343
@ NonTcpPacket
Definition: TcpReassembly.h:358
@ Ignore_Retransimission
Definition: TcpReassembly.h:348
@ TcpMessageHandled
Definition: TcpReassembly.h:314
@ OutOfOrderTcpMessageBuffered
Definition: TcpReassembly.h:326
@ Ignore_PacketWithNoData
Definition: TcpReassembly.h:338
const ConnectionInfoList & getConnectionInformation() const
Definition: TcpReassembly.h:459
void(* OnTcpConnectionStart)(const ConnectionData &connectionData, void *userCookie)
Definition: TcpReassembly.h:390
uint32_t purgeClosedConnections(uint32_t maxNumToClean=0)
ReassemblyStatus reassemblePacket(Packet &tcpData)
std::unordered_map< uint32_t, ConnectionData > ConnectionInfoList
Definition: TcpReassembly.h:370
void(* OnTcpMessageReady)(int8_t side, const TcpStreamData &tcpData, void *userCookie)
Definition: TcpReassembly.h:381
void closeConnection(uint32_t flowKey)
ConnectionEndReason
Definition: TcpReassembly.h:291
@ TcpReassemblyConnectionClosedByFIN_RST
Definition: TcpReassembly.h:293
@ TcpReassemblyConnectionClosedManually
Definition: TcpReassembly.h:295
void(* OnTcpConnectionEnd)(const ConnectionData &connectionData, ConnectionEndReason reason, void *userCookie)
Definition: TcpReassembly.h:400
Definition: TcpReassembly.h:148
TcpStreamData(const uint8_t *tcpData, size_t tcpDataLength, size_t missingBytes, const ConnectionData &connData, timeval timestamp)
Definition: TcpReassembly.h:158
bool isBytesMissing() const
Definition: TcpReassembly.h:195
size_t getDataLength() const
Definition: TcpReassembly.h:177
timeval getTimeStamp() const
Definition: TcpReassembly.h:213
size_t getMissingByteCount() const
Definition: TcpReassembly.h:186
const uint8_t * getData() const
Definition: TcpReassembly.h:168
const ConnectionData & getConnectionData() const
Definition: TcpReassembly.h:204
The main namespace for the PcapPlusPlus lib.
Definition: TcpReassembly.h:98
IPAddress srcIP
Definition: TcpReassembly.h:100
IPAddress dstIP
Definition: TcpReassembly.h:102
timeval endTime
Definition: TcpReassembly.h:112
void setEndTime(const timeval &endTimeValue)
Definition: TcpReassembly.h:133
timeval startTime
Definition: TcpReassembly.h:110
uint16_t dstPort
Definition: TcpReassembly.h:106
uint16_t srcPort
Definition: TcpReassembly.h:104
void setStartTime(const timeval &startTimeValue)
Definition: TcpReassembly.h:124
ConnectionData()
Definition: TcpReassembly.h:117
uint32_t flowKey
Definition: TcpReassembly.h:108
Definition: TcpReassembly.h:231
uint32_t closedConnectionDelay
Definition: TcpReassembly.h:239
uint32_t maxNumToClean
Definition: TcpReassembly.h:245
uint32_t maxOutOfOrderFragments
Definition: TcpReassembly.h:251
TcpReassemblyConfiguration(bool removeConnInfo=true, uint32_t closedConnectionDelay=5, uint32_t maxNumToClean=30, uint32_t maxOutOfOrderFragments=0, bool enableBaseBufferClearCondition=true)
Definition: TcpReassembly.h:270
bool enableBaseBufferClearCondition
Definition: TcpReassembly.h:255
bool removeConnInfo
Definition: TcpReassembly.h:233