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() : 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 string
* QuicStreamFrame::GetDataAsString() const {
122 string
* data_string
= new string();
123 data_string
->reserve(data
.TotalBufferSize());
124 for (size_t i
= 0; i
< data
.Size(); ++i
) {
125 data_string
->append(static_cast<char*>(data
.iovec()[i
].iov_base
),
126 data
.iovec()[i
].iov_len
);
128 DCHECK_EQ(data_string
->size(), data
.TotalBufferSize());
132 uint32
MakeQuicTag(char a
, char b
, char c
, char d
) {
133 return static_cast<uint32
>(a
) |
134 static_cast<uint32
>(b
) << 8 |
135 static_cast<uint32
>(c
) << 16 |
136 static_cast<uint32
>(d
) << 24;
139 bool ContainsQuicTag(const QuicTagVector
& tag_vector
, QuicTag tag
) {
140 return std::find(tag_vector
.begin(), tag_vector
.end(), tag
)
144 QuicVersionVector
QuicSupportedVersions() {
145 QuicVersionVector supported_versions
;
146 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
147 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
149 return supported_versions
;
152 QuicTag
QuicVersionToQuicTag(const QuicVersion version
) {
154 case QUIC_VERSION_24
:
155 return MakeQuicTag('Q', '0', '2', '4');
156 case QUIC_VERSION_25
:
157 return MakeQuicTag('Q', '0', '2', '5');
159 // This shold be an ERROR because we should never attempt to convert an
160 // invalid QuicVersion to be written to the wire.
161 LOG(ERROR
) << "Unsupported QuicVersion: " << version
;
166 QuicVersion
QuicTagToQuicVersion(const QuicTag version_tag
) {
167 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
168 if (version_tag
== QuicVersionToQuicTag(kSupportedQuicVersions
[i
])) {
169 return kSupportedQuicVersions
[i
];
172 // Reading from the client so this should not be considered an ERROR.
173 DVLOG(1) << "Unsupported QuicTag version: "
174 << QuicUtils::TagToString(version_tag
);
175 return QUIC_VERSION_UNSUPPORTED
;
178 #define RETURN_STRING_LITERAL(x) \
182 string
QuicVersionToString(const QuicVersion version
) {
184 RETURN_STRING_LITERAL(QUIC_VERSION_24
);
185 RETURN_STRING_LITERAL(QUIC_VERSION_25
);
187 return "QUIC_VERSION_UNSUPPORTED";
191 string
QuicVersionVectorToString(const QuicVersionVector
& versions
) {
193 for (size_t i
= 0; i
< versions
.size(); ++i
) {
197 result
.append(QuicVersionToString(versions
[i
]));
202 ostream
& operator<<(ostream
& os
, const Perspective
& s
) {
203 if (s
== Perspective::IS_SERVER
) {
211 ostream
& operator<<(ostream
& os
, const QuicPacketHeader
& header
) {
212 os
<< "{ connection_id: " << header
.public_header
.connection_id
213 << ", connection_id_length:" << header
.public_header
.connection_id_length
214 << ", sequence_number_length:"
215 << header
.public_header
.sequence_number_length
216 << ", reset_flag: " << header
.public_header
.reset_flag
217 << ", version_flag: " << header
.public_header
.version_flag
;
218 if (header
.public_header
.version_flag
) {
220 for (size_t i
= 0; i
< header
.public_header
.versions
.size(); ++i
) {
221 os
<< header
.public_header
.versions
[i
] << " ";
224 os
<< ", fec_flag: " << header
.fec_flag
225 << ", entropy_flag: " << header
.entropy_flag
226 << ", entropy hash: " << static_cast<int>(header
.entropy_hash
)
227 << ", sequence_number: " << header
.packet_sequence_number
228 << ", is_in_fec_group:" << header
.is_in_fec_group
229 << ", fec_group: " << header
.fec_group
<< "}\n";
233 bool IsAwaitingPacket(const QuicAckFrame
& ack_frame
,
234 QuicPacketSequenceNumber sequence_number
) {
235 return sequence_number
> ack_frame
.largest_observed
||
236 ContainsKey(ack_frame
.missing_packets
, sequence_number
);
239 void InsertMissingPacketsBetween(QuicAckFrame
* ack_frame
,
240 QuicPacketSequenceNumber lower
,
241 QuicPacketSequenceNumber higher
) {
242 for (QuicPacketSequenceNumber i
= lower
; i
< higher
; ++i
) {
243 ack_frame
->missing_packets
.insert(i
);
247 QuicStopWaitingFrame::QuicStopWaitingFrame()
252 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
254 QuicAckFrame::QuicAckFrame()
257 delta_time_largest_observed(QuicTime::Delta::Infinite()),
258 is_truncated(false) {}
260 QuicAckFrame::~QuicAckFrame() {}
262 QuicRstStreamErrorCode
AdjustErrorForVersion(
263 QuicRstStreamErrorCode error_code
,
264 QuicVersion version
) {
268 QuicRstStreamFrame::QuicRstStreamFrame()
270 error_code(QUIC_STREAM_NO_ERROR
) {
273 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id
,
274 QuicRstStreamErrorCode error_code
,
275 QuicStreamOffset bytes_written
)
276 : stream_id(stream_id
),
277 error_code(error_code
),
278 byte_offset(bytes_written
) {
279 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
282 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
283 : error_code(QUIC_NO_ERROR
) {
286 QuicFrame::QuicFrame() {}
288 QuicFrame::QuicFrame(QuicPaddingFrame
* padding_frame
)
289 : type(PADDING_FRAME
),
290 padding_frame(padding_frame
) {
293 QuicFrame::QuicFrame(QuicStreamFrame
* stream_frame
)
294 : type(STREAM_FRAME
),
295 stream_frame(stream_frame
) {
298 QuicFrame::QuicFrame(QuicAckFrame
* frame
)
303 QuicFrame::QuicFrame(QuicStopWaitingFrame
* frame
)
304 : type(STOP_WAITING_FRAME
),
305 stop_waiting_frame(frame
) {
308 QuicFrame::QuicFrame(QuicPingFrame
* frame
)
313 QuicFrame::QuicFrame(QuicRstStreamFrame
* frame
)
314 : type(RST_STREAM_FRAME
),
315 rst_stream_frame(frame
) {
318 QuicFrame::QuicFrame(QuicConnectionCloseFrame
* frame
)
319 : type(CONNECTION_CLOSE_FRAME
),
320 connection_close_frame(frame
) {
323 QuicFrame::QuicFrame(QuicGoAwayFrame
* frame
)
324 : type(GOAWAY_FRAME
),
325 goaway_frame(frame
) {
328 QuicFrame::QuicFrame(QuicWindowUpdateFrame
* frame
)
329 : type(WINDOW_UPDATE_FRAME
),
330 window_update_frame(frame
) {
333 QuicFrame::QuicFrame(QuicBlockedFrame
* frame
)
334 : type(BLOCKED_FRAME
),
335 blocked_frame(frame
) {
338 QuicFecData::QuicFecData() : fec_group(0) {}
340 ostream
& operator<<(ostream
& os
, const QuicStopWaitingFrame
& sent_info
) {
341 os
<< "entropy_hash: " << static_cast<int>(sent_info
.entropy_hash
)
342 << " least_unacked: " << sent_info
.least_unacked
;
346 ostream
& operator<<(ostream
& os
, const QuicAckFrame
& ack_frame
) {
347 os
<< "entropy_hash: " << static_cast<int>(ack_frame
.entropy_hash
)
348 << " largest_observed: " << ack_frame
.largest_observed
349 << " delta_time_largest_observed: "
350 << ack_frame
.delta_time_largest_observed
.ToMicroseconds()
351 << " missing_packets: [ ";
352 for (SequenceNumberSet::const_iterator it
= ack_frame
.missing_packets
.begin();
353 it
!= ack_frame
.missing_packets
.end(); ++it
) {
356 os
<< " ] is_truncated: " << ack_frame
.is_truncated
;
357 os
<< " revived_packets: [ ";
358 for (SequenceNumberSet::const_iterator it
= ack_frame
.revived_packets
.begin();
359 it
!= ack_frame
.revived_packets
.end(); ++it
) {
362 os
<< " ] received_packets: [ ";
363 for (PacketTimeList::const_iterator it
=
364 ack_frame
.received_packet_times
.begin();
365 it
!= ack_frame
.received_packet_times
.end(); ++it
) {
366 os
<< it
->first
<< " at " << it
->second
.ToDebuggingValue() << " ";
372 ostream
& operator<<(ostream
& os
, const QuicFrame
& frame
) {
373 switch (frame
.type
) {
374 case PADDING_FRAME
: {
375 os
<< "type { PADDING_FRAME } ";
378 case RST_STREAM_FRAME
: {
379 os
<< "type { RST_STREAM_FRAME } " << *(frame
.rst_stream_frame
);
382 case CONNECTION_CLOSE_FRAME
: {
383 os
<< "type { CONNECTION_CLOSE_FRAME } "
384 << *(frame
.connection_close_frame
);
388 os
<< "type { GOAWAY_FRAME } " << *(frame
.goaway_frame
);
391 case WINDOW_UPDATE_FRAME
: {
392 os
<< "type { WINDOW_UPDATE_FRAME } " << *(frame
.window_update_frame
);
395 case BLOCKED_FRAME
: {
396 os
<< "type { BLOCKED_FRAME } " << *(frame
.blocked_frame
);
400 os
<< "type { STREAM_FRAME } " << *(frame
.stream_frame
);
404 os
<< "type { ACK_FRAME } " << *(frame
.ack_frame
);
407 case STOP_WAITING_FRAME
: {
408 os
<< "type { STOP_WAITING_FRAME } " << *(frame
.stop_waiting_frame
);
412 os
<< "type { PING_FRAME } ";
416 LOG(ERROR
) << "Unknown frame type: " << frame
.type
;
423 ostream
& operator<<(ostream
& os
, const QuicRstStreamFrame
& rst_frame
) {
424 os
<< "stream_id { " << rst_frame
.stream_id
<< " } "
425 << "error_code { " << rst_frame
.error_code
<< " } "
426 << "error_details { " << rst_frame
.error_details
<< " }\n";
430 ostream
& operator<<(ostream
& os
,
431 const QuicConnectionCloseFrame
& connection_close_frame
) {
432 os
<< "error_code { " << connection_close_frame
.error_code
<< " } "
433 << "error_details { " << connection_close_frame
.error_details
<< " }\n";
437 ostream
& operator<<(ostream
& os
, const QuicGoAwayFrame
& goaway_frame
) {
438 os
<< "error_code { " << goaway_frame
.error_code
<< " } "
439 << "last_good_stream_id { " << goaway_frame
.last_good_stream_id
<< " } "
440 << "reason_phrase { " << goaway_frame
.reason_phrase
<< " }\n";
444 ostream
& operator<<(ostream
& os
,
445 const QuicWindowUpdateFrame
& window_update_frame
) {
446 os
<< "stream_id { " << window_update_frame
.stream_id
<< " } "
447 << "byte_offset { " << window_update_frame
.byte_offset
<< " }\n";
451 ostream
& operator<<(ostream
& os
, const QuicBlockedFrame
& blocked_frame
) {
452 os
<< "stream_id { " << blocked_frame
.stream_id
<< " }\n";
456 ostream
& operator<<(ostream
& os
, const QuicStreamFrame
& stream_frame
) {
457 os
<< "stream_id { " << stream_frame
.stream_id
<< " } "
458 << "fin { " << stream_frame
.fin
<< " } "
459 << "offset { " << stream_frame
.offset
<< " } "
461 << QuicUtils::StringToHexASCIIDump(*(stream_frame
.GetDataAsString()))
466 QuicGoAwayFrame::QuicGoAwayFrame()
467 : error_code(QUIC_NO_ERROR
),
468 last_good_stream_id(0) {
471 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code
,
472 QuicStreamId last_good_stream_id
,
473 const string
& reason
)
474 : error_code(error_code
),
475 last_good_stream_id(last_good_stream_id
),
476 reason_phrase(reason
) {
477 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
480 QuicData::QuicData(const char* buffer
,
484 owns_buffer_(false) {
487 QuicData::QuicData(char* buffer
,
492 owns_buffer_(owns_buffer
) {
495 QuicData::~QuicData() {
497 delete [] const_cast<char*>(buffer_
);
501 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id
,
502 QuicStreamOffset byte_offset
)
503 : stream_id(stream_id
),
504 byte_offset(byte_offset
) {}
506 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id
)
507 : stream_id(stream_id
) {}
509 QuicPacket::QuicPacket(char* buffer
,
512 QuicConnectionIdLength connection_id_length
,
513 bool includes_version
,
514 QuicSequenceNumberLength sequence_number_length
)
515 : QuicData(buffer
, length
, owns_buffer
),
517 connection_id_length_(connection_id_length
),
518 includes_version_(includes_version
),
519 sequence_number_length_(sequence_number_length
) {
522 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer
,
524 : QuicData(buffer
, length
) {
527 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer
,
530 : QuicData(buffer
, length
, owns_buffer
) {
533 StringPiece
QuicPacket::FecProtectedData() const {
534 const size_t start_of_fec
= GetStartOfFecProtectedData(
535 connection_id_length_
, includes_version_
, sequence_number_length_
);
536 return StringPiece(data() + start_of_fec
, length() - start_of_fec
);
539 StringPiece
QuicPacket::AssociatedData() const {
541 data() + kStartOfHashData
,
542 GetStartOfEncryptedData(
543 connection_id_length_
, includes_version_
, sequence_number_length_
) -
547 StringPiece
QuicPacket::BeforePlaintext() const {
548 return StringPiece(data(), GetStartOfEncryptedData(connection_id_length_
,
550 sequence_number_length_
));
553 StringPiece
QuicPacket::Plaintext() const {
554 const size_t start_of_encrypted_data
=
555 GetStartOfEncryptedData(
556 connection_id_length_
, includes_version_
, sequence_number_length_
);
557 return StringPiece(data() + start_of_encrypted_data
,
558 length() - start_of_encrypted_data
);
561 RetransmittableFrames::RetransmittableFrames(EncryptionLevel level
)
562 : encryption_level_(level
), has_crypto_handshake_(NOT_HANDSHAKE
) {
565 RetransmittableFrames::~RetransmittableFrames() {
566 for (QuicFrames::iterator it
= frames_
.begin(); it
!= frames_
.end(); ++it
) {
569 delete it
->padding_frame
;
572 delete it
->stream_frame
;
575 delete it
->ack_frame
;
577 case STOP_WAITING_FRAME
:
578 delete it
->stop_waiting_frame
;
581 delete it
->ping_frame
;
583 case RST_STREAM_FRAME
:
584 delete it
->rst_stream_frame
;
586 case CONNECTION_CLOSE_FRAME
:
587 delete it
->connection_close_frame
;
590 delete it
->goaway_frame
;
592 case WINDOW_UPDATE_FRAME
:
593 delete it
->window_update_frame
;
596 delete it
->blocked_frame
;
598 case NUM_FRAME_TYPES
:
599 DCHECK(false) << "Cannot delete type: " << it
->type
;
602 STLDeleteElements(&stream_data_
);
605 const QuicFrame
& RetransmittableFrames::AddStreamFrame(
606 QuicStreamFrame
* stream_frame
) {
607 // Make an owned copy of the stream frame's data.
608 stream_data_
.push_back(stream_frame
->GetDataAsString());
609 // Ensure the stream frame's IOVector points to the owned copy of the data.
610 stream_frame
->data
.Clear();
611 stream_frame
->data
.Append(const_cast<char*>(stream_data_
.back()->data()),
612 stream_data_
.back()->size());
613 frames_
.push_back(QuicFrame(stream_frame
));
614 if (stream_frame
->stream_id
== kCryptoStreamId
) {
615 has_crypto_handshake_
= IS_HANDSHAKE
;
617 return frames_
.back();
620 const QuicFrame
& RetransmittableFrames::AddNonStreamFrame(
621 const QuicFrame
& frame
) {
622 DCHECK_NE(frame
.type
, STREAM_FRAME
);
623 frames_
.push_back(frame
);
624 return frames_
.back();
627 void RetransmittableFrames::RemoveFramesForStream(QuicStreamId stream_id
) {
628 QuicFrames::iterator it
= frames_
.begin();
629 while (it
!= frames_
.end()) {
630 if (it
->type
!= STREAM_FRAME
|| it
->stream_frame
->stream_id
!= stream_id
) {
634 delete it
->stream_frame
;
635 it
= frames_
.erase(it
);
639 SerializedPacket::SerializedPacket(
640 QuicPacketSequenceNumber sequence_number
,
641 QuicSequenceNumberLength sequence_number_length
,
642 QuicEncryptedPacket
* packet
,
643 QuicPacketEntropyHash entropy_hash
,
644 RetransmittableFrames
* retransmittable_frames
)
645 : sequence_number(sequence_number
),
646 sequence_number_length(sequence_number_length
),
648 entropy_hash(entropy_hash
),
649 retransmittable_frames(retransmittable_frames
),
650 is_fec_packet(false) {
653 SerializedPacket::~SerializedPacket() {}
655 QuicEncryptedPacket
* QuicEncryptedPacket::Clone() const {
656 char* buffer
= new char[this->length()];
657 memcpy(buffer
, this->data(), this->length());
658 return new QuicEncryptedPacket(buffer
, this->length(), true);
661 ostream
& operator<<(ostream
& os
, const QuicEncryptedPacket
& s
) {
662 os
<< s
.length() << "-byte data";
666 TransmissionInfo::TransmissionInfo()
667 : retransmittable_frames(nullptr),
668 sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER
),
669 sent_time(QuicTime::Zero()),
672 transmission_type(NOT_RETRANSMISSION
),
673 all_transmissions(nullptr),
676 is_fec_packet(false) {
679 TransmissionInfo::TransmissionInfo(
680 RetransmittableFrames
* retransmittable_frames
,
681 QuicSequenceNumberLength sequence_number_length
,
682 TransmissionType transmission_type
,
684 QuicByteCount bytes_sent
,
686 : retransmittable_frames(retransmittable_frames
),
687 sequence_number_length(sequence_number_length
),
688 sent_time(sent_time
),
689 bytes_sent(bytes_sent
),
691 transmission_type(transmission_type
),
692 all_transmissions(nullptr),
695 is_fec_packet(is_fec_packet
) {