blob: de855a0cad8fbcc8d01d203c2dbced1664eda80d [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>
vasilvv872e7a32019-03-12 16:42:44 -070010#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050011
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/crypto/quic_random.h"
13#include "net/third_party/quiche/src/quic/core/frames/quic_frame.h"
dschinaziadc75072019-08-19 10:54:45 -070014#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050015#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
16#include "net/third_party/quiche/src/quic/core/quic_types.h"
17#include "net/third_party/quiche/src/quic/core/quic_versions.h"
18#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
19#include "net/third_party/quiche/src/quic/platform/api/quic_iovec.h"
20#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050021#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
22#include "net/third_party/quiche/src/quic/platform/api/quic_uint128.h"
23
24namespace quic {
25
26class QUIC_EXPORT_PRIVATE QuicUtils {
27 public:
28 QuicUtils() = delete;
29
30 // Returns the 64 bit FNV1a hash of the data. See
31 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
32 static uint64_t FNV1a_64_Hash(QuicStringPiece data);
33
34 // Returns the 128 bit FNV1a hash of the data. See
35 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
36 static QuicUint128 FNV1a_128_Hash(QuicStringPiece data);
37
38 // Returns the 128 bit FNV1a hash of the two sequences of data. See
39 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
40 static QuicUint128 FNV1a_128_Hash_Two(QuicStringPiece data1,
41 QuicStringPiece data2);
42
43 // Returns the 128 bit FNV1a hash of the three sequences of data. See
44 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
45 static QuicUint128 FNV1a_128_Hash_Three(QuicStringPiece data1,
46 QuicStringPiece data2,
47 QuicStringPiece data3);
48
49 // SerializeUint128 writes the first 96 bits of |v| in little-endian form
50 // to |out|.
51 static void SerializeUint128Short(QuicUint128 v, uint8_t* out);
52
53 // Returns the level of encryption as a char*
54 static const char* EncryptionLevelToString(EncryptionLevel level);
55
56 // Returns TransmissionType as a char*
57 static const char* TransmissionTypeToString(TransmissionType type);
58
59 // Returns AddressChangeType as a string.
vasilvvc48c8712019-03-11 13:38:16 -070060 static std::string AddressChangeTypeToString(AddressChangeType type);
QUICHE teama6ef0a62019-03-07 20:34:33 -050061
62 // Returns SentPacketState as a char*.
63 static const char* SentPacketStateToString(SentPacketState state);
64
65 // Returns QuicLongHeaderType as a char*.
66 static const char* QuicLongHeaderTypetoString(QuicLongHeaderType type);
67
fayang3eb82212019-04-16 12:05:46 -070068 // Returns AckResult as a char*.
69 static const char* AckResultToString(AckResult result);
70
QUICHE teama6ef0a62019-03-07 20:34:33 -050071 // Determines and returns change type of address change from |old_address| to
72 // |new_address|.
73 static AddressChangeType DetermineAddressChangeType(
74 const QuicSocketAddress& old_address,
75 const QuicSocketAddress& new_address);
76
77 // Copies |buffer_length| bytes from iov starting at offset |iov_offset| into
78 // buffer. |iov| must be at least iov_offset+length total length and buffer
79 // must be at least |length| long.
80 static void CopyToBuffer(const struct iovec* iov,
81 int iov_count,
82 size_t iov_offset,
83 size_t buffer_length,
84 char* buffer);
85
86 // Creates an iovec pointing to the same data as |data|.
87 static struct iovec MakeIovec(QuicStringPiece data);
88
nharper4eba09b2019-06-26 20:17:25 -070089 // Returns the opposite Perspective of the |perspective| passed in.
90 static constexpr Perspective InvertPerspective(Perspective perspective) {
91 return perspective == Perspective::IS_CLIENT ? Perspective::IS_SERVER
92 : Perspective::IS_CLIENT;
93 }
94
QUICHE teama6ef0a62019-03-07 20:34:33 -050095 // Returns true if a packet is ackable. A packet is unackable if it can never
96 // be acked. Occurs when a packet is never sent, after it is acknowledged
97 // once, or if it's a crypto packet we never expect to receive an ack for.
98 static bool IsAckable(SentPacketState state);
99
100 // Returns true if frame with |type| is retransmittable. A retransmittable
101 // frame should be retransmitted if it is detected as lost.
102 static bool IsRetransmittableFrame(QuicFrameType type);
103
104 // Returns true if |frame| is a handshake frame in version |version|.
105 static bool IsHandshakeFrame(const QuicFrame& frame,
106 QuicTransportVersion transport_version);
107
108 // Returns packet state corresponding to |retransmission_type|.
109 static SentPacketState RetransmissionTypeToPacketState(
110 TransmissionType retransmission_type);
111
112 // Returns true if header with |first_byte| is considered as an IETF QUIC
dschinazi48ac9192019-07-31 00:07:26 -0700113 // packet header. This only works on the server.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500114 static bool IsIetfPacketHeader(uint8_t first_byte);
115
116 // Returns true if header with |first_byte| is considered as an IETF QUIC
117 // short packet header.
118 static bool IsIetfPacketShortHeader(uint8_t first_byte);
119
120 // Returns ID to denote an invalid stream of |version|.
121 static QuicStreamId GetInvalidStreamId(QuicTransportVersion version);
122
123 // Returns crypto stream ID of |version|.
124 static QuicStreamId GetCryptoStreamId(QuicTransportVersion version);
125
nharper46833c32019-05-15 21:33:05 -0700126 // Returns whether |id| is the stream ID for the crypto stream. If |version|
127 // is a version where crypto data doesn't go over stream frames, this function
128 // will always return false.
129 static bool IsCryptoStreamId(QuicTransportVersion version, QuicStreamId id);
130
QUICHE teama6ef0a62019-03-07 20:34:33 -0500131 // Returns headers stream ID of |version|.
132 static QuicStreamId GetHeadersStreamId(QuicTransportVersion version);
133
134 // Returns true if |id| is considered as client initiated stream ID.
135 static bool IsClientInitiatedStreamId(QuicTransportVersion version,
136 QuicStreamId id);
137
138 // Returns true if |id| is considered as server initiated stream ID.
139 static bool IsServerInitiatedStreamId(QuicTransportVersion version,
140 QuicStreamId id);
141
142 // Returns true if |id| is considered as bidirectional stream ID. Only used in
143 // v99.
144 static bool IsBidirectionalStreamId(QuicStreamId id);
145
146 // Returns stream type. Either |perspective| or |peer_initiated| would be
147 // enough together with |id|. This method enforces that the three parameters
148 // are consistent. Only used in v99.
149 static StreamType GetStreamType(QuicStreamId id,
150 Perspective perspective,
151 bool peer_initiated);
152
153 // Returns the delta between consecutive stream IDs of the same type.
154 static QuicStreamId StreamIdDelta(QuicTransportVersion version);
155
156 // Returns the first initiated bidirectional stream ID of |perspective|.
157 static QuicStreamId GetFirstBidirectionalStreamId(
158 QuicTransportVersion version,
159 Perspective perspective);
160
161 // Returns the first initiated unidirectional stream ID of |perspective|.
162 static QuicStreamId GetFirstUnidirectionalStreamId(
163 QuicTransportVersion version,
164 Perspective perspective);
165
dschinaziadc75072019-08-19 10:54:45 -0700166 // Generates a 64bit connection ID derived from the input connection ID.
167 // This is guaranteed to be deterministic (calling this method with two
168 // connection IDs that are equal is guaranteed to produce the same result).
169 static QuicConnectionId CreateReplacementConnectionId(
170 QuicConnectionId connection_id);
171
QUICHE teama6ef0a62019-03-07 20:34:33 -0500172 // Generates a random 64bit connection ID.
173 static QuicConnectionId CreateRandomConnectionId();
174
175 // Generates a random 64bit connection ID using the provided QuicRandom.
176 static QuicConnectionId CreateRandomConnectionId(QuicRandom* random);
177
QUICHE teamc65d1d12019-03-19 20:58:04 -0700178 // Generates a random connection ID of the given length.
179 static QuicConnectionId CreateRandomConnectionId(
180 uint8_t connection_id_length);
181
182 // Generates a random connection ID of the given length using the provided
183 // QuicRandom.
184 static QuicConnectionId CreateRandomConnectionId(uint8_t connection_id_length,
185 QuicRandom* random);
186
QUICHE teama6ef0a62019-03-07 20:34:33 -0500187 // Returns true if the QUIC version allows variable length connection IDs.
188 static bool VariableLengthConnectionIdAllowedForVersion(
189 QuicTransportVersion version);
190
dschinazi6c84c142019-07-31 09:11:49 -0700191 // Returns true if the connection ID length is valid for this QUIC version.
192 static bool IsConnectionIdLengthValidForVersion(
193 size_t connection_id_length,
194 QuicTransportVersion transport_version);
195
QUICHE teama6ef0a62019-03-07 20:34:33 -0500196 // Returns true if the connection ID is valid for this QUIC version.
dschinazi6c84c142019-07-31 09:11:49 -0700197 static bool IsConnectionIdValidForVersion(
198 QuicConnectionId connection_id,
199 QuicTransportVersion transport_version);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500200
201 // Returns a connection ID suitable for QUIC use-cases that do not need the
202 // connection ID for multiplexing. If the version allows variable lengths,
203 // a connection of length zero is returned, otherwise 64bits set to zero.
204 static QuicConnectionId CreateZeroConnectionId(QuicTransportVersion version);
205
206 // Generates a 128bit stateless reset token based on a connection ID.
207 static QuicUint128 GenerateStatelessResetToken(
208 QuicConnectionId connection_id);
QUICHE team10b22a12019-03-21 15:31:42 -0700209
210 // Determines packet number space from |encryption_level|.
211 static PacketNumberSpace GetPacketNumberSpace(
212 EncryptionLevel encryption_level);
QUICHE team1dfa46b2019-03-22 10:39:10 -0700213
214 // Determines encryption level to send packets in |packet_number_space|.
215 static EncryptionLevel GetEncryptionLevel(
216 PacketNumberSpace packet_number_space);
fkastenholz3c4eabf2019-04-22 07:49:59 -0700217
218 // Get the maximum value for a V99/IETF QUIC stream count. If a count
219 // exceeds this value, it will result in a stream ID that exceeds the
220 // implementation limit on stream ID size.
221 static QuicStreamCount GetMaxStreamCount(bool unidirectional,
222 Perspective perspective);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500223};
224
225} // namespace quic
226
227#endif // QUICHE_QUIC_CORE_QUIC_UTILS_H_