PcapPlusPlus  20.08
TcpReassembly.h
Go to the documentation of this file.
1 #ifndef PACKETPP_TCP_REASSEMBLY
2 #define PACKETPP_TCP_REASSEMBLY
3 
4 #include "Packet.h"
5 #include "IpAddress.h"
6 #include "PointerVector.h"
7 #include <map>
8 #include <list>
9 #include <time.h>
10 
11 
71 namespace pcpp
72 {
73 
79 {
85  uint16_t srcPort;
87  uint16_t dstPort;
89  uint32_t flowKey;
91  timeval startTime;
93  timeval endTime;
94 
99 
104  void setStartTime(const timeval &startTime) { this->startTime = startTime; }
105 
110  void setEndTime(const timeval &endTime) { this->endTime = endTime; }
111 };
112 
113 
114 class TcpReassembly;
115 
116 
123 {
124 public:
131  TcpStreamData(const uint8_t* tcpData, size_t tcpDataLength, const ConnectionData& connData)
132  : m_Data(tcpData), m_DataLen(tcpDataLength), m_Connection(connData)
133  {
134  }
135 
140  const uint8_t* getData() const { return m_Data; }
141 
146  size_t getDataLength() const { return m_DataLen; }
147 
152  const ConnectionData& getConnectionData() const { return m_Connection; }
153 
154 private:
155  const uint8_t* m_Data;
156  size_t m_DataLen;
157  const ConnectionData& m_Connection;
158 };
159 
160 
166 {
169 
174 
178  uint32_t maxNumToClean;
179 
188  {
189  }
190 };
191 
192 
198 {
199 public:
200 
205  {
210  };
211 
216  {
272  };
273 
277  typedef std::map<uint32_t, ConnectionData> ConnectionInfoList;
278 
286  typedef void (*OnTcpMessageReady)(int8_t side, const TcpStreamData& tcpData, void* userCookie);
287 
294  typedef void (*OnTcpConnectionStart)(const ConnectionData& connectionData, void* userCookie);
295 
303  typedef void (*OnTcpConnectionEnd)(const ConnectionData& connectionData, ConnectionEndReason reason, void* userCookie);
304 
313  TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void* userCookie = NULL, OnTcpConnectionStart onConnectionStartCallback = NULL, OnTcpConnectionEnd onConnectionEndCallback = NULL, const TcpReassemblyConfiguration &config = TcpReassemblyConfiguration());
314 
322 
330 
336  void closeConnection(uint32_t flowKey);
337 
342  void closeAllConnections();
343 
348  const ConnectionInfoList& getConnectionInformation() const { return m_ConnectionInfo; }
349 
355  int isConnectionOpen(const ConnectionData& connection) const;
356 
362  uint32_t purgeClosedConnections(uint32_t maxNumToClean = 0);
363 
364 private:
365  struct TcpFragment
366  {
367  uint32_t sequence;
368  size_t dataLength;
369  uint8_t* data;
370 
371  TcpFragment() : sequence(0), dataLength(0), data(NULL) {}
372  ~TcpFragment() { delete [] data; }
373  };
374 
375  struct TcpOneSideData
376  {
377  IPAddress srcIP;
378  uint16_t srcPort;
379  uint32_t sequence;
380  PointerVector<TcpFragment> tcpFragmentList;
381  bool gotFinOrRst;
382 
383  TcpOneSideData() : srcPort(0), sequence(0), gotFinOrRst(false) {}
384  };
385 
386  struct TcpReassemblyData
387  {
388  bool closed;
389  int8_t numOfSides;
390  int8_t prevSide;
391  TcpOneSideData twoSides[2];
392  ConnectionData connData;
393 
394  TcpReassemblyData() : closed(false), numOfSides(0), prevSide(-1) {}
395  };
396 
397  typedef std::map<uint32_t, TcpReassemblyData> ConnectionList;
398  typedef std::map<time_t, std::list<uint32_t> > CleanupList;
399 
400  OnTcpMessageReady m_OnMessageReadyCallback;
401  OnTcpConnectionStart m_OnConnStart;
402  OnTcpConnectionEnd m_OnConnEnd;
403  void* m_UserCookie;
404  ConnectionList m_ConnectionList;
405  ConnectionInfoList m_ConnectionInfo;
406  CleanupList m_CleanupList;
407  bool m_RemoveConnInfo;
408  uint32_t m_ClosedConnectionDelay;
409  uint32_t m_MaxNumToClean;
410  time_t m_PurgeTimepoint;
411 
412  void checkOutOfOrderFragments(TcpReassemblyData* tcpReassemblyData, int8_t sideIndex, bool cleanWholeFragList);
413 
414  void handleFinOrRst(TcpReassemblyData* tcpReassemblyData, int8_t sideIndex, uint32_t flowKey);
415 
416  void closeConnectionInternal(uint32_t flowKey, ConnectionEndReason reason);
417 
418  void insertIntoCleanupList(uint32_t flowKey);
419 };
420 
421 }
422 
423 #endif /* PACKETPP_TCP_REASSEMBLY */
pcpp::TcpStreamData::getData
const uint8_t * getData() const
Definition: TcpReassembly.h:140
pcpp::Packet
Definition: Packet.h:26
pcpp::TcpReassembly::closeConnection
void closeConnection(uint32_t flowKey)
pcpp::TcpReassembly::ConnectionEndReason
ConnectionEndReason
Definition: TcpReassembly.h:204
pcpp::ConnectionData::ConnectionData
ConnectionData()
Definition: TcpReassembly.h:98
pcpp::TcpReassembly::Ignore_PacketWithNoData
@ Ignore_PacketWithNoData
Definition: TcpReassembly.h:245
pcpp::ConnectionData::setEndTime
void setEndTime(const timeval &endTime)
Definition: TcpReassembly.h:110
pcpp::TcpReassembly::reassemblePacket
ReassemblyStatus reassemblePacket(Packet &tcpData)
pcpp::ConnectionData::setStartTime
void setStartTime(const timeval &startTime)
Definition: TcpReassembly.h:104
pcpp::TcpReassembly::OnTcpMessageReady
void(* OnTcpMessageReady)(int8_t side, const TcpStreamData &tcpData, void *userCookie)
Definition: TcpReassembly.h:286
pcpp::TcpReassembly::Ignore_PacketOfClosedFlow
@ Ignore_PacketOfClosedFlow
Definition: TcpReassembly.h:250
pcpp::ConnectionData
Definition: TcpReassembly.h:78
pcpp::TcpReassemblyConfiguration::closedConnectionDelay
uint32_t closedConnectionDelay
Definition: TcpReassembly.h:173
pcpp::IPAddress
‍**
Definition: IpAddress.h:236
pcpp::TcpReassembly::NonTcpPacket
@ NonTcpPacket
Definition: TcpReassembly.h:265
pcpp::TcpReassemblyConfiguration::TcpReassemblyConfiguration
TcpReassemblyConfiguration(bool removeConnInfo=true, uint32_t closedConnectionDelay=5, uint32_t maxNumToClean=30)
Definition: TcpReassembly.h:186
pcpp::TcpReassembly::closeAllConnections
void closeAllConnections()
pcpp::TcpReassembly::TcpReassembly
TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void *userCookie=NULL, OnTcpConnectionStart onConnectionStartCallback=NULL, OnTcpConnectionEnd onConnectionEndCallback=NULL, const TcpReassemblyConfiguration &config=TcpReassemblyConfiguration())
pcpp::TcpStreamData
Definition: TcpReassembly.h:122
pcpp::TcpReassembly::Error_PacketDoesNotMatchFlow
@ Error_PacketDoesNotMatchFlow
Definition: TcpReassembly.h:271
pcpp::TcpReassemblyConfiguration
Definition: TcpReassembly.h:165
pcpp::ConnectionData::endTime
timeval endTime
Definition: TcpReassembly.h:93
pcpp::TcpReassembly::OnTcpConnectionStart
void(* OnTcpConnectionStart)(const ConnectionData &connectionData, void *userCookie)
Definition: TcpReassembly.h:294
PointerVector.h
pcpp::TcpReassemblyConfiguration::maxNumToClean
uint32_t maxNumToClean
Definition: TcpReassembly.h:178
pcpp::TcpReassembly::FIN_RSTWithNoData
@ FIN_RSTWithNoData
Definition: TcpReassembly.h:239
pcpp::TcpReassembly::TcpReassemblyConnectionClosedManually
@ TcpReassemblyConnectionClosedManually
Definition: TcpReassembly.h:209
pcpp::TcpReassembly
Definition: TcpReassembly.h:197
pcpp::TcpReassembly::isConnectionOpen
int isConnectionOpen(const ConnectionData &connection) const
IpAddress.h
pcpp::TcpReassembly::ReassemblyStatus
ReassemblyStatus
Definition: TcpReassembly.h:215
pcpp::ConnectionData::dstPort
uint16_t dstPort
Definition: TcpReassembly.h:87
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::TcpReassembly::OnTcpConnectionEnd
void(* OnTcpConnectionEnd)(const ConnectionData &connectionData, ConnectionEndReason reason, void *userCookie)
Definition: TcpReassembly.h:303
pcpp::TcpReassembly::Ignore_Retransimission
@ Ignore_Retransimission
Definition: TcpReassembly.h:255
pcpp::TcpStreamData::getConnectionData
const ConnectionData & getConnectionData() const
Definition: TcpReassembly.h:152
pcpp::TcpStreamData::TcpStreamData
TcpStreamData(const uint8_t *tcpData, size_t tcpDataLength, const ConnectionData &connData)
Definition: TcpReassembly.h:131
pcpp::TcpStreamData::getDataLength
size_t getDataLength() const
Definition: TcpReassembly.h:146
pcpp::ConnectionData::flowKey
uint32_t flowKey
Definition: TcpReassembly.h:89
Packet.h
pcpp::TcpReassembly::TcpMessageHandled
@ TcpMessageHandled
Definition: TcpReassembly.h:225
pcpp::TcpReassembly::OutOfOrderTcpMessageBuffered
@ OutOfOrderTcpMessageBuffered
Definition: TcpReassembly.h:233
pcpp::ConnectionData::startTime
timeval startTime
Definition: TcpReassembly.h:91
pcpp::TcpReassembly::NonIpPacket
@ NonIpPacket
Definition: TcpReassembly.h:260
pcpp::ConnectionData::srcPort
uint16_t srcPort
Definition: TcpReassembly.h:85
pcpp::TcpReassembly::ConnectionInfoList
std::map< uint32_t, ConnectionData > ConnectionInfoList
Definition: TcpReassembly.h:277
pcpp::TcpReassembly::purgeClosedConnections
uint32_t purgeClosedConnections(uint32_t maxNumToClean=0)
pcpp::TcpReassembly::TcpReassemblyConnectionClosedByFIN_RST
@ TcpReassemblyConnectionClosedByFIN_RST
Definition: TcpReassembly.h:207
pcpp::ConnectionData::srcIP
IPAddress srcIP
Definition: TcpReassembly.h:81
pcpp::TcpReassemblyConfiguration::removeConnInfo
bool removeConnInfo
Definition: TcpReassembly.h:168
pcpp::ConnectionData::dstIP
IPAddress dstIP
Definition: TcpReassembly.h:83
pcpp::RawPacket
Definition: RawPacket.h:252
pcpp::TcpReassembly::getConnectionInformation
const ConnectionInfoList & getConnectionInformation() const
Definition: TcpReassembly.h:348