Set initial self address for all server QUIC connections and client connections that know its own address.

Protected by FLAGS_quic_reloadable_flag_quic_connection_set_initial_self_address.

PiperOrigin-RevId: 334900288
Change-Id: I06ef5fe30bf63a4cebcf97a17efc577eb7fcc29a
diff --git a/quic/core/quic_connection.cc b/quic/core/quic_connection.cc
index accc271..2b4d04c 100644
--- a/quic/core/quic_connection.cc
+++ b/quic/core/quic_connection.cc
@@ -211,6 +211,7 @@
 
 QuicConnection::QuicConnection(
     QuicConnectionId server_connection_id,
+    QuicSocketAddress initial_self_address,
     QuicSocketAddress initial_peer_address,
     QuicConnectionHelperInterface* helper,
     QuicAlarmFactory* alarm_factory,
@@ -237,6 +238,10 @@
       server_connection_id_(server_connection_id),
       client_connection_id_(EmptyQuicConnectionId()),
       client_connection_id_is_set_(false),
+      self_address_(
+          GetQuicReloadableFlag(quic_connection_set_initial_self_address)
+              ? initial_self_address
+              : QuicSocketAddress()),
       peer_address_(initial_peer_address),
       direct_peer_address_(initial_peer_address),
       active_effective_peer_migration_type_(NO_CHANGE),
@@ -319,6 +324,11 @@
                              alarm_factory_),
       support_handshake_done_(version().HasHandshakeDone()) {
   QUIC_BUG_IF(!start_peer_migration_earlier_ && send_path_response_);
+  if (GetQuicReloadableFlag(quic_connection_set_initial_self_address)) {
+    DCHECK(perspective_ == Perspective::IS_CLIENT ||
+           self_address_.IsInitialized());
+    QUIC_RELOADABLE_FLAG_COUNT(quic_connection_set_initial_self_address);
+  }
   if (fix_missing_connected_checks_) {
     QUIC_RELOADABLE_FLAG_COUNT(quic_add_missing_connected_checks);
   }
diff --git a/quic/core/quic_connection.h b/quic/core/quic_connection.h
index 37c9801..0617478 100644
--- a/quic/core/quic_connection.h
+++ b/quic/core/quic_connection.h
@@ -417,6 +417,7 @@
   // specifies whether the connection takes ownership of |writer|. |helper| must
   // outlive this connection.
   QuicConnection(QuicConnectionId server_connection_id,
+                 QuicSocketAddress initial_self_address,
                  QuicSocketAddress initial_peer_address,
                  QuicConnectionHelperInterface* helper,
                  QuicAlarmFactory* alarm_factory,
diff --git a/quic/core/quic_connection_test.cc b/quic/core/quic_connection_test.cc
index 04601c1..0fa0b49 100644
--- a/quic/core/quic_connection_test.cc
+++ b/quic/core/quic_connection_test.cc
@@ -181,14 +181,16 @@
 class TestConnection : public QuicConnection {
  public:
   TestConnection(QuicConnectionId connection_id,
-                 QuicSocketAddress address,
+                 QuicSocketAddress initial_self_address,
+                 QuicSocketAddress initial_peer_address,
                  TestConnectionHelper* helper,
                  TestAlarmFactory* alarm_factory,
                  TestPacketWriter* writer,
                  Perspective perspective,
                  ParsedQuicVersion version)
       : QuicConnection(connection_id,
-                       address,
+                       initial_self_address,
+                       initial_peer_address,
                        helper,
                        alarm_factory,
                        writer,
@@ -593,6 +595,7 @@
         writer_(
             new TestPacketWriter(version(), &clock_, Perspective::IS_CLIENT)),
         connection_(connection_id_,
+                    kSelfAddress,
                     kPeerAddress,
                     helper_.get(),
                     alarm_factory_.get(),
@@ -1510,6 +1513,7 @@
   QuicIpAddress host;
   host.FromString("1.1.1.1");
   QuicSocketAddress self_address1(host, 443);
+  connection_.SetSelfAddress(self_address1);
   ProcessFramePacketWithAddresses(MakeCryptoFrame(), self_address1,
                                   kPeerAddress, ENCRYPTION_INITIAL);
   // Cause self_address change to mapped Ipv4 address.
@@ -2229,8 +2233,8 @@
 }
 
 TEST_P(QuicConnectionTest, SmallerServerMaxPacketSize) {
-  TestConnection connection(TestConnectionId(), kPeerAddress, helper_.get(),
-                            alarm_factory_.get(), writer_.get(),
+  TestConnection connection(TestConnectionId(), kSelfAddress, kPeerAddress,
+                            helper_.get(), alarm_factory_.get(), writer_.get(),
                             Perspective::IS_SERVER, version());
   EXPECT_EQ(Perspective::IS_SERVER, connection.perspective());
   EXPECT_EQ(1000u, connection.max_packet_length());
@@ -2344,8 +2348,8 @@
   const QuicConnectionId connection_id = TestConnectionId(17);
   const QuicByteCount lower_max_packet_size = 1240;
   writer_->set_max_packet_size(lower_max_packet_size);
-  TestConnection connection(connection_id, kPeerAddress, helper_.get(),
-                            alarm_factory_.get(), writer_.get(),
+  TestConnection connection(connection_id, kSelfAddress, kPeerAddress,
+                            helper_.get(), alarm_factory_.get(), writer_.get(),
                             Perspective::IS_CLIENT, version());
   EXPECT_EQ(Perspective::IS_CLIENT, connection.perspective());
   EXPECT_EQ(lower_max_packet_size, connection.max_packet_length());
@@ -6829,11 +6833,11 @@
 }
 
 TEST_P(QuicConnectionTest, Pacing) {
-  TestConnection server(connection_id_, kSelfAddress, helper_.get(),
-                        alarm_factory_.get(), writer_.get(),
+  TestConnection server(connection_id_, kPeerAddress, kSelfAddress,
+                        helper_.get(), alarm_factory_.get(), writer_.get(),
                         Perspective::IS_SERVER, version());
-  TestConnection client(connection_id_, kPeerAddress, helper_.get(),
-                        alarm_factory_.get(), writer_.get(),
+  TestConnection client(connection_id_, kSelfAddress, kPeerAddress,
+                        helper_.get(), alarm_factory_.get(), writer_.get(),
                         Perspective::IS_CLIENT, version());
   EXPECT_FALSE(QuicSentPacketManagerPeer::UsingPacing(
       static_cast<const QuicSentPacketManager*>(