blob: 955e82f8cd051d5da01e06511578eb8a467ff66c [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#include "net/third_party/quiche/src/quic/core/quic_utils.h"
6
7#include <algorithm>
8#include <cstdint>
vasilvv872e7a32019-03-12 16:42:44 -07009#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050010
QUICHE teamc65d1d12019-03-19 20:58:04 -070011#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/quic_constants.h"
13#include "net/third_party/quiche/src/quic/core/quic_types.h"
14#include "net/third_party/quiche/src/quic/platform/api/quic_aligned.h"
15#include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h"
16#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
17#include "net/third_party/quiche/src/quic/platform/api/quic_endian.h"
dschinazi8eb45e92019-05-10 11:36:15 -070018#include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050019#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
20#include "net/third_party/quiche/src/quic/platform/api/quic_prefetch.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050021#include "net/third_party/quiche/src/quic/platform/api/quic_uint128.h"
22
23namespace quic {
24namespace {
25
26// We know that >= GCC 4.8 and Clang have a __uint128_t intrinsic. Other
27// compilers don't necessarily, notably MSVC.
28#if defined(__x86_64__) && \
29 ((defined(__GNUC__) && \
30 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) || \
31 defined(__clang__))
32#define QUIC_UTIL_HAS_UINT128 1
33#endif
34
35#ifdef QUIC_UTIL_HAS_UINT128
36QuicUint128 IncrementalHashFast(QuicUint128 uhash, QuicStringPiece data) {
37 // This code ends up faster than the naive implementation for 2 reasons:
38 // 1. QuicUint128 is sufficiently complicated that the compiler
39 // cannot transform the multiplication by kPrime into a shift-multiply-add;
40 // it has go through all of the instructions for a 128-bit multiply.
41 // 2. Because there are so fewer instructions (around 13), the hot loop fits
42 // nicely in the instruction queue of many Intel CPUs.
43 // kPrime = 309485009821345068724781371
44 static const QuicUint128 kPrime =
45 (static_cast<QuicUint128>(16777216) << 64) + 315;
46 auto hi = QuicUint128High64(uhash);
47 auto lo = QuicUint128Low64(uhash);
48 QuicUint128 xhash = (static_cast<QuicUint128>(hi) << 64) + lo;
49 const uint8_t* octets = reinterpret_cast<const uint8_t*>(data.data());
50 for (size_t i = 0; i < data.length(); ++i) {
51 xhash = (xhash ^ static_cast<uint32_t>(octets[i])) * kPrime;
52 }
53 return MakeQuicUint128(QuicUint128High64(xhash), QuicUint128Low64(xhash));
54}
55#endif
56
57#ifndef QUIC_UTIL_HAS_UINT128
58// Slow implementation of IncrementalHash. In practice, only used by Chromium.
59QuicUint128 IncrementalHashSlow(QuicUint128 hash, QuicStringPiece data) {
60 // kPrime = 309485009821345068724781371
61 static const QuicUint128 kPrime = MakeQuicUint128(16777216, 315);
62 const uint8_t* octets = reinterpret_cast<const uint8_t*>(data.data());
63 for (size_t i = 0; i < data.length(); ++i) {
64 hash = hash ^ MakeQuicUint128(0, octets[i]);
65 hash = hash * kPrime;
66 }
67 return hash;
68}
69#endif
70
71QuicUint128 IncrementalHash(QuicUint128 hash, QuicStringPiece data) {
72#ifdef QUIC_UTIL_HAS_UINT128
73 return IncrementalHashFast(hash, data);
74#else
75 return IncrementalHashSlow(hash, data);
76#endif
77}
78
79} // namespace
80
81// static
82uint64_t QuicUtils::FNV1a_64_Hash(QuicStringPiece data) {
83 static const uint64_t kOffset = UINT64_C(14695981039346656037);
84 static const uint64_t kPrime = UINT64_C(1099511628211);
85
86 const uint8_t* octets = reinterpret_cast<const uint8_t*>(data.data());
87
88 uint64_t hash = kOffset;
89
90 for (size_t i = 0; i < data.length(); ++i) {
91 hash = hash ^ octets[i];
92 hash = hash * kPrime;
93 }
94
95 return hash;
96}
97
98// static
99QuicUint128 QuicUtils::FNV1a_128_Hash(QuicStringPiece data) {
100 return FNV1a_128_Hash_Three(data, QuicStringPiece(), QuicStringPiece());
101}
102
103// static
104QuicUint128 QuicUtils::FNV1a_128_Hash_Two(QuicStringPiece data1,
105 QuicStringPiece data2) {
106 return FNV1a_128_Hash_Three(data1, data2, QuicStringPiece());
107}
108
109// static
110QuicUint128 QuicUtils::FNV1a_128_Hash_Three(QuicStringPiece data1,
111 QuicStringPiece data2,
112 QuicStringPiece data3) {
113 // The two constants are defined as part of the hash algorithm.
114 // see http://www.isthe.com/chongo/tech/comp/fnv/
115 // kOffset = 144066263297769815596495629667062367629
116 const QuicUint128 kOffset = MakeQuicUint128(UINT64_C(7809847782465536322),
117 UINT64_C(7113472399480571277));
118
119 QuicUint128 hash = IncrementalHash(kOffset, data1);
120 if (data2.empty()) {
121 return hash;
122 }
123
124 hash = IncrementalHash(hash, data2);
125 if (data3.empty()) {
126 return hash;
127 }
128 return IncrementalHash(hash, data3);
129}
130
131// static
132void QuicUtils::SerializeUint128Short(QuicUint128 v, uint8_t* out) {
133 const uint64_t lo = QuicUint128Low64(v);
134 const uint64_t hi = QuicUint128High64(v);
135 // This assumes that the system is little-endian.
136 memcpy(out, &lo, sizeof(lo));
137 memcpy(out + sizeof(lo), &hi, sizeof(hi) / 2);
138}
139
140#define RETURN_STRING_LITERAL(x) \
141 case x: \
142 return #x;
143
144// static
145const char* QuicUtils::EncryptionLevelToString(EncryptionLevel level) {
146 switch (level) {
QUICHE team6987b4a2019-03-15 16:23:04 -0700147 RETURN_STRING_LITERAL(ENCRYPTION_INITIAL);
QUICHE team88ea0082019-03-15 10:05:26 -0700148 RETURN_STRING_LITERAL(ENCRYPTION_HANDSHAKE);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500149 RETURN_STRING_LITERAL(ENCRYPTION_ZERO_RTT);
150 RETURN_STRING_LITERAL(ENCRYPTION_FORWARD_SECURE);
151 RETURN_STRING_LITERAL(NUM_ENCRYPTION_LEVELS);
152 }
153 return "INVALID_ENCRYPTION_LEVEL";
154}
155
156// static
157const char* QuicUtils::TransmissionTypeToString(TransmissionType type) {
158 switch (type) {
159 RETURN_STRING_LITERAL(NOT_RETRANSMISSION);
160 RETURN_STRING_LITERAL(HANDSHAKE_RETRANSMISSION);
161 RETURN_STRING_LITERAL(LOSS_RETRANSMISSION);
162 RETURN_STRING_LITERAL(ALL_UNACKED_RETRANSMISSION);
163 RETURN_STRING_LITERAL(ALL_INITIAL_RETRANSMISSION);
164 RETURN_STRING_LITERAL(RTO_RETRANSMISSION);
165 RETURN_STRING_LITERAL(TLP_RETRANSMISSION);
166 RETURN_STRING_LITERAL(PROBING_RETRANSMISSION);
167 }
168 return "INVALID_TRANSMISSION_TYPE";
169}
170
vasilvvc48c8712019-03-11 13:38:16 -0700171std::string QuicUtils::AddressChangeTypeToString(AddressChangeType type) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500172 switch (type) {
173 RETURN_STRING_LITERAL(NO_CHANGE);
174 RETURN_STRING_LITERAL(PORT_CHANGE);
175 RETURN_STRING_LITERAL(IPV4_SUBNET_CHANGE);
176 RETURN_STRING_LITERAL(IPV4_TO_IPV6_CHANGE);
177 RETURN_STRING_LITERAL(IPV6_TO_IPV4_CHANGE);
178 RETURN_STRING_LITERAL(IPV6_TO_IPV6_CHANGE);
179 RETURN_STRING_LITERAL(IPV4_TO_IPV4_CHANGE);
180 }
181 return "INVALID_ADDRESS_CHANGE_TYPE";
182}
183
184const char* QuicUtils::SentPacketStateToString(SentPacketState state) {
185 switch (state) {
186 RETURN_STRING_LITERAL(OUTSTANDING);
187 RETURN_STRING_LITERAL(NEVER_SENT);
188 RETURN_STRING_LITERAL(ACKED);
189 RETURN_STRING_LITERAL(UNACKABLE);
190 RETURN_STRING_LITERAL(HANDSHAKE_RETRANSMITTED);
191 RETURN_STRING_LITERAL(LOST);
192 RETURN_STRING_LITERAL(TLP_RETRANSMITTED);
193 RETURN_STRING_LITERAL(RTO_RETRANSMITTED);
194 RETURN_STRING_LITERAL(PROBE_RETRANSMITTED);
195 }
196 return "INVALID_SENT_PACKET_STATE";
197}
198
199// static
200const char* QuicUtils::QuicLongHeaderTypetoString(QuicLongHeaderType type) {
201 switch (type) {
202 RETURN_STRING_LITERAL(VERSION_NEGOTIATION);
203 RETURN_STRING_LITERAL(INITIAL);
204 RETURN_STRING_LITERAL(RETRY);
205 RETURN_STRING_LITERAL(HANDSHAKE);
206 RETURN_STRING_LITERAL(ZERO_RTT_PROTECTED);
207 default:
208 return "INVALID_PACKET_TYPE";
209 }
210}
211
212// static
fayang3eb82212019-04-16 12:05:46 -0700213const char* QuicUtils::AckResultToString(AckResult result) {
214 switch (result) {
215 RETURN_STRING_LITERAL(PACKETS_NEWLY_ACKED);
216 RETURN_STRING_LITERAL(NO_PACKETS_NEWLY_ACKED);
217 RETURN_STRING_LITERAL(UNSENT_PACKETS_ACKED);
218 RETURN_STRING_LITERAL(UNACKABLE_PACKETS_ACKED);
219 RETURN_STRING_LITERAL(PACKETS_ACKED_IN_WRONG_PACKET_NUMBER_SPACE);
220 }
221 return "INVALID_ACK_RESULT";
222}
223
224// static
QUICHE teama6ef0a62019-03-07 20:34:33 -0500225AddressChangeType QuicUtils::DetermineAddressChangeType(
226 const QuicSocketAddress& old_address,
227 const QuicSocketAddress& new_address) {
228 if (!old_address.IsInitialized() || !new_address.IsInitialized() ||
229 old_address == new_address) {
230 return NO_CHANGE;
231 }
232
233 if (old_address.host() == new_address.host()) {
234 return PORT_CHANGE;
235 }
236
237 bool old_ip_is_ipv4 = old_address.host().IsIPv4() ? true : false;
238 bool migrating_ip_is_ipv4 = new_address.host().IsIPv4() ? true : false;
239 if (old_ip_is_ipv4 && !migrating_ip_is_ipv4) {
240 return IPV4_TO_IPV6_CHANGE;
241 }
242
243 if (!old_ip_is_ipv4) {
244 return migrating_ip_is_ipv4 ? IPV6_TO_IPV4_CHANGE : IPV6_TO_IPV6_CHANGE;
245 }
246
247 const int kSubnetMaskLength = 24;
248 if (old_address.host().InSameSubnet(new_address.host(), kSubnetMaskLength)) {
249 // Subnet part does not change (here, we use /24), which is considered to be
250 // caused by NATs.
251 return IPV4_SUBNET_CHANGE;
252 }
253
254 return IPV4_TO_IPV4_CHANGE;
255}
256
257// static
258void QuicUtils::CopyToBuffer(const struct iovec* iov,
259 int iov_count,
260 size_t iov_offset,
261 size_t buffer_length,
262 char* buffer) {
263 int iovnum = 0;
264 while (iovnum < iov_count && iov_offset >= iov[iovnum].iov_len) {
265 iov_offset -= iov[iovnum].iov_len;
266 ++iovnum;
267 }
268 DCHECK_LE(iovnum, iov_count);
269 DCHECK_LE(iov_offset, iov[iovnum].iov_len);
270 if (iovnum >= iov_count || buffer_length == 0) {
271 return;
272 }
273
274 // Unroll the first iteration that handles iov_offset.
275 const size_t iov_available = iov[iovnum].iov_len - iov_offset;
276 size_t copy_len = std::min(buffer_length, iov_available);
277
278 // Try to prefetch the next iov if there is at least one more after the
279 // current. Otherwise, it looks like an irregular access that the hardware
280 // prefetcher won't speculatively prefetch. Only prefetch one iov because
281 // generally, the iov_offset is not 0, input iov consists of 2K buffers and
282 // the output buffer is ~1.4K.
283 if (copy_len == iov_available && iovnum + 1 < iov_count) {
284 char* next_base = static_cast<char*>(iov[iovnum + 1].iov_base);
285 // Prefetch 2 cachelines worth of data to get the prefetcher started; leave
286 // it to the hardware prefetcher after that.
287 QuicPrefetchT0(next_base);
288 if (iov[iovnum + 1].iov_len >= 64) {
289 QuicPrefetchT0(next_base + QUIC_CACHELINE_SIZE);
290 }
291 }
292
293 const char* src = static_cast<char*>(iov[iovnum].iov_base) + iov_offset;
294 while (true) {
295 memcpy(buffer, src, copy_len);
296 buffer_length -= copy_len;
297 buffer += copy_len;
298 if (buffer_length == 0 || ++iovnum >= iov_count) {
299 break;
300 }
301 src = static_cast<char*>(iov[iovnum].iov_base);
302 copy_len = std::min(buffer_length, iov[iovnum].iov_len);
303 }
304 QUIC_BUG_IF(buffer_length > 0) << "Failed to copy entire length to buffer.";
305}
306
307// static
308struct iovec QuicUtils::MakeIovec(QuicStringPiece data) {
309 struct iovec iov = {const_cast<char*>(data.data()),
310 static_cast<size_t>(data.size())};
311 return iov;
312}
313
314// static
315bool QuicUtils::IsAckable(SentPacketState state) {
316 return state != NEVER_SENT && state != ACKED && state != UNACKABLE;
317}
318
319// static
320bool QuicUtils::IsRetransmittableFrame(QuicFrameType type) {
321 switch (type) {
322 case ACK_FRAME:
323 case PADDING_FRAME:
324 case STOP_WAITING_FRAME:
325 case MTU_DISCOVERY_FRAME:
326 return false;
327 default:
328 return true;
329 }
330}
331
332// static
333bool QuicUtils::IsHandshakeFrame(const QuicFrame& frame,
334 QuicTransportVersion transport_version) {
QUICHE teamea740082019-03-11 17:58:43 -0700335 if (!QuicVersionUsesCryptoFrames(transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500336 return frame.type == STREAM_FRAME &&
337 frame.stream_frame.stream_id == GetCryptoStreamId(transport_version);
338 } else {
339 return frame.type == CRYPTO_FRAME;
340 }
341}
342
343// static
344SentPacketState QuicUtils::RetransmissionTypeToPacketState(
345 TransmissionType retransmission_type) {
346 switch (retransmission_type) {
347 case ALL_UNACKED_RETRANSMISSION:
348 case ALL_INITIAL_RETRANSMISSION:
349 return UNACKABLE;
350 case HANDSHAKE_RETRANSMISSION:
351 return HANDSHAKE_RETRANSMITTED;
352 case LOSS_RETRANSMISSION:
353 return LOST;
354 case TLP_RETRANSMISSION:
355 return TLP_RETRANSMITTED;
356 case RTO_RETRANSMISSION:
357 return RTO_RETRANSMITTED;
358 case PROBING_RETRANSMISSION:
359 return PROBE_RETRANSMITTED;
360 default:
361 QUIC_BUG << QuicUtils::TransmissionTypeToString(retransmission_type)
362 << " is not a retransmission_type";
363 return UNACKABLE;
364 }
365}
366
367// static
368bool QuicUtils::IsIetfPacketHeader(uint8_t first_byte) {
369 return (first_byte & FLAGS_LONG_HEADER) || (first_byte & FLAGS_FIXED_BIT) ||
370 !(first_byte & FLAGS_DEMULTIPLEXING_BIT);
371}
372
373// static
374bool QuicUtils::IsIetfPacketShortHeader(uint8_t first_byte) {
375 return IsIetfPacketHeader(first_byte) && !(first_byte & FLAGS_LONG_HEADER);
376}
377
378// static
379QuicStreamId QuicUtils::GetInvalidStreamId(QuicTransportVersion version) {
380 return version == QUIC_VERSION_99 ? std::numeric_limits<QuicStreamId>::max()
381 : 0;
382}
383
384// static
385QuicStreamId QuicUtils::GetCryptoStreamId(QuicTransportVersion version) {
nharper46833c32019-05-15 21:33:05 -0700386 QUIC_BUG_IF(QuicVersionUsesCryptoFrames(version))
387 << "CRYPTO data aren't in stream frames; they have no stream ID.";
388 return QuicVersionUsesCryptoFrames(version) ? GetInvalidStreamId(version) : 1;
389}
390
391// static
392bool QuicUtils::IsCryptoStreamId(QuicTransportVersion version,
393 QuicStreamId stream_id) {
394 if (QuicVersionUsesCryptoFrames(version)) {
395 return false;
396 }
397 return stream_id == GetCryptoStreamId(version);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500398}
399
400// static
401QuicStreamId QuicUtils::GetHeadersStreamId(QuicTransportVersion version) {
dschinazi552accc2019-06-17 17:07:34 -0700402 if (version == QUIC_VERSION_99) {
403 // TODO(b/130659182) Turn this into a QUIC_BUG once we've fully removed
404 // the headers stream in those versions.
405 return GetQuicFlag(FLAGS_quic_headers_stream_id_in_v99);
406 }
nharpercd820e02019-05-16 15:12:07 -0700407 return GetFirstBidirectionalStreamId(version, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500408}
409
410// static
411bool QuicUtils::IsClientInitiatedStreamId(QuicTransportVersion version,
412 QuicStreamId id) {
413 if (id == GetInvalidStreamId(version)) {
414 return false;
415 }
416 return version == QUIC_VERSION_99 ? id % 2 == 0 : id % 2 != 0;
417}
418
419// static
420bool QuicUtils::IsServerInitiatedStreamId(QuicTransportVersion version,
421 QuicStreamId id) {
422 if (id == GetInvalidStreamId(version)) {
423 return false;
424 }
425 return version == QUIC_VERSION_99 ? id % 2 != 0 : id % 2 == 0;
426}
427
428// static
429bool QuicUtils::IsBidirectionalStreamId(QuicStreamId id) {
430 return id % 4 < 2;
431}
432
433// static
434StreamType QuicUtils::GetStreamType(QuicStreamId id,
435 Perspective perspective,
436 bool peer_initiated) {
437 if (IsBidirectionalStreamId(id)) {
438 return BIDIRECTIONAL;
439 }
440
441 if (peer_initiated) {
442 if (perspective == Perspective::IS_SERVER) {
443 DCHECK_EQ(2u, id % 4);
444 } else {
445 DCHECK_EQ(Perspective::IS_CLIENT, perspective);
446 DCHECK_EQ(3u, id % 4);
447 }
448 return READ_UNIDIRECTIONAL;
449 }
450
451 if (perspective == Perspective::IS_SERVER) {
452 DCHECK_EQ(3u, id % 4);
453 } else {
454 DCHECK_EQ(Perspective::IS_CLIENT, perspective);
455 DCHECK_EQ(2u, id % 4);
456 }
457 return WRITE_UNIDIRECTIONAL;
458}
459
460// static
461QuicStreamId QuicUtils::StreamIdDelta(QuicTransportVersion version) {
462 return version == QUIC_VERSION_99 ? 4 : 2;
463}
464
465// static
466QuicStreamId QuicUtils::GetFirstBidirectionalStreamId(
467 QuicTransportVersion version,
468 Perspective perspective) {
nharpercd820e02019-05-16 15:12:07 -0700469 if (version == QUIC_VERSION_99) {
470 return perspective == Perspective::IS_CLIENT ? 0 : 1;
471 } else if (QuicVersionUsesCryptoFrames(version)) {
472 return perspective == Perspective::IS_CLIENT ? 1 : 2;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500473 }
nharpercd820e02019-05-16 15:12:07 -0700474 return perspective == Perspective::IS_CLIENT ? 3 : 2;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500475}
476
477// static
478QuicStreamId QuicUtils::GetFirstUnidirectionalStreamId(
479 QuicTransportVersion version,
480 Perspective perspective) {
nharpercd820e02019-05-16 15:12:07 -0700481 if (version == QUIC_VERSION_99) {
482 return perspective == Perspective::IS_CLIENT ? 2 : 3;
483 } else if (QuicVersionUsesCryptoFrames(version)) {
484 return perspective == Perspective::IS_CLIENT ? 1 : 2;
485 }
486 return perspective == Perspective::IS_CLIENT ? 3 : 2;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500487}
488
489// static
490QuicConnectionId QuicUtils::CreateRandomConnectionId() {
QUICHE teamc65d1d12019-03-19 20:58:04 -0700491 return CreateRandomConnectionId(kQuicDefaultConnectionIdLength,
492 QuicRandom::GetInstance());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500493}
494
495// static
496QuicConnectionId QuicUtils::CreateRandomConnectionId(QuicRandom* random) {
QUICHE teamc65d1d12019-03-19 20:58:04 -0700497 return CreateRandomConnectionId(kQuicDefaultConnectionIdLength, random);
498}
499// static
500QuicConnectionId QuicUtils::CreateRandomConnectionId(
501 uint8_t connection_id_length) {
502 return CreateRandomConnectionId(connection_id_length,
503 QuicRandom::GetInstance());
504}
505
506// static
507QuicConnectionId QuicUtils::CreateRandomConnectionId(
508 uint8_t connection_id_length,
509 QuicRandom* random) {
510 if (connection_id_length == 0) {
511 return EmptyQuicConnectionId();
512 }
513 if (connection_id_length > kQuicMaxConnectionIdLength) {
514 QUIC_BUG << "Tried to CreateRandomConnectionId of invalid length "
515 << static_cast<int>(connection_id_length);
516 connection_id_length = kQuicMaxConnectionIdLength;
517 }
518 char connection_id_bytes[kQuicMaxConnectionIdLength];
519 random->RandBytes(connection_id_bytes, connection_id_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500520 return QuicConnectionId(static_cast<char*>(connection_id_bytes),
QUICHE teamc65d1d12019-03-19 20:58:04 -0700521 connection_id_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500522}
523
524// static
525bool QuicUtils::VariableLengthConnectionIdAllowedForVersion(
526 QuicTransportVersion version) {
dschinazi8eb45e92019-05-10 11:36:15 -0700527 if (!GetQuicRestartFlag(
528 quic_allow_variable_length_connection_id_for_negotiation)) {
529 return version >= QUIC_VERSION_47;
530 }
531 QUIC_RESTART_FLAG_COUNT(
532 quic_allow_variable_length_connection_id_for_negotiation);
533 // We allow variable length connection IDs for unsupported versions to
534 // ensure that IETF version negotiation works when other implementations
535 // trigger version negotiation with custom connection ID lengths.
536 return version >= QUIC_VERSION_47 || version == QUIC_VERSION_UNSUPPORTED;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500537}
538
539// static
540QuicConnectionId QuicUtils::CreateZeroConnectionId(
541 QuicTransportVersion version) {
542 if (!VariableLengthConnectionIdAllowedForVersion(version)) {
543 char connection_id_bytes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
544 return QuicConnectionId(static_cast<char*>(connection_id_bytes),
545 QUIC_ARRAYSIZE(connection_id_bytes));
546 }
547 return EmptyQuicConnectionId();
548}
549
550// static
551bool QuicUtils::IsConnectionIdValidForVersion(QuicConnectionId connection_id,
552 QuicTransportVersion version) {
553 if (VariableLengthConnectionIdAllowedForVersion(version)) {
554 return true;
555 }
556 return connection_id.length() == kQuicDefaultConnectionIdLength;
557}
558
559QuicUint128 QuicUtils::GenerateStatelessResetToken(
560 QuicConnectionId connection_id) {
561 uint64_t data_bytes[3] = {0, 0, 0};
562 static_assert(sizeof(data_bytes) >= kQuicMaxConnectionIdLength,
563 "kQuicMaxConnectionIdLength changed");
564 memcpy(data_bytes, connection_id.data(), connection_id.length());
565 // This is designed so that the common case of 64bit connection IDs
566 // produces a stateless reset token that is equal to the connection ID
567 // interpreted as a 64bit unsigned integer, to facilitate debugging.
568 return MakeQuicUint128(
569 QuicEndian::NetToHost64(sizeof(uint64_t) ^ connection_id.length() ^
570 data_bytes[1] ^ data_bytes[2]),
571 QuicEndian::NetToHost64(data_bytes[0]));
572}
573
fkastenholz3c4eabf2019-04-22 07:49:59 -0700574// Returns the maximum value that a stream count may have, taking into account
575// the fact that bidirectional, client initiated, streams have one fewer stream
576// available than the others. This is because the old crypto streams, with ID ==
577// 0 are not included in the count.
578// The version is not included in the call, nor does the method take the version
579// into account, because this is called only from code used for IETF QUIC.
580// TODO(fkastenholz): Remove this method and replace calls to it with direct
581// references to kMaxQuicStreamIdCount when streamid 0 becomes a normal stream
582// id.
583// static
584QuicStreamCount QuicUtils::GetMaxStreamCount(bool unidirectional,
585 Perspective perspective) {
586 if (!unidirectional && perspective == Perspective::IS_CLIENT) {
587 return kMaxQuicStreamCount >> 2;
588 }
589 return (kMaxQuicStreamCount >> 2) + 1;
590}
591
QUICHE team10b22a12019-03-21 15:31:42 -0700592// static
593PacketNumberSpace QuicUtils::GetPacketNumberSpace(
594 EncryptionLevel encryption_level) {
595 switch (encryption_level) {
596 case ENCRYPTION_INITIAL:
597 return INITIAL_DATA;
598 case ENCRYPTION_HANDSHAKE:
599 return HANDSHAKE_DATA;
600 case ENCRYPTION_ZERO_RTT:
601 case ENCRYPTION_FORWARD_SECURE:
602 return APPLICATION_DATA;
603 default:
604 QUIC_BUG << "Try to get packet number space of encryption level: "
605 << EncryptionLevelToString(encryption_level);
606 return NUM_PACKET_NUMBER_SPACES;
607 }
608}
609
QUICHE team1dfa46b2019-03-22 10:39:10 -0700610// static
611EncryptionLevel QuicUtils::GetEncryptionLevel(
612 PacketNumberSpace packet_number_space) {
613 switch (packet_number_space) {
614 case INITIAL_DATA:
615 return ENCRYPTION_INITIAL;
616 case HANDSHAKE_DATA:
617 return ENCRYPTION_HANDSHAKE;
618 case APPLICATION_DATA:
619 return ENCRYPTION_FORWARD_SECURE;
620 default:
621 DCHECK(false);
622 return NUM_ENCRYPTION_LEVELS;
623 }
624}
625
QUICHE teama6ef0a62019-03-07 20:34:33 -0500626#undef RETURN_STRING_LITERAL // undef for jumbo builds
627} // namespace quic