quic_protocol.h revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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/ip_endpoint.h" 23#include "net/base/net_export.h" 24#include "net/quic/iovector.h" 25#include "net/quic/quic_bandwidth.h" 26#include "net/quic/quic_time.h" 27 28namespace net { 29 30using ::operator<<; 31 32class QuicAckNotifier; 33class QuicPacket; 34struct QuicPacketHeader; 35 36typedef uint64 QuicConnectionId; 37typedef uint32 QuicStreamId; 38typedef uint64 QuicStreamOffset; 39typedef uint64 QuicPacketSequenceNumber; 40typedef QuicPacketSequenceNumber QuicFecGroupNumber; 41typedef uint64 QuicPublicResetNonceProof; 42typedef uint8 QuicPacketEntropyHash; 43typedef uint32 QuicHeaderId; 44// QuicTag is the type of a tag in the wire protocol. 45typedef uint32 QuicTag; 46typedef std::vector<QuicTag> QuicTagVector; 47typedef std::map<QuicTag, std::string> QuicTagValueMap; 48// TODO(rtenneti): Didn't use SpdyPriority because SpdyPriority is uint8 and 49// QuicPriority is uint32. Use SpdyPriority when we change the QUIC_VERSION. 50typedef uint32 QuicPriority; 51 52// TODO(rch): Consider Quic specific names for these constants. 53// Default and initial maximum size in bytes of a QUIC packet. 54const QuicByteCount kDefaultMaxPacketSize = 1200; 55// The maximum packet size of any QUIC packet, based on ethernet's max size, 56// minus the IP and UDP headers. IPv6 has a 40 byte header, UPD adds an 57// additional 8 bytes. This is a total overhead of 48 bytes. Ethernet's 58// max packet size is 1500 bytes, 1500 - 48 = 1452. 59const QuicByteCount kMaxPacketSize = 1452; 60 61// Maximum size of the initial congestion window in packets. 62const size_t kDefaultInitialWindow = 10; 63const uint32 kMaxInitialWindow = 100; 64 65// Default size of initial flow control window. 66const uint32 kDefaultFlowControlSendWindow = 16 * 1024; // 16 KB 67 68// Maximum size of the congestion window, in packets, for TCP congestion control 69// algorithms. 70const size_t kMaxTcpCongestionWindow = 200; 71 72// Don't allow a client to suggest an RTT longer than 15 seconds. 73const uint32 kMaxInitialRoundTripTimeUs = 15 * kNumMicrosPerSecond; 74 75// Maximum number of open streams per connection. 76const size_t kDefaultMaxStreamsPerConnection = 100; 77 78// Number of bytes reserved for public flags in the packet header. 79const size_t kPublicFlagsSize = 1; 80// Number of bytes reserved for version number in the packet header. 81const size_t kQuicVersionSize = 4; 82// Number of bytes reserved for private flags in the packet header. 83const size_t kPrivateFlagsSize = 1; 84// Number of bytes reserved for FEC group in the packet header. 85const size_t kFecGroupSize = 1; 86 87// Signifies that the QuicPacket will contain version of the protocol. 88const bool kIncludeVersion = true; 89 90// Index of the first byte in a QUIC packet which is used in hash calculation. 91const size_t kStartOfHashData = 0; 92 93// Limit on the delta between stream IDs. 94const QuicStreamId kMaxStreamIdDelta = 200; 95// Limit on the delta between header IDs. 96const QuicHeaderId kMaxHeaderIdDelta = 200; 97 98// Reserved ID for the crypto stream. 99const QuicStreamId kCryptoStreamId = 1; 100 101// Reserved ID for the headers stream. 102const QuicStreamId kHeadersStreamId = 3; 103 104// This is the default network timeout a for connection till the crypto 105// handshake succeeds and the negotiated timeout from the handshake is received. 106const int64 kDefaultInitialTimeoutSecs = 120; // 2 mins. 107const int64 kDefaultTimeoutSecs = 60 * 10; // 10 minutes. 108const int64 kDefaultMaxTimeForCryptoHandshakeSecs = 5; // 5 secs. 109 110// Default ping timeout. 111const int64 kPingTimeoutSecs = 15; // 15 secs. 112 113// We define an unsigned 16-bit floating point value, inspired by IEEE floats 114// (http://en.wikipedia.org/wiki/Half_precision_floating-point_format), 115// with 5-bit exponent (bias 1), 11-bit mantissa (effective 12 with hidden 116// bit) and denormals, but without signs, transfinites or fractions. Wire format 117// 16 bits (little-endian byte order) are split into exponent (high 5) and 118// mantissa (low 11) and decoded as: 119// uint64 value; 120// if (exponent == 0) value = mantissa; 121// else value = (mantissa | 1 << 11) << (exponent - 1) 122const int kUFloat16ExponentBits = 5; 123const int kUFloat16MaxExponent = (1 << kUFloat16ExponentBits) - 2; // 30 124const int kUFloat16MantissaBits = 16 - kUFloat16ExponentBits; // 11 125const int kUFloat16MantissaEffectiveBits = kUFloat16MantissaBits + 1; // 12 126const uint64 kUFloat16MaxValue = // 0x3FFC0000000 127 ((GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits) - 1) << 128 kUFloat16MaxExponent; 129 130enum TransmissionType { 131 NOT_RETRANSMISSION, 132 FIRST_TRANSMISSION_TYPE = NOT_RETRANSMISSION, 133 HANDSHAKE_RETRANSMISSION, // Retransmits due to handshake timeouts. 134 ALL_UNACKED_RETRANSMISSION, // Retransmits of all unacked packets. 135 LOSS_RETRANSMISSION, // Retransmits due to loss detection. 136 RTO_RETRANSMISSION, // Retransmits due to retransmit time out. 137 TLP_RETRANSMISSION, // Tail loss probes. 138 LAST_TRANSMISSION_TYPE = TLP_RETRANSMISSION, 139}; 140 141enum RetransmissionType { 142 INITIAL_ENCRYPTION_ONLY, 143 ALL_PACKETS 144}; 145 146enum HasRetransmittableData { 147 NO_RETRANSMITTABLE_DATA, 148 HAS_RETRANSMITTABLE_DATA, 149}; 150 151enum IsHandshake { 152 NOT_HANDSHAKE, 153 IS_HANDSHAKE 154}; 155 156enum QuicFrameType { 157 // Regular frame types. The values set here cannot change without the 158 // introduction of a new QUIC version. 159 PADDING_FRAME = 0, 160 RST_STREAM_FRAME = 1, 161 CONNECTION_CLOSE_FRAME = 2, 162 GOAWAY_FRAME = 3, 163 WINDOW_UPDATE_FRAME = 4, 164 BLOCKED_FRAME = 5, 165 STOP_WAITING_FRAME = 6, 166 PING_FRAME = 7, 167 168 // STREAM, ACK, and CONGESTION_FEEDBACK frames are special frames. They are 169 // encoded differently on the wire and their values do not need to be stable. 170 STREAM_FRAME, 171 ACK_FRAME, 172 CONGESTION_FEEDBACK_FRAME, 173 NUM_FRAME_TYPES 174}; 175 176enum QuicConnectionIdLength { 177 PACKET_0BYTE_CONNECTION_ID = 0, 178 PACKET_1BYTE_CONNECTION_ID = 1, 179 PACKET_4BYTE_CONNECTION_ID = 4, 180 PACKET_8BYTE_CONNECTION_ID = 8 181}; 182 183enum InFecGroup { 184 NOT_IN_FEC_GROUP, 185 IN_FEC_GROUP, 186}; 187 188enum QuicSequenceNumberLength { 189 PACKET_1BYTE_SEQUENCE_NUMBER = 1, 190 PACKET_2BYTE_SEQUENCE_NUMBER = 2, 191 PACKET_4BYTE_SEQUENCE_NUMBER = 4, 192 PACKET_6BYTE_SEQUENCE_NUMBER = 6 193}; 194 195// Used to indicate a QuicSequenceNumberLength using two flag bits. 196enum QuicSequenceNumberLengthFlags { 197 PACKET_FLAGS_1BYTE_SEQUENCE = 0, // 00 198 PACKET_FLAGS_2BYTE_SEQUENCE = 1, // 01 199 PACKET_FLAGS_4BYTE_SEQUENCE = 1 << 1, // 10 200 PACKET_FLAGS_6BYTE_SEQUENCE = 1 << 1 | 1, // 11 201}; 202 203// The public flags are specified in one byte. 204enum QuicPacketPublicFlags { 205 PACKET_PUBLIC_FLAGS_NONE = 0, 206 207 // Bit 0: Does the packet header contains version info? 208 PACKET_PUBLIC_FLAGS_VERSION = 1 << 0, 209 210 // Bit 1: Is this packet a public reset packet? 211 PACKET_PUBLIC_FLAGS_RST = 1 << 1, 212 213 // Bits 2 and 3 specify the length of the ConnectionId as follows: 214 // ----00--: 0 bytes 215 // ----01--: 1 byte 216 // ----10--: 4 bytes 217 // ----11--: 8 bytes 218 PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID = 0, 219 PACKET_PUBLIC_FLAGS_1BYTE_CONNECTION_ID = 1 << 2, 220 PACKET_PUBLIC_FLAGS_4BYTE_CONNECTION_ID = 1 << 3, 221 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID = 1 << 3 | 1 << 2, 222 223 // Bits 4 and 5 describe the packet sequence number length as follows: 224 // --00----: 1 byte 225 // --01----: 2 bytes 226 // --10----: 4 bytes 227 // --11----: 6 bytes 228 PACKET_PUBLIC_FLAGS_1BYTE_SEQUENCE = PACKET_FLAGS_1BYTE_SEQUENCE << 4, 229 PACKET_PUBLIC_FLAGS_2BYTE_SEQUENCE = PACKET_FLAGS_2BYTE_SEQUENCE << 4, 230 PACKET_PUBLIC_FLAGS_4BYTE_SEQUENCE = PACKET_FLAGS_4BYTE_SEQUENCE << 4, 231 PACKET_PUBLIC_FLAGS_6BYTE_SEQUENCE = PACKET_FLAGS_6BYTE_SEQUENCE << 4, 232 233 // All bits set (bits 6 and 7 are not currently used): 00111111 234 PACKET_PUBLIC_FLAGS_MAX = (1 << 6) - 1 235}; 236 237// The private flags are specified in one byte. 238enum QuicPacketPrivateFlags { 239 PACKET_PRIVATE_FLAGS_NONE = 0, 240 241 // Bit 0: Does this packet contain an entropy bit? 242 PACKET_PRIVATE_FLAGS_ENTROPY = 1 << 0, 243 244 // Bit 1: Payload is part of an FEC group? 245 PACKET_PRIVATE_FLAGS_FEC_GROUP = 1 << 1, 246 247 // Bit 2: Payload is FEC as opposed to frames? 248 PACKET_PRIVATE_FLAGS_FEC = 1 << 2, 249 250 // All bits set (bits 3-7 are not currently used): 00000111 251 PACKET_PRIVATE_FLAGS_MAX = (1 << 3) - 1 252}; 253 254// The available versions of QUIC. Guaranteed that the integer value of the enum 255// will match the version number. 256// When adding a new version to this enum you should add it to 257// kSupportedQuicVersions (if appropriate), and also add a new case to the 258// helper methods QuicVersionToQuicTag, QuicTagToQuicVersion, and 259// QuicVersionToString. 260enum QuicVersion { 261 // Special case to indicate unknown/unsupported QUIC version. 262 QUIC_VERSION_UNSUPPORTED = 0, 263 264 QUIC_VERSION_15 = 15, 265 QUIC_VERSION_16 = 16, 266 QUIC_VERSION_17 = 17, 267 QUIC_VERSION_18 = 18, // Current version. 268}; 269 270// This vector contains QUIC versions which we currently support. 271// This should be ordered such that the highest supported version is the first 272// element, with subsequent elements in descending order (versions can be 273// skipped as necessary). 274// 275// IMPORTANT: if you are addding to this list, follow the instructions at 276// http://sites/quic/adding-and-removing-versions 277static const QuicVersion kSupportedQuicVersions[] = {QUIC_VERSION_18, 278 QUIC_VERSION_17, 279 QUIC_VERSION_16, 280 QUIC_VERSION_15}; 281 282typedef std::vector<QuicVersion> QuicVersionVector; 283 284// Returns a vector of QUIC versions in kSupportedQuicVersions. 285NET_EXPORT_PRIVATE QuicVersionVector QuicSupportedVersions(); 286 287// QuicTag is written to and read from the wire, but we prefer to use 288// the more readable QuicVersion at other levels. 289// Helper function which translates from a QuicVersion to a QuicTag. Returns 0 290// if QuicVersion is unsupported. 291NET_EXPORT_PRIVATE QuicTag QuicVersionToQuicTag(const QuicVersion version); 292 293// Returns appropriate QuicVersion from a QuicTag. 294// Returns QUIC_VERSION_UNSUPPORTED if version_tag cannot be understood. 295NET_EXPORT_PRIVATE QuicVersion QuicTagToQuicVersion(const QuicTag version_tag); 296 297// Helper function which translates from a QuicVersion to a string. 298// Returns strings corresponding to enum names (e.g. QUIC_VERSION_6). 299NET_EXPORT_PRIVATE std::string QuicVersionToString(const QuicVersion version); 300 301// Returns comma separated list of string representations of QuicVersion enum 302// values in the supplied |versions| vector. 303NET_EXPORT_PRIVATE std::string QuicVersionVectorToString( 304 const QuicVersionVector& versions); 305 306// Version and Crypto tags are written to the wire with a big-endian 307// representation of the name of the tag. For example 308// the client hello tag (CHLO) will be written as the 309// following 4 bytes: 'C' 'H' 'L' 'O'. Since it is 310// stored in memory as a little endian uint32, we need 311// to reverse the order of the bytes. 312 313// MakeQuicTag returns a value given the four bytes. For example: 314// MakeQuicTag('C', 'H', 'L', 'O'); 315NET_EXPORT_PRIVATE QuicTag MakeQuicTag(char a, char b, char c, char d); 316 317// Size in bytes of the data or fec packet header. 318NET_EXPORT_PRIVATE size_t GetPacketHeaderSize(const QuicPacketHeader& header); 319 320NET_EXPORT_PRIVATE size_t GetPacketHeaderSize( 321 QuicConnectionIdLength connection_id_length, 322 bool include_version, 323 QuicSequenceNumberLength sequence_number_length, 324 InFecGroup is_in_fec_group); 325 326// Index of the first byte in a QUIC packet of FEC protected data. 327NET_EXPORT_PRIVATE size_t GetStartOfFecProtectedData( 328 QuicConnectionIdLength connection_id_length, 329 bool include_version, 330 QuicSequenceNumberLength sequence_number_length); 331// Index of the first byte in a QUIC packet of encrypted data. 332NET_EXPORT_PRIVATE size_t GetStartOfEncryptedData( 333 QuicConnectionIdLength connection_id_length, 334 bool include_version, 335 QuicSequenceNumberLength sequence_number_length); 336 337enum QuicRstStreamErrorCode { 338 QUIC_STREAM_NO_ERROR = 0, 339 340 // There was some error which halted stream processing. 341 QUIC_ERROR_PROCESSING_STREAM, 342 // We got two fin or reset offsets which did not match. 343 QUIC_MULTIPLE_TERMINATION_OFFSETS, 344 // We got bad payload and can not respond to it at the protocol level. 345 QUIC_BAD_APPLICATION_PAYLOAD, 346 // Stream closed due to connection error. No reset frame is sent when this 347 // happens. 348 QUIC_STREAM_CONNECTION_ERROR, 349 // GoAway frame sent. No more stream can be created. 350 QUIC_STREAM_PEER_GOING_AWAY, 351 // The stream has been cancelled. 352 QUIC_STREAM_CANCELLED, 353 // Sending a RST to allow for proper flow control accounting. 354 QUIC_RST_FLOW_CONTROL_ACCOUNTING, 355 356 // No error. Used as bound while iterating. 357 QUIC_STREAM_LAST_ERROR, 358}; 359 360// Because receiving an unknown QuicRstStreamErrorCode results in connection 361// teardown, we use this to make sure any errors predating a given version are 362// downgraded to the most appropriate existing error. 363NET_EXPORT_PRIVATE QuicRstStreamErrorCode AdjustErrorForVersion( 364 QuicRstStreamErrorCode error_code, 365 QuicVersion version); 366 367// These values must remain stable as they are uploaded to UMA histograms. 368// To add a new error code, use the current value of QUIC_LAST_ERROR and 369// increment QUIC_LAST_ERROR. 370enum QuicErrorCode { 371 QUIC_NO_ERROR = 0, 372 373 // Connection has reached an invalid state. 374 QUIC_INTERNAL_ERROR = 1, 375 // There were data frames after the a fin or reset. 376 QUIC_STREAM_DATA_AFTER_TERMINATION = 2, 377 // Control frame is malformed. 378 QUIC_INVALID_PACKET_HEADER = 3, 379 // Frame data is malformed. 380 QUIC_INVALID_FRAME_DATA = 4, 381 // The packet contained no payload. 382 QUIC_MISSING_PAYLOAD = 48, 383 // FEC data is malformed. 384 QUIC_INVALID_FEC_DATA = 5, 385 // STREAM frame data is malformed. 386 QUIC_INVALID_STREAM_DATA = 46, 387 // STREAM frame data is not encrypted. 388 QUIC_UNENCRYPTED_STREAM_DATA = 61, 389 // RST_STREAM frame data is malformed. 390 QUIC_INVALID_RST_STREAM_DATA = 6, 391 // CONNECTION_CLOSE frame data is malformed. 392 QUIC_INVALID_CONNECTION_CLOSE_DATA = 7, 393 // GOAWAY frame data is malformed. 394 QUIC_INVALID_GOAWAY_DATA = 8, 395 // WINDOW_UPDATE frame data is malformed. 396 QUIC_INVALID_WINDOW_UPDATE_DATA = 57, 397 // BLOCKED frame data is malformed. 398 QUIC_INVALID_BLOCKED_DATA = 58, 399 // STOP_WAITING frame data is malformed. 400 QUIC_INVALID_STOP_WAITING_DATA = 60, 401 // ACK frame data is malformed. 402 QUIC_INVALID_ACK_DATA = 9, 403 // CONGESTION_FEEDBACK frame data is malformed. 404 QUIC_INVALID_CONGESTION_FEEDBACK_DATA = 47, 405 // Version negotiation packet is malformed. 406 QUIC_INVALID_VERSION_NEGOTIATION_PACKET = 10, 407 // Public RST packet is malformed. 408 QUIC_INVALID_PUBLIC_RST_PACKET = 11, 409 // There was an error decrypting. 410 QUIC_DECRYPTION_FAILURE = 12, 411 // There was an error encrypting. 412 QUIC_ENCRYPTION_FAILURE = 13, 413 // The packet exceeded kMaxPacketSize. 414 QUIC_PACKET_TOO_LARGE = 14, 415 // Data was sent for a stream which did not exist. 416 QUIC_PACKET_FOR_NONEXISTENT_STREAM = 15, 417 // The peer is going away. May be a client or server. 418 QUIC_PEER_GOING_AWAY = 16, 419 // A stream ID was invalid. 420 QUIC_INVALID_STREAM_ID = 17, 421 // A priority was invalid. 422 QUIC_INVALID_PRIORITY = 49, 423 // Too many streams already open. 424 QUIC_TOO_MANY_OPEN_STREAMS = 18, 425 // Received public reset for this connection. 426 QUIC_PUBLIC_RESET = 19, 427 // Invalid protocol version. 428 QUIC_INVALID_VERSION = 20, 429 430 // deprecated: QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED = 21 431 432 // The Header ID for a stream was too far from the previous. 433 QUIC_INVALID_HEADER_ID = 22, 434 // Negotiable parameter received during handshake had invalid value. 435 QUIC_INVALID_NEGOTIATED_VALUE = 23, 436 // There was an error decompressing data. 437 QUIC_DECOMPRESSION_FAILURE = 24, 438 // We hit our prenegotiated (or default) timeout 439 QUIC_CONNECTION_TIMED_OUT = 25, 440 // There was an error encountered migrating addresses 441 QUIC_ERROR_MIGRATING_ADDRESS = 26, 442 // There was an error while writing to the socket. 443 QUIC_PACKET_WRITE_ERROR = 27, 444 // There was an error while reading from the socket. 445 QUIC_PACKET_READ_ERROR = 51, 446 // We received a STREAM_FRAME with no data and no fin flag set. 447 QUIC_INVALID_STREAM_FRAME = 50, 448 // We received invalid data on the headers stream. 449 QUIC_INVALID_HEADERS_STREAM_DATA = 56, 450 // The peer violated the flow control protocol. 451 QUIC_FLOW_CONTROL_ERROR = 59, 452 453 // Crypto errors. 454 455 // Hanshake failed. 456 QUIC_HANDSHAKE_FAILED = 28, 457 // Handshake message contained out of order tags. 458 QUIC_CRYPTO_TAGS_OUT_OF_ORDER = 29, 459 // Handshake message contained too many entries. 460 QUIC_CRYPTO_TOO_MANY_ENTRIES = 30, 461 // Handshake message contained an invalid value length. 462 QUIC_CRYPTO_INVALID_VALUE_LENGTH = 31, 463 // A crypto message was received after the handshake was complete. 464 QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE = 32, 465 // A crypto message was received with an illegal message tag. 466 QUIC_INVALID_CRYPTO_MESSAGE_TYPE = 33, 467 // A crypto message was received with an illegal parameter. 468 QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER = 34, 469 // An invalid channel id signature was supplied. 470 QUIC_INVALID_CHANNEL_ID_SIGNATURE = 52, 471 // A crypto message was received with a mandatory parameter missing. 472 QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND = 35, 473 // A crypto message was received with a parameter that has no overlap 474 // with the local parameter. 475 QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP = 36, 476 // A crypto message was received that contained a parameter with too few 477 // values. 478 QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND = 37, 479 // An internal error occured in crypto processing. 480 QUIC_CRYPTO_INTERNAL_ERROR = 38, 481 // A crypto handshake message specified an unsupported version. 482 QUIC_CRYPTO_VERSION_NOT_SUPPORTED = 39, 483 // There was no intersection between the crypto primitives supported by the 484 // peer and ourselves. 485 QUIC_CRYPTO_NO_SUPPORT = 40, 486 // The server rejected our client hello messages too many times. 487 QUIC_CRYPTO_TOO_MANY_REJECTS = 41, 488 // The client rejected the server's certificate chain or signature. 489 QUIC_PROOF_INVALID = 42, 490 // A crypto message was received with a duplicate tag. 491 QUIC_CRYPTO_DUPLICATE_TAG = 43, 492 // A crypto message was received with the wrong encryption level (i.e. it 493 // should have been encrypted but was not.) 494 QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT = 44, 495 // The server config for a server has expired. 496 QUIC_CRYPTO_SERVER_CONFIG_EXPIRED = 45, 497 // We failed to setup the symmetric keys for a connection. 498 QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED = 53, 499 // A handshake message arrived, but we are still validating the 500 // previous handshake message. 501 QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO = 54, 502 // This connection involved a version negotiation which appears to have been 503 // tampered with. 504 QUIC_VERSION_NEGOTIATION_MISMATCH = 55, 505 506 // No error. Used as bound while iterating. 507 QUIC_LAST_ERROR = 62, 508}; 509 510struct NET_EXPORT_PRIVATE QuicPacketPublicHeader { 511 QuicPacketPublicHeader(); 512 explicit QuicPacketPublicHeader(const QuicPacketPublicHeader& other); 513 ~QuicPacketPublicHeader(); 514 515 // Universal header. All QuicPacket headers will have a connection_id and 516 // public flags. 517 QuicConnectionId connection_id; 518 QuicConnectionIdLength connection_id_length; 519 bool reset_flag; 520 bool version_flag; 521 QuicSequenceNumberLength sequence_number_length; 522 QuicVersionVector versions; 523}; 524 525// Header for Data or FEC packets. 526struct NET_EXPORT_PRIVATE QuicPacketHeader { 527 QuicPacketHeader(); 528 explicit QuicPacketHeader(const QuicPacketPublicHeader& header); 529 530 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 531 std::ostream& os, const QuicPacketHeader& s); 532 533 QuicPacketPublicHeader public_header; 534 bool fec_flag; 535 bool entropy_flag; 536 QuicPacketEntropyHash entropy_hash; 537 QuicPacketSequenceNumber packet_sequence_number; 538 InFecGroup is_in_fec_group; 539 QuicFecGroupNumber fec_group; 540}; 541 542struct NET_EXPORT_PRIVATE QuicPublicResetPacket { 543 QuicPublicResetPacket(); 544 explicit QuicPublicResetPacket(const QuicPacketPublicHeader& header); 545 546 QuicPacketPublicHeader public_header; 547 QuicPublicResetNonceProof nonce_proof; 548 QuicPacketSequenceNumber rejected_sequence_number; 549 IPEndPoint client_address; 550}; 551 552enum QuicVersionNegotiationState { 553 START_NEGOTIATION = 0, 554 // Server-side this implies we've sent a version negotiation packet and are 555 // waiting on the client to select a compatible version. Client-side this 556 // implies we've gotten a version negotiation packet, are retransmitting the 557 // initial packets with a supported version and are waiting for our first 558 // packet from the server. 559 NEGOTIATION_IN_PROGRESS, 560 // This indicates this endpoint has received a packet from the peer with a 561 // version this endpoint supports. Version negotiation is complete, and the 562 // version number will no longer be sent with future packets. 563 NEGOTIATED_VERSION 564}; 565 566typedef QuicPacketPublicHeader QuicVersionNegotiationPacket; 567 568// A padding frame contains no payload. 569struct NET_EXPORT_PRIVATE QuicPaddingFrame { 570}; 571 572// A ping frame contains no payload, though it is retransmittable, 573// and ACK'd just like other normal frames. 574struct NET_EXPORT_PRIVATE QuicPingFrame { 575}; 576 577struct NET_EXPORT_PRIVATE QuicStreamFrame { 578 QuicStreamFrame(); 579 QuicStreamFrame(const QuicStreamFrame& frame); 580 QuicStreamFrame(QuicStreamId stream_id, 581 bool fin, 582 QuicStreamOffset offset, 583 IOVector data); 584 585 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 586 std::ostream& os, const QuicStreamFrame& s); 587 588 // Returns a copy of the IOVector |data| as a heap-allocated string. 589 // Caller must take ownership of the returned string. 590 std::string* GetDataAsString() const; 591 592 QuicStreamId stream_id; 593 bool fin; 594 QuicStreamOffset offset; // Location of this data in the stream. 595 IOVector data; 596 597 // If this is set, then when this packet is ACKed the AckNotifier will be 598 // informed. 599 QuicAckNotifier* notifier; 600}; 601 602// TODO(ianswett): Re-evaluate the trade-offs of hash_set vs set when framing 603// is finalized. 604typedef std::set<QuicPacketSequenceNumber> SequenceNumberSet; 605// TODO(pwestin): Add a way to enforce the max size of this map. 606typedef std::map<QuicPacketSequenceNumber, QuicTime> TimeMap; 607 608struct NET_EXPORT_PRIVATE ReceivedPacketInfo { 609 ReceivedPacketInfo(); 610 ~ReceivedPacketInfo(); 611 612 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 613 std::ostream& os, const ReceivedPacketInfo& s); 614 615 // Entropy hash of all packets up to largest observed not including missing 616 // packets. 617 QuicPacketEntropyHash entropy_hash; 618 619 // The highest packet sequence number we've observed from the peer. 620 // 621 // In general, this should be the largest packet number we've received. In 622 // the case of truncated acks, we may have to advertise a lower "upper bound" 623 // than largest received, to avoid implicitly acking missing packets that 624 // don't fit in the missing packet list due to size limitations. In this 625 // case, largest_observed may be a packet which is also in the missing packets 626 // list. 627 QuicPacketSequenceNumber largest_observed; 628 629 // Time elapsed since largest_observed was received until this Ack frame was 630 // sent. 631 QuicTime::Delta delta_time_largest_observed; 632 633 // TODO(satyamshekhar): Can be optimized using an interval set like data 634 // structure. 635 // The set of packets which we're expecting and have not received. 636 SequenceNumberSet missing_packets; 637 638 // Whether the ack had to be truncated when sent. 639 bool is_truncated; 640 641 // Packets which have been revived via FEC. 642 // All of these must also be in missing_packets. 643 SequenceNumberSet revived_packets; 644}; 645 646// True if the sequence number is greater than largest_observed or is listed 647// as missing. 648// Always returns false for sequence numbers less than least_unacked. 649bool NET_EXPORT_PRIVATE IsAwaitingPacket( 650 const ReceivedPacketInfo& received_info, 651 QuicPacketSequenceNumber sequence_number); 652 653// Inserts missing packets between [lower, higher). 654void NET_EXPORT_PRIVATE InsertMissingPacketsBetween( 655 ReceivedPacketInfo* received_info, 656 QuicPacketSequenceNumber lower, 657 QuicPacketSequenceNumber higher); 658 659struct NET_EXPORT_PRIVATE QuicStopWaitingFrame { 660 QuicStopWaitingFrame(); 661 ~QuicStopWaitingFrame(); 662 663 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 664 std::ostream& os, const QuicStopWaitingFrame& s); 665 666 // Entropy hash of all packets up to, but not including, the least unacked 667 // packet. 668 QuicPacketEntropyHash entropy_hash; 669 // The lowest packet we've sent which is unacked, and we expect an ack for. 670 QuicPacketSequenceNumber least_unacked; 671}; 672 673struct NET_EXPORT_PRIVATE QuicAckFrame { 674 QuicAckFrame(); 675 676 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 677 std::ostream& os, const QuicAckFrame& s); 678 679 QuicStopWaitingFrame sent_info; 680 ReceivedPacketInfo received_info; 681}; 682 683// Defines for all types of congestion feedback that will be negotiated in QUIC, 684// kTCP MUST be supported by all QUIC implementations to guarantee 100% 685// compatibility. 686enum CongestionFeedbackType { 687 kTCP, // Used to mimic TCP. 688 kInterArrival, // Use additional inter arrival information. 689 kFixRate, // Provided for testing. 690}; 691 692enum LossDetectionType { 693 kNack, // Used to mimic TCP's loss detection. 694 kTime, // Time based loss detection. 695}; 696 697struct NET_EXPORT_PRIVATE CongestionFeedbackMessageTCP { 698 CongestionFeedbackMessageTCP(); 699 700 QuicByteCount receive_window; 701}; 702 703struct NET_EXPORT_PRIVATE CongestionFeedbackMessageInterArrival { 704 CongestionFeedbackMessageInterArrival(); 705 ~CongestionFeedbackMessageInterArrival(); 706 707 // The set of received packets since the last feedback was sent, along with 708 // their arrival times. 709 TimeMap received_packet_times; 710}; 711 712struct NET_EXPORT_PRIVATE CongestionFeedbackMessageFixRate { 713 CongestionFeedbackMessageFixRate(); 714 QuicBandwidth bitrate; 715}; 716 717struct NET_EXPORT_PRIVATE QuicCongestionFeedbackFrame { 718 QuicCongestionFeedbackFrame(); 719 ~QuicCongestionFeedbackFrame(); 720 721 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 722 std::ostream& os, const QuicCongestionFeedbackFrame& c); 723 724 CongestionFeedbackType type; 725 // This should really be a union, but since the inter arrival struct 726 // is non-trivial, C++ prohibits it. 727 CongestionFeedbackMessageTCP tcp; 728 CongestionFeedbackMessageInterArrival inter_arrival; 729 CongestionFeedbackMessageFixRate fix_rate; 730}; 731 732struct NET_EXPORT_PRIVATE QuicRstStreamFrame { 733 QuicRstStreamFrame(); 734 QuicRstStreamFrame(QuicStreamId stream_id, 735 QuicRstStreamErrorCode error_code, 736 QuicStreamOffset bytes_written); 737 738 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 739 std::ostream& os, const QuicRstStreamFrame& r); 740 741 QuicStreamId stream_id; 742 QuicRstStreamErrorCode error_code; 743 std::string error_details; 744 745 // Used to update flow control windows. On termination of a stream, both 746 // endpoints must inform the peer of the number of bytes they have sent on 747 // that stream. This can be done through normal termination (data packet with 748 // FIN) or through a RST. 749 QuicStreamOffset byte_offset; 750}; 751 752struct NET_EXPORT_PRIVATE QuicConnectionCloseFrame { 753 QuicConnectionCloseFrame(); 754 755 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 756 std::ostream& os, const QuicConnectionCloseFrame& c); 757 758 QuicErrorCode error_code; 759 std::string error_details; 760}; 761 762struct NET_EXPORT_PRIVATE QuicGoAwayFrame { 763 QuicGoAwayFrame(); 764 QuicGoAwayFrame(QuicErrorCode error_code, 765 QuicStreamId last_good_stream_id, 766 const std::string& reason); 767 768 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 769 std::ostream& os, const QuicGoAwayFrame& g); 770 771 QuicErrorCode error_code; 772 QuicStreamId last_good_stream_id; 773 std::string reason_phrase; 774}; 775 776// Flow control updates per-stream and at the connection levoel. 777// Based on SPDY's WINDOW_UPDATE frame, but uses an absolute byte offset rather 778// than a window delta. 779// TODO(rjshade): A possible future optimization is to make stream_id and 780// byte_offset variable length, similar to stream frames. 781struct NET_EXPORT_PRIVATE QuicWindowUpdateFrame { 782 QuicWindowUpdateFrame() {} 783 QuicWindowUpdateFrame(QuicStreamId stream_id, QuicStreamOffset byte_offset); 784 785 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 786 std::ostream& os, const QuicWindowUpdateFrame& w); 787 788 // The stream this frame applies to. 0 is a special case meaning the overall 789 // connection rather than a specific stream. 790 QuicStreamId stream_id; 791 792 // Byte offset in the stream or connection. The receiver of this frame must 793 // not send data which would result in this offset being exceeded. 794 QuicStreamOffset byte_offset; 795}; 796 797// The BLOCKED frame is used to indicate to the remote endpoint that this 798// endpoint believes itself to be flow-control blocked but otherwise ready to 799// send data. The BLOCKED frame is purely advisory and optional. 800// Based on SPDY's BLOCKED frame (undocumented as of 2014-01-28). 801struct NET_EXPORT_PRIVATE QuicBlockedFrame { 802 QuicBlockedFrame() {} 803 explicit QuicBlockedFrame(QuicStreamId stream_id); 804 805 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 806 std::ostream& os, const QuicBlockedFrame& b); 807 808 // The stream this frame applies to. 0 is a special case meaning the overall 809 // connection rather than a specific stream. 810 QuicStreamId stream_id; 811}; 812 813// EncryptionLevel enumerates the stages of encryption that a QUIC connection 814// progresses through. When retransmitting a packet, the encryption level needs 815// to be specified so that it is retransmitted at a level which the peer can 816// understand. 817enum EncryptionLevel { 818 ENCRYPTION_NONE = 0, 819 ENCRYPTION_INITIAL = 1, 820 ENCRYPTION_FORWARD_SECURE = 2, 821 822 NUM_ENCRYPTION_LEVELS, 823}; 824 825struct NET_EXPORT_PRIVATE QuicFrame { 826 QuicFrame(); 827 explicit QuicFrame(QuicPaddingFrame* padding_frame); 828 explicit QuicFrame(QuicStreamFrame* stream_frame); 829 explicit QuicFrame(QuicAckFrame* frame); 830 explicit QuicFrame(QuicCongestionFeedbackFrame* frame); 831 explicit QuicFrame(QuicRstStreamFrame* frame); 832 explicit QuicFrame(QuicConnectionCloseFrame* frame); 833 explicit QuicFrame(QuicStopWaitingFrame* frame); 834 explicit QuicFrame(QuicPingFrame* frame); 835 explicit QuicFrame(QuicGoAwayFrame* frame); 836 explicit QuicFrame(QuicWindowUpdateFrame* frame); 837 explicit QuicFrame(QuicBlockedFrame* frame); 838 839 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 840 std::ostream& os, const QuicFrame& frame); 841 842 QuicFrameType type; 843 union { 844 QuicPaddingFrame* padding_frame; 845 QuicStreamFrame* stream_frame; 846 QuicAckFrame* ack_frame; 847 QuicCongestionFeedbackFrame* congestion_feedback_frame; 848 QuicStopWaitingFrame* stop_waiting_frame; 849 QuicPingFrame* ping_frame; 850 QuicRstStreamFrame* rst_stream_frame; 851 QuicConnectionCloseFrame* connection_close_frame; 852 QuicGoAwayFrame* goaway_frame; 853 QuicWindowUpdateFrame* window_update_frame; 854 QuicBlockedFrame* blocked_frame; 855 }; 856}; 857 858typedef std::vector<QuicFrame> QuicFrames; 859 860struct NET_EXPORT_PRIVATE QuicFecData { 861 QuicFecData(); 862 863 // The FEC group number is also the sequence number of the first 864 // FEC protected packet. The last protected packet's sequence number will 865 // be one less than the sequence number of the FEC packet. 866 QuicFecGroupNumber fec_group; 867 base::StringPiece redundancy; 868}; 869 870class NET_EXPORT_PRIVATE QuicData { 871 public: 872 QuicData(const char* buffer, size_t length); 873 QuicData(char* buffer, size_t length, bool owns_buffer); 874 virtual ~QuicData(); 875 876 base::StringPiece AsStringPiece() const { 877 return base::StringPiece(data(), length()); 878 } 879 880 const char* data() const { return buffer_; } 881 size_t length() const { return length_; } 882 883 private: 884 const char* buffer_; 885 size_t length_; 886 bool owns_buffer_; 887 888 DISALLOW_COPY_AND_ASSIGN(QuicData); 889}; 890 891class NET_EXPORT_PRIVATE QuicPacket : public QuicData { 892 public: 893 static QuicPacket* NewDataPacket( 894 char* buffer, 895 size_t length, 896 bool owns_buffer, 897 QuicConnectionIdLength connection_id_length, 898 bool includes_version, 899 QuicSequenceNumberLength sequence_number_length) { 900 return new QuicPacket(buffer, length, owns_buffer, connection_id_length, 901 includes_version, sequence_number_length, false); 902 } 903 904 static QuicPacket* NewFecPacket( 905 char* buffer, 906 size_t length, 907 bool owns_buffer, 908 QuicConnectionIdLength connection_id_length, 909 bool includes_version, 910 QuicSequenceNumberLength sequence_number_length) { 911 return new QuicPacket(buffer, length, owns_buffer, connection_id_length, 912 includes_version, sequence_number_length, true); 913 } 914 915 base::StringPiece FecProtectedData() const; 916 base::StringPiece AssociatedData() const; 917 base::StringPiece BeforePlaintext() const; 918 base::StringPiece Plaintext() const; 919 920 bool is_fec_packet() const { return is_fec_packet_; } 921 922 char* mutable_data() { return buffer_; } 923 924 private: 925 QuicPacket(char* buffer, 926 size_t length, 927 bool owns_buffer, 928 QuicConnectionIdLength connection_id_length, 929 bool includes_version, 930 QuicSequenceNumberLength sequence_number_length, 931 bool is_fec_packet); 932 933 char* buffer_; 934 const bool is_fec_packet_; 935 const QuicConnectionIdLength connection_id_length_; 936 const bool includes_version_; 937 const QuicSequenceNumberLength sequence_number_length_; 938 939 DISALLOW_COPY_AND_ASSIGN(QuicPacket); 940}; 941 942class NET_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData { 943 public: 944 QuicEncryptedPacket(const char* buffer, size_t length); 945 QuicEncryptedPacket(char* buffer, size_t length, bool owns_buffer); 946 947 // Clones the packet into a new packet which owns the buffer. 948 QuicEncryptedPacket* Clone() const; 949 950 // By default, gtest prints the raw bytes of an object. The bool data 951 // member (in the base class QuicData) causes this object to have padding 952 // bytes, which causes the default gtest object printer to read 953 // uninitialize memory. So we need to teach gtest how to print this object. 954 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 955 std::ostream& os, const QuicEncryptedPacket& s); 956 957 private: 958 DISALLOW_COPY_AND_ASSIGN(QuicEncryptedPacket); 959}; 960 961class NET_EXPORT_PRIVATE RetransmittableFrames { 962 public: 963 RetransmittableFrames(); 964 ~RetransmittableFrames(); 965 966 // Allocates a local copy of the referenced StringPiece has QuicStreamFrame 967 // use it. 968 // Takes ownership of |stream_frame|. 969 const QuicFrame& AddStreamFrame(QuicStreamFrame* stream_frame); 970 // Takes ownership of the frame inside |frame|. 971 const QuicFrame& AddNonStreamFrame(const QuicFrame& frame); 972 const QuicFrames& frames() const { return frames_; } 973 974 IsHandshake HasCryptoHandshake() const; 975 976 void set_encryption_level(EncryptionLevel level); 977 EncryptionLevel encryption_level() const { 978 return encryption_level_; 979 } 980 981 private: 982 QuicFrames frames_; 983 EncryptionLevel encryption_level_; 984 // Data referenced by the StringPiece of a QuicStreamFrame. 985 std::vector<std::string*> stream_data_; 986 987 DISALLOW_COPY_AND_ASSIGN(RetransmittableFrames); 988}; 989 990struct NET_EXPORT_PRIVATE SerializedPacket { 991 SerializedPacket(QuicPacketSequenceNumber sequence_number, 992 QuicSequenceNumberLength sequence_number_length, 993 QuicPacket* packet, 994 QuicPacketEntropyHash entropy_hash, 995 RetransmittableFrames* retransmittable_frames); 996 ~SerializedPacket(); 997 998 QuicPacketSequenceNumber sequence_number; 999 QuicSequenceNumberLength sequence_number_length; 1000 QuicPacket* packet; 1001 QuicPacketEntropyHash entropy_hash; 1002 RetransmittableFrames* retransmittable_frames; 1003 1004 // If set, these will be called when this packet is ACKed by the peer. 1005 std::set<QuicAckNotifier*> notifiers; 1006}; 1007 1008// A struct for functions which consume data payloads and fins. 1009struct NET_EXPORT_PRIVATE QuicConsumedData { 1010 QuicConsumedData(size_t bytes_consumed, bool fin_consumed); 1011 1012 // By default, gtest prints the raw bytes of an object. The bool data 1013 // member causes this object to have padding bytes, which causes the 1014 // default gtest object printer to read uninitialize memory. So we need 1015 // to teach gtest how to print this object. 1016 NET_EXPORT_PRIVATE friend std::ostream& operator<<( 1017 std::ostream& os, const QuicConsumedData& s); 1018 1019 // How many bytes were consumed. 1020 size_t bytes_consumed; 1021 1022 // True if an incoming fin was consumed. 1023 bool fin_consumed; 1024}; 1025 1026enum WriteStatus { 1027 WRITE_STATUS_OK, 1028 WRITE_STATUS_BLOCKED, 1029 WRITE_STATUS_ERROR, 1030}; 1031 1032// A struct used to return the result of write calls including either the number 1033// of bytes written or the error code, depending upon the status. 1034struct NET_EXPORT_PRIVATE WriteResult { 1035 WriteResult(WriteStatus status, int bytes_written_or_error_code); 1036 WriteResult(); 1037 1038 WriteStatus status; 1039 union { 1040 int bytes_written; // only valid when status is OK 1041 int error_code; // only valid when status is ERROR 1042 }; 1043}; 1044 1045} // namespace net 1046 1047#endif // NET_QUIC_QUIC_PROTOCOL_H_ 1048