PcapPlusPlus  19.12
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  {
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 
375  TcpOption getTcpOption(TcpOptionType option) const;
376 
382 
390  TcpOption getNextTcpOption(TcpOption& tcpOption) const;
391 
395  size_t getTcpOptionCount() const;
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  size_t getHeaderLen() const { return getTcpHeader()->dataOffset*4 ;}
451 
455  void computeCalculateFields();
456 
457  std::string toString() const;
458 
460 
461  private:
462 
463  TLVRecordReader<TcpOption> m_OptionReader;
464  int m_NumOfTrailingBytes;
465 
466  void initLayer();
467  uint8_t* getOptionsBasePtr() const { 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 */
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:224
pcpp::TLVRecord
Definition: TLVData.h:23
pcpp::TCPOPT_MSS
Definition: TcpLayer.h:93
pcpp::Packet
Definition: Packet.h:26
pcpp::TCPOPT_SNACK
Definition: TcpLayer.h:119
pcpp::TCPOPT_EXP_FD
Definition: TcpLayer.h:129
Layer.h
pcpp::TLVRecord::TLVRawData::recordType
uint8_t recordType
Definition: TLVData.h:31
pcpp::TcpLayer::getTcpHeader
tcphdr * getTcpHeader() const
Definition: TcpLayer.h:367
pcpp::TcpLayer::TcpLayer
TcpLayer()
pcpp::TcpOptionBuilder::NopEolOptionTypes
NopEolOptionTypes
Definition: TcpLayer.h:262
pcpp::tcphdr::ackFlag
uint16_t ackFlag
Definition: TcpLayer.h:32
pcpp::TcpOptionBuilder::TcpOptionBuilder
TcpOptionBuilder(TcpOptionType optionType, uint8_t optionValue)
Definition: TcpLayer.h:286
pcpp::tcphdr
Definition: TcpLayer.h:22
pcpp::tcphdr::cwrFlag
uint16_t cwrFlag
Definition: TcpLayer.h:32
pcpp::TcpOption::getTotalSize
size_t getTotalSize() const
Definition: TcpLayer.h:225
pcpp::TcpOptionBuilder::TcpOptionBuilder
TcpOptionBuilder(TcpOptionType optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: TcpLayer.h:277
pcpp::TCPOPT_MD5
Definition: TcpLayer.h:113
pcpp::TcpLayer::removeTcpOption
bool removeTcpOption(TcpOptionType optionType)
pcpp::TcpOptionBuilder::EOL
Definition: TcpLayer.h:267
pcpp::TcpLayer::addTcpOption
TcpOption addTcpOption(const TcpOptionBuilder &optionBuilder)
pcpp::TcpOption::TcpOption
TcpOption(uint8_t *optionRawData)
Definition: TcpLayer.h:204
pcpp::tcphdr::windowSize
uint16_t windowSize
Definition: TcpLayer.h:75
pcpp::tcphdr::urgentPointer
uint16_t urgentPointer
Definition: TcpLayer.h:79
pcpp::TCPOPT_RECBOUND
Definition: TcpLayer.h:121
pcpp::TCPOPT_EXP_FE
Definition: TcpLayer.h:131
pcpp::TCPOPT_MPTCP
Definition: TcpLayer.h:115
pcpp::TcpOptionBuilder::TcpOptionBuilder
TcpOptionBuilder(TcpOptionType optionType, uint16_t optionValue)
Definition: TcpLayer.h:295
pcpp::tcphdr::pshFlag
uint16_t pshFlag
Definition: TcpLayer.h:32
TLVData.h
pcpp::TCPOPT_QS
Definition: TcpLayer.h:125
pcpp::TcpOptionBuilder::TcpOptionBuilder
TcpOptionBuilder(TcpOptionType optionType, uint32_t optionValue)
Definition: TcpLayer.h:304
pcpp::tcphdr::rstFlag
uint16_t rstFlag
Definition: TcpLayer.h:32
pcpp::TcpLayer::calculateChecksum
uint16_t calculateChecksum(bool writeResultToPacket)
pcpp::tcphdr::eceFlag
uint16_t eceFlag
Definition: TcpLayer.h:32
pcpp::TcpOptionBuilder
Definition: TcpLayer.h:254
pcpp::TCPOPT_CCECHO
Definition: TcpLayer.h:111
pcpp::TcpLayer::getHeaderLen
size_t getHeaderLen() const
Definition: TcpLayer.h:450
pcpp::PCPP_TCPOPT_SACK
Definition: TcpLayer.h:99
pcpp::TcpLayer::getTcpOptionCount
size_t getTcpOptionCount() const
pcpp::TcpLayer::getNextTcpOption
TcpOption getNextTcpOption(TcpOption &tcpOption) const
pcpp::PCPP_TCPOPT_TIMESTAMP
Definition: TcpLayer.h:105
pcpp::TcpLayer::removeAllTcpOptions
bool removeAllTcpOptions()
pcpp::OsiModelTransportLayer
Definition: ProtocolType.h:233
pcpp::TCPOPT_SCPS
Definition: TcpLayer.h:117
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::TLVRecordBuilder
Definition: TLVData.h:351
pcpp::TCPOPT_RVBD_TRPY
Definition: TcpLayer.h:135
pcpp::tcphdr::synFlag
uint16_t synFlag
Definition: TcpLayer.h:32
pcpp::tcphdr::finFlag
uint16_t finFlag
Definition: TcpLayer.h:32
pcpp::TCPOPT_CCNEW
Definition: TcpLayer.h:109
pcpp::Layer
Definition: Layer.h:70
pcpp::TcpOptionType
TcpOptionType
Definition: TcpLayer.h:87
pcpp::TcpOption::getDataSize
size_t getDataSize() const
Definition: TcpLayer.h:236
pcpp::TcpLayer::toString
std::string toString() const
pcpp::TcpLayer::getOsiModelLayer
OsiModelLayer getOsiModelLayer() const
Definition: TcpLayer.h:459
pcpp::TLVRecordReader
Definition: TLVData.h:197
pcpp::tcphdr::sequenceNumber
uint32_t sequenceNumber
Definition: TcpLayer.h:28
pcpp::TcpOption::getTcpOptionType
TcpOptionType getTcpOptionType() const
Definition: TcpLayer.h:215
pcpp::TCPOPT_CORREXP
Definition: TcpLayer.h:123
pcpp::TcpOption
Definition: TcpLayer.h:196
pcpp::PCPP_TCPOPT_NOP
Definition: TcpLayer.h:89
pcpp::tcphdr::portDst
uint16_t portDst
Definition: TcpLayer.h:26
pcpp::TcpLayer::getFirstTcpOption
TcpOption getFirstTcpOption() const
pcpp::PCPP_TCPOPT_EOL
Definition: TcpLayer.h:91
pcpp::TcpOption::~TcpOption
~TcpOption()
Definition: TcpLayer.h:209
pcpp::PCPP_TCPOPT_WINDOW
Definition: TcpLayer.h:95
pcpp::tcphdr::headerChecksum
uint16_t headerChecksum
Definition: TcpLayer.h:77
pcpp::TcpLayer::computeCalculateFields
void computeCalculateFields()
pcpp::tcphdr::dataOffset
uint16_t dataOffset
Definition: TcpLayer.h:32
pcpp::tcphdr::portSrc
uint16_t portSrc
Definition: TcpLayer.h:24
pcpp::TcpLayer::addTcpOptionAfter
TcpOption addTcpOptionAfter(const TcpOptionBuilder &optionBuilder, TcpOptionType prevOptionType=TCPOPT_Unknown)
pcpp::TcpLayer::operator=
TcpLayer & operator=(const TcpLayer &other)
pcpp::TCPOPT_SACK_PERM
Definition: TcpLayer.h:97
pcpp::TcpLayer::parseNextLayer
void parseNextLayer()
pcpp::TcpOptionBuilder::NOP
Definition: TcpLayer.h:265
pcpp::TCPOPT_Unknown
Definition: TcpLayer.h:137
pcpp::TCPOPT_USER_TO
Definition: TcpLayer.h:127
pcpp::TLVRecord::TLVRawData::recordLen
uint8_t recordLen
Definition: TLVData.h:33
pcpp::TCPOPT_CC
Definition: TcpLayer.h:107
pcpp::TcpOptionBuilder::build
TcpOption build() const
pcpp::TCPOPT_RVBD_PROBE
Definition: TcpLayer.h:133
pcpp::tcphdr::ackNumber
uint32_t ackNumber
Definition: TcpLayer.h:30
pcpp::TcpLayer
Definition: TcpLayer.h:327
pcpp::tcphdr::urgFlag
uint16_t urgFlag
Definition: TcpLayer.h:32
pcpp::TCPOPT_ECHOREPLY
Definition: TcpLayer.h:103
pcpp::TcpLayer::getTcpOption
TcpOption getTcpOption(TcpOptionType option) const
pcpp::TCPOPT_ECHO
Definition: TcpLayer.h:101