PcapPlusPlus  Next
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 <chrono>
8 #include <map>
9 #include <list>
10 #include <time.h>
11 
91 namespace pcpp
92 {
93 
99  {
105  uint16_t srcPort;
107  uint16_t dstPort;
109  uint32_t flowKey;
111  timeval startTime;
113  timeval endTime;
115  std::chrono::time_point<std::chrono::high_resolution_clock> startTimePrecise;
117  std::chrono::time_point<std::chrono::high_resolution_clock> endTimePrecise;
118 
123  {}
124 
129  void setStartTime(const std::chrono::time_point<std::chrono::high_resolution_clock>& startTimeValue);
130 
135  void setEndTime(const std::chrono::time_point<std::chrono::high_resolution_clock>& endTimeValue);
136  };
137 
138  class TcpReassembly;
139 
147  {
148  public:
157  TcpStreamData(const uint8_t* tcpData, size_t tcpDataLength, size_t missingBytes, const ConnectionData& connData,
158  std::chrono::time_point<std::chrono::high_resolution_clock> timestamp)
159  : m_Data(tcpData), m_DataLen(tcpDataLength), m_MissingBytes(missingBytes), m_Connection(connData),
160  m_Timestamp(timestamp)
161  {}
162 
167  const uint8_t* getData() const
168  {
169  return m_Data;
170  }
171 
176  size_t getDataLength() const
177  {
178  return m_DataLen;
179  }
180 
185  size_t getMissingByteCount() const
186  {
187  return m_MissingBytes;
188  }
189 
194  bool isBytesMissing() const
195  {
196  return getMissingByteCount() > 0;
197  }
198 
204  {
205  return m_Connection;
206  }
207 
211  timeval getTimeStamp() const;
212 
216  std::chrono::time_point<std::chrono::high_resolution_clock> getTimeStampPrecise() const
217  {
218  return m_Timestamp;
219  }
220 
221  private:
222  const uint8_t* m_Data;
223  size_t m_DataLen;
224  size_t m_MissingBytes;
225  const ConnectionData& m_Connection;
226  std::chrono::time_point<std::chrono::high_resolution_clock> m_Timestamp;
227  };
228 
234  {
237 
243 
248  uint32_t maxNumToClean;
249 
255 
259 
274  uint32_t maxNumToClean = 30, uint32_t maxOutOfOrderFragments = 0,
275  bool enableBaseBufferClearCondition = true)
279  {}
280  };
281 
288  {
289  public:
294  {
299  };
300 
305  {
368  };
369 
373  typedef std::unordered_map<uint32_t, ConnectionData> ConnectionInfoList;
374 
384  typedef void (*OnTcpMessageReady)(int8_t side, const TcpStreamData& tcpData, void* userCookie);
385 
393  typedef void (*OnTcpConnectionStart)(const ConnectionData& connectionData, void* userCookie);
394 
403  typedef void (*OnTcpConnectionEnd)(const ConnectionData& connectionData, ConnectionEndReason reason,
404  void* userCookie);
405 
418  explicit TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void* userCookie = nullptr,
419  OnTcpConnectionStart onConnectionStartCallback = nullptr,
420  OnTcpConnectionEnd onConnectionEndCallback = nullptr,
422 
432 
442 
449  void closeConnection(uint32_t flowKey);
450 
456 
463  {
464  return m_ConnectionInfo;
465  }
466 
473  int isConnectionOpen(const ConnectionData& connection) const;
474 
481  uint32_t purgeClosedConnections(uint32_t maxNumToClean = 0);
482 
483  private:
484  struct TcpFragment
485  {
486  uint32_t sequence;
487  size_t dataLength;
488  uint8_t* data;
489  std::chrono::time_point<std::chrono::high_resolution_clock> timestamp;
490 
491  TcpFragment() : sequence(0), dataLength(0), data(nullptr)
492  {}
493  ~TcpFragment()
494  {
495  delete[] data;
496  }
497  };
498 
499  struct TcpOneSideData
500  {
501  IPAddress srcIP;
502  uint16_t srcPort;
503  uint32_t sequence;
504  PointerVector<TcpFragment> tcpFragmentList;
505  bool gotFinOrRst;
506 
507  TcpOneSideData() : srcPort(0), sequence(0), gotFinOrRst(false)
508  {}
509  };
510 
511  struct TcpReassemblyData
512  {
513  bool closed;
514  int8_t numOfSides;
515  int8_t prevSide;
516  TcpOneSideData twoSides[2];
517  ConnectionData connData;
518 
519  TcpReassemblyData() : closed(false), numOfSides(0), prevSide(-1)
520  {}
521  };
522 
523  class OutOfOrderProcessingGuard
524  {
525  private:
526  bool& m_Flag;
527 
528  public:
529  explicit OutOfOrderProcessingGuard(bool& flag) : m_Flag(flag)
530  {
531  m_Flag = true;
532  }
533 
534  ~OutOfOrderProcessingGuard()
535  {
536  m_Flag = false;
537  }
538 
539  // Disable copy and move operations
540  OutOfOrderProcessingGuard(const OutOfOrderProcessingGuard&) = delete;
541  OutOfOrderProcessingGuard& operator=(const OutOfOrderProcessingGuard&) = delete;
542  };
543 
544  typedef std::unordered_map<uint32_t, TcpReassemblyData> ConnectionList;
545  typedef std::map<time_t, std::list<uint32_t>> CleanupList;
546 
547  OnTcpMessageReady m_OnMessageReadyCallback;
548  OnTcpConnectionStart m_OnConnStart;
549  OnTcpConnectionEnd m_OnConnEnd;
550  void* m_UserCookie;
551  ConnectionList m_ConnectionList;
552  ConnectionInfoList m_ConnectionInfo;
553  CleanupList m_CleanupList;
554  bool m_RemoveConnInfo;
555  uint32_t m_ClosedConnectionDelay;
556  uint32_t m_MaxNumToClean;
557  size_t m_MaxOutOfOrderFragments;
558  time_t m_PurgeTimepoint;
559  bool m_EnableBaseBufferClearCondition;
560  bool m_ProcessingOutOfOrder = false;
561 
562  void checkOutOfOrderFragments(TcpReassemblyData* tcpReassemblyData, int8_t sideIndex, bool cleanWholeFragList);
563 
564  void handleFinOrRst(TcpReassemblyData* tcpReassemblyData, int8_t sideIndex, uint32_t flowKey, bool isRst);
565 
566  void closeConnectionInternal(uint32_t flowKey, ConnectionEndReason reason);
567 
568  void insertIntoCleanupList(uint32_t flowKey);
569  };
570 
571 } // namespace pcpp
Definition: IpAddress.h:358
Definition: Packet.h:27
Definition: RawPacket.h:269
Definition: TcpReassembly.h:288
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:305
@ NonIpPacket
Definition: TcpReassembly.h:356
@ FIN_RSTWithNoData
Definition: TcpReassembly.h:335
@ Error_PacketDoesNotMatchFlow
Definition: TcpReassembly.h:367
@ Ignore_PacketOfClosedFlow
Definition: TcpReassembly.h:346
@ NonTcpPacket
Definition: TcpReassembly.h:361
@ Ignore_Retransimission
Definition: TcpReassembly.h:351
@ TcpMessageHandled
Definition: TcpReassembly.h:317
@ OutOfOrderTcpMessageBuffered
Definition: TcpReassembly.h:329
@ Ignore_PacketWithNoData
Definition: TcpReassembly.h:341
const ConnectionInfoList & getConnectionInformation() const
Definition: TcpReassembly.h:462
void(* OnTcpConnectionStart)(const ConnectionData &connectionData, void *userCookie)
Definition: TcpReassembly.h:393
uint32_t purgeClosedConnections(uint32_t maxNumToClean=0)
ReassemblyStatus reassemblePacket(Packet &tcpData)
std::unordered_map< uint32_t, ConnectionData > ConnectionInfoList
Definition: TcpReassembly.h:373
void(* OnTcpMessageReady)(int8_t side, const TcpStreamData &tcpData, void *userCookie)
Definition: TcpReassembly.h:384
void closeConnection(uint32_t flowKey)
ConnectionEndReason
Definition: TcpReassembly.h:294
@ TcpReassemblyConnectionClosedByFIN_RST
Definition: TcpReassembly.h:296
@ TcpReassemblyConnectionClosedManually
Definition: TcpReassembly.h:298
void(* OnTcpConnectionEnd)(const ConnectionData &connectionData, ConnectionEndReason reason, void *userCookie)
Definition: TcpReassembly.h:403
Definition: TcpReassembly.h:147
bool isBytesMissing() const
Definition: TcpReassembly.h:194
size_t getDataLength() const
Definition: TcpReassembly.h:176
timeval getTimeStamp() const
size_t getMissingByteCount() const
Definition: TcpReassembly.h:185
TcpStreamData(const uint8_t *tcpData, size_t tcpDataLength, size_t missingBytes, const ConnectionData &connData, std::chrono::time_point< std::chrono::high_resolution_clock > timestamp)
Definition: TcpReassembly.h:157
std::chrono::time_point< std::chrono::high_resolution_clock > getTimeStampPrecise() const
Definition: TcpReassembly.h:216
const uint8_t * getData() const
Definition: TcpReassembly.h:167
const ConnectionData & getConnectionData() const
Definition: TcpReassembly.h:203
The main namespace for the PcapPlusPlus lib.
Definition: TcpReassembly.h:99
IPAddress srcIP
Definition: TcpReassembly.h:101
IPAddress dstIP
Definition: TcpReassembly.h:103
std::chrono::time_point< std::chrono::high_resolution_clock > startTimePrecise
Definition: TcpReassembly.h:115
timeval endTime
Definition: TcpReassembly.h:113
timeval startTime
Definition: TcpReassembly.h:111
void setStartTime(const std::chrono::time_point< std::chrono::high_resolution_clock > &startTimeValue)
std::chrono::time_point< std::chrono::high_resolution_clock > endTimePrecise
Definition: TcpReassembly.h:117
uint16_t dstPort
Definition: TcpReassembly.h:107
uint16_t srcPort
Definition: TcpReassembly.h:105
ConnectionData()
Definition: TcpReassembly.h:122
uint32_t flowKey
Definition: TcpReassembly.h:109
void setEndTime(const std::chrono::time_point< std::chrono::high_resolution_clock > &endTimeValue)
Definition: TcpReassembly.h:234
uint32_t closedConnectionDelay
Definition: TcpReassembly.h:242
uint32_t maxNumToClean
Definition: TcpReassembly.h:248
uint32_t maxOutOfOrderFragments
Definition: TcpReassembly.h:254
TcpReassemblyConfiguration(bool removeConnInfo=true, uint32_t closedConnectionDelay=5, uint32_t maxNumToClean=30, uint32_t maxOutOfOrderFragments=0, bool enableBaseBufferClearCondition=true)
Definition: TcpReassembly.h:273
bool enableBaseBufferClearCondition
Definition: TcpReassembly.h:258
bool removeConnInfo
Definition: TcpReassembly.h:236