blob: c143154f1a04e773af02c48997175d063cf6ccb0 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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>
wubcfddec82020-01-13 07:45:27 -080010#include <sstream>
vasilvv872e7a32019-03-12 16:42:44 -070011#include <string>
wubcfddec82020-01-13 07:45:27 -080012#include <type_traits>
QUICHE teama6ef0a62019-03-07 20:34:33 -050013
QUICHE teama6ef0a62019-03-07 20:34:33 -050014#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"
dschinaziadc75072019-08-19 10:54:45 -070016#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050017#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 teama6ef0a62019-03-07 20:34:33 -050023#include "net/third_party/quiche/src/quic/platform/api/quic_uint128.h"
dmcardlecf0bfcf2019-12-13 08:08:21 -080024#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050025
26namespace quic {
27
28class 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
dmcardlecf0bfcf2019-12-13 08:08:21 -080034 static uint64_t FNV1a_64_Hash(quiche::QuicheStringPiece data);
QUICHE teama6ef0a62019-03-07 20:34:33 -050035
36 // Returns the 128 bit FNV1a hash of the data. See
37 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
dmcardlecf0bfcf2019-12-13 08:08:21 -080038 static QuicUint128 FNV1a_128_Hash(quiche::QuicheStringPiece data);
QUICHE teama6ef0a62019-03-07 20:34:33 -050039
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
dmcardlecf0bfcf2019-12-13 08:08:21 -080042 static QuicUint128 FNV1a_128_Hash_Two(quiche::QuicheStringPiece data1,
43 quiche::QuicheStringPiece data2);
QUICHE teama6ef0a62019-03-07 20:34:33 -050044
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
dmcardlecf0bfcf2019-12-13 08:08:21 -080047 static QuicUint128 FNV1a_128_Hash_Three(quiche::QuicheStringPiece data1,
48 quiche::QuicheStringPiece data2,
49 quiche::QuicheStringPiece data3);
QUICHE teama6ef0a62019-03-07 20:34:33 -050050
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 teama6ef0a62019-03-07 20:34:33 -050055 // Returns AddressChangeType as a string.
vasilvvc48c8712019-03-11 13:38:16 -070056 static std::string AddressChangeTypeToString(AddressChangeType type);
QUICHE teama6ef0a62019-03-07 20:34:33 -050057
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
fayang3eb82212019-04-16 12:05:46 -070064 // Returns AckResult as a char*.
65 static const char* AckResultToString(AckResult result);
66
QUICHE teama6ef0a62019-03-07 20:34:33 -050067 // 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|.
dmcardlecf0bfcf2019-12-13 08:08:21 -080083 static struct iovec MakeIovec(quiche::QuicheStringPiece data);
QUICHE teama6ef0a62019-03-07 20:34:33 -050084
nharper4eba09b2019-06-26 20:17:25 -070085 // 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 teama6ef0a62019-03-07 20:34:33 -050091 // 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
wubf6ed10e2020-01-31 13:22:02 -0800104 // Return true if any frame in |frames| is of |type|.
105 static bool ContainsFrameType(const QuicFrames& frames, QuicFrameType type);
106
QUICHE teama6ef0a62019-03-07 20:34:33 -0500107 // 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
dschinazi48ac9192019-07-31 00:07:26 -0700112 // packet header. This only works on the server.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500113 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
nharper46833c32019-05-15 21:33:05 -0700125 // 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 teama6ef0a62019-03-07 20:34:33 -0500130 // 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
dschinazi18cdf132019-10-09 16:08:18 -0700141 // 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 teama6ef0a62019-03-07 20:34:33 -0500147 // 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
dschinaziadc75072019-08-19 10:54:45 -0700171 // 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 teama6ef0a62019-03-07 20:34:33 -0500177 // 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 teamc65d1d12019-03-19 20:58:04 -0700183 // 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
dschinazi6c84c142019-07-31 09:11:49 -0700192 // 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 teama6ef0a62019-03-07 20:34:33 -0500197 // Returns true if the connection ID is valid for this QUIC version.
dschinazi6c84c142019-07-31 09:11:49 -0700198 static bool IsConnectionIdValidForVersion(
199 QuicConnectionId connection_id,
200 QuicTransportVersion transport_version);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500201
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 team10b22a12019-03-21 15:31:42 -0700210
211 // Determines packet number space from |encryption_level|.
212 static PacketNumberSpace GetPacketNumberSpace(
213 EncryptionLevel encryption_level);
QUICHE team1dfa46b2019-03-22 10:39:10 -0700214
215 // Determines encryption level to send packets in |packet_number_space|.
216 static EncryptionLevel GetEncryptionLevel(
217 PacketNumberSpace packet_number_space);
fkastenholz3c4eabf2019-04-22 07:49:59 -0700218
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 teama6ef0a62019-03-07 20:34:33 -0500224};
225
wubcfddec82020-01-13 07:45:27 -0800226template <typename Mask>
227class 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
wub1dc01cf2020-01-27 13:04:14 -0800249 void ClearAll() { mask_ = 0; }
250
wubcfddec82020-01-13 07:45:27 -0800251 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
283using BitMask64 = BitMask<uint64_t>;
284
QUICHE teama6ef0a62019-03-07 20:34:33 -0500285} // namespace quic
286
287#endif // QUICHE_QUIC_CORE_QUIC_UTILS_H_