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;
74 
78  ConnectionData() : srcIP(NULL), dstIP(NULL), srcPort(0), dstPort(0), flowKey(0) {}
79 
84 
88  ConnectionData(const ConnectionData& other);
89 
94 
99  void setSrcIpAddress(const IPAddress* sourceIP) { srcIP = sourceIP->clone(); }
100 
105  void setDstIpAddress(const IPAddress* destIP) { dstIP = destIP->clone(); }
106 
107 private:
108 
109  void copyData(const ConnectionData& other);
110 };
111 
112 
113 class TcpReassembly;
114 
115 
122 {
123  friend class TcpReassembly;
124 
125 public:
126 
130  TcpStreamData();
131 
138  TcpStreamData(uint8_t* tcpData, size_t tcpDataLength, ConnectionData connData);
139 
143  ~TcpStreamData();
144 
151 
158  TcpStreamData& operator=(const TcpStreamData& other);
159 
164  inline uint8_t* getData() { return m_Data; }
165 
170  inline size_t getDataLength() { return m_DataLen; }
171 
176  inline ConnectionData getConnectionData() { return m_Connection; }
177 
178 private:
179  uint8_t* m_Data;
180  size_t m_DataLen;
181  ConnectionData m_Connection;
182  bool m_DeleteDataOnDestruction;
183 
184  void setDeleteDataOnDestruction(bool flag) { m_DeleteDataOnDestruction = flag; }
185  void copyData(const TcpStreamData& other);
186 };
187 
188 
194 {
195 public:
196 
201  {
206  };
207 
215  typedef void (*OnTcpMessageReady)(int side, TcpStreamData tcpData, void* userCookie);
216 
223  typedef void (*OnTcpConnectionStart)(ConnectionData connectionData, void* userCookie);
224 
232  typedef void (*OnTcpConnectionEnd)(ConnectionData connectionData, ConnectionEndReason reason, void* userCookie);
233 
241  TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void* userCookie = NULL, OnTcpConnectionStart onConnectionStartCallback = NULL, OnTcpConnectionEnd onConnectionEndCallback = NULL);
242 
247  ~TcpReassembly();
248 
254  void reassemblePacket(Packet& tcpData);
255 
261  void reassemblePacket(RawPacket* tcpRawData);
262 
268  void closeConnection(uint32_t flowKey);
269 
274  void closeAllConnections();
275 
280  const std::vector<ConnectionData>& getConnectionInformation() const;
281 
287  int isConnectionOpen(const ConnectionData& connection);
288 
289 private:
290  struct TcpFragment
291  {
292  uint32_t sequence;
293  size_t dataLength;
294  uint8_t* data;
295 
296  TcpFragment() { sequence = 0; dataLength = 0; data = NULL; }
297  ~TcpFragment() { if (data != NULL) delete [] data; }
298  };
299 
300  struct TcpOneSideData
301  {
302  IPAddress* srcIP;
303  uint16_t srcPort;
304  uint32_t sequence;
305  PointerVector<TcpFragment> tcpFragmentList;
306  bool gotFinOrRst;
307 
308  void setSrcIP(IPAddress* sourrcIP);
309 
310  TcpOneSideData() { srcIP = NULL; srcPort = 0; sequence = 0; gotFinOrRst = false; }
311 
312  ~TcpOneSideData() { if (srcIP != NULL) delete srcIP; }
313  };
314 
315  struct TcpReassemblyData
316  {
317  int numOfSides;
318  int prevSide;
319  TcpOneSideData twoSides[2];
320  ConnectionData connData;
321 
322  TcpReassemblyData() { numOfSides = 0; prevSide = -1; }
323  };
324 
325  OnTcpMessageReady m_OnMessageReadyCallback;
326  OnTcpConnectionStart m_OnConnStart;
327  OnTcpConnectionEnd m_OnConnEnd;
328  void* m_UserCookie;
329  std::map<uint32_t, TcpReassemblyData*> m_ConnectionList;
330  std::map<uint32_t, bool> m_ClosedConnectionList;
331  std::vector<ConnectionData> m_ConnectionInfo;
332 
333  void checkOutOfOrderFragments(TcpReassemblyData* tcpReassemblyData, int sideIndex, bool cleanWholeFragList);
334 
335  std::string prepareMissingDataMessage(uint32_t missingDataLen);
336 
337  void handleFinOrRst(TcpReassemblyData* tcpReassemblyData, int sideIndex, uint32_t flowKey);
338 
339  void closeConnectionInternal(uint32_t flowKey, ConnectionEndReason reason);
340 };
341 
342 }
343 
344 #endif /* PACKETPP_TCP_REASSEMBLY */
ConnectionEndReason
Definition: TcpReassembly.h:200
void(* OnTcpConnectionStart)(ConnectionData connectionData, void *userCookie)
Definition: TcpReassembly.h:223
void(* OnTcpMessageReady)(int side, TcpStreamData tcpData, void *userCookie)
Definition: TcpReassembly.h:215
const std::vector< ConnectionData > & getConnectionInformation() const
Definition: RawPacket.h:220
Definition: Packet.h:26
The main namespace for the PcapPlusPlus lib.
void(* OnTcpConnectionEnd)(ConnectionData connectionData, ConnectionEndReason reason, void *userCookie)
Definition: TcpReassembly.h:232
uint32_t flowKey
Definition: TcpReassembly.h:73
ConnectionData()
Definition: TcpReassembly.h:78
void setSrcIpAddress(const IPAddress *sourceIP)
Definition: TcpReassembly.h:99
void setDstIpAddress(const IPAddress *destIP)
Definition: TcpReassembly.h:105
ConnectionData & operator=(const ConnectionData &other)
Definition: TcpReassembly.h:62
IPAddress * srcIP
Definition: TcpReassembly.h:65
TcpStreamData & operator=(const TcpStreamData &other)
Definition: IpAddress.h:27
void reassemblePacket(Packet &tcpData)
Definition: TcpReassembly.h:193
virtual IPAddress * clone() const =0
size_t srcPort
Definition: TcpReassembly.h:69
Definition: TcpReassembly.h:121
size_t dstPort
Definition: TcpReassembly.h:71
IPAddress * dstIP
Definition: TcpReassembly.h:67
ConnectionData getConnectionData()
Definition: TcpReassembly.h:176
uint8_t * getData()
Definition: TcpReassembly.h:164
size_t getDataLength()
Definition: TcpReassembly.h:170
void closeConnection(uint32_t flowKey)
TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void *userCookie=NULL, OnTcpConnectionStart onConnectionStartCallback=NULL, OnTcpConnectionEnd onConnectionEndCallback=NULL)
int isConnectionOpen(const ConnectionData &connection)