1 // 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.
5 #include "net/quic/quic_protocol.h"
7 #include "base/stl_util.h"
8 #include "net/quic/quic_utils.h"
10 using base::StringPiece
;
12 using std::numeric_limits
;
18 size_t GetPacketHeaderSize(const QuicPacketHeader
& header
) {
19 return GetPacketHeaderSize(header
.public_header
.connection_id_length
,
20 header
.public_header
.version_flag
,
21 header
.public_header
.sequence_number_length
,
22 header
.is_in_fec_group
);
25 size_t GetPacketHeaderSize(QuicConnectionIdLength connection_id_length
,
27 QuicSequenceNumberLength sequence_number_length
,
28 InFecGroup is_in_fec_group
) {
29 return kPublicFlagsSize
+ connection_id_length
+
30 (include_version
? kQuicVersionSize
: 0) + sequence_number_length
+
31 kPrivateFlagsSize
+ (is_in_fec_group
== IN_FEC_GROUP
? kFecGroupSize
: 0);
34 size_t GetStartOfFecProtectedData(
35 QuicConnectionIdLength connection_id_length
,
37 QuicSequenceNumberLength sequence_number_length
) {
38 return GetPacketHeaderSize(connection_id_length
,
40 sequence_number_length
,
44 size_t GetStartOfEncryptedData(
45 QuicConnectionIdLength connection_id_length
,
47 QuicSequenceNumberLength sequence_number_length
) {
48 // Don't include the fec size, since encryption starts before private flags.
49 return GetPacketHeaderSize(connection_id_length
,
51 sequence_number_length
,
52 NOT_IN_FEC_GROUP
) - kPrivateFlagsSize
;
55 QuicPacketPublicHeader::QuicPacketPublicHeader()
57 connection_id_length(PACKET_8BYTE_CONNECTION_ID
),
60 sequence_number_length(PACKET_6BYTE_SEQUENCE_NUMBER
) {
63 QuicPacketPublicHeader::QuicPacketPublicHeader(
64 const QuicPacketPublicHeader
& other
)
65 : connection_id(other
.connection_id
),
66 connection_id_length(other
.connection_id_length
),
67 reset_flag(other
.reset_flag
),
68 version_flag(other
.version_flag
),
69 sequence_number_length(other
.sequence_number_length
),
70 versions(other
.versions
) {
73 QuicPacketPublicHeader::~QuicPacketPublicHeader() {}
75 QuicPacketHeader::QuicPacketHeader()
76 : packet_sequence_number(0),
80 is_in_fec_group(NOT_IN_FEC_GROUP
),
84 QuicPacketHeader::QuicPacketHeader(const QuicPacketPublicHeader
& header
)
85 : public_header(header
),
86 packet_sequence_number(0),
90 is_in_fec_group(NOT_IN_FEC_GROUP
),
94 QuicPublicResetPacket::QuicPublicResetPacket()
96 rejected_sequence_number(0) {}
98 QuicPublicResetPacket::QuicPublicResetPacket(
99 const QuicPacketPublicHeader
& header
)
100 : public_header(header
),
102 rejected_sequence_number(0) {}
104 QuicStreamFrame::QuicStreamFrame() : stream_id(0), fin(false), offset(0) {
107 QuicStreamFrame::QuicStreamFrame(const QuicStreamFrame
& frame
)
108 : stream_id(frame
.stream_id
),
110 offset(frame
.offset
),
114 QuicStreamFrame::QuicStreamFrame(QuicStreamId stream_id
,
116 QuicStreamOffset offset
,
118 : stream_id(stream_id
), fin(fin
), offset(offset
), data(data
) {
121 uint32
MakeQuicTag(char a
, char b
, char c
, char d
) {
122 return static_cast<uint32
>(a
) |
123 static_cast<uint32
>(b
) << 8 |
124 static_cast<uint32
>(c
) << 16 |
125 static_cast<uint32
>(d
) << 24;
128 bool ContainsQuicTag(const QuicTagVector
& tag_vector
, QuicTag tag
) {
129 return std::find(tag_vector
.begin(), tag_vector
.end(), tag
)
133 QuicVersionVector
QuicSupportedVersions() {
134 QuicVersionVector supported_versions
;
135 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
136 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
138 return supported_versions
;
141 QuicTag
QuicVersionToQuicTag(const QuicVersion version
) {
143 case QUIC_VERSION_24
:
144 return MakeQuicTag('Q', '0', '2', '4');
145 case QUIC_VERSION_25
:
146 return MakeQuicTag('Q', '0', '2', '5');
148 // This shold be an ERROR because we should never attempt to convert an
149 // invalid QuicVersion to be written to the wire.
150 LOG(ERROR
) << "Unsupported QuicVersion: " << version
;
155 QuicVersion
QuicTagToQuicVersion(const QuicTag version_tag
) {
156 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
157 if (version_tag
== QuicVersionToQuicTag(kSupportedQuicVersions
[i
])) {
158 return kSupportedQuicVersions
[i
];
161 // Reading from the client so this should not be considered an ERROR.
162 DVLOG(1) << "Unsupported QuicTag version: "
163 << QuicUtils::TagToString(version_tag
);
164 return QUIC_VERSION_UNSUPPORTED
;
167 #define RETURN_STRING_LITERAL(x) \
171 string
QuicVersionToString(const QuicVersion version
) {
173 RETURN_STRING_LITERAL(QUIC_VERSION_24
);
174 RETURN_STRING_LITERAL(QUIC_VERSION_25
);
176 return "QUIC_VERSION_UNSUPPORTED";
180 string
QuicVersionVectorToString(const QuicVersionVector
& versions
) {
182 for (size_t i
= 0; i
< versions
.size(); ++i
) {
186 result
.append(QuicVersionToString(versions
[i
]));
191 ostream
& operator<<(ostream
& os
, const Perspective
& s
) {
192 if (s
== Perspective::IS_SERVER
) {
200 ostream
& operator<<(ostream
& os
, const QuicPacketHeader
& header
) {
201 os
<< "{ connection_id: " << header
.public_header
.connection_id
202 << ", connection_id_length:" << header
.public_header
.connection_id_length
203 << ", sequence_number_length:"
204 << header
.public_header
.sequence_number_length
205 << ", reset_flag: " << header
.public_header
.reset_flag
206 << ", version_flag: " << header
.public_header
.version_flag
;
207 if (header
.public_header
.version_flag
) {
209 for (size_t i
= 0; i
< header
.public_header
.versions
.size(); ++i
) {
210 os
<< header
.public_header
.versions
[i
] << " ";
213 os
<< ", fec_flag: " << header
.fec_flag
214 << ", entropy_flag: " << header
.entropy_flag
215 << ", entropy hash: " << static_cast<int>(header
.entropy_hash
)
216 << ", sequence_number: " << header
.packet_sequence_number
217 << ", is_in_fec_group:" << header
.is_in_fec_group
218 << ", fec_group: " << header
.fec_group
<< "}\n";
222 bool IsAwaitingPacket(const QuicAckFrame
& ack_frame
,
223 QuicPacketSequenceNumber sequence_number
) {
224 return sequence_number
> ack_frame
.largest_observed
||
225 ContainsKey(ack_frame
.missing_packets
, sequence_number
);
228 void InsertMissingPacketsBetween(QuicAckFrame
* ack_frame
,
229 QuicPacketSequenceNumber lower
,
230 QuicPacketSequenceNumber higher
) {
231 for (QuicPacketSequenceNumber i
= lower
; i
< higher
; ++i
) {
232 ack_frame
->missing_packets
.insert(i
);
236 QuicStopWaitingFrame::QuicStopWaitingFrame()
241 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
243 QuicAckFrame::QuicAckFrame()
246 delta_time_largest_observed(QuicTime::Delta::Infinite()),
247 is_truncated(false) {}
249 QuicAckFrame::~QuicAckFrame() {}
251 QuicRstStreamErrorCode
AdjustErrorForVersion(
252 QuicRstStreamErrorCode error_code
,
253 QuicVersion version
) {
257 QuicRstStreamFrame::QuicRstStreamFrame()
258 : stream_id(0), error_code(QUIC_STREAM_NO_ERROR
), byte_offset(0) {}
260 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id
,
261 QuicRstStreamErrorCode error_code
,
262 QuicStreamOffset bytes_written
)
263 : stream_id(stream_id
),
264 error_code(error_code
),
265 byte_offset(bytes_written
) {
266 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
269 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
270 : error_code(QUIC_NO_ERROR
) {
273 QuicFrame::QuicFrame() {}
275 QuicFrame::QuicFrame(QuicPaddingFrame
* padding_frame
)
276 : type(PADDING_FRAME
),
277 padding_frame(padding_frame
) {
280 QuicFrame::QuicFrame(QuicStreamFrame
* stream_frame
)
281 : type(STREAM_FRAME
),
282 stream_frame(stream_frame
) {
285 QuicFrame::QuicFrame(QuicAckFrame
* frame
)
290 QuicFrame::QuicFrame(QuicMtuDiscoveryFrame
* frame
)
291 : type(MTU_DISCOVERY_FRAME
), mtu_discovery_frame(frame
) {
294 QuicFrame::QuicFrame(QuicStopWaitingFrame
* frame
)
295 : type(STOP_WAITING_FRAME
),
296 stop_waiting_frame(frame
) {
299 QuicFrame::QuicFrame(QuicPingFrame
* frame
)
304 QuicFrame::QuicFrame(QuicRstStreamFrame
* frame
)
305 : type(RST_STREAM_FRAME
),
306 rst_stream_frame(frame
) {
309 QuicFrame::QuicFrame(QuicConnectionCloseFrame
* frame
)
310 : type(CONNECTION_CLOSE_FRAME
),
311 connection_close_frame(frame
) {
314 QuicFrame::QuicFrame(QuicGoAwayFrame
* frame
)
315 : type(GOAWAY_FRAME
),
316 goaway_frame(frame
) {
319 QuicFrame::QuicFrame(QuicWindowUpdateFrame
* frame
)
320 : type(WINDOW_UPDATE_FRAME
),
321 window_update_frame(frame
) {
324 QuicFrame::QuicFrame(QuicBlockedFrame
* frame
)
325 : type(BLOCKED_FRAME
),
326 blocked_frame(frame
) {
329 QuicFecData::QuicFecData() : fec_group(0) {}
331 ostream
& operator<<(ostream
& os
, const QuicStopWaitingFrame
& sent_info
) {
332 os
<< "entropy_hash: " << static_cast<int>(sent_info
.entropy_hash
)
333 << " least_unacked: " << sent_info
.least_unacked
;
337 ostream
& operator<<(ostream
& os
, const QuicAckFrame
& ack_frame
) {
338 os
<< "entropy_hash: " << static_cast<int>(ack_frame
.entropy_hash
)
339 << " largest_observed: " << ack_frame
.largest_observed
340 << " delta_time_largest_observed: "
341 << ack_frame
.delta_time_largest_observed
.ToMicroseconds()
342 << " missing_packets: [ ";
343 for (SequenceNumberSet::const_iterator it
= ack_frame
.missing_packets
.begin();
344 it
!= ack_frame
.missing_packets
.end(); ++it
) {
347 os
<< " ] is_truncated: " << ack_frame
.is_truncated
;
348 os
<< " revived_packets: [ ";
349 for (SequenceNumberSet::const_iterator it
= ack_frame
.revived_packets
.begin();
350 it
!= ack_frame
.revived_packets
.end(); ++it
) {
353 os
<< " ] received_packets: [ ";
354 for (PacketTimeList::const_iterator it
=
355 ack_frame
.received_packet_times
.begin();
356 it
!= ack_frame
.received_packet_times
.end(); ++it
) {
357 os
<< it
->first
<< " at " << it
->second
.ToDebuggingValue() << " ";
363 ostream
& operator<<(ostream
& os
, const QuicFrame
& frame
) {
364 switch (frame
.type
) {
365 case PADDING_FRAME
: {
366 os
<< "type { PADDING_FRAME } ";
369 case RST_STREAM_FRAME
: {
370 os
<< "type { RST_STREAM_FRAME } " << *(frame
.rst_stream_frame
);
373 case CONNECTION_CLOSE_FRAME
: {
374 os
<< "type { CONNECTION_CLOSE_FRAME } "
375 << *(frame
.connection_close_frame
);
379 os
<< "type { GOAWAY_FRAME } " << *(frame
.goaway_frame
);
382 case WINDOW_UPDATE_FRAME
: {
383 os
<< "type { WINDOW_UPDATE_FRAME } " << *(frame
.window_update_frame
);
386 case BLOCKED_FRAME
: {
387 os
<< "type { BLOCKED_FRAME } " << *(frame
.blocked_frame
);
391 os
<< "type { STREAM_FRAME } " << *(frame
.stream_frame
);
395 os
<< "type { ACK_FRAME } " << *(frame
.ack_frame
);
398 case STOP_WAITING_FRAME
: {
399 os
<< "type { STOP_WAITING_FRAME } " << *(frame
.stop_waiting_frame
);
403 os
<< "type { PING_FRAME } ";
406 case MTU_DISCOVERY_FRAME
: {
407 os
<< "type { MTU_DISCOVERY_FRAME } ";
411 LOG(ERROR
) << "Unknown frame type: " << frame
.type
;
418 ostream
& operator<<(ostream
& os
, const QuicRstStreamFrame
& rst_frame
) {
419 os
<< "stream_id { " << rst_frame
.stream_id
<< " } "
420 << "error_code { " << rst_frame
.error_code
<< " } "
421 << "error_details { " << rst_frame
.error_details
<< " }\n";
425 ostream
& operator<<(ostream
& os
,
426 const QuicConnectionCloseFrame
& connection_close_frame
) {
427 os
<< "error_code { " << connection_close_frame
.error_code
<< " } "
428 << "error_details { " << connection_close_frame
.error_details
<< " }\n";
432 ostream
& operator<<(ostream
& os
, const QuicGoAwayFrame
& goaway_frame
) {
433 os
<< "error_code { " << goaway_frame
.error_code
<< " } "
434 << "last_good_stream_id { " << goaway_frame
.last_good_stream_id
<< " } "
435 << "reason_phrase { " << goaway_frame
.reason_phrase
<< " }\n";
439 ostream
& operator<<(ostream
& os
,
440 const QuicWindowUpdateFrame
& window_update_frame
) {
441 os
<< "stream_id { " << window_update_frame
.stream_id
<< " } "
442 << "byte_offset { " << window_update_frame
.byte_offset
<< " }\n";
446 ostream
& operator<<(ostream
& os
, const QuicBlockedFrame
& blocked_frame
) {
447 os
<< "stream_id { " << blocked_frame
.stream_id
<< " }\n";
451 ostream
& operator<<(ostream
& os
, const QuicStreamFrame
& stream_frame
) {
452 os
<< "stream_id { " << stream_frame
.stream_id
<< " } "
453 << "fin { " << stream_frame
.fin
<< " } "
454 << "offset { " << stream_frame
.offset
<< " } "
455 << "data { " << QuicUtils::StringToHexASCIIDump(stream_frame
.data
)
460 QuicGoAwayFrame::QuicGoAwayFrame()
461 : error_code(QUIC_NO_ERROR
),
462 last_good_stream_id(0) {
465 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code
,
466 QuicStreamId last_good_stream_id
,
467 const string
& reason
)
468 : error_code(error_code
),
469 last_good_stream_id(last_good_stream_id
),
470 reason_phrase(reason
) {
471 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
474 QuicData::QuicData(const char* buffer
,
478 owns_buffer_(false) {
481 QuicData::QuicData(char* buffer
,
486 owns_buffer_(owns_buffer
) {
489 QuicData::~QuicData() {
491 delete [] const_cast<char*>(buffer_
);
495 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id
,
496 QuicStreamOffset byte_offset
)
497 : stream_id(stream_id
),
498 byte_offset(byte_offset
) {}
500 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id
)
501 : stream_id(stream_id
) {}
503 QuicPacket::QuicPacket(char* buffer
,
506 QuicConnectionIdLength connection_id_length
,
507 bool includes_version
,
508 QuicSequenceNumberLength sequence_number_length
)
509 : QuicData(buffer
, length
, owns_buffer
),
511 connection_id_length_(connection_id_length
),
512 includes_version_(includes_version
),
513 sequence_number_length_(sequence_number_length
) {
516 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer
,
518 : QuicData(buffer
, length
) {
521 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer
,
524 : QuicData(buffer
, length
, owns_buffer
) {
527 StringPiece
QuicPacket::FecProtectedData() const {
528 const size_t start_of_fec
= GetStartOfFecProtectedData(
529 connection_id_length_
, includes_version_
, sequence_number_length_
);
530 return StringPiece(data() + start_of_fec
, length() - start_of_fec
);
533 StringPiece
QuicPacket::AssociatedData() const {
535 data() + kStartOfHashData
,
536 GetStartOfEncryptedData(
537 connection_id_length_
, includes_version_
, sequence_number_length_
) -
541 StringPiece
QuicPacket::BeforePlaintext() const {
542 return StringPiece(data(), GetStartOfEncryptedData(connection_id_length_
,
544 sequence_number_length_
));
547 StringPiece
QuicPacket::Plaintext() const {
548 const size_t start_of_encrypted_data
=
549 GetStartOfEncryptedData(
550 connection_id_length_
, includes_version_
, sequence_number_length_
);
551 return StringPiece(data() + start_of_encrypted_data
,
552 length() - start_of_encrypted_data
);
555 RetransmittableFrames::RetransmittableFrames(EncryptionLevel level
)
556 : encryption_level_(level
),
557 has_crypto_handshake_(NOT_HANDSHAKE
),
558 needs_padding_(false) {
561 RetransmittableFrames::~RetransmittableFrames() {
562 for (QuicFrames::iterator it
= frames_
.begin(); it
!= frames_
.end(); ++it
) {
565 delete it
->padding_frame
;
568 delete it
->stream_frame
;
571 delete it
->ack_frame
;
573 case MTU_DISCOVERY_FRAME
:
574 delete it
->mtu_discovery_frame
;
576 case STOP_WAITING_FRAME
:
577 delete it
->stop_waiting_frame
;
580 delete it
->ping_frame
;
582 case RST_STREAM_FRAME
:
583 delete it
->rst_stream_frame
;
585 case CONNECTION_CLOSE_FRAME
:
586 delete it
->connection_close_frame
;
589 delete it
->goaway_frame
;
591 case WINDOW_UPDATE_FRAME
:
592 delete it
->window_update_frame
;
595 delete it
->blocked_frame
;
597 case NUM_FRAME_TYPES
:
598 DCHECK(false) << "Cannot delete type: " << it
->type
;
601 for (const char* buffer
: stream_data_
) {
606 const QuicFrame
& RetransmittableFrames::AddFrame(const QuicFrame
& frame
) {
607 return AddFrame(frame
, nullptr);
610 const QuicFrame
& RetransmittableFrames::AddFrame(const QuicFrame
& frame
,
612 if (frame
.type
== STREAM_FRAME
&&
613 frame
.stream_frame
->stream_id
== kCryptoStreamId
) {
614 has_crypto_handshake_
= IS_HANDSHAKE
;
616 if (buffer
!= nullptr) {
617 stream_data_
.push_back(buffer
);
619 frames_
.push_back(frame
);
620 return frames_
.back();
623 void RetransmittableFrames::RemoveFramesForStream(QuicStreamId stream_id
) {
624 QuicFrames::iterator it
= frames_
.begin();
625 while (it
!= frames_
.end()) {
626 if (it
->type
!= STREAM_FRAME
|| it
->stream_frame
->stream_id
!= stream_id
) {
630 delete it
->stream_frame
;
631 it
= frames_
.erase(it
);
635 SerializedPacket::SerializedPacket(
636 QuicPacketSequenceNumber sequence_number
,
637 QuicSequenceNumberLength sequence_number_length
,
638 QuicEncryptedPacket
* packet
,
639 QuicPacketEntropyHash entropy_hash
,
640 RetransmittableFrames
* retransmittable_frames
)
642 retransmittable_frames(retransmittable_frames
),
643 sequence_number(sequence_number
),
644 sequence_number_length(sequence_number_length
),
645 entropy_hash(entropy_hash
),
646 is_fec_packet(false) {
649 SerializedPacket::~SerializedPacket() {}
651 QuicEncryptedPacket
* QuicEncryptedPacket::Clone() const {
652 char* buffer
= new char[this->length()];
653 memcpy(buffer
, this->data(), this->length());
654 return new QuicEncryptedPacket(buffer
, this->length(), true);
657 ostream
& operator<<(ostream
& os
, const QuicEncryptedPacket
& s
) {
658 os
<< s
.length() << "-byte data";
662 TransmissionInfo::TransmissionInfo()
663 : retransmittable_frames(nullptr),
664 sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER
),
665 sent_time(QuicTime::Zero()),
668 transmission_type(NOT_RETRANSMISSION
),
669 all_transmissions(nullptr),
672 is_fec_packet(false) {
675 TransmissionInfo::TransmissionInfo(
676 RetransmittableFrames
* retransmittable_frames
,
677 QuicSequenceNumberLength sequence_number_length
,
678 TransmissionType transmission_type
,
680 QuicByteCount bytes_sent
,
682 : retransmittable_frames(retransmittable_frames
),
683 sequence_number_length(sequence_number_length
),
684 sent_time(sent_time
),
685 bytes_sent(bytes_sent
),
687 transmission_type(transmission_type
),
688 all_transmissions(nullptr),
691 is_fec_packet(is_fec_packet
) {