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
.packet_number_length
,
22 header
.is_in_fec_group
);
25 size_t GetPacketHeaderSize(QuicConnectionIdLength connection_id_length
,
27 QuicPacketNumberLength packet_number_length
,
28 InFecGroup is_in_fec_group
) {
29 return kPublicFlagsSize
+ connection_id_length
+
30 (include_version
? kQuicVersionSize
: 0) + packet_number_length
+
32 (is_in_fec_group
== IN_FEC_GROUP
? kFecGroupSize
: 0);
35 size_t GetStartOfFecProtectedData(QuicConnectionIdLength connection_id_length
,
37 QuicPacketNumberLength packet_number_length
) {
38 return GetPacketHeaderSize(connection_id_length
, include_version
,
39 packet_number_length
, IN_FEC_GROUP
);
42 size_t GetStartOfEncryptedData(QuicConnectionIdLength connection_id_length
,
44 QuicPacketNumberLength packet_number_length
) {
45 // Don't include the fec size, since encryption starts before private flags.
46 return GetPacketHeaderSize(connection_id_length
, include_version
,
47 packet_number_length
, NOT_IN_FEC_GROUP
) -
51 QuicPacketPublicHeader::QuicPacketPublicHeader()
53 connection_id_length(PACKET_8BYTE_CONNECTION_ID
),
56 packet_number_length(PACKET_6BYTE_PACKET_NUMBER
) {}
58 QuicPacketPublicHeader::QuicPacketPublicHeader(
59 const QuicPacketPublicHeader
& other
)
60 : connection_id(other
.connection_id
),
61 connection_id_length(other
.connection_id_length
),
62 reset_flag(other
.reset_flag
),
63 version_flag(other
.version_flag
),
64 packet_number_length(other
.packet_number_length
),
65 versions(other
.versions
) {}
67 QuicPacketPublicHeader::~QuicPacketPublicHeader() {}
69 QuicPacketHeader::QuicPacketHeader()
70 : packet_packet_number(0),
74 is_in_fec_group(NOT_IN_FEC_GROUP
),
77 QuicPacketHeader::QuicPacketHeader(const QuicPacketPublicHeader
& header
)
78 : public_header(header
),
79 packet_packet_number(0),
83 is_in_fec_group(NOT_IN_FEC_GROUP
),
86 QuicPublicResetPacket::QuicPublicResetPacket()
87 : nonce_proof(0), rejected_packet_number(0) {}
89 QuicPublicResetPacket::QuicPublicResetPacket(
90 const QuicPacketPublicHeader
& header
)
91 : public_header(header
), nonce_proof(0), rejected_packet_number(0) {}
93 QuicStreamFrame::QuicStreamFrame() : stream_id(0), fin(false), offset(0) {
96 QuicStreamFrame::QuicStreamFrame(const QuicStreamFrame
& frame
)
97 : stream_id(frame
.stream_id
),
103 QuicStreamFrame::QuicStreamFrame(QuicStreamId stream_id
,
105 QuicStreamOffset offset
,
107 : stream_id(stream_id
), fin(fin
), offset(offset
), data(data
) {
110 uint32
MakeQuicTag(char a
, char b
, char c
, char d
) {
111 return static_cast<uint32
>(a
) |
112 static_cast<uint32
>(b
) << 8 |
113 static_cast<uint32
>(c
) << 16 |
114 static_cast<uint32
>(d
) << 24;
117 bool ContainsQuicTag(const QuicTagVector
& tag_vector
, QuicTag tag
) {
118 return std::find(tag_vector
.begin(), tag_vector
.end(), tag
)
122 QuicVersionVector
QuicSupportedVersions() {
123 QuicVersionVector supported_versions
;
124 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
125 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
127 return supported_versions
;
130 QuicTag
QuicVersionToQuicTag(const QuicVersion version
) {
132 case QUIC_VERSION_24
:
133 return MakeQuicTag('Q', '0', '2', '4');
134 case QUIC_VERSION_25
:
135 return MakeQuicTag('Q', '0', '2', '5');
137 // This shold be an ERROR because we should never attempt to convert an
138 // invalid QuicVersion to be written to the wire.
139 LOG(ERROR
) << "Unsupported QuicVersion: " << version
;
144 QuicVersion
QuicTagToQuicVersion(const QuicTag version_tag
) {
145 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
146 if (version_tag
== QuicVersionToQuicTag(kSupportedQuicVersions
[i
])) {
147 return kSupportedQuicVersions
[i
];
150 // Reading from the client so this should not be considered an ERROR.
151 DVLOG(1) << "Unsupported QuicTag version: "
152 << QuicUtils::TagToString(version_tag
);
153 return QUIC_VERSION_UNSUPPORTED
;
156 #define RETURN_STRING_LITERAL(x) \
160 string
QuicVersionToString(const QuicVersion version
) {
162 RETURN_STRING_LITERAL(QUIC_VERSION_24
);
163 RETURN_STRING_LITERAL(QUIC_VERSION_25
);
165 return "QUIC_VERSION_UNSUPPORTED";
169 string
QuicVersionVectorToString(const QuicVersionVector
& versions
) {
171 for (size_t i
= 0; i
< versions
.size(); ++i
) {
175 result
.append(QuicVersionToString(versions
[i
]));
180 ostream
& operator<<(ostream
& os
, const Perspective
& s
) {
181 if (s
== Perspective::IS_SERVER
) {
189 ostream
& operator<<(ostream
& os
, const QuicPacketHeader
& header
) {
190 os
<< "{ connection_id: " << header
.public_header
.connection_id
191 << ", connection_id_length:" << header
.public_header
.connection_id_length
192 << ", packet_number_length:" << header
.public_header
.packet_number_length
193 << ", reset_flag: " << header
.public_header
.reset_flag
194 << ", version_flag: " << header
.public_header
.version_flag
;
195 if (header
.public_header
.version_flag
) {
197 for (size_t i
= 0; i
< header
.public_header
.versions
.size(); ++i
) {
198 os
<< header
.public_header
.versions
[i
] << " ";
201 os
<< ", fec_flag: " << header
.fec_flag
202 << ", entropy_flag: " << header
.entropy_flag
203 << ", entropy hash: " << static_cast<int>(header
.entropy_hash
)
204 << ", packet_number: " << header
.packet_packet_number
205 << ", is_in_fec_group:" << header
.is_in_fec_group
206 << ", fec_group: " << header
.fec_group
<< "}\n";
210 bool IsAwaitingPacket(const QuicAckFrame
& ack_frame
,
211 QuicPacketNumber packet_number
) {
212 return packet_number
> ack_frame
.largest_observed
||
213 ContainsKey(ack_frame
.missing_packets
, packet_number
);
216 void InsertMissingPacketsBetween(QuicAckFrame
* ack_frame
,
217 QuicPacketNumber lower
,
218 QuicPacketNumber higher
) {
219 for (QuicPacketNumber i
= lower
; i
< higher
; ++i
) {
220 ack_frame
->missing_packets
.insert(i
);
224 QuicStopWaitingFrame::QuicStopWaitingFrame()
229 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
231 QuicAckFrame::QuicAckFrame()
234 delta_time_largest_observed(QuicTime::Delta::Infinite()),
235 is_truncated(false) {}
237 QuicAckFrame::~QuicAckFrame() {}
239 QuicRstStreamErrorCode
AdjustErrorForVersion(
240 QuicRstStreamErrorCode error_code
,
241 QuicVersion version
) {
245 QuicRstStreamFrame::QuicRstStreamFrame()
246 : stream_id(0), error_code(QUIC_STREAM_NO_ERROR
), byte_offset(0) {}
248 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id
,
249 QuicRstStreamErrorCode error_code
,
250 QuicStreamOffset bytes_written
)
251 : stream_id(stream_id
),
252 error_code(error_code
),
253 byte_offset(bytes_written
) {
254 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
257 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
258 : error_code(QUIC_NO_ERROR
) {
261 QuicFrame::QuicFrame() {}
263 QuicFrame::QuicFrame(QuicPaddingFrame
* padding_frame
)
264 : type(PADDING_FRAME
),
265 padding_frame(padding_frame
) {
268 QuicFrame::QuicFrame(QuicStreamFrame
* stream_frame
)
269 : type(STREAM_FRAME
),
270 stream_frame(stream_frame
) {
273 QuicFrame::QuicFrame(QuicAckFrame
* frame
)
278 QuicFrame::QuicFrame(QuicMtuDiscoveryFrame
* frame
)
279 : type(MTU_DISCOVERY_FRAME
), mtu_discovery_frame(frame
) {
282 QuicFrame::QuicFrame(QuicStopWaitingFrame
* frame
)
283 : type(STOP_WAITING_FRAME
),
284 stop_waiting_frame(frame
) {
287 QuicFrame::QuicFrame(QuicPingFrame
* frame
)
292 QuicFrame::QuicFrame(QuicRstStreamFrame
* frame
)
293 : type(RST_STREAM_FRAME
),
294 rst_stream_frame(frame
) {
297 QuicFrame::QuicFrame(QuicConnectionCloseFrame
* frame
)
298 : type(CONNECTION_CLOSE_FRAME
),
299 connection_close_frame(frame
) {
302 QuicFrame::QuicFrame(QuicGoAwayFrame
* frame
)
303 : type(GOAWAY_FRAME
),
304 goaway_frame(frame
) {
307 QuicFrame::QuicFrame(QuicWindowUpdateFrame
* frame
)
308 : type(WINDOW_UPDATE_FRAME
),
309 window_update_frame(frame
) {
312 QuicFrame::QuicFrame(QuicBlockedFrame
* frame
)
313 : type(BLOCKED_FRAME
),
314 blocked_frame(frame
) {
317 QuicFecData::QuicFecData() : fec_group(0) {}
319 ostream
& operator<<(ostream
& os
, const QuicStopWaitingFrame
& sent_info
) {
320 os
<< "entropy_hash: " << static_cast<int>(sent_info
.entropy_hash
)
321 << " least_unacked: " << sent_info
.least_unacked
;
325 ostream
& operator<<(ostream
& os
, const QuicAckFrame
& ack_frame
) {
326 os
<< "entropy_hash: " << static_cast<int>(ack_frame
.entropy_hash
)
327 << " largest_observed: " << ack_frame
.largest_observed
328 << " delta_time_largest_observed: "
329 << ack_frame
.delta_time_largest_observed
.ToMicroseconds()
330 << " missing_packets: [ ";
331 for (PacketNumberSet::const_iterator it
= ack_frame
.missing_packets
.begin();
332 it
!= ack_frame
.missing_packets
.end(); ++it
) {
335 os
<< " ] is_truncated: " << ack_frame
.is_truncated
;
336 os
<< " revived_packets: [ ";
337 for (PacketNumberSet::const_iterator it
= ack_frame
.revived_packets
.begin();
338 it
!= ack_frame
.revived_packets
.end(); ++it
) {
341 os
<< " ] received_packets: [ ";
342 for (PacketTimeList::const_iterator it
=
343 ack_frame
.received_packet_times
.begin();
344 it
!= ack_frame
.received_packet_times
.end(); ++it
) {
345 os
<< it
->first
<< " at " << it
->second
.ToDebuggingValue() << " ";
351 ostream
& operator<<(ostream
& os
, const QuicFrame
& frame
) {
352 switch (frame
.type
) {
353 case PADDING_FRAME
: {
354 os
<< "type { PADDING_FRAME } ";
357 case RST_STREAM_FRAME
: {
358 os
<< "type { RST_STREAM_FRAME } " << *(frame
.rst_stream_frame
);
361 case CONNECTION_CLOSE_FRAME
: {
362 os
<< "type { CONNECTION_CLOSE_FRAME } "
363 << *(frame
.connection_close_frame
);
367 os
<< "type { GOAWAY_FRAME } " << *(frame
.goaway_frame
);
370 case WINDOW_UPDATE_FRAME
: {
371 os
<< "type { WINDOW_UPDATE_FRAME } " << *(frame
.window_update_frame
);
374 case BLOCKED_FRAME
: {
375 os
<< "type { BLOCKED_FRAME } " << *(frame
.blocked_frame
);
379 os
<< "type { STREAM_FRAME } " << *(frame
.stream_frame
);
383 os
<< "type { ACK_FRAME } " << *(frame
.ack_frame
);
386 case STOP_WAITING_FRAME
: {
387 os
<< "type { STOP_WAITING_FRAME } " << *(frame
.stop_waiting_frame
);
391 os
<< "type { PING_FRAME } ";
394 case MTU_DISCOVERY_FRAME
: {
395 os
<< "type { MTU_DISCOVERY_FRAME } ";
399 LOG(ERROR
) << "Unknown frame type: " << frame
.type
;
406 ostream
& operator<<(ostream
& os
, const QuicRstStreamFrame
& rst_frame
) {
407 os
<< "stream_id { " << rst_frame
.stream_id
<< " } "
408 << "error_code { " << rst_frame
.error_code
<< " } "
409 << "error_details { " << rst_frame
.error_details
<< " }\n";
413 ostream
& operator<<(ostream
& os
,
414 const QuicConnectionCloseFrame
& connection_close_frame
) {
415 os
<< "error_code { " << connection_close_frame
.error_code
<< " } "
416 << "error_details { " << connection_close_frame
.error_details
<< " }\n";
420 ostream
& operator<<(ostream
& os
, const QuicGoAwayFrame
& goaway_frame
) {
421 os
<< "error_code { " << goaway_frame
.error_code
<< " } "
422 << "last_good_stream_id { " << goaway_frame
.last_good_stream_id
<< " } "
423 << "reason_phrase { " << goaway_frame
.reason_phrase
<< " }\n";
427 ostream
& operator<<(ostream
& os
,
428 const QuicWindowUpdateFrame
& window_update_frame
) {
429 os
<< "stream_id { " << window_update_frame
.stream_id
<< " } "
430 << "byte_offset { " << window_update_frame
.byte_offset
<< " }\n";
434 ostream
& operator<<(ostream
& os
, const QuicBlockedFrame
& blocked_frame
) {
435 os
<< "stream_id { " << blocked_frame
.stream_id
<< " }\n";
439 ostream
& operator<<(ostream
& os
, const QuicStreamFrame
& stream_frame
) {
440 os
<< "stream_id { " << stream_frame
.stream_id
<< " } "
441 << "fin { " << stream_frame
.fin
<< " } "
442 << "offset { " << stream_frame
.offset
<< " } "
443 << "data { " << QuicUtils::StringToHexASCIIDump(stream_frame
.data
)
448 QuicGoAwayFrame::QuicGoAwayFrame()
449 : error_code(QUIC_NO_ERROR
),
450 last_good_stream_id(0) {
453 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code
,
454 QuicStreamId last_good_stream_id
,
455 const string
& reason
)
456 : error_code(error_code
),
457 last_good_stream_id(last_good_stream_id
),
458 reason_phrase(reason
) {
459 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
462 QuicData::QuicData(const char* buffer
,
466 owns_buffer_(false) {
469 QuicData::QuicData(char* buffer
,
474 owns_buffer_(owns_buffer
) {
477 QuicData::~QuicData() {
479 delete [] const_cast<char*>(buffer_
);
483 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id
,
484 QuicStreamOffset byte_offset
)
485 : stream_id(stream_id
),
486 byte_offset(byte_offset
) {}
488 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id
)
489 : stream_id(stream_id
) {}
491 QuicPacket::QuicPacket(char* buffer
,
494 QuicConnectionIdLength connection_id_length
,
495 bool includes_version
,
496 QuicPacketNumberLength packet_number_length
)
497 : QuicData(buffer
, length
, owns_buffer
),
499 connection_id_length_(connection_id_length
),
500 includes_version_(includes_version
),
501 packet_number_length_(packet_number_length
) {}
503 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer
,
505 : QuicData(buffer
, length
) {
508 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer
,
511 : QuicData(buffer
, length
, owns_buffer
) {
514 StringPiece
QuicPacket::FecProtectedData() const {
515 const size_t start_of_fec
= GetStartOfFecProtectedData(
516 connection_id_length_
, includes_version_
, packet_number_length_
);
517 return StringPiece(data() + start_of_fec
, length() - start_of_fec
);
520 StringPiece
QuicPacket::AssociatedData() const {
522 data() + kStartOfHashData
,
523 GetStartOfEncryptedData(connection_id_length_
, includes_version_
,
524 packet_number_length_
) -
528 StringPiece
QuicPacket::BeforePlaintext() const {
530 data(), GetStartOfEncryptedData(connection_id_length_
, includes_version_
,
531 packet_number_length_
));
534 StringPiece
QuicPacket::Plaintext() const {
535 const size_t start_of_encrypted_data
= GetStartOfEncryptedData(
536 connection_id_length_
, includes_version_
, packet_number_length_
);
537 return StringPiece(data() + start_of_encrypted_data
,
538 length() - start_of_encrypted_data
);
541 RetransmittableFrames::RetransmittableFrames(EncryptionLevel level
)
542 : encryption_level_(level
),
543 has_crypto_handshake_(NOT_HANDSHAKE
),
544 needs_padding_(false) {
547 RetransmittableFrames::~RetransmittableFrames() {
548 for (QuicFrames::iterator it
= frames_
.begin(); it
!= frames_
.end(); ++it
) {
551 delete it
->padding_frame
;
554 delete it
->stream_frame
;
557 delete it
->ack_frame
;
559 case MTU_DISCOVERY_FRAME
:
560 delete it
->mtu_discovery_frame
;
562 case STOP_WAITING_FRAME
:
563 delete it
->stop_waiting_frame
;
566 delete it
->ping_frame
;
568 case RST_STREAM_FRAME
:
569 delete it
->rst_stream_frame
;
571 case CONNECTION_CLOSE_FRAME
:
572 delete it
->connection_close_frame
;
575 delete it
->goaway_frame
;
577 case WINDOW_UPDATE_FRAME
:
578 delete it
->window_update_frame
;
581 delete it
->blocked_frame
;
583 case NUM_FRAME_TYPES
:
584 DCHECK(false) << "Cannot delete type: " << it
->type
;
587 for (const char* buffer
: stream_data_
) {
592 const QuicFrame
& RetransmittableFrames::AddFrame(const QuicFrame
& frame
) {
593 return AddFrame(frame
, nullptr);
596 const QuicFrame
& RetransmittableFrames::AddFrame(const QuicFrame
& frame
,
598 if (frame
.type
== STREAM_FRAME
&&
599 frame
.stream_frame
->stream_id
== kCryptoStreamId
) {
600 has_crypto_handshake_
= IS_HANDSHAKE
;
602 if (buffer
!= nullptr) {
603 stream_data_
.push_back(buffer
);
605 frames_
.push_back(frame
);
606 return frames_
.back();
609 void RetransmittableFrames::RemoveFramesForStream(QuicStreamId stream_id
) {
610 QuicFrames::iterator it
= frames_
.begin();
611 while (it
!= frames_
.end()) {
612 if (it
->type
!= STREAM_FRAME
|| it
->stream_frame
->stream_id
!= stream_id
) {
616 delete it
->stream_frame
;
617 it
= frames_
.erase(it
);
621 SerializedPacket::SerializedPacket(
622 QuicPacketNumber packet_number
,
623 QuicPacketNumberLength packet_number_length
,
624 QuicEncryptedPacket
* packet
,
625 QuicPacketEntropyHash entropy_hash
,
626 RetransmittableFrames
* retransmittable_frames
,
628 bool has_stop_waiting
)
630 retransmittable_frames(retransmittable_frames
),
631 packet_number(packet_number
),
632 packet_number_length(packet_number_length
),
633 entropy_hash(entropy_hash
),
634 is_fec_packet(false),
636 has_stop_waiting(has_stop_waiting
) {}
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 packet_number_length(PACKET_1BYTE_PACKET_NUMBER
),
654 sent_time(QuicTime::Zero()),
657 transmission_type(NOT_RETRANSMISSION
),
658 all_transmissions(nullptr),
661 is_fec_packet(false) {}
663 TransmissionInfo::TransmissionInfo(
664 RetransmittableFrames
* retransmittable_frames
,
665 QuicPacketNumberLength packet_number_length
,
666 TransmissionType transmission_type
,
668 QuicByteCount bytes_sent
,
670 : retransmittable_frames(retransmittable_frames
),
671 packet_number_length(packet_number_length
),
672 sent_time(sent_time
),
673 bytes_sent(bytes_sent
),
675 transmission_type(transmission_type
),
676 all_transmissions(nullptr),
679 is_fec_packet(is_fec_packet
) {}