blob: 93417ac1d48ebe68741919501245595747ec132a [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef QUICHE_QUIC_CORE_QUIC_PACKETS_H_
6#define QUICHE_QUIC_CORE_QUIC_PACKETS_H_
7
8#include <cstddef>
9#include <cstdint>
10#include <limits>
11#include <list>
12#include <memory>
13#include <ostream>
14#include <utility>
15#include <vector>
16
QUICHE teama6ef0a62019-03-07 20:34:33 -050017#include "net/third_party/quiche/src/quic/core/frames/quic_frame.h"
18#include "net/third_party/quiche/src/quic/core/quic_ack_listener_interface.h"
19#include "net/third_party/quiche/src/quic/core/quic_bandwidth.h"
20#include "net/third_party/quiche/src/quic/core/quic_constants.h"
21#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
22#include "net/third_party/quiche/src/quic/core/quic_time.h"
23#include "net/third_party/quiche/src/quic/core/quic_types.h"
24#include "net/third_party/quiche/src/quic/core/quic_versions.h"
25#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
26#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
27#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
28#include "net/third_party/quiche/src/quic/platform/api/quic_uint128.h"
29
30namespace quic {
31
32class QuicPacket;
33struct QuicPacketHeader;
34
QUICHE team2252b702019-05-14 23:55:14 -040035// Returns the destination connection ID of |header| when |perspective| is
36// server, and the source connection ID when |perspective| is client.
37QUIC_EXPORT_PRIVATE QuicConnectionId
38GetServerConnectionIdAsRecipient(const QuicPacketHeader& header,
39 Perspective perspective);
40
41// Returns the destination connection ID of |header| when |perspective| is
42// client, and the source connection ID when |perspective| is server.
43QUIC_EXPORT_PRIVATE QuicConnectionId
dschinaziac6805d2019-05-30 09:44:27 -070044GetClientConnectionIdAsRecipient(const QuicPacketHeader& header,
45 Perspective perspective);
46
47// Returns the destination connection ID of |header| when |perspective| is
48// client, and the source connection ID when |perspective| is server.
49QUIC_EXPORT_PRIVATE QuicConnectionId
QUICHE team2252b702019-05-14 23:55:14 -040050GetServerConnectionIdAsSender(const QuicPacketHeader& header,
51 Perspective perspective);
52
53// Returns the destination connection ID included of |header| when |perspective|
54// is client, and the source connection ID included when |perspective| is
55// server.
56QUIC_EXPORT_PRIVATE QuicConnectionIdIncluded
57GetServerConnectionIdIncludedAsSender(const QuicPacketHeader& header,
58 Perspective perspective);
59
dschinaziac6805d2019-05-30 09:44:27 -070060// Returns the destination connection ID of |header| when |perspective| is
61// server, and the source connection ID when |perspective| is client.
62QUIC_EXPORT_PRIVATE QuicConnectionId
63GetClientConnectionIdAsSender(const QuicPacketHeader& header,
64 Perspective perspective);
65
QUICHE team2252b702019-05-14 23:55:14 -040066// Returns the destination connection ID included of |header| when |perspective|
67// is server, and the source connection ID included when |perspective| is
68// client.
69QUIC_EXPORT_PRIVATE QuicConnectionIdIncluded
70GetClientConnectionIdIncludedAsSender(const QuicPacketHeader& header,
71 Perspective perspective);
72
QUICHE teama6ef0a62019-03-07 20:34:33 -050073// Number of connection ID bytes that are actually included over the wire.
74QUIC_EXPORT_PRIVATE QuicConnectionIdLength
75GetIncludedConnectionIdLength(QuicConnectionId connection_id,
76 QuicConnectionIdIncluded connection_id_included);
77
78// Number of destination connection ID bytes that are actually included over the
79// wire for this particular header.
80QUIC_EXPORT_PRIVATE QuicConnectionIdLength
81GetIncludedDestinationConnectionIdLength(const QuicPacketHeader& header);
82
83// Number of source connection ID bytes that are actually included over the
84// wire for this particular header.
85QUIC_EXPORT_PRIVATE QuicConnectionIdLength
86GetIncludedSourceConnectionIdLength(const QuicPacketHeader& header);
87
88// Size in bytes of the data packet header.
89QUIC_EXPORT_PRIVATE size_t GetPacketHeaderSize(QuicTransportVersion version,
90 const QuicPacketHeader& header);
91
92QUIC_EXPORT_PRIVATE size_t
93GetPacketHeaderSize(QuicTransportVersion version,
94 QuicConnectionIdLength destination_connection_id_length,
95 QuicConnectionIdLength source_connection_id_length,
96 bool include_version,
97 bool include_diversification_nonce,
98 QuicPacketNumberLength packet_number_length,
99 QuicVariableLengthIntegerLength retry_token_length_length,
100 QuicByteCount retry_token_length,
101 QuicVariableLengthIntegerLength length_length);
102
103// Index of the first byte in a QUIC packet of encrypted data.
104QUIC_EXPORT_PRIVATE size_t
105GetStartOfEncryptedData(QuicTransportVersion version,
106 const QuicPacketHeader& header);
107
108QUIC_EXPORT_PRIVATE size_t GetStartOfEncryptedData(
109 QuicTransportVersion version,
110 QuicConnectionIdLength destination_connection_id_length,
111 QuicConnectionIdLength source_connection_id_length,
112 bool include_version,
113 bool include_diversification_nonce,
114 QuicPacketNumberLength packet_number_length,
115 QuicVariableLengthIntegerLength retry_token_length_length,
116 QuicByteCount retry_token_length,
117 QuicVariableLengthIntegerLength length_length);
118
119struct QUIC_EXPORT_PRIVATE QuicPacketHeader {
120 QuicPacketHeader();
121 QuicPacketHeader(const QuicPacketHeader& other);
122 ~QuicPacketHeader();
123
124 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
125 std::ostream& os,
126 const QuicPacketHeader& header);
127
128 // Universal header. All QuicPacket headers will have a connection_id and
129 // public flags.
130 QuicConnectionId destination_connection_id;
131 QuicConnectionIdIncluded destination_connection_id_included;
132 QuicConnectionId source_connection_id;
133 QuicConnectionIdIncluded source_connection_id_included;
134 // This is only used for Google QUIC.
135 bool reset_flag;
136 // For Google QUIC, version flag in packets from the server means version
137 // negotiation packet. For IETF QUIC, version flag means long header.
138 bool version_flag;
139 // Indicates whether |possible_stateless_reset_token| contains a valid value
140 // parsed from the packet buffer. IETF QUIC only, always false for GQUIC.
141 bool has_possible_stateless_reset_token;
142 QuicPacketNumberLength packet_number_length;
dschinazi244f6dc2019-05-06 15:45:16 -0700143 uint8_t type_byte;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500144 ParsedQuicVersion version;
145 // nonce contains an optional, 32-byte nonce value. If not included in the
146 // packet, |nonce| will be empty.
147 DiversificationNonce* nonce;
148 QuicPacketNumber packet_number;
149 // Format of this header.
150 PacketHeaderFormat form;
151 // Short packet type is reflected in packet_number_length.
152 QuicLongHeaderType long_packet_type;
153 // Only valid if |has_possible_stateless_reset_token| is true.
154 // Stores last 16 bytes of a this packet, used to check whether this packet is
155 // a stateless reset packet on decryption failure.
156 QuicUint128 possible_stateless_reset_token;
157 // Length of the retry token length variable length integer field,
158 // carried only by v99 IETF Initial packets.
159 QuicVariableLengthIntegerLength retry_token_length_length;
160 // Retry token, carried only by v99 IETF Initial packets.
161 QuicStringPiece retry_token;
162 // Length of the length variable length integer field,
163 // carried only by v99 IETF Initial, 0-RTT and Handshake packets.
164 QuicVariableLengthIntegerLength length_length;
165 // Length of the packet number and payload, carried only by v99 IETF Initial,
166 // 0-RTT and Handshake packets. Also includes the length of the
167 // diversification nonce in server to client 0-RTT packets.
168 QuicByteCount remaining_packet_length;
169};
170
171struct QUIC_EXPORT_PRIVATE QuicPublicResetPacket {
172 QuicPublicResetPacket();
173 explicit QuicPublicResetPacket(QuicConnectionId connection_id);
174
175 QuicConnectionId connection_id;
176 QuicPublicResetNonceProof nonce_proof;
177 QuicSocketAddress client_address;
178 // An arbitrary string to identify an endpoint. Used by clients to
179 // differentiate traffic from Google servers vs Non-google servers.
180 // Will not be used if empty().
vasilvvc48c8712019-03-11 13:38:16 -0700181 std::string endpoint_id;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500182};
183
184struct QUIC_EXPORT_PRIVATE QuicVersionNegotiationPacket {
185 QuicVersionNegotiationPacket();
186 explicit QuicVersionNegotiationPacket(QuicConnectionId connection_id);
187 QuicVersionNegotiationPacket(const QuicVersionNegotiationPacket& other);
188 ~QuicVersionNegotiationPacket();
189
190 QuicConnectionId connection_id;
191 ParsedQuicVersionVector versions;
192};
193
194struct QUIC_EXPORT_PRIVATE QuicIetfStatelessResetPacket {
195 QuicIetfStatelessResetPacket();
196 QuicIetfStatelessResetPacket(const QuicPacketHeader& header,
197 QuicUint128 token);
198 QuicIetfStatelessResetPacket(const QuicIetfStatelessResetPacket& other);
199 ~QuicIetfStatelessResetPacket();
200
201 QuicPacketHeader header;
202 QuicUint128 stateless_reset_token;
203};
204
205class QUIC_EXPORT_PRIVATE QuicData {
206 public:
dschinazi4b5a68a2019-08-15 15:45:36 -0700207 // Creates a QuicData from a buffer and length. Does not own the buffer.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500208 QuicData(const char* buffer, size_t length);
dschinazi4b5a68a2019-08-15 15:45:36 -0700209 // Creates a QuicData from a buffer and length,
210 // optionally taking ownership of the buffer.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500211 QuicData(const char* buffer, size_t length, bool owns_buffer);
dschinazi4b5a68a2019-08-15 15:45:36 -0700212 // Creates a QuicData from a QuicStringPiece. Does not own the buffer.
213 QuicData(QuicStringPiece data);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500214 QuicData(const QuicData&) = delete;
215 QuicData& operator=(const QuicData&) = delete;
216 virtual ~QuicData();
217
218 QuicStringPiece AsStringPiece() const {
219 return QuicStringPiece(data(), length());
220 }
221
222 const char* data() const { return buffer_; }
223 size_t length() const { return length_; }
224
225 private:
226 const char* buffer_;
227 size_t length_;
228 bool owns_buffer_;
229};
230
231class QUIC_EXPORT_PRIVATE QuicPacket : public QuicData {
232 public:
233 QuicPacket(char* buffer,
234 size_t length,
235 bool owns_buffer,
236 QuicConnectionIdLength destination_connection_id_length,
237 QuicConnectionIdLength source_connection_id_length,
238 bool includes_version,
239 bool includes_diversification_nonce,
240 QuicPacketNumberLength packet_number_length,
241 QuicVariableLengthIntegerLength retry_token_length_length,
242 QuicByteCount retry_token_length,
243 QuicVariableLengthIntegerLength length_length);
244 QuicPacket(QuicTransportVersion version,
245 char* buffer,
246 size_t length,
247 bool owns_buffer,
248 const QuicPacketHeader& header);
249 QuicPacket(const QuicPacket&) = delete;
250 QuicPacket& operator=(const QuicPacket&) = delete;
251
252 QuicStringPiece AssociatedData(QuicTransportVersion version) const;
253 QuicStringPiece Plaintext(QuicTransportVersion version) const;
254
255 char* mutable_data() { return buffer_; }
256
257 private:
258 char* buffer_;
259 const QuicConnectionIdLength destination_connection_id_length_;
260 const QuicConnectionIdLength source_connection_id_length_;
261 const bool includes_version_;
262 const bool includes_diversification_nonce_;
263 const QuicPacketNumberLength packet_number_length_;
264 const QuicVariableLengthIntegerLength retry_token_length_length_;
265 const QuicByteCount retry_token_length_;
266 const QuicVariableLengthIntegerLength length_length_;
267};
268
269class QUIC_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
270 public:
dschinazi4b5a68a2019-08-15 15:45:36 -0700271 // Creates a QuicEncryptedPacket from a buffer and length.
272 // Does not own the buffer.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500273 QuicEncryptedPacket(const char* buffer, size_t length);
dschinazi4b5a68a2019-08-15 15:45:36 -0700274 // Creates a QuicEncryptedPacket from a buffer and length,
275 // optionally taking ownership of the buffer.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500276 QuicEncryptedPacket(const char* buffer, size_t length, bool owns_buffer);
dschinazi4b5a68a2019-08-15 15:45:36 -0700277 // Creates a QuicEncryptedPacket from a QuicStringPiece.
278 // Does not own the buffer.
279 QuicEncryptedPacket(QuicStringPiece data);
280
QUICHE teama6ef0a62019-03-07 20:34:33 -0500281 QuicEncryptedPacket(const QuicEncryptedPacket&) = delete;
282 QuicEncryptedPacket& operator=(const QuicEncryptedPacket&) = delete;
283
284 // Clones the packet into a new packet which owns the buffer.
285 std::unique_ptr<QuicEncryptedPacket> Clone() const;
286
287 // By default, gtest prints the raw bytes of an object. The bool data
288 // member (in the base class QuicData) causes this object to have padding
289 // bytes, which causes the default gtest object printer to read
290 // uninitialize memory. So we need to teach gtest how to print this object.
291 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
292 std::ostream& os,
293 const QuicEncryptedPacket& s);
294};
295
296// A received encrypted QUIC packet, with a recorded time of receipt.
297class QUIC_EXPORT_PRIVATE QuicReceivedPacket : public QuicEncryptedPacket {
298 public:
299 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time);
300 QuicReceivedPacket(const char* buffer,
301 size_t length,
302 QuicTime receipt_time,
303 bool owns_buffer);
304 QuicReceivedPacket(const char* buffer,
305 size_t length,
306 QuicTime receipt_time,
307 bool owns_buffer,
308 int ttl,
309 bool ttl_valid);
310 QuicReceivedPacket(const char* buffer,
311 size_t length,
312 QuicTime receipt_time,
313 bool owns_buffer,
314 int ttl,
315 bool ttl_valid,
316 char* packet_headers,
317 size_t headers_length,
318 bool owns_header_buffer);
319 ~QuicReceivedPacket();
320 QuicReceivedPacket(const QuicReceivedPacket&) = delete;
321 QuicReceivedPacket& operator=(const QuicReceivedPacket&) = delete;
322
323 // Clones the packet into a new packet which owns the buffer.
324 std::unique_ptr<QuicReceivedPacket> Clone() const;
325
326 // Returns the time at which the packet was received.
327 QuicTime receipt_time() const { return receipt_time_; }
328
329 // This is the TTL of the packet, assuming ttl_vaild_ is true.
330 int ttl() const { return ttl_; }
331
332 // Start of packet headers.
333 char* packet_headers() const { return packet_headers_; }
334
335 // Length of packet headers.
336 int headers_length() const { return headers_length_; }
337
338 // By default, gtest prints the raw bytes of an object. The bool data
339 // member (in the base class QuicData) causes this object to have padding
340 // bytes, which causes the default gtest object printer to read
341 // uninitialize memory. So we need to teach gtest how to print this object.
342 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
343 std::ostream& os,
344 const QuicReceivedPacket& s);
345
346 private:
347 const QuicTime receipt_time_;
348 int ttl_;
349 // Points to the start of packet headers.
350 char* packet_headers_;
351 // Length of packet headers.
352 int headers_length_;
353 // Whether owns the buffer for packet headers.
354 bool owns_header_buffer_;
355};
356
357struct QUIC_EXPORT_PRIVATE SerializedPacket {
358 SerializedPacket(QuicPacketNumber packet_number,
359 QuicPacketNumberLength packet_number_length,
360 const char* encrypted_buffer,
361 QuicPacketLength encrypted_length,
362 bool has_ack,
363 bool has_stop_waiting);
364 SerializedPacket(const SerializedPacket& other);
365 SerializedPacket& operator=(const SerializedPacket& other);
366 SerializedPacket(SerializedPacket&& other);
367 ~SerializedPacket();
368
369 // Not owned.
370 const char* encrypted_buffer;
371 QuicPacketLength encrypted_length;
372 QuicFrames retransmittable_frames;
fayang51152fd2019-10-21 06:48:09 -0700373 QuicFrames nonretransmittable_frames;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500374 IsHandshake has_crypto_handshake;
375 // -1: full padding to the end of a max-sized packet
376 // 0: no padding
377 // otherwise: only pad up to num_padding_bytes bytes
378 int16_t num_padding_bytes;
379 QuicPacketNumber packet_number;
380 QuicPacketNumberLength packet_number_length;
381 EncryptionLevel encryption_level;
fayang02a28742019-12-04 07:09:38 -0800382 // TODO(fayang): Remove has_ack and has_stop_waiting.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500383 bool has_ack;
384 bool has_stop_waiting;
385 TransmissionType transmission_type;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500386 // The largest acked of the AckFrame in this packet if has_ack is true,
387 // 0 otherwise.
388 QuicPacketNumber largest_acked;
fayangfb5fb612019-10-21 13:19:14 -0700389 // Indicates whether this packet has a copy of ack frame in
390 // nonretransmittable_frames.
391 bool has_ack_frame_copy;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500392};
393
fayangfb5fb612019-10-21 13:19:14 -0700394// Make a copy of |serialized| (including the underlying frames). |copy_buffer|
395// indicates whether the encrypted buffer should be copied.
396QUIC_EXPORT_PRIVATE SerializedPacket* CopySerializedPacket(
397 const SerializedPacket& serialized,
398 QuicBufferAllocator* allocator,
399 bool copy_buffer);
400
QUICHE teama6ef0a62019-03-07 20:34:33 -0500401// Deletes and clears all the frames and the packet from serialized packet.
402QUIC_EXPORT_PRIVATE void ClearSerializedPacket(
403 SerializedPacket* serialized_packet);
404
405// Allocates a new char[] of size |packet.encrypted_length| and copies in
406// |packet.encrypted_buffer|.
407QUIC_EXPORT_PRIVATE char* CopyBuffer(const SerializedPacket& packet);
fayang58f71072019-11-05 08:47:02 -0800408// Allocates a new char[] of size |encrypted_length| and copies in
409// |encrypted_buffer|.
410QUIC_EXPORT_PRIVATE char* CopyBuffer(const char* encrypted_buffer,
411 QuicPacketLength encrypted_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500412
413struct QUIC_EXPORT_PRIVATE SerializedPacketDeleter {
414 void operator()(SerializedPacket* packet) {
415 if (packet->encrypted_buffer != nullptr) {
416 delete[] packet->encrypted_buffer;
417 }
418 delete packet;
419 }
420};
421
422// On destruction, OwningSerializedPacketPointer deletes a packet's (on-heap)
423// encrypted_buffer before deleting the (also on-heap) packet itself.
424// TODO(wub): Maybe delete retransmittable_frames too?
425typedef std::unique_ptr<SerializedPacket, SerializedPacketDeleter>
426 OwningSerializedPacketPointer;
427
428// Context for an incoming packet.
429struct QUIC_EXPORT_PRIVATE QuicPerPacketContext {
430 virtual ~QuicPerPacketContext() {}
431};
432
fayang1ed1f762019-06-24 11:40:04 -0700433// ReceivedPacketInfo comprises information obtained by parsing the unencrypted
434// bytes of a received packet.
435struct QUIC_EXPORT_PRIVATE ReceivedPacketInfo {
436 ReceivedPacketInfo(const QuicSocketAddress& self_address,
437 const QuicSocketAddress& peer_address,
438 const QuicReceivedPacket& packet);
439 ReceivedPacketInfo(const ReceivedPacketInfo& other) = default;
440
441 ~ReceivedPacketInfo();
442
443 std::string ToString() const;
444
445 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
446 std::ostream& os,
447 const ReceivedPacketInfo& packet_info);
448
449 const QuicSocketAddress& self_address;
450 const QuicSocketAddress& peer_address;
451 const QuicReceivedPacket& packet;
452
fayang1ed1f762019-06-24 11:40:04 -0700453 PacketHeaderFormat form;
fayange3f2f7b2019-09-19 17:01:57 -0700454 // This is only used if the form is IETF_QUIC_LONG_HEADER_PACKET.
455 QuicLongHeaderType long_packet_type;
fayang1ed1f762019-06-24 11:40:04 -0700456 bool version_flag;
dschinazi48ac9192019-07-31 00:07:26 -0700457 bool use_length_prefix;
fayang1ed1f762019-06-24 11:40:04 -0700458 QuicVersionLabel version_label;
459 ParsedQuicVersion version;
460 QuicConnectionId destination_connection_id;
461 QuicConnectionId source_connection_id;
462};
463
QUICHE teama6ef0a62019-03-07 20:34:33 -0500464} // namespace quic
465
466#endif // QUICHE_QUIC_CORE_QUIC_PACKETS_H_