PcapPlusPlus  19.12
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 
98  ConnectionData() : srcIP(NULL), dstIP(NULL), srcPort(0), dstPort(0), flowKey(0), startTime(), endTime() {}
99 
103  ~ConnectionData();
104 
108  ConnectionData(const ConnectionData& other);
109 
113  ConnectionData& operator=(const ConnectionData& other);
114 
119  void setSrcIpAddress(const IPAddress* sourceIP) { srcIP = sourceIP->clone(); }
120 
125  void setDstIpAddress(const IPAddress* destIP) { dstIP = destIP->clone(); }
126 
131  void setStartTime(const timeval &startTime) { this->startTime = startTime; }
132 
137  void setEndTime(const timeval &endTime) { this->endTime = endTime; }
138 
139 private:
140 
141  void copyData(const ConnectionData& other);
142 };
143 
144 
145 class TcpReassembly;
146 
147 
154 {
155 public:
162  TcpStreamData(const uint8_t* tcpData, size_t tcpDataLength, const ConnectionData& connData)
163  : m_Data(tcpData), m_DataLen(tcpDataLength), m_Connection(connData)
164  {
165  }
166 
171  const uint8_t* getData() const { return m_Data; }
172 
177  size_t getDataLength() const { return m_DataLen; }
178 
183  const ConnectionData& getConnectionData() const { return m_Connection; }
184 
185 private:
186  const uint8_t* m_Data;
187  size_t m_DataLen;
188  const ConnectionData& m_Connection;
189 };
190 
191 
197 {
200 
205 
209  uint32_t maxNumToClean;
210 
219  {
220  }
221 };
222 
223 
229 {
230 public:
231 
236  {
241  };
242 
246  typedef std::map<uint32_t, ConnectionData> ConnectionInfoList;
247 
255  typedef void (*OnTcpMessageReady)(int side, const TcpStreamData& tcpData, void* userCookie);
256 
263  typedef void (*OnTcpConnectionStart)(const ConnectionData& connectionData, void* userCookie);
264 
272  typedef void (*OnTcpConnectionEnd)(const ConnectionData& connectionData, ConnectionEndReason reason, void* userCookie);
273 
282  TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void* userCookie = NULL, OnTcpConnectionStart onConnectionStartCallback = NULL, OnTcpConnectionEnd onConnectionEndCallback = NULL, const TcpReassemblyConfiguration &config = TcpReassemblyConfiguration());
283 
288  ~TcpReassembly();
289 
295  void reassemblePacket(Packet& tcpData);
296 
302  void reassemblePacket(RawPacket* tcpRawData);
303 
309  void closeConnection(uint32_t flowKey);
310 
315  void closeAllConnections();
316 
321  const ConnectionInfoList& getConnectionInformation() const { return m_ConnectionInfo; }
322 
328  int isConnectionOpen(const ConnectionData& connection) const;
329 
335  uint32_t purgeClosedConnections(uint32_t maxNumToClean = 0);
336 
337 private:
338  struct TcpFragment
339  {
340  uint32_t sequence;
341  size_t dataLength;
342  uint8_t* data;
343 
344  TcpFragment() { sequence = 0; dataLength = 0; data = NULL; }
345  ~TcpFragment() { if (data != NULL) delete [] data; }
346  };
347 
348  struct TcpOneSideData
349  {
350  IPAddress* srcIP;
351  uint16_t srcPort;
352  uint32_t sequence;
353  PointerVector<TcpFragment> tcpFragmentList;
354  bool gotFinOrRst;
355 
356  void setSrcIP(IPAddress* sourrcIP);
357 
358  TcpOneSideData() { srcIP = NULL; srcPort = 0; sequence = 0; gotFinOrRst = false; }
359 
360  ~TcpOneSideData() { if (srcIP != NULL) delete srcIP; }
361  };
362 
363  struct TcpReassemblyData
364  {
365  int numOfSides;
366  int prevSide;
367  TcpOneSideData twoSides[2];
368  ConnectionData connData;
369 
370  TcpReassemblyData() { numOfSides = 0; prevSide = -1; }
371  };
372 
373  typedef std::map<uint32_t, TcpReassemblyData *> ConnectionList;
374  typedef std::map<time_t, std::list<uint32_t> > CleanupList;
375 
376  OnTcpMessageReady m_OnMessageReadyCallback;
377  OnTcpConnectionStart m_OnConnStart;
378  OnTcpConnectionEnd m_OnConnEnd;
379  void* m_UserCookie;
380  ConnectionList m_ConnectionList;
381  ConnectionInfoList m_ConnectionInfo;
382  CleanupList m_CleanupList;
383  bool m_RemoveConnInfo;
384  uint32_t m_ClosedConnectionDelay;
385  uint32_t m_MaxNumToClean;
386  time_t m_PurgeTimepoint;
387 
388  void checkOutOfOrderFragments(TcpReassemblyData* tcpReassemblyData, int sideIndex, bool cleanWholeFragList);
389 
390  std::string prepareMissingDataMessage(uint32_t missingDataLen);
391 
392  void handleFinOrRst(TcpReassemblyData* tcpReassemblyData, int sideIndex, uint32_t flowKey);
393 
394  void closeConnectionInternal(uint32_t flowKey, ConnectionEndReason reason);
395 
396  void insertIntoCleanupList(uint32_t flowKey);
397 };
398 
399 }
400 
401 #endif /* PACKETPP_TCP_REASSEMBLY */
pcpp::ConnectionData::setDstIpAddress
void setDstIpAddress(const IPAddress *destIP)
Definition: TcpReassembly.h:125
pcpp::TcpStreamData::getData
const uint8_t * getData() const
Definition: TcpReassembly.h:171
pcpp::ConnectionData::~ConnectionData
~ConnectionData()
pcpp::Packet
Definition: Packet.h:26
pcpp::TcpReassembly::~TcpReassembly
~TcpReassembly()
pcpp::TcpReassembly::closeConnection
void closeConnection(uint32_t flowKey)
pcpp::TcpReassembly::ConnectionEndReason
ConnectionEndReason
Definition: TcpReassembly.h:235
pcpp::ConnectionData::ConnectionData
ConnectionData()
Definition: TcpReassembly.h:98
pcpp::ConnectionData::setEndTime
void setEndTime(const timeval &endTime)
Definition: TcpReassembly.h:137
pcpp::ConnectionData::setStartTime
void setStartTime(const timeval &startTime)
Definition: TcpReassembly.h:131
pcpp::ConnectionData
Definition: TcpReassembly.h:78
pcpp::TcpReassemblyConfiguration::closedConnectionDelay
uint32_t closedConnectionDelay
Definition: TcpReassembly.h:204
pcpp::IPAddress
Definition: IpAddress.h:28
pcpp::TcpReassemblyConfiguration::TcpReassemblyConfiguration
TcpReassemblyConfiguration(bool removeConnInfo=true, uint32_t closedConnectionDelay=5, uint32_t maxNumToClean=30)
Definition: TcpReassembly.h:217
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::TcpReassembly::OnTcpMessageReady
void(* OnTcpMessageReady)(int side, const TcpStreamData &tcpData, void *userCookie)
Definition: TcpReassembly.h:255
pcpp::TcpStreamData
Definition: TcpReassembly.h:153
pcpp::TcpReassemblyConfiguration
Definition: TcpReassembly.h:196
pcpp::ConnectionData::endTime
timeval endTime
Definition: TcpReassembly.h:93
pcpp::TcpReassembly::OnTcpConnectionStart
void(* OnTcpConnectionStart)(const ConnectionData &connectionData, void *userCookie)
Definition: TcpReassembly.h:263
PointerVector.h
pcpp::TcpReassemblyConfiguration::maxNumToClean
uint32_t maxNumToClean
Definition: TcpReassembly.h:209
pcpp::TcpReassembly::TcpReassemblyConnectionClosedManually
Definition: TcpReassembly.h:240
pcpp::TcpReassembly
Definition: TcpReassembly.h:228
pcpp::TcpReassembly::isConnectionOpen
int isConnectionOpen(const ConnectionData &connection) const
IpAddress.h
pcpp::ConnectionData::dstPort
uint16_t dstPort
Definition: TcpReassembly.h:87
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::ConnectionData::srcIP
IPAddress * srcIP
Definition: TcpReassembly.h:81
pcpp::TcpReassembly::OnTcpConnectionEnd
void(* OnTcpConnectionEnd)(const ConnectionData &connectionData, ConnectionEndReason reason, void *userCookie)
Definition: TcpReassembly.h:272
pcpp::TcpStreamData::getConnectionData
const ConnectionData & getConnectionData() const
Definition: TcpReassembly.h:183
pcpp::TcpStreamData::TcpStreamData
TcpStreamData(const uint8_t *tcpData, size_t tcpDataLength, const ConnectionData &connData)
Definition: TcpReassembly.h:162
pcpp::ConnectionData::dstIP
IPAddress * dstIP
Definition: TcpReassembly.h:83
pcpp::TcpStreamData::getDataLength
size_t getDataLength() const
Definition: TcpReassembly.h:177
pcpp::ConnectionData::operator=
ConnectionData & operator=(const ConnectionData &other)
pcpp::ConnectionData::flowKey
uint32_t flowKey
Definition: TcpReassembly.h:89
Packet.h
pcpp::ConnectionData::startTime
timeval startTime
Definition: TcpReassembly.h:91
pcpp::ConnectionData::srcPort
uint16_t srcPort
Definition: TcpReassembly.h:85
pcpp::TcpReassembly::ConnectionInfoList
std::map< uint32_t, ConnectionData > ConnectionInfoList
Definition: TcpReassembly.h:246
pcpp::TcpReassembly::purgeClosedConnections
uint32_t purgeClosedConnections(uint32_t maxNumToClean=0)
pcpp::TcpReassembly::TcpReassemblyConnectionClosedByFIN_RST
Definition: TcpReassembly.h:238
pcpp::TcpReassembly::reassemblePacket
void reassemblePacket(Packet &tcpData)
pcpp::TcpReassemblyConfiguration::removeConnInfo
bool removeConnInfo
Definition: TcpReassembly.h:199
pcpp::ConnectionData::setSrcIpAddress
void setSrcIpAddress(const IPAddress *sourceIP)
Definition: TcpReassembly.h:119
pcpp::RawPacket
Definition: RawPacket.h:219
pcpp::IPAddress::clone
virtual IPAddress * clone() const =0
pcpp::TcpReassembly::getConnectionInformation
const ConnectionInfoList & getConnectionInformation() const
Definition: TcpReassembly.h:321