Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / net / quic / quic_protocol.cc
blobfe7959e1d46d6d3971d90eee23e6eaad63453c24
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 : fec_flag(false),
77 entropy_flag(false),
78 entropy_hash(0),
79 packet_sequence_number(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 fec_flag(false),
87 entropy_flag(false),
88 entropy_hash(0),
89 packet_sequence_number(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()
105 : stream_id(0),
106 fin(false),
107 offset(0),
108 notifier(nullptr) {}
110 QuicStreamFrame::QuicStreamFrame(const QuicStreamFrame& frame)
111 : stream_id(frame.stream_id),
112 fin(frame.fin),
113 offset(frame.offset),
114 data(frame.data),
115 notifier(frame.notifier) {
118 QuicStreamFrame::QuicStreamFrame(QuicStreamId stream_id,
119 bool fin,
120 QuicStreamOffset offset,
121 IOVector data)
122 : stream_id(stream_id),
123 fin(fin),
124 offset(offset),
125 data(data),
126 notifier(nullptr) {}
128 string* QuicStreamFrame::GetDataAsString() const {
129 string* data_string = new string();
130 data_string->reserve(data.TotalBufferSize());
131 for (size_t i = 0; i < data.Size(); ++i) {
132 data_string->append(static_cast<char*>(data.iovec()[i].iov_base),
133 data.iovec()[i].iov_len);
135 DCHECK_EQ(data_string->size(), data.TotalBufferSize());
136 return data_string;
139 uint32 MakeQuicTag(char a, char b, char c, char d) {
140 return static_cast<uint32>(a) |
141 static_cast<uint32>(b) << 8 |
142 static_cast<uint32>(c) << 16 |
143 static_cast<uint32>(d) << 24;
146 bool ContainsQuicTag(const QuicTagVector& tag_vector, QuicTag tag) {
147 return std::find(tag_vector.begin(), tag_vector.end(), tag)
148 != tag_vector.end();
151 QuicVersionVector QuicSupportedVersions() {
152 QuicVersionVector supported_versions;
153 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
154 supported_versions.push_back(kSupportedQuicVersions[i]);
156 return supported_versions;
159 QuicTag QuicVersionToQuicTag(const QuicVersion version) {
160 switch (version) {
161 case QUIC_VERSION_23:
162 return MakeQuicTag('Q', '0', '2', '3');
163 case QUIC_VERSION_24:
164 return MakeQuicTag('Q', '0', '2', '4');
165 default:
166 // This shold be an ERROR because we should never attempt to convert an
167 // invalid QuicVersion to be written to the wire.
168 LOG(ERROR) << "Unsupported QuicVersion: " << version;
169 return 0;
173 QuicVersion QuicTagToQuicVersion(const QuicTag version_tag) {
174 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
175 if (version_tag == QuicVersionToQuicTag(kSupportedQuicVersions[i])) {
176 return kSupportedQuicVersions[i];
179 // Reading from the client so this should not be considered an ERROR.
180 DVLOG(1) << "Unsupported QuicTag version: "
181 << QuicUtils::TagToString(version_tag);
182 return QUIC_VERSION_UNSUPPORTED;
185 #define RETURN_STRING_LITERAL(x) \
186 case x: \
187 return #x
189 string QuicVersionToString(const QuicVersion version) {
190 switch (version) {
191 RETURN_STRING_LITERAL(QUIC_VERSION_23);
192 RETURN_STRING_LITERAL(QUIC_VERSION_24);
193 default:
194 return "QUIC_VERSION_UNSUPPORTED";
198 string QuicVersionVectorToString(const QuicVersionVector& versions) {
199 string result = "";
200 for (size_t i = 0; i < versions.size(); ++i) {
201 if (i != 0) {
202 result.append(",");
204 result.append(QuicVersionToString(versions[i]));
206 return result;
209 ostream& operator<<(ostream& os, const QuicPacketHeader& header) {
210 os << "{ connection_id: " << header.public_header.connection_id
211 << ", connection_id_length:" << header.public_header.connection_id_length
212 << ", sequence_number_length:"
213 << header.public_header.sequence_number_length
214 << ", reset_flag: " << header.public_header.reset_flag
215 << ", version_flag: " << header.public_header.version_flag;
216 if (header.public_header.version_flag) {
217 os << " version: ";
218 for (size_t i = 0; i < header.public_header.versions.size(); ++i) {
219 os << header.public_header.versions[0] << " ";
222 os << ", fec_flag: " << header.fec_flag
223 << ", entropy_flag: " << header.entropy_flag
224 << ", entropy hash: " << static_cast<int>(header.entropy_hash)
225 << ", sequence_number: " << header.packet_sequence_number
226 << ", is_in_fec_group:" << header.is_in_fec_group
227 << ", fec_group: " << header.fec_group<< "}\n";
228 return os;
231 bool IsAwaitingPacket(const QuicAckFrame& ack_frame,
232 QuicPacketSequenceNumber sequence_number) {
233 return sequence_number > ack_frame.largest_observed ||
234 ContainsKey(ack_frame.missing_packets, sequence_number);
237 void InsertMissingPacketsBetween(QuicAckFrame* ack_frame,
238 QuicPacketSequenceNumber lower,
239 QuicPacketSequenceNumber higher) {
240 for (QuicPacketSequenceNumber i = lower; i < higher; ++i) {
241 ack_frame->missing_packets.insert(i);
245 QuicStopWaitingFrame::QuicStopWaitingFrame()
246 : entropy_hash(0),
247 least_unacked(0) {
250 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
252 QuicAckFrame::QuicAckFrame()
253 : entropy_hash(0),
254 largest_observed(0),
255 delta_time_largest_observed(QuicTime::Delta::Infinite()),
256 is_truncated(false) {}
258 QuicAckFrame::~QuicAckFrame() {}
260 QuicRstStreamErrorCode AdjustErrorForVersion(
261 QuicRstStreamErrorCode error_code,
262 QuicVersion version) {
263 return error_code;
266 QuicRstStreamFrame::QuicRstStreamFrame()
267 : stream_id(0),
268 error_code(QUIC_STREAM_NO_ERROR) {
271 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id,
272 QuicRstStreamErrorCode error_code,
273 QuicStreamOffset bytes_written)
274 : stream_id(stream_id),
275 error_code(error_code),
276 byte_offset(bytes_written) {
277 DCHECK_LE(error_code, numeric_limits<uint8>::max());
280 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
281 : error_code(QUIC_NO_ERROR) {
284 QuicFrame::QuicFrame() {}
286 QuicFrame::QuicFrame(QuicPaddingFrame* padding_frame)
287 : type(PADDING_FRAME),
288 padding_frame(padding_frame) {
291 QuicFrame::QuicFrame(QuicStreamFrame* stream_frame)
292 : type(STREAM_FRAME),
293 stream_frame(stream_frame) {
296 QuicFrame::QuicFrame(QuicAckFrame* frame)
297 : type(ACK_FRAME),
298 ack_frame(frame) {
301 QuicFrame::QuicFrame(QuicStopWaitingFrame* frame)
302 : type(STOP_WAITING_FRAME),
303 stop_waiting_frame(frame) {
306 QuicFrame::QuicFrame(QuicPingFrame* frame)
307 : type(PING_FRAME),
308 ping_frame(frame) {
311 QuicFrame::QuicFrame(QuicRstStreamFrame* frame)
312 : type(RST_STREAM_FRAME),
313 rst_stream_frame(frame) {
316 QuicFrame::QuicFrame(QuicConnectionCloseFrame* frame)
317 : type(CONNECTION_CLOSE_FRAME),
318 connection_close_frame(frame) {
321 QuicFrame::QuicFrame(QuicGoAwayFrame* frame)
322 : type(GOAWAY_FRAME),
323 goaway_frame(frame) {
326 QuicFrame::QuicFrame(QuicWindowUpdateFrame* frame)
327 : type(WINDOW_UPDATE_FRAME),
328 window_update_frame(frame) {
331 QuicFrame::QuicFrame(QuicBlockedFrame* frame)
332 : type(BLOCKED_FRAME),
333 blocked_frame(frame) {
336 QuicFecData::QuicFecData() : fec_group(0) {}
338 ostream& operator<<(ostream& os, const QuicStopWaitingFrame& sent_info) {
339 os << "entropy_hash: " << static_cast<int>(sent_info.entropy_hash)
340 << " least_unacked: " << sent_info.least_unacked;
341 return os;
344 ostream& operator<<(ostream& os, const QuicAckFrame& ack_frame) {
345 os << "entropy_hash: " << static_cast<int>(ack_frame.entropy_hash)
346 << " largest_observed: " << ack_frame.largest_observed
347 << " delta_time_largest_observed: "
348 << ack_frame.delta_time_largest_observed.ToMicroseconds()
349 << " missing_packets: [ ";
350 for (SequenceNumberSet::const_iterator it = ack_frame.missing_packets.begin();
351 it != ack_frame.missing_packets.end(); ++it) {
352 os << *it << " ";
354 os << " ] is_truncated: " << ack_frame.is_truncated;
355 os << " revived_packets: [ ";
356 for (SequenceNumberSet::const_iterator it = ack_frame.revived_packets.begin();
357 it != ack_frame.revived_packets.end(); ++it) {
358 os << *it << " ";
360 os << " ] received_packets: [ ";
361 for (PacketTimeList::const_iterator it =
362 ack_frame.received_packet_times.begin();
363 it != ack_frame.received_packet_times.end(); ++it) {
364 os << it->first << " at " << it->second.ToDebuggingValue() << " ";
366 os << " ]";
367 return os;
370 ostream& operator<<(ostream& os, const QuicFrame& frame) {
371 switch (frame.type) {
372 case PADDING_FRAME: {
373 os << "type { PADDING_FRAME } ";
374 break;
376 case RST_STREAM_FRAME: {
377 os << "type { " << RST_STREAM_FRAME << " } " << *(frame.rst_stream_frame);
378 break;
380 case CONNECTION_CLOSE_FRAME: {
381 os << "type { CONNECTION_CLOSE_FRAME } "
382 << *(frame.connection_close_frame);
383 break;
385 case GOAWAY_FRAME: {
386 os << "type { GOAWAY_FRAME } " << *(frame.goaway_frame);
387 break;
389 case WINDOW_UPDATE_FRAME: {
390 os << "type { WINDOW_UPDATE_FRAME } " << *(frame.window_update_frame);
391 break;
393 case BLOCKED_FRAME: {
394 os << "type { BLOCKED_FRAME } " << *(frame.blocked_frame);
395 break;
397 case STREAM_FRAME: {
398 os << "type { STREAM_FRAME } " << *(frame.stream_frame);
399 break;
401 case ACK_FRAME: {
402 os << "type { ACK_FRAME } " << *(frame.ack_frame);
403 break;
405 case STOP_WAITING_FRAME: {
406 os << "type { STOP_WAITING_FRAME } " << *(frame.stop_waiting_frame);
407 break;
409 case PING_FRAME: {
410 os << "type { PING_FRAME } ";
411 break;
413 default: {
414 LOG(ERROR) << "Unknown frame type: " << frame.type;
415 break;
418 return os;
421 ostream& operator<<(ostream& os, const QuicRstStreamFrame& rst_frame) {
422 os << "stream_id { " << rst_frame.stream_id << " } "
423 << "error_code { " << rst_frame.error_code << " } "
424 << "error_details { " << rst_frame.error_details << " }\n";
425 return os;
428 ostream& operator<<(ostream& os,
429 const QuicConnectionCloseFrame& connection_close_frame) {
430 os << "error_code { " << connection_close_frame.error_code << " } "
431 << "error_details { " << connection_close_frame.error_details << " }\n";
432 return os;
435 ostream& operator<<(ostream& os, const QuicGoAwayFrame& goaway_frame) {
436 os << "error_code { " << goaway_frame.error_code << " } "
437 << "last_good_stream_id { " << goaway_frame.last_good_stream_id << " } "
438 << "reason_phrase { " << goaway_frame.reason_phrase << " }\n";
439 return os;
442 ostream& operator<<(ostream& os,
443 const QuicWindowUpdateFrame& window_update_frame) {
444 os << "stream_id { " << window_update_frame.stream_id << " } "
445 << "byte_offset { " << window_update_frame.byte_offset << " }\n";
446 return os;
449 ostream& operator<<(ostream& os, const QuicBlockedFrame& blocked_frame) {
450 os << "stream_id { " << blocked_frame.stream_id << " }\n";
451 return os;
454 ostream& operator<<(ostream& os, const QuicStreamFrame& stream_frame) {
455 os << "stream_id { " << stream_frame.stream_id << " } "
456 << "fin { " << stream_frame.fin << " } "
457 << "offset { " << stream_frame.offset << " } "
458 << "data { "
459 << QuicUtils::StringToHexASCIIDump(*(stream_frame.GetDataAsString()))
460 << " }\n";
461 return os;
464 QuicGoAwayFrame::QuicGoAwayFrame()
465 : error_code(QUIC_NO_ERROR),
466 last_good_stream_id(0) {
469 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code,
470 QuicStreamId last_good_stream_id,
471 const string& reason)
472 : error_code(error_code),
473 last_good_stream_id(last_good_stream_id),
474 reason_phrase(reason) {
475 DCHECK_LE(error_code, numeric_limits<uint8>::max());
478 QuicData::QuicData(const char* buffer,
479 size_t length)
480 : buffer_(buffer),
481 length_(length),
482 owns_buffer_(false) {
485 QuicData::QuicData(char* buffer,
486 size_t length,
487 bool owns_buffer)
488 : buffer_(buffer),
489 length_(length),
490 owns_buffer_(owns_buffer) {
493 QuicData::~QuicData() {
494 if (owns_buffer_) {
495 delete [] const_cast<char*>(buffer_);
499 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id,
500 QuicStreamOffset byte_offset)
501 : stream_id(stream_id),
502 byte_offset(byte_offset) {}
504 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id)
505 : stream_id(stream_id) {}
507 QuicPacket::QuicPacket(char* buffer,
508 size_t length,
509 bool owns_buffer,
510 QuicConnectionIdLength connection_id_length,
511 bool includes_version,
512 QuicSequenceNumberLength sequence_number_length)
513 : QuicData(buffer, length, owns_buffer),
514 buffer_(buffer),
515 connection_id_length_(connection_id_length),
516 includes_version_(includes_version),
517 sequence_number_length_(sequence_number_length) {
520 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer,
521 size_t length)
522 : QuicData(buffer, length) {
525 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer,
526 size_t length,
527 bool owns_buffer)
528 : QuicData(buffer, length, owns_buffer) {
531 StringPiece QuicPacket::FecProtectedData() const {
532 const size_t start_of_fec = GetStartOfFecProtectedData(
533 connection_id_length_, includes_version_, sequence_number_length_);
534 return StringPiece(data() + start_of_fec, length() - start_of_fec);
537 StringPiece QuicPacket::AssociatedData() const {
538 return StringPiece(
539 data() + kStartOfHashData,
540 GetStartOfEncryptedData(
541 connection_id_length_, includes_version_, sequence_number_length_) -
542 kStartOfHashData);
545 StringPiece QuicPacket::BeforePlaintext() const {
546 return StringPiece(data(), GetStartOfEncryptedData(connection_id_length_,
547 includes_version_,
548 sequence_number_length_));
551 StringPiece QuicPacket::Plaintext() const {
552 const size_t start_of_encrypted_data =
553 GetStartOfEncryptedData(
554 connection_id_length_, includes_version_, sequence_number_length_);
555 return StringPiece(data() + start_of_encrypted_data,
556 length() - start_of_encrypted_data);
559 RetransmittableFrames::RetransmittableFrames(EncryptionLevel level)
560 : encryption_level_(level), has_crypto_handshake_(NOT_HANDSHAKE) {
563 RetransmittableFrames::~RetransmittableFrames() {
564 for (QuicFrames::iterator it = frames_.begin(); it != frames_.end(); ++it) {
565 switch (it->type) {
566 case PADDING_FRAME:
567 delete it->padding_frame;
568 break;
569 case STREAM_FRAME:
570 delete it->stream_frame;
571 break;
572 case ACK_FRAME:
573 delete it->ack_frame;
574 break;
575 case STOP_WAITING_FRAME:
576 delete it->stop_waiting_frame;
577 break;
578 case PING_FRAME:
579 delete it->ping_frame;
580 break;
581 case RST_STREAM_FRAME:
582 delete it->rst_stream_frame;
583 break;
584 case CONNECTION_CLOSE_FRAME:
585 delete it->connection_close_frame;
586 break;
587 case GOAWAY_FRAME:
588 delete it->goaway_frame;
589 break;
590 case WINDOW_UPDATE_FRAME:
591 delete it->window_update_frame;
592 break;
593 case BLOCKED_FRAME:
594 delete it->blocked_frame;
595 break;
596 case NUM_FRAME_TYPES:
597 DCHECK(false) << "Cannot delete type: " << it->type;
600 STLDeleteElements(&stream_data_);
603 const QuicFrame& RetransmittableFrames::AddStreamFrame(
604 QuicStreamFrame* stream_frame) {
605 // Make an owned copy of the stream frame's data.
606 stream_data_.push_back(stream_frame->GetDataAsString());
607 // Ensure the stream frame's IOVector points to the owned copy of the data.
608 stream_frame->data.Clear();
609 stream_frame->data.Append(const_cast<char*>(stream_data_.back()->data()),
610 stream_data_.back()->size());
611 frames_.push_back(QuicFrame(stream_frame));
612 if (stream_frame->stream_id == kCryptoStreamId) {
613 has_crypto_handshake_ = IS_HANDSHAKE;
615 return frames_.back();
618 const QuicFrame& RetransmittableFrames::AddNonStreamFrame(
619 const QuicFrame& frame) {
620 DCHECK_NE(frame.type, STREAM_FRAME);
621 frames_.push_back(frame);
622 return frames_.back();
625 SerializedPacket::SerializedPacket(
626 QuicPacketSequenceNumber sequence_number,
627 QuicSequenceNumberLength sequence_number_length,
628 QuicEncryptedPacket* packet,
629 QuicPacketEntropyHash entropy_hash,
630 RetransmittableFrames* retransmittable_frames)
631 : sequence_number(sequence_number),
632 sequence_number_length(sequence_number_length),
633 packet(packet),
634 entropy_hash(entropy_hash),
635 retransmittable_frames(retransmittable_frames),
636 is_fec_packet(false) {
639 SerializedPacket::~SerializedPacket() {}
641 QuicEncryptedPacket* QuicEncryptedPacket::Clone() const {
642 char* buffer = new char[this->length()];
643 memcpy(buffer, this->data(), this->length());
644 return new QuicEncryptedPacket(buffer, this->length(), true);
647 ostream& operator<<(ostream& os, const QuicEncryptedPacket& s) {
648 os << s.length() << "-byte data";
649 return os;
652 TransmissionInfo::TransmissionInfo()
653 : retransmittable_frames(nullptr),
654 sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER),
655 sent_time(QuicTime::Zero()),
656 bytes_sent(0),
657 nack_count(0),
658 transmission_type(NOT_RETRANSMISSION),
659 all_transmissions(nullptr),
660 in_flight(false),
661 is_unackable(false),
662 is_fec_packet(false) {}
664 TransmissionInfo::TransmissionInfo(
665 RetransmittableFrames* retransmittable_frames,
666 QuicSequenceNumberLength sequence_number_length,
667 TransmissionType transmission_type,
668 QuicTime sent_time)
669 : retransmittable_frames(retransmittable_frames),
670 sequence_number_length(sequence_number_length),
671 sent_time(sent_time),
672 bytes_sent(0),
673 nack_count(0),
674 transmission_type(transmission_type),
675 all_transmissions(nullptr),
676 in_flight(false),
677 is_unackable(false),
678 is_fec_packet(false) {}
680 } // namespace net