blob: d2aaf34c66ed5d3ccc6a14d3a4589fd4eb78206d [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
17#include "base/macros.h"
18#include "net/third_party/quiche/src/quic/core/frames/quic_frame.h"
19#include "net/third_party/quiche/src/quic/core/quic_ack_listener_interface.h"
20#include "net/third_party/quiche/src/quic/core/quic_bandwidth.h"
21#include "net/third_party/quiche/src/quic/core/quic_constants.h"
22#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
23#include "net/third_party/quiche/src/quic/core/quic_time.h"
24#include "net/third_party/quiche/src/quic/core/quic_types.h"
25#include "net/third_party/quiche/src/quic/core/quic_versions.h"
26#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
27#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
28#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
29#include "net/third_party/quiche/src/quic/platform/api/quic_uint128.h"
30
31namespace quic {
32
33class QuicPacket;
34struct QuicPacketHeader;
35
36// Number of connection ID bytes that are actually included over the wire.
37QUIC_EXPORT_PRIVATE QuicConnectionIdLength
38GetIncludedConnectionIdLength(QuicConnectionId connection_id,
39 QuicConnectionIdIncluded connection_id_included);
40
41// Number of destination connection ID bytes that are actually included over the
42// wire for this particular header.
43QUIC_EXPORT_PRIVATE QuicConnectionIdLength
44GetIncludedDestinationConnectionIdLength(const QuicPacketHeader& header);
45
46// Number of source connection ID bytes that are actually included over the
47// wire for this particular header.
48QUIC_EXPORT_PRIVATE QuicConnectionIdLength
49GetIncludedSourceConnectionIdLength(const QuicPacketHeader& header);
50
51// Size in bytes of the data packet header.
52QUIC_EXPORT_PRIVATE size_t GetPacketHeaderSize(QuicTransportVersion version,
53 const QuicPacketHeader& header);
54
55QUIC_EXPORT_PRIVATE size_t
56GetPacketHeaderSize(QuicTransportVersion version,
57 QuicConnectionIdLength destination_connection_id_length,
58 QuicConnectionIdLength source_connection_id_length,
59 bool include_version,
60 bool include_diversification_nonce,
61 QuicPacketNumberLength packet_number_length,
62 QuicVariableLengthIntegerLength retry_token_length_length,
63 QuicByteCount retry_token_length,
64 QuicVariableLengthIntegerLength length_length);
65
66// Index of the first byte in a QUIC packet of encrypted data.
67QUIC_EXPORT_PRIVATE size_t
68GetStartOfEncryptedData(QuicTransportVersion version,
69 const QuicPacketHeader& header);
70
71QUIC_EXPORT_PRIVATE size_t GetStartOfEncryptedData(
72 QuicTransportVersion version,
73 QuicConnectionIdLength destination_connection_id_length,
74 QuicConnectionIdLength source_connection_id_length,
75 bool include_version,
76 bool include_diversification_nonce,
77 QuicPacketNumberLength packet_number_length,
78 QuicVariableLengthIntegerLength retry_token_length_length,
79 QuicByteCount retry_token_length,
80 QuicVariableLengthIntegerLength length_length);
81
82struct QUIC_EXPORT_PRIVATE QuicPacketHeader {
83 QuicPacketHeader();
84 QuicPacketHeader(const QuicPacketHeader& other);
85 ~QuicPacketHeader();
86
87 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
88 std::ostream& os,
89 const QuicPacketHeader& header);
90
91 // Universal header. All QuicPacket headers will have a connection_id and
92 // public flags.
93 QuicConnectionId destination_connection_id;
94 QuicConnectionIdIncluded destination_connection_id_included;
95 QuicConnectionId source_connection_id;
96 QuicConnectionIdIncluded source_connection_id_included;
97 // This is only used for Google QUIC.
98 bool reset_flag;
99 // For Google QUIC, version flag in packets from the server means version
100 // negotiation packet. For IETF QUIC, version flag means long header.
101 bool version_flag;
102 // Indicates whether |possible_stateless_reset_token| contains a valid value
103 // parsed from the packet buffer. IETF QUIC only, always false for GQUIC.
104 bool has_possible_stateless_reset_token;
105 QuicPacketNumberLength packet_number_length;
106 ParsedQuicVersion version;
107 // nonce contains an optional, 32-byte nonce value. If not included in the
108 // packet, |nonce| will be empty.
109 DiversificationNonce* nonce;
110 QuicPacketNumber packet_number;
111 // Format of this header.
112 PacketHeaderFormat form;
113 // Short packet type is reflected in packet_number_length.
114 QuicLongHeaderType long_packet_type;
115 // Only valid if |has_possible_stateless_reset_token| is true.
116 // Stores last 16 bytes of a this packet, used to check whether this packet is
117 // a stateless reset packet on decryption failure.
118 QuicUint128 possible_stateless_reset_token;
119 // Length of the retry token length variable length integer field,
120 // carried only by v99 IETF Initial packets.
121 QuicVariableLengthIntegerLength retry_token_length_length;
122 // Retry token, carried only by v99 IETF Initial packets.
123 QuicStringPiece retry_token;
124 // Length of the length variable length integer field,
125 // carried only by v99 IETF Initial, 0-RTT and Handshake packets.
126 QuicVariableLengthIntegerLength length_length;
127 // Length of the packet number and payload, carried only by v99 IETF Initial,
128 // 0-RTT and Handshake packets. Also includes the length of the
129 // diversification nonce in server to client 0-RTT packets.
130 QuicByteCount remaining_packet_length;
131};
132
133struct QUIC_EXPORT_PRIVATE QuicPublicResetPacket {
134 QuicPublicResetPacket();
135 explicit QuicPublicResetPacket(QuicConnectionId connection_id);
136
137 QuicConnectionId connection_id;
138 QuicPublicResetNonceProof nonce_proof;
139 QuicSocketAddress client_address;
140 // An arbitrary string to identify an endpoint. Used by clients to
141 // differentiate traffic from Google servers vs Non-google servers.
142 // Will not be used if empty().
143 QuicString endpoint_id;
144};
145
146struct QUIC_EXPORT_PRIVATE QuicVersionNegotiationPacket {
147 QuicVersionNegotiationPacket();
148 explicit QuicVersionNegotiationPacket(QuicConnectionId connection_id);
149 QuicVersionNegotiationPacket(const QuicVersionNegotiationPacket& other);
150 ~QuicVersionNegotiationPacket();
151
152 QuicConnectionId connection_id;
153 ParsedQuicVersionVector versions;
154};
155
156struct QUIC_EXPORT_PRIVATE QuicIetfStatelessResetPacket {
157 QuicIetfStatelessResetPacket();
158 QuicIetfStatelessResetPacket(const QuicPacketHeader& header,
159 QuicUint128 token);
160 QuicIetfStatelessResetPacket(const QuicIetfStatelessResetPacket& other);
161 ~QuicIetfStatelessResetPacket();
162
163 QuicPacketHeader header;
164 QuicUint128 stateless_reset_token;
165};
166
167class QUIC_EXPORT_PRIVATE QuicData {
168 public:
169 QuicData(const char* buffer, size_t length);
170 QuicData(const char* buffer, size_t length, bool owns_buffer);
171 QuicData(const QuicData&) = delete;
172 QuicData& operator=(const QuicData&) = delete;
173 virtual ~QuicData();
174
175 QuicStringPiece AsStringPiece() const {
176 return QuicStringPiece(data(), length());
177 }
178
179 const char* data() const { return buffer_; }
180 size_t length() const { return length_; }
181
182 private:
183 const char* buffer_;
184 size_t length_;
185 bool owns_buffer_;
186};
187
188class QUIC_EXPORT_PRIVATE QuicPacket : public QuicData {
189 public:
190 QuicPacket(char* buffer,
191 size_t length,
192 bool owns_buffer,
193 QuicConnectionIdLength destination_connection_id_length,
194 QuicConnectionIdLength source_connection_id_length,
195 bool includes_version,
196 bool includes_diversification_nonce,
197 QuicPacketNumberLength packet_number_length,
198 QuicVariableLengthIntegerLength retry_token_length_length,
199 QuicByteCount retry_token_length,
200 QuicVariableLengthIntegerLength length_length);
201 QuicPacket(QuicTransportVersion version,
202 char* buffer,
203 size_t length,
204 bool owns_buffer,
205 const QuicPacketHeader& header);
206 QuicPacket(const QuicPacket&) = delete;
207 QuicPacket& operator=(const QuicPacket&) = delete;
208
209 QuicStringPiece AssociatedData(QuicTransportVersion version) const;
210 QuicStringPiece Plaintext(QuicTransportVersion version) const;
211
212 char* mutable_data() { return buffer_; }
213
214 private:
215 char* buffer_;
216 const QuicConnectionIdLength destination_connection_id_length_;
217 const QuicConnectionIdLength source_connection_id_length_;
218 const bool includes_version_;
219 const bool includes_diversification_nonce_;
220 const QuicPacketNumberLength packet_number_length_;
221 const QuicVariableLengthIntegerLength retry_token_length_length_;
222 const QuicByteCount retry_token_length_;
223 const QuicVariableLengthIntegerLength length_length_;
224};
225
226class QUIC_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
227 public:
228 QuicEncryptedPacket(const char* buffer, size_t length);
229 QuicEncryptedPacket(const char* buffer, size_t length, bool owns_buffer);
230 QuicEncryptedPacket(const QuicEncryptedPacket&) = delete;
231 QuicEncryptedPacket& operator=(const QuicEncryptedPacket&) = delete;
232
233 // Clones the packet into a new packet which owns the buffer.
234 std::unique_ptr<QuicEncryptedPacket> Clone() const;
235
236 // By default, gtest prints the raw bytes of an object. The bool data
237 // member (in the base class QuicData) causes this object to have padding
238 // bytes, which causes the default gtest object printer to read
239 // uninitialize memory. So we need to teach gtest how to print this object.
240 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<(
241 std::ostream& os,
242 const QuicEncryptedPacket& s);
243};
244
245// A received encrypted QUIC packet, with a recorded time of receipt.
246class QUIC_EXPORT_PRIVATE QuicReceivedPacket : public QuicEncryptedPacket {
247 public:
248 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time);
249 QuicReceivedPacket(const char* buffer,
250 size_t length,
251 QuicTime receipt_time,
252 bool owns_buffer);
253 QuicReceivedPacket(const char* buffer,
254 size_t length,
255 QuicTime receipt_time,
256 bool owns_buffer,
257 int ttl,
258 bool ttl_valid);
259 QuicReceivedPacket(const char* buffer,
260 size_t length,
261 QuicTime receipt_time,
262 bool owns_buffer,
263 int ttl,
264 bool ttl_valid,
265 char* packet_headers,
266 size_t headers_length,
267 bool owns_header_buffer);
268 ~QuicReceivedPacket();
269 QuicReceivedPacket(const QuicReceivedPacket&) = delete;
270 QuicReceivedPacket& operator=(const QuicReceivedPacket&) = delete;
271
272 // Clones the packet into a new packet which owns the buffer.
273 std::unique_ptr<QuicReceivedPacket> Clone() const;
274
275 // Returns the time at which the packet was received.
276 QuicTime receipt_time() const { return receipt_time_; }
277
278 // This is the TTL of the packet, assuming ttl_vaild_ is true.
279 int ttl() const { return ttl_; }
280
281 // Start of packet headers.
282 char* packet_headers() const { return packet_headers_; }
283
284 // Length of packet headers.
285 int headers_length() const { return headers_length_; }
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 QuicReceivedPacket& s);
294
295 private:
296 const QuicTime receipt_time_;
297 int ttl_;
298 // Points to the start of packet headers.
299 char* packet_headers_;
300 // Length of packet headers.
301 int headers_length_;
302 // Whether owns the buffer for packet headers.
303 bool owns_header_buffer_;
304};
305
306struct QUIC_EXPORT_PRIVATE SerializedPacket {
307 SerializedPacket(QuicPacketNumber packet_number,
308 QuicPacketNumberLength packet_number_length,
309 const char* encrypted_buffer,
310 QuicPacketLength encrypted_length,
311 bool has_ack,
312 bool has_stop_waiting);
313 SerializedPacket(const SerializedPacket& other);
314 SerializedPacket& operator=(const SerializedPacket& other);
315 SerializedPacket(SerializedPacket&& other);
316 ~SerializedPacket();
317
318 // Not owned.
319 const char* encrypted_buffer;
320 QuicPacketLength encrypted_length;
321 QuicFrames retransmittable_frames;
322 IsHandshake has_crypto_handshake;
323 // -1: full padding to the end of a max-sized packet
324 // 0: no padding
325 // otherwise: only pad up to num_padding_bytes bytes
326 int16_t num_padding_bytes;
327 QuicPacketNumber packet_number;
328 QuicPacketNumberLength packet_number_length;
329 EncryptionLevel encryption_level;
330 bool has_ack;
331 bool has_stop_waiting;
332 TransmissionType transmission_type;
333 QuicPacketNumber original_packet_number;
334 // The largest acked of the AckFrame in this packet if has_ack is true,
335 // 0 otherwise.
336 QuicPacketNumber largest_acked;
337};
338
339// Deletes and clears all the frames and the packet from serialized packet.
340QUIC_EXPORT_PRIVATE void ClearSerializedPacket(
341 SerializedPacket* serialized_packet);
342
343// Allocates a new char[] of size |packet.encrypted_length| and copies in
344// |packet.encrypted_buffer|.
345QUIC_EXPORT_PRIVATE char* CopyBuffer(const SerializedPacket& packet);
346
347struct QUIC_EXPORT_PRIVATE SerializedPacketDeleter {
348 void operator()(SerializedPacket* packet) {
349 if (packet->encrypted_buffer != nullptr) {
350 delete[] packet->encrypted_buffer;
351 }
352 delete packet;
353 }
354};
355
356// On destruction, OwningSerializedPacketPointer deletes a packet's (on-heap)
357// encrypted_buffer before deleting the (also on-heap) packet itself.
358// TODO(wub): Maybe delete retransmittable_frames too?
359typedef std::unique_ptr<SerializedPacket, SerializedPacketDeleter>
360 OwningSerializedPacketPointer;
361
362// Context for an incoming packet.
363struct QUIC_EXPORT_PRIVATE QuicPerPacketContext {
364 virtual ~QuicPerPacketContext() {}
365};
366
367} // namespace quic
368
369#endif // QUICHE_QUIC_CORE_QUIC_PACKETS_H_