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