Introduce IsConnectionIdLengthValidForVersion

This CL is part of a larger change to allow the new connection ID invariants. It adds a new method QuicUtils::IsConnectionIdLengthValidForVersion() whose goal will be to replace uses of kQuicMaxConnectionIdLength in the codebase. This CL also plumbs the QUIC version to the TLS transport parameter parse/serialize code so it can call IsConnectionIdLengthValidForVersion. I suspect the transport parameter code will eventually need the version anyway as we create more QUIC versions that support TLS.

gfe-relnote: refactor, protected by disabled quic_enable_v47 flag.
PiperOrigin-RevId: 260938227
Change-Id: I590f7117de2b245044469e6dcdcca6f503c7a625
diff --git a/quic/core/quic_utils.cc b/quic/core/quic_utils.cc
index 160ac65..a9de14a 100644
--- a/quic/core/quic_utils.cc
+++ b/quic/core/quic_utils.cc
@@ -540,12 +540,30 @@
 }
 
 // static
-bool QuicUtils::IsConnectionIdValidForVersion(QuicConnectionId connection_id,
-                                              QuicTransportVersion version) {
-  if (VariableLengthConnectionIdAllowedForVersion(version)) {
-    return true;
+bool QuicUtils::IsConnectionIdLengthValidForVersion(
+    size_t connection_id_length,
+    QuicTransportVersion transport_version) {
+  // No version of QUIC can support lengths that do not fit in an uint8_t.
+  if (connection_id_length >
+      static_cast<size_t>(std::numeric_limits<uint8_t>::max())) {
+    return false;
   }
-  return connection_id.length() == kQuicDefaultConnectionIdLength;
+  const uint8_t connection_id_length8 =
+      static_cast<uint8_t>(connection_id_length);
+  // Versions that do not support variable lengths only support length 8.
+  if (!VariableLengthConnectionIdAllowedForVersion(transport_version)) {
+    return connection_id_length8 == kQuicDefaultConnectionIdLength;
+  }
+  // Currently all other versions require the length to be at most 18 bytes.
+  return connection_id_length8 <= kQuicMaxConnectionIdLength;
+}
+
+// static
+bool QuicUtils::IsConnectionIdValidForVersion(
+    QuicConnectionId connection_id,
+    QuicTransportVersion transport_version) {
+  return IsConnectionIdLengthValidForVersion(connection_id.length(),
+                                             transport_version);
 }
 
 QuicUint128 QuicUtils::GenerateStatelessResetToken(