blob: b1964e7b47d65ce2d0f535f84409515294f47a7d [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:
207 QuicData(const char* buffer, size_t length);
208 QuicData(const char* buffer, size_t length, bool owns_buffer);
209 QuicData(const QuicData&) = delete;
210 QuicData& operator=(const QuicData&) = delete;
211 virtual ~QuicData();
212
213 QuicStringPiece AsStringPiece() const {
214 return QuicStringPiece(data(), length());
215 }
216
217 const char* data() const { return buffer_; }
218 size_t length() const { return length_; }
219
220 private:
221 const char* buffer_;
222 size_t length_;
223 bool owns_buffer_;
224};
225
226class QUIC_EXPORT_PRIVATE QuicPacket : public QuicData {
227 public:
228 QuicPacket(char* buffer,
229 size_t length,
230 bool owns_buffer,
231 QuicConnectionIdLength destination_connection_id_length,
232 QuicConnectionIdLength source_connection_id_length,
233 bool includes_version,
234 bool includes_diversification_nonce,
235 QuicPacketNumberLength packet_number_length,
236 QuicVariableLengthIntegerLength retry_token_length_length,
237 QuicByteCount retry_token_length,
238 QuicVariableLengthIntegerLength length_length);
239 QuicPacket(QuicTransportVersion version,
240 char* buffer,
241 size_t length,
242 bool owns_buffer,
243 const QuicPacketHeader& header);
244 QuicPacket(const QuicPacket&) = delete;
245 QuicPacket& operator=(const QuicPacket&) = delete;
246
247 QuicStringPiece AssociatedData(QuicTransportVersion version) const;
248 QuicStringPiece Plaintext(QuicTransportVersion version) const;
249
250 char* mutable_data() { return buffer_; }
251
252 private:
253 char* buffer_;
254 const QuicConnectionIdLength destination_connection_id_length_;
255 const QuicConnectionIdLength source_connection_id_length_;
256 const bool includes_version_;
257 const bool includes_diversification_nonce_;
258 const QuicPacketNumberLength packet_number_length_;
259 const QuicVariableLengthIntegerLength retry_token_length_length_;
260 const QuicByteCount retry_token_length_;
261 const QuicVariableLengthIntegerLength length_length_;
262};
263
264class QUIC_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
265 public:
266 QuicEncryptedPacket(const char* buffer, size_t length);
267 QuicEncryptedPacket(const char* buffer, size_t length, bool owns_buffer);
268 QuicEncryptedPacket(const QuicEncryptedPacket&) = delete;
269 QuicEncryptedPacket& operator=(const QuicEncryptedPacket&) = delete;
270
271 // Clones the packet into a new packet which owns the buffer.
272 std::unique_ptr<QuicEncryptedPacket> Clone() const;
273
274 // By default, gtest prints the raw bytes of an object. The bool data
275 // member (in the base class QuicData) causes this object to have padding
276 // bytes, which causes the default gtest object printer to read
277 // uninitialize memory. So we need to teach gtest how to print this object.
278 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
279 std::ostream& os,
280 const QuicEncryptedPacket& s);
281};
282
283// A received encrypted QUIC packet, with a recorded time of receipt.
284class QUIC_EXPORT_PRIVATE QuicReceivedPacket : public QuicEncryptedPacket {
285 public:
286 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time);
287 QuicReceivedPacket(const char* buffer,
288 size_t length,
289 QuicTime receipt_time,
290 bool owns_buffer);
291 QuicReceivedPacket(const char* buffer,
292 size_t length,
293 QuicTime receipt_time,
294 bool owns_buffer,
295 int ttl,
296 bool ttl_valid);
297 QuicReceivedPacket(const char* buffer,
298 size_t length,
299 QuicTime receipt_time,
300 bool owns_buffer,
301 int ttl,
302 bool ttl_valid,
303 char* packet_headers,
304 size_t headers_length,
305 bool owns_header_buffer);
306 ~QuicReceivedPacket();
307 QuicReceivedPacket(const QuicReceivedPacket&) = delete;
308 QuicReceivedPacket& operator=(const QuicReceivedPacket&) = delete;
309
310 // Clones the packet into a new packet which owns the buffer.
311 std::unique_ptr<QuicReceivedPacket> Clone() const;
312
313 // Returns the time at which the packet was received.
314 QuicTime receipt_time() const { return receipt_time_; }
315
316 // This is the TTL of the packet, assuming ttl_vaild_ is true.
317 int ttl() const { return ttl_; }
318
319 // Start of packet headers.
320 char* packet_headers() const { return packet_headers_; }
321
322 // Length of packet headers.
323 int headers_length() const { return headers_length_; }
324
325 // By default, gtest prints the raw bytes of an object. The bool data
326 // member (in the base class QuicData) causes this object to have padding
327 // bytes, which causes the default gtest object printer to read
328 // uninitialize memory. So we need to teach gtest how to print this object.
329 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
330 std::ostream& os,
331 const QuicReceivedPacket& s);
332
333 private:
334 const QuicTime receipt_time_;
335 int ttl_;
336 // Points to the start of packet headers.
337 char* packet_headers_;
338 // Length of packet headers.
339 int headers_length_;
340 // Whether owns the buffer for packet headers.
341 bool owns_header_buffer_;
342};
343
344struct QUIC_EXPORT_PRIVATE SerializedPacket {
345 SerializedPacket(QuicPacketNumber packet_number,
346 QuicPacketNumberLength packet_number_length,
347 const char* encrypted_buffer,
348 QuicPacketLength encrypted_length,
349 bool has_ack,
350 bool has_stop_waiting);
351 SerializedPacket(const SerializedPacket& other);
352 SerializedPacket& operator=(const SerializedPacket& other);
353 SerializedPacket(SerializedPacket&& other);
354 ~SerializedPacket();
355
356 // Not owned.
357 const char* encrypted_buffer;
358 QuicPacketLength encrypted_length;
359 QuicFrames retransmittable_frames;
360 IsHandshake has_crypto_handshake;
361 // -1: full padding to the end of a max-sized packet
362 // 0: no padding
363 // otherwise: only pad up to num_padding_bytes bytes
364 int16_t num_padding_bytes;
365 QuicPacketNumber packet_number;
366 QuicPacketNumberLength packet_number_length;
367 EncryptionLevel encryption_level;
368 bool has_ack;
369 bool has_stop_waiting;
370 TransmissionType transmission_type;
371 QuicPacketNumber original_packet_number;
372 // The largest acked of the AckFrame in this packet if has_ack is true,
373 // 0 otherwise.
374 QuicPacketNumber largest_acked;
375};
376
377// Deletes and clears all the frames and the packet from serialized packet.
378QUIC_EXPORT_PRIVATE void ClearSerializedPacket(
379 SerializedPacket* serialized_packet);
380
381// Allocates a new char[] of size |packet.encrypted_length| and copies in
382// |packet.encrypted_buffer|.
383QUIC_EXPORT_PRIVATE char* CopyBuffer(const SerializedPacket& packet);
384
385struct QUIC_EXPORT_PRIVATE SerializedPacketDeleter {
386 void operator()(SerializedPacket* packet) {
387 if (packet->encrypted_buffer != nullptr) {
388 delete[] packet->encrypted_buffer;
389 }
390 delete packet;
391 }
392};
393
394// On destruction, OwningSerializedPacketPointer deletes a packet's (on-heap)
395// encrypted_buffer before deleting the (also on-heap) packet itself.
396// TODO(wub): Maybe delete retransmittable_frames too?
397typedef std::unique_ptr<SerializedPacket, SerializedPacketDeleter>
398 OwningSerializedPacketPointer;
399
400// Context for an incoming packet.
401struct QUIC_EXPORT_PRIVATE QuicPerPacketContext {
402 virtual ~QuicPerPacketContext() {}
403};
404
405} // namespace quic
406
407#endif // QUICHE_QUIC_CORE_QUIC_PACKETS_H_