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_23
:
162 return MakeQuicTag('Q', '0', '2', '3');
163 case QUIC_VERSION_24
:
164 return MakeQuicTag('Q', '0', '2', '4');
166 // This shold be an ERROR because we should never attempt to convert an
167 // invalid QuicVersion to be written to the wire.
168 LOG(ERROR
) << "Unsupported QuicVersion: " << version
;
173 QuicVersion
QuicTagToQuicVersion(const QuicTag version_tag
) {
174 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
175 if (version_tag
== QuicVersionToQuicTag(kSupportedQuicVersions
[i
])) {
176 return kSupportedQuicVersions
[i
];
179 // Reading from the client so this should not be considered an ERROR.
180 DVLOG(1) << "Unsupported QuicTag version: "
181 << QuicUtils::TagToString(version_tag
);
182 return QUIC_VERSION_UNSUPPORTED
;
185 #define RETURN_STRING_LITERAL(x) \
189 string
QuicVersionToString(const QuicVersion version
) {
191 RETURN_STRING_LITERAL(QUIC_VERSION_23
);
192 RETURN_STRING_LITERAL(QUIC_VERSION_24
);
194 return "QUIC_VERSION_UNSUPPORTED";
198 string
QuicVersionVectorToString(const QuicVersionVector
& versions
) {
200 for (size_t i
= 0; i
< versions
.size(); ++i
) {
204 result
.append(QuicVersionToString(versions
[i
]));
209 ostream
& operator<<(ostream
& os
, const QuicPacketHeader
& header
) {
210 os
<< "{ connection_id: " << header
.public_header
.connection_id
211 << ", connection_id_length:" << header
.public_header
.connection_id_length
212 << ", sequence_number_length:"
213 << header
.public_header
.sequence_number_length
214 << ", reset_flag: " << header
.public_header
.reset_flag
215 << ", version_flag: " << header
.public_header
.version_flag
;
216 if (header
.public_header
.version_flag
) {
218 for (size_t i
= 0; i
< header
.public_header
.versions
.size(); ++i
) {
219 os
<< header
.public_header
.versions
[0] << " ";
222 os
<< ", fec_flag: " << header
.fec_flag
223 << ", entropy_flag: " << header
.entropy_flag
224 << ", entropy hash: " << static_cast<int>(header
.entropy_hash
)
225 << ", sequence_number: " << header
.packet_sequence_number
226 << ", is_in_fec_group:" << header
.is_in_fec_group
227 << ", fec_group: " << header
.fec_group
<< "}\n";
231 bool IsAwaitingPacket(const QuicAckFrame
& ack_frame
,
232 QuicPacketSequenceNumber sequence_number
) {
233 return sequence_number
> ack_frame
.largest_observed
||
234 ContainsKey(ack_frame
.missing_packets
, sequence_number
);
237 void InsertMissingPacketsBetween(QuicAckFrame
* ack_frame
,
238 QuicPacketSequenceNumber lower
,
239 QuicPacketSequenceNumber higher
) {
240 for (QuicPacketSequenceNumber i
= lower
; i
< higher
; ++i
) {
241 ack_frame
->missing_packets
.insert(i
);
245 QuicStopWaitingFrame::QuicStopWaitingFrame()
250 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
252 QuicAckFrame::QuicAckFrame()
255 delta_time_largest_observed(QuicTime::Delta::Infinite()),
256 is_truncated(false) {}
258 QuicAckFrame::~QuicAckFrame() {}
260 QuicRstStreamErrorCode
AdjustErrorForVersion(
261 QuicRstStreamErrorCode error_code
,
262 QuicVersion version
) {
266 QuicRstStreamFrame::QuicRstStreamFrame()
268 error_code(QUIC_STREAM_NO_ERROR
) {
271 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id
,
272 QuicRstStreamErrorCode error_code
,
273 QuicStreamOffset bytes_written
)
274 : stream_id(stream_id
),
275 error_code(error_code
),
276 byte_offset(bytes_written
) {
277 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
280 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
281 : error_code(QUIC_NO_ERROR
) {
284 QuicFrame::QuicFrame() {}
286 QuicFrame::QuicFrame(QuicPaddingFrame
* padding_frame
)
287 : type(PADDING_FRAME
),
288 padding_frame(padding_frame
) {
291 QuicFrame::QuicFrame(QuicStreamFrame
* stream_frame
)
292 : type(STREAM_FRAME
),
293 stream_frame(stream_frame
) {
296 QuicFrame::QuicFrame(QuicAckFrame
* frame
)
301 QuicFrame::QuicFrame(QuicStopWaitingFrame
* frame
)
302 : type(STOP_WAITING_FRAME
),
303 stop_waiting_frame(frame
) {
306 QuicFrame::QuicFrame(QuicPingFrame
* frame
)
311 QuicFrame::QuicFrame(QuicRstStreamFrame
* frame
)
312 : type(RST_STREAM_FRAME
),
313 rst_stream_frame(frame
) {
316 QuicFrame::QuicFrame(QuicConnectionCloseFrame
* frame
)
317 : type(CONNECTION_CLOSE_FRAME
),
318 connection_close_frame(frame
) {
321 QuicFrame::QuicFrame(QuicGoAwayFrame
* frame
)
322 : type(GOAWAY_FRAME
),
323 goaway_frame(frame
) {
326 QuicFrame::QuicFrame(QuicWindowUpdateFrame
* frame
)
327 : type(WINDOW_UPDATE_FRAME
),
328 window_update_frame(frame
) {
331 QuicFrame::QuicFrame(QuicBlockedFrame
* frame
)
332 : type(BLOCKED_FRAME
),
333 blocked_frame(frame
) {
336 QuicFecData::QuicFecData() : fec_group(0) {}
338 ostream
& operator<<(ostream
& os
, const QuicStopWaitingFrame
& sent_info
) {
339 os
<< "entropy_hash: " << static_cast<int>(sent_info
.entropy_hash
)
340 << " least_unacked: " << sent_info
.least_unacked
;
344 ostream
& operator<<(ostream
& os
, const QuicAckFrame
& ack_frame
) {
345 os
<< "entropy_hash: " << static_cast<int>(ack_frame
.entropy_hash
)
346 << " largest_observed: " << ack_frame
.largest_observed
347 << " delta_time_largest_observed: "
348 << ack_frame
.delta_time_largest_observed
.ToMicroseconds()
349 << " missing_packets: [ ";
350 for (SequenceNumberSet::const_iterator it
= ack_frame
.missing_packets
.begin();
351 it
!= ack_frame
.missing_packets
.end(); ++it
) {
354 os
<< " ] is_truncated: " << ack_frame
.is_truncated
;
355 os
<< " revived_packets: [ ";
356 for (SequenceNumberSet::const_iterator it
= ack_frame
.revived_packets
.begin();
357 it
!= ack_frame
.revived_packets
.end(); ++it
) {
360 os
<< " ] received_packets: [ ";
361 for (PacketTimeList::const_iterator it
=
362 ack_frame
.received_packet_times
.begin();
363 it
!= ack_frame
.received_packet_times
.end(); ++it
) {
364 os
<< it
->first
<< " at " << it
->second
.ToDebuggingValue() << " ";
370 ostream
& operator<<(ostream
& os
, const QuicFrame
& frame
) {
371 switch (frame
.type
) {
372 case PADDING_FRAME
: {
373 os
<< "type { PADDING_FRAME } ";
376 case RST_STREAM_FRAME
: {
377 os
<< "type { " << RST_STREAM_FRAME
<< " } " << *(frame
.rst_stream_frame
);
380 case CONNECTION_CLOSE_FRAME
: {
381 os
<< "type { CONNECTION_CLOSE_FRAME } "
382 << *(frame
.connection_close_frame
);
386 os
<< "type { GOAWAY_FRAME } " << *(frame
.goaway_frame
);
389 case WINDOW_UPDATE_FRAME
: {
390 os
<< "type { WINDOW_UPDATE_FRAME } " << *(frame
.window_update_frame
);
393 case BLOCKED_FRAME
: {
394 os
<< "type { BLOCKED_FRAME } " << *(frame
.blocked_frame
);
398 os
<< "type { STREAM_FRAME } " << *(frame
.stream_frame
);
402 os
<< "type { ACK_FRAME } " << *(frame
.ack_frame
);
405 case STOP_WAITING_FRAME
: {
406 os
<< "type { STOP_WAITING_FRAME } " << *(frame
.stop_waiting_frame
);
410 os
<< "type { PING_FRAME } ";
414 LOG(ERROR
) << "Unknown frame type: " << frame
.type
;
421 ostream
& operator<<(ostream
& os
, const QuicRstStreamFrame
& rst_frame
) {
422 os
<< "stream_id { " << rst_frame
.stream_id
<< " } "
423 << "error_code { " << rst_frame
.error_code
<< " } "
424 << "error_details { " << rst_frame
.error_details
<< " }\n";
428 ostream
& operator<<(ostream
& os
,
429 const QuicConnectionCloseFrame
& connection_close_frame
) {
430 os
<< "error_code { " << connection_close_frame
.error_code
<< " } "
431 << "error_details { " << connection_close_frame
.error_details
<< " }\n";
435 ostream
& operator<<(ostream
& os
, const QuicGoAwayFrame
& goaway_frame
) {
436 os
<< "error_code { " << goaway_frame
.error_code
<< " } "
437 << "last_good_stream_id { " << goaway_frame
.last_good_stream_id
<< " } "
438 << "reason_phrase { " << goaway_frame
.reason_phrase
<< " }\n";
442 ostream
& operator<<(ostream
& os
,
443 const QuicWindowUpdateFrame
& window_update_frame
) {
444 os
<< "stream_id { " << window_update_frame
.stream_id
<< " } "
445 << "byte_offset { " << window_update_frame
.byte_offset
<< " }\n";
449 ostream
& operator<<(ostream
& os
, const QuicBlockedFrame
& blocked_frame
) {
450 os
<< "stream_id { " << blocked_frame
.stream_id
<< " }\n";
454 ostream
& operator<<(ostream
& os
, const QuicStreamFrame
& stream_frame
) {
455 os
<< "stream_id { " << stream_frame
.stream_id
<< " } "
456 << "fin { " << stream_frame
.fin
<< " } "
457 << "offset { " << stream_frame
.offset
<< " } "
459 << QuicUtils::StringToHexASCIIDump(*(stream_frame
.GetDataAsString()))
464 QuicGoAwayFrame::QuicGoAwayFrame()
465 : error_code(QUIC_NO_ERROR
),
466 last_good_stream_id(0) {
469 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code
,
470 QuicStreamId last_good_stream_id
,
471 const string
& reason
)
472 : error_code(error_code
),
473 last_good_stream_id(last_good_stream_id
),
474 reason_phrase(reason
) {
475 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
478 QuicData::QuicData(const char* buffer
,
482 owns_buffer_(false) {
485 QuicData::QuicData(char* buffer
,
490 owns_buffer_(owns_buffer
) {
493 QuicData::~QuicData() {
495 delete [] const_cast<char*>(buffer_
);
499 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id
,
500 QuicStreamOffset byte_offset
)
501 : stream_id(stream_id
),
502 byte_offset(byte_offset
) {}
504 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id
)
505 : stream_id(stream_id
) {}
507 QuicPacket::QuicPacket(char* buffer
,
510 QuicConnectionIdLength connection_id_length
,
511 bool includes_version
,
512 QuicSequenceNumberLength sequence_number_length
)
513 : QuicData(buffer
, length
, owns_buffer
),
515 connection_id_length_(connection_id_length
),
516 includes_version_(includes_version
),
517 sequence_number_length_(sequence_number_length
) {
520 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer
,
522 : QuicData(buffer
, length
) {
525 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer
,
528 : QuicData(buffer
, length
, owns_buffer
) {
531 StringPiece
QuicPacket::FecProtectedData() const {
532 const size_t start_of_fec
= GetStartOfFecProtectedData(
533 connection_id_length_
, includes_version_
, sequence_number_length_
);
534 return StringPiece(data() + start_of_fec
, length() - start_of_fec
);
537 StringPiece
QuicPacket::AssociatedData() const {
539 data() + kStartOfHashData
,
540 GetStartOfEncryptedData(
541 connection_id_length_
, includes_version_
, sequence_number_length_
) -
545 StringPiece
QuicPacket::BeforePlaintext() const {
546 return StringPiece(data(), GetStartOfEncryptedData(connection_id_length_
,
548 sequence_number_length_
));
551 StringPiece
QuicPacket::Plaintext() const {
552 const size_t start_of_encrypted_data
=
553 GetStartOfEncryptedData(
554 connection_id_length_
, includes_version_
, sequence_number_length_
);
555 return StringPiece(data() + start_of_encrypted_data
,
556 length() - start_of_encrypted_data
);
559 RetransmittableFrames::RetransmittableFrames(EncryptionLevel level
)
560 : encryption_level_(level
), has_crypto_handshake_(NOT_HANDSHAKE
) {
563 RetransmittableFrames::~RetransmittableFrames() {
564 for (QuicFrames::iterator it
= frames_
.begin(); it
!= frames_
.end(); ++it
) {
567 delete it
->padding_frame
;
570 delete it
->stream_frame
;
573 delete it
->ack_frame
;
575 case STOP_WAITING_FRAME
:
576 delete it
->stop_waiting_frame
;
579 delete it
->ping_frame
;
581 case RST_STREAM_FRAME
:
582 delete it
->rst_stream_frame
;
584 case CONNECTION_CLOSE_FRAME
:
585 delete it
->connection_close_frame
;
588 delete it
->goaway_frame
;
590 case WINDOW_UPDATE_FRAME
:
591 delete it
->window_update_frame
;
594 delete it
->blocked_frame
;
596 case NUM_FRAME_TYPES
:
597 DCHECK(false) << "Cannot delete type: " << it
->type
;
600 STLDeleteElements(&stream_data_
);
603 const QuicFrame
& RetransmittableFrames::AddStreamFrame(
604 QuicStreamFrame
* stream_frame
) {
605 // Make an owned copy of the stream frame's data.
606 stream_data_
.push_back(stream_frame
->GetDataAsString());
607 // Ensure the stream frame's IOVector points to the owned copy of the data.
608 stream_frame
->data
.Clear();
609 stream_frame
->data
.Append(const_cast<char*>(stream_data_
.back()->data()),
610 stream_data_
.back()->size());
611 frames_
.push_back(QuicFrame(stream_frame
));
612 if (stream_frame
->stream_id
== kCryptoStreamId
) {
613 has_crypto_handshake_
= IS_HANDSHAKE
;
615 return frames_
.back();
618 const QuicFrame
& RetransmittableFrames::AddNonStreamFrame(
619 const QuicFrame
& frame
) {
620 DCHECK_NE(frame
.type
, STREAM_FRAME
);
621 frames_
.push_back(frame
);
622 return frames_
.back();
625 SerializedPacket::SerializedPacket(
626 QuicPacketSequenceNumber sequence_number
,
627 QuicSequenceNumberLength sequence_number_length
,
628 QuicEncryptedPacket
* packet
,
629 QuicPacketEntropyHash entropy_hash
,
630 RetransmittableFrames
* retransmittable_frames
)
631 : sequence_number(sequence_number
),
632 sequence_number_length(sequence_number_length
),
634 entropy_hash(entropy_hash
),
635 retransmittable_frames(retransmittable_frames
),
636 is_fec_packet(false) {
639 SerializedPacket::~SerializedPacket() {}
641 QuicEncryptedPacket
* QuicEncryptedPacket::Clone() const {
642 char* buffer
= new char[this->length()];
643 memcpy(buffer
, this->data(), this->length());
644 return new QuicEncryptedPacket(buffer
, this->length(), true);
647 ostream
& operator<<(ostream
& os
, const QuicEncryptedPacket
& s
) {
648 os
<< s
.length() << "-byte data";
652 TransmissionInfo::TransmissionInfo()
653 : retransmittable_frames(nullptr),
654 sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER
),
655 sent_time(QuicTime::Zero()),
658 transmission_type(NOT_RETRANSMISSION
),
659 all_transmissions(nullptr),
662 is_fec_packet(false) {}
664 TransmissionInfo::TransmissionInfo(
665 RetransmittableFrames
* retransmittable_frames
,
666 QuicSequenceNumberLength sequence_number_length
,
667 TransmissionType transmission_type
,
669 : retransmittable_frames(retransmittable_frames
),
670 sequence_number_length(sequence_number_length
),
671 sent_time(sent_time
),
674 transmission_type(transmission_type
),
675 all_transmissions(nullptr),
678 is_fec_packet(false) {}