PcapPlusPlus  23.09
TcpLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_TCP_LAYER
2 #define PACKETPP_TCP_LAYER
3 
4 #include "Layer.h"
5 #include "TLVData.h"
6 #include <string.h>
7 
9 
14 namespace pcpp
15 {
16 
21 #pragma pack(push,1)
22  struct tcphdr
23  {
25  uint16_t portSrc;
27  uint16_t portDst;
29  uint32_t sequenceNumber;
31  uint32_t ackNumber;
32 #if (BYTE_ORDER == LITTLE_ENDIAN)
33  uint16_t reserved:4,
35  dataOffset:4,
37  finFlag:1,
39  synFlag:1,
41  rstFlag:1,
43  pshFlag:1,
45  ackFlag:1,
47  urgFlag:1,
49  eceFlag:1,
51  cwrFlag:1;
52 #elif (BYTE_ORDER == BIG_ENDIAN)
53 
54  uint16_t dataOffset:4,
55  reserved:4,
57  cwrFlag:1,
59  eceFlag:1,
61  urgFlag:1,
63  ackFlag:1,
65  pshFlag:1,
67  rstFlag:1,
69  synFlag:1,
71  finFlag:1;
72 #else
73 #error "Endian is not LE nor BE..."
74 #endif
75 
76  uint16_t windowSize;
78  uint16_t headerChecksum;
80  uint16_t urgentPointer;
81  };
82 #pragma pack(pop)
83 
84 
89  {
109  TCPOPT_CC = 11,
117  TCPOPT_MPTCP = 0x1e,
127  TCPOPT_QS = 27,
140  };
141 
142 
143  // TCP option lengths
144 
146 #define PCPP_TCPOLEN_NOP 1
147 
148 #define PCPP_TCPOLEN_EOL 1
149 
150 #define PCPP_TCPOLEN_MSS 4
151 
152 #define PCPP_TCPOLEN_WINDOW 3
153 
154 #define PCPP_TCPOLEN_SACK_PERM 2
155 
156 #define PCPP_TCPOLEN_SACK_MIN 2
157 
158 #define PCPP_TCPOLEN_ECHO 6
159 
160 #define PCPP_TCPOLEN_ECHOREPLY 6
161 
162 #define PCPP_TCPOLEN_TIMESTAMP 10
163 
164 #define PCPP_TCPOLEN_CC 6
165 
166 #define PCPP_TCPOLEN_CCNEW 6
167 
168 #define PCPP_TCPOLEN_CCECHO 6
169 
170 #define PCPP_TCPOLEN_MD5 18
171 
172 #define PCPP_TCPOLEN_MPTCP_MIN 8
173 
174 #define PCPP_TCPOLEN_SCPS 4
175 
176 #define PCPP_TCPOLEN_SNACK 6
177 
178 #define PCPP_TCPOLEN_RECBOUND 2
179 
180 #define PCPP_TCPOLEN_CORREXP 2
181 
182 #define PCPP_TCPOLEN_QS 8
183 
184 #define PCPP_TCPOLEN_USER_TO 4
185 
186 #define PCPP_TCPOLEN_RVBD_PROBE_MIN 3
187 
188 #define PCPP_TCPOLEN_RVBD_TRPY_MIN 16
189 
190 #define PCPP_TCPOLEN_EXP_MIN 2
191 
192 
198  class TcpOption : public TLVRecord<uint8_t, uint8_t>
199  {
200  public:
201 
206  explicit TcpOption(uint8_t* optionRawData) : TLVRecord(optionRawData) { }
207 
212 
218  {
219  if (m_Data == nullptr)
220  return TCPOPT_Unknown;
221 
222  return (TcpOptionType)m_Data->recordType;
223  }
224 
225  // implement abstract methods
226 
227  size_t getTotalSize() const
228  {
229  if (m_Data == nullptr)
230  return 0;
231 
232  if (m_Data->recordType == (uint8_t)PCPP_TCPOPT_NOP || m_Data->recordType == (uint8_t)PCPP_TCPOPT_EOL)
233  return sizeof(uint8_t);
234 
235  return (size_t)m_Data->recordLen;
236  }
237 
238  size_t getDataSize() const
239  {
240  if (m_Data == nullptr)
241  return 0;
242 
243  if (m_Data->recordType == (uint8_t)PCPP_TCPOPT_NOP || m_Data->recordType == (uint8_t)PCPP_TCPOPT_EOL)
244  return 0;
245 
246  return (size_t)m_Data->recordLen - (2*sizeof(uint8_t));
247  }
248  };
249 
250 
257  {
258 
259  public:
260 
265  {
269  EOL
270  };
271 
279  TcpOptionBuilder(TcpOptionType optionType, const uint8_t* optionValue, uint8_t optionValueLen) :
280  TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen) {}
281 
288  TcpOptionBuilder(TcpOptionType optionType, uint8_t optionValue) :
289  TLVRecordBuilder((uint8_t)optionType, optionValue) {}
290 
297  TcpOptionBuilder(TcpOptionType optionType, uint16_t optionValue) :
298  TLVRecordBuilder((uint8_t)optionType, optionValue) {}
299 
306  TcpOptionBuilder(TcpOptionType optionType, uint32_t optionValue) :
307  TLVRecordBuilder((uint8_t)optionType, optionValue) {}
308 
315  explicit TcpOptionBuilder(NopEolOptionTypes optionType);
316 
321  TcpOption build() const;
322  };
323 
324 
329  class TcpLayer : public Layer
330  {
331  public:
339  TcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
340 
344  TcpLayer();
345 
351  TcpLayer(uint16_t portSrc, uint16_t portDst);
352 
353  ~TcpLayer() {}
354 
358  TcpLayer(const TcpLayer& other);
359 
363  TcpLayer& operator=(const TcpLayer& other);
364 
369  tcphdr* getTcpHeader() const { return (tcphdr*)m_Data; }
370 
374  uint16_t getSrcPort() const;
375 
379  uint16_t getDstPort() const;
380 
387  TcpOption getTcpOption(TcpOptionType option) const;
388 
393  TcpOption getFirstTcpOption() const;
394 
402  TcpOption getNextTcpOption(TcpOption& tcpOption) const;
403 
407  size_t getTcpOptionCount() const;
408 
416  TcpOption addTcpOption(const TcpOptionBuilder& optionBuilder);
417 
427  TcpOption addTcpOptionAfter(const TcpOptionBuilder& optionBuilder, TcpOptionType prevOptionType = TCPOPT_Unknown);
428 
435  bool removeTcpOption(TcpOptionType optionType);
436 
442  bool removeAllTcpOptions();
443 
444 
450  uint16_t calculateChecksum(bool writeResultToPacket);
451 
458  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
459 
460  // implement abstract methods
461 
465  void parseNextLayer();
466 
470  size_t getHeaderLen() const { return getTcpHeader()->dataOffset*4 ;}
471 
475  void computeCalculateFields();
476 
477  std::string toString() const;
478 
480 
481  private:
482 
483  TLVRecordReader<TcpOption> m_OptionReader;
484  int m_NumOfTrailingBytes;
485 
486  void initLayer();
487  uint8_t* getOptionsBasePtr() const { return m_Data + sizeof(tcphdr); }
488  TcpOption addTcpOptionAt(const TcpOptionBuilder& optionBuilder, int offset);
489  void adjustTcpOptionTrailer(size_t totalOptSize);
490  void copyLayerData(const TcpLayer& other);
491  };
492 
493 
494  // implementation of inline methods
495 
496  bool TcpLayer::isDataValid(const uint8_t* data, size_t dataLen)
497  {
498  const tcphdr* hdr = reinterpret_cast<const tcphdr*>(data);
499  return dataLen >= sizeof(tcphdr)
500  && hdr->dataOffset >= 5 /* the minimum TCP header size */
501  && dataLen >= hdr->dataOffset * sizeof(uint32_t);
502  }
503 
504 } // namespace pcpp
505 
506 #endif /* PACKETPP_TCP_LAYER */
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
Definition: TcpLayer.h:135
Definition: TcpLayer.h:121
Definition: TcpLayer.h:103
uint16_t dataOffset
Definition: TcpLayer.h:33
Definition: TcpLayer.h:131
uint16_t windowSize
Definition: TcpLayer.h:76
Definition: TLVData.h:376
Definition: Layer.h:70
uint16_t headerChecksum
Definition: TcpLayer.h:78
Definition: TLVData.h:24
Definition: Packet.h:26
Definition: TcpLayer.h:115
uint16_t eceFlag
Definition: TcpLayer.h:33
uint16_t urgFlag
Definition: TcpLayer.h:33
~TcpOption()
Definition: TcpLayer.h:211
uint16_t portSrc
Definition: TcpLayer.h:25
uint16_t urgentPointer
Definition: TcpLayer.h:80
Definition: TcpLayer.h:22
Definition: TcpLayer.h:133
Definition: TLVData.h:217
uint32_t sequenceNumber
Definition: TcpLayer.h:29
NopEolOptionTypes
Definition: TcpLayer.h:264
Definition: TcpLayer.h:198
Definition: TcpLayer.h:123
size_t getTotalSize() const
Definition: TcpLayer.h:227
uint16_t pshFlag
Definition: TcpLayer.h:33
Definition: TcpLayer.h:117
Definition: TcpLayer.h:113
Definition: TcpLayer.h:127
TcpOptionBuilder(TcpOptionType optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: TcpLayer.h:279
uint16_t rstFlag
Definition: TcpLayer.h:33
Definition: TcpLayer.h:256
TcpOptionBuilder(TcpOptionType optionType, uint8_t optionValue)
Definition: TcpLayer.h:288
Definition: TcpLayer.h:267
Definition: TcpLayer.h:101
Definition: ProtocolType.h:342
uint32_t ackNumber
Definition: TcpLayer.h:31
uint16_t ackFlag
Definition: TcpLayer.h:33
TcpOptionType
Definition: TcpLayer.h:88
Definition: TcpLayer.h:107
Definition: TcpLayer.h:111
Definition: TcpLayer.h:119
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: TcpLayer.h:496
size_t getDataSize() const
Definition: TcpLayer.h:238
Definition: TcpLayer.h:329
tcphdr * getTcpHeader() const
Definition: TcpLayer.h:369
uint16_t synFlag
Definition: TcpLayer.h:33
Definition: TcpLayer.h:125
Definition: TcpLayer.h:137
Definition: TcpLayer.h:97
TcpOption(uint8_t *optionRawData)
Definition: TcpLayer.h:206
TcpOptionType getTcpOptionType() const
Definition: TcpLayer.h:217
Definition: TcpLayer.h:99
uint16_t cwrFlag
Definition: TcpLayer.h:33
uint16_t portDst
Definition: TcpLayer.h:27
Definition: TcpLayer.h:91
Definition: TcpLayer.h:129
Definition: TcpLayer.h:93
uint16_t finFlag
Definition: TcpLayer.h:33
TcpOptionBuilder(TcpOptionType optionType, uint32_t optionValue)
Definition: TcpLayer.h:306
Definition: TcpLayer.h:105
Definition: TcpLayer.h:139
size_t getHeaderLen() const
Definition: TcpLayer.h:470
OsiModelLayer getOsiModelLayer() const
Definition: TcpLayer.h:479
Definition: TcpLayer.h:95
Definition: TcpLayer.h:109
TcpOptionBuilder(TcpOptionType optionType, uint16_t optionValue)
Definition: TcpLayer.h:297