PcapPlusPlus
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 
55 namespace pcpp
56 {
57 
63 {
69  size_t srcPort;
71  size_t dstPort;
73  uint32_t flowKey;
75  timeval startTime;
77  timeval endTime;
78 
82  ConnectionData() : srcIP(NULL), dstIP(NULL), srcPort(0), dstPort(0), flowKey(0), startTime(), endTime() {}
83 
88 
92  ConnectionData(const ConnectionData& other);
93 
98 
103  void setSrcIpAddress(const IPAddress* sourceIP) { srcIP = sourceIP->clone(); }
104 
109  void setDstIpAddress(const IPAddress* destIP) { dstIP = destIP->clone(); }
110 
115  void setStartTime(const timeval &startTime) { this->startTime = startTime; }
116 
121  void setEndTime(const timeval &endTime) { this->endTime = endTime; }
122 
123 private:
124 
125  void copyData(const ConnectionData& other);
126 };
127 
128 
129 class TcpReassembly;
130 
131 
138 {
139  friend class TcpReassembly;
140 
141 public:
142 
146  TcpStreamData();
147 
154  TcpStreamData(uint8_t* tcpData, size_t tcpDataLength, ConnectionData connData);
155 
159  ~TcpStreamData();
160 
167 
174  TcpStreamData& operator=(const TcpStreamData& other);
175 
180  inline uint8_t* getData() { return m_Data; }
181 
186  inline size_t getDataLength() { return m_DataLen; }
187 
192  inline ConnectionData getConnectionData() { return m_Connection; }
193 
194 private:
195  uint8_t* m_Data;
196  size_t m_DataLen;
197  ConnectionData m_Connection;
198  bool m_DeleteDataOnDestruction;
199 
200  void setDeleteDataOnDestruction(bool flag) { m_DeleteDataOnDestruction = flag; }
201  void copyData(const TcpStreamData& other);
202 };
203 
204 
210 {
211 public:
212 
217  {
222  };
223 
231  typedef void (*OnTcpMessageReady)(int side, TcpStreamData tcpData, void* userCookie);
232 
239  typedef void (*OnTcpConnectionStart)(ConnectionData connectionData, void* userCookie);
240 
248  typedef void (*OnTcpConnectionEnd)(ConnectionData connectionData, ConnectionEndReason reason, void* userCookie);
249 
257  TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void* userCookie = NULL, OnTcpConnectionStart onConnectionStartCallback = NULL, OnTcpConnectionEnd onConnectionEndCallback = NULL);
258 
263  ~TcpReassembly();
264 
270  void reassemblePacket(Packet& tcpData);
271 
277  void reassemblePacket(RawPacket* tcpRawData);
278 
284  void closeConnection(uint32_t flowKey);
285 
290  void closeAllConnections();
291 
296  const std::vector<ConnectionData>& getConnectionInformation() const;
297 
303  int isConnectionOpen(const ConnectionData& connection);
304 
305 private:
306  struct TcpFragment
307  {
308  uint32_t sequence;
309  size_t dataLength;
310  uint8_t* data;
311 
312  TcpFragment() { sequence = 0; dataLength = 0; data = NULL; }
313  ~TcpFragment() { if (data != NULL) delete [] data; }
314  };
315 
316  struct TcpOneSideData
317  {
318  IPAddress* srcIP;
319  uint16_t srcPort;
320  uint32_t sequence;
321  PointerVector<TcpFragment> tcpFragmentList;
322  bool gotFinOrRst;
323 
324  void setSrcIP(IPAddress* sourrcIP);
325 
326  TcpOneSideData() { srcIP = NULL; srcPort = 0; sequence = 0; gotFinOrRst = false; }
327 
328  ~TcpOneSideData() { if (srcIP != NULL) delete srcIP; }
329  };
330 
331  struct TcpReassemblyData
332  {
333  int numOfSides;
334  int prevSide;
335  TcpOneSideData twoSides[2];
336  ConnectionData connData;
337 
338  TcpReassemblyData() { numOfSides = 0; prevSide = -1; }
339  };
340 
341  OnTcpMessageReady m_OnMessageReadyCallback;
342  OnTcpConnectionStart m_OnConnStart;
343  OnTcpConnectionEnd m_OnConnEnd;
344  void* m_UserCookie;
345  std::map<uint32_t, TcpReassemblyData*> m_ConnectionList;
346  std::map<uint32_t, bool> m_ClosedConnectionList;
347  std::vector<ConnectionData> m_ConnectionInfo;
348 
349  void checkOutOfOrderFragments(TcpReassemblyData* tcpReassemblyData, int sideIndex, bool cleanWholeFragList);
350 
351  std::string prepareMissingDataMessage(uint32_t missingDataLen);
352 
353  void handleFinOrRst(TcpReassemblyData* tcpReassemblyData, int sideIndex, uint32_t flowKey);
354 
355  void closeConnectionInternal(uint32_t flowKey, ConnectionEndReason reason);
356 };
357 
358 }
359 
360 #endif /* PACKETPP_TCP_REASSEMBLY */
void(* OnTcpConnectionStart)(ConnectionData connectionData, void *userCookie)
Definition: TcpReassembly.h:239
ConnectionData()
Definition: TcpReassembly.h:82
ConnectionEndReason
Definition: TcpReassembly.h:216
void reassemblePacket(Packet &tcpData)
size_t srcPort
Definition: TcpReassembly.h:69
void(* OnTcpMessageReady)(int side, TcpStreamData tcpData, void *userCookie)
Definition: TcpReassembly.h:231
IPAddress * srcIP
Definition: TcpReassembly.h:65
size_t dstPort
Definition: TcpReassembly.h:71
TcpStreamData & operator=(const TcpStreamData &other)
void(* OnTcpConnectionEnd)(ConnectionData connectionData, ConnectionEndReason reason, void *userCookie)
Definition: TcpReassembly.h:248
virtual IPAddress * clone() const =0
size_t getDataLength()
Definition: TcpReassembly.h:186
TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void *userCookie=NULL, OnTcpConnectionStart onConnectionStartCallback=NULL, OnTcpConnectionEnd onConnectionEndCallback=NULL)
timeval endTime
Definition: TcpReassembly.h:77
ConnectionData & operator=(const ConnectionData &other)
uint32_t flowKey
Definition: TcpReassembly.h:73
Definition: RawPacket.h:219
Definition: Packet.h:26
Definition: TcpReassembly.h:137
void setEndTime(const timeval &endTime)
Definition: TcpReassembly.h:121
uint8_t * getData()
Definition: TcpReassembly.h:180
Definition: IpAddress.h:27
void setStartTime(const timeval &startTime)
Definition: TcpReassembly.h:115
IPAddress * dstIP
Definition: TcpReassembly.h:67
void setDstIpAddress(const IPAddress *destIP)
Definition: TcpReassembly.h:109
const std::vector< ConnectionData > & getConnectionInformation() const
ConnectionData getConnectionData()
Definition: TcpReassembly.h:192
Definition: TcpReassembly.h:62
int isConnectionOpen(const ConnectionData &connection)
Definition: TcpReassembly.h:209
timeval startTime
Definition: TcpReassembly.h:75
void setSrcIpAddress(const IPAddress *sourceIP)
Definition: TcpReassembly.h:103
void closeConnection(uint32_t flowKey)
The main namespace for the PcapPlusPlus lib.