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 | |
| 28 | // Connection IDs can be 0-18 bytes per IETF specifications. |
| 29 | const uint8_t kQuicMaxConnectionIdLength = 18; |
| 30 | |
| 31 | // kQuicDefaultConnectionIdLength is the only supported length for QUIC |
| 32 | // versions < v99, and is the default picked for all versions. |
| 33 | const uint8_t kQuicDefaultConnectionIdLength = 8; |
| 34 | |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 35 | // According to the IETF spec, the initial server connection ID generated by |
| 36 | // the client must be at least this long. |
| 37 | const uint8_t kQuicMinimumInitialConnectionIdLength = 8; |
| 38 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 39 | class QUIC_EXPORT_PRIVATE QuicConnectionId { |
| 40 | public: |
| 41 | // Creates a connection ID of length zero. |
| 42 | QuicConnectionId(); |
| 43 | |
| 44 | // Creates a connection ID from network order bytes. |
| 45 | QuicConnectionId(const char* data, uint8_t length); |
| 46 | |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 47 | // Creates a connection ID from another connection ID. |
| 48 | QuicConnectionId(const QuicConnectionId& other); |
| 49 | |
| 50 | // Assignment operator. |
| 51 | QuicConnectionId& operator=(const QuicConnectionId& other); |
| 52 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 53 | ~QuicConnectionId(); |
| 54 | |
| 55 | // Returns the length of the connection ID, in bytes. |
| 56 | uint8_t length() const; |
| 57 | |
| 58 | // Sets the length of the connection ID, in bytes. |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 59 | // WARNING: Calling set_length() can change the in-memory location of the |
| 60 | // connection ID. Callers must therefore ensure they call data() or |
| 61 | // mutable_data() after they call set_length(). |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 62 | void set_length(uint8_t length); |
| 63 | |
| 64 | // Returns a pointer to the connection ID bytes, in network byte order. |
| 65 | const char* data() const; |
| 66 | |
| 67 | // Returns a mutable pointer to the connection ID bytes, |
| 68 | // in network byte order. |
| 69 | char* mutable_data(); |
| 70 | |
| 71 | // Returns whether the connection ID has length zero. |
| 72 | bool IsEmpty() const; |
| 73 | |
| 74 | // Hash() is required to use connection IDs as keys in hash tables. |
| 75 | size_t Hash() const; |
| 76 | |
| 77 | // Generates an ASCII string that represents |
| 78 | // the contents of the connection ID, or "0" if it is empty. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 79 | std::string ToString() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 80 | |
| 81 | // operator<< allows easily logging connection IDs. |
| 82 | friend QUIC_EXPORT_PRIVATE std::ostream& operator<<( |
| 83 | std::ostream& os, |
| 84 | const QuicConnectionId& v); |
| 85 | |
| 86 | bool operator==(const QuicConnectionId& v) const; |
| 87 | bool operator!=(const QuicConnectionId& v) const; |
| 88 | // operator< is required to use connection IDs as keys in hash tables. |
| 89 | bool operator<(const QuicConnectionId& v) const; |
| 90 | |
| 91 | private: |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 92 | uint8_t length_; // length of the connection ID, in bytes. |
| 93 | // The connection ID is represented in network byte order. |
| 94 | union { |
| 95 | // When quic_use_allocated_connection_ids is false, the connection ID is |
| 96 | // stored in the first |length_| bytes of |data_|. |
| 97 | char data_[kQuicMaxConnectionIdLength]; |
| 98 | // When quic_use_allocated_connection_ids is true, if the connection ID |
| 99 | // fits in |data_short_|, it is stored in the first |length_| bytes of |
| 100 | // |data_short_|. Otherwise it is stored in |data_long_| which is |
| 101 | // guaranteed to have a size equal to |length_|. A value of 11 was chosen |
| 102 | // because our commonly used connection ID length is 8 and with the length, |
| 103 | // the class is padded to 12 bytes anyway. |
| 104 | char data_short_[11]; |
| 105 | char* data_long_; |
| 106 | }; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | // Creates a connection ID of length zero, unless the restart flag |
| 110 | // quic_connection_ids_network_byte_order is false in which case |
| 111 | // it returns an 8-byte all-zeroes connection ID. |
| 112 | QUIC_EXPORT_PRIVATE QuicConnectionId EmptyQuicConnectionId(); |
| 113 | |
| 114 | // QuicConnectionIdHash can be passed as hash argument to hash tables. |
| 115 | class QuicConnectionIdHash { |
| 116 | public: |
| 117 | size_t operator()(QuicConnectionId const& connection_id) const noexcept { |
| 118 | return connection_id.Hash(); |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | } // namespace quic |
| 123 | |
| 124 | #endif // QUICHE_QUIC_CORE_QUIC_CONNECTION_ID_H_ |