PcapPlusPlus
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  inline 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()
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  {
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  inline tcphdr* getTcpHeader() { return (tcphdr*)m_Data; }
368 
376 
382 
391 
395  size_t getTcpOptionCount();
396 
404  TcpOption addTcpOption(const TcpOptionBuilder& optionBuilder);
405 
415  TcpOption addTcpOptionAfter(const TcpOptionBuilder& optionBuilder, TcpOptionType prevOptionType = TCPOPT_Unknown);
416 
423  bool removeTcpOption(TcpOptionType optionType);
424 
430  bool removeAllTcpOptions();
431 
432 
438  uint16_t calculateChecksum(bool writeResultToPacket);
439 
440  // implement abstract methods
441 
445  void parseNextLayer();
446 
450  inline size_t getHeaderLen() { return getTcpHeader()->dataOffset*4 ;}
451 
455  void computeCalculateFields();
456 
457  std::string toString();
458 
460 
461  private:
462 
463  TLVRecordReader<TcpOption> m_OptionReader;
464  int m_NumOfTrailingBytes;
465 
466  void initLayer();
467  inline uint8_t* getOptionsBasePtr() { return m_Data + sizeof(tcphdr); }
468  TcpOption addTcpOptionAt(const TcpOptionBuilder& optionBuilder, int offset);
469  void adjustTcpOptionTrailer(size_t totalOptSize);
470  void copyLayerData(const TcpLayer& other);
471  };
472 
473 } // namespace pcpp
474 
475 #endif /* PACKETPP_TCP_LAYER */
TcpOption getNextTcpOption(TcpOption &tcpOption)
uint8_t recordLen
Definition: TLVData.h:33
Definition: TcpLayer.h:137
uint16_t finFlag
Definition: TcpLayer.h:32
uint16_t urgFlag
Definition: TcpLayer.h:32
Definition: TcpLayer.h:91
Definition: TcpLayer.h:131
size_t getTotalSize() const
Definition: TcpLayer.h:225
uint8_t recordType
Definition: TLVData.h:31
Definition: ProtocolType.h:222
Definition: TcpLayer.h:129
Definition: TcpLayer.h:107
uint16_t eceFlag
Definition: TcpLayer.h:32
uint16_t portSrc
Definition: TcpLayer.h:24
TcpOption getFirstTcpOption()
Definition: TcpLayer.h:95
Definition: TLVData.h:333
TcpOptionBuilder(TcpOptionType optionType, uint32_t optionValue)
Definition: TcpLayer.h:304
TcpOptionBuilder(TcpOptionType optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: TcpLayer.h:277
Definition: TLVData.h:23
tcphdr * getTcpHeader()
Definition: TcpLayer.h:367
Definition: TcpLayer.h:103
uint32_t ackNumber
Definition: TcpLayer.h:30
Definition: TcpLayer.h:127
TcpOption addTcpOption(const TcpOptionBuilder &optionBuilder)
std::string toString()
uint16_t pshFlag
Definition: TcpLayer.h:32
uint16_t rstFlag
Definition: TcpLayer.h:32
Definition: TcpLayer.h:22
Definition: Layer.h:70
Definition: TcpLayer.h:265
Definition: TcpLayer.h:97
size_t getHeaderLen()
Definition: TcpLayer.h:450
Definition: Packet.h:26
NopEolOptionTypes
Definition: TcpLayer.h:262
Definition: TcpLayer.h:111
Definition: TLVData.h:189
Definition: TcpLayer.h:133
Definition: TcpLayer.h:135
Definition: TcpLayer.h:254
uint16_t portDst
Definition: TcpLayer.h:26
Definition: TcpLayer.h:125
Definition: TcpLayer.h:267
Definition: TcpLayer.h:89
uint16_t ackFlag
Definition: TcpLayer.h:32
uint32_t sequenceNumber
Definition: TcpLayer.h:28
OsiModelLayer
Definition: ProtocolType.h:213
void parseNextLayer()
Definition: TcpLayer.h:99
TcpOption(uint8_t *optionRawData)
Definition: TcpLayer.h:204
Definition: TcpLayer.h:115
TcpOption addTcpOptionAfter(const TcpOptionBuilder &optionBuilder, TcpOptionType prevOptionType=TCPOPT_Unknown)
size_t getTcpOptionCount()
TcpOption getTcpOption(TcpOptionType option)
TcpOptionBuilder(TcpOptionType optionType, uint16_t optionValue)
Definition: TcpLayer.h:295
Definition: TcpLayer.h:117
Definition: TcpLayer.h:119
uint16_t dataOffset
Definition: TcpLayer.h:32
TcpOptionBuilder(TcpOptionType optionType, uint8_t optionValue)
Definition: TcpLayer.h:286
TcpOption build() const
OsiModelLayer getOsiModelLayer()
Definition: TcpLayer.h:459
Definition: TcpLayer.h:113
uint16_t headerChecksum
Definition: TcpLayer.h:77
uint16_t cwrFlag
Definition: TcpLayer.h:32
Definition: TcpLayer.h:196
Definition: TcpLayer.h:109
TcpOptionType getTcpOptionType()
Definition: TcpLayer.h:215
uint16_t calculateChecksum(bool writeResultToPacket)
TcpOptionType
Definition: TcpLayer.h:87
bool removeTcpOption(TcpOptionType optionType)
Definition: TcpLayer.h:93
uint16_t urgentPointer
Definition: TcpLayer.h:79
Definition: TcpLayer.h:123
TcpLayer & operator=(const TcpLayer &other)
size_t getDataSize()
Definition: TcpLayer.h:236
Definition: TcpLayer.h:327
void computeCalculateFields()
~TcpOption()
Definition: TcpLayer.h:209
Definition: TcpLayer.h:121
Definition: TcpLayer.h:101
The main namespace for the PcapPlusPlus lib.
Definition: TcpLayer.h:105
uint16_t windowSize
Definition: TcpLayer.h:75
bool removeAllTcpOptions()
uint16_t synFlag
Definition: TcpLayer.h:32