blob: bac025e79a0cc56beb9560973239c5c53c4b6e9d [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
88 // Returns true if a packet is ackable. A packet is unackable if it can never
89 // be acked. Occurs when a packet is never sent, after it is acknowledged
90 // once, or if it's a crypto packet we never expect to receive an ack for.
91 static bool IsAckable(SentPacketState state);
92
93 // Returns true if frame with |type| is retransmittable. A retransmittable
94 // frame should be retransmitted if it is detected as lost.
95 static bool IsRetransmittableFrame(QuicFrameType type);
96
97 // Returns true if |frame| is a handshake frame in version |version|.
98 static bool IsHandshakeFrame(const QuicFrame& frame,
99 QuicTransportVersion transport_version);
100
101 // Returns packet state corresponding to |retransmission_type|.
102 static SentPacketState RetransmissionTypeToPacketState(
103 TransmissionType retransmission_type);
104
105 // Returns true if header with |first_byte| is considered as an IETF QUIC
106 // packet header.
107 static bool IsIetfPacketHeader(uint8_t first_byte);
108
109 // Returns true if header with |first_byte| is considered as an IETF QUIC
110 // short packet header.
111 static bool IsIetfPacketShortHeader(uint8_t first_byte);
112
113 // Returns ID to denote an invalid stream of |version|.
114 static QuicStreamId GetInvalidStreamId(QuicTransportVersion version);
115
116 // Returns crypto stream ID of |version|.
117 static QuicStreamId GetCryptoStreamId(QuicTransportVersion version);
118
nharper46833c32019-05-15 21:33:05 -0700119 // Returns whether |id| is the stream ID for the crypto stream. If |version|
120 // is a version where crypto data doesn't go over stream frames, this function
121 // will always return false.
122 static bool IsCryptoStreamId(QuicTransportVersion version, QuicStreamId id);
123
QUICHE teama6ef0a62019-03-07 20:34:33 -0500124 // Returns headers stream ID of |version|.
125 static QuicStreamId GetHeadersStreamId(QuicTransportVersion version);
126
127 // Returns true if |id| is considered as client initiated stream ID.
128 static bool IsClientInitiatedStreamId(QuicTransportVersion version,
129 QuicStreamId id);
130
131 // Returns true if |id| is considered as server initiated stream ID.
132 static bool IsServerInitiatedStreamId(QuicTransportVersion version,
133 QuicStreamId id);
134
135 // Returns true if |id| is considered as bidirectional stream ID. Only used in
136 // v99.
137 static bool IsBidirectionalStreamId(QuicStreamId id);
138
139 // Returns stream type. Either |perspective| or |peer_initiated| would be
140 // enough together with |id|. This method enforces that the three parameters
141 // are consistent. Only used in v99.
142 static StreamType GetStreamType(QuicStreamId id,
143 Perspective perspective,
144 bool peer_initiated);
145
146 // Returns the delta between consecutive stream IDs of the same type.
147 static QuicStreamId StreamIdDelta(QuicTransportVersion version);
148
149 // Returns the first initiated bidirectional stream ID of |perspective|.
150 static QuicStreamId GetFirstBidirectionalStreamId(
151 QuicTransportVersion version,
152 Perspective perspective);
153
154 // Returns the first initiated unidirectional stream ID of |perspective|.
155 static QuicStreamId GetFirstUnidirectionalStreamId(
156 QuicTransportVersion version,
157 Perspective perspective);
158
159 // Generates a random 64bit connection ID.
160 static QuicConnectionId CreateRandomConnectionId();
161
162 // Generates a random 64bit connection ID using the provided QuicRandom.
163 static QuicConnectionId CreateRandomConnectionId(QuicRandom* random);
164
QUICHE teamc65d1d12019-03-19 20:58:04 -0700165 // Generates a random connection ID of the given length.
166 static QuicConnectionId CreateRandomConnectionId(
167 uint8_t connection_id_length);
168
169 // Generates a random connection ID of the given length using the provided
170 // QuicRandom.
171 static QuicConnectionId CreateRandomConnectionId(uint8_t connection_id_length,
172 QuicRandom* random);
173
QUICHE teama6ef0a62019-03-07 20:34:33 -0500174 // Returns true if the QUIC version allows variable length connection IDs.
175 static bool VariableLengthConnectionIdAllowedForVersion(
176 QuicTransportVersion version);
177
178 // Returns true if the connection ID is valid for this QUIC version.
179 static bool IsConnectionIdValidForVersion(QuicConnectionId connection_id,
180 QuicTransportVersion version);
181
182 // Returns a connection ID suitable for QUIC use-cases that do not need the
183 // connection ID for multiplexing. If the version allows variable lengths,
184 // a connection of length zero is returned, otherwise 64bits set to zero.
185 static QuicConnectionId CreateZeroConnectionId(QuicTransportVersion version);
186
187 // Generates a 128bit stateless reset token based on a connection ID.
188 static QuicUint128 GenerateStatelessResetToken(
189 QuicConnectionId connection_id);
QUICHE team10b22a12019-03-21 15:31:42 -0700190
191 // Determines packet number space from |encryption_level|.
192 static PacketNumberSpace GetPacketNumberSpace(
193 EncryptionLevel encryption_level);
QUICHE team1dfa46b2019-03-22 10:39:10 -0700194
195 // Determines encryption level to send packets in |packet_number_space|.
196 static EncryptionLevel GetEncryptionLevel(
197 PacketNumberSpace packet_number_space);
fkastenholz3c4eabf2019-04-22 07:49:59 -0700198
199 // Get the maximum value for a V99/IETF QUIC stream count. If a count
200 // exceeds this value, it will result in a stream ID that exceeds the
201 // implementation limit on stream ID size.
202 static QuicStreamCount GetMaxStreamCount(bool unidirectional,
203 Perspective perspective);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500204};
205
206} // namespace quic
207
208#endif // QUICHE_QUIC_CORE_QUIC_UTILS_H_