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_16:
163 return MakeQuicTag('Q', '0', '1', '6');
164 case QUIC_VERSION_18:
165 return MakeQuicTag('Q', '0', '1', '8');
166 case QUIC_VERSION_19:
167 return MakeQuicTag('Q', '0', '1', '9');
168 case QUIC_VERSION_21:
169 return MakeQuicTag('Q', '0', '2', '1');
170 case QUIC_VERSION_22:
171 return MakeQuicTag('Q', '0', '2', '2');
172 case QUIC_VERSION_23:
173 return MakeQuicTag('Q', '0', '2', '3');
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_16);
201 RETURN_STRING_LITERAL(QUIC_VERSION_18);
202 RETURN_STRING_LITERAL(QUIC_VERSION_19);
203 RETURN_STRING_LITERAL(QUIC_VERSION_21);
204 RETURN_STRING_LITERAL(QUIC_VERSION_22);
205 RETURN_STRING_LITERAL(QUIC_VERSION_23);
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
IsAwaitingPacket(const QuicAckFrame & ack_frame,QuicPacketSequenceNumber sequence_number)244 bool IsAwaitingPacket(const QuicAckFrame& ack_frame,
245 QuicPacketSequenceNumber sequence_number) {
246 return sequence_number > ack_frame.largest_observed ||
247 ContainsKey(ack_frame.missing_packets, sequence_number);
248 }
249
InsertMissingPacketsBetween(QuicAckFrame * ack_frame,QuicPacketSequenceNumber lower,QuicPacketSequenceNumber higher)250 void InsertMissingPacketsBetween(QuicAckFrame* ack_frame,
251 QuicPacketSequenceNumber lower,
252 QuicPacketSequenceNumber higher) {
253 for (QuicPacketSequenceNumber i = lower; i < higher; ++i) {
254 ack_frame->missing_packets.insert(i);
255 }
256 }
257
QuicStopWaitingFrame()258 QuicStopWaitingFrame::QuicStopWaitingFrame()
259 : entropy_hash(0),
260 least_unacked(0) {
261 }
262
~QuicStopWaitingFrame()263 QuicStopWaitingFrame::~QuicStopWaitingFrame() {}
264
QuicAckFrame()265 QuicAckFrame::QuicAckFrame()
266 : entropy_hash(0),
267 largest_observed(0),
268 delta_time_largest_observed(QuicTime::Delta::Infinite()),
269 is_truncated(false) {}
270
~QuicAckFrame()271 QuicAckFrame::~QuicAckFrame() {}
272
CongestionFeedbackMessageTCP()273 CongestionFeedbackMessageTCP::CongestionFeedbackMessageTCP()
274 : receive_window(0) {
275 }
276
QuicCongestionFeedbackFrame()277 QuicCongestionFeedbackFrame::QuicCongestionFeedbackFrame() : type(kTCP) {}
278
~QuicCongestionFeedbackFrame()279 QuicCongestionFeedbackFrame::~QuicCongestionFeedbackFrame() {}
280
AdjustErrorForVersion(QuicRstStreamErrorCode error_code,QuicVersion version)281 QuicRstStreamErrorCode AdjustErrorForVersion(
282 QuicRstStreamErrorCode error_code,
283 QuicVersion version) {
284 switch (error_code) {
285 case QUIC_RST_FLOW_CONTROL_ACCOUNTING:
286 if (version < QUIC_VERSION_18) {
287 return QUIC_STREAM_NO_ERROR;
288 }
289 break;
290 default:
291 return error_code;
292 }
293 return error_code;
294 }
295
QuicRstStreamFrame()296 QuicRstStreamFrame::QuicRstStreamFrame()
297 : stream_id(0),
298 error_code(QUIC_STREAM_NO_ERROR) {
299 }
300
QuicRstStreamFrame(QuicStreamId stream_id,QuicRstStreamErrorCode error_code,QuicStreamOffset bytes_written)301 QuicRstStreamFrame::QuicRstStreamFrame(QuicStreamId stream_id,
302 QuicRstStreamErrorCode error_code,
303 QuicStreamOffset bytes_written)
304 : stream_id(stream_id),
305 error_code(error_code),
306 byte_offset(bytes_written) {
307 DCHECK_LE(error_code, numeric_limits<uint8>::max());
308 }
309
QuicConnectionCloseFrame()310 QuicConnectionCloseFrame::QuicConnectionCloseFrame()
311 : error_code(QUIC_NO_ERROR) {
312 }
313
QuicFrame()314 QuicFrame::QuicFrame() {}
315
QuicFrame(QuicPaddingFrame * padding_frame)316 QuicFrame::QuicFrame(QuicPaddingFrame* padding_frame)
317 : type(PADDING_FRAME),
318 padding_frame(padding_frame) {
319 }
320
QuicFrame(QuicStreamFrame * stream_frame)321 QuicFrame::QuicFrame(QuicStreamFrame* stream_frame)
322 : type(STREAM_FRAME),
323 stream_frame(stream_frame) {
324 }
325
QuicFrame(QuicAckFrame * frame)326 QuicFrame::QuicFrame(QuicAckFrame* frame)
327 : type(ACK_FRAME),
328 ack_frame(frame) {
329 }
330
QuicFrame(QuicCongestionFeedbackFrame * frame)331 QuicFrame::QuicFrame(QuicCongestionFeedbackFrame* frame)
332 : type(CONGESTION_FEEDBACK_FRAME),
333 congestion_feedback_frame(frame) {
334 }
335
QuicFrame(QuicStopWaitingFrame * frame)336 QuicFrame::QuicFrame(QuicStopWaitingFrame* frame)
337 : type(STOP_WAITING_FRAME),
338 stop_waiting_frame(frame) {
339 }
340
QuicFrame(QuicPingFrame * frame)341 QuicFrame::QuicFrame(QuicPingFrame* frame)
342 : type(PING_FRAME),
343 ping_frame(frame) {
344 }
345
QuicFrame(QuicRstStreamFrame * frame)346 QuicFrame::QuicFrame(QuicRstStreamFrame* frame)
347 : type(RST_STREAM_FRAME),
348 rst_stream_frame(frame) {
349 }
350
QuicFrame(QuicConnectionCloseFrame * frame)351 QuicFrame::QuicFrame(QuicConnectionCloseFrame* frame)
352 : type(CONNECTION_CLOSE_FRAME),
353 connection_close_frame(frame) {
354 }
355
QuicFrame(QuicGoAwayFrame * frame)356 QuicFrame::QuicFrame(QuicGoAwayFrame* frame)
357 : type(GOAWAY_FRAME),
358 goaway_frame(frame) {
359 }
360
QuicFrame(QuicWindowUpdateFrame * frame)361 QuicFrame::QuicFrame(QuicWindowUpdateFrame* frame)
362 : type(WINDOW_UPDATE_FRAME),
363 window_update_frame(frame) {
364 }
365
QuicFrame(QuicBlockedFrame * frame)366 QuicFrame::QuicFrame(QuicBlockedFrame* frame)
367 : type(BLOCKED_FRAME),
368 blocked_frame(frame) {
369 }
370
QuicFecData()371 QuicFecData::QuicFecData() : fec_group(0) {}
372
operator <<(ostream & os,const QuicStopWaitingFrame & sent_info)373 ostream& operator<<(ostream& os, const QuicStopWaitingFrame& sent_info) {
374 os << "entropy_hash: " << static_cast<int>(sent_info.entropy_hash)
375 << " least_unacked: " << sent_info.least_unacked;
376 return os;
377 }
378
operator <<(ostream & os,const QuicAckFrame & ack_frame)379 ostream& operator<<(ostream& os, const QuicAckFrame& ack_frame) {
380 os << "entropy_hash: " << static_cast<int>(ack_frame.entropy_hash)
381 << " largest_observed: " << ack_frame.largest_observed
382 << " delta_time_largest_observed: "
383 << ack_frame.delta_time_largest_observed.ToMicroseconds()
384 << " missing_packets: [ ";
385 for (SequenceNumberSet::const_iterator it = ack_frame.missing_packets.begin();
386 it != ack_frame.missing_packets.end(); ++it) {
387 os << *it << " ";
388 }
389 os << " ] is_truncated: " << ack_frame.is_truncated;
390 os << " revived_packets: [ ";
391 for (SequenceNumberSet::const_iterator it = ack_frame.revived_packets.begin();
392 it != ack_frame.revived_packets.end(); ++it) {
393 os << *it << " ";
394 }
395 os << " ] received_packets: [ ";
396 for (PacketTimeList::const_iterator it =
397 ack_frame.received_packet_times.begin();
398 it != ack_frame.received_packet_times.end(); ++it) {
399 os << it->first << " at " << it->second.ToDebuggingValue() << " ";
400 }
401 os << " ]";
402 return os;
403 }
404
operator <<(ostream & os,const QuicFrame & frame)405 ostream& operator<<(ostream& os, const QuicFrame& frame) {
406 switch (frame.type) {
407 case PADDING_FRAME: {
408 os << "type { PADDING_FRAME } ";
409 break;
410 }
411 case RST_STREAM_FRAME: {
412 os << "type { " << RST_STREAM_FRAME << " } " << *(frame.rst_stream_frame);
413 break;
414 }
415 case CONNECTION_CLOSE_FRAME: {
416 os << "type { CONNECTION_CLOSE_FRAME } "
417 << *(frame.connection_close_frame);
418 break;
419 }
420 case GOAWAY_FRAME: {
421 os << "type { GOAWAY_FRAME } " << *(frame.goaway_frame);
422 break;
423 }
424 case WINDOW_UPDATE_FRAME: {
425 os << "type { WINDOW_UPDATE_FRAME } " << *(frame.window_update_frame);
426 break;
427 }
428 case BLOCKED_FRAME: {
429 os << "type { BLOCKED_FRAME } " << *(frame.blocked_frame);
430 break;
431 }
432 case STREAM_FRAME: {
433 os << "type { STREAM_FRAME } " << *(frame.stream_frame);
434 break;
435 }
436 case ACK_FRAME: {
437 os << "type { ACK_FRAME } " << *(frame.ack_frame);
438 break;
439 }
440 case CONGESTION_FEEDBACK_FRAME: {
441 os << "type { CONGESTION_FEEDBACK_FRAME } "
442 << *(frame.congestion_feedback_frame);
443 break;
444 }
445 case STOP_WAITING_FRAME: {
446 os << "type { STOP_WAITING_FRAME } " << *(frame.stop_waiting_frame);
447 break;
448 }
449 case PING_FRAME: {
450 os << "type { PING_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 QuicCongestionFeedbackFrame & congestion_frame)504 ostream& operator<<(ostream& os,
505 const QuicCongestionFeedbackFrame& congestion_frame) {
506 os << "type: " << congestion_frame.type;
507 switch (congestion_frame.type) {
508 case kTCP: {
509 const CongestionFeedbackMessageTCP& tcp = congestion_frame.tcp;
510 os << " receive_window: " << tcp.receive_window;
511 break;
512 }
513 }
514 return os;
515 }
516
QuicGoAwayFrame()517 QuicGoAwayFrame::QuicGoAwayFrame()
518 : error_code(QUIC_NO_ERROR),
519 last_good_stream_id(0) {
520 }
521
QuicGoAwayFrame(QuicErrorCode error_code,QuicStreamId last_good_stream_id,const string & reason)522 QuicGoAwayFrame::QuicGoAwayFrame(QuicErrorCode error_code,
523 QuicStreamId last_good_stream_id,
524 const string& reason)
525 : error_code(error_code),
526 last_good_stream_id(last_good_stream_id),
527 reason_phrase(reason) {
528 DCHECK_LE(error_code, numeric_limits<uint8>::max());
529 }
530
QuicData(const char * buffer,size_t length)531 QuicData::QuicData(const char* buffer,
532 size_t length)
533 : buffer_(buffer),
534 length_(length),
535 owns_buffer_(false) {
536 }
537
QuicData(char * buffer,size_t length,bool owns_buffer)538 QuicData::QuicData(char* buffer,
539 size_t length,
540 bool owns_buffer)
541 : buffer_(buffer),
542 length_(length),
543 owns_buffer_(owns_buffer) {
544 }
545
~QuicData()546 QuicData::~QuicData() {
547 if (owns_buffer_) {
548 delete [] const_cast<char*>(buffer_);
549 }
550 }
551
QuicWindowUpdateFrame(QuicStreamId stream_id,QuicStreamOffset byte_offset)552 QuicWindowUpdateFrame::QuicWindowUpdateFrame(QuicStreamId stream_id,
553 QuicStreamOffset byte_offset)
554 : stream_id(stream_id),
555 byte_offset(byte_offset) {}
556
QuicBlockedFrame(QuicStreamId stream_id)557 QuicBlockedFrame::QuicBlockedFrame(QuicStreamId stream_id)
558 : stream_id(stream_id) {}
559
QuicPacket(char * buffer,size_t length,bool owns_buffer,QuicConnectionIdLength connection_id_length,bool includes_version,QuicSequenceNumberLength sequence_number_length,bool is_fec_packet)560 QuicPacket::QuicPacket(char* buffer,
561 size_t length,
562 bool owns_buffer,
563 QuicConnectionIdLength connection_id_length,
564 bool includes_version,
565 QuicSequenceNumberLength sequence_number_length,
566 bool is_fec_packet)
567 : QuicData(buffer, length, owns_buffer),
568 buffer_(buffer),
569 is_fec_packet_(is_fec_packet),
570 connection_id_length_(connection_id_length),
571 includes_version_(includes_version),
572 sequence_number_length_(sequence_number_length) {
573 }
574
QuicEncryptedPacket(const char * buffer,size_t length)575 QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer,
576 size_t length)
577 : QuicData(buffer, length) {
578 }
579
QuicEncryptedPacket(char * buffer,size_t length,bool owns_buffer)580 QuicEncryptedPacket::QuicEncryptedPacket(char* buffer,
581 size_t length,
582 bool owns_buffer)
583 : QuicData(buffer, length, owns_buffer) {
584 }
585
FecProtectedData() const586 StringPiece QuicPacket::FecProtectedData() const {
587 const size_t start_of_fec = GetStartOfFecProtectedData(
588 connection_id_length_, includes_version_, sequence_number_length_);
589 return StringPiece(data() + start_of_fec, length() - start_of_fec);
590 }
591
AssociatedData() const592 StringPiece QuicPacket::AssociatedData() const {
593 return StringPiece(
594 data() + kStartOfHashData,
595 GetStartOfEncryptedData(
596 connection_id_length_, includes_version_, sequence_number_length_) -
597 kStartOfHashData);
598 }
599
BeforePlaintext() const600 StringPiece QuicPacket::BeforePlaintext() const {
601 return StringPiece(data(), GetStartOfEncryptedData(connection_id_length_,
602 includes_version_,
603 sequence_number_length_));
604 }
605
Plaintext() const606 StringPiece QuicPacket::Plaintext() const {
607 const size_t start_of_encrypted_data =
608 GetStartOfEncryptedData(
609 connection_id_length_, includes_version_, sequence_number_length_);
610 return StringPiece(data() + start_of_encrypted_data,
611 length() - start_of_encrypted_data);
612 }
613
RetransmittableFrames()614 RetransmittableFrames::RetransmittableFrames()
615 : encryption_level_(NUM_ENCRYPTION_LEVELS),
616 has_crypto_handshake_(NOT_HANDSHAKE) {
617 }
618
~RetransmittableFrames()619 RetransmittableFrames::~RetransmittableFrames() {
620 for (QuicFrames::iterator it = frames_.begin(); it != frames_.end(); ++it) {
621 switch (it->type) {
622 case PADDING_FRAME:
623 delete it->padding_frame;
624 break;
625 case STREAM_FRAME:
626 delete it->stream_frame;
627 break;
628 case ACK_FRAME:
629 delete it->ack_frame;
630 break;
631 case CONGESTION_FEEDBACK_FRAME:
632 delete it->congestion_feedback_frame;
633 break;
634 case STOP_WAITING_FRAME:
635 delete it->stop_waiting_frame;
636 break;
637 case PING_FRAME:
638 delete it->ping_frame;
639 break;
640 case RST_STREAM_FRAME:
641 delete it->rst_stream_frame;
642 break;
643 case CONNECTION_CLOSE_FRAME:
644 delete it->connection_close_frame;
645 break;
646 case GOAWAY_FRAME:
647 delete it->goaway_frame;
648 break;
649 case WINDOW_UPDATE_FRAME:
650 delete it->window_update_frame;
651 break;
652 case BLOCKED_FRAME:
653 delete it->blocked_frame;
654 break;
655 case NUM_FRAME_TYPES:
656 DCHECK(false) << "Cannot delete type: " << it->type;
657 }
658 }
659 STLDeleteElements(&stream_data_);
660 }
661
AddStreamFrame(QuicStreamFrame * stream_frame)662 const QuicFrame& RetransmittableFrames::AddStreamFrame(
663 QuicStreamFrame* stream_frame) {
664 // Make an owned copy of the stream frame's data.
665 stream_data_.push_back(stream_frame->GetDataAsString());
666 // Ensure the stream frame's IOVector points to the owned copy of the data.
667 stream_frame->data.Clear();
668 stream_frame->data.Append(const_cast<char*>(stream_data_.back()->data()),
669 stream_data_.back()->size());
670 frames_.push_back(QuicFrame(stream_frame));
671 if (stream_frame->stream_id == kCryptoStreamId) {
672 has_crypto_handshake_ = IS_HANDSHAKE;
673 }
674 return frames_.back();
675 }
676
AddNonStreamFrame(const QuicFrame & frame)677 const QuicFrame& RetransmittableFrames::AddNonStreamFrame(
678 const QuicFrame& frame) {
679 DCHECK_NE(frame.type, STREAM_FRAME);
680 frames_.push_back(frame);
681 return frames_.back();
682 }
683
set_encryption_level(EncryptionLevel level)684 void RetransmittableFrames::set_encryption_level(EncryptionLevel level) {
685 encryption_level_ = level;
686 }
687
SerializedPacket(QuicPacketSequenceNumber sequence_number,QuicSequenceNumberLength sequence_number_length,QuicPacket * packet,QuicPacketEntropyHash entropy_hash,RetransmittableFrames * retransmittable_frames)688 SerializedPacket::SerializedPacket(
689 QuicPacketSequenceNumber sequence_number,
690 QuicSequenceNumberLength sequence_number_length,
691 QuicPacket* packet,
692 QuicPacketEntropyHash entropy_hash,
693 RetransmittableFrames* retransmittable_frames)
694 : sequence_number(sequence_number),
695 sequence_number_length(sequence_number_length),
696 packet(packet),
697 entropy_hash(entropy_hash),
698 retransmittable_frames(retransmittable_frames) {
699 }
700
~SerializedPacket()701 SerializedPacket::~SerializedPacket() {}
702
Clone() const703 QuicEncryptedPacket* QuicEncryptedPacket::Clone() const {
704 char* buffer = new char[this->length()];
705 memcpy(buffer, this->data(), this->length());
706 return new QuicEncryptedPacket(buffer, this->length(), true);
707 }
708
operator <<(ostream & os,const QuicEncryptedPacket & s)709 ostream& operator<<(ostream& os, const QuicEncryptedPacket& s) {
710 os << s.length() << "-byte data";
711 return os;
712 }
713
TransmissionInfo()714 TransmissionInfo::TransmissionInfo()
715 : retransmittable_frames(NULL),
716 sequence_number_length(PACKET_1BYTE_SEQUENCE_NUMBER),
717 sent_time(QuicTime::Zero()),
718 bytes_sent(0),
719 nack_count(0),
720 transmission_type(NOT_RETRANSMISSION),
721 all_transmissions(NULL),
722 in_flight(false),
723 is_unackable(false) {}
724
TransmissionInfo(RetransmittableFrames * retransmittable_frames,QuicSequenceNumberLength sequence_number_length)725 TransmissionInfo::TransmissionInfo(
726 RetransmittableFrames* retransmittable_frames,
727 QuicSequenceNumberLength sequence_number_length)
728 : retransmittable_frames(retransmittable_frames),
729 sequence_number_length(sequence_number_length),
730 sent_time(QuicTime::Zero()),
731 bytes_sent(0),
732 nack_count(0),
733 transmission_type(NOT_RETRANSMISSION),
734 all_transmissions(NULL),
735 in_flight(false),
736 is_unackable(false) {}
737
TransmissionInfo(RetransmittableFrames * retransmittable_frames,QuicSequenceNumberLength sequence_number_length,TransmissionType transmission_type,SequenceNumberList * all_transmissions)738 TransmissionInfo::TransmissionInfo(
739 RetransmittableFrames* retransmittable_frames,
740 QuicSequenceNumberLength sequence_number_length,
741 TransmissionType transmission_type,
742 SequenceNumberList* all_transmissions)
743 : retransmittable_frames(retransmittable_frames),
744 sequence_number_length(sequence_number_length),
745 sent_time(QuicTime::Zero()),
746 bytes_sent(0),
747 nack_count(0),
748 transmission_type(transmission_type),
749 all_transmissions(all_transmissions),
750 in_flight(false),
751 is_unackable(false) {}
752
753 } // namespace net
754