blob: 4123c27f94c0ab52edac1e330563dda95d188504 [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
bnc463f2352019-10-10 04:49:34 -07007#include <utility>
8
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
10#include "net/third_party/quiche/src/quic/core/quic_types.h"
11#include "net/third_party/quiche/src/quic/core/quic_utils.h"
12#include "net/third_party/quiche/src/quic/core/quic_versions.h"
rchf0a9f372019-05-15 21:36:43 -070013#include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050014#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
fayang1ed1f762019-06-24 11:40:04 -070015#include "net/third_party/quiche/src/quic/platform/api/quic_string_utils.h"
dmcardlecf0bfcf2019-12-13 08:08:21 -080016#include "net/third_party/quiche/src/common/platform/api/quiche_str_cat.h"
17#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
18#include "net/third_party/quiche/src/common/platform/api/quiche_text_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050019
20namespace quic {
21
QUICHE team2252b702019-05-14 23:55:14 -040022QuicConnectionId GetServerConnectionIdAsRecipient(
23 const QuicPacketHeader& header,
24 Perspective perspective) {
dschinazi5e1a7b22019-07-31 12:23:21 -070025 if (perspective == Perspective::IS_SERVER) {
QUICHE team2252b702019-05-14 23:55:14 -040026 return header.destination_connection_id;
27 }
28 return header.source_connection_id;
29}
30
dschinaziac6805d2019-05-30 09:44:27 -070031QuicConnectionId GetClientConnectionIdAsRecipient(
32 const QuicPacketHeader& header,
33 Perspective perspective) {
dschinaziac6805d2019-05-30 09:44:27 -070034 if (perspective == Perspective::IS_CLIENT) {
35 return header.destination_connection_id;
36 }
37 return header.source_connection_id;
38}
39
QUICHE team2252b702019-05-14 23:55:14 -040040QuicConnectionId GetServerConnectionIdAsSender(const QuicPacketHeader& header,
41 Perspective perspective) {
dschinazi5e1a7b22019-07-31 12:23:21 -070042 if (perspective == Perspective::IS_CLIENT) {
QUICHE team2252b702019-05-14 23:55:14 -040043 return header.destination_connection_id;
44 }
QUICHE team2252b702019-05-14 23:55:14 -040045 return header.source_connection_id;
46}
47
48QuicConnectionIdIncluded GetServerConnectionIdIncludedAsSender(
49 const QuicPacketHeader& header,
50 Perspective perspective) {
dschinazi5e1a7b22019-07-31 12:23:21 -070051 if (perspective == Perspective::IS_CLIENT) {
QUICHE team2252b702019-05-14 23:55:14 -040052 return header.destination_connection_id_included;
53 }
QUICHE team2252b702019-05-14 23:55:14 -040054 return header.source_connection_id_included;
55}
56
dschinaziac6805d2019-05-30 09:44:27 -070057QuicConnectionId GetClientConnectionIdAsSender(const QuicPacketHeader& header,
58 Perspective perspective) {
dschinazi5e1a7b22019-07-31 12:23:21 -070059 if (perspective == Perspective::IS_CLIENT) {
dschinaziac6805d2019-05-30 09:44:27 -070060 return header.source_connection_id;
61 }
dschinaziac6805d2019-05-30 09:44:27 -070062 return header.destination_connection_id;
63}
64
QUICHE team2252b702019-05-14 23:55:14 -040065QuicConnectionIdIncluded GetClientConnectionIdIncludedAsSender(
66 const QuicPacketHeader& header,
67 Perspective perspective) {
dschinazi5e1a7b22019-07-31 12:23:21 -070068 if (perspective == Perspective::IS_CLIENT) {
QUICHE team2252b702019-05-14 23:55:14 -040069 return header.source_connection_id_included;
70 }
71 return header.destination_connection_id_included;
72}
73
QUICHE teama6ef0a62019-03-07 20:34:33 -050074QuicConnectionIdLength GetIncludedConnectionIdLength(
75 QuicConnectionId connection_id,
76 QuicConnectionIdIncluded connection_id_included) {
77 DCHECK(connection_id_included == CONNECTION_ID_PRESENT ||
78 connection_id_included == CONNECTION_ID_ABSENT);
79 return connection_id_included == CONNECTION_ID_PRESENT
80 ? static_cast<QuicConnectionIdLength>(connection_id.length())
81 : PACKET_0BYTE_CONNECTION_ID;
82}
83
84QuicConnectionIdLength GetIncludedDestinationConnectionIdLength(
85 const QuicPacketHeader& header) {
86 return GetIncludedConnectionIdLength(
87 header.destination_connection_id,
88 header.destination_connection_id_included);
89}
90
91QuicConnectionIdLength GetIncludedSourceConnectionIdLength(
92 const QuicPacketHeader& header) {
93 return GetIncludedConnectionIdLength(header.source_connection_id,
94 header.source_connection_id_included);
95}
96
97size_t GetPacketHeaderSize(QuicTransportVersion version,
98 const QuicPacketHeader& header) {
99 return GetPacketHeaderSize(
100 version, GetIncludedDestinationConnectionIdLength(header),
101 GetIncludedSourceConnectionIdLength(header), header.version_flag,
102 header.nonce != nullptr, header.packet_number_length,
103 header.retry_token_length_length, header.retry_token.length(),
104 header.length_length);
105}
106
107size_t GetPacketHeaderSize(
108 QuicTransportVersion version,
109 QuicConnectionIdLength destination_connection_id_length,
110 QuicConnectionIdLength source_connection_id_length,
111 bool include_version,
112 bool include_diversification_nonce,
113 QuicPacketNumberLength packet_number_length,
114 QuicVariableLengthIntegerLength retry_token_length_length,
115 QuicByteCount retry_token_length,
116 QuicVariableLengthIntegerLength length_length) {
fayangd4291e42019-05-30 10:31:21 -0700117 if (VersionHasIetfInvariantHeader(version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500118 if (include_version) {
119 // Long header.
nharperd43f1d62019-07-01 15:18:20 -0700120 size_t size = kPacketHeaderTypeSize + kConnectionIdLengthSize +
121 destination_connection_id_length +
fayang36825da2019-08-21 14:01:27 -0700122 source_connection_id_length + packet_number_length +
nharperd43f1d62019-07-01 15:18:20 -0700123 kQuicVersionSize;
124 if (include_diversification_nonce) {
125 size += kDiversificationNonceSize;
126 }
dschinazi48ac9192019-07-31 00:07:26 -0700127 if (VersionHasLengthPrefixedConnectionIds(version)) {
128 size += kConnectionIdLengthSize;
129 }
nharperd43f1d62019-07-01 15:18:20 -0700130 DCHECK(QuicVersionHasLongHeaderLengths(version) ||
131 retry_token_length_length + retry_token_length + length_length ==
132 0);
nharper405f7192019-09-11 08:28:06 -0700133 if (QuicVersionHasLongHeaderLengths(version)) {
nharperd43f1d62019-07-01 15:18:20 -0700134 size += retry_token_length_length + retry_token_length + length_length;
135 }
136 return size;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500137 }
138 // Short header.
139 return kPacketHeaderTypeSize + destination_connection_id_length +
140 packet_number_length;
141 }
QUICHE team2252b702019-05-14 23:55:14 -0400142 // Google QUIC versions <= 43 can only carry one connection ID.
143 DCHECK(destination_connection_id_length == 0 ||
144 source_connection_id_length == 0);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500145 return kPublicFlagsSize + destination_connection_id_length +
QUICHE team2252b702019-05-14 23:55:14 -0400146 source_connection_id_length +
QUICHE teama6ef0a62019-03-07 20:34:33 -0500147 (include_version ? kQuicVersionSize : 0) + packet_number_length +
148 (include_diversification_nonce ? kDiversificationNonceSize : 0);
149}
150
151size_t GetStartOfEncryptedData(QuicTransportVersion version,
152 const QuicPacketHeader& header) {
153 return GetPacketHeaderSize(version, header);
154}
155
156size_t GetStartOfEncryptedData(
157 QuicTransportVersion version,
158 QuicConnectionIdLength destination_connection_id_length,
159 QuicConnectionIdLength source_connection_id_length,
160 bool include_version,
161 bool include_diversification_nonce,
162 QuicPacketNumberLength packet_number_length,
163 QuicVariableLengthIntegerLength retry_token_length_length,
164 QuicByteCount retry_token_length,
165 QuicVariableLengthIntegerLength length_length) {
166 // Encryption starts before private flags.
167 return GetPacketHeaderSize(
168 version, destination_connection_id_length, source_connection_id_length,
169 include_version, include_diversification_nonce, packet_number_length,
170 retry_token_length_length, retry_token_length, length_length);
171}
172
173QuicPacketHeader::QuicPacketHeader()
174 : destination_connection_id(EmptyQuicConnectionId()),
175 destination_connection_id_included(CONNECTION_ID_PRESENT),
176 source_connection_id(EmptyQuicConnectionId()),
177 source_connection_id_included(CONNECTION_ID_ABSENT),
178 reset_flag(false),
179 version_flag(false),
180 has_possible_stateless_reset_token(false),
181 packet_number_length(PACKET_4BYTE_PACKET_NUMBER),
182 version(UnsupportedQuicVersion()),
183 nonce(nullptr),
184 form(GOOGLE_QUIC_PACKET),
185 long_packet_type(INITIAL),
186 possible_stateless_reset_token(0),
187 retry_token_length_length(VARIABLE_LENGTH_INTEGER_LENGTH_0),
dmcardlecf0bfcf2019-12-13 08:08:21 -0800188 retry_token(quiche::QuicheStringPiece()),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500189 length_length(VARIABLE_LENGTH_INTEGER_LENGTH_0),
190 remaining_packet_length(0) {}
191
192QuicPacketHeader::QuicPacketHeader(const QuicPacketHeader& other) = default;
193
194QuicPacketHeader::~QuicPacketHeader() {}
195
danzhddf023a2019-12-20 13:33:36 -0800196QuicPacketHeader& QuicPacketHeader::operator=(const QuicPacketHeader& other) =
197 default;
198
QUICHE teama6ef0a62019-03-07 20:34:33 -0500199QuicPublicResetPacket::QuicPublicResetPacket()
200 : connection_id(EmptyQuicConnectionId()), nonce_proof(0) {}
201
202QuicPublicResetPacket::QuicPublicResetPacket(QuicConnectionId connection_id)
203 : connection_id(connection_id), nonce_proof(0) {}
204
205QuicVersionNegotiationPacket::QuicVersionNegotiationPacket()
206 : connection_id(EmptyQuicConnectionId()) {}
207
208QuicVersionNegotiationPacket::QuicVersionNegotiationPacket(
209 QuicConnectionId connection_id)
210 : connection_id(connection_id) {}
211
212QuicVersionNegotiationPacket::QuicVersionNegotiationPacket(
213 const QuicVersionNegotiationPacket& other) = default;
214
215QuicVersionNegotiationPacket::~QuicVersionNegotiationPacket() {}
216
217QuicIetfStatelessResetPacket::QuicIetfStatelessResetPacket()
218 : stateless_reset_token(0) {}
219
220QuicIetfStatelessResetPacket::QuicIetfStatelessResetPacket(
221 const QuicPacketHeader& header,
222 QuicUint128 token)
223 : header(header), stateless_reset_token(token) {}
224
225QuicIetfStatelessResetPacket::QuicIetfStatelessResetPacket(
226 const QuicIetfStatelessResetPacket& other) = default;
227
228QuicIetfStatelessResetPacket::~QuicIetfStatelessResetPacket() {}
229
230std::ostream& operator<<(std::ostream& os, const QuicPacketHeader& header) {
231 os << "{ destination_connection_id: " << header.destination_connection_id
232 << " ("
233 << (header.destination_connection_id_included == CONNECTION_ID_PRESENT
234 ? "present"
235 : "absent")
236 << "), source_connection_id: " << header.source_connection_id << " ("
237 << (header.source_connection_id_included == CONNECTION_ID_PRESENT
238 ? "present"
239 : "absent")
nharper4437e402020-01-06 16:47:08 -0800240 << "), packet_number_length: "
241 << static_cast<int>(header.packet_number_length)
QUICHE teama6ef0a62019-03-07 20:34:33 -0500242 << ", reset_flag: " << header.reset_flag
243 << ", version_flag: " << header.version_flag;
244 if (header.version_flag) {
245 os << ", version: " << ParsedQuicVersionToString(header.version);
246 if (header.long_packet_type != INVALID_PACKET_TYPE) {
247 os << ", long_packet_type: "
248 << QuicUtils::QuicLongHeaderTypetoString(header.long_packet_type);
249 }
250 if (header.retry_token_length_length != VARIABLE_LENGTH_INTEGER_LENGTH_0) {
251 os << ", retry_token_length_length: "
252 << static_cast<int>(header.retry_token_length_length);
253 }
254 if (header.retry_token.length() != 0) {
255 os << ", retry_token_length: " << header.retry_token.length();
256 }
257 if (header.length_length != VARIABLE_LENGTH_INTEGER_LENGTH_0) {
258 os << ", length_length: " << static_cast<int>(header.length_length);
259 }
260 if (header.remaining_packet_length != 0) {
261 os << ", remaining_packet_length: " << header.remaining_packet_length;
262 }
263 }
264 if (header.nonce != nullptr) {
265 os << ", diversification_nonce: "
dmcardlecf0bfcf2019-12-13 08:08:21 -0800266 << quiche::QuicheTextUtils::HexEncode(quiche::QuicheStringPiece(
267 header.nonce->data(), header.nonce->size()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500268 }
269 os << ", packet_number: " << header.packet_number << " }\n";
270 return os;
271}
272
273QuicData::QuicData(const char* buffer, size_t length)
274 : buffer_(buffer), length_(length), owns_buffer_(false) {}
275
276QuicData::QuicData(const char* buffer, size_t length, bool owns_buffer)
277 : buffer_(buffer), length_(length), owns_buffer_(owns_buffer) {}
278
dmcardlecf0bfcf2019-12-13 08:08:21 -0800279QuicData::QuicData(quiche::QuicheStringPiece packet_data)
dschinazi4b5a68a2019-08-15 15:45:36 -0700280 : buffer_(packet_data.data()),
281 length_(packet_data.length()),
282 owns_buffer_(false) {}
283
QUICHE teama6ef0a62019-03-07 20:34:33 -0500284QuicData::~QuicData() {
285 if (owns_buffer_) {
286 delete[] const_cast<char*>(buffer_);
287 }
288}
289
290QuicPacket::QuicPacket(
291 char* buffer,
292 size_t length,
293 bool owns_buffer,
294 QuicConnectionIdLength destination_connection_id_length,
295 QuicConnectionIdLength source_connection_id_length,
296 bool includes_version,
297 bool includes_diversification_nonce,
298 QuicPacketNumberLength packet_number_length,
299 QuicVariableLengthIntegerLength retry_token_length_length,
300 QuicByteCount retry_token_length,
301 QuicVariableLengthIntegerLength length_length)
302 : QuicData(buffer, length, owns_buffer),
303 buffer_(buffer),
304 destination_connection_id_length_(destination_connection_id_length),
305 source_connection_id_length_(source_connection_id_length),
306 includes_version_(includes_version),
307 includes_diversification_nonce_(includes_diversification_nonce),
308 packet_number_length_(packet_number_length),
309 retry_token_length_length_(retry_token_length_length),
310 retry_token_length_(retry_token_length),
311 length_length_(length_length) {}
312
dschinazi17d42422019-06-18 16:35:07 -0700313QuicPacket::QuicPacket(QuicTransportVersion /*version*/,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500314 char* buffer,
315 size_t length,
316 bool owns_buffer,
317 const QuicPacketHeader& header)
318 : QuicPacket(buffer,
319 length,
320 owns_buffer,
321 GetIncludedDestinationConnectionIdLength(header),
322 GetIncludedSourceConnectionIdLength(header),
323 header.version_flag,
324 header.nonce != nullptr,
325 header.packet_number_length,
326 header.retry_token_length_length,
327 header.retry_token.length(),
328 header.length_length) {}
329
330QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer, size_t length)
331 : QuicData(buffer, length) {}
332
333QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer,
334 size_t length,
335 bool owns_buffer)
336 : QuicData(buffer, length, owns_buffer) {}
337
dmcardlecf0bfcf2019-12-13 08:08:21 -0800338QuicEncryptedPacket::QuicEncryptedPacket(quiche::QuicheStringPiece data)
dschinazi4b5a68a2019-08-15 15:45:36 -0700339 : QuicData(data) {}
340
QUICHE teama6ef0a62019-03-07 20:34:33 -0500341std::unique_ptr<QuicEncryptedPacket> QuicEncryptedPacket::Clone() const {
342 char* buffer = new char[this->length()];
343 memcpy(buffer, this->data(), this->length());
vasilvv0fc587f2019-09-06 13:33:08 -0700344 return std::make_unique<QuicEncryptedPacket>(buffer, this->length(), true);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500345}
346
347std::ostream& operator<<(std::ostream& os, const QuicEncryptedPacket& s) {
348 os << s.length() << "-byte data";
349 return os;
350}
351
352QuicReceivedPacket::QuicReceivedPacket(const char* buffer,
353 size_t length,
354 QuicTime receipt_time)
355 : QuicReceivedPacket(buffer,
356 length,
357 receipt_time,
358 false /* owns_buffer */) {}
359
360QuicReceivedPacket::QuicReceivedPacket(const char* buffer,
361 size_t length,
362 QuicTime receipt_time,
363 bool owns_buffer)
364 : QuicReceivedPacket(buffer,
365 length,
366 receipt_time,
367 owns_buffer,
368 0 /* ttl */,
369 true /* ttl_valid */) {}
370
371QuicReceivedPacket::QuicReceivedPacket(const char* buffer,
372 size_t length,
373 QuicTime receipt_time,
374 bool owns_buffer,
375 int ttl,
376 bool ttl_valid)
377 : quic::QuicReceivedPacket(buffer,
378 length,
379 receipt_time,
380 owns_buffer,
381 ttl,
382 ttl_valid,
383 nullptr /* packet_headers */,
384 0 /* headers_length */,
385 false /* owns_header_buffer */) {}
386
387QuicReceivedPacket::QuicReceivedPacket(const char* buffer,
388 size_t length,
389 QuicTime receipt_time,
390 bool owns_buffer,
391 int ttl,
392 bool ttl_valid,
393 char* packet_headers,
394 size_t headers_length,
395 bool owns_header_buffer)
396 : QuicEncryptedPacket(buffer, length, owns_buffer),
397 receipt_time_(receipt_time),
398 ttl_(ttl_valid ? ttl : -1),
399 packet_headers_(packet_headers),
400 headers_length_(headers_length),
401 owns_header_buffer_(owns_header_buffer) {}
402
403QuicReceivedPacket::~QuicReceivedPacket() {
404 if (owns_header_buffer_) {
405 delete[] static_cast<char*>(packet_headers_);
406 }
407}
408
409std::unique_ptr<QuicReceivedPacket> QuicReceivedPacket::Clone() const {
410 char* buffer = new char[this->length()];
411 memcpy(buffer, this->data(), this->length());
412 if (this->packet_headers()) {
413 char* headers_buffer = new char[this->headers_length()];
414 memcpy(headers_buffer, this->packet_headers(), this->headers_length());
vasilvv0fc587f2019-09-06 13:33:08 -0700415 return std::make_unique<QuicReceivedPacket>(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500416 buffer, this->length(), receipt_time(), true, ttl(), ttl() >= 0,
417 headers_buffer, this->headers_length(), true);
418 }
419
vasilvv0fc587f2019-09-06 13:33:08 -0700420 return std::make_unique<QuicReceivedPacket>(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500421 buffer, this->length(), receipt_time(), true, ttl(), ttl() >= 0);
422}
423
424std::ostream& operator<<(std::ostream& os, const QuicReceivedPacket& s) {
425 os << s.length() << "-byte data";
426 return os;
427}
428
dmcardlecf0bfcf2019-12-13 08:08:21 -0800429quiche::QuicheStringPiece QuicPacket::AssociatedData(
430 QuicTransportVersion version) const {
431 return quiche::QuicheStringPiece(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500432 data(),
433 GetStartOfEncryptedData(version, destination_connection_id_length_,
434 source_connection_id_length_, includes_version_,
435 includes_diversification_nonce_,
436 packet_number_length_, retry_token_length_length_,
437 retry_token_length_, length_length_));
438}
439
dmcardlecf0bfcf2019-12-13 08:08:21 -0800440quiche::QuicheStringPiece QuicPacket::Plaintext(
441 QuicTransportVersion version) const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500442 const size_t start_of_encrypted_data = GetStartOfEncryptedData(
443 version, destination_connection_id_length_, source_connection_id_length_,
444 includes_version_, includes_diversification_nonce_, packet_number_length_,
445 retry_token_length_length_, retry_token_length_, length_length_);
dmcardlecf0bfcf2019-12-13 08:08:21 -0800446 return quiche::QuicheStringPiece(data() + start_of_encrypted_data,
447 length() - start_of_encrypted_data);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500448}
449
450SerializedPacket::SerializedPacket(QuicPacketNumber packet_number,
451 QuicPacketNumberLength packet_number_length,
452 const char* encrypted_buffer,
453 QuicPacketLength encrypted_length,
454 bool has_ack,
455 bool has_stop_waiting)
456 : encrypted_buffer(encrypted_buffer),
457 encrypted_length(encrypted_length),
458 has_crypto_handshake(NOT_HANDSHAKE),
459 num_padding_bytes(0),
460 packet_number(packet_number),
461 packet_number_length(packet_number_length),
QUICHE team6987b4a2019-03-15 16:23:04 -0700462 encryption_level(ENCRYPTION_INITIAL),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500463 has_ack(has_ack),
464 has_stop_waiting(has_stop_waiting),
fayangfb5fb612019-10-21 13:19:14 -0700465 transmission_type(NOT_RETRANSMISSION),
466 has_ack_frame_copy(false) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500467
468SerializedPacket::SerializedPacket(const SerializedPacket& other) = default;
469
470SerializedPacket& SerializedPacket::operator=(const SerializedPacket& other) =
471 default;
472
473SerializedPacket::SerializedPacket(SerializedPacket&& other)
474 : encrypted_buffer(other.encrypted_buffer),
475 encrypted_length(other.encrypted_length),
476 has_crypto_handshake(other.has_crypto_handshake),
477 num_padding_bytes(other.num_padding_bytes),
478 packet_number(other.packet_number),
479 packet_number_length(other.packet_number_length),
480 encryption_level(other.encryption_level),
481 has_ack(other.has_ack),
482 has_stop_waiting(other.has_stop_waiting),
483 transmission_type(other.transmission_type),
fayangfb5fb612019-10-21 13:19:14 -0700484 largest_acked(other.largest_acked),
485 has_ack_frame_copy(other.has_ack_frame_copy) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500486 retransmittable_frames.swap(other.retransmittable_frames);
fayang51152fd2019-10-21 06:48:09 -0700487 nonretransmittable_frames.swap(other.nonretransmittable_frames);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500488}
489
490SerializedPacket::~SerializedPacket() {}
491
fayangfb5fb612019-10-21 13:19:14 -0700492SerializedPacket* CopySerializedPacket(const SerializedPacket& serialized,
493 QuicBufferAllocator* allocator,
494 bool copy_buffer) {
495 SerializedPacket* copy = new SerializedPacket(serialized);
496 if (copy_buffer) {
497 copy->encrypted_buffer = CopyBuffer(serialized);
498 }
499 // Copy underlying frames.
500 copy->retransmittable_frames =
501 CopyQuicFrames(allocator, serialized.retransmittable_frames);
502 copy->nonretransmittable_frames.clear();
503 for (const auto& frame : serialized.nonretransmittable_frames) {
504 if (frame.type == ACK_FRAME) {
505 copy->has_ack_frame_copy = true;
506 }
507 copy->nonretransmittable_frames.push_back(CopyQuicFrame(allocator, frame));
508 }
509 return copy;
510}
511
QUICHE teama6ef0a62019-03-07 20:34:33 -0500512void ClearSerializedPacket(SerializedPacket* serialized_packet) {
513 if (!serialized_packet->retransmittable_frames.empty()) {
514 DeleteFrames(&serialized_packet->retransmittable_frames);
515 }
fayang51152fd2019-10-21 06:48:09 -0700516 for (auto& frame : serialized_packet->nonretransmittable_frames) {
fayangfb5fb612019-10-21 13:19:14 -0700517 if (!serialized_packet->has_ack_frame_copy && frame.type == ACK_FRAME) {
518 // Do not delete ack frame if the packet does not own a copy of it.
fayang51152fd2019-10-21 06:48:09 -0700519 continue;
520 }
521 DeleteFrame(&frame);
522 }
523 serialized_packet->nonretransmittable_frames.clear();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500524 serialized_packet->encrypted_buffer = nullptr;
525 serialized_packet->encrypted_length = 0;
526 serialized_packet->largest_acked.Clear();
527}
528
529char* CopyBuffer(const SerializedPacket& packet) {
530 char* dst_buffer = new char[packet.encrypted_length];
531 memcpy(dst_buffer, packet.encrypted_buffer, packet.encrypted_length);
532 return dst_buffer;
533}
534
fayang58f71072019-11-05 08:47:02 -0800535char* CopyBuffer(const char* encrypted_buffer,
536 QuicPacketLength encrypted_length) {
537 char* dst_buffer = new char[encrypted_length];
538 memcpy(dst_buffer, encrypted_buffer, encrypted_length);
539 return dst_buffer;
540}
541
fayang1ed1f762019-06-24 11:40:04 -0700542ReceivedPacketInfo::ReceivedPacketInfo(const QuicSocketAddress& self_address,
543 const QuicSocketAddress& peer_address,
544 const QuicReceivedPacket& packet)
545 : self_address(self_address),
546 peer_address(peer_address),
547 packet(packet),
548 form(GOOGLE_QUIC_PACKET),
fayange3f2f7b2019-09-19 17:01:57 -0700549 long_packet_type(INVALID_PACKET_TYPE),
fayang1ed1f762019-06-24 11:40:04 -0700550 version_flag(false),
dschinazi48ac9192019-07-31 00:07:26 -0700551 use_length_prefix(false),
fayang1ed1f762019-06-24 11:40:04 -0700552 version_label(0),
553 version(PROTOCOL_UNSUPPORTED, QUIC_VERSION_UNSUPPORTED),
554 destination_connection_id(EmptyQuicConnectionId()),
555 source_connection_id(EmptyQuicConnectionId()) {}
556
557ReceivedPacketInfo::~ReceivedPacketInfo() {}
558
559std::string ReceivedPacketInfo::ToString() const {
dmcardlecf0bfcf2019-12-13 08:08:21 -0800560 std::string output = quiche::QuicheStrCat(
561 "{ self_address: ", self_address.ToString(),
562 ", peer_address: ", peer_address.ToString(),
563 ", packet_length: ", packet.length(), ", header_format: ", form,
564 ", version_flag: ", version_flag);
fayang1ed1f762019-06-24 11:40:04 -0700565 if (version_flag) {
566 QuicStrAppend(&output, ", version: ", ParsedQuicVersionToString(version));
567 }
568 QuicStrAppend(
569 &output,
570 ", destination_connection_id: ", destination_connection_id.ToString(),
571 ", source_connection_id: ", source_connection_id.ToString(), " }\n");
572 return output;
573}
574
575std::ostream& operator<<(std::ostream& os,
576 const ReceivedPacketInfo& packet_info) {
577 os << packet_info.ToString();
578 return os;
579}
580
QUICHE teama6ef0a62019-03-07 20:34:33 -0500581} // namespace quic