Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / quic / quic_protocol.cc
blobff8e944970c8893a6443d845e6a6d7374be92cea
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() : 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 IOVector data)
118 : stream_id(stream_id), fin(fin), offset(offset), data(data) {
121 string* QuicStreamFrame::GetDataAsString() const {
122 string* data_string = new string();
123 data_string->reserve(data.TotalBufferSize());
124 for (size_t i = 0; i < data.Size(); ++i) {
125 data_string->append(static_cast<char*>(data.iovec()[i].iov_base),
126 data.iovec()[i].iov_len);
128 DCHECK_EQ(data_string->size(), data.TotalBufferSize());
129 return data_string;
132 uint32 MakeQuicTag(char a, char b, char c, char d) {
133 return static_cast<uint32>(a) |
134 static_cast<uint32>(b) << 8 |
135 static_cast<uint32>(c) << 16 |
136 static_cast<uint32>(d) << 24;
139 bool ContainsQuicTag(const QuicTagVector& tag_vector, QuicTag tag) {
140 return std::find(tag_vector.begin(), tag_vector.end(), tag)
141 != tag_vector.end();
144 QuicVersionVector QuicSupportedVersions() {
145 QuicVersionVector supported_versions;
146 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
147 supported_versions.push_back(kSupportedQuicVersions[i]);
149 return supported_versions;
152 QuicTag QuicVersionToQuicTag(const QuicVersion version) {
153 switch (version) {
154 case QUIC_VERSION_23:
155 return MakeQuicTag('Q', '0', '2', '3');
156 case QUIC_VERSION_24:
157 return MakeQuicTag('Q', '0', '2', '4');
158 case QUIC_VERSION_25:
159 return MakeQuicTag('Q', '0', '2', '5');
160 default:
161 // This shold be an ERROR because we should never attempt to convert an
162 // invalid QuicVersion to be written to the wire.
163 LOG(ERROR) << "Unsupported QuicVersion: " << version;
164 return 0;
168 QuicVersion QuicTagToQuicVersion(const QuicTag version_tag) {
169 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
170 if (version_tag == QuicVersionToQuicTag(kSupportedQuicVersions[i])) {
171 return kSupportedQuicVersions[i];
174 // Reading from the client so this should not be considered an ERROR.
175 DVLOG(1) << "Unsupported QuicTag version: "
176 << QuicUtils::TagToString(version_tag);
177 return QUIC_VERSION_UNSUPPORTED;
180 #define RETURN_STRING_LITERAL(x) \
181 case x: \
182 return #x
184 string QuicVersionToString(const QuicVersion version) {
185 switch (version) {
186 RETURN_STRING_LITERAL(QUIC_VERSION_23);
187 RETURN_STRING_LITERAL(QUIC_VERSION_24);
188 RETURN_STRING_LITERAL(QUIC_VERSION_25);
189 default:
190 return "QUIC_VERSION_UNSUPPORTED";
194 string QuicVersionVectorToString(const QuicVersionVector& versions) {
195 string result = "";
196 for (size_t i = 0; i < versions.size(); ++i) {
197 if (i != 0) {
198 result.append(",");
200 result.append(QuicVersionToString(versions[i]));
202 return result;
205 ostream& operator<<(ostream& os, const Perspective& s) {
206 if (s == Perspective::IS_SERVER) {
207 os << "IS_SERVER";
208 } else {
209 os << "IS_CLIENT";
211 return os;
214 ostream& operator<<(ostream& os, const QuicPacketHeader& header) {
215 os << "{ connection_id: " << header.public_header.connection_id
216 << ", connection_id_length:" << header.public_header.connection_id_length
217 << ", sequence_number_length:"
218 << header.public_header.sequence_number_length
219 << ", reset_flag: " << header.public_header.reset_flag
220 << ", version_flag: " << header.public_header.version_flag;
221 if (header.public_header.version_flag) {
222 os << " version: ";
223 for (size_t i = 0; i < header.public_header.versions.size(); ++i) {
224 os << header.public_header.versions[i] << " ";
227 os << ", fec_flag: " << header.fec_flag
228 << ", entropy_flag: " << header.entropy_flag
229 << ", entropy hash: " << static_cast<int>(header.entropy_hash)
230 << ", sequence_number: " << header.packet_sequence_number
231 << ", is_in_fec_group:" << header.is_in_fec_group
232 << ", fec_group: " << header.fec_group<< "}\n";
233 return os;
236 bool IsAwaitingPacket(const QuicAckFrame& ack_frame,
237 QuicPacketSequenceNumber sequence_number) {
238 return sequence_number > ack_frame.largest_observed ||
239 ContainsKey(ack_frame.missing_packets, sequence_number);
242 void InsertMissingPacketsBetween(QuicAckFrame* ack_frame,
243 QuicPacketSequenceNumber lower,
244 QuicPacketSequenceNumber higher) {
245 for (QuicPacketSequenceNumber i = lower; i < higher; ++i) {
246 ack_frame->missing_packets.insert(i);
250 QuicStopWaitingFrame::QuicStopWaitingFrame()
251 : entropy_hash(0),
252 least_unacked(0) {
255 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
257 QuicAckFrame::QuicAckFrame()
258 : entropy_hash(0),
259 largest_observed(0),
260 delta_time_largest_observed(QuicTime::Delta::Infinite()),
261 is_truncated(false) {}
263 QuicAckFrame::~QuicAckFrame() {}
265 QuicRstStreamErrorCode AdjustErrorForVersion(
266 QuicRstStreamErrorCode error_code,
267 QuicVersion version) {
268 return error_code;
271 QuicRstStreamFrame::QuicRstStreamFrame()
272 : stream_id(0),
273 error_code(QUIC_STREAM_NO_ERROR) {
276 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id,
277 QuicRstStreamErrorCode error_code,
278 QuicStreamOffset bytes_written)
279 : stream_id(stream_id),
280 error_code(error_code),
281 byte_offset(bytes_written) {
282 DCHECK_LE(error_code, numeric_limits<uint8>::max());
285 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
286 : error_code(QUIC_NO_ERROR) {
289 QuicFrame::QuicFrame() {}
291 QuicFrame::QuicFrame(QuicPaddingFrame* padding_frame)
292 : type(PADDING_FRAME),
293 padding_frame(padding_frame) {
296 QuicFrame::QuicFrame(QuicStreamFrame* stream_frame)
297 : type(STREAM_FRAME),
298 stream_frame(stream_frame) {
301 QuicFrame::QuicFrame(QuicAckFrame* frame)
302 : type(ACK_FRAME),
303 ack_frame(frame) {
306 QuicFrame::QuicFrame(QuicStopWaitingFrame* frame)
307 : type(STOP_WAITING_FRAME),
308 stop_waiting_frame(frame) {
311 QuicFrame::QuicFrame(QuicPingFrame* frame)
312 : type(PING_FRAME),
313 ping_frame(frame) {
316 QuicFrame::QuicFrame(QuicRstStreamFrame* frame)
317 : type(RST_STREAM_FRAME),
318 rst_stream_frame(frame) {
321 QuicFrame::QuicFrame(QuicConnectionCloseFrame* frame)
322 : type(CONNECTION_CLOSE_FRAME),
323 connection_close_frame(frame) {
326 QuicFrame::QuicFrame(QuicGoAwayFrame* frame)
327 : type(GOAWAY_FRAME),
328 goaway_frame(frame) {
331 QuicFrame::QuicFrame(QuicWindowUpdateFrame* frame)
332 : type(WINDOW_UPDATE_FRAME),
333 window_update_frame(frame) {
336 QuicFrame::QuicFrame(QuicBlockedFrame* frame)
337 : type(BLOCKED_FRAME),
338 blocked_frame(frame) {
341 QuicFecData::QuicFecData() : fec_group(0) {}
343 ostream& operator<<(ostream& os, const QuicStopWaitingFrame& sent_info) {
344 os << "entropy_hash: " << static_cast<int>(sent_info.entropy_hash)
345 << " least_unacked: " << sent_info.least_unacked;
346 return os;
349 ostream& operator<<(ostream& os, const QuicAckFrame& ack_frame) {
350 os << "entropy_hash: " << static_cast<int>(ack_frame.entropy_hash)
351 << " largest_observed: " << ack_frame.largest_observed
352 << " delta_time_largest_observed: "
353 << ack_frame.delta_time_largest_observed.ToMicroseconds()
354 << " missing_packets: [ ";
355 for (SequenceNumberSet::const_iterator it = ack_frame.missing_packets.begin();
356 it != ack_frame.missing_packets.end(); ++it) {
357 os << *it << " ";
359 os << " ] is_truncated: " << ack_frame.is_truncated;
360 os << " revived_packets: [ ";
361 for (SequenceNumberSet::const_iterator it = ack_frame.revived_packets.begin();
362 it != ack_frame.revived_packets.end(); ++it) {
363 os << *it << " ";
365 os << " ] received_packets: [ ";
366 for (PacketTimeList::const_iterator it =
367 ack_frame.received_packet_times.begin();
368 it != ack_frame.received_packet_times.end(); ++it) {
369 os << it->first << " at " << it->second.ToDebuggingValue() << " ";
371 os << " ]";
372 return os;
375 ostream& operator<<(ostream& os, const QuicFrame& frame) {
376 switch (frame.type) {
377 case PADDING_FRAME: {
378 os << "type { PADDING_FRAME } ";
379 break;
381 case RST_STREAM_FRAME: {
382 os << "type { RST_STREAM_FRAME } " << *(frame.rst_stream_frame);
383 break;
385 case CONNECTION_CLOSE_FRAME: {
386 os << "type { CONNECTION_CLOSE_FRAME } "
387 << *(frame.connection_close_frame);
388 break;
390 case GOAWAY_FRAME: {
391 os << "type { GOAWAY_FRAME } " << *(frame.goaway_frame);
392 break;
394 case WINDOW_UPDATE_FRAME: {
395 os << "type { WINDOW_UPDATE_FRAME } " << *(frame.window_update_frame);
396 break;
398 case BLOCKED_FRAME: {
399 os << "type { BLOCKED_FRAME } " << *(frame.blocked_frame);
400 break;
402 case STREAM_FRAME: {
403 os << "type { STREAM_FRAME } " << *(frame.stream_frame);
404 break;
406 case ACK_FRAME: {
407 os << "type { ACK_FRAME } " << *(frame.ack_frame);
408 break;
410 case STOP_WAITING_FRAME: {
411 os << "type { STOP_WAITING_FRAME } " << *(frame.stop_waiting_frame);
412 break;
414 case PING_FRAME: {
415 os << "type { PING_FRAME } ";
416 break;
418 default: {
419 LOG(ERROR) << "Unknown frame type: " << frame.type;
420 break;
423 return os;
426 ostream& operator<<(ostream& os, const QuicRstStreamFrame& rst_frame) {
427 os << "stream_id { " << rst_frame.stream_id << " } "
428 << "error_code { " << rst_frame.error_code << " } "
429 << "error_details { " << rst_frame.error_details << " }\n";
430 return os;
433 ostream& operator<<(ostream& os,
434 const QuicConnectionCloseFrame& connection_close_frame) {
435 os << "error_code { " << connection_close_frame.error_code << " } "
436 << "error_details { " << connection_close_frame.error_details << " }\n";
437 return os;
440 ostream& operator<<(ostream& os, const QuicGoAwayFrame& goaway_frame) {
441 os << "error_code { " << goaway_frame.error_code << " } "
442 << "last_good_stream_id { " << goaway_frame.last_good_stream_id << " } "
443 << "reason_phrase { " << goaway_frame.reason_phrase << " }\n";
444 return os;
447 ostream& operator<<(ostream& os,
448 const QuicWindowUpdateFrame& window_update_frame) {
449 os << "stream_id { " << window_update_frame.stream_id << " } "
450 << "byte_offset { " << window_update_frame.byte_offset << " }\n";
451 return os;
454 ostream& operator<<(ostream& os, const QuicBlockedFrame& blocked_frame) {
455 os << "stream_id { " << blocked_frame.stream_id << " }\n";
456 return os;
459 ostream& operator<<(ostream& os, const QuicStreamFrame& stream_frame) {
460 os << "stream_id { " << stream_frame.stream_id << " } "
461 << "fin { " << stream_frame.fin << " } "
462 << "offset { " << stream_frame.offset << " } "
463 << "data { "
464 << QuicUtils::StringToHexASCIIDump(*(stream_frame.GetDataAsString()))
465 << " }\n";
466 return os;
469 QuicGoAwayFrame::QuicGoAwayFrame()
470 : error_code(QUIC_NO_ERROR),
471 last_good_stream_id(0) {
474 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code,
475 QuicStreamId last_good_stream_id,
476 const string& reason)
477 : error_code(error_code),
478 last_good_stream_id(last_good_stream_id),
479 reason_phrase(reason) {
480 DCHECK_LE(error_code, numeric_limits<uint8>::max());
483 QuicData::QuicData(const char* buffer,
484 size_t length)
485 : buffer_(buffer),
486 length_(length),
487 owns_buffer_(false) {
490 QuicData::QuicData(char* buffer,
491 size_t length,
492 bool owns_buffer)
493 : buffer_(buffer),
494 length_(length),
495 owns_buffer_(owns_buffer) {
498 QuicData::~QuicData() {
499 if (owns_buffer_) {
500 delete [] const_cast<char*>(buffer_);
504 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id,
505 QuicStreamOffset byte_offset)
506 : stream_id(stream_id),
507 byte_offset(byte_offset) {}
509 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id)
510 : stream_id(stream_id) {}
512 QuicPacket::QuicPacket(char* buffer,
513 size_t length,
514 bool owns_buffer,
515 QuicConnectionIdLength connection_id_length,
516 bool includes_version,
517 QuicSequenceNumberLength sequence_number_length)
518 : QuicData(buffer, length, owns_buffer),
519 buffer_(buffer),
520 connection_id_length_(connection_id_length),
521 includes_version_(includes_version),
522 sequence_number_length_(sequence_number_length) {
525 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer,
526 size_t length)
527 : QuicData(buffer, length) {
530 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer,
531 size_t length,
532 bool owns_buffer)
533 : QuicData(buffer, length, owns_buffer) {
536 StringPiece QuicPacket::FecProtectedData() const {
537 const size_t start_of_fec = GetStartOfFecProtectedData(
538 connection_id_length_, includes_version_, sequence_number_length_);
539 return StringPiece(data() + start_of_fec, length() - start_of_fec);
542 StringPiece QuicPacket::AssociatedData() const {
543 return StringPiece(
544 data() + kStartOfHashData,
545 GetStartOfEncryptedData(
546 connection_id_length_, includes_version_, sequence_number_length_) -
547 kStartOfHashData);
550 StringPiece QuicPacket::BeforePlaintext() const {
551 return StringPiece(data(), GetStartOfEncryptedData(connection_id_length_,
552 includes_version_,
553 sequence_number_length_));
556 StringPiece QuicPacket::Plaintext() const {
557 const size_t start_of_encrypted_data =
558 GetStartOfEncryptedData(
559 connection_id_length_, includes_version_, sequence_number_length_);
560 return StringPiece(data() + start_of_encrypted_data,
561 length() - start_of_encrypted_data);
564 RetransmittableFrames::RetransmittableFrames(EncryptionLevel level)
565 : encryption_level_(level), has_crypto_handshake_(NOT_HANDSHAKE) {
568 RetransmittableFrames::~RetransmittableFrames() {
569 for (QuicFrames::iterator it = frames_.begin(); it != frames_.end(); ++it) {
570 switch (it->type) {
571 case PADDING_FRAME:
572 delete it->padding_frame;
573 break;
574 case STREAM_FRAME:
575 delete it->stream_frame;
576 break;
577 case ACK_FRAME:
578 delete it->ack_frame;
579 break;
580 case STOP_WAITING_FRAME:
581 delete it->stop_waiting_frame;
582 break;
583 case PING_FRAME:
584 delete it->ping_frame;
585 break;
586 case RST_STREAM_FRAME:
587 delete it->rst_stream_frame;
588 break;
589 case CONNECTION_CLOSE_FRAME:
590 delete it->connection_close_frame;
591 break;
592 case GOAWAY_FRAME:
593 delete it->goaway_frame;
594 break;
595 case WINDOW_UPDATE_FRAME:
596 delete it->window_update_frame;
597 break;
598 case BLOCKED_FRAME:
599 delete it->blocked_frame;
600 break;
601 case NUM_FRAME_TYPES:
602 DCHECK(false) << "Cannot delete type: " << it->type;
605 STLDeleteElements(&stream_data_);
608 const QuicFrame& RetransmittableFrames::AddStreamFrame(
609 QuicStreamFrame* stream_frame) {
610 // Make an owned copy of the stream frame's data.
611 stream_data_.push_back(stream_frame->GetDataAsString());
612 // Ensure the stream frame's IOVector points to the owned copy of the data.
613 stream_frame->data.Clear();
614 stream_frame->data.Append(const_cast<char*>(stream_data_.back()->data()),
615 stream_data_.back()->size());
616 frames_.push_back(QuicFrame(stream_frame));
617 if (stream_frame->stream_id == kCryptoStreamId) {
618 has_crypto_handshake_ = IS_HANDSHAKE;
620 return frames_.back();
623 const QuicFrame& RetransmittableFrames::AddNonStreamFrame(
624 const QuicFrame& frame) {
625 DCHECK_NE(frame.type, STREAM_FRAME);
626 frames_.push_back(frame);
627 return frames_.back();
630 void RetransmittableFrames::RemoveFramesForStream(QuicStreamId stream_id) {
631 QuicFrames::iterator it = frames_.begin();
632 while (it != frames_.end()) {
633 if (it->type != STREAM_FRAME || it->stream_frame->stream_id != stream_id) {
634 ++it;
635 continue;
637 delete it->stream_frame;
638 it = frames_.erase(it);
642 SerializedPacket::SerializedPacket(
643 QuicPacketSequenceNumber sequence_number,
644 QuicSequenceNumberLength sequence_number_length,
645 QuicEncryptedPacket* packet,
646 QuicPacketEntropyHash entropy_hash,
647 RetransmittableFrames* retransmittable_frames)
648 : sequence_number(sequence_number),
649 sequence_number_length(sequence_number_length),
650 packet(packet),
651 entropy_hash(entropy_hash),
652 retransmittable_frames(retransmittable_frames),
653 is_fec_packet(false) {
656 SerializedPacket::~SerializedPacket() {}
658 QuicEncryptedPacket* QuicEncryptedPacket::Clone() const {
659 char* buffer = new char[this->length()];
660 memcpy(buffer, this->data(), this->length());
661 return new QuicEncryptedPacket(buffer, this->length(), true);
664 ostream& operator<<(ostream& os, const QuicEncryptedPacket& s) {
665 os << s.length() << "-byte data";
666 return os;
669 TransmissionInfo::TransmissionInfo()
670 : retransmittable_frames(nullptr),
671 sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER),
672 sent_time(QuicTime::Zero()),
673 bytes_sent(0),
674 nack_count(0),
675 transmission_type(NOT_RETRANSMISSION),
676 all_transmissions(nullptr),
677 in_flight(false),
678 is_unackable(false),
679 is_fec_packet(false) {
682 TransmissionInfo::TransmissionInfo(
683 RetransmittableFrames* retransmittable_frames,
684 QuicSequenceNumberLength sequence_number_length,
685 TransmissionType transmission_type,
686 QuicTime sent_time,
687 QuicByteCount bytes_sent,
688 bool is_fec_packet)
689 : retransmittable_frames(retransmittable_frames),
690 sequence_number_length(sequence_number_length),
691 sent_time(sent_time),
692 bytes_sent(bytes_sent),
693 nack_count(0),
694 transmission_type(transmission_type),
695 all_transmissions(nullptr),
696 in_flight(false),
697 is_unackable(false),
698 is_fec_packet(is_fec_packet) {
701 } // namespace net