• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "net/quic/quic_protocol.h"
6 
7 #include "base/stl_util.h"
8 #include "net/quic/quic_utils.h"
9 
10 using base::StringPiece;
11 using std::map;
12 using std::numeric_limits;
13 using std::ostream;
14 using std::string;
15 
16 namespace net {
17 
GetPacketHeaderSize(const QuicPacketHeader & header)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);
23 }
24 
GetPacketHeaderSize(QuicConnectionIdLength connection_id_length,bool include_version,QuicSequenceNumberLength sequence_number_length,InFecGroup 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);
32 }
33 
GetStartOfFecProtectedData(QuicConnectionIdLength connection_id_length,bool include_version,QuicSequenceNumberLength sequence_number_length)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);
42 }
43 
GetStartOfEncryptedData(QuicConnectionIdLength connection_id_length,bool include_version,QuicSequenceNumberLength sequence_number_length)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;
53 }
54 
QuicPacketPublicHeader()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) {
61 }
62 
QuicPacketPublicHeader(const QuicPacketPublicHeader & other)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) {
71 }
72 
~QuicPacketPublicHeader()73 QuicPacketPublicHeader::~QuicPacketPublicHeader() {}
74 
QuicPacketHeader()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) {
82 }
83 
QuicPacketHeader(const QuicPacketPublicHeader & header)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) {
92 }
93 
QuicPublicResetPacket()94 QuicPublicResetPacket::QuicPublicResetPacket()
95     : nonce_proof(0),
96       rejected_sequence_number(0) {}
97 
QuicPublicResetPacket(const QuicPacketPublicHeader & header)98 QuicPublicResetPacket::QuicPublicResetPacket(
99     const QuicPacketPublicHeader& header)
100     : public_header(header),
101       nonce_proof(0),
102       rejected_sequence_number(0) {}
103 
QuicStreamFrame()104 QuicStreamFrame::QuicStreamFrame()
105     : stream_id(0),
106       fin(false),
107       offset(0),
108       notifier(NULL) {}
109 
QuicStreamFrame(const QuicStreamFrame & frame)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) {
116 }
117 
QuicStreamFrame(QuicStreamId stream_id,bool fin,QuicStreamOffset offset,IOVector data)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(NULL) {
127 }
128 
GetDataAsString() const129 string* QuicStreamFrame::GetDataAsString() const {
130   string* data_string = new string();
131   data_string->reserve(data.TotalBufferSize());
132   for (size_t i = 0; i < data.Size(); ++i) {
133     data_string->append(static_cast<char*>(data.iovec()[i].iov_base),
134                         data.iovec()[i].iov_len);
135   }
136   DCHECK_EQ(data_string->size(), data.TotalBufferSize());
137   return data_string;
138 }
139 
MakeQuicTag(char a,char b,char c,char d)140 uint32 MakeQuicTag(char a, char b, char c, char d) {
141   return static_cast<uint32>(a) |
142          static_cast<uint32>(b) << 8 |
143          static_cast<uint32>(c) << 16 |
144          static_cast<uint32>(d) << 24;
145 }
146 
ContainsQuicTag(const QuicTagVector & tag_vector,QuicTag tag)147 bool ContainsQuicTag(const QuicTagVector& tag_vector, QuicTag tag) {
148   return std::find(tag_vector.begin(), tag_vector.end(),  tag)
149       != tag_vector.end();
150 }
151 
QuicSupportedVersions()152 QuicVersionVector QuicSupportedVersions() {
153   QuicVersionVector supported_versions;
154   for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
155     supported_versions.push_back(kSupportedQuicVersions[i]);
156   }
157   return supported_versions;
158 }
159 
QuicVersionToQuicTag(const QuicVersion version)160 QuicTag QuicVersionToQuicTag(const QuicVersion version) {
161   switch (version) {
162     case QUIC_VERSION_15:
163       return MakeQuicTag('Q', '0', '1', '5');
164     case QUIC_VERSION_16:
165       return MakeQuicTag('Q', '0', '1', '6');
166     case QUIC_VERSION_17:
167       return MakeQuicTag('Q', '0', '1', '7');
168     case QUIC_VERSION_18:
169       return MakeQuicTag('Q', '0', '1', '8');
170     case QUIC_VERSION_19:
171       return MakeQuicTag('Q', '0', '1', '9');
172     case QUIC_VERSION_20:
173       return MakeQuicTag('Q', '0', '2', '0');
174     default:
175       // This shold be an ERROR because we should never attempt to convert an
176       // invalid QuicVersion to be written to the wire.
177       LOG(ERROR) << "Unsupported QuicVersion: " << version;
178       return 0;
179   }
180 }
181 
QuicTagToQuicVersion(const QuicTag version_tag)182 QuicVersion QuicTagToQuicVersion(const QuicTag version_tag) {
183   for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
184     if (version_tag == QuicVersionToQuicTag(kSupportedQuicVersions[i])) {
185       return kSupportedQuicVersions[i];
186     }
187   }
188   // Reading from the client so this should not be considered an ERROR.
189   DVLOG(1) << "Unsupported QuicTag version: "
190            << QuicUtils::TagToString(version_tag);
191   return QUIC_VERSION_UNSUPPORTED;
192 }
193 
194 #define RETURN_STRING_LITERAL(x) \
195 case x: \
196 return #x
197 
QuicVersionToString(const QuicVersion version)198 string QuicVersionToString(const QuicVersion version) {
199   switch (version) {
200     RETURN_STRING_LITERAL(QUIC_VERSION_15);
201     RETURN_STRING_LITERAL(QUIC_VERSION_16);
202     RETURN_STRING_LITERAL(QUIC_VERSION_17);
203     RETURN_STRING_LITERAL(QUIC_VERSION_18);
204     RETURN_STRING_LITERAL(QUIC_VERSION_19);
205     RETURN_STRING_LITERAL(QUIC_VERSION_20);
206     default:
207       return "QUIC_VERSION_UNSUPPORTED";
208   }
209 }
210 
QuicVersionVectorToString(const QuicVersionVector & versions)211 string QuicVersionVectorToString(const QuicVersionVector& versions) {
212   string result = "";
213   for (size_t i = 0; i < versions.size(); ++i) {
214     if (i != 0) {
215       result.append(",");
216     }
217     result.append(QuicVersionToString(versions[i]));
218   }
219   return result;
220 }
221 
operator <<(ostream & os,const QuicPacketHeader & header)222 ostream& operator<<(ostream& os, const QuicPacketHeader& header) {
223   os << "{ connection_id: " << header.public_header.connection_id
224      << ", connection_id_length:" << header.public_header.connection_id_length
225      << ", sequence_number_length:"
226      << header.public_header.sequence_number_length
227      << ", reset_flag: " << header.public_header.reset_flag
228      << ", version_flag: " << header.public_header.version_flag;
229   if (header.public_header.version_flag) {
230     os << " version: ";
231     for (size_t i = 0; i < header.public_header.versions.size(); ++i) {
232       os << header.public_header.versions[0] << " ";
233     }
234   }
235   os << ", fec_flag: " << header.fec_flag
236      << ", entropy_flag: " << header.entropy_flag
237      << ", entropy hash: " << static_cast<int>(header.entropy_hash)
238      << ", sequence_number: " << header.packet_sequence_number
239      << ", is_in_fec_group:" << header.is_in_fec_group
240      << ", fec_group: " << header.fec_group<< "}\n";
241   return os;
242 }
243 
ReceivedPacketInfo()244 ReceivedPacketInfo::ReceivedPacketInfo()
245     : entropy_hash(0),
246       largest_observed(0),
247       delta_time_largest_observed(QuicTime::Delta::Infinite()),
248       is_truncated(false) {}
249 
~ReceivedPacketInfo()250 ReceivedPacketInfo::~ReceivedPacketInfo() {}
251 
IsAwaitingPacket(const ReceivedPacketInfo & received_info,QuicPacketSequenceNumber sequence_number)252 bool IsAwaitingPacket(const ReceivedPacketInfo& received_info,
253                       QuicPacketSequenceNumber sequence_number) {
254   return sequence_number > received_info.largest_observed ||
255       ContainsKey(received_info.missing_packets, sequence_number);
256 }
257 
InsertMissingPacketsBetween(ReceivedPacketInfo * received_info,QuicPacketSequenceNumber lower,QuicPacketSequenceNumber higher)258 void InsertMissingPacketsBetween(ReceivedPacketInfo* received_info,
259                                  QuicPacketSequenceNumber lower,
260                                  QuicPacketSequenceNumber higher) {
261   for (QuicPacketSequenceNumber i = lower; i < higher; ++i) {
262     received_info->missing_packets.insert(i);
263   }
264 }
265 
QuicStopWaitingFrame()266 QuicStopWaitingFrame::QuicStopWaitingFrame()
267     : entropy_hash(0),
268       least_unacked(0) {
269 }
270 
~QuicStopWaitingFrame()271 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
272 
QuicAckFrame()273 QuicAckFrame::QuicAckFrame() {}
274 
CongestionFeedbackMessageTCP()275 CongestionFeedbackMessageTCP::CongestionFeedbackMessageTCP()
276     : receive_window(0) {
277 }
278 
CongestionFeedbackMessageInterArrival()279 CongestionFeedbackMessageInterArrival::CongestionFeedbackMessageInterArrival() {
280 }
281 
282 CongestionFeedbackMessageInterArrival::
~CongestionFeedbackMessageInterArrival()283     ~CongestionFeedbackMessageInterArrival() {}
284 
QuicCongestionFeedbackFrame()285 QuicCongestionFeedbackFrame::QuicCongestionFeedbackFrame() : type(kTCP) {}
286 
~QuicCongestionFeedbackFrame()287 QuicCongestionFeedbackFrame::~QuicCongestionFeedbackFrame() {}
288 
AdjustErrorForVersion(QuicRstStreamErrorCode error_code,QuicVersion version)289 QuicRstStreamErrorCode AdjustErrorForVersion(
290     QuicRstStreamErrorCode error_code,
291     QuicVersion version) {
292   switch (error_code) {
293     case QUIC_RST_FLOW_CONTROL_ACCOUNTING:
294       if (version <= QUIC_VERSION_17) {
295         return QUIC_STREAM_NO_ERROR;
296       }
297       break;
298     default:
299       return error_code;
300   }
301   return error_code;
302 }
303 
QuicRstStreamFrame()304 QuicRstStreamFrame::QuicRstStreamFrame()
305     : stream_id(0),
306       error_code(QUIC_STREAM_NO_ERROR) {
307 }
308 
QuicRstStreamFrame(QuicStreamId stream_id,QuicRstStreamErrorCode error_code,QuicStreamOffset bytes_written)309 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id,
310                                        QuicRstStreamErrorCode error_code,
311                                        QuicStreamOffset bytes_written)
312     : stream_id(stream_id),
313       error_code(error_code),
314       byte_offset(bytes_written) {
315   DCHECK_LE(error_code, numeric_limits<uint8>::max());
316 }
317 
QuicConnectionCloseFrame()318 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
319     : error_code(QUIC_NO_ERROR) {
320 }
321 
QuicFrame()322 QuicFrame::QuicFrame() {}
323 
QuicFrame(QuicPaddingFrame * padding_frame)324 QuicFrame::QuicFrame(QuicPaddingFrame* padding_frame)
325     : type(PADDING_FRAME),
326       padding_frame(padding_frame) {
327 }
328 
QuicFrame(QuicStreamFrame * stream_frame)329 QuicFrame::QuicFrame(QuicStreamFrame* stream_frame)
330     : type(STREAM_FRAME),
331       stream_frame(stream_frame) {
332 }
333 
QuicFrame(QuicAckFrame * frame)334 QuicFrame::QuicFrame(QuicAckFrame* frame)
335     : type(ACK_FRAME),
336       ack_frame(frame) {
337 }
338 
QuicFrame(QuicCongestionFeedbackFrame * frame)339 QuicFrame::QuicFrame(QuicCongestionFeedbackFrame* frame)
340     : type(CONGESTION_FEEDBACK_FRAME),
341       congestion_feedback_frame(frame) {
342 }
343 
QuicFrame(QuicStopWaitingFrame * frame)344 QuicFrame::QuicFrame(QuicStopWaitingFrame* frame)
345     : type(STOP_WAITING_FRAME),
346       stop_waiting_frame(frame) {
347 }
348 
QuicFrame(QuicPingFrame * frame)349 QuicFrame::QuicFrame(QuicPingFrame* frame)
350     : type(PING_FRAME),
351       ping_frame(frame) {
352 }
353 
QuicFrame(QuicRstStreamFrame * frame)354 QuicFrame::QuicFrame(QuicRstStreamFrame* frame)
355     : type(RST_STREAM_FRAME),
356       rst_stream_frame(frame) {
357 }
358 
QuicFrame(QuicConnectionCloseFrame * frame)359 QuicFrame::QuicFrame(QuicConnectionCloseFrame* frame)
360     : type(CONNECTION_CLOSE_FRAME),
361       connection_close_frame(frame) {
362 }
363 
QuicFrame(QuicGoAwayFrame * frame)364 QuicFrame::QuicFrame(QuicGoAwayFrame* frame)
365     : type(GOAWAY_FRAME),
366       goaway_frame(frame) {
367 }
368 
QuicFrame(QuicWindowUpdateFrame * frame)369 QuicFrame::QuicFrame(QuicWindowUpdateFrame* frame)
370     : type(WINDOW_UPDATE_FRAME),
371       window_update_frame(frame) {
372 }
373 
QuicFrame(QuicBlockedFrame * frame)374 QuicFrame::QuicFrame(QuicBlockedFrame* frame)
375     : type(BLOCKED_FRAME),
376       blocked_frame(frame) {
377 }
378 
QuicFecData()379 QuicFecData::QuicFecData() : fec_group(0) {}
380 
operator <<(ostream & os,const QuicStopWaitingFrame & sent_info)381 ostream& operator<<(ostream& os, const QuicStopWaitingFrame& sent_info) {
382   os << "entropy_hash: " << static_cast<int>(sent_info.entropy_hash)
383      << " least_unacked: " << sent_info.least_unacked;
384   return os;
385 }
386 
operator <<(ostream & os,const ReceivedPacketInfo & received_info)387 ostream& operator<<(ostream& os, const ReceivedPacketInfo& received_info) {
388   os << "entropy_hash: " << static_cast<int>(received_info.entropy_hash)
389      << " is_truncated: " << received_info.is_truncated
390      << " largest_observed: " << received_info.largest_observed
391      << " delta_time_largest_observed: "
392      << received_info.delta_time_largest_observed.ToMicroseconds()
393      << " missing_packets: [ ";
394   for (SequenceNumberSet::const_iterator it =
395            received_info.missing_packets.begin();
396        it != received_info.missing_packets.end(); ++it) {
397     os << *it << " ";
398   }
399   os << " ] revived_packets: [ ";
400   for (SequenceNumberSet::const_iterator it =
401            received_info.revived_packets.begin();
402        it != received_info.revived_packets.end(); ++it) {
403     os << *it << " ";
404   }
405   os << " ]";
406   return os;
407 }
408 
operator <<(ostream & os,const QuicFrame & frame)409 ostream& operator<<(ostream& os, const QuicFrame& frame) {
410   switch (frame.type) {
411   case PADDING_FRAME: {
412       os << "type { PADDING_FRAME } ";
413       break;
414     }
415     case RST_STREAM_FRAME: {
416       os << "type { " << RST_STREAM_FRAME << " } " << *(frame.rst_stream_frame);
417       break;
418     }
419     case CONNECTION_CLOSE_FRAME: {
420       os << "type { CONNECTION_CLOSE_FRAME } "
421          << *(frame.connection_close_frame);
422       break;
423     }
424     case GOAWAY_FRAME: {
425       os << "type { GOAWAY_FRAME } " << *(frame.goaway_frame);
426       break;
427     }
428     case WINDOW_UPDATE_FRAME: {
429       os << "type { WINDOW_UPDATE_FRAME } " << *(frame.window_update_frame);
430       break;
431     }
432     case BLOCKED_FRAME: {
433       os << "type { BLOCKED_FRAME } " << *(frame.blocked_frame);
434       break;
435     }
436     case STREAM_FRAME: {
437       os << "type { STREAM_FRAME } " << *(frame.stream_frame);
438       break;
439     }
440     case ACK_FRAME: {
441       os << "type { ACK_FRAME } " << *(frame.ack_frame);
442       break;
443     }
444     case CONGESTION_FEEDBACK_FRAME: {
445       os << "type { CONGESTION_FEEDBACK_FRAME } "
446          << *(frame.congestion_feedback_frame);
447       break;
448     }
449     case STOP_WAITING_FRAME: {
450       os << "type { STOP_WAITING_FRAME } " << *(frame.stop_waiting_frame);
451       break;
452     }
453     default: {
454       LOG(ERROR) << "Unknown frame type: " << frame.type;
455       break;
456     }
457   }
458   return os;
459 }
460 
operator <<(ostream & os,const QuicRstStreamFrame & rst_frame)461 ostream& operator<<(ostream& os, const QuicRstStreamFrame& rst_frame) {
462   os << "stream_id { " << rst_frame.stream_id << " } "
463      << "error_code { " << rst_frame.error_code << " } "
464      << "error_details { " << rst_frame.error_details << " }\n";
465   return os;
466 }
467 
operator <<(ostream & os,const QuicConnectionCloseFrame & connection_close_frame)468 ostream& operator<<(ostream& os,
469                     const QuicConnectionCloseFrame& connection_close_frame) {
470   os << "error_code { " << connection_close_frame.error_code << " } "
471      << "error_details { " << connection_close_frame.error_details << " }\n";
472   return os;
473 }
474 
operator <<(ostream & os,const QuicGoAwayFrame & goaway_frame)475 ostream& operator<<(ostream& os, const QuicGoAwayFrame& goaway_frame) {
476   os << "error_code { " << goaway_frame.error_code << " } "
477      << "last_good_stream_id { " << goaway_frame.last_good_stream_id << " } "
478      << "reason_phrase { " << goaway_frame.reason_phrase << " }\n";
479   return os;
480 }
481 
operator <<(ostream & os,const QuicWindowUpdateFrame & window_update_frame)482 ostream& operator<<(ostream& os,
483                     const QuicWindowUpdateFrame& window_update_frame) {
484   os << "stream_id { " << window_update_frame.stream_id << " } "
485      << "byte_offset { " << window_update_frame.byte_offset << " }\n";
486   return os;
487 }
488 
operator <<(ostream & os,const QuicBlockedFrame & blocked_frame)489 ostream& operator<<(ostream& os, const QuicBlockedFrame& blocked_frame) {
490   os << "stream_id { " << blocked_frame.stream_id << " }\n";
491   return os;
492 }
493 
operator <<(ostream & os,const QuicStreamFrame & stream_frame)494 ostream& operator<<(ostream& os, const QuicStreamFrame& stream_frame) {
495   os << "stream_id { " << stream_frame.stream_id << " } "
496      << "fin { " << stream_frame.fin << " } "
497      << "offset { " << stream_frame.offset << " } "
498      << "data { "
499      << QuicUtils::StringToHexASCIIDump(*(stream_frame.GetDataAsString()))
500      << " }\n";
501   return os;
502 }
503 
operator <<(ostream & os,const QuicAckFrame & ack_frame)504 ostream& operator<<(ostream& os, const QuicAckFrame& ack_frame) {
505   os << "sent info { " << ack_frame.sent_info << " } "
506      << "received info { " << ack_frame.received_info << " }\n";
507   return os;
508 }
509 
operator <<(ostream & os,const QuicCongestionFeedbackFrame & congestion_frame)510 ostream& operator<<(ostream& os,
511                     const QuicCongestionFeedbackFrame& congestion_frame) {
512   os << "type: " << congestion_frame.type;
513   switch (congestion_frame.type) {
514     case kInterArrival: {
515       const CongestionFeedbackMessageInterArrival& inter_arrival =
516           congestion_frame.inter_arrival;
517       os << " received packets: [ ";
518       for (TimeMap::const_iterator it =
519                inter_arrival.received_packet_times.begin();
520            it != inter_arrival.received_packet_times.end(); ++it) {
521         os << it->first << "@" << it->second.ToDebuggingValue() << " ";
522       }
523       os << "]";
524       break;
525     }
526     case kFixRate: {
527       os << " bitrate_in_bytes_per_second: "
528          << congestion_frame.fix_rate.bitrate.ToBytesPerSecond();
529       break;
530     }
531     case kTCP: {
532       const CongestionFeedbackMessageTCP& tcp = congestion_frame.tcp;
533       os << " receive_window: " << tcp.receive_window;
534       break;
535     }
536     case kTCPBBR: {
537       LOG(DFATAL) << "TCPBBR is not yet supported.";
538       break;
539     }
540   }
541   return os;
542 }
543 
CongestionFeedbackMessageFixRate()544 CongestionFeedbackMessageFixRate::CongestionFeedbackMessageFixRate()
545     : bitrate(QuicBandwidth::Zero()) {
546 }
547 
QuicGoAwayFrame()548 QuicGoAwayFrame::QuicGoAwayFrame()
549     : error_code(QUIC_NO_ERROR),
550       last_good_stream_id(0) {
551 }
552 
QuicGoAwayFrame(QuicErrorCode error_code,QuicStreamId last_good_stream_id,const string & reason)553 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code,
554                                  QuicStreamId last_good_stream_id,
555                                  const string& reason)
556     : error_code(error_code),
557       last_good_stream_id(last_good_stream_id),
558       reason_phrase(reason) {
559   DCHECK_LE(error_code, numeric_limits<uint8>::max());
560 }
561 
QuicData(const char * buffer,size_t length)562 QuicData::QuicData(const char* buffer,
563                    size_t length)
564     : buffer_(buffer),
565       length_(length),
566       owns_buffer_(false) {
567 }
568 
QuicData(char * buffer,size_t length,bool owns_buffer)569 QuicData::QuicData(char* buffer,
570                    size_t length,
571                    bool owns_buffer)
572     : buffer_(buffer),
573       length_(length),
574       owns_buffer_(owns_buffer) {
575 }
576 
~QuicData()577 QuicData::~QuicData() {
578   if (owns_buffer_) {
579     delete [] const_cast<char*>(buffer_);
580   }
581 }
582 
QuicWindowUpdateFrame(QuicStreamId stream_id,QuicStreamOffset byte_offset)583 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id,
584                                              QuicStreamOffset byte_offset)
585     : stream_id(stream_id),
586       byte_offset(byte_offset) {}
587 
QuicBlockedFrame(QuicStreamId stream_id)588 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id)
589     : stream_id(stream_id) {}
590 
QuicPacket(char * buffer,size_t length,bool owns_buffer,QuicConnectionIdLength connection_id_length,bool includes_version,QuicSequenceNumberLength sequence_number_length,bool is_fec_packet)591 QuicPacket::QuicPacket(char* buffer,
592                        size_t length,
593                        bool owns_buffer,
594                        QuicConnectionIdLength connection_id_length,
595                        bool includes_version,
596                        QuicSequenceNumberLength sequence_number_length,
597                        bool is_fec_packet)
598     : QuicData(buffer, length, owns_buffer),
599       buffer_(buffer),
600       is_fec_packet_(is_fec_packet),
601       connection_id_length_(connection_id_length),
602       includes_version_(includes_version),
603       sequence_number_length_(sequence_number_length) {
604 }
605 
QuicEncryptedPacket(const char * buffer,size_t length)606 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer,
607                                          size_t length)
608     : QuicData(buffer, length) {
609 }
610 
QuicEncryptedPacket(char * buffer,size_t length,bool owns_buffer)611 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer,
612                                          size_t length,
613                                          bool owns_buffer)
614       : QuicData(buffer, length, owns_buffer) {
615 }
616 
FecProtectedData() const617 StringPiece QuicPacket::FecProtectedData() const {
618   const size_t start_of_fec = GetStartOfFecProtectedData(
619       connection_id_length_, includes_version_, sequence_number_length_);
620   return StringPiece(data() + start_of_fec, length() - start_of_fec);
621 }
622 
AssociatedData() const623 StringPiece QuicPacket::AssociatedData() const {
624   return StringPiece(
625       data() + kStartOfHashData,
626       GetStartOfEncryptedData(
627           connection_id_length_, includes_version_, sequence_number_length_) -
628       kStartOfHashData);
629 }
630 
BeforePlaintext() const631 StringPiece QuicPacket::BeforePlaintext() const {
632   return StringPiece(data(), GetStartOfEncryptedData(connection_id_length_,
633                                                      includes_version_,
634                                                      sequence_number_length_));
635 }
636 
Plaintext() const637 StringPiece QuicPacket::Plaintext() const {
638   const size_t start_of_encrypted_data =
639       GetStartOfEncryptedData(
640           connection_id_length_, includes_version_, sequence_number_length_);
641   return StringPiece(data() + start_of_encrypted_data,
642                      length() - start_of_encrypted_data);
643 }
644 
RetransmittableFrames()645 RetransmittableFrames::RetransmittableFrames()
646     : encryption_level_(NUM_ENCRYPTION_LEVELS) {
647 }
648 
~RetransmittableFrames()649 RetransmittableFrames::~RetransmittableFrames() {
650   for (QuicFrames::iterator it = frames_.begin(); it != frames_.end(); ++it) {
651     switch (it->type) {
652       case PADDING_FRAME:
653         delete it->padding_frame;
654         break;
655       case STREAM_FRAME:
656         delete it->stream_frame;
657         break;
658       case ACK_FRAME:
659         delete it->ack_frame;
660         break;
661       case CONGESTION_FEEDBACK_FRAME:
662         delete it->congestion_feedback_frame;
663         break;
664       case STOP_WAITING_FRAME:
665         delete it->stop_waiting_frame;
666         break;
667       case PING_FRAME:
668         delete it->ping_frame;
669         break;
670       case RST_STREAM_FRAME:
671         delete it->rst_stream_frame;
672         break;
673       case CONNECTION_CLOSE_FRAME:
674         delete it->connection_close_frame;
675         break;
676       case GOAWAY_FRAME:
677         delete it->goaway_frame;
678         break;
679       case WINDOW_UPDATE_FRAME:
680         delete it->window_update_frame;
681         break;
682       case BLOCKED_FRAME:
683         delete it->blocked_frame;
684         break;
685       case NUM_FRAME_TYPES:
686         DCHECK(false) << "Cannot delete type: " << it->type;
687     }
688   }
689   STLDeleteElements(&stream_data_);
690 }
691 
AddStreamFrame(QuicStreamFrame * stream_frame)692 const QuicFrame& RetransmittableFrames::AddStreamFrame(
693     QuicStreamFrame* stream_frame) {
694   // Make an owned copy of the stream frame's data.
695   stream_data_.push_back(stream_frame->GetDataAsString());
696   // Ensure the stream frame's IOVector points to the owned copy of the data.
697   stream_frame->data.Clear();
698   stream_frame->data.Append(const_cast<char*>(stream_data_.back()->data()),
699                             stream_data_.back()->size());
700   frames_.push_back(QuicFrame(stream_frame));
701   return frames_.back();
702 }
703 
AddNonStreamFrame(const QuicFrame & frame)704 const QuicFrame& RetransmittableFrames::AddNonStreamFrame(
705     const QuicFrame& frame) {
706   DCHECK_NE(frame.type, STREAM_FRAME);
707   frames_.push_back(frame);
708   return frames_.back();
709 }
710 
HasCryptoHandshake() const711 IsHandshake RetransmittableFrames::HasCryptoHandshake() const {
712   for (size_t i = 0; i < frames().size(); ++i) {
713     if (frames()[i].type == STREAM_FRAME &&
714         frames()[i].stream_frame->stream_id == kCryptoStreamId) {
715       return IS_HANDSHAKE;
716     }
717   }
718   return NOT_HANDSHAKE;
719 }
720 
set_encryption_level(EncryptionLevel level)721 void RetransmittableFrames::set_encryption_level(EncryptionLevel level) {
722   encryption_level_ = level;
723 }
724 
SerializedPacket(QuicPacketSequenceNumber sequence_number,QuicSequenceNumberLength sequence_number_length,QuicPacket * packet,QuicPacketEntropyHash entropy_hash,RetransmittableFrames * retransmittable_frames)725 SerializedPacket::SerializedPacket(
726     QuicPacketSequenceNumber sequence_number,
727     QuicSequenceNumberLength sequence_number_length,
728     QuicPacket* packet,
729     QuicPacketEntropyHash entropy_hash,
730     RetransmittableFrames* retransmittable_frames)
731     : sequence_number(sequence_number),
732       sequence_number_length(sequence_number_length),
733       packet(packet),
734       entropy_hash(entropy_hash),
735       retransmittable_frames(retransmittable_frames) {
736 }
737 
~SerializedPacket()738 SerializedPacket::~SerializedPacket() {}
739 
Clone() const740 QuicEncryptedPacket* QuicEncryptedPacket::Clone() const {
741   char* buffer = new char[this->length()];
742   memcpy(buffer, this->data(), this->length());
743   return new QuicEncryptedPacket(buffer, this->length(), true);
744 }
745 
operator <<(ostream & os,const QuicEncryptedPacket & s)746 ostream& operator<<(ostream& os, const QuicEncryptedPacket& s) {
747   os << s.length() << "-byte data";
748   return os;
749 }
750 
TransmissionInfo()751 TransmissionInfo::TransmissionInfo()
752     : retransmittable_frames(NULL),
753       sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER),
754       sent_time(QuicTime::Zero()),
755       bytes_sent(0),
756       nack_count(0),
757       transmission_type(NOT_RETRANSMISSION),
758       all_transmissions(NULL),
759       in_flight(false) {}
760 
TransmissionInfo(RetransmittableFrames * retransmittable_frames,QuicPacketSequenceNumber sequence_number,QuicSequenceNumberLength sequence_number_length)761 TransmissionInfo::TransmissionInfo(
762     RetransmittableFrames* retransmittable_frames,
763     QuicPacketSequenceNumber sequence_number,
764     QuicSequenceNumberLength sequence_number_length)
765     : retransmittable_frames(retransmittable_frames),
766       sequence_number_length(sequence_number_length),
767       sent_time(QuicTime::Zero()),
768       bytes_sent(0),
769       nack_count(0),
770       transmission_type(NOT_RETRANSMISSION),
771       all_transmissions(new SequenceNumberSet),
772       in_flight(false) {
773   all_transmissions->insert(sequence_number);
774 }
775 
TransmissionInfo(RetransmittableFrames * retransmittable_frames,QuicPacketSequenceNumber sequence_number,QuicSequenceNumberLength sequence_number_length,TransmissionType transmission_type,SequenceNumberSet * all_transmissions)776 TransmissionInfo::TransmissionInfo(
777     RetransmittableFrames* retransmittable_frames,
778     QuicPacketSequenceNumber sequence_number,
779     QuicSequenceNumberLength sequence_number_length,
780     TransmissionType transmission_type,
781     SequenceNumberSet* all_transmissions)
782     : retransmittable_frames(retransmittable_frames),
783       sequence_number_length(sequence_number_length),
784       sent_time(QuicTime::Zero()),
785       bytes_sent(0),
786       nack_count(0),
787       transmission_type(transmission_type),
788       all_transmissions(all_transmissions),
789       in_flight(false) {
790   all_transmissions->insert(sequence_number);
791 }
792 
793 }  // namespace net
794