Refactor out the logic which responds to connectivity probing and connection migration from QuicConnection::OnPacketComplete(). No behavior change, not protected.

Split the logic into IETF branch and GQuic branch.

PiperOrigin-RevId: 324848626
Change-Id: I2444f6697c524cafc35e3036e81818eb1c8213d2
diff --git a/quic/core/quic_connection.cc b/quic/core/quic_connection.cc
index f3d5431..16d7e5c 100644
--- a/quic/core/quic_connection.cc
+++ b/quic/core/quic_connection.cc
@@ -1665,44 +1665,7 @@
       << ENDPOINT << "Received a padded PING packet. is_probing: "
       << IsCurrentPacketConnectivityProbing();
 
-  if (IsCurrentPacketConnectivityProbing()) {
-    DCHECK(!version().HasIetfQuicFrames());
-    visitor_->OnPacketReceived(last_packet_destination_address_,
-                               last_packet_source_address_,
-                               /*is_connectivity_probe=*/true);
-  } else if (perspective_ == Perspective::IS_CLIENT) {
-    // This node is a client, notify that a speculative connectivity probing
-    // packet has been received anyway.
-    QUIC_DVLOG(1) << ENDPOINT
-                  << "Received a speculative connectivity probing packet for "
-                  << GetServerConnectionIdAsRecipient(last_header_,
-                                                      perspective_)
-                  << " from ip:port: " << last_packet_source_address_.ToString()
-                  << " to ip:port: "
-                  << last_packet_destination_address_.ToString();
-    visitor_->OnPacketReceived(last_packet_destination_address_,
-                               last_packet_source_address_,
-                               /*is_connectivity_probe=*/false);
-  } else if (version().HasIetfQuicFrames() &&
-             !received_path_challenge_payloads_.empty()) {
-    if (current_effective_peer_migration_type_ != NO_CHANGE) {
-      // TODO(b/150095588): change the stats to
-      // num_valid_path_challenge_received.
-      ++stats_.num_connectivity_probing_received;
-    }
-    // If the packet contains PATH CHALLENGE, send appropriate RESPONSE.
-    // There was at least one PATH CHALLENGE in the received packet,
-    // Generate the required PATH RESPONSE.
-    SendGenericPathProbePacket(nullptr, last_packet_source_address_,
-                               /* is_response=*/true);
-  } else if (last_header_.packet_number == GetLargestReceivedPacket()) {
-    direct_peer_address_ = last_packet_source_address_;
-    if (current_effective_peer_migration_type_ != NO_CHANGE) {
-      // TODO(fayang): When multiple packet number spaces is supported, only
-      // start peer migration for the application data.
-      StartEffectivePeerMigration(current_effective_peer_migration_type_);
-    }
-  }
+  MaybeRespondToConnectivityProbingOrMigration();
 
   current_effective_peer_migration_type_ = NO_CHANGE;
 
@@ -1721,6 +1684,64 @@
   CloseIfTooManyOutstandingSentPackets();
 }
 
+void QuicConnection::MaybeRespondToConnectivityProbingOrMigration() {
+  if (version().HasIetfQuicFrames()) {
+    if (perspective_ == Perspective::IS_CLIENT) {
+      // This node is a client, notify that a speculative connectivity probing
+      // packet has been received anyway.
+      visitor_->OnPacketReceived(last_packet_destination_address_,
+                                 last_packet_source_address_,
+                                 /*is_connectivity_probe=*/false);
+      return;
+    }
+    if (!received_path_challenge_payloads_.empty()) {
+      if (current_effective_peer_migration_type_ != NO_CHANGE) {
+        // TODO(b/150095588): change the stats to
+        // num_valid_path_challenge_received.
+        ++stats_.num_connectivity_probing_received;
+      }
+      // If the packet contains PATH CHALLENGE, send appropriate RESPONSE.
+      // There was at least one PATH CHALLENGE in the received packet,
+      // Generate the required PATH RESPONSE.
+      SendGenericPathProbePacket(nullptr, last_packet_source_address_,
+                                 /* is_response=*/true);
+      return;
+    }
+  } else {
+    if (IsCurrentPacketConnectivityProbing()) {
+      visitor_->OnPacketReceived(last_packet_destination_address_,
+                                 last_packet_source_address_,
+                                 /*is_connectivity_probe=*/true);
+      return;
+    }
+    if (perspective_ == Perspective::IS_CLIENT) {
+      // This node is a client, notify that a speculative connectivity probing
+      // packet has been received anyway.
+      QUIC_DVLOG(1) << ENDPOINT
+                    << "Received a speculative connectivity probing packet for "
+                    << GetServerConnectionIdAsRecipient(last_header_,
+                                                        perspective_)
+                    << " from ip:port: "
+                    << last_packet_source_address_.ToString() << " to ip:port: "
+                    << last_packet_destination_address_.ToString();
+      visitor_->OnPacketReceived(last_packet_destination_address_,
+                                 last_packet_source_address_,
+                                 /*is_connectivity_probe=*/false);
+      return;
+    }
+  }
+  // Server starts to migrate connection upon receiving of non-probing packet
+  // from a new peer address.
+  if (last_header_.packet_number == GetLargestReceivedPacket()) {
+    direct_peer_address_ = last_packet_source_address_;
+    if (current_effective_peer_migration_type_ != NO_CHANGE) {
+      // TODO(fayang): When multiple packet number spaces is supported, only
+      // start peer migration for the application data.
+      StartEffectivePeerMigration(current_effective_peer_migration_type_);
+    }
+  }
+}
+
 bool QuicConnection::IsValidStatelessResetToken(QuicUint128 token) const {
   return stateless_reset_token_received_ &&
          token == received_stateless_reset_token_;
diff --git a/quic/core/quic_connection.h b/quic/core/quic_connection.h
index c1bf5bb..50c6496 100644
--- a/quic/core/quic_connection.h
+++ b/quic/core/quic_connection.h
@@ -1341,6 +1341,15 @@
   void MaybeActivateLegacyVersionEncapsulation();
   void MaybeDisactivateLegacyVersionEncapsulation();
 
+  // For Google Quic, if the current packet is connectivity probing packet, call
+  // session OnPacketReceived() which eventually sends connectivity probing
+  // response on server side. And no-op on client side. And for both Google Quic
+  // and IETF Quic, start migration if the current packet is a non-probing
+  // packet.
+  // TODO(danzh) rename to MaybeRespondToPeerMigration() when Google Quic is
+  // deprecated.
+  void MaybeRespondToConnectivityProbingOrMigration();
+
   QuicFramer framer_;
 
   // Contents received in the current packet, especially used to identify