PcapPlusPlus  Next
QuicLayer.h
Go to the documentation of this file.
1 #pragma once
2 #include "Layer.h"
3 #include "Packet.h"
4 #include <vector>
5 #include <string>
6 
8 
11 namespace pcpp
12 {
15  class QuicV1Layer : public Layer
16  {
17  public:
22  enum class QuicPacketType : uint8_t
23  {
26  Initial = 0,
28  ZeroRTT = 1,
30  Handshake = 2,
33  Retry = 3,
36  VersionNegotiation = 253,
38  OneRtt = 254,
39  };
40 
44  enum class QuicHeaderForm : uint8_t
45  {
48  ShortHeader = 0,
51  LongHeader = 1
52  };
53 
57  {
59  const uint8_t* data;
61  size_t length;
62  };
63 
71  static QuicV1Layer* parseQuicLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
72 
74  virtual QuicPacketType getPacketType() const = 0;
75 
79 
81  uint8_t getFixedBit() const;
82 
86  static bool isQuicPort(uint16_t port)
87  {
88  return port == 443;
89  }
90 
91  // implement abstract methods
92 
97  void parseNextLayer() override;
98 
100  void computeCalculateFields() override
101  {}
102 
104  std::string toString() const override;
105 
108  {
109  return OsiModelTransportLayer;
110  }
111 
112  protected:
113  QuicV1Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
114  : Layer(data, dataLen, prevLayer, packet, QUICv1)
115  {}
116 
122  {
123 #if (BYTE_ORDER == LITTLE_ENDIAN)
124  uint8_t : 6, fixedBit : 1, headerForm : 1;
125 #else
126  uint8_t headerForm : 1, fixedBit : 1, : 6;
127 #endif
128  };
129 
130 #pragma pack(push, 1)
138  {
139 #if (BYTE_ORDER == LITTLE_ENDIAN)
140  uint8_t packetNumberLength : 2, reserved : 2, longPacketType : 2, fixedBit : 1, headerForm : 1;
141 #else
142  uint8_t headerForm : 1, fixedBit : 1, longPacketType : 2, reserved : 2, packetNumberLength : 2;
143 #endif
144  uint32_t version;
145  uint8_t destinationConnectionIdLength;
146  };
147 #pragma pack(pop)
148  static_assert(sizeof(quic_long_header) == 6, "quic_long_header size is not 6 bytes");
149 
150 #pragma pack(push, 1)
157  {
158 #if (BYTE_ORDER == LITTLE_ENDIAN)
159  uint8_t packetNumberLength : 2, keyPhase : 1, reserved : 2, spinBit : 1, fixedBit : 1, headerForm : 1;
160 #else
161  uint8_t headerForm : 1, fixedBit : 1, spinBit : 1, reserved : 2, keyPhase : 1, packetNumberLength : 2;
162 #endif
163  };
164 #pragma pack(pop)
165  static_assert(sizeof(quic_short_header) == 1, "quic_short_header size is not 1 byte");
166 
167  private:
168  quic_common_header* getCommonHeader() const
169  {
170  return reinterpret_cast<quic_common_header*>(m_Data);
171  }
172  };
173 
180  {
181  public:
183  QuicPacketType getPacketType() const override;
184 
186  uint32_t getVersion() const;
187 
190  std::vector<uint8_t> getDestinationConnectionId() const;
191 
195 
198  std::vector<uint8_t> getSourceConnectionId() const;
199 
202  std::string getSourceConnectionIdAsString() const;
203 
204  protected:
205  using QuicV1Layer::QuicV1Layer;
206 
212  {
214  size_t length;
216  size_t offset;
217 
222  {}
223  };
224 
225  OffsetAndLength getDestConIdOffsetAndLength() const;
226 
227  OffsetAndLength getSrcConIdOffsetAndLength() const;
228 
229  private:
230  static constexpr int destinationConnectionIdOffset = sizeof(quic_long_header);
231 
232  static bool isDataValid(const uint8_t* data, size_t dataLen);
233 
234  quic_long_header* getLongHeader() const
235  {
236  return reinterpret_cast<quic_long_header*>(m_Data);
237  }
238 
239  friend class QuicV1Layer;
240  };
241 
248  {
249  public:
252  uint64_t getLength() const;
253 
254  // implement abstract methods
255 
259  size_t getHeaderLen() const override;
260 
266 
267  protected:
274  {
276  uint64_t value;
278  size_t size;
279 
284  {}
285  };
286 
287  virtual size_t getLengthOffset() const;
288 
289  VarintValueAndSize getVarintValueAndSize(size_t offset) const;
290 
291  private:
292  using QuicV1LongHeaderLayer::QuicV1LongHeaderLayer;
293  };
294 
300  {
301  public:
304  std::vector<uint8_t> getToken() const;
305 
308  std::string getTokenAsString() const;
309 
310  private:
311  using QuicV1EstablishmentLayer::QuicV1EstablishmentLayer;
312 
313  size_t getLengthOffset() const override;
314 
315  size_t getTokenLengthOffset() const;
316 
317  friend class QuicV1Layer;
318  };
319 
325  {
326  using QuicV1EstablishmentLayer::QuicV1EstablishmentLayer;
327 
328  friend class QuicV1Layer;
329  };
330 
336  {
337  using QuicV1EstablishmentLayer::QuicV1EstablishmentLayer;
338 
339  friend class QuicV1Layer;
340  };
341 
348  {
349  public:
352  std::vector<uint8_t> getRetryToken() const;
353 
356  std::string getRetryTokenAsString() const;
357 
360  std::vector<uint8_t> getRetryIntegrityTag() const;
361 
364  std::string getRetryIntegrityTagAsString() const;
365 
366  // implement abstract methods
367 
370  size_t getHeaderLen() const override
371  {
372  return m_DataLen;
373  }
374 
375  private:
376  using QuicV1LongHeaderLayer::QuicV1LongHeaderLayer;
377 
378  static constexpr size_t retryIntegritySize = 16;
379 
380  size_t getRetryTokenOffset() const;
381 
382  friend class QuicV1Layer;
383  };
384 
391  {
392  public:
394  QuicPacketType getPacketType() const override
395  {
397  }
398 
401  std::vector<uint32_t> getSupportedVersions() const;
402 
403  // implement abstract methods
404 
407  size_t getHeaderLen() const override
408  {
409  return m_DataLen;
410  }
411 
412  private:
413  using QuicV1LongHeaderLayer::QuicV1LongHeaderLayer;
414 
415  friend class QuicV1Layer;
416  };
417 
423  {
424  public:
426  QuicPacketType getPacketType() const override
427  {
428  return QuicPacketType::OneRtt;
429  }
430 
433  bool getSpinBit() const
434  {
435  return getShortHeader()->spinBit;
436  }
437 
440  bool getKeyPhaseBit() const
441  {
442  return getShortHeader()->keyPhase;
443  }
444 
449 
450  // implement abstract methods
451 
454  size_t getHeaderLen() const override
455  {
456  return m_DataLen;
457  }
458 
459  private:
460  using QuicV1Layer::QuicV1Layer;
461 
462  static bool isDataValid(const uint8_t* data, size_t dataLen);
463 
464  quic_short_header* getShortHeader() const
465  {
466  return reinterpret_cast<quic_short_header*>(m_Data);
467  }
468 
469  friend class QuicV1Layer;
470  };
471 } // namespace pcpp
Definition: Layer.h:116
Definition: Packet.h:48
Definition: QuicLayer.h:248
size_t getHeaderLen() const override
ProtectedPayload getProtectedPayload() const
Definition: QuicLayer.h:336
Definition: QuicLayer.h:300
std::string getTokenAsString() const
std::vector< uint8_t > getToken() const
Definition: QuicLayer.h:16
QuicHeaderForm
Definition: QuicLayer.h:45
QuicPacketType
Definition: QuicLayer.h:23
@ Handshake
Handshake packet - carries the remainder of the cryptographic handshake.
@ ZeroRTT
0-RTT packet - carries application data sent before the handshake completes
@ OneRtt
1-RTT packet - a short-header packet carrying post-handshake application data
std::string toString() const override
uint8_t getFixedBit() const
void computeCalculateFields() override
Does nothing for this layer.
Definition: QuicLayer.h:100
static bool isQuicPort(uint16_t port)
Definition: QuicLayer.h:86
static QuicV1Layer * parseQuicLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
OsiModelLayer getOsiModelLayer() const override
Definition: QuicLayer.h:107
void parseNextLayer() override
QuicHeaderForm getHeaderForm() const
virtual QuicPacketType getPacketType() const =0
Definition: QuicLayer.h:180
std::vector< uint8_t > getDestinationConnectionId() const
std::vector< uint8_t > getSourceConnectionId() const
std::string getSourceConnectionIdAsString() const
QuicPacketType getPacketType() const override
uint32_t getVersion() const
std::string getDestinationConnectionIdAsString() const
Definition: QuicLayer.h:423
QuicPacketType getPacketType() const override
Definition: QuicLayer.h:426
ProtectedPayload getProtectedPayload() const
bool getSpinBit() const
Definition: QuicLayer.h:433
bool getKeyPhaseBit() const
Definition: QuicLayer.h:440
size_t getHeaderLen() const override
Definition: QuicLayer.h:454
Definition: QuicLayer.h:348
std::string getRetryIntegrityTagAsString() const
size_t getHeaderLen() const override
Definition: QuicLayer.h:370
std::vector< uint8_t > getRetryToken() const
std::vector< uint8_t > getRetryIntegrityTag() const
std::string getRetryTokenAsString() const
Definition: QuicLayer.h:391
size_t getHeaderLen() const override
Definition: QuicLayer.h:407
QuicPacketType getPacketType() const override
Definition: QuicLayer.h:394
std::vector< uint32_t > getSupportedVersions() const
Definition: QuicLayer.h:325
const ProtocolType QUICv1
QUIC v1 protocol.
Definition: ProtocolType.h:266
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:272
@ OsiModelTransportLayer
Transport layer (layer 4)
Definition: ProtocolType.h:280
VarintValueAndSize(uint64_t value, size_t size)
Definition: QuicLayer.h:283
uint64_t value
The varint's decoded value.
Definition: QuicLayer.h:276
size_t size
The varint's encoded size on the wire, in bytes (1, 2, 4 or 8)
Definition: QuicLayer.h:278
Definition: QuicLayer.h:57
const uint8_t * data
Pointer to the beginning of the protected data.
Definition: QuicLayer.h:59
size_t length
Length of the protected data in bytes.
Definition: QuicLayer.h:61
Definition: QuicLayer.h:122
Definition: QuicLayer.h:138
Definition: QuicLayer.h:157
size_t offset
The field's offset from the start of the packet, in bytes.
Definition: QuicLayer.h:216
size_t length
The field's length, in bytes.
Definition: QuicLayer.h:214
OffsetAndLength(size_t length, size_t offset)
Definition: QuicLayer.h:221