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 <string.h>
6 
8 
13 namespace pcpp
14 {
15 
20 #pragma pack(push,1)
21  struct tcphdr {
23  uint16_t portSrc;
25  uint16_t portDst;
27  uint32_t sequenceNumber;
29  uint32_t ackNumber;
30 #if (BYTE_ORDER == LITTLE_ENDIAN)
31  uint16_t reserved:4,
33  dataOffset:4,
35  finFlag:1,
37  synFlag:1,
39  rstFlag:1,
41  pshFlag:1,
43  ackFlag:1,
45  urgFlag:1,
47  eceFlag:1,
49  cwrFlag:1;
50 #elif (BYTE_ORDER == BIG_ENDIAN)
51 
52  uint16_t dataOffset:4,
53  reserved:4,
55  cwrFlag:1,
57  eceFlag:1,
59  urgFlag:1,
61  ackFlag:1,
63  pshFlag:1,
65  rstFlag:1,
67  synFlag:1,
69  finFlag:1;
70 #else
71 #error "Endian is not LE nor BE..."
72 #endif
73 
74  uint16_t windowSize;
76  uint16_t headerChecksum;
78  uint16_t urgentPointer;
79  };
80 #pragma pack(pop)
81 
82 
86  enum TcpOption {
106  TCPOPT_CC = 11,
114  TCPOPT_MPTCP = 0x1e,
124  TCPOPT_QS = 27,
135  };
136 
137 
138  // TCP option lengths
139 
141 #define PCPP_TCPOLEN_NOP 1
142 
143 #define PCPP_TCPOLEN_EOL 1
144 
145 #define PCPP_TCPOLEN_MSS 4
146 
147 #define PCPP_TCPOLEN_WINDOW 3
148 
149 #define PCPP_TCPOLEN_SACK_PERM 2
150 
151 #define PCPP_TCPOLEN_SACK_MIN 2
152 
153 #define PCPP_TCPOLEN_ECHO 6
154 
155 #define PCPP_TCPOLEN_ECHOREPLY 6
156 
157 #define PCPP_TCPOLEN_TIMESTAMP 10
158 
159 #define PCPP_TCPOLEN_CC 6
160 
161 #define PCPP_TCPOLEN_CCNEW 6
162 
163 #define PCPP_TCPOLEN_CCECHO 6
164 
165 #define PCPP_TCPOLEN_MD5 18
166 
167 #define PCPP_TCPOLEN_MPTCP_MIN 8
168 
169 #define PCPP_TCPOLEN_SCPS 4
170 
171 #define PCPP_TCPOLEN_SNACK 6
172 
173 #define PCPP_TCPOLEN_RECBOUND 2
174 
175 #define PCPP_TCPOLEN_CORREXP 2
176 
177 #define PCPP_TCPOLEN_QS 8
178 
179 #define PCPP_TCPOLEN_USER_TO 4
180 
181 #define PCPP_TCPOLEN_RVBD_PROBE_MIN 3
182 
183 #define PCPP_TCPOLEN_RVBD_TRPY_MIN 16
184 
185 #define PCPP_TCPOLEN_EXP_MIN 2
186 
192  {
193  public:
195  uint8_t option;
197  uint8_t len;
199  uint8_t value[];
200 
210  template<typename T>
211  T getValueAs(int valueOffset = 0)
212  {
213  if (getTotalSize() <= 2*sizeof(uint8_t) + valueOffset)
214  return 0;
215  if (getTotalSize() - 2*sizeof(uint8_t) - valueOffset < sizeof(T))
216  return 0;
217 
218  T result;
219  memcpy(&result, value+valueOffset, sizeof(T));
220  return result;
221  }
222 
231  template<typename T>
232  void setValue(T newValue, int valueOffset = 0)
233  {
234  memcpy(value+valueOffset, &newValue, sizeof(T));
235  }
236 
241  inline size_t getTotalSize() const
242  {
243  if (option == (uint8_t)PCPP_TCPOPT_NOP || option == (uint8_t)PCPP_TCPOPT_EOL)
244  return sizeof(uint8_t);
245 
246  return (size_t)len;
247  }
248 
252  inline TcpOption getType() {return (TcpOption)option;}
253  private:
254  // private c'tor which isn't implemented to make this struct impossible to construct
255  TcpOptionData();
256  };
257 
258 
263  class TcpLayer : public Layer
264  {
265  public:
273  TcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
274 
278  TcpLayer();
279 
285  TcpLayer(uint16_t portSrc, uint16_t portDst);
286 
287  ~TcpLayer() {}
288 
292  TcpLayer(const TcpLayer& other);
293 
297  TcpLayer& operator=(const TcpLayer& other);
298 
303  inline tcphdr* getTcpHeader() { return (tcphdr*)m_Data; };
304 
311 
317 
326 
330  size_t getTcpOptionsCount();
331 
342  TcpOptionData* addTcpOption(TcpOption optionType, uint8_t optionLength, const uint8_t* optionData);
343 
356  TcpOptionData* addTcpOptionAfter(TcpOption optionType, uint8_t optionLength, const uint8_t* optionData, TcpOptionData* prevOption);
357 
364  bool removeTcpOption(TcpOption optionType);
365 
371  bool removeAllTcpOptions();
372 
373 
379  uint16_t calculateChecksum(bool writeResultToPacket);
380 
381  // implement abstract methods
382 
386  void parseNextLayer();
387 
391  inline size_t getHeaderLen() { return getTcpHeader()->dataOffset*4 ;}
392 
396  void computeCalculateFields();
397 
398  std::string toString();
399 
401 
402  private:
403 
404  size_t m_TcpOptionsCount;
405  int m_NumOfTrailingBytes;
406 
407  void initLayer();
408  TcpOptionData* castPtrToTcpOptionData(uint8_t* ptr);
409  TcpOptionData* addTcpOptionAt(TcpOption optionType, uint8_t optionLength, const uint8_t* optionData, int offset);
410  void adjustTcpOptionTrailer(size_t totalOptSize);
411  void copyLayerData(const TcpLayer& other);
412  };
413 
414 } // namespace pcpp
415 
416 #endif /* PACKETPP_TCP_LAYER */
uint8_t option
Definition: TcpLayer.h:195
uint16_t cwrFlag
Definition: TcpLayer.h:31
Definition: TcpLayer.h:96
Definition: TcpLayer.h:126
bool removeTcpOption(TcpOption optionType)
TcpOption getType()
Definition: TcpLayer.h:252
Definition: TcpLayer.h:191
uint16_t headerChecksum
Definition: TcpLayer.h:76
Definition: TcpLayer.h:100
TcpOptionData * addTcpOptionAfter(TcpOption optionType, uint8_t optionLength, const uint8_t *optionData, TcpOptionData *prevOption)
Definition: TcpLayer.h:88
uint16_t synFlag
Definition: TcpLayer.h:31
Definition: TcpLayer.h:108
OsiModelLayer getOsiModelLayer()
Definition: TcpLayer.h:400
Definition: TcpLayer.h:124
Definition: TcpLayer.h:116
Definition: TcpLayer.h:106
Definition: TcpLayer.h:130
Definition: TcpLayer.h:94
uint8_t len
Definition: TcpLayer.h:197
uint16_t finFlag
Definition: TcpLayer.h:31
Definition: Packet.h:26
Definition: TcpLayer.h:102
The main namespace for the PcapPlusPlus lib.
uint32_t ackNumber
Definition: TcpLayer.h:29
Definition: TcpLayer.h:112
uint8_t value[]
Definition: TcpLayer.h:199
uint16_t windowSize
Definition: TcpLayer.h:74
Definition: TcpLayer.h:134
uint16_t portSrc
Definition: TcpLayer.h:23
void computeCalculateFields()
OsiModelLayer
Definition: ProtocolType.h:208
void parseNextLayer()
Definition: TcpLayer.h:21
uint16_t calculateChecksum(bool writeResultToPacket)
size_t getTotalSize() const
Definition: TcpLayer.h:241
Definition: TcpLayer.h:122
TcpOptionData * addTcpOption(TcpOption optionType, uint8_t optionLength, const uint8_t *optionData)
Definition: TcpLayer.h:110
uint32_t sequenceNumber
Definition: TcpLayer.h:27
bool removeAllTcpOptions()
size_t getTcpOptionsCount()
Definition: TcpLayer.h:114
Definition: TcpLayer.h:263
uint16_t pshFlag
Definition: TcpLayer.h:31
tcphdr * getTcpHeader()
Definition: TcpLayer.h:303
uint16_t urgentPointer
Definition: TcpLayer.h:78
void setValue(T newValue, int valueOffset=0)
Definition: TcpLayer.h:232
Definition: ProtocolType.h:217
TcpLayer & operator=(const TcpLayer &other)
TcpOptionData * getFirstTcpOptionData()
Definition: Layer.h:70
Definition: TcpLayer.h:132
Definition: TcpLayer.h:128
TcpOptionData * getTcpOptionData(TcpOption option)
T getValueAs(int valueOffset=0)
Definition: TcpLayer.h:211
uint16_t portDst
Definition: TcpLayer.h:25
Definition: TcpLayer.h:98
size_t getHeaderLen()
Definition: TcpLayer.h:391
uint16_t rstFlag
Definition: TcpLayer.h:31
uint16_t dataOffset
Definition: TcpLayer.h:31
Definition: TcpLayer.h:118
Definition: TcpLayer.h:92
Definition: TcpLayer.h:120
Definition: TcpLayer.h:90
uint16_t urgFlag
Definition: TcpLayer.h:31
Definition: TcpLayer.h:104
std::string toString()
uint16_t ackFlag
Definition: TcpLayer.h:31
uint16_t eceFlag
Definition: TcpLayer.h:31
TcpOption
Definition: TcpLayer.h:86
TcpOptionData * getNextTcpOptionData(TcpOptionData *tcpOption)