Delete unused downloads page asset.
[chromium-blink-merge.git] / net / quic / quic_protocol.cc
blob404c72b6bd936c7fe1759c9db266bb3620502c46
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.packet_number_length,
22 header.is_in_fec_group);
25 size_t GetPacketHeaderSize(QuicConnectionIdLength connection_id_length,
26 bool include_version,
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 +
31 kPrivateFlagsSize +
32 (is_in_fec_group == IN_FEC_GROUP ? kFecGroupSize : 0);
35 size_t GetStartOfFecProtectedData(QuicConnectionIdLength connection_id_length,
36 bool include_version,
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,
43 bool include_version,
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) -
48 kPrivateFlagsSize;
51 QuicPacketPublicHeader::QuicPacketPublicHeader()
52 : connection_id(0),
53 connection_id_length(PACKET_8BYTE_CONNECTION_ID),
54 reset_flag(false),
55 version_flag(false),
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),
71 fec_flag(false),
72 entropy_flag(false),
73 entropy_hash(0),
74 is_in_fec_group(NOT_IN_FEC_GROUP),
75 fec_group(0) {}
77 QuicPacketHeader::QuicPacketHeader(const QuicPacketPublicHeader& header)
78 : public_header(header),
79 packet_packet_number(0),
80 fec_flag(false),
81 entropy_flag(false),
82 entropy_hash(0),
83 is_in_fec_group(NOT_IN_FEC_GROUP),
84 fec_group(0) {}
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),
98 fin(frame.fin),
99 offset(frame.offset),
100 data(frame.data) {
103 QuicStreamFrame::QuicStreamFrame(QuicStreamId stream_id,
104 bool fin,
105 QuicStreamOffset offset,
106 StringPiece data)
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)
119 != tag_vector.end();
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) {
131 switch (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');
136 default:
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;
140 return 0;
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) \
157 case x: \
158 return #x
160 string QuicVersionToString(const QuicVersion version) {
161 switch (version) {
162 RETURN_STRING_LITERAL(QUIC_VERSION_24);
163 RETURN_STRING_LITERAL(QUIC_VERSION_25);
164 default:
165 return "QUIC_VERSION_UNSUPPORTED";
169 string QuicVersionVectorToString(const QuicVersionVector& versions) {
170 string result = "";
171 for (size_t i = 0; i < versions.size(); ++i) {
172 if (i != 0) {
173 result.append(",");
175 result.append(QuicVersionToString(versions[i]));
177 return result;
180 ostream& operator<<(ostream& os, const Perspective& s) {
181 if (s == Perspective::IS_SERVER) {
182 os << "IS_SERVER";
183 } else {
184 os << "IS_CLIENT";
186 return os;
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) {
196 os << " version: ";
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";
207 return os;
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()
225 : entropy_hash(0),
226 least_unacked(0) {
229 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
231 QuicAckFrame::QuicAckFrame()
232 : entropy_hash(0),
233 largest_observed(0),
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) {
242 return error_code;
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)
274 : type(ACK_FRAME),
275 ack_frame(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)
288 : type(PING_FRAME),
289 ping_frame(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;
322 return os;
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) {
333 os << *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) {
339 os << *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() << " ";
347 os << " ]";
348 return os;
351 ostream& operator<<(ostream& os, const QuicFrame& frame) {
352 switch (frame.type) {
353 case PADDING_FRAME: {
354 os << "type { PADDING_FRAME } ";
355 break;
357 case RST_STREAM_FRAME: {
358 os << "type { RST_STREAM_FRAME } " << *(frame.rst_stream_frame);
359 break;
361 case CONNECTION_CLOSE_FRAME: {
362 os << "type { CONNECTION_CLOSE_FRAME } "
363 << *(frame.connection_close_frame);
364 break;
366 case GOAWAY_FRAME: {
367 os << "type { GOAWAY_FRAME } " << *(frame.goaway_frame);
368 break;
370 case WINDOW_UPDATE_FRAME: {
371 os << "type { WINDOW_UPDATE_FRAME } " << *(frame.window_update_frame);
372 break;
374 case BLOCKED_FRAME: {
375 os << "type { BLOCKED_FRAME } " << *(frame.blocked_frame);
376 break;
378 case STREAM_FRAME: {
379 os << "type { STREAM_FRAME } " << *(frame.stream_frame);
380 break;
382 case ACK_FRAME: {
383 os << "type { ACK_FRAME } " << *(frame.ack_frame);
384 break;
386 case STOP_WAITING_FRAME: {
387 os << "type { STOP_WAITING_FRAME } " << *(frame.stop_waiting_frame);
388 break;
390 case PING_FRAME: {
391 os << "type { PING_FRAME } ";
392 break;
394 case MTU_DISCOVERY_FRAME: {
395 os << "type { MTU_DISCOVERY_FRAME } ";
396 break;
398 default: {
399 LOG(ERROR) << "Unknown frame type: " << frame.type;
400 break;
403 return os;
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";
410 return os;
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";
417 return os;
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";
424 return os;
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";
431 return os;
434 ostream& operator<<(ostream& os, const QuicBlockedFrame& blocked_frame) {
435 os << "stream_id { " << blocked_frame.stream_id << " }\n";
436 return os;
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)
444 << " }\n";
445 return os;
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,
463 size_t length)
464 : buffer_(buffer),
465 length_(length),
466 owns_buffer_(false) {
469 QuicData::QuicData(char* buffer,
470 size_t length,
471 bool owns_buffer)
472 : buffer_(buffer),
473 length_(length),
474 owns_buffer_(owns_buffer) {
477 QuicData::~QuicData() {
478 if (owns_buffer_) {
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,
492 size_t length,
493 bool owns_buffer,
494 QuicConnectionIdLength connection_id_length,
495 bool includes_version,
496 QuicPacketNumberLength packet_number_length)
497 : QuicData(buffer, length, owns_buffer),
498 buffer_(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,
504 size_t length)
505 : QuicData(buffer, length) {
508 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer,
509 size_t length,
510 bool owns_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 {
521 return StringPiece(
522 data() + kStartOfHashData,
523 GetStartOfEncryptedData(connection_id_length_, includes_version_,
524 packet_number_length_) -
525 kStartOfHashData);
528 StringPiece QuicPacket::BeforePlaintext() const {
529 return StringPiece(
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) {
549 switch (it->type) {
550 case PADDING_FRAME:
551 delete it->padding_frame;
552 break;
553 case STREAM_FRAME:
554 delete it->stream_frame;
555 break;
556 case ACK_FRAME:
557 delete it->ack_frame;
558 break;
559 case MTU_DISCOVERY_FRAME:
560 delete it->mtu_discovery_frame;
561 break;
562 case STOP_WAITING_FRAME:
563 delete it->stop_waiting_frame;
564 break;
565 case PING_FRAME:
566 delete it->ping_frame;
567 break;
568 case RST_STREAM_FRAME:
569 delete it->rst_stream_frame;
570 break;
571 case CONNECTION_CLOSE_FRAME:
572 delete it->connection_close_frame;
573 break;
574 case GOAWAY_FRAME:
575 delete it->goaway_frame;
576 break;
577 case WINDOW_UPDATE_FRAME:
578 delete it->window_update_frame;
579 break;
580 case BLOCKED_FRAME:
581 delete it->blocked_frame;
582 break;
583 case NUM_FRAME_TYPES:
584 DCHECK(false) << "Cannot delete type: " << it->type;
587 for (const char* buffer : stream_data_) {
588 delete[] buffer;
592 const QuicFrame& RetransmittableFrames::AddFrame(const QuicFrame& frame) {
593 return AddFrame(frame, nullptr);
596 const QuicFrame& RetransmittableFrames::AddFrame(const QuicFrame& frame,
597 char* buffer) {
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) {
613 ++it;
614 continue;
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,
627 bool has_ack,
628 bool has_stop_waiting)
629 : packet(packet),
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),
635 has_ack(has_ack),
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";
648 return os;
651 TransmissionInfo::TransmissionInfo()
652 : retransmittable_frames(nullptr),
653 packet_number_length(PACKET_1BYTE_PACKET_NUMBER),
654 sent_time(QuicTime::Zero()),
655 bytes_sent(0),
656 nack_count(0),
657 transmission_type(NOT_RETRANSMISSION),
658 all_transmissions(nullptr),
659 in_flight(false),
660 is_unackable(false),
661 is_fec_packet(false) {}
663 TransmissionInfo::TransmissionInfo(
664 RetransmittableFrames* retransmittable_frames,
665 QuicPacketNumberLength packet_number_length,
666 TransmissionType transmission_type,
667 QuicTime sent_time,
668 QuicByteCount bytes_sent,
669 bool is_fec_packet)
670 : retransmittable_frames(retransmittable_frames),
671 packet_number_length(packet_number_length),
672 sent_time(sent_time),
673 bytes_sent(bytes_sent),
674 nack_count(0),
675 transmission_type(transmission_type),
676 all_transmissions(nullptr),
677 in_flight(false),
678 is_unackable(false),
679 is_fec_packet(is_fec_packet) {}
681 } // namespace net