blob: e8e1931b9755ed1db8f276e0a4b555f105493d68 [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"
QUICHE teama6ef0a62019-03-07 20:34:33 -050027#include "net/third_party/quiche/src/quic/platform/api/quic_uint128.h"
dmcardlecf0bfcf2019-12-13 08:08:21 -080028#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050029
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
danzhddf023a2019-12-20 13:33:36 -0800124 QuicPacketHeader& operator=(const QuicPacketHeader& other);
125
QUICHE teama6ef0a62019-03-07 20:34:33 -0500126 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
127 std::ostream& os,
128 const QuicPacketHeader& header);
129
130 // Universal header. All QuicPacket headers will have a connection_id and
131 // public flags.
132 QuicConnectionId destination_connection_id;
133 QuicConnectionIdIncluded destination_connection_id_included;
134 QuicConnectionId source_connection_id;
135 QuicConnectionIdIncluded source_connection_id_included;
136 // This is only used for Google QUIC.
137 bool reset_flag;
138 // For Google QUIC, version flag in packets from the server means version
139 // negotiation packet. For IETF QUIC, version flag means long header.
140 bool version_flag;
141 // Indicates whether |possible_stateless_reset_token| contains a valid value
142 // parsed from the packet buffer. IETF QUIC only, always false for GQUIC.
143 bool has_possible_stateless_reset_token;
144 QuicPacketNumberLength packet_number_length;
dschinazi244f6dc2019-05-06 15:45:16 -0700145 uint8_t type_byte;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500146 ParsedQuicVersion version;
147 // nonce contains an optional, 32-byte nonce value. If not included in the
148 // packet, |nonce| will be empty.
149 DiversificationNonce* nonce;
150 QuicPacketNumber packet_number;
151 // Format of this header.
152 PacketHeaderFormat form;
153 // Short packet type is reflected in packet_number_length.
154 QuicLongHeaderType long_packet_type;
155 // Only valid if |has_possible_stateless_reset_token| is true.
156 // Stores last 16 bytes of a this packet, used to check whether this packet is
157 // a stateless reset packet on decryption failure.
158 QuicUint128 possible_stateless_reset_token;
159 // Length of the retry token length variable length integer field,
160 // carried only by v99 IETF Initial packets.
161 QuicVariableLengthIntegerLength retry_token_length_length;
162 // Retry token, carried only by v99 IETF Initial packets.
dmcardlecf0bfcf2019-12-13 08:08:21 -0800163 quiche::QuicheStringPiece retry_token;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500164 // Length of the length variable length integer field,
165 // carried only by v99 IETF Initial, 0-RTT and Handshake packets.
166 QuicVariableLengthIntegerLength length_length;
167 // Length of the packet number and payload, carried only by v99 IETF Initial,
168 // 0-RTT and Handshake packets. Also includes the length of the
169 // diversification nonce in server to client 0-RTT packets.
170 QuicByteCount remaining_packet_length;
171};
172
173struct QUIC_EXPORT_PRIVATE QuicPublicResetPacket {
174 QuicPublicResetPacket();
175 explicit QuicPublicResetPacket(QuicConnectionId connection_id);
176
177 QuicConnectionId connection_id;
178 QuicPublicResetNonceProof nonce_proof;
179 QuicSocketAddress client_address;
180 // An arbitrary string to identify an endpoint. Used by clients to
181 // differentiate traffic from Google servers vs Non-google servers.
182 // Will not be used if empty().
vasilvvc48c8712019-03-11 13:38:16 -0700183 std::string endpoint_id;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500184};
185
186struct QUIC_EXPORT_PRIVATE QuicVersionNegotiationPacket {
187 QuicVersionNegotiationPacket();
188 explicit QuicVersionNegotiationPacket(QuicConnectionId connection_id);
189 QuicVersionNegotiationPacket(const QuicVersionNegotiationPacket& other);
190 ~QuicVersionNegotiationPacket();
191
192 QuicConnectionId connection_id;
193 ParsedQuicVersionVector versions;
194};
195
196struct QUIC_EXPORT_PRIVATE QuicIetfStatelessResetPacket {
197 QuicIetfStatelessResetPacket();
198 QuicIetfStatelessResetPacket(const QuicPacketHeader& header,
199 QuicUint128 token);
200 QuicIetfStatelessResetPacket(const QuicIetfStatelessResetPacket& other);
201 ~QuicIetfStatelessResetPacket();
202
203 QuicPacketHeader header;
204 QuicUint128 stateless_reset_token;
205};
206
207class QUIC_EXPORT_PRIVATE QuicData {
208 public:
dschinazi4b5a68a2019-08-15 15:45:36 -0700209 // Creates a QuicData from a buffer and length. Does not own the buffer.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500210 QuicData(const char* buffer, size_t length);
dschinazi4b5a68a2019-08-15 15:45:36 -0700211 // Creates a QuicData from a buffer and length,
212 // optionally taking ownership of the buffer.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500213 QuicData(const char* buffer, size_t length, bool owns_buffer);
dmcardlecf0bfcf2019-12-13 08:08:21 -0800214 // Creates a QuicData from a quiche::QuicheStringPiece. Does not own the
215 // buffer.
216 QuicData(quiche::QuicheStringPiece data);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500217 QuicData(const QuicData&) = delete;
218 QuicData& operator=(const QuicData&) = delete;
219 virtual ~QuicData();
220
dmcardlecf0bfcf2019-12-13 08:08:21 -0800221 quiche::QuicheStringPiece AsStringPiece() const {
222 return quiche::QuicheStringPiece(data(), length());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500223 }
224
225 const char* data() const { return buffer_; }
226 size_t length() const { return length_; }
227
228 private:
229 const char* buffer_;
230 size_t length_;
231 bool owns_buffer_;
232};
233
234class QUIC_EXPORT_PRIVATE QuicPacket : public QuicData {
235 public:
236 QuicPacket(char* buffer,
237 size_t length,
238 bool owns_buffer,
239 QuicConnectionIdLength destination_connection_id_length,
240 QuicConnectionIdLength source_connection_id_length,
241 bool includes_version,
242 bool includes_diversification_nonce,
243 QuicPacketNumberLength packet_number_length,
244 QuicVariableLengthIntegerLength retry_token_length_length,
245 QuicByteCount retry_token_length,
246 QuicVariableLengthIntegerLength length_length);
247 QuicPacket(QuicTransportVersion version,
248 char* buffer,
249 size_t length,
250 bool owns_buffer,
251 const QuicPacketHeader& header);
252 QuicPacket(const QuicPacket&) = delete;
253 QuicPacket& operator=(const QuicPacket&) = delete;
254
dmcardlecf0bfcf2019-12-13 08:08:21 -0800255 quiche::QuicheStringPiece AssociatedData(QuicTransportVersion version) const;
256 quiche::QuicheStringPiece Plaintext(QuicTransportVersion version) const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500257
258 char* mutable_data() { return buffer_; }
259
260 private:
261 char* buffer_;
262 const QuicConnectionIdLength destination_connection_id_length_;
263 const QuicConnectionIdLength source_connection_id_length_;
264 const bool includes_version_;
265 const bool includes_diversification_nonce_;
266 const QuicPacketNumberLength packet_number_length_;
267 const QuicVariableLengthIntegerLength retry_token_length_length_;
268 const QuicByteCount retry_token_length_;
269 const QuicVariableLengthIntegerLength length_length_;
270};
271
272class QUIC_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
273 public:
dschinazi4b5a68a2019-08-15 15:45:36 -0700274 // Creates a QuicEncryptedPacket from a buffer and length.
275 // Does not own the buffer.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500276 QuicEncryptedPacket(const char* buffer, size_t length);
dschinazi4b5a68a2019-08-15 15:45:36 -0700277 // Creates a QuicEncryptedPacket from a buffer and length,
278 // optionally taking ownership of the buffer.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500279 QuicEncryptedPacket(const char* buffer, size_t length, bool owns_buffer);
dmcardlecf0bfcf2019-12-13 08:08:21 -0800280 // Creates a QuicEncryptedPacket from a quiche::QuicheStringPiece.
dschinazi4b5a68a2019-08-15 15:45:36 -0700281 // Does not own the buffer.
dmcardlecf0bfcf2019-12-13 08:08:21 -0800282 QuicEncryptedPacket(quiche::QuicheStringPiece data);
dschinazi4b5a68a2019-08-15 15:45:36 -0700283
QUICHE teama6ef0a62019-03-07 20:34:33 -0500284 QuicEncryptedPacket(const QuicEncryptedPacket&) = delete;
285 QuicEncryptedPacket& operator=(const QuicEncryptedPacket&) = delete;
286
287 // Clones the packet into a new packet which owns the buffer.
288 std::unique_ptr<QuicEncryptedPacket> Clone() const;
289
290 // By default, gtest prints the raw bytes of an object. The bool data
291 // member (in the base class QuicData) causes this object to have padding
292 // bytes, which causes the default gtest object printer to read
293 // uninitialize memory. So we need to teach gtest how to print this object.
294 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
295 std::ostream& os,
296 const QuicEncryptedPacket& s);
297};
298
299// A received encrypted QUIC packet, with a recorded time of receipt.
300class QUIC_EXPORT_PRIVATE QuicReceivedPacket : public QuicEncryptedPacket {
301 public:
302 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time);
303 QuicReceivedPacket(const char* buffer,
304 size_t length,
305 QuicTime receipt_time,
306 bool owns_buffer);
307 QuicReceivedPacket(const char* buffer,
308 size_t length,
309 QuicTime receipt_time,
310 bool owns_buffer,
311 int ttl,
312 bool ttl_valid);
313 QuicReceivedPacket(const char* buffer,
314 size_t length,
315 QuicTime receipt_time,
316 bool owns_buffer,
317 int ttl,
318 bool ttl_valid,
319 char* packet_headers,
320 size_t headers_length,
321 bool owns_header_buffer);
322 ~QuicReceivedPacket();
323 QuicReceivedPacket(const QuicReceivedPacket&) = delete;
324 QuicReceivedPacket& operator=(const QuicReceivedPacket&) = delete;
325
326 // Clones the packet into a new packet which owns the buffer.
327 std::unique_ptr<QuicReceivedPacket> Clone() const;
328
329 // Returns the time at which the packet was received.
330 QuicTime receipt_time() const { return receipt_time_; }
331
332 // This is the TTL of the packet, assuming ttl_vaild_ is true.
333 int ttl() const { return ttl_; }
334
335 // Start of packet headers.
336 char* packet_headers() const { return packet_headers_; }
337
338 // Length of packet headers.
339 int headers_length() const { return headers_length_; }
340
341 // By default, gtest prints the raw bytes of an object. The bool data
342 // member (in the base class QuicData) causes this object to have padding
343 // bytes, which causes the default gtest object printer to read
344 // uninitialize memory. So we need to teach gtest how to print this object.
345 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
346 std::ostream& os,
347 const QuicReceivedPacket& s);
348
349 private:
350 const QuicTime receipt_time_;
351 int ttl_;
352 // Points to the start of packet headers.
353 char* packet_headers_;
354 // Length of packet headers.
355 int headers_length_;
356 // Whether owns the buffer for packet headers.
357 bool owns_header_buffer_;
358};
359
360struct QUIC_EXPORT_PRIVATE SerializedPacket {
361 SerializedPacket(QuicPacketNumber packet_number,
362 QuicPacketNumberLength packet_number_length,
363 const char* encrypted_buffer,
364 QuicPacketLength encrypted_length,
365 bool has_ack,
366 bool has_stop_waiting);
367 SerializedPacket(const SerializedPacket& other);
368 SerializedPacket& operator=(const SerializedPacket& other);
369 SerializedPacket(SerializedPacket&& other);
370 ~SerializedPacket();
371
372 // Not owned.
373 const char* encrypted_buffer;
374 QuicPacketLength encrypted_length;
375 QuicFrames retransmittable_frames;
fayang51152fd2019-10-21 06:48:09 -0700376 QuicFrames nonretransmittable_frames;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500377 IsHandshake has_crypto_handshake;
378 // -1: full padding to the end of a max-sized packet
379 // 0: no padding
380 // otherwise: only pad up to num_padding_bytes bytes
381 int16_t num_padding_bytes;
382 QuicPacketNumber packet_number;
383 QuicPacketNumberLength packet_number_length;
384 EncryptionLevel encryption_level;
fayang02a28742019-12-04 07:09:38 -0800385 // TODO(fayang): Remove has_ack and has_stop_waiting.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500386 bool has_ack;
387 bool has_stop_waiting;
388 TransmissionType transmission_type;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500389 // The largest acked of the AckFrame in this packet if has_ack is true,
390 // 0 otherwise.
391 QuicPacketNumber largest_acked;
fayangfb5fb612019-10-21 13:19:14 -0700392 // Indicates whether this packet has a copy of ack frame in
393 // nonretransmittable_frames.
394 bool has_ack_frame_copy;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500395};
396
fayangfb5fb612019-10-21 13:19:14 -0700397// Make a copy of |serialized| (including the underlying frames). |copy_buffer|
398// indicates whether the encrypted buffer should be copied.
399QUIC_EXPORT_PRIVATE SerializedPacket* CopySerializedPacket(
400 const SerializedPacket& serialized,
401 QuicBufferAllocator* allocator,
402 bool copy_buffer);
403
QUICHE teama6ef0a62019-03-07 20:34:33 -0500404// Deletes and clears all the frames and the packet from serialized packet.
405QUIC_EXPORT_PRIVATE void ClearSerializedPacket(
406 SerializedPacket* serialized_packet);
407
408// Allocates a new char[] of size |packet.encrypted_length| and copies in
409// |packet.encrypted_buffer|.
410QUIC_EXPORT_PRIVATE char* CopyBuffer(const SerializedPacket& packet);
fayang58f71072019-11-05 08:47:02 -0800411// Allocates a new char[] of size |encrypted_length| and copies in
412// |encrypted_buffer|.
413QUIC_EXPORT_PRIVATE char* CopyBuffer(const char* encrypted_buffer,
414 QuicPacketLength encrypted_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500415
416struct QUIC_EXPORT_PRIVATE SerializedPacketDeleter {
417 void operator()(SerializedPacket* packet) {
418 if (packet->encrypted_buffer != nullptr) {
419 delete[] packet->encrypted_buffer;
420 }
421 delete packet;
422 }
423};
424
425// On destruction, OwningSerializedPacketPointer deletes a packet's (on-heap)
426// encrypted_buffer before deleting the (also on-heap) packet itself.
427// TODO(wub): Maybe delete retransmittable_frames too?
428typedef std::unique_ptr<SerializedPacket, SerializedPacketDeleter>
429 OwningSerializedPacketPointer;
430
431// Context for an incoming packet.
432struct QUIC_EXPORT_PRIVATE QuicPerPacketContext {
433 virtual ~QuicPerPacketContext() {}
434};
435
fayang1ed1f762019-06-24 11:40:04 -0700436// ReceivedPacketInfo comprises information obtained by parsing the unencrypted
437// bytes of a received packet.
438struct QUIC_EXPORT_PRIVATE ReceivedPacketInfo {
439 ReceivedPacketInfo(const QuicSocketAddress& self_address,
440 const QuicSocketAddress& peer_address,
441 const QuicReceivedPacket& packet);
442 ReceivedPacketInfo(const ReceivedPacketInfo& other) = default;
443
444 ~ReceivedPacketInfo();
445
446 std::string ToString() const;
447
448 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
449 std::ostream& os,
450 const ReceivedPacketInfo& packet_info);
451
452 const QuicSocketAddress& self_address;
453 const QuicSocketAddress& peer_address;
454 const QuicReceivedPacket& packet;
455
fayang1ed1f762019-06-24 11:40:04 -0700456 PacketHeaderFormat form;
fayange3f2f7b2019-09-19 17:01:57 -0700457 // This is only used if the form is IETF_QUIC_LONG_HEADER_PACKET.
458 QuicLongHeaderType long_packet_type;
fayang1ed1f762019-06-24 11:40:04 -0700459 bool version_flag;
dschinazi48ac9192019-07-31 00:07:26 -0700460 bool use_length_prefix;
fayang1ed1f762019-06-24 11:40:04 -0700461 QuicVersionLabel version_label;
462 ParsedQuicVersion version;
463 QuicConnectionId destination_connection_id;
464 QuicConnectionId source_connection_id;
465};
466
QUICHE teama6ef0a62019-03-07 20:34:33 -0500467} // namespace quic
468
469#endif // QUICHE_QUIC_CORE_QUIC_PACKETS_H_