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