blob: 3732af302140253ef4db02bea9538e413bb47926 [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#include "net/third_party/quiche/src/quic/core/quic_packets.h"
6
7#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
8#include "net/third_party/quiche/src/quic/core/quic_types.h"
9#include "net/third_party/quiche/src/quic/core/quic_utils.h"
10#include "net/third_party/quiche/src/quic/core/quic_versions.h"
11#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
12#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
13#include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h"
14#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
15#include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h"
16
17namespace quic {
18
QUICHE team2252b702019-05-14 23:55:14 -040019QuicConnectionId GetServerConnectionIdAsRecipient(
20 const QuicPacketHeader& header,
21 Perspective perspective) {
22 if (perspective == Perspective::IS_SERVER ||
23 !GetQuicRestartFlag(quic_do_not_override_connection_id)) {
24 return header.destination_connection_id;
25 }
26 return header.source_connection_id;
27}
28
29QuicConnectionId GetServerConnectionIdAsSender(const QuicPacketHeader& header,
30 Perspective perspective) {
31 if (perspective == Perspective::IS_CLIENT ||
32 !GetQuicRestartFlag(quic_do_not_override_connection_id)) {
33 return header.destination_connection_id;
34 }
35 QUIC_RESTART_FLAG_COUNT_N(quic_do_not_override_connection_id, 3, 5);
36 return header.source_connection_id;
37}
38
39QuicConnectionIdIncluded GetServerConnectionIdIncludedAsSender(
40 const QuicPacketHeader& header,
41 Perspective perspective) {
42 if (perspective == Perspective::IS_CLIENT ||
43 !GetQuicRestartFlag(quic_do_not_override_connection_id)) {
44 return header.destination_connection_id_included;
45 }
46 QUIC_RESTART_FLAG_COUNT_N(quic_do_not_override_connection_id, 4, 5);
47 return header.source_connection_id_included;
48}
49
50QuicConnectionIdIncluded GetClientConnectionIdIncludedAsSender(
51 const QuicPacketHeader& header,
52 Perspective perspective) {
53 if (perspective == Perspective::IS_CLIENT ||
54 !GetQuicRestartFlag(quic_do_not_override_connection_id)) {
55 return header.source_connection_id_included;
56 }
57 return header.destination_connection_id_included;
58}
59
QUICHE teama6ef0a62019-03-07 20:34:33 -050060QuicConnectionIdLength GetIncludedConnectionIdLength(
61 QuicConnectionId connection_id,
62 QuicConnectionIdIncluded connection_id_included) {
63 DCHECK(connection_id_included == CONNECTION_ID_PRESENT ||
64 connection_id_included == CONNECTION_ID_ABSENT);
65 return connection_id_included == CONNECTION_ID_PRESENT
66 ? static_cast<QuicConnectionIdLength>(connection_id.length())
67 : PACKET_0BYTE_CONNECTION_ID;
68}
69
70QuicConnectionIdLength GetIncludedDestinationConnectionIdLength(
71 const QuicPacketHeader& header) {
72 return GetIncludedConnectionIdLength(
73 header.destination_connection_id,
74 header.destination_connection_id_included);
75}
76
77QuicConnectionIdLength GetIncludedSourceConnectionIdLength(
78 const QuicPacketHeader& header) {
79 return GetIncludedConnectionIdLength(header.source_connection_id,
80 header.source_connection_id_included);
81}
82
83size_t GetPacketHeaderSize(QuicTransportVersion version,
84 const QuicPacketHeader& header) {
85 return GetPacketHeaderSize(
86 version, GetIncludedDestinationConnectionIdLength(header),
87 GetIncludedSourceConnectionIdLength(header), header.version_flag,
88 header.nonce != nullptr, header.packet_number_length,
89 header.retry_token_length_length, header.retry_token.length(),
90 header.length_length);
91}
92
93size_t GetPacketHeaderSize(
94 QuicTransportVersion version,
95 QuicConnectionIdLength destination_connection_id_length,
96 QuicConnectionIdLength source_connection_id_length,
97 bool include_version,
98 bool include_diversification_nonce,
99 QuicPacketNumberLength packet_number_length,
100 QuicVariableLengthIntegerLength retry_token_length_length,
101 QuicByteCount retry_token_length,
102 QuicVariableLengthIntegerLength length_length) {
103 if (version > QUIC_VERSION_43) {
104 if (include_version) {
105 // Long header.
106 return kPacketHeaderTypeSize + kConnectionIdLengthSize +
107 destination_connection_id_length + source_connection_id_length +
108 (version > QUIC_VERSION_44 ? packet_number_length
109 : PACKET_4BYTE_PACKET_NUMBER) +
110 kQuicVersionSize +
111 (include_diversification_nonce ? kDiversificationNonceSize : 0) +
112 retry_token_length_length + retry_token_length + length_length;
113 }
114 // Short header.
115 return kPacketHeaderTypeSize + destination_connection_id_length +
116 packet_number_length;
117 }
QUICHE team2252b702019-05-14 23:55:14 -0400118 // Google QUIC versions <= 43 can only carry one connection ID.
119 DCHECK(destination_connection_id_length == 0 ||
120 source_connection_id_length == 0);
121 DCHECK(source_connection_id_length == 0 ||
122 GetQuicRestartFlag(quic_do_not_override_connection_id));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500123 return kPublicFlagsSize + destination_connection_id_length +
QUICHE team2252b702019-05-14 23:55:14 -0400124 source_connection_id_length +
QUICHE teama6ef0a62019-03-07 20:34:33 -0500125 (include_version ? kQuicVersionSize : 0) + packet_number_length +
126 (include_diversification_nonce ? kDiversificationNonceSize : 0);
127}
128
129size_t GetStartOfEncryptedData(QuicTransportVersion version,
130 const QuicPacketHeader& header) {
131 return GetPacketHeaderSize(version, header);
132}
133
134size_t GetStartOfEncryptedData(
135 QuicTransportVersion version,
136 QuicConnectionIdLength destination_connection_id_length,
137 QuicConnectionIdLength source_connection_id_length,
138 bool include_version,
139 bool include_diversification_nonce,
140 QuicPacketNumberLength packet_number_length,
141 QuicVariableLengthIntegerLength retry_token_length_length,
142 QuicByteCount retry_token_length,
143 QuicVariableLengthIntegerLength length_length) {
144 // Encryption starts before private flags.
145 return GetPacketHeaderSize(
146 version, destination_connection_id_length, source_connection_id_length,
147 include_version, include_diversification_nonce, packet_number_length,
148 retry_token_length_length, retry_token_length, length_length);
149}
150
151QuicPacketHeader::QuicPacketHeader()
152 : destination_connection_id(EmptyQuicConnectionId()),
153 destination_connection_id_included(CONNECTION_ID_PRESENT),
154 source_connection_id(EmptyQuicConnectionId()),
155 source_connection_id_included(CONNECTION_ID_ABSENT),
156 reset_flag(false),
157 version_flag(false),
158 has_possible_stateless_reset_token(false),
159 packet_number_length(PACKET_4BYTE_PACKET_NUMBER),
160 version(UnsupportedQuicVersion()),
161 nonce(nullptr),
162 form(GOOGLE_QUIC_PACKET),
163 long_packet_type(INITIAL),
164 possible_stateless_reset_token(0),
165 retry_token_length_length(VARIABLE_LENGTH_INTEGER_LENGTH_0),
166 retry_token(QuicStringPiece()),
167 length_length(VARIABLE_LENGTH_INTEGER_LENGTH_0),
168 remaining_packet_length(0) {}
169
170QuicPacketHeader::QuicPacketHeader(const QuicPacketHeader& other) = default;
171
172QuicPacketHeader::~QuicPacketHeader() {}
173
174QuicPublicResetPacket::QuicPublicResetPacket()
175 : connection_id(EmptyQuicConnectionId()), nonce_proof(0) {}
176
177QuicPublicResetPacket::QuicPublicResetPacket(QuicConnectionId connection_id)
178 : connection_id(connection_id), nonce_proof(0) {}
179
180QuicVersionNegotiationPacket::QuicVersionNegotiationPacket()
181 : connection_id(EmptyQuicConnectionId()) {}
182
183QuicVersionNegotiationPacket::QuicVersionNegotiationPacket(
184 QuicConnectionId connection_id)
185 : connection_id(connection_id) {}
186
187QuicVersionNegotiationPacket::QuicVersionNegotiationPacket(
188 const QuicVersionNegotiationPacket& other) = default;
189
190QuicVersionNegotiationPacket::~QuicVersionNegotiationPacket() {}
191
192QuicIetfStatelessResetPacket::QuicIetfStatelessResetPacket()
193 : stateless_reset_token(0) {}
194
195QuicIetfStatelessResetPacket::QuicIetfStatelessResetPacket(
196 const QuicPacketHeader& header,
197 QuicUint128 token)
198 : header(header), stateless_reset_token(token) {}
199
200QuicIetfStatelessResetPacket::QuicIetfStatelessResetPacket(
201 const QuicIetfStatelessResetPacket& other) = default;
202
203QuicIetfStatelessResetPacket::~QuicIetfStatelessResetPacket() {}
204
205std::ostream& operator<<(std::ostream& os, const QuicPacketHeader& header) {
206 os << "{ destination_connection_id: " << header.destination_connection_id
207 << " ("
208 << (header.destination_connection_id_included == CONNECTION_ID_PRESENT
209 ? "present"
210 : "absent")
211 << "), source_connection_id: " << header.source_connection_id << " ("
212 << (header.source_connection_id_included == CONNECTION_ID_PRESENT
213 ? "present"
214 : "absent")
215 << "), packet_number_length: " << header.packet_number_length
216 << ", reset_flag: " << header.reset_flag
217 << ", version_flag: " << header.version_flag;
218 if (header.version_flag) {
219 os << ", version: " << ParsedQuicVersionToString(header.version);
220 if (header.long_packet_type != INVALID_PACKET_TYPE) {
221 os << ", long_packet_type: "
222 << QuicUtils::QuicLongHeaderTypetoString(header.long_packet_type);
223 }
224 if (header.retry_token_length_length != VARIABLE_LENGTH_INTEGER_LENGTH_0) {
225 os << ", retry_token_length_length: "
226 << static_cast<int>(header.retry_token_length_length);
227 }
228 if (header.retry_token.length() != 0) {
229 os << ", retry_token_length: " << header.retry_token.length();
230 }
231 if (header.length_length != VARIABLE_LENGTH_INTEGER_LENGTH_0) {
232 os << ", length_length: " << static_cast<int>(header.length_length);
233 }
234 if (header.remaining_packet_length != 0) {
235 os << ", remaining_packet_length: " << header.remaining_packet_length;
236 }
237 }
238 if (header.nonce != nullptr) {
239 os << ", diversification_nonce: "
240 << QuicTextUtils::HexEncode(
241 QuicStringPiece(header.nonce->data(), header.nonce->size()));
242 }
243 os << ", packet_number: " << header.packet_number << " }\n";
244 return os;
245}
246
247QuicData::QuicData(const char* buffer, size_t length)
248 : buffer_(buffer), length_(length), owns_buffer_(false) {}
249
250QuicData::QuicData(const char* buffer, size_t length, bool owns_buffer)
251 : buffer_(buffer), length_(length), owns_buffer_(owns_buffer) {}
252
253QuicData::~QuicData() {
254 if (owns_buffer_) {
255 delete[] const_cast<char*>(buffer_);
256 }
257}
258
259QuicPacket::QuicPacket(
260 char* buffer,
261 size_t length,
262 bool owns_buffer,
263 QuicConnectionIdLength destination_connection_id_length,
264 QuicConnectionIdLength source_connection_id_length,
265 bool includes_version,
266 bool includes_diversification_nonce,
267 QuicPacketNumberLength packet_number_length,
268 QuicVariableLengthIntegerLength retry_token_length_length,
269 QuicByteCount retry_token_length,
270 QuicVariableLengthIntegerLength length_length)
271 : QuicData(buffer, length, owns_buffer),
272 buffer_(buffer),
273 destination_connection_id_length_(destination_connection_id_length),
274 source_connection_id_length_(source_connection_id_length),
275 includes_version_(includes_version),
276 includes_diversification_nonce_(includes_diversification_nonce),
277 packet_number_length_(packet_number_length),
278 retry_token_length_length_(retry_token_length_length),
279 retry_token_length_(retry_token_length),
280 length_length_(length_length) {}
281
282QuicPacket::QuicPacket(QuicTransportVersion version,
283 char* buffer,
284 size_t length,
285 bool owns_buffer,
286 const QuicPacketHeader& header)
287 : QuicPacket(buffer,
288 length,
289 owns_buffer,
290 GetIncludedDestinationConnectionIdLength(header),
291 GetIncludedSourceConnectionIdLength(header),
292 header.version_flag,
293 header.nonce != nullptr,
294 header.packet_number_length,
295 header.retry_token_length_length,
296 header.retry_token.length(),
297 header.length_length) {}
298
299QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer, size_t length)
300 : QuicData(buffer, length) {}
301
302QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer,
303 size_t length,
304 bool owns_buffer)
305 : QuicData(buffer, length, owns_buffer) {}
306
307std::unique_ptr<QuicEncryptedPacket> QuicEncryptedPacket::Clone() const {
308 char* buffer = new char[this->length()];
309 memcpy(buffer, this->data(), this->length());
310 return QuicMakeUnique<QuicEncryptedPacket>(buffer, this->length(), true);
311}
312
313std::ostream& operator<<(std::ostream& os, const QuicEncryptedPacket& s) {
314 os << s.length() << "-byte data";
315 return os;
316}
317
318QuicReceivedPacket::QuicReceivedPacket(const char* buffer,
319 size_t length,
320 QuicTime receipt_time)
321 : QuicReceivedPacket(buffer,
322 length,
323 receipt_time,
324 false /* owns_buffer */) {}
325
326QuicReceivedPacket::QuicReceivedPacket(const char* buffer,
327 size_t length,
328 QuicTime receipt_time,
329 bool owns_buffer)
330 : QuicReceivedPacket(buffer,
331 length,
332 receipt_time,
333 owns_buffer,
334 0 /* ttl */,
335 true /* ttl_valid */) {}
336
337QuicReceivedPacket::QuicReceivedPacket(const char* buffer,
338 size_t length,
339 QuicTime receipt_time,
340 bool owns_buffer,
341 int ttl,
342 bool ttl_valid)
343 : quic::QuicReceivedPacket(buffer,
344 length,
345 receipt_time,
346 owns_buffer,
347 ttl,
348 ttl_valid,
349 nullptr /* packet_headers */,
350 0 /* headers_length */,
351 false /* owns_header_buffer */) {}
352
353QuicReceivedPacket::QuicReceivedPacket(const char* buffer,
354 size_t length,
355 QuicTime receipt_time,
356 bool owns_buffer,
357 int ttl,
358 bool ttl_valid,
359 char* packet_headers,
360 size_t headers_length,
361 bool owns_header_buffer)
362 : QuicEncryptedPacket(buffer, length, owns_buffer),
363 receipt_time_(receipt_time),
364 ttl_(ttl_valid ? ttl : -1),
365 packet_headers_(packet_headers),
366 headers_length_(headers_length),
367 owns_header_buffer_(owns_header_buffer) {}
368
369QuicReceivedPacket::~QuicReceivedPacket() {
370 if (owns_header_buffer_) {
371 delete[] static_cast<char*>(packet_headers_);
372 }
373}
374
375std::unique_ptr<QuicReceivedPacket> QuicReceivedPacket::Clone() const {
376 char* buffer = new char[this->length()];
377 memcpy(buffer, this->data(), this->length());
378 if (this->packet_headers()) {
379 char* headers_buffer = new char[this->headers_length()];
380 memcpy(headers_buffer, this->packet_headers(), this->headers_length());
381 return QuicMakeUnique<QuicReceivedPacket>(
382 buffer, this->length(), receipt_time(), true, ttl(), ttl() >= 0,
383 headers_buffer, this->headers_length(), true);
384 }
385
386 return QuicMakeUnique<QuicReceivedPacket>(
387 buffer, this->length(), receipt_time(), true, ttl(), ttl() >= 0);
388}
389
390std::ostream& operator<<(std::ostream& os, const QuicReceivedPacket& s) {
391 os << s.length() << "-byte data";
392 return os;
393}
394
395QuicStringPiece QuicPacket::AssociatedData(QuicTransportVersion version) const {
396 return QuicStringPiece(
397 data(),
398 GetStartOfEncryptedData(version, destination_connection_id_length_,
399 source_connection_id_length_, includes_version_,
400 includes_diversification_nonce_,
401 packet_number_length_, retry_token_length_length_,
402 retry_token_length_, length_length_));
403}
404
405QuicStringPiece QuicPacket::Plaintext(QuicTransportVersion version) const {
406 const size_t start_of_encrypted_data = GetStartOfEncryptedData(
407 version, destination_connection_id_length_, source_connection_id_length_,
408 includes_version_, includes_diversification_nonce_, packet_number_length_,
409 retry_token_length_length_, retry_token_length_, length_length_);
410 return QuicStringPiece(data() + start_of_encrypted_data,
411 length() - start_of_encrypted_data);
412}
413
414SerializedPacket::SerializedPacket(QuicPacketNumber packet_number,
415 QuicPacketNumberLength packet_number_length,
416 const char* encrypted_buffer,
417 QuicPacketLength encrypted_length,
418 bool has_ack,
419 bool has_stop_waiting)
420 : encrypted_buffer(encrypted_buffer),
421 encrypted_length(encrypted_length),
422 has_crypto_handshake(NOT_HANDSHAKE),
423 num_padding_bytes(0),
424 packet_number(packet_number),
425 packet_number_length(packet_number_length),
QUICHE team6987b4a2019-03-15 16:23:04 -0700426 encryption_level(ENCRYPTION_INITIAL),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500427 has_ack(has_ack),
428 has_stop_waiting(has_stop_waiting),
429 transmission_type(NOT_RETRANSMISSION) {}
430
431SerializedPacket::SerializedPacket(const SerializedPacket& other) = default;
432
433SerializedPacket& SerializedPacket::operator=(const SerializedPacket& other) =
434 default;
435
436SerializedPacket::SerializedPacket(SerializedPacket&& other)
437 : encrypted_buffer(other.encrypted_buffer),
438 encrypted_length(other.encrypted_length),
439 has_crypto_handshake(other.has_crypto_handshake),
440 num_padding_bytes(other.num_padding_bytes),
441 packet_number(other.packet_number),
442 packet_number_length(other.packet_number_length),
443 encryption_level(other.encryption_level),
444 has_ack(other.has_ack),
445 has_stop_waiting(other.has_stop_waiting),
446 transmission_type(other.transmission_type),
447 original_packet_number(other.original_packet_number),
448 largest_acked(other.largest_acked) {
449 retransmittable_frames.swap(other.retransmittable_frames);
450}
451
452SerializedPacket::~SerializedPacket() {}
453
454void ClearSerializedPacket(SerializedPacket* serialized_packet) {
455 if (!serialized_packet->retransmittable_frames.empty()) {
456 DeleteFrames(&serialized_packet->retransmittable_frames);
457 }
458 serialized_packet->encrypted_buffer = nullptr;
459 serialized_packet->encrypted_length = 0;
460 serialized_packet->largest_acked.Clear();
461}
462
463char* CopyBuffer(const SerializedPacket& packet) {
464 char* dst_buffer = new char[packet.encrypted_length];
465 memcpy(dst_buffer, packet.encrypted_buffer, packet.encrypted_length);
466 return dst_buffer;
467}
468
469} // namespace quic