Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / net / quic / quic_protocol.cc
blobdeaeaf36815c82eadb962bcc9d5692b6ce4ff454
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;
12 using std::map;
13 using std::numeric_limits;
14 using std::ostream;
15 using std::string;
17 namespace net {
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,
27 bool include_version,
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 +
32 kPrivateFlagsSize +
33 (is_in_fec_group == IN_FEC_GROUP ? kFecGroupSize : 0);
36 size_t GetStartOfFecProtectedData(QuicConnectionIdLength connection_id_length,
37 bool include_version,
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,
44 bool include_version,
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) -
49 kPrivateFlagsSize;
52 QuicPacketPublicHeader::QuicPacketPublicHeader()
53 : connection_id(0),
54 connection_id_length(PACKET_8BYTE_CONNECTION_ID),
55 reset_flag(false),
56 version_flag(false),
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),
72 fec_flag(false),
73 entropy_flag(false),
74 entropy_hash(0),
75 is_in_fec_group(NOT_IN_FEC_GROUP),
76 fec_group(0) {}
78 QuicPacketHeader::QuicPacketHeader(const QuicPacketPublicHeader& header)
79 : public_header(header),
80 packet_packet_number(0),
81 fec_flag(false),
82 entropy_flag(false),
83 entropy_hash(0),
84 is_in_fec_group(NOT_IN_FEC_GROUP),
85 fec_group(0) {}
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),
99 fin(frame.fin),
100 offset(frame.offset),
101 data(frame.data) {
104 QuicStreamFrame::QuicStreamFrame(QuicStreamId stream_id,
105 bool fin,
106 QuicStreamOffset offset,
107 StringPiece data)
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)
120 != tag_vector.end();
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) {
132 switch (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');
137 default:
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;
141 return 0;
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) \
158 case x: \
159 return #x
161 string QuicVersionToString(const QuicVersion version) {
162 switch (version) {
163 RETURN_STRING_LITERAL(QUIC_VERSION_24);
164 RETURN_STRING_LITERAL(QUIC_VERSION_25);
165 default:
166 return "QUIC_VERSION_UNSUPPORTED";
170 string QuicVersionVectorToString(const QuicVersionVector& versions) {
171 string result = "";
172 for (size_t i = 0; i < versions.size(); ++i) {
173 if (i != 0) {
174 result.append(",");
176 result.append(QuicVersionToString(versions[i]));
178 return result;
181 ostream& operator<<(ostream& os, const Perspective& s) {
182 if (s == Perspective::IS_SERVER) {
183 os << "IS_SERVER";
184 } else {
185 os << "IS_CLIENT";
187 return os;
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) {
197 os << " version: ";
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";
208 return os;
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()
218 : entropy_hash(0),
219 least_unacked(0) {
222 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
224 QuicAckFrame::QuicAckFrame()
225 : entropy_hash(0),
226 largest_observed(0),
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) {
235 return error_code;
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)
267 : type(ACK_FRAME),
268 ack_frame(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)
281 : type(PING_FRAME),
282 ping_frame(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;
315 return os;
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),
328 current_(first),
329 last_(last) {}
331 PacketNumberQueue::const_iterator::const_iterator(const const_iterator& other) =
332 default;
333 // TODO(rtenneti): on windows RValue reference gives errors.
334 // PacketNumberQueue::const_iterator::const_iterator(const_iterator&& other) =
335 // default;
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_;
349 } else {
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_;
358 } else {
359 return set_iter_ == other.set_iter_;
363 PacketNumberQueue::const_iterator::value_type
364 PacketNumberQueue::const_iterator::
365 operator*() const {
366 if (use_interval_set_) {
367 return current_;
368 } else {
369 return *set_iter_;
373 PacketNumberQueue::const_iterator& PacketNumberQueue::const_iterator::
374 operator++() {
375 if (use_interval_set_) {
376 ++current_;
377 if (current_ < last_) {
378 if (current_ >= interval_set_iter_->max()) {
379 ++interval_set_iter_;
380 current_ = interval_set_iter_->min();
382 } else {
383 current_ = last_;
385 } else {
386 ++set_iter_;
388 return *this;
391 PacketNumberQueue::const_iterator PacketNumberQueue::const_iterator::operator++(
392 int /* postincrement */) {
393 PacketNumberQueue::const_iterator preincrement(*this);
394 operator++();
395 return preincrement;
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) =
410 // default;
412 void PacketNumberQueue::Add(QuicPacketNumber packet_number) {
413 if (use_interval_set_) {
414 packet_number_intervals_.Add(packet_number, packet_number + 1);
415 } else {
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);
423 } else {
424 for (QuicPacketNumber packet_number = lower; packet_number < higher;
425 ++packet_number) {
426 Add(packet_number);
431 void PacketNumberQueue::Remove(QuicPacketNumber packet_number) {
432 if (use_interval_set_) {
433 packet_number_intervals_.Difference(packet_number, packet_number + 1);
434 } else {
435 packet_number_set_.erase(packet_number);
439 bool PacketNumberQueue::RemoveUpTo(QuicPacketNumber higher) {
440 if (use_interval_set_) {
441 if (Empty()) {
442 return false;
444 const QuicPacketNumber old_min = Min();
445 packet_number_intervals_.Difference(0, higher);
446 return Empty() || old_min != Min();
447 } else {
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);
458 } else {
459 return ContainsKey(packet_number_set_, packet_number);
463 bool PacketNumberQueue::Empty() const {
464 if (use_interval_set_) {
465 return packet_number_intervals_.Empty();
466 } else {
467 return packet_number_set_.empty();
471 QuicPacketNumber PacketNumberQueue::Min() const {
472 DCHECK(!Empty());
473 if (use_interval_set_) {
474 return packet_number_intervals_.begin()->min();
475 } else {
476 return *packet_number_set_.begin();
480 QuicPacketNumber PacketNumberQueue::Max() const {
481 DCHECK(!Empty());
482 if (use_interval_set_) {
483 return packet_number_intervals_.rbegin()->max() - 1;
484 } else {
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();
495 return num_packets;
496 } else {
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()) {
506 first = 0;
507 last = 0;
508 } else {
509 first = packet_number_intervals_.begin()->min();
510 last = packet_number_intervals_.rbegin()->max();
512 return const_iterator(packet_number_intervals_.begin(), first, last);
513 } else {
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);
524 } else {
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()) {
535 first = 0;
536 last = 0;
537 return const_iterator(packet_number_intervals_.begin(), first, last);
539 if (!packet_number_intervals_.Contains(packet_number)) {
540 return end();
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);
547 } else {
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 << " ";
556 return os;
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) {
569 os << *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() << " ";
577 os << " ]";
578 return os;
581 ostream& operator<<(ostream& os, const QuicFrame& frame) {
582 switch (frame.type) {
583 case PADDING_FRAME: {
584 os << "type { PADDING_FRAME } ";
585 break;
587 case RST_STREAM_FRAME: {
588 os << "type { RST_STREAM_FRAME } " << *(frame.rst_stream_frame);
589 break;
591 case CONNECTION_CLOSE_FRAME: {
592 os << "type { CONNECTION_CLOSE_FRAME } "
593 << *(frame.connection_close_frame);
594 break;
596 case GOAWAY_FRAME: {
597 os << "type { GOAWAY_FRAME } " << *(frame.goaway_frame);
598 break;
600 case WINDOW_UPDATE_FRAME: {
601 os << "type { WINDOW_UPDATE_FRAME } " << *(frame.window_update_frame);
602 break;
604 case BLOCKED_FRAME: {
605 os << "type { BLOCKED_FRAME } " << *(frame.blocked_frame);
606 break;
608 case STREAM_FRAME: {
609 os << "type { STREAM_FRAME } " << *(frame.stream_frame);
610 break;
612 case ACK_FRAME: {
613 os << "type { ACK_FRAME } " << *(frame.ack_frame);
614 break;
616 case STOP_WAITING_FRAME: {
617 os << "type { STOP_WAITING_FRAME } " << *(frame.stop_waiting_frame);
618 break;
620 case PING_FRAME: {
621 os << "type { PING_FRAME } ";
622 break;
624 case MTU_DISCOVERY_FRAME: {
625 os << "type { MTU_DISCOVERY_FRAME } ";
626 break;
628 default: {
629 LOG(ERROR) << "Unknown frame type: " << frame.type;
630 break;
633 return os;
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";
640 return os;
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";
647 return os;
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";
654 return os;
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";
661 return os;
664 ostream& operator<<(ostream& os, const QuicBlockedFrame& blocked_frame) {
665 os << "stream_id { " << blocked_frame.stream_id << " }\n";
666 return os;
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)
674 << " }\n";
675 return os;
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,
693 size_t length)
694 : buffer_(buffer),
695 length_(length),
696 owns_buffer_(false) {
699 QuicData::QuicData(char* buffer,
700 size_t length,
701 bool owns_buffer)
702 : buffer_(buffer),
703 length_(length),
704 owns_buffer_(owns_buffer) {
707 QuicData::~QuicData() {
708 if (owns_buffer_) {
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,
722 size_t length,
723 bool owns_buffer,
724 QuicConnectionIdLength connection_id_length,
725 bool includes_version,
726 QuicPacketNumberLength packet_number_length)
727 : QuicData(buffer, length, owns_buffer),
728 buffer_(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,
734 size_t length)
735 : QuicData(buffer, length) {
738 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer,
739 size_t length,
740 bool owns_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 {
751 return StringPiece(
752 data() + kStartOfHashData,
753 GetStartOfEncryptedData(connection_id_length_, includes_version_,
754 packet_number_length_) -
755 kStartOfHashData);
758 StringPiece QuicPacket::BeforePlaintext() const {
759 return StringPiece(
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) {
779 switch (it->type) {
780 case PADDING_FRAME:
781 delete it->padding_frame;
782 break;
783 case STREAM_FRAME:
784 delete it->stream_frame;
785 break;
786 case ACK_FRAME:
787 delete it->ack_frame;
788 break;
789 case MTU_DISCOVERY_FRAME:
790 delete it->mtu_discovery_frame;
791 break;
792 case STOP_WAITING_FRAME:
793 delete it->stop_waiting_frame;
794 break;
795 case PING_FRAME:
796 delete it->ping_frame;
797 break;
798 case RST_STREAM_FRAME:
799 delete it->rst_stream_frame;
800 break;
801 case CONNECTION_CLOSE_FRAME:
802 delete it->connection_close_frame;
803 break;
804 case GOAWAY_FRAME:
805 delete it->goaway_frame;
806 break;
807 case WINDOW_UPDATE_FRAME:
808 delete it->window_update_frame;
809 break;
810 case BLOCKED_FRAME:
811 delete it->blocked_frame;
812 break;
813 case NUM_FRAME_TYPES:
814 DCHECK(false) << "Cannot delete type: " << it->type;
817 for (const char* buffer : stream_data_) {
818 delete[] buffer;
822 const QuicFrame& RetransmittableFrames::AddFrame(const QuicFrame& frame) {
823 return AddFrame(frame, nullptr);
826 const QuicFrame& RetransmittableFrames::AddFrame(const QuicFrame& frame,
827 char* buffer) {
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) {
843 ++it;
844 continue;
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,
857 bool has_ack,
858 bool has_stop_waiting)
859 : packet(packet),
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),
865 has_ack(has_ack),
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";
878 return os;
881 TransmissionInfo::TransmissionInfo()
882 : retransmittable_frames(nullptr),
883 packet_number_length(PACKET_1BYTE_PACKET_NUMBER),
884 sent_time(QuicTime::Zero()),
885 bytes_sent(0),
886 nack_count(0),
887 transmission_type(NOT_RETRANSMISSION),
888 all_transmissions(nullptr),
889 in_flight(false),
890 is_unackable(false),
891 is_fec_packet(false) {}
893 TransmissionInfo::TransmissionInfo(
894 RetransmittableFrames* retransmittable_frames,
895 QuicPacketNumberLength packet_number_length,
896 TransmissionType transmission_type,
897 QuicTime sent_time,
898 QuicByteCount bytes_sent,
899 bool is_fec_packet)
900 : retransmittable_frames(retransmittable_frames),
901 packet_number_length(packet_number_length),
902 sent_time(sent_time),
903 bytes_sent(bytes_sent),
904 nack_count(0),
905 transmission_type(transmission_type),
906 all_transmissions(nullptr),
907 in_flight(false),
908 is_unackable(false),
909 is_fec_packet(is_fec_packet) {}
911 } // namespace net