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(IPv4Address::Zero), dstIP(IPv4Address::Zero), srcPort(0), dstPort(0), flowKey(0) {}
79 };
80 
81 
82 class TcpReassembly;
83 
84 
91 {
92  friend class TcpReassembly;
93 
94 public:
95 
99  TcpStreamData();
100 
107  TcpStreamData(uint8_t* tcpData, size_t tcpDataLength, ConnectionData connData);
108 
112  ~TcpStreamData();
113 
120 
127  TcpStreamData& operator=(const TcpStreamData& other);
128 
133  inline uint8_t* getData() { return m_Data; }
134 
139  inline size_t getDataLength() { return m_DataLen; }
140 
145  inline ConnectionData getConnectionData() { return m_Connection; }
146 
147 private:
148  uint8_t* m_Data;
149  size_t m_DataLen;
150  ConnectionData m_Connection;
151  bool m_DeleteDataOnDestruction;
152 
153  void setDeleteDataOnDestruction(bool flag) { m_DeleteDataOnDestruction = flag; }
154  void copyData(const TcpStreamData& other);
155 };
156 
157 
163 {
164 public:
165 
170  {
175  };
176 
184  typedef void (*OnTcpMessageReady)(int side, TcpStreamData tcpData, void* userCookie);
185 
192  typedef void (*OnTcpConnectionStart)(ConnectionData connectionData, void* userCookie);
193 
201  typedef void (*OnTcpConnectionEnd)(ConnectionData connectionData, ConnectionEndReason reason, void* userCookie);
202 
210  TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void* userCookie = NULL, OnTcpConnectionStart onConnectionStartCallback = NULL, OnTcpConnectionEnd onConnectionEndCallback = NULL);
211 
216  ~TcpReassembly();
217 
223  void ReassemblePacket(Packet& tcpData);
224 
230  void ReassemblePacket(RawPacket* tcpRawData);
231 
237  void closeConnection(uint32_t flowKey);
238 
243  void closeAllConnections();
244 
249  const std::vector<ConnectionData>& getConnectionInformation() const;
250 
256  int isConnectionOpen(const ConnectionData& connection);
257 
258 private:
259  struct TcpFragment
260  {
261  uint32_t sequence;
262  size_t dataLength;
263  uint8_t* data;
264 
265  TcpFragment() { sequence = 0; dataLength = 0; data = NULL; }
266  ~TcpFragment() { if (data != NULL) delete [] data; }
267  };
268 
269  struct TcpOneSideData
270  {
271  uint32_t srcIP;
272  uint16_t srcPort;
273  uint32_t sequence;
274  PointerVector<TcpFragment> tcpFragmentList;
275  bool gotFinOrRst;
276 
277  TcpOneSideData() { srcIP = 0; srcPort = 0; sequence = 0; gotFinOrRst = false; }
278  };
279 
280  struct TcpReassemblyData
281  {
282  int numOfSides;
283  int prevSide;
284  TcpOneSideData twoSides[2];
285  ConnectionData connData;
286 
287  TcpReassemblyData() { numOfSides = 0; prevSide = -1; }
288  };
289 
290  OnTcpMessageReady m_OnMessageReadyCallback;
291  OnTcpConnectionStart m_OnConnStart;
292  OnTcpConnectionEnd m_OnConnEnd;
293  void* m_UserCookie;
294  std::map<uint32_t, TcpReassemblyData*> m_ConnectionList;
295  std::map<uint32_t, bool> m_ClosedConnectionList;
296  std::vector<ConnectionData> m_ConnectionInfo;
297 
298  void checkOutOfOrderFragments(TcpReassemblyData* tcpReassemblyData, int sideIndex, bool cleanWholeFragList);
299 
300  std::string prepareMissingDataMessage(uint32_t missingDataLen);
301 
302  void handleFinOrRst(TcpReassemblyData* tcpReassemblyData, int sideIndex, uint32_t flowKey);
303 
304  void closeConnectionInternal(uint32_t flowKey, ConnectionEndReason reason);
305 };
306 
307 }
308 
309 #endif /* PACKETPP_TCP_REASSEMBLY */
size_t dstPort
Definition: TcpReassembly.h:71
Definition: RawPacket.h:220
Definition: TcpReassembly.h:90
uint32_t flowKey
Definition: TcpReassembly.h:73
ConnectionData getConnectionData()
Definition: TcpReassembly.h:145
Definition: IpAddress.h:99
IPv4Address dstIP
Definition: TcpReassembly.h:67
Definition: Packet.h:26
Definition: TcpReassembly.h:162
void(* OnTcpMessageReady)(int side, TcpStreamData tcpData, void *userCookie)
Definition: TcpReassembly.h:184
size_t getDataLength()
Definition: TcpReassembly.h:139
ConnectionEndReason
Definition: TcpReassembly.h:169
IPv4Address srcIP
Definition: TcpReassembly.h:65
int isConnectionOpen(const ConnectionData &connection)
TcpStreamData & operator=(const TcpStreamData &other)
void(* OnTcpConnectionEnd)(ConnectionData connectionData, ConnectionEndReason reason, void *userCookie)
Definition: TcpReassembly.h:201
TcpReassembly(OnTcpMessageReady onMessageReadyCallback, void *userCookie=NULL, OnTcpConnectionStart onConnectionStartCallback=NULL, OnTcpConnectionEnd onConnectionEndCallback=NULL)
uint8_t * getData()
Definition: TcpReassembly.h:133
const std::vector< ConnectionData > & getConnectionInformation() const
ConnectionData()
Definition: TcpReassembly.h:78
size_t srcPort
Definition: TcpReassembly.h:69
The main namespace for the PcapPlusPlus lib.
void(* OnTcpConnectionStart)(ConnectionData connectionData, void *userCookie)
Definition: TcpReassembly.h:192
void closeConnection(uint32_t flowKey)
void ReassemblePacket(Packet &tcpData)
Definition: TcpReassembly.h:62