blob: 9e97fa90a3228229009056cad934e34860352f6a [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
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.
dmcardlecf0bfcf2019-12-13 08:08:21 -0800161 quiche::QuicheStringPiece retry_token;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500162 // 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);
dmcardlecf0bfcf2019-12-13 08:08:21 -0800212 // Creates a QuicData from a quiche::QuicheStringPiece. Does not own the
213 // buffer.
214 QuicData(quiche::QuicheStringPiece data);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500215 QuicData(const QuicData&) = delete;
216 QuicData& operator=(const QuicData&) = delete;
217 virtual ~QuicData();
218
dmcardlecf0bfcf2019-12-13 08:08:21 -0800219 quiche::QuicheStringPiece AsStringPiece() const {
220 return quiche::QuicheStringPiece(data(), length());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500221 }
222
223 const char* data() const { return buffer_; }
224 size_t length() const { return length_; }
225
226 private:
227 const char* buffer_;
228 size_t length_;
229 bool owns_buffer_;
230};
231
232class QUIC_EXPORT_PRIVATE QuicPacket : public QuicData {
233 public:
234 QuicPacket(char* buffer,
235 size_t length,
236 bool owns_buffer,
237 QuicConnectionIdLength destination_connection_id_length,
238 QuicConnectionIdLength source_connection_id_length,
239 bool includes_version,
240 bool includes_diversification_nonce,
241 QuicPacketNumberLength packet_number_length,
242 QuicVariableLengthIntegerLength retry_token_length_length,
243 QuicByteCount retry_token_length,
244 QuicVariableLengthIntegerLength length_length);
245 QuicPacket(QuicTransportVersion version,
246 char* buffer,
247 size_t length,
248 bool owns_buffer,
249 const QuicPacketHeader& header);
250 QuicPacket(const QuicPacket&) = delete;
251 QuicPacket& operator=(const QuicPacket&) = delete;
252
dmcardlecf0bfcf2019-12-13 08:08:21 -0800253 quiche::QuicheStringPiece AssociatedData(QuicTransportVersion version) const;
254 quiche::QuicheStringPiece Plaintext(QuicTransportVersion version) const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500255
256 char* mutable_data() { return buffer_; }
257
258 private:
259 char* buffer_;
260 const QuicConnectionIdLength destination_connection_id_length_;
261 const QuicConnectionIdLength source_connection_id_length_;
262 const bool includes_version_;
263 const bool includes_diversification_nonce_;
264 const QuicPacketNumberLength packet_number_length_;
265 const QuicVariableLengthIntegerLength retry_token_length_length_;
266 const QuicByteCount retry_token_length_;
267 const QuicVariableLengthIntegerLength length_length_;
268};
269
270class QUIC_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
271 public:
dschinazi4b5a68a2019-08-15 15:45:36 -0700272 // Creates a QuicEncryptedPacket from a buffer and length.
273 // Does not own the buffer.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500274 QuicEncryptedPacket(const char* buffer, size_t length);
dschinazi4b5a68a2019-08-15 15:45:36 -0700275 // Creates a QuicEncryptedPacket from a buffer and length,
276 // optionally taking ownership of the buffer.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500277 QuicEncryptedPacket(const char* buffer, size_t length, bool owns_buffer);
dmcardlecf0bfcf2019-12-13 08:08:21 -0800278 // Creates a QuicEncryptedPacket from a quiche::QuicheStringPiece.
dschinazi4b5a68a2019-08-15 15:45:36 -0700279 // Does not own the buffer.
dmcardlecf0bfcf2019-12-13 08:08:21 -0800280 QuicEncryptedPacket(quiche::QuicheStringPiece data);
dschinazi4b5a68a2019-08-15 15:45:36 -0700281
QUICHE teama6ef0a62019-03-07 20:34:33 -0500282 QuicEncryptedPacket(const QuicEncryptedPacket&) = delete;
283 QuicEncryptedPacket& operator=(const QuicEncryptedPacket&) = delete;
284
285 // Clones the packet into a new packet which owns the buffer.
286 std::unique_ptr<QuicEncryptedPacket> Clone() const;
287
288 // By default, gtest prints the raw bytes of an object. The bool data
289 // member (in the base class QuicData) causes this object to have padding
290 // bytes, which causes the default gtest object printer to read
291 // uninitialize memory. So we need to teach gtest how to print this object.
292 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
293 std::ostream& os,
294 const QuicEncryptedPacket& s);
295};
296
297// A received encrypted QUIC packet, with a recorded time of receipt.
298class QUIC_EXPORT_PRIVATE QuicReceivedPacket : public QuicEncryptedPacket {
299 public:
300 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time);
301 QuicReceivedPacket(const char* buffer,
302 size_t length,
303 QuicTime receipt_time,
304 bool owns_buffer);
305 QuicReceivedPacket(const char* buffer,
306 size_t length,
307 QuicTime receipt_time,
308 bool owns_buffer,
309 int ttl,
310 bool ttl_valid);
311 QuicReceivedPacket(const char* buffer,
312 size_t length,
313 QuicTime receipt_time,
314 bool owns_buffer,
315 int ttl,
316 bool ttl_valid,
317 char* packet_headers,
318 size_t headers_length,
319 bool owns_header_buffer);
320 ~QuicReceivedPacket();
321 QuicReceivedPacket(const QuicReceivedPacket&) = delete;
322 QuicReceivedPacket& operator=(const QuicReceivedPacket&) = delete;
323
324 // Clones the packet into a new packet which owns the buffer.
325 std::unique_ptr<QuicReceivedPacket> Clone() const;
326
327 // Returns the time at which the packet was received.
328 QuicTime receipt_time() const { return receipt_time_; }
329
330 // This is the TTL of the packet, assuming ttl_vaild_ is true.
331 int ttl() const { return ttl_; }
332
333 // Start of packet headers.
334 char* packet_headers() const { return packet_headers_; }
335
336 // Length of packet headers.
337 int headers_length() const { return headers_length_; }
338
339 // By default, gtest prints the raw bytes of an object. The bool data
340 // member (in the base class QuicData) causes this object to have padding
341 // bytes, which causes the default gtest object printer to read
342 // uninitialize memory. So we need to teach gtest how to print this object.
343 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
344 std::ostream& os,
345 const QuicReceivedPacket& s);
346
347 private:
348 const QuicTime receipt_time_;
349 int ttl_;
350 // Points to the start of packet headers.
351 char* packet_headers_;
352 // Length of packet headers.
353 int headers_length_;
354 // Whether owns the buffer for packet headers.
355 bool owns_header_buffer_;
356};
357
358struct QUIC_EXPORT_PRIVATE SerializedPacket {
359 SerializedPacket(QuicPacketNumber packet_number,
360 QuicPacketNumberLength packet_number_length,
361 const char* encrypted_buffer,
362 QuicPacketLength encrypted_length,
363 bool has_ack,
364 bool has_stop_waiting);
365 SerializedPacket(const SerializedPacket& other);
366 SerializedPacket& operator=(const SerializedPacket& other);
367 SerializedPacket(SerializedPacket&& other);
368 ~SerializedPacket();
369
370 // Not owned.
371 const char* encrypted_buffer;
372 QuicPacketLength encrypted_length;
373 QuicFrames retransmittable_frames;
fayang51152fd2019-10-21 06:48:09 -0700374 QuicFrames nonretransmittable_frames;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500375 IsHandshake has_crypto_handshake;
376 // -1: full padding to the end of a max-sized packet
377 // 0: no padding
378 // otherwise: only pad up to num_padding_bytes bytes
379 int16_t num_padding_bytes;
380 QuicPacketNumber packet_number;
381 QuicPacketNumberLength packet_number_length;
382 EncryptionLevel encryption_level;
fayang02a28742019-12-04 07:09:38 -0800383 // TODO(fayang): Remove has_ack and has_stop_waiting.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500384 bool has_ack;
385 bool has_stop_waiting;
386 TransmissionType transmission_type;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500387 // The largest acked of the AckFrame in this packet if has_ack is true,
388 // 0 otherwise.
389 QuicPacketNumber largest_acked;
fayangfb5fb612019-10-21 13:19:14 -0700390 // Indicates whether this packet has a copy of ack frame in
391 // nonretransmittable_frames.
392 bool has_ack_frame_copy;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500393};
394
fayangfb5fb612019-10-21 13:19:14 -0700395// Make a copy of |serialized| (including the underlying frames). |copy_buffer|
396// indicates whether the encrypted buffer should be copied.
397QUIC_EXPORT_PRIVATE SerializedPacket* CopySerializedPacket(
398 const SerializedPacket& serialized,
399 QuicBufferAllocator* allocator,
400 bool copy_buffer);
401
QUICHE teama6ef0a62019-03-07 20:34:33 -0500402// Deletes and clears all the frames and the packet from serialized packet.
403QUIC_EXPORT_PRIVATE void ClearSerializedPacket(
404 SerializedPacket* serialized_packet);
405
406// Allocates a new char[] of size |packet.encrypted_length| and copies in
407// |packet.encrypted_buffer|.
408QUIC_EXPORT_PRIVATE char* CopyBuffer(const SerializedPacket& packet);
fayang58f71072019-11-05 08:47:02 -0800409// Allocates a new char[] of size |encrypted_length| and copies in
410// |encrypted_buffer|.
411QUIC_EXPORT_PRIVATE char* CopyBuffer(const char* encrypted_buffer,
412 QuicPacketLength encrypted_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500413
414struct QUIC_EXPORT_PRIVATE SerializedPacketDeleter {
415 void operator()(SerializedPacket* packet) {
416 if (packet->encrypted_buffer != nullptr) {
417 delete[] packet->encrypted_buffer;
418 }
419 delete packet;
420 }
421};
422
423// On destruction, OwningSerializedPacketPointer deletes a packet's (on-heap)
424// encrypted_buffer before deleting the (also on-heap) packet itself.
425// TODO(wub): Maybe delete retransmittable_frames too?
426typedef std::unique_ptr<SerializedPacket, SerializedPacketDeleter>
427 OwningSerializedPacketPointer;
428
429// Context for an incoming packet.
430struct QUIC_EXPORT_PRIVATE QuicPerPacketContext {
431 virtual ~QuicPerPacketContext() {}
432};
433
fayang1ed1f762019-06-24 11:40:04 -0700434// ReceivedPacketInfo comprises information obtained by parsing the unencrypted
435// bytes of a received packet.
436struct QUIC_EXPORT_PRIVATE ReceivedPacketInfo {
437 ReceivedPacketInfo(const QuicSocketAddress& self_address,
438 const QuicSocketAddress& peer_address,
439 const QuicReceivedPacket& packet);
440 ReceivedPacketInfo(const ReceivedPacketInfo& other) = default;
441
442 ~ReceivedPacketInfo();
443
444 std::string ToString() const;
445
446 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
447 std::ostream& os,
448 const ReceivedPacketInfo& packet_info);
449
450 const QuicSocketAddress& self_address;
451 const QuicSocketAddress& peer_address;
452 const QuicReceivedPacket& packet;
453
fayang1ed1f762019-06-24 11:40:04 -0700454 PacketHeaderFormat form;
fayange3f2f7b2019-09-19 17:01:57 -0700455 // This is only used if the form is IETF_QUIC_LONG_HEADER_PACKET.
456 QuicLongHeaderType long_packet_type;
fayang1ed1f762019-06-24 11:40:04 -0700457 bool version_flag;
dschinazi48ac9192019-07-31 00:07:26 -0700458 bool use_length_prefix;
fayang1ed1f762019-06-24 11:40:04 -0700459 QuicVersionLabel version_label;
460 ParsedQuicVersion version;
461 QuicConnectionId destination_connection_id;
462 QuicConnectionId source_connection_id;
463};
464
QUICHE teama6ef0a62019-03-07 20:34:33 -0500465} // namespace quic
466
467#endif // QUICHE_QUIC_CORE_QUIC_PACKETS_H_