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_flags.h"
9 #include "net/quic/quic_utils.h"
11 using base::StringPiece
;
13 using std::numeric_limits
;
19 size_t GetPacketHeaderSize(const QuicPacketHeader
& header
) {
20 return GetPacketHeaderSize(header
.public_header
.connection_id_length
,
21 header
.public_header
.version_flag
,
22 header
.public_header
.packet_number_length
,
23 header
.is_in_fec_group
);
26 size_t GetPacketHeaderSize(QuicConnectionIdLength connection_id_length
,
28 QuicPacketNumberLength packet_number_length
,
29 InFecGroup is_in_fec_group
) {
30 return kPublicFlagsSize
+ connection_id_length
+
31 (include_version
? kQuicVersionSize
: 0) + packet_number_length
+
33 (is_in_fec_group
== IN_FEC_GROUP
? kFecGroupSize
: 0);
36 size_t GetStartOfFecProtectedData(QuicConnectionIdLength connection_id_length
,
38 QuicPacketNumberLength packet_number_length
) {
39 return GetPacketHeaderSize(connection_id_length
, include_version
,
40 packet_number_length
, IN_FEC_GROUP
);
43 size_t GetStartOfEncryptedData(QuicConnectionIdLength connection_id_length
,
45 QuicPacketNumberLength packet_number_length
) {
46 // Don't include the fec size, since encryption starts before private flags.
47 return GetPacketHeaderSize(connection_id_length
, include_version
,
48 packet_number_length
, NOT_IN_FEC_GROUP
) -
52 QuicPacketPublicHeader::QuicPacketPublicHeader()
54 connection_id_length(PACKET_8BYTE_CONNECTION_ID
),
57 packet_number_length(PACKET_6BYTE_PACKET_NUMBER
) {}
59 QuicPacketPublicHeader::QuicPacketPublicHeader(
60 const QuicPacketPublicHeader
& other
)
61 : connection_id(other
.connection_id
),
62 connection_id_length(other
.connection_id_length
),
63 reset_flag(other
.reset_flag
),
64 version_flag(other
.version_flag
),
65 packet_number_length(other
.packet_number_length
),
66 versions(other
.versions
) {}
68 QuicPacketPublicHeader::~QuicPacketPublicHeader() {}
70 QuicPacketHeader::QuicPacketHeader()
71 : packet_packet_number(0),
75 is_in_fec_group(NOT_IN_FEC_GROUP
),
78 QuicPacketHeader::QuicPacketHeader(const QuicPacketPublicHeader
& header
)
79 : public_header(header
),
80 packet_packet_number(0),
84 is_in_fec_group(NOT_IN_FEC_GROUP
),
87 QuicPublicResetPacket::QuicPublicResetPacket()
88 : nonce_proof(0), rejected_packet_number(0) {}
90 QuicPublicResetPacket::QuicPublicResetPacket(
91 const QuicPacketPublicHeader
& header
)
92 : public_header(header
), nonce_proof(0), rejected_packet_number(0) {}
94 QuicStreamFrame::QuicStreamFrame() : stream_id(0), fin(false), offset(0) {
97 QuicStreamFrame::QuicStreamFrame(const QuicStreamFrame
& frame
)
98 : stream_id(frame
.stream_id
),
100 offset(frame
.offset
),
104 QuicStreamFrame::QuicStreamFrame(QuicStreamId stream_id
,
106 QuicStreamOffset offset
,
108 : stream_id(stream_id
), fin(fin
), offset(offset
), data(data
) {
111 uint32
MakeQuicTag(char a
, char b
, char c
, char d
) {
112 return static_cast<uint32
>(a
) |
113 static_cast<uint32
>(b
) << 8 |
114 static_cast<uint32
>(c
) << 16 |
115 static_cast<uint32
>(d
) << 24;
118 bool ContainsQuicTag(const QuicTagVector
& tag_vector
, QuicTag tag
) {
119 return std::find(tag_vector
.begin(), tag_vector
.end(), tag
)
123 QuicVersionVector
QuicSupportedVersions() {
124 QuicVersionVector supported_versions
;
125 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
126 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
128 return supported_versions
;
131 QuicTag
QuicVersionToQuicTag(const QuicVersion version
) {
133 case QUIC_VERSION_24
:
134 return MakeQuicTag('Q', '0', '2', '4');
135 case QUIC_VERSION_25
:
136 return MakeQuicTag('Q', '0', '2', '5');
138 // This shold be an ERROR because we should never attempt to convert an
139 // invalid QuicVersion to be written to the wire.
140 LOG(ERROR
) << "Unsupported QuicVersion: " << version
;
145 QuicVersion
QuicTagToQuicVersion(const QuicTag version_tag
) {
146 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
147 if (version_tag
== QuicVersionToQuicTag(kSupportedQuicVersions
[i
])) {
148 return kSupportedQuicVersions
[i
];
151 // Reading from the client so this should not be considered an ERROR.
152 DVLOG(1) << "Unsupported QuicTag version: "
153 << QuicUtils::TagToString(version_tag
);
154 return QUIC_VERSION_UNSUPPORTED
;
157 #define RETURN_STRING_LITERAL(x) \
161 string
QuicVersionToString(const QuicVersion version
) {
163 RETURN_STRING_LITERAL(QUIC_VERSION_24
);
164 RETURN_STRING_LITERAL(QUIC_VERSION_25
);
166 return "QUIC_VERSION_UNSUPPORTED";
170 string
QuicVersionVectorToString(const QuicVersionVector
& versions
) {
172 for (size_t i
= 0; i
< versions
.size(); ++i
) {
176 result
.append(QuicVersionToString(versions
[i
]));
181 ostream
& operator<<(ostream
& os
, const Perspective
& s
) {
182 if (s
== Perspective::IS_SERVER
) {
190 ostream
& operator<<(ostream
& os
, const QuicPacketHeader
& header
) {
191 os
<< "{ connection_id: " << header
.public_header
.connection_id
192 << ", connection_id_length:" << header
.public_header
.connection_id_length
193 << ", packet_number_length:" << header
.public_header
.packet_number_length
194 << ", reset_flag: " << header
.public_header
.reset_flag
195 << ", version_flag: " << header
.public_header
.version_flag
;
196 if (header
.public_header
.version_flag
) {
198 for (size_t i
= 0; i
< header
.public_header
.versions
.size(); ++i
) {
199 os
<< header
.public_header
.versions
[i
] << " ";
202 os
<< ", fec_flag: " << header
.fec_flag
203 << ", entropy_flag: " << header
.entropy_flag
204 << ", entropy hash: " << static_cast<int>(header
.entropy_hash
)
205 << ", packet_number: " << header
.packet_packet_number
206 << ", is_in_fec_group:" << header
.is_in_fec_group
207 << ", fec_group: " << header
.fec_group
<< "}\n";
211 bool IsAwaitingPacket(const QuicAckFrame
& ack_frame
,
212 QuicPacketNumber packet_number
) {
213 return packet_number
> ack_frame
.largest_observed
||
214 ack_frame
.missing_packets
.Contains(packet_number
);
217 QuicStopWaitingFrame::QuicStopWaitingFrame()
222 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
224 QuicAckFrame::QuicAckFrame()
227 delta_time_largest_observed(QuicTime::Delta::Infinite()),
228 is_truncated(false) {}
230 QuicAckFrame::~QuicAckFrame() {}
232 QuicRstStreamErrorCode
AdjustErrorForVersion(
233 QuicRstStreamErrorCode error_code
,
234 QuicVersion version
) {
238 QuicRstStreamFrame::QuicRstStreamFrame()
239 : stream_id(0), error_code(QUIC_STREAM_NO_ERROR
), byte_offset(0) {}
241 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id
,
242 QuicRstStreamErrorCode error_code
,
243 QuicStreamOffset bytes_written
)
244 : stream_id(stream_id
),
245 error_code(error_code
),
246 byte_offset(bytes_written
) {
247 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
250 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
251 : error_code(QUIC_NO_ERROR
) {
254 QuicFrame::QuicFrame() {}
256 QuicFrame::QuicFrame(QuicPaddingFrame
* padding_frame
)
257 : type(PADDING_FRAME
),
258 padding_frame(padding_frame
) {
261 QuicFrame::QuicFrame(QuicStreamFrame
* stream_frame
)
262 : type(STREAM_FRAME
),
263 stream_frame(stream_frame
) {
266 QuicFrame::QuicFrame(QuicAckFrame
* frame
)
271 QuicFrame::QuicFrame(QuicMtuDiscoveryFrame
* frame
)
272 : type(MTU_DISCOVERY_FRAME
), mtu_discovery_frame(frame
) {
275 QuicFrame::QuicFrame(QuicStopWaitingFrame
* frame
)
276 : type(STOP_WAITING_FRAME
),
277 stop_waiting_frame(frame
) {
280 QuicFrame::QuicFrame(QuicPingFrame
* frame
)
285 QuicFrame::QuicFrame(QuicRstStreamFrame
* frame
)
286 : type(RST_STREAM_FRAME
),
287 rst_stream_frame(frame
) {
290 QuicFrame::QuicFrame(QuicConnectionCloseFrame
* frame
)
291 : type(CONNECTION_CLOSE_FRAME
),
292 connection_close_frame(frame
) {
295 QuicFrame::QuicFrame(QuicGoAwayFrame
* frame
)
296 : type(GOAWAY_FRAME
),
297 goaway_frame(frame
) {
300 QuicFrame::QuicFrame(QuicWindowUpdateFrame
* frame
)
301 : type(WINDOW_UPDATE_FRAME
),
302 window_update_frame(frame
) {
305 QuicFrame::QuicFrame(QuicBlockedFrame
* frame
)
306 : type(BLOCKED_FRAME
),
307 blocked_frame(frame
) {
310 QuicFecData::QuicFecData() : fec_group(0) {}
312 ostream
& operator<<(ostream
& os
, const QuicStopWaitingFrame
& sent_info
) {
313 os
<< "entropy_hash: " << static_cast<int>(sent_info
.entropy_hash
)
314 << " least_unacked: " << sent_info
.least_unacked
;
318 PacketNumberQueue::const_iterator::const_iterator(
319 PacketNumberSet::const_iterator set_iter
)
320 : use_interval_set_(false), set_iter_(set_iter
) {}
322 PacketNumberQueue::const_iterator::const_iterator(
323 IntervalSet
<QuicPacketNumber
>::const_iterator interval_set_iter
,
324 QuicPacketNumber first
,
325 QuicPacketNumber last
)
326 : use_interval_set_(true),
327 interval_set_iter_(interval_set_iter
),
331 PacketNumberQueue::const_iterator::const_iterator(const const_iterator
& other
) =
333 // TODO(rtenneti): on windows RValue reference gives errors.
334 // PacketNumberQueue::const_iterator::const_iterator(const_iterator&& other) =
336 PacketNumberQueue::const_iterator::~const_iterator() {}
338 PacketNumberQueue::const_iterator
& PacketNumberQueue::const_iterator::operator=(
339 const const_iterator
& other
) = default;
340 // TODO(rtenneti): on windows RValue reference gives errors.
341 // PacketNumberQueue::const_iterator&
342 // PacketNumberQueue::const_iterator::operator=(
343 // const_iterator&& other) = default;
345 bool PacketNumberQueue::const_iterator::operator!=(
346 const const_iterator
& other
) const {
347 if (use_interval_set_
) {
348 return current_
!= other
.current_
;
350 return set_iter_
!= other
.set_iter_
;
354 bool PacketNumberQueue::const_iterator::operator==(
355 const const_iterator
& other
) const {
356 if (use_interval_set_
) {
357 return current_
== other
.current_
;
359 return set_iter_
== other
.set_iter_
;
363 PacketNumberQueue::const_iterator::value_type
364 PacketNumberQueue::const_iterator::
366 if (use_interval_set_
) {
373 PacketNumberQueue::const_iterator
& PacketNumberQueue::const_iterator::
375 if (use_interval_set_
) {
377 if (current_
< last_
) {
378 if (current_
>= interval_set_iter_
->max()) {
379 ++interval_set_iter_
;
380 current_
= interval_set_iter_
->min();
391 PacketNumberQueue::const_iterator
PacketNumberQueue::const_iterator::operator++(
392 int /* postincrement */) {
393 PacketNumberQueue::const_iterator
preincrement(*this);
398 PacketNumberQueue::PacketNumberQueue()
399 : use_interval_set_(FLAGS_quic_packet_queue_use_interval_set
) {}
401 PacketNumberQueue::PacketNumberQueue(const PacketNumberQueue
& other
) = default;
402 // TODO(rtenneti): on windows RValue reference gives errors.
403 // PacketNumberQueue::PacketNumberQueue(PacketNumberQueue&& other) = default;
404 PacketNumberQueue::~PacketNumberQueue() {}
406 PacketNumberQueue
& PacketNumberQueue::operator=(
407 const PacketNumberQueue
& other
) = default;
408 // TODO(rtenneti): on windows RValue reference gives errors.
409 // PacketNumberQueue& PacketNumberQueue::operator=(PacketNumberQueue&& other) =
412 void PacketNumberQueue::Add(QuicPacketNumber packet_number
) {
413 if (use_interval_set_
) {
414 packet_number_intervals_
.Add(packet_number
, packet_number
+ 1);
416 packet_number_set_
.insert(packet_number
);
420 void PacketNumberQueue::Add(QuicPacketNumber lower
, QuicPacketNumber higher
) {
421 if (use_interval_set_
) {
422 packet_number_intervals_
.Add(lower
, higher
);
424 for (QuicPacketNumber packet_number
= lower
; packet_number
< higher
;
431 void PacketNumberQueue::Remove(QuicPacketNumber packet_number
) {
432 if (use_interval_set_
) {
433 packet_number_intervals_
.Difference(packet_number
, packet_number
+ 1);
435 packet_number_set_
.erase(packet_number
);
439 bool PacketNumberQueue::RemoveUpTo(QuicPacketNumber higher
) {
440 if (use_interval_set_
) {
444 const QuicPacketNumber old_min
= Min();
445 packet_number_intervals_
.Difference(0, higher
);
446 return Empty() || old_min
!= Min();
448 const size_t orig_size
= packet_number_set_
.size();
449 packet_number_set_
.erase(packet_number_set_
.begin(),
450 packet_number_set_
.lower_bound(higher
));
451 return orig_size
!= packet_number_set_
.size();
455 bool PacketNumberQueue::Contains(QuicPacketNumber packet_number
) const {
456 if (use_interval_set_
) {
457 return packet_number_intervals_
.Contains(packet_number
);
459 return ContainsKey(packet_number_set_
, packet_number
);
463 bool PacketNumberQueue::Empty() const {
464 if (use_interval_set_
) {
465 return packet_number_intervals_
.Empty();
467 return packet_number_set_
.empty();
471 QuicPacketNumber
PacketNumberQueue::Min() const {
473 if (use_interval_set_
) {
474 return packet_number_intervals_
.begin()->min();
476 return *packet_number_set_
.begin();
480 QuicPacketNumber
PacketNumberQueue::Max() const {
482 if (use_interval_set_
) {
483 return packet_number_intervals_
.rbegin()->max() - 1;
485 return *packet_number_set_
.rbegin();
489 size_t PacketNumberQueue::NumPacketsSlow() const {
490 if (use_interval_set_
) {
491 size_t num_packets
= 0;
492 for (const auto& interval
: packet_number_intervals_
) {
493 num_packets
+= interval
.Length();
497 return packet_number_set_
.size();
501 PacketNumberQueue::const_iterator
PacketNumberQueue::begin() const {
502 if (use_interval_set_
) {
503 QuicPacketNumber first
;
504 QuicPacketNumber last
;
505 if (packet_number_intervals_
.Empty()) {
509 first
= packet_number_intervals_
.begin()->min();
510 last
= packet_number_intervals_
.rbegin()->max();
512 return const_iterator(packet_number_intervals_
.begin(), first
, last
);
514 return const_iterator(packet_number_set_
.begin());
518 PacketNumberQueue::const_iterator
PacketNumberQueue::end() const {
519 if (use_interval_set_
) {
520 QuicPacketNumber last
= packet_number_intervals_
.Empty()
522 : packet_number_intervals_
.rbegin()->max();
523 return const_iterator(packet_number_intervals_
.end(), last
, last
);
525 return const_iterator(packet_number_set_
.end());
529 PacketNumberQueue::const_iterator
PacketNumberQueue::lower_bound(
530 QuicPacketNumber packet_number
) const {
531 if (use_interval_set_
) {
532 QuicPacketNumber first
;
533 QuicPacketNumber last
;
534 if (packet_number_intervals_
.Empty()) {
537 return const_iterator(packet_number_intervals_
.begin(), first
, last
);
539 if (!packet_number_intervals_
.Contains(packet_number
)) {
542 IntervalSet
<QuicPacketNumber
>::const_iterator it
=
543 packet_number_intervals_
.Find(packet_number
);
544 first
= packet_number
;
545 last
= packet_number_intervals_
.rbegin()->max();
546 return const_iterator(it
, first
, last
);
548 return const_iterator(packet_number_set_
.lower_bound(packet_number
));
552 ostream
& operator<<(ostream
& os
, const PacketNumberQueue
& q
) {
553 for (QuicPacketNumber packet_number
: q
) {
554 os
<< packet_number
<< " ";
559 ostream
& operator<<(ostream
& os
, const QuicAckFrame
& ack_frame
) {
560 os
<< "entropy_hash: " << static_cast<int>(ack_frame
.entropy_hash
)
561 << " largest_observed: " << ack_frame
.largest_observed
562 << " delta_time_largest_observed: "
563 << ack_frame
.delta_time_largest_observed
.ToMicroseconds()
564 << " missing_packets: [ " << ack_frame
.missing_packets
565 << " ] is_truncated: " << ack_frame
.is_truncated
;
566 os
<< " revived_packets: [ ";
567 for (PacketNumberSet::const_iterator it
= ack_frame
.revived_packets
.begin();
568 it
!= ack_frame
.revived_packets
.end(); ++it
) {
571 os
<< " ] received_packets: [ ";
572 for (PacketTimeList::const_iterator it
=
573 ack_frame
.received_packet_times
.begin();
574 it
!= ack_frame
.received_packet_times
.end(); ++it
) {
575 os
<< it
->first
<< " at " << it
->second
.ToDebuggingValue() << " ";
581 ostream
& operator<<(ostream
& os
, const QuicFrame
& frame
) {
582 switch (frame
.type
) {
583 case PADDING_FRAME
: {
584 os
<< "type { PADDING_FRAME } ";
587 case RST_STREAM_FRAME
: {
588 os
<< "type { RST_STREAM_FRAME } " << *(frame
.rst_stream_frame
);
591 case CONNECTION_CLOSE_FRAME
: {
592 os
<< "type { CONNECTION_CLOSE_FRAME } "
593 << *(frame
.connection_close_frame
);
597 os
<< "type { GOAWAY_FRAME } " << *(frame
.goaway_frame
);
600 case WINDOW_UPDATE_FRAME
: {
601 os
<< "type { WINDOW_UPDATE_FRAME } " << *(frame
.window_update_frame
);
604 case BLOCKED_FRAME
: {
605 os
<< "type { BLOCKED_FRAME } " << *(frame
.blocked_frame
);
609 os
<< "type { STREAM_FRAME } " << *(frame
.stream_frame
);
613 os
<< "type { ACK_FRAME } " << *(frame
.ack_frame
);
616 case STOP_WAITING_FRAME
: {
617 os
<< "type { STOP_WAITING_FRAME } " << *(frame
.stop_waiting_frame
);
621 os
<< "type { PING_FRAME } ";
624 case MTU_DISCOVERY_FRAME
: {
625 os
<< "type { MTU_DISCOVERY_FRAME } ";
629 LOG(ERROR
) << "Unknown frame type: " << frame
.type
;
636 ostream
& operator<<(ostream
& os
, const QuicRstStreamFrame
& rst_frame
) {
637 os
<< "stream_id { " << rst_frame
.stream_id
<< " } "
638 << "error_code { " << rst_frame
.error_code
<< " } "
639 << "error_details { " << rst_frame
.error_details
<< " }\n";
643 ostream
& operator<<(ostream
& os
,
644 const QuicConnectionCloseFrame
& connection_close_frame
) {
645 os
<< "error_code { " << connection_close_frame
.error_code
<< " } "
646 << "error_details { " << connection_close_frame
.error_details
<< " }\n";
650 ostream
& operator<<(ostream
& os
, const QuicGoAwayFrame
& goaway_frame
) {
651 os
<< "error_code { " << goaway_frame
.error_code
<< " } "
652 << "last_good_stream_id { " << goaway_frame
.last_good_stream_id
<< " } "
653 << "reason_phrase { " << goaway_frame
.reason_phrase
<< " }\n";
657 ostream
& operator<<(ostream
& os
,
658 const QuicWindowUpdateFrame
& window_update_frame
) {
659 os
<< "stream_id { " << window_update_frame
.stream_id
<< " } "
660 << "byte_offset { " << window_update_frame
.byte_offset
<< " }\n";
664 ostream
& operator<<(ostream
& os
, const QuicBlockedFrame
& blocked_frame
) {
665 os
<< "stream_id { " << blocked_frame
.stream_id
<< " }\n";
669 ostream
& operator<<(ostream
& os
, const QuicStreamFrame
& stream_frame
) {
670 os
<< "stream_id { " << stream_frame
.stream_id
<< " } "
671 << "fin { " << stream_frame
.fin
<< " } "
672 << "offset { " << stream_frame
.offset
<< " } "
673 << "data { " << QuicUtils::StringToHexASCIIDump(stream_frame
.data
)
678 QuicGoAwayFrame::QuicGoAwayFrame()
679 : error_code(QUIC_NO_ERROR
),
680 last_good_stream_id(0) {
683 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code
,
684 QuicStreamId last_good_stream_id
,
685 const string
& reason
)
686 : error_code(error_code
),
687 last_good_stream_id(last_good_stream_id
),
688 reason_phrase(reason
) {
689 DCHECK_LE(error_code
, numeric_limits
<uint8
>::max());
692 QuicData::QuicData(const char* buffer
,
696 owns_buffer_(false) {
699 QuicData::QuicData(char* buffer
,
704 owns_buffer_(owns_buffer
) {
707 QuicData::~QuicData() {
709 delete [] const_cast<char*>(buffer_
);
713 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id
,
714 QuicStreamOffset byte_offset
)
715 : stream_id(stream_id
),
716 byte_offset(byte_offset
) {}
718 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id
)
719 : stream_id(stream_id
) {}
721 QuicPacket::QuicPacket(char* buffer
,
724 QuicConnectionIdLength connection_id_length
,
725 bool includes_version
,
726 QuicPacketNumberLength packet_number_length
)
727 : QuicData(buffer
, length
, owns_buffer
),
729 connection_id_length_(connection_id_length
),
730 includes_version_(includes_version
),
731 packet_number_length_(packet_number_length
) {}
733 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer
,
735 : QuicData(buffer
, length
) {
738 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer
,
741 : QuicData(buffer
, length
, owns_buffer
) {
744 StringPiece
QuicPacket::FecProtectedData() const {
745 const size_t start_of_fec
= GetStartOfFecProtectedData(
746 connection_id_length_
, includes_version_
, packet_number_length_
);
747 return StringPiece(data() + start_of_fec
, length() - start_of_fec
);
750 StringPiece
QuicPacket::AssociatedData() const {
752 data() + kStartOfHashData
,
753 GetStartOfEncryptedData(connection_id_length_
, includes_version_
,
754 packet_number_length_
) -
758 StringPiece
QuicPacket::BeforePlaintext() const {
760 data(), GetStartOfEncryptedData(connection_id_length_
, includes_version_
,
761 packet_number_length_
));
764 StringPiece
QuicPacket::Plaintext() const {
765 const size_t start_of_encrypted_data
= GetStartOfEncryptedData(
766 connection_id_length_
, includes_version_
, packet_number_length_
);
767 return StringPiece(data() + start_of_encrypted_data
,
768 length() - start_of_encrypted_data
);
771 RetransmittableFrames::RetransmittableFrames(EncryptionLevel level
)
772 : encryption_level_(level
),
773 has_crypto_handshake_(NOT_HANDSHAKE
),
774 needs_padding_(false) {
777 RetransmittableFrames::~RetransmittableFrames() {
778 for (QuicFrames::iterator it
= frames_
.begin(); it
!= frames_
.end(); ++it
) {
781 delete it
->padding_frame
;
784 delete it
->stream_frame
;
787 delete it
->ack_frame
;
789 case MTU_DISCOVERY_FRAME
:
790 delete it
->mtu_discovery_frame
;
792 case STOP_WAITING_FRAME
:
793 delete it
->stop_waiting_frame
;
796 delete it
->ping_frame
;
798 case RST_STREAM_FRAME
:
799 delete it
->rst_stream_frame
;
801 case CONNECTION_CLOSE_FRAME
:
802 delete it
->connection_close_frame
;
805 delete it
->goaway_frame
;
807 case WINDOW_UPDATE_FRAME
:
808 delete it
->window_update_frame
;
811 delete it
->blocked_frame
;
813 case NUM_FRAME_TYPES
:
814 DCHECK(false) << "Cannot delete type: " << it
->type
;
817 for (const char* buffer
: stream_data_
) {
822 const QuicFrame
& RetransmittableFrames::AddFrame(const QuicFrame
& frame
) {
823 return AddFrame(frame
, nullptr);
826 const QuicFrame
& RetransmittableFrames::AddFrame(const QuicFrame
& frame
,
828 if (frame
.type
== STREAM_FRAME
&&
829 frame
.stream_frame
->stream_id
== kCryptoStreamId
) {
830 has_crypto_handshake_
= IS_HANDSHAKE
;
832 if (buffer
!= nullptr) {
833 stream_data_
.push_back(buffer
);
835 frames_
.push_back(frame
);
836 return frames_
.back();
839 void RetransmittableFrames::RemoveFramesForStream(QuicStreamId stream_id
) {
840 QuicFrames::iterator it
= frames_
.begin();
841 while (it
!= frames_
.end()) {
842 if (it
->type
!= STREAM_FRAME
|| it
->stream_frame
->stream_id
!= stream_id
) {
846 delete it
->stream_frame
;
847 it
= frames_
.erase(it
);
851 SerializedPacket::SerializedPacket(
852 QuicPacketNumber packet_number
,
853 QuicPacketNumberLength packet_number_length
,
854 QuicEncryptedPacket
* packet
,
855 QuicPacketEntropyHash entropy_hash
,
856 RetransmittableFrames
* retransmittable_frames
,
858 bool has_stop_waiting
)
860 retransmittable_frames(retransmittable_frames
),
861 packet_number(packet_number
),
862 packet_number_length(packet_number_length
),
863 entropy_hash(entropy_hash
),
864 is_fec_packet(false),
866 has_stop_waiting(has_stop_waiting
) {}
868 SerializedPacket::~SerializedPacket() {}
870 QuicEncryptedPacket
* QuicEncryptedPacket::Clone() const {
871 char* buffer
= new char[this->length()];
872 memcpy(buffer
, this->data(), this->length());
873 return new QuicEncryptedPacket(buffer
, this->length(), true);
876 ostream
& operator<<(ostream
& os
, const QuicEncryptedPacket
& s
) {
877 os
<< s
.length() << "-byte data";
881 TransmissionInfo::TransmissionInfo()
882 : retransmittable_frames(nullptr),
883 packet_number_length(PACKET_1BYTE_PACKET_NUMBER
),
884 sent_time(QuicTime::Zero()),
887 transmission_type(NOT_RETRANSMISSION
),
888 all_transmissions(nullptr),
891 is_fec_packet(false) {}
893 TransmissionInfo::TransmissionInfo(
894 RetransmittableFrames
* retransmittable_frames
,
895 QuicPacketNumberLength packet_number_length
,
896 TransmissionType transmission_type
,
898 QuicByteCount bytes_sent
,
900 : retransmittable_frames(retransmittable_frames
),
901 packet_number_length(packet_number_length
),
902 sent_time(sent_time
),
903 bytes_sent(bytes_sent
),
905 transmission_type(transmission_type
),
906 all_transmissions(nullptr),
909 is_fec_packet(is_fec_packet
) {}