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 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()
259 error_code(QUIC_STREAM_NO_ERROR
) {
262 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id
,
263 QuicRstStreamErrorCode error_code
,
264 QuicStreamOffset bytes_written
)
265 : stream_id(stream_id
),
266 error_code(error_code
),
267 byte_offset(bytes_written
) {
268 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
271 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
272 : error_code(QUIC_NO_ERROR
) {
275 QuicFrame::QuicFrame() {}
277 QuicFrame::QuicFrame(QuicPaddingFrame
* padding_frame
)
278 : type(PADDING_FRAME
),
279 padding_frame(padding_frame
) {
282 QuicFrame::QuicFrame(QuicStreamFrame
* stream_frame
)
283 : type(STREAM_FRAME
),
284 stream_frame(stream_frame
) {
287 QuicFrame::QuicFrame(QuicAckFrame
* frame
)
292 QuicFrame::QuicFrame(QuicStopWaitingFrame
* frame
)
293 : type(STOP_WAITING_FRAME
),
294 stop_waiting_frame(frame
) {
297 QuicFrame::QuicFrame(QuicPingFrame
* frame
)
302 QuicFrame::QuicFrame(QuicRstStreamFrame
* frame
)
303 : type(RST_STREAM_FRAME
),
304 rst_stream_frame(frame
) {
307 QuicFrame::QuicFrame(QuicConnectionCloseFrame
* frame
)
308 : type(CONNECTION_CLOSE_FRAME
),
309 connection_close_frame(frame
) {
312 QuicFrame::QuicFrame(QuicGoAwayFrame
* frame
)
313 : type(GOAWAY_FRAME
),
314 goaway_frame(frame
) {
317 QuicFrame::QuicFrame(QuicWindowUpdateFrame
* frame
)
318 : type(WINDOW_UPDATE_FRAME
),
319 window_update_frame(frame
) {
322 QuicFrame::QuicFrame(QuicBlockedFrame
* frame
)
323 : type(BLOCKED_FRAME
),
324 blocked_frame(frame
) {
327 QuicFecData::QuicFecData() : fec_group(0) {}
329 ostream
& operator<<(ostream
& os
, const QuicStopWaitingFrame
& sent_info
) {
330 os
<< "entropy_hash: " << static_cast<int>(sent_info
.entropy_hash
)
331 << " least_unacked: " << sent_info
.least_unacked
;
335 ostream
& operator<<(ostream
& os
, const QuicAckFrame
& ack_frame
) {
336 os
<< "entropy_hash: " << static_cast<int>(ack_frame
.entropy_hash
)
337 << " largest_observed: " << ack_frame
.largest_observed
338 << " delta_time_largest_observed: "
339 << ack_frame
.delta_time_largest_observed
.ToMicroseconds()
340 << " missing_packets: [ ";
341 for (SequenceNumberSet::const_iterator it
= ack_frame
.missing_packets
.begin();
342 it
!= ack_frame
.missing_packets
.end(); ++it
) {
345 os
<< " ] is_truncated: " << ack_frame
.is_truncated
;
346 os
<< " revived_packets: [ ";
347 for (SequenceNumberSet::const_iterator it
= ack_frame
.revived_packets
.begin();
348 it
!= ack_frame
.revived_packets
.end(); ++it
) {
351 os
<< " ] received_packets: [ ";
352 for (PacketTimeList::const_iterator it
=
353 ack_frame
.received_packet_times
.begin();
354 it
!= ack_frame
.received_packet_times
.end(); ++it
) {
355 os
<< it
->first
<< " at " << it
->second
.ToDebuggingValue() << " ";
361 ostream
& operator<<(ostream
& os
, const QuicFrame
& frame
) {
362 switch (frame
.type
) {
363 case PADDING_FRAME
: {
364 os
<< "type { PADDING_FRAME } ";
367 case RST_STREAM_FRAME
: {
368 os
<< "type { RST_STREAM_FRAME } " << *(frame
.rst_stream_frame
);
371 case CONNECTION_CLOSE_FRAME
: {
372 os
<< "type { CONNECTION_CLOSE_FRAME } "
373 << *(frame
.connection_close_frame
);
377 os
<< "type { GOAWAY_FRAME } " << *(frame
.goaway_frame
);
380 case WINDOW_UPDATE_FRAME
: {
381 os
<< "type { WINDOW_UPDATE_FRAME } " << *(frame
.window_update_frame
);
384 case BLOCKED_FRAME
: {
385 os
<< "type { BLOCKED_FRAME } " << *(frame
.blocked_frame
);
389 os
<< "type { STREAM_FRAME } " << *(frame
.stream_frame
);
393 os
<< "type { ACK_FRAME } " << *(frame
.ack_frame
);
396 case STOP_WAITING_FRAME
: {
397 os
<< "type { STOP_WAITING_FRAME } " << *(frame
.stop_waiting_frame
);
401 os
<< "type { PING_FRAME } ";
405 LOG(ERROR
) << "Unknown frame type: " << frame
.type
;
412 ostream
& operator<<(ostream
& os
, const QuicRstStreamFrame
& rst_frame
) {
413 os
<< "stream_id { " << rst_frame
.stream_id
<< " } "
414 << "error_code { " << rst_frame
.error_code
<< " } "
415 << "error_details { " << rst_frame
.error_details
<< " }\n";
419 ostream
& operator<<(ostream
& os
,
420 const QuicConnectionCloseFrame
& connection_close_frame
) {
421 os
<< "error_code { " << connection_close_frame
.error_code
<< " } "
422 << "error_details { " << connection_close_frame
.error_details
<< " }\n";
426 ostream
& operator<<(ostream
& os
, const QuicGoAwayFrame
& goaway_frame
) {
427 os
<< "error_code { " << goaway_frame
.error_code
<< " } "
428 << "last_good_stream_id { " << goaway_frame
.last_good_stream_id
<< " } "
429 << "reason_phrase { " << goaway_frame
.reason_phrase
<< " }\n";
433 ostream
& operator<<(ostream
& os
,
434 const QuicWindowUpdateFrame
& window_update_frame
) {
435 os
<< "stream_id { " << window_update_frame
.stream_id
<< " } "
436 << "byte_offset { " << window_update_frame
.byte_offset
<< " }\n";
440 ostream
& operator<<(ostream
& os
, const QuicBlockedFrame
& blocked_frame
) {
441 os
<< "stream_id { " << blocked_frame
.stream_id
<< " }\n";
445 ostream
& operator<<(ostream
& os
, const QuicStreamFrame
& stream_frame
) {
446 os
<< "stream_id { " << stream_frame
.stream_id
<< " } "
447 << "fin { " << stream_frame
.fin
<< " } "
448 << "offset { " << stream_frame
.offset
<< " } "
449 << "data { " << QuicUtils::StringToHexASCIIDump(stream_frame
.data
)
454 QuicGoAwayFrame::QuicGoAwayFrame()
455 : error_code(QUIC_NO_ERROR
),
456 last_good_stream_id(0) {
459 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code
,
460 QuicStreamId last_good_stream_id
,
461 const string
& reason
)
462 : error_code(error_code
),
463 last_good_stream_id(last_good_stream_id
),
464 reason_phrase(reason
) {
465 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
468 QuicData::QuicData(const char* buffer
,
472 owns_buffer_(false) {
475 QuicData::QuicData(char* buffer
,
480 owns_buffer_(owns_buffer
) {
483 QuicData::~QuicData() {
485 delete [] const_cast<char*>(buffer_
);
489 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id
,
490 QuicStreamOffset byte_offset
)
491 : stream_id(stream_id
),
492 byte_offset(byte_offset
) {}
494 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id
)
495 : stream_id(stream_id
) {}
497 QuicPacket::QuicPacket(char* buffer
,
500 QuicConnectionIdLength connection_id_length
,
501 bool includes_version
,
502 QuicSequenceNumberLength sequence_number_length
)
503 : QuicData(buffer
, length
, owns_buffer
),
505 connection_id_length_(connection_id_length
),
506 includes_version_(includes_version
),
507 sequence_number_length_(sequence_number_length
) {
510 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer
,
512 : QuicData(buffer
, length
) {
515 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer
,
518 : QuicData(buffer
, length
, owns_buffer
) {
521 StringPiece
QuicPacket::FecProtectedData() const {
522 const size_t start_of_fec
= GetStartOfFecProtectedData(
523 connection_id_length_
, includes_version_
, sequence_number_length_
);
524 return StringPiece(data() + start_of_fec
, length() - start_of_fec
);
527 StringPiece
QuicPacket::AssociatedData() const {
529 data() + kStartOfHashData
,
530 GetStartOfEncryptedData(
531 connection_id_length_
, includes_version_
, sequence_number_length_
) -
535 StringPiece
QuicPacket::BeforePlaintext() const {
536 return StringPiece(data(), GetStartOfEncryptedData(connection_id_length_
,
538 sequence_number_length_
));
541 StringPiece
QuicPacket::Plaintext() const {
542 const size_t start_of_encrypted_data
=
543 GetStartOfEncryptedData(
544 connection_id_length_
, includes_version_
, sequence_number_length_
);
545 return StringPiece(data() + start_of_encrypted_data
,
546 length() - start_of_encrypted_data
);
549 RetransmittableFrames::RetransmittableFrames(EncryptionLevel level
)
550 : encryption_level_(level
), has_crypto_handshake_(NOT_HANDSHAKE
) {
553 RetransmittableFrames::~RetransmittableFrames() {
554 for (QuicFrames::iterator it
= frames_
.begin(); it
!= frames_
.end(); ++it
) {
557 delete it
->padding_frame
;
560 delete it
->stream_frame
;
563 delete it
->ack_frame
;
565 case STOP_WAITING_FRAME
:
566 delete it
->stop_waiting_frame
;
569 delete it
->ping_frame
;
571 case RST_STREAM_FRAME
:
572 delete it
->rst_stream_frame
;
574 case CONNECTION_CLOSE_FRAME
:
575 delete it
->connection_close_frame
;
578 delete it
->goaway_frame
;
580 case WINDOW_UPDATE_FRAME
:
581 delete it
->window_update_frame
;
584 delete it
->blocked_frame
;
586 case NUM_FRAME_TYPES
:
587 DCHECK(false) << "Cannot delete type: " << it
->type
;
590 for (const char* buffer
: stream_data_
) {
595 const QuicFrame
& RetransmittableFrames::AddFrame(const QuicFrame
& frame
) {
596 return AddFrame(frame
, nullptr);
599 const QuicFrame
& RetransmittableFrames::AddFrame(const QuicFrame
& frame
,
601 if (frame
.type
== STREAM_FRAME
&&
602 frame
.stream_frame
->stream_id
== kCryptoStreamId
) {
603 has_crypto_handshake_
= IS_HANDSHAKE
;
605 if (buffer
!= nullptr) {
606 stream_data_
.push_back(buffer
);
608 frames_
.push_back(frame
);
609 return frames_
.back();
612 void RetransmittableFrames::RemoveFramesForStream(QuicStreamId stream_id
) {
613 QuicFrames::iterator it
= frames_
.begin();
614 while (it
!= frames_
.end()) {
615 if (it
->type
!= STREAM_FRAME
|| it
->stream_frame
->stream_id
!= stream_id
) {
619 delete it
->stream_frame
;
620 it
= frames_
.erase(it
);
624 SerializedPacket::SerializedPacket(
625 QuicPacketSequenceNumber sequence_number
,
626 QuicSequenceNumberLength sequence_number_length
,
627 QuicEncryptedPacket
* packet
,
628 QuicPacketEntropyHash entropy_hash
,
629 RetransmittableFrames
* retransmittable_frames
)
630 : sequence_number(sequence_number
),
631 sequence_number_length(sequence_number_length
),
633 entropy_hash(entropy_hash
),
634 retransmittable_frames(retransmittable_frames
),
635 is_fec_packet(false) {
638 SerializedPacket::~SerializedPacket() {}
640 QuicEncryptedPacket
* QuicEncryptedPacket::Clone() const {
641 char* buffer
= new char[this->length()];
642 memcpy(buffer
, this->data(), this->length());
643 return new QuicEncryptedPacket(buffer
, this->length(), true);
646 ostream
& operator<<(ostream
& os
, const QuicEncryptedPacket
& s
) {
647 os
<< s
.length() << "-byte data";
651 TransmissionInfo::TransmissionInfo()
652 : retransmittable_frames(nullptr),
653 sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER
),
654 sent_time(QuicTime::Zero()),
657 transmission_type(NOT_RETRANSMISSION
),
658 all_transmissions(nullptr),
661 is_fec_packet(false) {
664 TransmissionInfo::TransmissionInfo(
665 RetransmittableFrames
* retransmittable_frames
,
666 QuicSequenceNumberLength sequence_number_length
,
667 TransmissionType transmission_type
,
669 QuicByteCount bytes_sent
,
671 : retransmittable_frames(retransmittable_frames
),
672 sequence_number_length(sequence_number_length
),
673 sent_time(sent_time
),
674 bytes_sent(bytes_sent
),
676 transmission_type(transmission_type
),
677 all_transmissions(nullptr),
680 is_fec_packet(is_fec_packet
) {