quic_protocol.h revision 3551c9c881056c480085172ff9840cab31610854
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/containers/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;
31struct QuicPacketHeader;
32
33typedef uint64 QuicGuid;
34typedef uint32 QuicStreamId;
35typedef uint64 QuicStreamOffset;
36typedef uint64 QuicPacketSequenceNumber;
37typedef QuicPacketSequenceNumber QuicFecGroupNumber;
38typedef uint64 QuicPublicResetNonceProof;
39typedef uint8 QuicPacketEntropyHash;
40typedef uint32 QuicHeaderId;
41// QuicTag is the type of a tag in the wire protocol.
42typedef uint32 QuicTag;
43typedef std::vector<QuicTag> QuicTagVector;
44
45// TODO(rch): Consider Quic specific names for these constants.
46// Maximum size in bytes of a QUIC packet.
47const QuicByteCount kMaxPacketSize = 1200;
48
49// Maximum number of open streams per connection.
50const size_t kDefaultMaxStreamsPerConnection = 100;
51
52// Number of bytes reserved for public flags in the packet header.
53const size_t kPublicFlagsSize = 1;
54// Number of bytes reserved for version number in the packet header.
55const size_t kQuicVersionSize = 4;
56// Number of bytes reserved for private flags in the packet header.
57const size_t kPrivateFlagsSize = 1;
58// Number of bytes reserved for FEC group in the packet header.
59const size_t kFecGroupSize = 1;
60// Number of bytes reserved for the nonce proof in public reset packet.
61const size_t kPublicResetNonceSize = 8;
62
63// Signifies that the QuicPacket will contain version of the protocol.
64const bool kIncludeVersion = true;
65
66// Index of the first byte in a QUIC packet which is used in hash calculation.
67const size_t kStartOfHashData = 0;
68
69// Limit on the delta between stream IDs.
70const QuicStreamId kMaxStreamIdDelta = 100;
71// Limit on the delta between header IDs.
72const QuicHeaderId kMaxHeaderIdDelta = 100;
73
74// Reserved ID for the crypto stream.
75// TODO(rch): ensure that this is not usable by any other streams.
76const QuicStreamId kCryptoStreamId = 1;
77
78// This is the default network timeout a for connection till the crypto
79// handshake succeeds and the negotiated timeout from the handshake is received.
80const int64 kDefaultInitialTimeoutSecs = 120;  // 2 mins.
81const int64 kDefaultTimeoutSecs = 60 * 10;  // 10 minutes.
82const int64 kDefaultMaxTimeForCryptoHandshakeSecs = 5;  // 5 secs.
83
84enum Retransmission {
85  NOT_RETRANSMISSION,
86  IS_RETRANSMISSION,
87};
88
89enum HasRetransmittableData {
90  NO_RETRANSMITTABLE_DATA,
91  HAS_RETRANSMITTABLE_DATA,
92};
93
94enum IsHandshake {
95  NOT_HANDSHAKE,
96  IS_HANDSHAKE
97};
98
99enum QuicFrameType {
100  PADDING_FRAME = 0,
101  STREAM_FRAME,
102  ACK_FRAME,
103  CONGESTION_FEEDBACK_FRAME,
104  RST_STREAM_FRAME,
105  CONNECTION_CLOSE_FRAME,
106  GOAWAY_FRAME,
107  NUM_FRAME_TYPES
108};
109
110enum QuicGuidLength {
111  PACKET_0BYTE_GUID = 0,
112  PACKET_1BYTE_GUID = 1,
113  PACKET_4BYTE_GUID = 4,
114  PACKET_8BYTE_GUID = 8
115};
116
117enum InFecGroup {
118  NOT_IN_FEC_GROUP,
119  IN_FEC_GROUP,
120};
121
122enum QuicSequenceNumberLength {
123  PACKET_1BYTE_SEQUENCE_NUMBER = 1,
124  PACKET_2BYTE_SEQUENCE_NUMBER = 2,
125  PACKET_4BYTE_SEQUENCE_NUMBER = 4,
126  PACKET_6BYTE_SEQUENCE_NUMBER = 6
127};
128
129// The public flags are specified in one byte.
130enum QuicPacketPublicFlags {
131  PACKET_PUBLIC_FLAGS_NONE = 0,
132
133  // Bit 0: Does the packet header contains version info?
134  PACKET_PUBLIC_FLAGS_VERSION = 1 << 0,
135
136  // Bit 1: Is this packet a public reset packet?
137  PACKET_PUBLIC_FLAGS_RST = 1 << 1,
138
139  // Bits 2 and 3 specify the length of the GUID as follows:
140  // ----00--: 0 bytes
141  // ----01--: 1 byte
142  // ----10--: 4 bytes
143  // ----11--: 8 bytes
144  PACKET_PUBLIC_FLAGS_0BYTE_GUID = 0,
145  PACKET_PUBLIC_FLAGS_1BYTE_GUID = 1 << 2,
146  PACKET_PUBLIC_FLAGS_4BYTE_GUID = 1 << 3,
147  PACKET_PUBLIC_FLAGS_8BYTE_GUID = 1 << 3 | 1 << 2,
148
149  // Bits 4 and 5 describe the packet sequence number length as follows:
150  // --00----: 1 byte
151  // --01----: 2 bytes
152  // --10----: 4 bytes
153  // --11----: 6 bytes
154  PACKET_PUBLIC_FLAGS_1BYTE_SEQUENCE = 0,
155  PACKET_PUBLIC_FLAGS_2BYTE_SEQUENCE = 1 << 4,
156  PACKET_PUBLIC_FLAGS_4BYTE_SEQUENCE = 1 << 5,
157  PACKET_PUBLIC_FLAGS_6BYTE_SEQUENCE = 1 << 5 | 1 << 4,
158
159  // All bits set (bits 6 and 7 are not currently used): 00111111
160  PACKET_PUBLIC_FLAGS_MAX = (1 << 6) - 1
161};
162
163// The private flags are specified in one byte.
164enum QuicPacketPrivateFlags {
165  PACKET_PRIVATE_FLAGS_NONE = 0,
166
167  // Bit 0: Does this packet contain an entropy bit?
168  PACKET_PRIVATE_FLAGS_ENTROPY = 1 << 0,
169
170  // Bit 1: Payload is part of an FEC group?
171  PACKET_PRIVATE_FLAGS_FEC_GROUP = 1 << 1,
172
173  // Bit 2: Payload is FEC as opposed to frames?
174  PACKET_PRIVATE_FLAGS_FEC = 1 << 2,
175
176  // All bits set (bits 3-7 are not currently used): 00000111
177  PACKET_PRIVATE_FLAGS_MAX = (1 << 3) - 1
178};
179
180// The available versions of QUIC. Guaranteed that the integer value of the enum
181// will match the version number.
182// When adding a new version to this enum you should add it to
183// kSupportedQuicVersions (if appropriate), and also add a new case to the
184// helper methods QuicVersionToQuicTag, QuicTagToQuicVersion, and
185// QuicVersionToString.
186enum QuicVersion {
187  // Special case to indicate unknown/unsupported QUIC version.
188  QUIC_VERSION_UNSUPPORTED = 0,
189
190  QUIC_VERSION_7 = 7,
191  QUIC_VERSION_8 = 8,  // Current version.
192};
193
194// This vector contains QUIC versions which we currently support.
195// This should be ordered such that the highest supported version is the first
196// element, with subsequent elements in descending order (versions can be
197// skipped as necessary).
198static const QuicVersion kSupportedQuicVersions[] =
199    {QUIC_VERSION_8, QUIC_VERSION_7};
200
201typedef std::vector<QuicVersion> QuicVersionVector;
202
203// Upper limit on versions we support.
204NET_EXPORT_PRIVATE QuicVersion QuicVersionMax();
205
206// Lower limit on versions we support.
207NET_EXPORT_PRIVATE QuicVersion QuicVersionMin();
208
209// QuicTag is written to and read from the wire, but we prefer to use
210// the more readable QuicVersion at other levels.
211// Helper function which translates from a QuicVersion to a QuicTag. Returns 0
212// if QuicVersion is unsupported.
213NET_EXPORT_PRIVATE QuicTag QuicVersionToQuicTag(const QuicVersion version);
214
215// Returns appropriate QuicVersion from a QuicTag.
216// Returns QUIC_VERSION_UNSUPPORTED if version_tag cannot be understood.
217NET_EXPORT_PRIVATE QuicVersion QuicTagToQuicVersion(const QuicTag version_tag);
218
219// Helper function which translates from a QuicVersion to a string.
220// Returns strings corresponding to enum names (e.g. QUIC_VERSION_6).
221NET_EXPORT_PRIVATE std::string QuicVersionToString(const QuicVersion version);
222
223// Returns comma separated list of string representations of QuicVersion enum
224// values in the supplied QuicVersionArray.
225NET_EXPORT_PRIVATE std::string QuicVersionArrayToString(
226    const QuicVersion versions[], int num_versions);
227
228// Version and Crypto tags are written to the wire with a big-endian
229// representation of the name of the tag.  For example
230// the client hello tag (CHLO) will be written as the
231// following 4 bytes: 'C' 'H' 'L' 'O'.  Since it is
232// stored in memory as a little endian uint32, we need
233// to reverse the order of the bytes.
234
235// MakeQuicTag returns a value given the four bytes. For example:
236//   MakeQuicTag('C', 'H', 'L', 'O');
237NET_EXPORT_PRIVATE QuicTag MakeQuicTag(char a, char b, char c, char d);
238
239// Size in bytes of the data or fec packet header.
240NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(QuicPacketHeader header);
241
242NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(
243    QuicGuidLength guid_length,
244    bool include_version,
245    QuicSequenceNumberLength sequence_number_length,
246    InFecGroup is_in_fec_group);
247
248// Size in bytes of the public reset packet.
249NET_EXPORT_PRIVATE size_t GetPublicResetPacketSize();
250
251// Index of the first byte in a QUIC packet of FEC protected data.
252NET_EXPORT_PRIVATE size_t GetStartOfFecProtectedData(
253    QuicGuidLength guid_length,
254    bool include_version,
255    QuicSequenceNumberLength sequence_number_length);
256// Index of the first byte in a QUIC packet of encrypted data.
257NET_EXPORT_PRIVATE size_t GetStartOfEncryptedData(
258    QuicGuidLength guid_length,
259    bool include_version,
260    QuicSequenceNumberLength sequence_number_length);
261
262enum QuicRstStreamErrorCode {
263  QUIC_STREAM_NO_ERROR = 0,
264
265  // There was some server error which halted stream processing.
266  QUIC_SERVER_ERROR_PROCESSING_STREAM,
267  // We got two fin or reset offsets which did not match.
268  QUIC_MULTIPLE_TERMINATION_OFFSETS,
269  // We got bad payload and can not respond to it at the protocol level.
270  QUIC_BAD_APPLICATION_PAYLOAD,
271  // Stream closed due to connection error. No reset frame is sent when this
272  // happens.
273  QUIC_STREAM_CONNECTION_ERROR,
274  // GoAway frame sent. No more stream can be created.
275  QUIC_STREAM_PEER_GOING_AWAY,
276
277  // No error. Used as bound while iterating.
278  QUIC_STREAM_LAST_ERROR,
279};
280
281// These values must remain stable as they are uploaded to UMA histograms.
282// To add a new error code, use the current value of QUIC_LAST_ERROR and
283// increment QUIC_LAST_ERROR.
284enum QuicErrorCode {
285  QUIC_NO_ERROR = 0,
286
287  // Connection has reached an invalid state.
288  QUIC_INTERNAL_ERROR = 1,
289  // There were data frames after the a fin or reset.
290  QUIC_STREAM_DATA_AFTER_TERMINATION = 2,
291  // Control frame is malformed.
292  QUIC_INVALID_PACKET_HEADER = 3,
293  // Frame data is malformed.
294  QUIC_INVALID_FRAME_DATA = 4,
295  // The packet contained no payload.
296  QUIC_MISSING_PAYLOAD = 48,
297  // FEC data is malformed.
298  QUIC_INVALID_FEC_DATA = 5,
299  // STREAM frame data is malformed.
300  QUIC_INVALID_STREAM_DATA = 46,
301  // RST_STREAM frame data is malformed.
302  QUIC_INVALID_RST_STREAM_DATA = 6,
303  // CONNECTION_CLOSE frame data is malformed.
304  QUIC_INVALID_CONNECTION_CLOSE_DATA = 7,
305  // GOAWAY frame data is malformed.
306  QUIC_INVALID_GOAWAY_DATA = 8,
307  // ACK frame data is malformed.
308  QUIC_INVALID_ACK_DATA = 9,
309  // CONGESTION_FEEDBACK frame data is malformed.
310  QUIC_INVALID_CONGESTION_FEEDBACK_DATA = 47,
311  // Version negotiation packet is malformed.
312  QUIC_INVALID_VERSION_NEGOTIATION_PACKET = 10,
313  // Public RST packet is malformed.
314  QUIC_INVALID_PUBLIC_RST_PACKET = 11,
315  // There was an error decrypting.
316  QUIC_DECRYPTION_FAILURE = 12,
317  // There was an error encrypting.
318  QUIC_ENCRYPTION_FAILURE = 13,
319  // The packet exceeded kMaxPacketSize.
320  QUIC_PACKET_TOO_LARGE = 14,
321  // Data was sent for a stream which did not exist.
322  QUIC_PACKET_FOR_NONEXISTENT_STREAM = 15,
323  // The peer is going away.  May be a client or server.
324  QUIC_PEER_GOING_AWAY = 16,
325  // A stream ID was invalid.
326  QUIC_INVALID_STREAM_ID = 17,
327  // Too many streams already open.
328  QUIC_TOO_MANY_OPEN_STREAMS = 18,
329  // Received public reset for this connection.
330  QUIC_PUBLIC_RESET = 19,
331  // Invalid protocol version.
332  QUIC_INVALID_VERSION = 20,
333  // Stream reset before headers decompressed.
334  QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED = 21,
335  // The Header ID for a stream was too far from the previous.
336  QUIC_INVALID_HEADER_ID = 22,
337  // Negotiable parameter received during handshake had invalid value.
338  QUIC_INVALID_NEGOTIATED_VALUE = 23,
339  // There was an error decompressing data.
340  QUIC_DECOMPRESSION_FAILURE = 24,
341  // We hit our prenegotiated (or default) timeout
342  QUIC_CONNECTION_TIMED_OUT = 25,
343  // There was an error encountered migrating addresses
344  QUIC_ERROR_MIGRATING_ADDRESS = 26,
345  // There was an error while writing the packet.
346  QUIC_PACKET_WRITE_ERROR = 27,
347
348
349  // Crypto errors.
350
351  // Hanshake failed.
352  QUIC_HANDSHAKE_FAILED = 28,
353  // Handshake message contained out of order tags.
354  QUIC_CRYPTO_TAGS_OUT_OF_ORDER = 29,
355  // Handshake message contained too many entries.
356  QUIC_CRYPTO_TOO_MANY_ENTRIES = 30,
357  // Handshake message contained an invalid value length.
358  QUIC_CRYPTO_INVALID_VALUE_LENGTH = 31,
359  // A crypto message was received after the handshake was complete.
360  QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE = 32,
361  // A crypto message was received with an illegal message tag.
362  QUIC_INVALID_CRYPTO_MESSAGE_TYPE = 33,
363  // A crypto message was received with an illegal parameter.
364  QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER = 34,
365  // A crypto message was received with a mandatory parameter missing.
366  QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND = 35,
367  // A crypto message was received with a parameter that has no overlap
368  // with the local parameter.
369  QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP = 36,
370  // A crypto message was received that contained a parameter with too few
371  // values.
372  QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND = 37,
373  // An internal error occured in crypto processing.
374  QUIC_CRYPTO_INTERNAL_ERROR = 38,
375  // A crypto handshake message specified an unsupported version.
376  QUIC_CRYPTO_VERSION_NOT_SUPPORTED = 39,
377  // There was no intersection between the crypto primitives supported by the
378  // peer and ourselves.
379  QUIC_CRYPTO_NO_SUPPORT = 40,
380  // The server rejected our client hello messages too many times.
381  QUIC_CRYPTO_TOO_MANY_REJECTS = 41,
382  // The client rejected the server's certificate chain or signature.
383  QUIC_PROOF_INVALID = 42,
384  // A crypto message was received with a duplicate tag.
385  QUIC_CRYPTO_DUPLICATE_TAG = 43,
386  // A crypto message was received with the wrong encryption level (i.e. it
387  // should have been encrypted but was not.)
388  QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT = 44,
389  // The server config for a server has expired.
390  QUIC_CRYPTO_SERVER_CONFIG_EXPIRED = 45,
391
392  // No error. Used as bound while iterating.
393  QUIC_LAST_ERROR = 49,
394};
395
396struct NET_EXPORT_PRIVATE QuicPacketPublicHeader {
397  QuicPacketPublicHeader();
398  explicit QuicPacketPublicHeader(const QuicPacketPublicHeader& other);
399  ~QuicPacketPublicHeader();
400
401  // Universal header. All QuicPacket headers will have a guid and public flags.
402  QuicGuid guid;
403  QuicGuidLength guid_length;
404  bool reset_flag;
405  bool version_flag;
406  QuicSequenceNumberLength sequence_number_length;
407  QuicVersionVector versions;
408};
409
410// Header for Data or FEC packets.
411struct NET_EXPORT_PRIVATE QuicPacketHeader {
412  QuicPacketHeader();
413  explicit QuicPacketHeader(const QuicPacketPublicHeader& header);
414
415  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
416      std::ostream& os, const QuicPacketHeader& s);
417
418  QuicPacketPublicHeader public_header;
419  bool fec_flag;
420  bool entropy_flag;
421  QuicPacketEntropyHash entropy_hash;
422  QuicPacketSequenceNumber packet_sequence_number;
423  InFecGroup is_in_fec_group;
424  QuicFecGroupNumber fec_group;
425};
426
427struct NET_EXPORT_PRIVATE QuicPublicResetPacket {
428  QuicPublicResetPacket() {}
429  explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header)
430      : public_header(header) {}
431  QuicPacketPublicHeader public_header;
432  QuicPacketSequenceNumber rejected_sequence_number;
433  QuicPublicResetNonceProof nonce_proof;
434};
435
436enum QuicVersionNegotiationState {
437  START_NEGOTIATION = 0,
438  // Server-side this implies we've sent a version negotiation packet and are
439  // waiting on the client to select a compatible version.  Client-side this
440  // implies we've gotten a version negotiation packet, are retransmitting the
441  // initial packets with a supported version and are waiting for our first
442  // packet from the server.
443  NEGOTIATION_IN_PROGRESS,
444  // This indicates this endpoint has received a packet from the peer with a
445  // version this endpoint supports.  Version negotiation is complete, and the
446  // version number will no longer be sent with future packets.
447  NEGOTIATED_VERSION
448};
449
450typedef QuicPacketPublicHeader QuicVersionNegotiationPacket;
451
452// A padding frame contains no payload.
453struct NET_EXPORT_PRIVATE QuicPaddingFrame {
454};
455
456struct NET_EXPORT_PRIVATE QuicStreamFrame {
457  QuicStreamFrame();
458  QuicStreamFrame(QuicStreamId stream_id,
459                  bool fin,
460                  QuicStreamOffset offset,
461                  base::StringPiece data);
462
463  QuicStreamId stream_id;
464  bool fin;
465  QuicStreamOffset offset;  // Location of this data in the stream.
466  base::StringPiece data;
467};
468
469// TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing
470// is finalized.
471typedef std::set<QuicPacketSequenceNumber> SequenceNumberSet;
472// TODO(pwestin): Add a way to enforce the max size of this map.
473typedef std::map<QuicPacketSequenceNumber, QuicTime> TimeMap;
474
475struct NET_EXPORT_PRIVATE ReceivedPacketInfo {
476  ReceivedPacketInfo();
477  ~ReceivedPacketInfo();
478  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
479      std::ostream& os, const ReceivedPacketInfo& s);
480
481  // Entropy hash of all packets up to largest observed not including missing
482  // packets.
483  QuicPacketEntropyHash entropy_hash;
484
485  // The highest packet sequence number we've observed from the peer.
486  //
487  // In general, this should be the largest packet number we've received.  In
488  // the case of truncated acks, we may have to advertise a lower "upper bound"
489  // than largest received, to avoid implicitly acking missing packets that
490  // don't fit in the missing packet list due to size limitations.  In this
491  // case, largest_observed may be a packet which is also in the missing packets
492  // list.
493  QuicPacketSequenceNumber largest_observed;
494
495  // Time elapsed since largest_observed was received until this Ack frame was
496  // sent.
497  QuicTime::Delta delta_time_largest_observed;
498
499  // TODO(satyamshekhar): Can be optimized using an interval set like data
500  // structure.
501  // The set of packets which we're expecting and have not received.
502  SequenceNumberSet missing_packets;
503};
504
505// True if the sequence number is greater than largest_observed or is listed
506// as missing.
507// Always returns false for sequence numbers less than least_unacked.
508bool NET_EXPORT_PRIVATE IsAwaitingPacket(
509    const ReceivedPacketInfo& received_info,
510    QuicPacketSequenceNumber sequence_number);
511
512// Inserts missing packets between [lower, higher).
513void NET_EXPORT_PRIVATE InsertMissingPacketsBetween(
514    ReceivedPacketInfo* received_info,
515    QuicPacketSequenceNumber lower,
516    QuicPacketSequenceNumber higher);
517
518struct NET_EXPORT_PRIVATE SentPacketInfo {
519  SentPacketInfo();
520  ~SentPacketInfo();
521  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
522      std::ostream& os, const SentPacketInfo& s);
523
524  // Entropy hash of all packets up to, but not including, the least unacked
525  // packet.
526  QuicPacketEntropyHash entropy_hash;
527  // The lowest packet we've sent which is unacked, and we expect an ack for.
528  QuicPacketSequenceNumber least_unacked;
529};
530
531struct NET_EXPORT_PRIVATE QuicAckFrame {
532  QuicAckFrame() {}
533  // Testing convenience method to construct a QuicAckFrame with all packets
534  // from least_unacked to largest_observed acked.
535  QuicAckFrame(QuicPacketSequenceNumber largest_observed,
536               QuicTime largest_observed_receive_time,
537               QuicPacketSequenceNumber least_unacked);
538
539  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
540      std::ostream& os, const QuicAckFrame& s);
541
542  SentPacketInfo sent_info;
543  ReceivedPacketInfo received_info;
544};
545
546// Defines for all types of congestion feedback that will be negotiated in QUIC,
547// kTCP MUST be supported by all QUIC implementations to guarantee 100%
548// compatibility.
549enum CongestionFeedbackType {
550  kTCP,  // Used to mimic TCP.
551  kInterArrival,  // Use additional inter arrival information.
552  kFixRate,  // Provided for testing.
553};
554
555struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP {
556  uint16 accumulated_number_of_lost_packets;
557  QuicByteCount receive_window;
558};
559
560struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival {
561  CongestionFeedbackMessageInterArrival();
562  ~CongestionFeedbackMessageInterArrival();
563  uint16 accumulated_number_of_lost_packets;
564  // The set of received packets since the last feedback was sent, along with
565  // their arrival times.
566  TimeMap received_packet_times;
567};
568
569struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate {
570  CongestionFeedbackMessageFixRate();
571  QuicBandwidth bitrate;
572};
573
574struct NET_EXPORT_PRIVATE QuicCongestionFeedbackFrame {
575  QuicCongestionFeedbackFrame();
576  ~QuicCongestionFeedbackFrame();
577
578  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
579      std::ostream& os, const QuicCongestionFeedbackFrame& c);
580
581  CongestionFeedbackType type;
582  // This should really be a union, but since the inter arrival struct
583  // is non-trivial, C++ prohibits it.
584  CongestionFeedbackMessageTCP tcp;
585  CongestionFeedbackMessageInterArrival inter_arrival;
586  CongestionFeedbackMessageFixRate fix_rate;
587};
588
589struct NET_EXPORT_PRIVATE QuicRstStreamFrame {
590  QuicRstStreamFrame() {}
591  QuicRstStreamFrame(QuicStreamId stream_id, QuicRstStreamErrorCode error_code)
592      : stream_id(stream_id), error_code(error_code) {
593    DCHECK_LE(error_code, std::numeric_limits<uint8>::max());
594  }
595
596  QuicStreamId stream_id;
597  QuicRstStreamErrorCode error_code;
598  std::string error_details;
599};
600
601struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame {
602  QuicErrorCode error_code;
603  std::string error_details;
604  QuicAckFrame ack_frame;
605};
606
607struct NET_EXPORT_PRIVATE QuicGoAwayFrame {
608  QuicGoAwayFrame() {}
609  QuicGoAwayFrame(QuicErrorCode error_code,
610                  QuicStreamId last_good_stream_id,
611                  const std::string& reason);
612
613  QuicErrorCode error_code;
614  QuicStreamId last_good_stream_id;
615  std::string reason_phrase;
616};
617
618// EncryptionLevel enumerates the stages of encryption that a QUIC connection
619// progresses through. When retransmitting a packet, the encryption level needs
620// to be specified so that it is retransmitted at a level which the peer can
621// understand.
622enum EncryptionLevel {
623  ENCRYPTION_NONE = 0,
624  ENCRYPTION_INITIAL = 1,
625  ENCRYPTION_FORWARD_SECURE = 2,
626
627  NUM_ENCRYPTION_LEVELS,
628};
629
630struct NET_EXPORT_PRIVATE QuicFrame {
631  QuicFrame() {}
632  explicit QuicFrame(QuicPaddingFrame* padding_frame)
633      : type(PADDING_FRAME),
634        padding_frame(padding_frame) {
635  }
636  explicit QuicFrame(QuicStreamFrame* stream_frame)
637      : type(STREAM_FRAME),
638        stream_frame(stream_frame) {
639  }
640  explicit QuicFrame(QuicAckFrame* frame)
641      : type(ACK_FRAME),
642        ack_frame(frame) {
643  }
644  explicit QuicFrame(QuicCongestionFeedbackFrame* frame)
645      : type(CONGESTION_FEEDBACK_FRAME),
646        congestion_feedback_frame(frame) {
647  }
648  explicit QuicFrame(QuicRstStreamFrame* frame)
649      : type(RST_STREAM_FRAME),
650        rst_stream_frame(frame) {
651  }
652  explicit QuicFrame(QuicConnectionCloseFrame* frame)
653      : type(CONNECTION_CLOSE_FRAME),
654        connection_close_frame(frame) {
655  }
656  explicit QuicFrame(QuicGoAwayFrame* frame)
657      : type(GOAWAY_FRAME),
658        goaway_frame(frame) {
659  }
660
661  QuicFrameType type;
662  union {
663    QuicPaddingFrame* padding_frame;
664    QuicStreamFrame* stream_frame;
665    QuicAckFrame* ack_frame;
666    QuicCongestionFeedbackFrame* congestion_feedback_frame;
667    QuicRstStreamFrame* rst_stream_frame;
668    QuicConnectionCloseFrame* connection_close_frame;
669    QuicGoAwayFrame* goaway_frame;
670  };
671};
672
673typedef std::vector<QuicFrame> QuicFrames;
674
675struct NET_EXPORT_PRIVATE QuicFecData {
676  QuicFecData();
677
678  // The FEC group number is also the sequence number of the first
679  // FEC protected packet.  The last protected packet's sequence number will
680  // be one less than the sequence number of the FEC packet.
681  QuicFecGroupNumber fec_group;
682  base::StringPiece redundancy;
683};
684
685class NET_EXPORT_PRIVATE QuicData {
686 public:
687  QuicData(const char* buffer, size_t length)
688      : buffer_(buffer),
689        length_(length),
690        owns_buffer_(false) {}
691
692  QuicData(char* buffer, size_t length, bool owns_buffer)
693      : buffer_(buffer),
694        length_(length),
695        owns_buffer_(owns_buffer) {}
696
697  virtual ~QuicData();
698
699  base::StringPiece AsStringPiece() const {
700    return base::StringPiece(data(), length());
701  }
702
703  const char* data() const { return buffer_; }
704  size_t length() const { return length_; }
705
706 private:
707  const char* buffer_;
708  size_t length_;
709  bool owns_buffer_;
710
711  DISALLOW_COPY_AND_ASSIGN(QuicData);
712};
713
714class NET_EXPORT_PRIVATE QuicPacket : public QuicData {
715 public:
716  static QuicPacket* NewDataPacket(
717      char* buffer,
718      size_t length,
719      bool owns_buffer,
720      QuicGuidLength guid_length,
721      bool includes_version,
722      QuicSequenceNumberLength sequence_number_length) {
723    return new QuicPacket(buffer, length, owns_buffer, guid_length,
724                          includes_version, sequence_number_length, false);
725  }
726
727  static QuicPacket* NewFecPacket(
728      char* buffer,
729      size_t length,
730      bool owns_buffer,
731      QuicGuidLength guid_length,
732      bool includes_version,
733      QuicSequenceNumberLength sequence_number_length) {
734    return new QuicPacket(buffer, length, owns_buffer, guid_length,
735                          includes_version, sequence_number_length, true);
736  }
737
738  base::StringPiece FecProtectedData() const;
739  base::StringPiece AssociatedData() const;
740  base::StringPiece BeforePlaintext() const;
741  base::StringPiece Plaintext() const;
742
743  bool is_fec_packet() const { return is_fec_packet_; }
744
745  char* mutable_data() { return buffer_; }
746
747 private:
748  QuicPacket(char* buffer,
749             size_t length,
750             bool owns_buffer,
751             QuicGuidLength guid_length,
752             bool includes_version,
753             QuicSequenceNumberLength sequence_number_length,
754             bool is_fec_packet)
755      : QuicData(buffer, length, owns_buffer),
756        buffer_(buffer),
757        is_fec_packet_(is_fec_packet),
758        guid_length_(guid_length),
759        includes_version_(includes_version),
760        sequence_number_length_(sequence_number_length) {}
761
762  char* buffer_;
763  const bool is_fec_packet_;
764  const QuicGuidLength guid_length_;
765  const bool includes_version_;
766  const QuicSequenceNumberLength sequence_number_length_;
767
768  DISALLOW_COPY_AND_ASSIGN(QuicPacket);
769};
770
771class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData {
772 public:
773  QuicEncryptedPacket(const char* buffer, size_t length)
774      : QuicData(buffer, length) {}
775
776  QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer)
777      : QuicData(buffer, length, owns_buffer) {}
778
779  // By default, gtest prints the raw bytes of an object. The bool data
780  // member (in the base class QuicData) causes this object to have padding
781  // bytes, which causes the default gtest object printer to read
782  // uninitialize memory. So we need to teach gtest how to print this object.
783  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
784      std::ostream& os, const QuicEncryptedPacket& s);
785
786 private:
787  DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket);
788};
789
790class NET_EXPORT_PRIVATE RetransmittableFrames {
791 public:
792  RetransmittableFrames();
793  ~RetransmittableFrames();
794
795  // Allocates a local copy of the referenced StringPiece has QuicStreamFrame
796  // use it.
797  // Takes ownership of |stream_frame|.
798  const QuicFrame& AddStreamFrame(QuicStreamFrame* stream_frame);
799  // Takes ownership of the frame inside |frame|.
800  const QuicFrame& AddNonStreamFrame(const QuicFrame& frame);
801  const QuicFrames& frames() const { return frames_; }
802
803  void set_encryption_level(EncryptionLevel level);
804  EncryptionLevel encryption_level() const {
805    return encryption_level_;
806  }
807
808 private:
809  QuicFrames frames_;
810  EncryptionLevel encryption_level_;
811  // Data referenced by the StringPiece of a QuicStreamFrame.
812  std::vector<std::string*> stream_data_;
813
814  DISALLOW_COPY_AND_ASSIGN(RetransmittableFrames);
815};
816
817struct NET_EXPORT_PRIVATE SerializedPacket {
818  SerializedPacket(QuicPacketSequenceNumber sequence_number,
819                   QuicSequenceNumberLength sequence_number_length,
820                   QuicPacket* packet,
821                   QuicPacketEntropyHash entropy_hash,
822                   RetransmittableFrames* retransmittable_frames)
823      : sequence_number(sequence_number),
824        sequence_number_length(sequence_number_length),
825        packet(packet),
826        entropy_hash(entropy_hash),
827        retransmittable_frames(retransmittable_frames) {}
828
829  QuicPacketSequenceNumber sequence_number;
830  QuicSequenceNumberLength sequence_number_length;
831  QuicPacket* packet;
832  QuicPacketEntropyHash entropy_hash;
833  RetransmittableFrames* retransmittable_frames;
834};
835
836// A struct for functions which consume data payloads and fins.
837// The first member of the pair indicates bytes consumed.
838// The second member of the pair indicates if an incoming fin was consumed.
839struct QuicConsumedData {
840  QuicConsumedData(size_t bytes_consumed, bool fin_consumed)
841      : bytes_consumed(bytes_consumed),
842        fin_consumed(fin_consumed) {}
843
844  // By default, gtest prints the raw bytes of an object. The bool data
845  // member causes this object to have padding bytes, which causes the
846  // default gtest object printer to read uninitialize memory. So we need
847  // to teach gtest how to print this object.
848  NET_EXPORT_PRIVATE friend std::ostream& operator<<(
849      std::ostream& os, const QuicConsumedData& s);
850
851  size_t bytes_consumed;
852  bool fin_consumed;
853};
854
855}  // namespace net
856
857#endif  // NET_QUIC_QUIC_PROTOCOL_H_
858