QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 2018 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 QUICHE_QUIC_CORE_QUIC_CONNECTION_ID_H_ |
| 6 | #define QUICHE_QUIC_CORE_QUIC_CONNECTION_ID_H_ |
| 7 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 9 | #include <vector> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 10 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_uint128.h" |
| 13 | |
| 14 | namespace quic { |
| 15 | |
| 16 | enum QuicConnectionIdLength { |
| 17 | PACKET_0BYTE_CONNECTION_ID = 0, |
| 18 | PACKET_8BYTE_CONNECTION_ID = 8, |
| 19 | }; |
| 20 | |
| 21 | // This is a property of QUIC headers, it indicates whether the connection ID |
| 22 | // should actually be sent over the wire (or was sent on received packets). |
| 23 | enum QuicConnectionIdIncluded : uint8_t { |
| 24 | CONNECTION_ID_PRESENT = 1, |
| 25 | CONNECTION_ID_ABSENT = 2, |
| 26 | }; |
| 27 | |
dschinazi | b953d02 | 2019-08-01 18:05:58 -0700 | [diff] [blame] | 28 | // Maximum connection ID length that we support in any packet or version. |
dschinazi | b012d21 | 2019-08-01 18:07:26 -0700 | [diff] [blame] | 29 | const uint8_t kQuicMaxConnectionIdAllVersionsLength = 20; |
| 30 | |
| 31 | // Maximum connection ID length supported by versions that use the encoding from |
| 32 | // draft-ietf-quic-invariants-06. |
| 33 | const uint8_t kQuicMaxConnectionIdWithLengthPrefixLength = 20; |
| 34 | |
| 35 | // Maximum connection ID length supported by versions that use the encoding from |
| 36 | // draft-ietf-quic-invariants-05. |
| 37 | const uint8_t kQuicMaxConnectionId4BitLength = 18; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 38 | |
| 39 | // kQuicDefaultConnectionIdLength is the only supported length for QUIC |
| 40 | // versions < v99, and is the default picked for all versions. |
| 41 | const uint8_t kQuicDefaultConnectionIdLength = 8; |
| 42 | |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 43 | // According to the IETF spec, the initial server connection ID generated by |
| 44 | // the client must be at least this long. |
| 45 | const uint8_t kQuicMinimumInitialConnectionIdLength = 8; |
| 46 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 47 | class QUIC_EXPORT_PRIVATE QuicConnectionId { |
| 48 | public: |
| 49 | // Creates a connection ID of length zero. |
| 50 | QuicConnectionId(); |
| 51 | |
| 52 | // Creates a connection ID from network order bytes. |
| 53 | QuicConnectionId(const char* data, uint8_t length); |
| 54 | |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 55 | // Creates a connection ID from another connection ID. |
| 56 | QuicConnectionId(const QuicConnectionId& other); |
| 57 | |
| 58 | // Assignment operator. |
| 59 | QuicConnectionId& operator=(const QuicConnectionId& other); |
| 60 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 61 | ~QuicConnectionId(); |
| 62 | |
| 63 | // Returns the length of the connection ID, in bytes. |
| 64 | uint8_t length() const; |
| 65 | |
| 66 | // Sets the length of the connection ID, in bytes. |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 67 | // WARNING: Calling set_length() can change the in-memory location of the |
| 68 | // connection ID. Callers must therefore ensure they call data() or |
| 69 | // mutable_data() after they call set_length(). |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 70 | void set_length(uint8_t length); |
| 71 | |
| 72 | // Returns a pointer to the connection ID bytes, in network byte order. |
| 73 | const char* data() const; |
| 74 | |
| 75 | // Returns a mutable pointer to the connection ID bytes, |
| 76 | // in network byte order. |
| 77 | char* mutable_data(); |
| 78 | |
| 79 | // Returns whether the connection ID has length zero. |
| 80 | bool IsEmpty() const; |
| 81 | |
| 82 | // Hash() is required to use connection IDs as keys in hash tables. |
dschinazi | 0fdff8e | 2019-07-18 09:07:39 -0700 | [diff] [blame] | 83 | // During the lifetime of a process, the output of Hash() is guaranteed to be |
| 84 | // the same for connection IDs that are equal to one another. Note however |
| 85 | // that this property is not guaranteed across process lifetimes. This makes |
| 86 | // Hash() suitable for data structures such as hash tables but not for sending |
| 87 | // a hash over the network. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 88 | size_t Hash() const; |
| 89 | |
| 90 | // Generates an ASCII string that represents |
| 91 | // the contents of the connection ID, or "0" if it is empty. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 92 | std::string ToString() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 93 | |
| 94 | // operator<< allows easily logging connection IDs. |
| 95 | friend QUIC_EXPORT_PRIVATE std::ostream& operator<<( |
| 96 | std::ostream& os, |
| 97 | const QuicConnectionId& v); |
| 98 | |
| 99 | bool operator==(const QuicConnectionId& v) const; |
| 100 | bool operator!=(const QuicConnectionId& v) const; |
| 101 | // operator< is required to use connection IDs as keys in hash tables. |
| 102 | bool operator<(const QuicConnectionId& v) const; |
| 103 | |
| 104 | private: |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 105 | uint8_t length_; // length of the connection ID, in bytes. |
| 106 | // The connection ID is represented in network byte order. |
| 107 | union { |
dschinazi | 0d06d7b | 2019-08-19 14:33:07 -0700 | [diff] [blame] | 108 | // If the connection ID fits in |data_short_|, it is stored in the |
| 109 | // first |length_| bytes of |data_short_|. |
| 110 | // Otherwise it is stored in |data_long_| which is guaranteed to have a size |
| 111 | // equal to |length_|. |
| 112 | // A value of 11 was chosen because our commonly used connection ID length |
| 113 | // is 8 and with the length, the class is padded to 12 bytes anyway. |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 114 | char data_short_[11]; |
| 115 | char* data_long_; |
| 116 | }; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | // Creates a connection ID of length zero, unless the restart flag |
| 120 | // quic_connection_ids_network_byte_order is false in which case |
| 121 | // it returns an 8-byte all-zeroes connection ID. |
| 122 | QUIC_EXPORT_PRIVATE QuicConnectionId EmptyQuicConnectionId(); |
| 123 | |
| 124 | // QuicConnectionIdHash can be passed as hash argument to hash tables. |
dschinazi | 0fdff8e | 2019-07-18 09:07:39 -0700 | [diff] [blame] | 125 | // During the lifetime of a process, the output of QuicConnectionIdHash is |
| 126 | // guaranteed to be the same for connection IDs that are equal to one another. |
| 127 | // Note however that this property is not guaranteed across process lifetimes. |
| 128 | // This makes QuicConnectionIdHash suitable for data structures such as hash |
| 129 | // tables but not for sending a hash over the network. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 130 | class QuicConnectionIdHash { |
| 131 | public: |
| 132 | size_t operator()(QuicConnectionId const& connection_id) const noexcept { |
| 133 | return connection_id.Hash(); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | } // namespace quic |
| 138 | |
| 139 | #endif // QUICHE_QUIC_CORE_QUIC_CONNECTION_ID_H_ |