blob: 294f52fa846fe5b452a89859d65483383445a26e [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"
14#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
15#include "net/third_party/quiche/src/quic/core/quic_types.h"
16#include "net/third_party/quiche/src/quic/core/quic_versions.h"
17#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
18#include "net/third_party/quiche/src/quic/platform/api/quic_iovec.h"
19#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050020#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
21#include "net/third_party/quiche/src/quic/platform/api/quic_uint128.h"
22
23namespace quic {
24
25class QUIC_EXPORT_PRIVATE QuicUtils {
26 public:
27 QuicUtils() = delete;
28
29 // Returns the 64 bit FNV1a hash of the data. See
30 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
31 static uint64_t FNV1a_64_Hash(QuicStringPiece data);
32
33 // Returns the 128 bit FNV1a hash of the data. See
34 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
35 static QuicUint128 FNV1a_128_Hash(QuicStringPiece data);
36
37 // Returns the 128 bit FNV1a hash of the two sequences of data. See
38 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
39 static QuicUint128 FNV1a_128_Hash_Two(QuicStringPiece data1,
40 QuicStringPiece data2);
41
42 // Returns the 128 bit FNV1a hash of the three sequences of data. See
43 // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
44 static QuicUint128 FNV1a_128_Hash_Three(QuicStringPiece data1,
45 QuicStringPiece data2,
46 QuicStringPiece data3);
47
48 // SerializeUint128 writes the first 96 bits of |v| in little-endian form
49 // to |out|.
50 static void SerializeUint128Short(QuicUint128 v, uint8_t* out);
51
52 // Returns the level of encryption as a char*
53 static const char* EncryptionLevelToString(EncryptionLevel level);
54
55 // Returns TransmissionType as a char*
56 static const char* TransmissionTypeToString(TransmissionType type);
57
58 // Returns AddressChangeType as a string.
vasilvvc48c8712019-03-11 13:38:16 -070059 static std::string AddressChangeTypeToString(AddressChangeType type);
QUICHE teama6ef0a62019-03-07 20:34:33 -050060
61 // Returns SentPacketState as a char*.
62 static const char* SentPacketStateToString(SentPacketState state);
63
64 // Returns QuicLongHeaderType as a char*.
65 static const char* QuicLongHeaderTypetoString(QuicLongHeaderType type);
66
fayang3eb82212019-04-16 12:05:46 -070067 // Returns AckResult as a char*.
68 static const char* AckResultToString(AckResult result);
69
QUICHE teama6ef0a62019-03-07 20:34:33 -050070 // Determines and returns change type of address change from |old_address| to
71 // |new_address|.
72 static AddressChangeType DetermineAddressChangeType(
73 const QuicSocketAddress& old_address,
74 const QuicSocketAddress& new_address);
75
76 // Copies |buffer_length| bytes from iov starting at offset |iov_offset| into
77 // buffer. |iov| must be at least iov_offset+length total length and buffer
78 // must be at least |length| long.
79 static void CopyToBuffer(const struct iovec* iov,
80 int iov_count,
81 size_t iov_offset,
82 size_t buffer_length,
83 char* buffer);
84
85 // Creates an iovec pointing to the same data as |data|.
86 static struct iovec MakeIovec(QuicStringPiece data);
87
nharper4eba09b2019-06-26 20:17:25 -070088 // Returns the opposite Perspective of the |perspective| passed in.
89 static constexpr Perspective InvertPerspective(Perspective perspective) {
90 return perspective == Perspective::IS_CLIENT ? Perspective::IS_SERVER
91 : Perspective::IS_CLIENT;
92 }
93
QUICHE teama6ef0a62019-03-07 20:34:33 -050094 // Returns true if a packet is ackable. A packet is unackable if it can never
95 // be acked. Occurs when a packet is never sent, after it is acknowledged
96 // once, or if it's a crypto packet we never expect to receive an ack for.
97 static bool IsAckable(SentPacketState state);
98
99 // Returns true if frame with |type| is retransmittable. A retransmittable
100 // frame should be retransmitted if it is detected as lost.
101 static bool IsRetransmittableFrame(QuicFrameType type);
102
103 // Returns true if |frame| is a handshake frame in version |version|.
104 static bool IsHandshakeFrame(const QuicFrame& frame,
105 QuicTransportVersion transport_version);
106
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
112 // packet header.
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
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
141 // Returns true if |id| is considered as bidirectional stream ID. Only used in
142 // v99.
143 static bool IsBidirectionalStreamId(QuicStreamId id);
144
145 // Returns stream type. Either |perspective| or |peer_initiated| would be
146 // enough together with |id|. This method enforces that the three parameters
147 // are consistent. Only used in v99.
148 static StreamType GetStreamType(QuicStreamId id,
149 Perspective perspective,
150 bool peer_initiated);
151
152 // Returns the delta between consecutive stream IDs of the same type.
153 static QuicStreamId StreamIdDelta(QuicTransportVersion version);
154
155 // Returns the first initiated bidirectional stream ID of |perspective|.
156 static QuicStreamId GetFirstBidirectionalStreamId(
157 QuicTransportVersion version,
158 Perspective perspective);
159
160 // Returns the first initiated unidirectional stream ID of |perspective|.
161 static QuicStreamId GetFirstUnidirectionalStreamId(
162 QuicTransportVersion version,
163 Perspective perspective);
164
165 // Generates a random 64bit connection ID.
166 static QuicConnectionId CreateRandomConnectionId();
167
168 // Generates a random 64bit connection ID using the provided QuicRandom.
169 static QuicConnectionId CreateRandomConnectionId(QuicRandom* random);
170
QUICHE teamc65d1d12019-03-19 20:58:04 -0700171 // Generates a random connection ID of the given length.
172 static QuicConnectionId CreateRandomConnectionId(
173 uint8_t connection_id_length);
174
175 // Generates a random connection ID of the given length using the provided
176 // QuicRandom.
177 static QuicConnectionId CreateRandomConnectionId(uint8_t connection_id_length,
178 QuicRandom* random);
179
QUICHE teama6ef0a62019-03-07 20:34:33 -0500180 // Returns true if the QUIC version allows variable length connection IDs.
181 static bool VariableLengthConnectionIdAllowedForVersion(
182 QuicTransportVersion version);
183
184 // Returns true if the connection ID is valid for this QUIC version.
185 static bool IsConnectionIdValidForVersion(QuicConnectionId connection_id,
186 QuicTransportVersion version);
187
188 // Returns a connection ID suitable for QUIC use-cases that do not need the
189 // connection ID for multiplexing. If the version allows variable lengths,
190 // a connection of length zero is returned, otherwise 64bits set to zero.
191 static QuicConnectionId CreateZeroConnectionId(QuicTransportVersion version);
192
193 // Generates a 128bit stateless reset token based on a connection ID.
194 static QuicUint128 GenerateStatelessResetToken(
195 QuicConnectionId connection_id);
QUICHE team10b22a12019-03-21 15:31:42 -0700196
197 // Determines packet number space from |encryption_level|.
198 static PacketNumberSpace GetPacketNumberSpace(
199 EncryptionLevel encryption_level);
QUICHE team1dfa46b2019-03-22 10:39:10 -0700200
201 // Determines encryption level to send packets in |packet_number_space|.
202 static EncryptionLevel GetEncryptionLevel(
203 PacketNumberSpace packet_number_space);
fkastenholz3c4eabf2019-04-22 07:49:59 -0700204
205 // Get the maximum value for a V99/IETF QUIC stream count. If a count
206 // exceeds this value, it will result in a stream ID that exceeds the
207 // implementation limit on stream ID size.
208 static QuicStreamCount GetMaxStreamCount(bool unidirectional,
209 Perspective perspective);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500210};
211
212} // namespace quic
213
214#endif // QUICHE_QUIC_CORE_QUIC_UTILS_H_