quic_protocol.h revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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#ifndef NET_QUIC_QUIC_PROTOCOL_H_
6#define NET_QUIC_QUIC_PROTOCOL_H_
7
8#include <stddef.h>
9#include <limits>
10#include <map>
11#include <ostream>
12#include <set>
13#include <string>
14#include <utility>
15#include <vector>
16
17#include "base/basictypes.h"
18#include "base/hash_tables.h"
19#include "base/logging.h"
20#include "base/strings/string_piece.h"
21#include "net/base/int128.h"
22#include "net/base/net_export.h"
23#include "net/quic/quic_bandwidth.h"
24#include "net/quic/quic_time.h"
25
26namespace net {
27
28using ::operator<<;
29
30class QuicPacket;
31
32typedef uint64 QuicGuid;
33typedef uint32 QuicStreamId;
34typedef uint64 QuicStreamOffset;
35typedef uint64 QuicPacketSequenceNumber;
36typedef QuicPacketSequenceNumber QuicFecGroupNumber;
37typedef uint64 QuicPublicResetNonceProof;
38typedef uint8 QuicPacketEntropyHash;
39typedef uint32 QuicHeaderId;
40// QuicTag is the type of a tag in the wire protocol.
41typedef uint32 QuicTag;
42typedef std::vector<QuicTag> QuicTagVector;
43
44// TODO(rch): Consider Quic specific names for these constants.
45// Maximum size in bytes of a QUIC packet.
46const QuicByteCount kMaxPacketSize = 1200;
47
48// Maximum number of open streams per connection.
49const size_t kDefaultMaxStreamsPerConnection = 100;
50
51// Number of bytes reserved for guid in the packet header.
52const size_t kQuicGuidSize = 8;
53// Number of bytes reserved for public flags in the packet header.
54const size_t kPublicFlagsSize = 1;
55// Number of bytes reserved for version number in the packet header.
56const size_t kQuicVersionSize = 4;
57// Number of bytes reserved for sequence number in the packet header.
58const size_t kSequenceNumberSize = 6;
59// Number of bytes reserved for private flags in the packet header.
60const size_t kPrivateFlagsSize = 1;
61// Number of bytes reserved for FEC group in the packet header.
62const size_t kFecGroupSize = 1;
63// Number of bytes reserved for the nonce proof in public reset packet.
64const size_t kPublicResetNonceSize = 8;
65
66// Signifies that the QuicPacket will contain version of the protocol.
67const bool kIncludeVersion = true;
68
69// Size in bytes of the data or fec packet header.
70NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(bool include_version);
71// Size in bytes of the public reset packet.
72NET_EXPORT_PRIVATE size_t GetPublicResetPacketSize();
73
74// Index of the first byte in a QUIC packet of FEC protected data.
75NET_EXPORT_PRIVATE size_t GetStartOfFecProtectedData(bool include_version);
76// Index of the first byte in a QUIC packet of encrypted data.
77NET_EXPORT_PRIVATE size_t GetStartOfEncryptedData(bool include_version);
78// Returns true if |version| is a supported protocol version.
79NET_EXPORT_PRIVATE bool IsSupportedVersion(QuicTag version);
80
81// Index of the first byte in a QUIC packet which is used in hash calculation.
82const size_t kStartOfHashData = 0;
83
84// Limit on the delta between stream IDs.
85const QuicStreamId kMaxStreamIdDelta = 100;
86// Limit on the delta between header IDs.
87const QuicHeaderId kMaxHeaderIdDelta = 100;
88
89// Reserved ID for the crypto stream.
90// TODO(rch): ensure that this is not usable by any other streams.
91const QuicStreamId kCryptoStreamId = 1;
92
93// Value which indicates this packet is not FEC protected.
94const uint8 kNoFecOffset = 0xFF;
95
96const int64 kDefaultTimeoutUs = 600000000;  // 10 minutes.
97
98enum Retransmission {
99  NOT_RETRANSMISSION,
100  IS_RETRANSMISSION,
101};
102
103enum HasRetransmittableData {
104  NO_RETRANSMITTABLE_DATA,
105  HAS_RETRANSMITTABLE_DATA,
106};
107
108enum QuicFrameType {
109  PADDING_FRAME = 0,
110  STREAM_FRAME,
111  ACK_FRAME,
112  CONGESTION_FEEDBACK_FRAME,
113  RST_STREAM_FRAME,
114  CONNECTION_CLOSE_FRAME,
115  GOAWAY_FRAME,
116  NUM_FRAME_TYPES
117};
118
119enum QuicPacketPublicFlags {
120  PACKET_PUBLIC_FLAGS_NONE = 0,
121  PACKET_PUBLIC_FLAGS_VERSION = 1 << 0,  // Packet header contains version info.
122  PACKET_PUBLIC_FLAGS_RST = 1 << 1,  // Packet is a public reset packet.
123  PACKET_PUBLIC_FLAGS_MAX = (1 << 2) - 1  // All bits set.
124};
125
126enum QuicPacketPrivateFlags {
127  PACKET_PRIVATE_FLAGS_NONE = 0,
128  PACKET_PRIVATE_FLAGS_FEC = 1 << 0,  // Payload is FEC as opposed to frames.
129  PACKET_PRIVATE_FLAGS_ENTROPY = 1 << 1,
130  PACKET_PRIVATE_FLAGS_FEC_ENTROPY = 1 << 2,
131  PACKET_PRIVATE_FLAGS_MAX = (1 << 3) - 1  // All bits set.
132};
133
134enum QuicRstStreamErrorCode {
135  QUIC_STREAM_NO_ERROR = 0,
136
137  // There was some server error which halted stream processing.
138  QUIC_SERVER_ERROR_PROCESSING_STREAM,
139  // We got two fin or reset offsets which did not match.
140  QUIC_MULTIPLE_TERMINATION_OFFSETS,
141  // We got bad payload and can not respond to it at the protocol level.
142  QUIC_BAD_APPLICATION_PAYLOAD,
143  // Stream closed due to connection error. No reset frame is sent when this
144  // happens.
145  QUIC_STREAM_CONNECTION_ERROR,
146  // GoAway frame sent. No more stream can be created.
147  QUIC_STREAM_PEER_GOING_AWAY,
148
149  // No error. Used as bound while iterating.
150  QUIC_STREAM_LAST_ERROR,
151};
152
153enum QuicErrorCode {
154  QUIC_NO_ERROR = 0,
155
156  // Connection has reached an invalid state.
157  QUIC_INTERNAL_ERROR,
158  // There were data frames after the a fin or reset.
159  QUIC_STREAM_DATA_AFTER_TERMINATION,
160  // Control frame is malformed.
161  QUIC_INVALID_PACKET_HEADER,
162  // Frame data is malformed.
163  QUIC_INVALID_FRAME_DATA,
164  // FEC data is malformed.
165  QUIC_INVALID_FEC_DATA,
166  // Stream rst data is malformed
167  QUIC_INVALID_RST_STREAM_DATA,
168  // Connection close data is malformed.
169  QUIC_INVALID_CONNECTION_CLOSE_DATA,
170  // GoAway data is malformed.
171  QUIC_INVALID_GOAWAY_DATA,
172  // Ack data is malformed.
173  QUIC_INVALID_ACK_DATA,
174  // Version negotiation packet is malformed.
175  QUIC_INVALID_VERSION_NEGOTIATION_PACKET,
176  // There was an error decrypting.
177  QUIC_DECRYPTION_FAILURE,
178  // There was an error encrypting.
179  QUIC_ENCRYPTION_FAILURE,
180  // The packet exceeded kMaxPacketSize.
181  QUIC_PACKET_TOO_LARGE,
182  // Data was sent for a stream which did not exist.
183  QUIC_PACKET_FOR_NONEXISTENT_STREAM,
184  // The peer is going away.  May be a client or server.
185  QUIC_PEER_GOING_AWAY,
186  // A stream ID was invalid.
187  QUIC_INVALID_STREAM_ID,
188  // Too many streams already open.
189  QUIC_TOO_MANY_OPEN_STREAMS,
190  // Received public reset for this connection.
191  QUIC_PUBLIC_RESET,
192  // Invalid protocol version.
193  QUIC_INVALID_VERSION,
194  // Stream reset before headers decompressed.
195  QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED,
196  // The Header ID for a stream was too far from the previous.
197  QUIC_INVALID_HEADER_ID,
198
199  // We hit our prenegotiated (or default) timeout
200  QUIC_CONNECTION_TIMED_OUT,
201
202  // Crypto errors.
203
204  // Handshake message contained out of order tags.
205  QUIC_CRYPTO_TAGS_OUT_OF_ORDER,
206  // Handshake message contained too many entries.
207  QUIC_CRYPTO_TOO_MANY_ENTRIES,
208  // Handshake message contained an invalid value length.
209  QUIC_CRYPTO_INVALID_VALUE_LENGTH,
210  // A crypto message was received after the handshake was complete.
211  QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE,
212  // A crypto message was received with an illegal message tag.
213  QUIC_INVALID_CRYPTO_MESSAGE_TYPE,
214  // A crypto message was received with an illegal parameter.
215  QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER,
216  // A crypto message was received with a mandatory parameter missing.
217  QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND,
218  // A crypto message was received with a parameter that has no overlap
219  // with the local parameter.
220  QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP,
221  // A crypto message was received that contained a parameter with too few
222  // values.
223  QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND,
224  // An internal error occured in crypto processing.
225  QUIC_CRYPTO_INTERNAL_ERROR,
226  // A crypto handshake message specified an unsupported version.
227  QUIC_CRYPTO_VERSION_NOT_SUPPORTED,
228  // There was no intersection between the crypto primitives supported by the
229  // peer and ourselves.
230  QUIC_CRYPTO_NO_SUPPORT,
231  // The server rejected our client hello messages too many times.
232  QUIC_CRYPTO_TOO_MANY_REJECTS,
233  // The client rejected the server's certificate chain or signature.
234  QUIC_PROOF_INVALID,
235  // A crypto message was received with a duplicate tag.
236  QUIC_CRYPTO_DUPLICATE_TAG,
237  // A crypto message was received with the wrong encryption level (i.e. it
238  // should have been encrypted but was not.)
239  QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT,
240  // The server config for a server has expired.
241  QUIC_CRYPTO_SERVER_CONFIG_EXPIRED,
242
243  // No error. Used as bound while iterating.
244  QUIC_LAST_ERROR,
245};
246
247// Version and Crypto tags are written to the wire with a big-endian
248// representation of the name of the tag.  For example
249// the client hello tag (CHLO) will be written as the
250// following 4 bytes: 'C' 'H' 'L' 'O'.  Since it is
251// stored in memory as a little endian uint32, we need
252// to reverse the order of the bytes.
253//
254// The TAG macro is used in header files to ensure that we don't create static
255// initialisers. In normal code, the MakeQuicTag function should be used.
256#define TAG(a, b, c, d) ((d << 24) + (c << 16) + (b << 8) + a)
257const QuicTag kUnsupportedVersion = -1;
258// Each time the wire format changes, this need needs to be incremented.
259// At some point, we will actually freeze the wire format and make an official
260// version number, but this works for now.
261const QuicTag kQuicVersion1 = TAG('Q', '0', '0', '1');
262#undef TAG
263
264// MakeQuicTag returns a value given the four bytes. For example:
265//   MakeQuicTag('C', 'H', 'L', 'O');
266uint32 NET_EXPORT_PRIVATE MakeQuicTag(char a, char b, char c, char d);
267
268struct NET_EXPORT_PRIVATE QuicPacketPublicHeader {
269  QuicPacketPublicHeader();
270  explicit QuicPacketPublicHeader(const QuicPacketPublicHeader& other);
271  ~QuicPacketPublicHeader();
272
273  QuicPacketPublicHeader& operator=(const QuicPacketPublicHeader& other);
274
275  // Universal header. All QuicPacket headers will have a guid and public flags.
276  QuicGuid guid;
277  bool reset_flag;
278  bool version_flag;
279  QuicTagVector versions;
280};
281
282// Header for Data or FEC packets.
283struct NET_EXPORT_PRIVATE QuicPacketHeader {
284  QuicPacketHeader();
285  explicit QuicPacketHeader(const QuicPacketPublicHeader& header);
286
287  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
288      std::ostream& os, const QuicPacketHeader& s);
289
290  QuicPacketPublicHeader public_header;
291  bool fec_flag;
292  bool fec_entropy_flag;
293  bool entropy_flag;
294  QuicPacketEntropyHash entropy_hash;
295  QuicPacketSequenceNumber packet_sequence_number;
296  QuicFecGroupNumber fec_group;
297};
298
299struct NET_EXPORT_PRIVATE QuicPublicResetPacket {
300  QuicPublicResetPacket() {}
301  explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header)
302      : public_header(header) {}
303  QuicPacketPublicHeader public_header;
304  QuicPacketSequenceNumber rejected_sequence_number;
305  QuicPublicResetNonceProof nonce_proof;
306};
307
308enum QuicVersionNegotiationState {
309  START_NEGOTIATION = 0,
310  SENT_NEGOTIATION_PACKET,
311  NEGOTIATED_VERSION
312};
313
314typedef QuicPacketPublicHeader QuicVersionNegotiationPacket;
315
316// A padding frame contains no payload.
317struct NET_EXPORT_PRIVATE QuicPaddingFrame {
318};
319
320struct NET_EXPORT_PRIVATE QuicStreamFrame {
321  QuicStreamFrame();
322  QuicStreamFrame(QuicStreamId stream_id,
323                  bool fin,
324                  QuicStreamOffset offset,
325                  base::StringPiece data);
326
327  QuicStreamId stream_id;
328  bool fin;
329  QuicStreamOffset offset;  // Location of this data in the stream.
330  base::StringPiece data;
331};
332
333// TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing
334// is finalized.
335typedef std::set<QuicPacketSequenceNumber> SequenceNumberSet;
336// TODO(pwestin): Add a way to enforce the max size of this map.
337typedef std::map<QuicPacketSequenceNumber, QuicTime> TimeMap;
338
339struct NET_EXPORT_PRIVATE ReceivedPacketInfo {
340  ReceivedPacketInfo();
341  ~ReceivedPacketInfo();
342  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
343      std::ostream& os, const ReceivedPacketInfo& s);
344
345  // Entropy hash of all packets up to largest observed not including missing
346  // packets.
347  QuicPacketEntropyHash entropy_hash;
348
349  // The highest packet sequence number we've observed from the peer.
350  //
351  // In general, this should be the largest packet number we've received.  In
352  // the case of truncated acks, we may have to advertise a lower "upper bound"
353  // than largest received, to avoid implicitly acking missing packets that
354  // don't fit in the missing packet list due to size limitations.  In this
355  // case, largest_observed may be a packet which is also in the missing packets
356  // list.
357  QuicPacketSequenceNumber largest_observed;
358
359  // Time elapsed since largest_observed was received until this Ack frame was
360  // sent.
361  QuicTime::Delta delta_time_largest_observed;
362
363  // TODO(satyamshekhar): Can be optimized using an interval set like data
364  // structure.
365  // The set of packets which we're expecting and have not received.
366  SequenceNumberSet missing_packets;
367};
368
369// True if the sequence number is greater than largest_observed or is listed
370// as missing.
371// Always returns false for sequence numbers less than least_unacked.
372bool NET_EXPORT_PRIVATE IsAwaitingPacket(
373    const ReceivedPacketInfo& received_info,
374    QuicPacketSequenceNumber sequence_number);
375
376// Inserts missing packets between [lower, higher).
377void NET_EXPORT_PRIVATE InsertMissingPacketsBetween(
378    ReceivedPacketInfo* received_info,
379    QuicPacketSequenceNumber lower,
380    QuicPacketSequenceNumber higher);
381
382struct NET_EXPORT_PRIVATE SentPacketInfo {
383  SentPacketInfo();
384  ~SentPacketInfo();
385  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
386      std::ostream& os, const SentPacketInfo& s);
387
388  // Entropy hash of all packets up to, but not including, the least unacked
389  // packet.
390  QuicPacketEntropyHash entropy_hash;
391  // The lowest packet we've sent which is unacked, and we expect an ack for.
392  QuicPacketSequenceNumber least_unacked;
393};
394
395struct NET_EXPORT_PRIVATE QuicAckFrame {
396  QuicAckFrame() {}
397  // Testing convenience method to construct a QuicAckFrame with all packets
398  // from least_unacked to largest_observed acked.
399  QuicAckFrame(QuicPacketSequenceNumber largest_observed,
400               QuicTime largest_observed_receive_time,
401               QuicPacketSequenceNumber least_unacked);
402
403  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
404      std::ostream& os, const QuicAckFrame& s);
405
406  SentPacketInfo sent_info;
407  ReceivedPacketInfo received_info;
408};
409
410// Defines for all types of congestion feedback that will be negotiated in QUIC,
411// kTCP MUST be supported by all QUIC implementations to guarentee 100%
412// compatibility.
413enum CongestionFeedbackType {
414  kTCP,  // Used to mimic TCP.
415  kInterArrival,  // Use additional inter arrival information.
416  kFixRate,  // Provided for testing.
417};
418
419struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP {
420  uint16 accumulated_number_of_lost_packets;
421  QuicByteCount receive_window;
422};
423
424struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival {
425  CongestionFeedbackMessageInterArrival();
426  ~CongestionFeedbackMessageInterArrival();
427  uint16 accumulated_number_of_lost_packets;
428  // The set of received packets since the last feedback was sent, along with
429  // their arrival times.
430  TimeMap received_packet_times;
431};
432
433struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate {
434  CongestionFeedbackMessageFixRate();
435  QuicBandwidth bitrate;
436};
437
438struct NET_EXPORT_PRIVATE QuicCongestionFeedbackFrame {
439  QuicCongestionFeedbackFrame();
440  ~QuicCongestionFeedbackFrame();
441
442  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
443      std::ostream& os, const QuicCongestionFeedbackFrame& c);
444
445  CongestionFeedbackType type;
446  // This should really be a union, but since the inter arrival struct
447  // is non-trivial, C++ prohibits it.
448  CongestionFeedbackMessageTCP tcp;
449  CongestionFeedbackMessageInterArrival inter_arrival;
450  CongestionFeedbackMessageFixRate fix_rate;
451};
452
453struct NET_EXPORT_PRIVATE QuicRstStreamFrame {
454  QuicRstStreamFrame() {}
455  QuicRstStreamFrame(QuicStreamId stream_id, QuicRstStreamErrorCode error_code)
456      : stream_id(stream_id), error_code(error_code) {
457    DCHECK_LE(error_code, std::numeric_limits<uint8>::max());
458  }
459
460  QuicStreamId stream_id;
461  QuicRstStreamErrorCode error_code;
462  std::string error_details;
463};
464
465struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame {
466  QuicErrorCode error_code;
467  std::string error_details;
468  QuicAckFrame ack_frame;
469};
470
471struct NET_EXPORT_PRIVATE QuicGoAwayFrame {
472  QuicGoAwayFrame() {}
473  QuicGoAwayFrame(QuicErrorCode error_code,
474                  QuicStreamId last_good_stream_id,
475                  const std::string& reason);
476
477  QuicErrorCode error_code;
478  QuicStreamId last_good_stream_id;
479  std::string reason_phrase;
480};
481
482// EncryptionLevel enumerates the stages of encryption that a QUIC connection
483// progresses through. When retransmitting a packet, the encryption level needs
484// to be specified so that it is retransmitted at a level which the peer can
485// understand.
486enum EncryptionLevel {
487  ENCRYPTION_NONE = 0,
488  ENCRYPTION_INITIAL = 1,
489  ENCRYPTION_FORWARD_SECURE = 2,
490
491  NUM_ENCRYPTION_LEVELS,
492};
493
494struct NET_EXPORT_PRIVATE QuicFrame {
495  QuicFrame() {}
496  explicit QuicFrame(QuicPaddingFrame* padding_frame)
497      : type(PADDING_FRAME),
498        padding_frame(padding_frame) {
499  }
500  explicit QuicFrame(QuicStreamFrame* stream_frame)
501      : type(STREAM_FRAME),
502        stream_frame(stream_frame) {
503  }
504  explicit QuicFrame(QuicAckFrame* frame)
505      : type(ACK_FRAME),
506        ack_frame(frame) {
507  }
508  explicit QuicFrame(QuicCongestionFeedbackFrame* frame)
509      : type(CONGESTION_FEEDBACK_FRAME),
510        congestion_feedback_frame(frame) {
511  }
512  explicit QuicFrame(QuicRstStreamFrame* frame)
513      : type(RST_STREAM_FRAME),
514        rst_stream_frame(frame) {
515  }
516  explicit QuicFrame(QuicConnectionCloseFrame* frame)
517      : type(CONNECTION_CLOSE_FRAME),
518        connection_close_frame(frame) {
519  }
520  explicit QuicFrame(QuicGoAwayFrame* frame)
521      : type(GOAWAY_FRAME),
522        goaway_frame(frame) {
523  }
524
525  QuicFrameType type;
526  union {
527    QuicPaddingFrame* padding_frame;
528    QuicStreamFrame* stream_frame;
529    QuicAckFrame* ack_frame;
530    QuicCongestionFeedbackFrame* congestion_feedback_frame;
531    QuicRstStreamFrame* rst_stream_frame;
532    QuicConnectionCloseFrame* connection_close_frame;
533    QuicGoAwayFrame* goaway_frame;
534  };
535};
536
537typedef std::vector<QuicFrame> QuicFrames;
538
539struct NET_EXPORT_PRIVATE QuicFecData {
540  QuicFecData();
541
542  bool operator==(const QuicFecData& other) const;
543
544  // The FEC group number is also the sequence number of the first
545  // FEC protected packet.  The last protected packet's sequence number will
546  // be one less than the sequence number of the FEC packet.
547  QuicFecGroupNumber fec_group;
548  base::StringPiece redundancy;
549};
550
551struct NET_EXPORT_PRIVATE QuicPacketData {
552  std::string data;
553};
554
555class NET_EXPORT_PRIVATE QuicData {
556 public:
557  QuicData(const char* buffer, size_t length)
558      : buffer_(buffer),
559        length_(length),
560        owns_buffer_(false) {}
561
562  QuicData(char* buffer, size_t length, bool owns_buffer)
563      : buffer_(buffer),
564        length_(length),
565        owns_buffer_(owns_buffer) {}
566
567  virtual ~QuicData();
568
569  base::StringPiece AsStringPiece() const {
570    return base::StringPiece(data(), length());
571  }
572
573  const char* data() const { return buffer_; }
574  size_t length() const { return length_; }
575
576 private:
577  const char* buffer_;
578  size_t length_;
579  bool owns_buffer_;
580
581  DISALLOW_COPY_AND_ASSIGN(QuicData);
582};
583
584class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
585 public:
586  static QuicPacket* NewDataPacket(char* buffer,
587                                   size_t length,
588                                   bool owns_buffer,
589                                   bool includes_version) {
590    return new QuicPacket(buffer, length, owns_buffer, includes_version, false);
591  }
592
593  static QuicPacket* NewFecPacket(char* buffer,
594                                  size_t length,
595                                  bool owns_buffer,
596                                  bool includes_version) {
597    return new QuicPacket(buffer, length, owns_buffer, includes_version, true);
598  }
599
600  base::StringPiece FecProtectedData() const;
601  base::StringPiece AssociatedData() const;
602  base::StringPiece BeforePlaintext() const;
603  base::StringPiece Plaintext() const;
604
605  bool is_fec_packet() const { return is_fec_packet_; }
606
607  bool includes_version() const { return includes_version_; }
608
609  char* mutable_data() { return buffer_; }
610
611 private:
612  QuicPacket(char* buffer,
613             size_t length,
614             bool owns_buffer,
615             bool includes_version,
616             bool is_fec_packet)
617      : QuicData(buffer, length, owns_buffer),
618        buffer_(buffer),
619        is_fec_packet_(is_fec_packet),
620        includes_version_(includes_version) {}
621
622  char* buffer_;
623  const bool is_fec_packet_;
624  const bool includes_version_;
625
626  DISALLOW_COPY_AND_ASSIGN(QuicPacket);
627};
628
629class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
630 public:
631  QuicEncryptedPacket(const char* buffer, size_t length)
632      : QuicData(buffer, length) {}
633
634  QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer)
635      : QuicData(buffer, length, owns_buffer) {}
636
637  // By default, gtest prints the raw bytes of an object. The bool data
638  // member (in the base class QuicData) causes this object to have padding
639  // bytes, which causes the default gtest object printer to read
640  // uninitialize memory. So we need to teach gtest how to print this object.
641  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
642      std::ostream& os, const QuicEncryptedPacket& s);
643
644 private:
645  DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket);
646};
647
648class NET_EXPORT_PRIVATE RetransmittableFrames {
649 public:
650  RetransmittableFrames();
651  ~RetransmittableFrames();
652
653  // Allocates a local copy of the referenced StringPiece has QuicStreamFrame
654  // use it.
655  // Takes ownership of |stream_frame|.
656  const QuicFrame& AddStreamFrame(QuicStreamFrame* stream_frame);
657  // Takes ownership of the frame inside |frame|.
658  const QuicFrame& AddNonStreamFrame(const QuicFrame& frame);
659  const QuicFrames& frames() const { return frames_; }
660
661  void set_encryption_level(EncryptionLevel level);
662  EncryptionLevel encryption_level() const {
663    return encryption_level_;
664  }
665
666 private:
667  QuicFrames frames_;
668  EncryptionLevel encryption_level_;
669  // Data referenced by the StringPiece of a QuicStreamFrame.
670  std::vector<std::string*> stream_data_;
671
672  DISALLOW_COPY_AND_ASSIGN(RetransmittableFrames);
673};
674
675struct NET_EXPORT_PRIVATE SerializedPacket {
676  SerializedPacket(QuicPacketSequenceNumber sequence_number,
677                   QuicPacket* packet,
678                   QuicPacketEntropyHash entropy_hash,
679                   RetransmittableFrames* retransmittable_frames)
680      : sequence_number(sequence_number),
681        packet(packet),
682        entropy_hash(entropy_hash),
683        retransmittable_frames(retransmittable_frames) {}
684
685  QuicPacketSequenceNumber sequence_number;
686  QuicPacket* packet;
687  QuicPacketEntropyHash entropy_hash;
688  RetransmittableFrames* retransmittable_frames;
689};
690
691// A struct for functions which consume data payloads and fins.
692// The first member of the pair indicates bytes consumed.
693// The second member of the pair indicates if an incoming fin was consumed.
694struct QuicConsumedData {
695  QuicConsumedData(size_t bytes_consumed, bool fin_consumed)
696      : bytes_consumed(bytes_consumed),
697        fin_consumed(fin_consumed) {}
698
699  // By default, gtest prints the raw bytes of an object. The bool data
700  // member causes this object to have padding bytes, which causes the
701  // default gtest object printer to read uninitialize memory. So we need
702  // to teach gtest how to print this object.
703  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
704      std::ostream& os, const QuicConsumedData& s);
705
706  size_t bytes_consumed;
707  bool fin_consumed;
708};
709
710}  // namespace net
711
712#endif  // NET_QUIC_QUIC_PROTOCOL_H_
713