PcapPlusPlus  21.05
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 {
24  uint16_t portSrc;
26  uint16_t portDst;
28  uint32_t sequenceNumber;
30  uint32_t ackNumber;
31 #if (BYTE_ORDER == LITTLE_ENDIAN)
32  uint16_t reserved:4,
34  dataOffset:4,
36  finFlag:1,
38  synFlag:1,
40  rstFlag:1,
42  pshFlag:1,
44  ackFlag:1,
46  urgFlag:1,
48  eceFlag:1,
50  cwrFlag:1;
51 #elif (BYTE_ORDER == BIG_ENDIAN)
52 
53  uint16_t dataOffset:4,
54  reserved:4,
56  cwrFlag:1,
58  eceFlag:1,
60  urgFlag:1,
62  ackFlag:1,
64  pshFlag:1,
66  rstFlag:1,
68  synFlag:1,
70  finFlag:1;
71 #else
72 #error "Endian is not LE nor BE..."
73 #endif
74 
75  uint16_t windowSize;
77  uint16_t headerChecksum;
79  uint16_t urgentPointer;
80  };
81 #pragma pack(pop)
82 
83 
107  TCPOPT_CC = 11,
115  TCPOPT_MPTCP = 0x1e,
125  TCPOPT_QS = 27,
138  };
139 
140 
141  // TCP option lengths
142 
144 #define PCPP_TCPOLEN_NOP 1
145 
146 #define PCPP_TCPOLEN_EOL 1
147 
148 #define PCPP_TCPOLEN_MSS 4
149 
150 #define PCPP_TCPOLEN_WINDOW 3
151 
152 #define PCPP_TCPOLEN_SACK_PERM 2
153 
154 #define PCPP_TCPOLEN_SACK_MIN 2
155 
156 #define PCPP_TCPOLEN_ECHO 6
157 
158 #define PCPP_TCPOLEN_ECHOREPLY 6
159 
160 #define PCPP_TCPOLEN_TIMESTAMP 10
161 
162 #define PCPP_TCPOLEN_CC 6
163 
164 #define PCPP_TCPOLEN_CCNEW 6
165 
166 #define PCPP_TCPOLEN_CCECHO 6
167 
168 #define PCPP_TCPOLEN_MD5 18
169 
170 #define PCPP_TCPOLEN_MPTCP_MIN 8
171 
172 #define PCPP_TCPOLEN_SCPS 4
173 
174 #define PCPP_TCPOLEN_SNACK 6
175 
176 #define PCPP_TCPOLEN_RECBOUND 2
177 
178 #define PCPP_TCPOLEN_CORREXP 2
179 
180 #define PCPP_TCPOLEN_QS 8
181 
182 #define PCPP_TCPOLEN_USER_TO 4
183 
184 #define PCPP_TCPOLEN_RVBD_PROBE_MIN 3
185 
186 #define PCPP_TCPOLEN_RVBD_TRPY_MIN 16
187 
188 #define PCPP_TCPOLEN_EXP_MIN 2
189 
190 
196  class TcpOption : public TLVRecord
197  {
198  public:
199 
204  TcpOption(uint8_t* optionRawData) : TLVRecord(optionRawData) { }
205 
210 
216  {
217  if (m_Data == NULL)
218  return TCPOPT_Unknown;
219 
220  return (TcpOptionType)m_Data->recordType;
221  }
222 
223  // implement abstract methods
224 
225  size_t getTotalSize() const
226  {
227  if (m_Data == NULL)
228  return (size_t)0;
229 
230  if (m_Data->recordType == (uint8_t)PCPP_TCPOPT_NOP || m_Data->recordType == (uint8_t)PCPP_TCPOPT_EOL)
231  return sizeof(uint8_t);
232 
233  return (size_t)m_Data->recordLen;
234  }
235 
236  size_t getDataSize() const
237  {
238  if (m_Data == NULL)
239  return 0;
240 
241  if (m_Data->recordType == (uint8_t)PCPP_TCPOPT_NOP || m_Data->recordType == (uint8_t)PCPP_TCPOPT_EOL)
242  return (size_t)0;
243 
244  return (size_t)m_Data->recordLen - (2*sizeof(uint8_t));
245  }
246  };
247 
248 
255  {
256 
257  public:
258 
263  {
267  EOL
268  };
269 
277  TcpOptionBuilder(TcpOptionType optionType, const uint8_t* optionValue, uint8_t optionValueLen) :
278  TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen) {}
279 
286  TcpOptionBuilder(TcpOptionType optionType, uint8_t optionValue) :
287  TLVRecordBuilder((uint8_t)optionType, optionValue) {}
288 
295  TcpOptionBuilder(TcpOptionType optionType, uint16_t optionValue) :
296  TLVRecordBuilder((uint8_t)optionType, optionValue) {}
297 
304  TcpOptionBuilder(TcpOptionType optionType, uint32_t optionValue) :
305  TLVRecordBuilder((uint8_t)optionType, optionValue) {}
306 
314 
319  TcpOption build() const;
320  };
321 
322 
327  class TcpLayer : public Layer
328  {
329  public:
337  TcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
338 
342  TcpLayer();
343 
349  TcpLayer(uint16_t portSrc, uint16_t portDst);
350 
351  ~TcpLayer() {}
352 
356  TcpLayer(const TcpLayer& other);
357 
361  TcpLayer& operator=(const TcpLayer& other);
362 
367  tcphdr* getTcpHeader() const { return (tcphdr*)m_Data; }
368 
372  uint16_t getSrcPort() const;
373 
377  uint16_t getDstPort() const;
378 
385  TcpOption getTcpOption(TcpOptionType option) const;
386 
391  TcpOption getFirstTcpOption() const;
392 
400  TcpOption getNextTcpOption(TcpOption& tcpOption) const;
401 
405  size_t getTcpOptionCount() const;
406 
414  TcpOption addTcpOption(const TcpOptionBuilder& optionBuilder);
415 
425  TcpOption addTcpOptionAfter(const TcpOptionBuilder& optionBuilder, TcpOptionType prevOptionType = TCPOPT_Unknown);
426 
433  bool removeTcpOption(TcpOptionType optionType);
434 
440  bool removeAllTcpOptions();
441 
442 
448  uint16_t calculateChecksum(bool writeResultToPacket);
449 
456  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
457 
458  // implement abstract methods
459 
463  void parseNextLayer();
464 
468  size_t getHeaderLen() const { return getTcpHeader()->dataOffset*4 ;}
469 
473  void computeCalculateFields();
474 
475  std::string toString() const;
476 
478 
479  private:
480 
481  TLVRecordReader<TcpOption> m_OptionReader;
482  int m_NumOfTrailingBytes;
483 
484  void initLayer();
485  uint8_t* getOptionsBasePtr() const { return m_Data + sizeof(tcphdr); }
486  TcpOption addTcpOptionAt(const TcpOptionBuilder& optionBuilder, int offset);
487  void adjustTcpOptionTrailer(size_t totalOptSize);
488  void copyLayerData(const TcpLayer& other);
489  };
490 
491 
492  // implementation of inline methods
493 
494  bool TcpLayer::isDataValid(const uint8_t* data, size_t dataLen)
495  {
496  const tcphdr* hdr = reinterpret_cast<const tcphdr*>(data);
497  return dataLen >= sizeof(tcphdr)
498  && hdr->dataOffset >= 5 /* the minimum TCP header size */
499  && dataLen >= hdr->dataOffset * sizeof(uint32_t);
500  }
501 
502 } // namespace pcpp
503 
504 #endif /* PACKETPP_TCP_LAYER */
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:253
Definition: TcpLayer.h:133
Definition: TcpLayer.h:119
Definition: TcpLayer.h:101
uint16_t dataOffset
Definition: TcpLayer.h:32
Definition: TcpLayer.h:129
uint16_t windowSize
Definition: TcpLayer.h:75
Definition: TLVData.h:351
Definition: Layer.h:70
uint16_t headerChecksum
Definition: TcpLayer.h:77
Definition: TLVData.h:23
Definition: Packet.h:26
Definition: TcpLayer.h:113
uint16_t eceFlag
Definition: TcpLayer.h:32
uint16_t urgFlag
Definition: TcpLayer.h:32
~TcpOption()
Definition: TcpLayer.h:209
uint16_t portSrc
Definition: TcpLayer.h:24
uint16_t urgentPointer
Definition: TcpLayer.h:79
Definition: TcpLayer.h:22
Definition: TcpLayer.h:131
Definition: TLVData.h:197
uint32_t sequenceNumber
Definition: TcpLayer.h:28
NopEolOptionTypes
Definition: TcpLayer.h:262
Definition: TcpLayer.h:196
Definition: TcpLayer.h:121
size_t getTotalSize() const
Definition: TcpLayer.h:225
uint16_t pshFlag
Definition: TcpLayer.h:32
Definition: TcpLayer.h:115
Definition: TcpLayer.h:111
Definition: TcpLayer.h:125
TcpOptionBuilder(TcpOptionType optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: TcpLayer.h:277
uint16_t rstFlag
Definition: TcpLayer.h:32
Definition: TcpLayer.h:254
TcpOptionBuilder(TcpOptionType optionType, uint8_t optionValue)
Definition: TcpLayer.h:286
Definition: TcpLayer.h:265
Definition: TcpLayer.h:99
Definition: ProtocolType.h:262
uint32_t ackNumber
Definition: TcpLayer.h:30
uint16_t ackFlag
Definition: TcpLayer.h:32
TcpOptionType
Definition: TcpLayer.h:87
Definition: TcpLayer.h:105
Definition: TcpLayer.h:109
Definition: TcpLayer.h:117
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: TcpLayer.h:494
size_t getDataSize() const
Definition: TcpLayer.h:236
Definition: TcpLayer.h:327
tcphdr * getTcpHeader() const
Definition: TcpLayer.h:367
uint16_t synFlag
Definition: TcpLayer.h:32
Definition: TcpLayer.h:123
Definition: TcpLayer.h:135
Definition: TcpLayer.h:95
TcpOption(uint8_t *optionRawData)
Definition: TcpLayer.h:204
TcpOptionType getTcpOptionType() const
Definition: TcpLayer.h:215
Definition: TcpLayer.h:97
uint16_t cwrFlag
Definition: TcpLayer.h:32
uint16_t portDst
Definition: TcpLayer.h:26
Definition: TcpLayer.h:89
Definition: TcpLayer.h:127
Definition: TcpLayer.h:91
uint16_t finFlag
Definition: TcpLayer.h:32
TcpOptionBuilder(TcpOptionType optionType, uint32_t optionValue)
Definition: TcpLayer.h:304
Definition: TcpLayer.h:103
Definition: TcpLayer.h:137
size_t getHeaderLen() const
Definition: TcpLayer.h:468
OsiModelLayer getOsiModelLayer() const
Definition: TcpLayer.h:477
Definition: TcpLayer.h:93
Definition: TcpLayer.h:107
TcpOptionBuilder(TcpOptionType optionType, uint16_t optionValue)
Definition: TcpLayer.h:295