QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 QUICHE_QUIC_CORE_QUIC_UTILS_H_ |
| 6 | #define QUICHE_QUIC_CORE_QUIC_UTILS_H_ |
| 7 | |
| 8 | #include <cstddef> |
| 9 | #include <cstdint> |
wub | cfddec8 | 2020-01-13 07:45:27 -0800 | [diff] [blame] | 10 | #include <sstream> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 11 | #include <string> |
wub | cfddec8 | 2020-01-13 07:45:27 -0800 | [diff] [blame] | 12 | #include <type_traits> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/core/crypto/quic_random.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/frames/quic_frame.h" |
dschinazi | adc7507 | 2019-08-19 10:54:45 -0700 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/quic/core/quic_error_codes.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 19 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
| 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
| 21 | #include "net/third_party/quiche/src/quic/platform/api/quic_iovec.h" |
| 22 | #include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | #include "net/third_party/quiche/src/quic/platform/api/quic_uint128.h" |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 24 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 25 | |
| 26 | namespace quic { |
| 27 | |
| 28 | class QUIC_EXPORT_PRIVATE QuicUtils { |
| 29 | public: |
| 30 | QuicUtils() = delete; |
| 31 | |
| 32 | // Returns the 64 bit FNV1a hash of the data. See |
| 33 | // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 34 | static uint64_t FNV1a_64_Hash(quiche::QuicheStringPiece data); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 35 | |
| 36 | // Returns the 128 bit FNV1a hash of the data. See |
| 37 | // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 38 | static QuicUint128 FNV1a_128_Hash(quiche::QuicheStringPiece data); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 39 | |
| 40 | // Returns the 128 bit FNV1a hash of the two sequences of data. See |
| 41 | // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 42 | static QuicUint128 FNV1a_128_Hash_Two(quiche::QuicheStringPiece data1, |
| 43 | quiche::QuicheStringPiece data2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 44 | |
| 45 | // Returns the 128 bit FNV1a hash of the three sequences of data. See |
| 46 | // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 47 | static QuicUint128 FNV1a_128_Hash_Three(quiche::QuicheStringPiece data1, |
| 48 | quiche::QuicheStringPiece data2, |
| 49 | quiche::QuicheStringPiece data3); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 50 | |
| 51 | // SerializeUint128 writes the first 96 bits of |v| in little-endian form |
| 52 | // to |out|. |
| 53 | static void SerializeUint128Short(QuicUint128 v, uint8_t* out); |
| 54 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 55 | // Returns AddressChangeType as a string. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 56 | static std::string AddressChangeTypeToString(AddressChangeType type); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 57 | |
| 58 | // Returns SentPacketState as a char*. |
| 59 | static const char* SentPacketStateToString(SentPacketState state); |
| 60 | |
| 61 | // Returns QuicLongHeaderType as a char*. |
| 62 | static const char* QuicLongHeaderTypetoString(QuicLongHeaderType type); |
| 63 | |
fayang | 3eb8221 | 2019-04-16 12:05:46 -0700 | [diff] [blame] | 64 | // Returns AckResult as a char*. |
| 65 | static const char* AckResultToString(AckResult result); |
| 66 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 67 | // Determines and returns change type of address change from |old_address| to |
| 68 | // |new_address|. |
| 69 | static AddressChangeType DetermineAddressChangeType( |
| 70 | const QuicSocketAddress& old_address, |
| 71 | const QuicSocketAddress& new_address); |
| 72 | |
| 73 | // Copies |buffer_length| bytes from iov starting at offset |iov_offset| into |
| 74 | // buffer. |iov| must be at least iov_offset+length total length and buffer |
| 75 | // must be at least |length| long. |
| 76 | static void CopyToBuffer(const struct iovec* iov, |
| 77 | int iov_count, |
| 78 | size_t iov_offset, |
| 79 | size_t buffer_length, |
| 80 | char* buffer); |
| 81 | |
| 82 | // Creates an iovec pointing to the same data as |data|. |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 83 | static struct iovec MakeIovec(quiche::QuicheStringPiece data); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 84 | |
nharper | 4eba09b | 2019-06-26 20:17:25 -0700 | [diff] [blame] | 85 | // Returns the opposite Perspective of the |perspective| passed in. |
| 86 | static constexpr Perspective InvertPerspective(Perspective perspective) { |
| 87 | return perspective == Perspective::IS_CLIENT ? Perspective::IS_SERVER |
| 88 | : Perspective::IS_CLIENT; |
| 89 | } |
| 90 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 91 | // Returns true if a packet is ackable. A packet is unackable if it can never |
| 92 | // be acked. Occurs when a packet is never sent, after it is acknowledged |
| 93 | // once, or if it's a crypto packet we never expect to receive an ack for. |
| 94 | static bool IsAckable(SentPacketState state); |
| 95 | |
| 96 | // Returns true if frame with |type| is retransmittable. A retransmittable |
| 97 | // frame should be retransmitted if it is detected as lost. |
| 98 | static bool IsRetransmittableFrame(QuicFrameType type); |
| 99 | |
| 100 | // Returns true if |frame| is a handshake frame in version |version|. |
| 101 | static bool IsHandshakeFrame(const QuicFrame& frame, |
| 102 | QuicTransportVersion transport_version); |
| 103 | |
wub | f6ed10e | 2020-01-31 13:22:02 -0800 | [diff] [blame] | 104 | // Return true if any frame in |frames| is of |type|. |
| 105 | static bool ContainsFrameType(const QuicFrames& frames, QuicFrameType type); |
| 106 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 107 | // Returns packet state corresponding to |retransmission_type|. |
| 108 | static SentPacketState RetransmissionTypeToPacketState( |
| 109 | TransmissionType retransmission_type); |
| 110 | |
| 111 | // Returns true if header with |first_byte| is considered as an IETF QUIC |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 112 | // packet header. This only works on the server. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 113 | static bool IsIetfPacketHeader(uint8_t first_byte); |
| 114 | |
| 115 | // Returns true if header with |first_byte| is considered as an IETF QUIC |
| 116 | // short packet header. |
| 117 | static bool IsIetfPacketShortHeader(uint8_t first_byte); |
| 118 | |
| 119 | // Returns ID to denote an invalid stream of |version|. |
| 120 | static QuicStreamId GetInvalidStreamId(QuicTransportVersion version); |
| 121 | |
| 122 | // Returns crypto stream ID of |version|. |
| 123 | static QuicStreamId GetCryptoStreamId(QuicTransportVersion version); |
| 124 | |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 125 | // Returns whether |id| is the stream ID for the crypto stream. If |version| |
| 126 | // is a version where crypto data doesn't go over stream frames, this function |
| 127 | // will always return false. |
| 128 | static bool IsCryptoStreamId(QuicTransportVersion version, QuicStreamId id); |
| 129 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 130 | // Returns headers stream ID of |version|. |
| 131 | static QuicStreamId GetHeadersStreamId(QuicTransportVersion version); |
| 132 | |
| 133 | // Returns true if |id| is considered as client initiated stream ID. |
| 134 | static bool IsClientInitiatedStreamId(QuicTransportVersion version, |
| 135 | QuicStreamId id); |
| 136 | |
| 137 | // Returns true if |id| is considered as server initiated stream ID. |
| 138 | static bool IsServerInitiatedStreamId(QuicTransportVersion version, |
| 139 | QuicStreamId id); |
| 140 | |
dschinazi | 18cdf13 | 2019-10-09 16:08:18 -0700 | [diff] [blame] | 141 | // Returns true if the stream ID represents a stream initiated by the |
| 142 | // provided perspective. |
| 143 | static bool IsOutgoingStreamId(ParsedQuicVersion version, |
| 144 | QuicStreamId id, |
| 145 | Perspective perspective); |
| 146 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 147 | // Returns true if |id| is considered as bidirectional stream ID. Only used in |
| 148 | // v99. |
| 149 | static bool IsBidirectionalStreamId(QuicStreamId id); |
| 150 | |
| 151 | // Returns stream type. Either |perspective| or |peer_initiated| would be |
| 152 | // enough together with |id|. This method enforces that the three parameters |
| 153 | // are consistent. Only used in v99. |
| 154 | static StreamType GetStreamType(QuicStreamId id, |
| 155 | Perspective perspective, |
| 156 | bool peer_initiated); |
| 157 | |
| 158 | // Returns the delta between consecutive stream IDs of the same type. |
| 159 | static QuicStreamId StreamIdDelta(QuicTransportVersion version); |
| 160 | |
| 161 | // Returns the first initiated bidirectional stream ID of |perspective|. |
| 162 | static QuicStreamId GetFirstBidirectionalStreamId( |
| 163 | QuicTransportVersion version, |
| 164 | Perspective perspective); |
| 165 | |
| 166 | // Returns the first initiated unidirectional stream ID of |perspective|. |
| 167 | static QuicStreamId GetFirstUnidirectionalStreamId( |
| 168 | QuicTransportVersion version, |
| 169 | Perspective perspective); |
| 170 | |
dschinazi | adc7507 | 2019-08-19 10:54:45 -0700 | [diff] [blame] | 171 | // Generates a 64bit connection ID derived from the input connection ID. |
| 172 | // This is guaranteed to be deterministic (calling this method with two |
| 173 | // connection IDs that are equal is guaranteed to produce the same result). |
| 174 | static QuicConnectionId CreateReplacementConnectionId( |
| 175 | QuicConnectionId connection_id); |
| 176 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 177 | // Generates a random 64bit connection ID. |
| 178 | static QuicConnectionId CreateRandomConnectionId(); |
| 179 | |
| 180 | // Generates a random 64bit connection ID using the provided QuicRandom. |
| 181 | static QuicConnectionId CreateRandomConnectionId(QuicRandom* random); |
| 182 | |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 183 | // Generates a random connection ID of the given length. |
| 184 | static QuicConnectionId CreateRandomConnectionId( |
| 185 | uint8_t connection_id_length); |
| 186 | |
| 187 | // Generates a random connection ID of the given length using the provided |
| 188 | // QuicRandom. |
| 189 | static QuicConnectionId CreateRandomConnectionId(uint8_t connection_id_length, |
| 190 | QuicRandom* random); |
| 191 | |
dschinazi | 6c84c14 | 2019-07-31 09:11:49 -0700 | [diff] [blame] | 192 | // Returns true if the connection ID length is valid for this QUIC version. |
| 193 | static bool IsConnectionIdLengthValidForVersion( |
| 194 | size_t connection_id_length, |
| 195 | QuicTransportVersion transport_version); |
| 196 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 197 | // Returns true if the connection ID is valid for this QUIC version. |
dschinazi | 6c84c14 | 2019-07-31 09:11:49 -0700 | [diff] [blame] | 198 | static bool IsConnectionIdValidForVersion( |
| 199 | QuicConnectionId connection_id, |
| 200 | QuicTransportVersion transport_version); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 201 | |
| 202 | // Returns a connection ID suitable for QUIC use-cases that do not need the |
| 203 | // connection ID for multiplexing. If the version allows variable lengths, |
| 204 | // a connection of length zero is returned, otherwise 64bits set to zero. |
| 205 | static QuicConnectionId CreateZeroConnectionId(QuicTransportVersion version); |
| 206 | |
| 207 | // Generates a 128bit stateless reset token based on a connection ID. |
| 208 | static QuicUint128 GenerateStatelessResetToken( |
| 209 | QuicConnectionId connection_id); |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 210 | |
| 211 | // Determines packet number space from |encryption_level|. |
| 212 | static PacketNumberSpace GetPacketNumberSpace( |
| 213 | EncryptionLevel encryption_level); |
QUICHE team | 1dfa46b | 2019-03-22 10:39:10 -0700 | [diff] [blame] | 214 | |
| 215 | // Determines encryption level to send packets in |packet_number_space|. |
| 216 | static EncryptionLevel GetEncryptionLevel( |
| 217 | PacketNumberSpace packet_number_space); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 218 | |
| 219 | // Get the maximum value for a V99/IETF QUIC stream count. If a count |
| 220 | // exceeds this value, it will result in a stream ID that exceeds the |
| 221 | // implementation limit on stream ID size. |
| 222 | static QuicStreamCount GetMaxStreamCount(bool unidirectional, |
| 223 | Perspective perspective); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 224 | }; |
| 225 | |
wub | cfddec8 | 2020-01-13 07:45:27 -0800 | [diff] [blame] | 226 | template <typename Mask> |
| 227 | class QUIC_EXPORT_PRIVATE BitMask { |
| 228 | public: |
| 229 | // explicit to prevent (incorrect) usage like "BitMask bitmask = 0;". |
| 230 | template <typename... Bits> |
| 231 | explicit BitMask(Bits... bits) { |
| 232 | mask_ = MakeMask(bits...); |
| 233 | } |
| 234 | |
| 235 | BitMask() = default; |
| 236 | BitMask(const BitMask& other) = default; |
| 237 | BitMask& operator=(const BitMask& other) = default; |
| 238 | |
| 239 | template <typename... Bits> |
| 240 | void Set(Bits... bits) { |
| 241 | mask_ |= MakeMask(bits...); |
| 242 | } |
| 243 | |
| 244 | template <typename Bit> |
| 245 | bool IsSet(Bit bit) const { |
| 246 | return (MakeMask(bit) & mask_) != 0; |
| 247 | } |
| 248 | |
wub | 1dc01cf | 2020-01-27 13:04:14 -0800 | [diff] [blame] | 249 | void ClearAll() { mask_ = 0; } |
| 250 | |
wub | cfddec8 | 2020-01-13 07:45:27 -0800 | [diff] [blame] | 251 | static constexpr size_t NumBits() { return 8 * sizeof(Mask); } |
| 252 | |
| 253 | friend bool operator==(const BitMask& lhs, const BitMask& rhs) { |
| 254 | return lhs.mask_ == rhs.mask_; |
| 255 | } |
| 256 | |
| 257 | std::string DebugString() const { |
| 258 | std::ostringstream oss; |
| 259 | oss << "0x" << std::hex << mask_; |
| 260 | return oss.str(); |
| 261 | } |
| 262 | |
| 263 | private: |
| 264 | template <typename Bit> |
| 265 | static std::enable_if_t<std::is_enum<Bit>::value, Mask> MakeMask(Bit bit) { |
| 266 | using IntType = typename std::underlying_type<Bit>::type; |
| 267 | return Mask(1) << static_cast<IntType>(bit); |
| 268 | } |
| 269 | |
| 270 | template <typename Bit> |
| 271 | static std::enable_if_t<!std::is_enum<Bit>::value, Mask> MakeMask(Bit bit) { |
| 272 | return Mask(1) << bit; |
| 273 | } |
| 274 | |
| 275 | template <typename Bit, typename... Bits> |
| 276 | static Mask MakeMask(Bit first_bit, Bits... other_bits) { |
| 277 | return MakeMask(first_bit) | MakeMask(other_bits...); |
| 278 | } |
| 279 | |
| 280 | Mask mask_ = 0; |
| 281 | }; |
| 282 | |
| 283 | using BitMask64 = BitMask<uint64_t>; |
| 284 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 285 | } // namespace quic |
| 286 | |
| 287 | #endif // QUICHE_QUIC_CORE_QUIC_UTILS_H_ |