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()
79 packet_sequence_number(0),
80 is_in_fec_group(NOT_IN_FEC_GROUP
),
84 QuicPacketHeader::QuicPacketHeader(const QuicPacketPublicHeader
& header
)
85 : public_header(header
),
89 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()
110 QuicStreamFrame::QuicStreamFrame(const QuicStreamFrame
& frame
)
111 : stream_id(frame
.stream_id
),
113 offset(frame
.offset
),
115 notifier(frame
.notifier
) {
118 QuicStreamFrame::QuicStreamFrame(QuicStreamId stream_id
,
120 QuicStreamOffset offset
,
122 : stream_id(stream_id
),
128 string
* QuicStreamFrame::GetDataAsString() const {
129 string
* data_string
= new string();
130 data_string
->reserve(data
.TotalBufferSize());
131 for (size_t i
= 0; i
< data
.Size(); ++i
) {
132 data_string
->append(static_cast<char*>(data
.iovec()[i
].iov_base
),
133 data
.iovec()[i
].iov_len
);
135 DCHECK_EQ(data_string
->size(), data
.TotalBufferSize());
139 uint32
MakeQuicTag(char a
, char b
, char c
, char d
) {
140 return static_cast<uint32
>(a
) |
141 static_cast<uint32
>(b
) << 8 |
142 static_cast<uint32
>(c
) << 16 |
143 static_cast<uint32
>(d
) << 24;
146 bool ContainsQuicTag(const QuicTagVector
& tag_vector
, QuicTag tag
) {
147 return std::find(tag_vector
.begin(), tag_vector
.end(), tag
)
151 QuicVersionVector
QuicSupportedVersions() {
152 QuicVersionVector supported_versions
;
153 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
154 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
156 return supported_versions
;
159 QuicTag
QuicVersionToQuicTag(const QuicVersion version
) {
161 case QUIC_VERSION_19
:
162 return MakeQuicTag('Q', '0', '1', '9');
163 case QUIC_VERSION_21
:
164 return MakeQuicTag('Q', '0', '2', '1');
165 case QUIC_VERSION_22
:
166 return MakeQuicTag('Q', '0', '2', '2');
167 case QUIC_VERSION_23
:
168 return MakeQuicTag('Q', '0', '2', '3');
170 // This shold be an ERROR because we should never attempt to convert an
171 // invalid QuicVersion to be written to the wire.
172 LOG(ERROR
) << "Unsupported QuicVersion: " << version
;
177 QuicVersion
QuicTagToQuicVersion(const QuicTag version_tag
) {
178 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
179 if (version_tag
== QuicVersionToQuicTag(kSupportedQuicVersions
[i
])) {
180 return kSupportedQuicVersions
[i
];
183 // Reading from the client so this should not be considered an ERROR.
184 DVLOG(1) << "Unsupported QuicTag version: "
185 << QuicUtils::TagToString(version_tag
);
186 return QUIC_VERSION_UNSUPPORTED
;
189 #define RETURN_STRING_LITERAL(x) \
193 string
QuicVersionToString(const QuicVersion version
) {
195 RETURN_STRING_LITERAL(QUIC_VERSION_19
);
196 RETURN_STRING_LITERAL(QUIC_VERSION_21
);
197 RETURN_STRING_LITERAL(QUIC_VERSION_22
);
198 RETURN_STRING_LITERAL(QUIC_VERSION_23
);
200 return "QUIC_VERSION_UNSUPPORTED";
204 string
QuicVersionVectorToString(const QuicVersionVector
& versions
) {
206 for (size_t i
= 0; i
< versions
.size(); ++i
) {
210 result
.append(QuicVersionToString(versions
[i
]));
215 ostream
& operator<<(ostream
& os
, const QuicPacketHeader
& header
) {
216 os
<< "{ connection_id: " << header
.public_header
.connection_id
217 << ", connection_id_length:" << header
.public_header
.connection_id_length
218 << ", sequence_number_length:"
219 << header
.public_header
.sequence_number_length
220 << ", reset_flag: " << header
.public_header
.reset_flag
221 << ", version_flag: " << header
.public_header
.version_flag
;
222 if (header
.public_header
.version_flag
) {
224 for (size_t i
= 0; i
< header
.public_header
.versions
.size(); ++i
) {
225 os
<< header
.public_header
.versions
[0] << " ";
228 os
<< ", fec_flag: " << header
.fec_flag
229 << ", entropy_flag: " << header
.entropy_flag
230 << ", entropy hash: " << static_cast<int>(header
.entropy_hash
)
231 << ", sequence_number: " << header
.packet_sequence_number
232 << ", is_in_fec_group:" << header
.is_in_fec_group
233 << ", fec_group: " << header
.fec_group
<< "}\n";
237 bool IsAwaitingPacket(const QuicAckFrame
& ack_frame
,
238 QuicPacketSequenceNumber sequence_number
) {
239 return sequence_number
> ack_frame
.largest_observed
||
240 ContainsKey(ack_frame
.missing_packets
, sequence_number
);
243 void InsertMissingPacketsBetween(QuicAckFrame
* ack_frame
,
244 QuicPacketSequenceNumber lower
,
245 QuicPacketSequenceNumber higher
) {
246 for (QuicPacketSequenceNumber i
= lower
; i
< higher
; ++i
) {
247 ack_frame
->missing_packets
.insert(i
);
251 QuicStopWaitingFrame::QuicStopWaitingFrame()
256 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
258 QuicAckFrame::QuicAckFrame()
261 delta_time_largest_observed(QuicTime::Delta::Infinite()),
262 is_truncated(false) {}
264 QuicAckFrame::~QuicAckFrame() {}
266 CongestionFeedbackMessageTCP::CongestionFeedbackMessageTCP()
267 : receive_window(0) {
270 QuicCongestionFeedbackFrame::QuicCongestionFeedbackFrame() : type(kTCP
) {}
272 QuicCongestionFeedbackFrame::~QuicCongestionFeedbackFrame() {}
274 QuicRstStreamErrorCode
AdjustErrorForVersion(
275 QuicRstStreamErrorCode error_code
,
276 QuicVersion version
) {
280 QuicRstStreamFrame::QuicRstStreamFrame()
282 error_code(QUIC_STREAM_NO_ERROR
) {
285 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id
,
286 QuicRstStreamErrorCode error_code
,
287 QuicStreamOffset bytes_written
)
288 : stream_id(stream_id
),
289 error_code(error_code
),
290 byte_offset(bytes_written
) {
291 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
294 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
295 : error_code(QUIC_NO_ERROR
) {
298 QuicFrame::QuicFrame() {}
300 QuicFrame::QuicFrame(QuicPaddingFrame
* padding_frame
)
301 : type(PADDING_FRAME
),
302 padding_frame(padding_frame
) {
305 QuicFrame::QuicFrame(QuicStreamFrame
* stream_frame
)
306 : type(STREAM_FRAME
),
307 stream_frame(stream_frame
) {
310 QuicFrame::QuicFrame(QuicAckFrame
* frame
)
315 QuicFrame::QuicFrame(QuicCongestionFeedbackFrame
* frame
)
316 : type(CONGESTION_FEEDBACK_FRAME
),
317 congestion_feedback_frame(frame
) {
320 QuicFrame::QuicFrame(QuicStopWaitingFrame
* frame
)
321 : type(STOP_WAITING_FRAME
),
322 stop_waiting_frame(frame
) {
325 QuicFrame::QuicFrame(QuicPingFrame
* frame
)
330 QuicFrame::QuicFrame(QuicRstStreamFrame
* frame
)
331 : type(RST_STREAM_FRAME
),
332 rst_stream_frame(frame
) {
335 QuicFrame::QuicFrame(QuicConnectionCloseFrame
* frame
)
336 : type(CONNECTION_CLOSE_FRAME
),
337 connection_close_frame(frame
) {
340 QuicFrame::QuicFrame(QuicGoAwayFrame
* frame
)
341 : type(GOAWAY_FRAME
),
342 goaway_frame(frame
) {
345 QuicFrame::QuicFrame(QuicWindowUpdateFrame
* frame
)
346 : type(WINDOW_UPDATE_FRAME
),
347 window_update_frame(frame
) {
350 QuicFrame::QuicFrame(QuicBlockedFrame
* frame
)
351 : type(BLOCKED_FRAME
),
352 blocked_frame(frame
) {
355 QuicFecData::QuicFecData() : fec_group(0) {}
357 ostream
& operator<<(ostream
& os
, const QuicStopWaitingFrame
& sent_info
) {
358 os
<< "entropy_hash: " << static_cast<int>(sent_info
.entropy_hash
)
359 << " least_unacked: " << sent_info
.least_unacked
;
363 ostream
& operator<<(ostream
& os
, const QuicAckFrame
& ack_frame
) {
364 os
<< "entropy_hash: " << static_cast<int>(ack_frame
.entropy_hash
)
365 << " largest_observed: " << ack_frame
.largest_observed
366 << " delta_time_largest_observed: "
367 << ack_frame
.delta_time_largest_observed
.ToMicroseconds()
368 << " missing_packets: [ ";
369 for (SequenceNumberSet::const_iterator it
= ack_frame
.missing_packets
.begin();
370 it
!= ack_frame
.missing_packets
.end(); ++it
) {
373 os
<< " ] is_truncated: " << ack_frame
.is_truncated
;
374 os
<< " revived_packets: [ ";
375 for (SequenceNumberSet::const_iterator it
= ack_frame
.revived_packets
.begin();
376 it
!= ack_frame
.revived_packets
.end(); ++it
) {
379 os
<< " ] received_packets: [ ";
380 for (PacketTimeList::const_iterator it
=
381 ack_frame
.received_packet_times
.begin();
382 it
!= ack_frame
.received_packet_times
.end(); ++it
) {
383 os
<< it
->first
<< " at " << it
->second
.ToDebuggingValue() << " ";
389 ostream
& operator<<(ostream
& os
, const QuicFrame
& frame
) {
390 switch (frame
.type
) {
391 case PADDING_FRAME
: {
392 os
<< "type { PADDING_FRAME } ";
395 case RST_STREAM_FRAME
: {
396 os
<< "type { " << RST_STREAM_FRAME
<< " } " << *(frame
.rst_stream_frame
);
399 case CONNECTION_CLOSE_FRAME
: {
400 os
<< "type { CONNECTION_CLOSE_FRAME } "
401 << *(frame
.connection_close_frame
);
405 os
<< "type { GOAWAY_FRAME } " << *(frame
.goaway_frame
);
408 case WINDOW_UPDATE_FRAME
: {
409 os
<< "type { WINDOW_UPDATE_FRAME } " << *(frame
.window_update_frame
);
412 case BLOCKED_FRAME
: {
413 os
<< "type { BLOCKED_FRAME } " << *(frame
.blocked_frame
);
417 os
<< "type { STREAM_FRAME } " << *(frame
.stream_frame
);
421 os
<< "type { ACK_FRAME } " << *(frame
.ack_frame
);
424 case CONGESTION_FEEDBACK_FRAME
: {
425 os
<< "type { CONGESTION_FEEDBACK_FRAME } "
426 << *(frame
.congestion_feedback_frame
);
429 case STOP_WAITING_FRAME
: {
430 os
<< "type { STOP_WAITING_FRAME } " << *(frame
.stop_waiting_frame
);
434 os
<< "type { PING_FRAME } ";
438 LOG(ERROR
) << "Unknown frame type: " << frame
.type
;
445 ostream
& operator<<(ostream
& os
, const QuicRstStreamFrame
& rst_frame
) {
446 os
<< "stream_id { " << rst_frame
.stream_id
<< " } "
447 << "error_code { " << rst_frame
.error_code
<< " } "
448 << "error_details { " << rst_frame
.error_details
<< " }\n";
452 ostream
& operator<<(ostream
& os
,
453 const QuicConnectionCloseFrame
& connection_close_frame
) {
454 os
<< "error_code { " << connection_close_frame
.error_code
<< " } "
455 << "error_details { " << connection_close_frame
.error_details
<< " }\n";
459 ostream
& operator<<(ostream
& os
, const QuicGoAwayFrame
& goaway_frame
) {
460 os
<< "error_code { " << goaway_frame
.error_code
<< " } "
461 << "last_good_stream_id { " << goaway_frame
.last_good_stream_id
<< " } "
462 << "reason_phrase { " << goaway_frame
.reason_phrase
<< " }\n";
466 ostream
& operator<<(ostream
& os
,
467 const QuicWindowUpdateFrame
& window_update_frame
) {
468 os
<< "stream_id { " << window_update_frame
.stream_id
<< " } "
469 << "byte_offset { " << window_update_frame
.byte_offset
<< " }\n";
473 ostream
& operator<<(ostream
& os
, const QuicBlockedFrame
& blocked_frame
) {
474 os
<< "stream_id { " << blocked_frame
.stream_id
<< " }\n";
478 ostream
& operator<<(ostream
& os
, const QuicStreamFrame
& stream_frame
) {
479 os
<< "stream_id { " << stream_frame
.stream_id
<< " } "
480 << "fin { " << stream_frame
.fin
<< " } "
481 << "offset { " << stream_frame
.offset
<< " } "
483 << QuicUtils::StringToHexASCIIDump(*(stream_frame
.GetDataAsString()))
488 ostream
& operator<<(ostream
& os
,
489 const QuicCongestionFeedbackFrame
& congestion_frame
) {
490 os
<< "type: " << congestion_frame
.type
;
491 switch (congestion_frame
.type
) {
493 const CongestionFeedbackMessageTCP
& tcp
= congestion_frame
.tcp
;
494 os
<< " receive_window: " << tcp
.receive_window
;
501 QuicGoAwayFrame::QuicGoAwayFrame()
502 : error_code(QUIC_NO_ERROR
),
503 last_good_stream_id(0) {
506 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code
,
507 QuicStreamId last_good_stream_id
,
508 const string
& reason
)
509 : error_code(error_code
),
510 last_good_stream_id(last_good_stream_id
),
511 reason_phrase(reason
) {
512 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
515 QuicData::QuicData(const char* buffer
,
519 owns_buffer_(false) {
522 QuicData::QuicData(char* buffer
,
527 owns_buffer_(owns_buffer
) {
530 QuicData::~QuicData() {
532 delete [] const_cast<char*>(buffer_
);
536 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id
,
537 QuicStreamOffset byte_offset
)
538 : stream_id(stream_id
),
539 byte_offset(byte_offset
) {}
541 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id
)
542 : stream_id(stream_id
) {}
544 QuicPacket::QuicPacket(char* buffer
,
547 QuicConnectionIdLength connection_id_length
,
548 bool includes_version
,
549 QuicSequenceNumberLength sequence_number_length
,
551 : QuicData(buffer
, length
, owns_buffer
),
553 is_fec_packet_(is_fec_packet
),
554 connection_id_length_(connection_id_length
),
555 includes_version_(includes_version
),
556 sequence_number_length_(sequence_number_length
) {
559 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer
,
561 : QuicData(buffer
, length
) {
564 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer
,
567 : QuicData(buffer
, length
, owns_buffer
) {
570 StringPiece
QuicPacket::FecProtectedData() const {
571 const size_t start_of_fec
= GetStartOfFecProtectedData(
572 connection_id_length_
, includes_version_
, sequence_number_length_
);
573 return StringPiece(data() + start_of_fec
, length() - start_of_fec
);
576 StringPiece
QuicPacket::AssociatedData() const {
578 data() + kStartOfHashData
,
579 GetStartOfEncryptedData(
580 connection_id_length_
, includes_version_
, sequence_number_length_
) -
584 StringPiece
QuicPacket::BeforePlaintext() const {
585 return StringPiece(data(), GetStartOfEncryptedData(connection_id_length_
,
587 sequence_number_length_
));
590 StringPiece
QuicPacket::Plaintext() const {
591 const size_t start_of_encrypted_data
=
592 GetStartOfEncryptedData(
593 connection_id_length_
, includes_version_
, sequence_number_length_
);
594 return StringPiece(data() + start_of_encrypted_data
,
595 length() - start_of_encrypted_data
);
598 RetransmittableFrames::RetransmittableFrames()
599 : encryption_level_(NUM_ENCRYPTION_LEVELS
),
600 has_crypto_handshake_(NOT_HANDSHAKE
) {
603 RetransmittableFrames::~RetransmittableFrames() {
604 for (QuicFrames::iterator it
= frames_
.begin(); it
!= frames_
.end(); ++it
) {
607 delete it
->padding_frame
;
610 delete it
->stream_frame
;
613 delete it
->ack_frame
;
615 case CONGESTION_FEEDBACK_FRAME
:
616 delete it
->congestion_feedback_frame
;
618 case STOP_WAITING_FRAME
:
619 delete it
->stop_waiting_frame
;
622 delete it
->ping_frame
;
624 case RST_STREAM_FRAME
:
625 delete it
->rst_stream_frame
;
627 case CONNECTION_CLOSE_FRAME
:
628 delete it
->connection_close_frame
;
631 delete it
->goaway_frame
;
633 case WINDOW_UPDATE_FRAME
:
634 delete it
->window_update_frame
;
637 delete it
->blocked_frame
;
639 case NUM_FRAME_TYPES
:
640 DCHECK(false) << "Cannot delete type: " << it
->type
;
643 STLDeleteElements(&stream_data_
);
646 const QuicFrame
& RetransmittableFrames::AddStreamFrame(
647 QuicStreamFrame
* stream_frame
) {
648 // Make an owned copy of the stream frame's data.
649 stream_data_
.push_back(stream_frame
->GetDataAsString());
650 // Ensure the stream frame's IOVector points to the owned copy of the data.
651 stream_frame
->data
.Clear();
652 stream_frame
->data
.Append(const_cast<char*>(stream_data_
.back()->data()),
653 stream_data_
.back()->size());
654 frames_
.push_back(QuicFrame(stream_frame
));
655 if (stream_frame
->stream_id
== kCryptoStreamId
) {
656 has_crypto_handshake_
= IS_HANDSHAKE
;
658 return frames_
.back();
661 const QuicFrame
& RetransmittableFrames::AddNonStreamFrame(
662 const QuicFrame
& frame
) {
663 DCHECK_NE(frame
.type
, STREAM_FRAME
);
664 frames_
.push_back(frame
);
665 return frames_
.back();
668 void RetransmittableFrames::set_encryption_level(EncryptionLevel level
) {
669 encryption_level_
= level
;
672 SerializedPacket::SerializedPacket(
673 QuicPacketSequenceNumber sequence_number
,
674 QuicSequenceNumberLength sequence_number_length
,
676 QuicPacketEntropyHash entropy_hash
,
677 RetransmittableFrames
* retransmittable_frames
)
678 : sequence_number(sequence_number
),
679 sequence_number_length(sequence_number_length
),
681 entropy_hash(entropy_hash
),
682 retransmittable_frames(retransmittable_frames
) {
685 SerializedPacket::~SerializedPacket() {}
687 QuicEncryptedPacket
* QuicEncryptedPacket::Clone() const {
688 char* buffer
= new char[this->length()];
689 memcpy(buffer
, this->data(), this->length());
690 return new QuicEncryptedPacket(buffer
, this->length(), true);
693 ostream
& operator<<(ostream
& os
, const QuicEncryptedPacket
& s
) {
694 os
<< s
.length() << "-byte data";
698 TransmissionInfo::TransmissionInfo()
699 : retransmittable_frames(nullptr),
700 sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER
),
701 sent_time(QuicTime::Zero()),
704 transmission_type(NOT_RETRANSMISSION
),
705 all_transmissions(nullptr),
708 is_fec_packet(false) {}
710 TransmissionInfo::TransmissionInfo(
711 RetransmittableFrames
* retransmittable_frames
,
712 QuicSequenceNumberLength sequence_number_length
,
713 TransmissionType transmission_type
,
715 : retransmittable_frames(retransmittable_frames
),
716 sequence_number_length(sequence_number_length
),
717 sent_time(sent_time
),
720 transmission_type(transmission_type
),
721 all_transmissions(nullptr),
724 is_fec_packet(false) {}