Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / quic / quic_protocol.cc
blob821274912fced9ab6301e9013cac1be09a34b1fe
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;
11 using std::map;
12 using std::numeric_limits;
13 using std::ostream;
14 using std::string;
16 namespace net {
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,
26 bool include_version,
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,
36 bool include_version,
37 QuicSequenceNumberLength sequence_number_length) {
38 return GetPacketHeaderSize(connection_id_length,
39 include_version,
40 sequence_number_length,
41 IN_FEC_GROUP);
44 size_t GetStartOfEncryptedData(
45 QuicConnectionIdLength connection_id_length,
46 bool include_version,
47 QuicSequenceNumberLength sequence_number_length) {
48 // Don't include the fec size, since encryption starts before private flags.
49 return GetPacketHeaderSize(connection_id_length,
50 include_version,
51 sequence_number_length,
52 NOT_IN_FEC_GROUP) - kPrivateFlagsSize;
55 QuicPacketPublicHeader::QuicPacketPublicHeader()
56 : connection_id(0),
57 connection_id_length(PACKET_8BYTE_CONNECTION_ID),
58 reset_flag(false),
59 version_flag(false),
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()
76 : packet_sequence_number(0),
77 fec_flag(false),
78 entropy_flag(false),
79 entropy_hash(0),
80 is_in_fec_group(NOT_IN_FEC_GROUP),
81 fec_group(0) {
84 QuicPacketHeader::QuicPacketHeader(const QuicPacketPublicHeader& header)
85 : public_header(header),
86 packet_sequence_number(0),
87 fec_flag(false),
88 entropy_flag(false),
89 entropy_hash(0),
90 is_in_fec_group(NOT_IN_FEC_GROUP),
91 fec_group(0) {
94 QuicPublicResetPacket::QuicPublicResetPacket()
95 : nonce_proof(0),
96 rejected_sequence_number(0) {}
98 QuicPublicResetPacket::QuicPublicResetPacket(
99 const QuicPacketPublicHeader& header)
100 : public_header(header),
101 nonce_proof(0),
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),
109 fin(frame.fin),
110 offset(frame.offset),
111 data(frame.data) {
114 QuicStreamFrame::QuicStreamFrame(QuicStreamId stream_id,
115 bool fin,
116 QuicStreamOffset offset,
117 StringPiece data)
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)
130 != tag_vector.end();
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) {
142 switch (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');
147 default:
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;
151 return 0;
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) \
168 case x: \
169 return #x
171 string QuicVersionToString(const QuicVersion version) {
172 switch (version) {
173 RETURN_STRING_LITERAL(QUIC_VERSION_24);
174 RETURN_STRING_LITERAL(QUIC_VERSION_25);
175 default:
176 return "QUIC_VERSION_UNSUPPORTED";
180 string QuicVersionVectorToString(const QuicVersionVector& versions) {
181 string result = "";
182 for (size_t i = 0; i < versions.size(); ++i) {
183 if (i != 0) {
184 result.append(",");
186 result.append(QuicVersionToString(versions[i]));
188 return result;
191 ostream& operator<<(ostream& os, const Perspective& s) {
192 if (s == Perspective::IS_SERVER) {
193 os << "IS_SERVER";
194 } else {
195 os << "IS_CLIENT";
197 return os;
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) {
208 os << " version: ";
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";
219 return os;
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()
237 : entropy_hash(0),
238 least_unacked(0) {
241 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
243 QuicAckFrame::QuicAckFrame()
244 : entropy_hash(0),
245 largest_observed(0),
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) {
254 return error_code;
257 QuicRstStreamFrame::QuicRstStreamFrame()
258 : stream_id(0), error_code(QUIC_STREAM_NO_ERROR), byte_offset(0) {}
260 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id,
261 QuicRstStreamErrorCode error_code,
262 QuicStreamOffset bytes_written)
263 : stream_id(stream_id),
264 error_code(error_code),
265 byte_offset(bytes_written) {
266 DCHECK_LE(error_code, numeric_limits<uint8>::max());
269 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
270 : error_code(QUIC_NO_ERROR) {
273 QuicFrame::QuicFrame() {}
275 QuicFrame::QuicFrame(QuicPaddingFrame* padding_frame)
276 : type(PADDING_FRAME),
277 padding_frame(padding_frame) {
280 QuicFrame::QuicFrame(QuicStreamFrame* stream_frame)
281 : type(STREAM_FRAME),
282 stream_frame(stream_frame) {
285 QuicFrame::QuicFrame(QuicAckFrame* frame)
286 : type(ACK_FRAME),
287 ack_frame(frame) {
290 QuicFrame::QuicFrame(QuicMtuDiscoveryFrame* frame)
291 : type(MTU_DISCOVERY_FRAME), mtu_discovery_frame(frame) {
294 QuicFrame::QuicFrame(QuicStopWaitingFrame* frame)
295 : type(STOP_WAITING_FRAME),
296 stop_waiting_frame(frame) {
299 QuicFrame::QuicFrame(QuicPingFrame* frame)
300 : type(PING_FRAME),
301 ping_frame(frame) {
304 QuicFrame::QuicFrame(QuicRstStreamFrame* frame)
305 : type(RST_STREAM_FRAME),
306 rst_stream_frame(frame) {
309 QuicFrame::QuicFrame(QuicConnectionCloseFrame* frame)
310 : type(CONNECTION_CLOSE_FRAME),
311 connection_close_frame(frame) {
314 QuicFrame::QuicFrame(QuicGoAwayFrame* frame)
315 : type(GOAWAY_FRAME),
316 goaway_frame(frame) {
319 QuicFrame::QuicFrame(QuicWindowUpdateFrame* frame)
320 : type(WINDOW_UPDATE_FRAME),
321 window_update_frame(frame) {
324 QuicFrame::QuicFrame(QuicBlockedFrame* frame)
325 : type(BLOCKED_FRAME),
326 blocked_frame(frame) {
329 QuicFecData::QuicFecData() : fec_group(0) {}
331 ostream& operator<<(ostream& os, const QuicStopWaitingFrame& sent_info) {
332 os << "entropy_hash: " << static_cast<int>(sent_info.entropy_hash)
333 << " least_unacked: " << sent_info.least_unacked;
334 return os;
337 ostream& operator<<(ostream& os, const QuicAckFrame& ack_frame) {
338 os << "entropy_hash: " << static_cast<int>(ack_frame.entropy_hash)
339 << " largest_observed: " << ack_frame.largest_observed
340 << " delta_time_largest_observed: "
341 << ack_frame.delta_time_largest_observed.ToMicroseconds()
342 << " missing_packets: [ ";
343 for (SequenceNumberSet::const_iterator it = ack_frame.missing_packets.begin();
344 it != ack_frame.missing_packets.end(); ++it) {
345 os << *it << " ";
347 os << " ] is_truncated: " << ack_frame.is_truncated;
348 os << " revived_packets: [ ";
349 for (SequenceNumberSet::const_iterator it = ack_frame.revived_packets.begin();
350 it != ack_frame.revived_packets.end(); ++it) {
351 os << *it << " ";
353 os << " ] received_packets: [ ";
354 for (PacketTimeList::const_iterator it =
355 ack_frame.received_packet_times.begin();
356 it != ack_frame.received_packet_times.end(); ++it) {
357 os << it->first << " at " << it->second.ToDebuggingValue() << " ";
359 os << " ]";
360 return os;
363 ostream& operator<<(ostream& os, const QuicFrame& frame) {
364 switch (frame.type) {
365 case PADDING_FRAME: {
366 os << "type { PADDING_FRAME } ";
367 break;
369 case RST_STREAM_FRAME: {
370 os << "type { RST_STREAM_FRAME } " << *(frame.rst_stream_frame);
371 break;
373 case CONNECTION_CLOSE_FRAME: {
374 os << "type { CONNECTION_CLOSE_FRAME } "
375 << *(frame.connection_close_frame);
376 break;
378 case GOAWAY_FRAME: {
379 os << "type { GOAWAY_FRAME } " << *(frame.goaway_frame);
380 break;
382 case WINDOW_UPDATE_FRAME: {
383 os << "type { WINDOW_UPDATE_FRAME } " << *(frame.window_update_frame);
384 break;
386 case BLOCKED_FRAME: {
387 os << "type { BLOCKED_FRAME } " << *(frame.blocked_frame);
388 break;
390 case STREAM_FRAME: {
391 os << "type { STREAM_FRAME } " << *(frame.stream_frame);
392 break;
394 case ACK_FRAME: {
395 os << "type { ACK_FRAME } " << *(frame.ack_frame);
396 break;
398 case STOP_WAITING_FRAME: {
399 os << "type { STOP_WAITING_FRAME } " << *(frame.stop_waiting_frame);
400 break;
402 case PING_FRAME: {
403 os << "type { PING_FRAME } ";
404 break;
406 case MTU_DISCOVERY_FRAME: {
407 os << "type { MTU_DISCOVERY_FRAME } ";
408 break;
410 default: {
411 LOG(ERROR) << "Unknown frame type: " << frame.type;
412 break;
415 return os;
418 ostream& operator<<(ostream& os, const QuicRstStreamFrame& rst_frame) {
419 os << "stream_id { " << rst_frame.stream_id << " } "
420 << "error_code { " << rst_frame.error_code << " } "
421 << "error_details { " << rst_frame.error_details << " }\n";
422 return os;
425 ostream& operator<<(ostream& os,
426 const QuicConnectionCloseFrame& connection_close_frame) {
427 os << "error_code { " << connection_close_frame.error_code << " } "
428 << "error_details { " << connection_close_frame.error_details << " }\n";
429 return os;
432 ostream& operator<<(ostream& os, const QuicGoAwayFrame& goaway_frame) {
433 os << "error_code { " << goaway_frame.error_code << " } "
434 << "last_good_stream_id { " << goaway_frame.last_good_stream_id << " } "
435 << "reason_phrase { " << goaway_frame.reason_phrase << " }\n";
436 return os;
439 ostream& operator<<(ostream& os,
440 const QuicWindowUpdateFrame& window_update_frame) {
441 os << "stream_id { " << window_update_frame.stream_id << " } "
442 << "byte_offset { " << window_update_frame.byte_offset << " }\n";
443 return os;
446 ostream& operator<<(ostream& os, const QuicBlockedFrame& blocked_frame) {
447 os << "stream_id { " << blocked_frame.stream_id << " }\n";
448 return os;
451 ostream& operator<<(ostream& os, const QuicStreamFrame& stream_frame) {
452 os << "stream_id { " << stream_frame.stream_id << " } "
453 << "fin { " << stream_frame.fin << " } "
454 << "offset { " << stream_frame.offset << " } "
455 << "data { " << QuicUtils::StringToHexASCIIDump(stream_frame.data)
456 << " }\n";
457 return os;
460 QuicGoAwayFrame::QuicGoAwayFrame()
461 : error_code(QUIC_NO_ERROR),
462 last_good_stream_id(0) {
465 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code,
466 QuicStreamId last_good_stream_id,
467 const string& reason)
468 : error_code(error_code),
469 last_good_stream_id(last_good_stream_id),
470 reason_phrase(reason) {
471 DCHECK_LE(error_code, numeric_limits<uint8>::max());
474 QuicData::QuicData(const char* buffer,
475 size_t length)
476 : buffer_(buffer),
477 length_(length),
478 owns_buffer_(false) {
481 QuicData::QuicData(char* buffer,
482 size_t length,
483 bool owns_buffer)
484 : buffer_(buffer),
485 length_(length),
486 owns_buffer_(owns_buffer) {
489 QuicData::~QuicData() {
490 if (owns_buffer_) {
491 delete [] const_cast<char*>(buffer_);
495 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id,
496 QuicStreamOffset byte_offset)
497 : stream_id(stream_id),
498 byte_offset(byte_offset) {}
500 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id)
501 : stream_id(stream_id) {}
503 QuicPacket::QuicPacket(char* buffer,
504 size_t length,
505 bool owns_buffer,
506 QuicConnectionIdLength connection_id_length,
507 bool includes_version,
508 QuicSequenceNumberLength sequence_number_length)
509 : QuicData(buffer, length, owns_buffer),
510 buffer_(buffer),
511 connection_id_length_(connection_id_length),
512 includes_version_(includes_version),
513 sequence_number_length_(sequence_number_length) {
516 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer,
517 size_t length)
518 : QuicData(buffer, length) {
521 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer,
522 size_t length,
523 bool owns_buffer)
524 : QuicData(buffer, length, owns_buffer) {
527 StringPiece QuicPacket::FecProtectedData() const {
528 const size_t start_of_fec = GetStartOfFecProtectedData(
529 connection_id_length_, includes_version_, sequence_number_length_);
530 return StringPiece(data() + start_of_fec, length() - start_of_fec);
533 StringPiece QuicPacket::AssociatedData() const {
534 return StringPiece(
535 data() + kStartOfHashData,
536 GetStartOfEncryptedData(
537 connection_id_length_, includes_version_, sequence_number_length_) -
538 kStartOfHashData);
541 StringPiece QuicPacket::BeforePlaintext() const {
542 return StringPiece(data(), GetStartOfEncryptedData(connection_id_length_,
543 includes_version_,
544 sequence_number_length_));
547 StringPiece QuicPacket::Plaintext() const {
548 const size_t start_of_encrypted_data =
549 GetStartOfEncryptedData(
550 connection_id_length_, includes_version_, sequence_number_length_);
551 return StringPiece(data() + start_of_encrypted_data,
552 length() - start_of_encrypted_data);
555 RetransmittableFrames::RetransmittableFrames(EncryptionLevel level)
556 : encryption_level_(level),
557 has_crypto_handshake_(NOT_HANDSHAKE),
558 needs_padding_(false) {
561 RetransmittableFrames::~RetransmittableFrames() {
562 for (QuicFrames::iterator it = frames_.begin(); it != frames_.end(); ++it) {
563 switch (it->type) {
564 case PADDING_FRAME:
565 delete it->padding_frame;
566 break;
567 case STREAM_FRAME:
568 delete it->stream_frame;
569 break;
570 case ACK_FRAME:
571 delete it->ack_frame;
572 break;
573 case MTU_DISCOVERY_FRAME:
574 delete it->mtu_discovery_frame;
575 break;
576 case STOP_WAITING_FRAME:
577 delete it->stop_waiting_frame;
578 break;
579 case PING_FRAME:
580 delete it->ping_frame;
581 break;
582 case RST_STREAM_FRAME:
583 delete it->rst_stream_frame;
584 break;
585 case CONNECTION_CLOSE_FRAME:
586 delete it->connection_close_frame;
587 break;
588 case GOAWAY_FRAME:
589 delete it->goaway_frame;
590 break;
591 case WINDOW_UPDATE_FRAME:
592 delete it->window_update_frame;
593 break;
594 case BLOCKED_FRAME:
595 delete it->blocked_frame;
596 break;
597 case NUM_FRAME_TYPES:
598 DCHECK(false) << "Cannot delete type: " << it->type;
601 for (const char* buffer : stream_data_) {
602 delete[] buffer;
606 const QuicFrame& RetransmittableFrames::AddFrame(const QuicFrame& frame) {
607 return AddFrame(frame, nullptr);
610 const QuicFrame& RetransmittableFrames::AddFrame(const QuicFrame& frame,
611 char* buffer) {
612 if (frame.type == STREAM_FRAME &&
613 frame.stream_frame->stream_id == kCryptoStreamId) {
614 has_crypto_handshake_ = IS_HANDSHAKE;
616 if (buffer != nullptr) {
617 stream_data_.push_back(buffer);
619 frames_.push_back(frame);
620 return frames_.back();
623 void RetransmittableFrames::RemoveFramesForStream(QuicStreamId stream_id) {
624 QuicFrames::iterator it = frames_.begin();
625 while (it != frames_.end()) {
626 if (it->type != STREAM_FRAME || it->stream_frame->stream_id != stream_id) {
627 ++it;
628 continue;
630 delete it->stream_frame;
631 it = frames_.erase(it);
635 SerializedPacket::SerializedPacket(
636 QuicPacketSequenceNumber sequence_number,
637 QuicSequenceNumberLength sequence_number_length,
638 QuicEncryptedPacket* packet,
639 QuicPacketEntropyHash entropy_hash,
640 RetransmittableFrames* retransmittable_frames)
641 : packet(packet),
642 retransmittable_frames(retransmittable_frames),
643 sequence_number(sequence_number),
644 sequence_number_length(sequence_number_length),
645 entropy_hash(entropy_hash),
646 is_fec_packet(false) {
649 SerializedPacket::~SerializedPacket() {}
651 QuicEncryptedPacket* QuicEncryptedPacket::Clone() const {
652 char* buffer = new char[this->length()];
653 memcpy(buffer, this->data(), this->length());
654 return new QuicEncryptedPacket(buffer, this->length(), true);
657 ostream& operator<<(ostream& os, const QuicEncryptedPacket& s) {
658 os << s.length() << "-byte data";
659 return os;
662 TransmissionInfo::TransmissionInfo()
663 : retransmittable_frames(nullptr),
664 sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER),
665 sent_time(QuicTime::Zero()),
666 bytes_sent(0),
667 nack_count(0),
668 transmission_type(NOT_RETRANSMISSION),
669 all_transmissions(nullptr),
670 in_flight(false),
671 is_unackable(false),
672 is_fec_packet(false) {
675 TransmissionInfo::TransmissionInfo(
676 RetransmittableFrames* retransmittable_frames,
677 QuicSequenceNumberLength sequence_number_length,
678 TransmissionType transmission_type,
679 QuicTime sent_time,
680 QuicByteCount bytes_sent,
681 bool is_fec_packet)
682 : retransmittable_frames(retransmittable_frames),
683 sequence_number_length(sequence_number_length),
684 sent_time(sent_time),
685 bytes_sent(bytes_sent),
686 nack_count(0),
687 transmission_type(transmission_type),
688 all_transmissions(nullptr),
689 in_flight(false),
690 is_unackable(false),
691 is_fec_packet(is_fec_packet) {
694 } // namespace net