Detangle p2p/connection.cc and port.cc

This CL does:
- Run IWYU on the relevant elements
- Make connection depend on port_interface, not port
- Make port_allocator depend only on port
- Move some constants from port.h into p2p_constants

This allows a dependency graph without ugly groups.

Bug: webrtc:15796
Change-Id: I0ff0e14eacdfe3b230a8d84902a78eb062d6c8af
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/336320
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41618}
diff --git a/api/BUILD.gn b/api/BUILD.gn
index cb23ea3..08e33ca 100644
--- a/api/BUILD.gn
+++ b/api/BUILD.gn
@@ -334,7 +334,7 @@
     ":sequence_checker",
     ":turn_customizer",
     "../call:rtp_interfaces",
-    "../p2p:connection_and_port",
+    "../p2p:connection",
     "../p2p:rtc_p2p",
     "../pc:media_factory",
     "../rtc_base:copy_on_write_buffer",
@@ -936,7 +936,7 @@
     ":make_ref_counted",
     ":packet_socket_factory",
     ":scoped_refptr",
-    "../p2p:connection_and_port",
+    "../p2p:connection",
     "../p2p:ice_transport_internal",
     "../p2p:p2p_constants",
     "../p2p:p2p_transport_channel",
diff --git a/api/test/pclf/BUILD.gn b/api/test/pclf/BUILD.gn
index 6d1d2be..20d7077 100644
--- a/api/test/pclf/BUILD.gn
+++ b/api/test/pclf/BUILD.gn
@@ -74,7 +74,7 @@
     "../../../api/transport:network_control",
     "../../../api/video_codecs:video_codecs_api",
     "../../../modules/audio_processing:api",
-    "../../../p2p:connection_and_port",
+    "../../../p2p:connection",
     "../../../p2p:rtc_p2p",
     "../../../rtc_base:network",
     "../../../rtc_base:rtc_certificate_generator",
diff --git a/examples/BUILD.gn b/examples/BUILD.gn
index 991f9d8..c3f66e0 100644
--- a/examples/BUILD.gn
+++ b/examples/BUILD.gn
@@ -702,7 +702,7 @@
       "../api/video_codecs:video_codecs_api",
       "../media:media_channel",
       "../media:rtc_media_base",
-      "../p2p:connection_and_port",
+      "../p2p:connection",
       "../p2p:rtc_p2p",
       "../pc:video_track_source",
       "../rtc_base:async_dns_resolver",
diff --git a/p2p/BUILD.gn b/p2p/BUILD.gn
index 140bf21..8a5c8df 100644
--- a/p2p/BUILD.gn
+++ b/p2p/BUILD.gn
@@ -74,7 +74,7 @@
     ":basic_async_resolver_factory",
     ":basic_ice_controller",
     ":candidate_pair_interface",
-    ":connection_and_port",
+    ":connection",
     ":connection_info",
     ":ice_agent_interface",
     ":ice_controller_factory_interface",
@@ -169,6 +169,7 @@
   absl_deps = [
     "//third_party/abseil-cpp/absl/algorithm:container",
     "//third_party/abseil-cpp/absl/base:core_headers",
+    "//third_party/abseil-cpp/absl/functional:any_invocable",
     "//third_party/abseil-cpp/absl/memory",
     "//third_party/abseil-cpp/absl/strings",
     "//third_party/abseil-cpp/absl/types:optional",
@@ -187,7 +188,7 @@
 rtc_source_set("active_ice_controller_interface") {
   sources = [ "base/active_ice_controller_interface.h" ]
   deps = [
-    ":connection_and_port",
+    ":connection",
     ":ice_switch_reason",
     ":ice_transport_internal",
     ":transport_description",
@@ -248,14 +249,10 @@
 # This set of files is too intertwined to break into separate
 # targets at this point.
 # TODO(bugs.webrtc.org/15796): Finish breakup
-rtc_library("connection_and_port") {
+rtc_library("connection") {
   sources = [
     "base/connection.cc",
     "base/connection.h",
-    "base/port.cc",
-    "base/port.h",
-    "base/port_allocator.cc",
-    "base/port_allocator.h",
   ]
   deps = [
     ":candidate_pair_interface",
@@ -276,28 +273,37 @@
     "../api/transport:enums",
     "../api/transport:field_trial_based_config",
     "../api/transport:stun_types",
+    "../api/units:time_delta",
     "../api/units:timestamp",
     "../logging:ice_log",
     "../rtc_base:async_packet_socket",
+    "../rtc_base:byte_buffer",
     "../rtc_base:callback_list",
     "../rtc_base:checks",
     "../rtc_base:crc32",
+    "../rtc_base:dscp",
     "../rtc_base:event_tracer",
+    "../rtc_base:ip_address",
     "../rtc_base:logging",
+    "../rtc_base:macromagic",
     "../rtc_base:mdns_responder_interface",
     "../rtc_base:net_helper",
     "../rtc_base:network",
+    "../rtc_base:network_constants",
     "../rtc_base:proxy_info",
     "../rtc_base:rate_tracker",
     "../rtc_base:rtc_numerics",
     "../rtc_base:safe_minmax",
+    "../rtc_base:socket",
     "../rtc_base:socket_address",
     "../rtc_base:ssl",
     "../rtc_base:stringutils",
     "../rtc_base:threading",
+    "../rtc_base:timeutils",
     "../rtc_base:weak_ptr",
     "../rtc_base/memory:always_valid_pointer",
     "../rtc_base/network:received_packet",
+    "../rtc_base/network:sent_packet",
     "../rtc_base/system:rtc_export",
     "../rtc_base/third_party/base64",
     "../rtc_base/third_party/sigslot",
@@ -305,6 +311,7 @@
   absl_deps = [
     "//third_party/abseil-cpp/absl/algorithm:container",
     "//third_party/abseil-cpp/absl/base:core_headers",
+    "//third_party/abseil-cpp/absl/functional:any_invocable",
     "//third_party/abseil-cpp/absl/memory",
     "//third_party/abseil-cpp/absl/strings",
     "//third_party/abseil-cpp/absl/strings:string_view",
@@ -327,7 +334,7 @@
 rtc_source_set("ice_agent_interface") {
   sources = [ "base/ice_agent_interface.h" ]
   deps = [
-    ":connection_and_port",
+    ":connection",
     ":ice_switch_reason",
     "../api:array_view",
   ]
@@ -339,7 +346,7 @@
     "base/ice_controller_interface.h",
   ]
   deps = [
-    ":connection_and_port",
+    ":connection",
     ":ice_switch_reason",
     ":ice_transport_internal",
     "../rtc_base:checks",
@@ -382,9 +389,10 @@
     "base/ice_transport_internal.h",
   ]
   deps = [
-    ":connection_and_port",
+    ":connection",
     ":p2p_constants",
     ":packet_transport_internal",
+    ":port",
     ":stun_dictionary",
     ":transport_description",
     "../api:candidate",
@@ -418,7 +426,7 @@
     ":active_ice_controller_factory_interface",
     ":basic_ice_controller",
     ":candidate_pair_interface",
-    ":connection_and_port",
+    ":connection",
     ":connection_info",
     ":ice_agent_interface",
     ":ice_controller_factory_interface",
@@ -427,6 +435,8 @@
     ":ice_transport_internal",
     ":p2p_constants",
     ":p2p_transport_channel_ice_field_trials",
+    ":port",
+    ":port_allocator",
     ":port_interface",
     ":regathering_controller",
     ":stun_dictionary",
@@ -487,7 +497,8 @@
     "base/packet_transport_internal.h",
   ]
   deps = [
-    ":connection_and_port",
+    ":connection",
+    ":port",
     "../rtc_base:async_packet_socket",
     "../rtc_base:network_route",
     "../rtc_base:socket",
@@ -497,6 +508,94 @@
   absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
 }
 
+rtc_library("port") {
+  sources = [
+    "base/port.cc",
+    "base/port.h",
+  ]
+  deps = [
+    ":candidate_pair_interface",
+    ":connection",
+    ":connection_info",
+    ":p2p_constants",
+    ":port_interface",
+    ":stun_request",
+    ":transport_description",
+    "../api:array_view",
+    "../api:candidate",
+    "../api:field_trials_view",
+    "../api:packet_socket_factory",
+    "../api:rtc_error",
+    "../api:sequence_checker",
+    "../api/task_queue",
+    "../api/transport:field_trial_based_config",
+    "../api/transport:stun_types",
+    "../api/units:time_delta",
+    "../logging:ice_log",
+    "../rtc_base:async_packet_socket",
+    "../rtc_base:byte_buffer",
+    "../rtc_base:callback_list",
+    "../rtc_base:checks",
+    "../rtc_base:crc32",
+    "../rtc_base:dscp",
+    "../rtc_base:event_tracer",
+    "../rtc_base:ip_address",
+    "../rtc_base:logging",
+    "../rtc_base:macromagic",
+    "../rtc_base:mdns_responder_interface",
+    "../rtc_base:net_helper",
+    "../rtc_base:network",
+    "../rtc_base:proxy_info",
+    "../rtc_base:rate_tracker",
+    "../rtc_base:socket_address",
+    "../rtc_base:ssl",
+    "../rtc_base:stringutils",
+    "../rtc_base:timeutils",
+    "../rtc_base:weak_ptr",
+    "../rtc_base/memory:always_valid_pointer",
+    "../rtc_base/network:received_packet",
+    "../rtc_base/network:sent_packet",
+    "../rtc_base/system:rtc_export",
+    "../rtc_base/third_party/sigslot",
+  ]
+  absl_deps = [
+    "//third_party/abseil-cpp/absl/base:core_headers",
+    "//third_party/abseil-cpp/absl/memory",
+    "//third_party/abseil-cpp/absl/strings",
+    "//third_party/abseil-cpp/absl/strings:string_view",
+    "//third_party/abseil-cpp/absl/types:optional",
+  ]
+}
+
+rtc_library("port_allocator") {
+  sources = [
+    "base/port_allocator.cc",
+    "base/port_allocator.h",
+  ]
+  deps = [
+    ":connection",
+    ":ice_credentials_iterator",
+    ":port",
+    ":port_interface",
+    ":transport_description",
+    "../api:candidate",
+    "../api:sequence_checker",
+    "../api/transport:enums",
+    "../rtc_base:checks",
+    "../rtc_base:network",
+    "../rtc_base:proxy_info",
+    "../rtc_base:socket_address",
+    "../rtc_base:ssl",
+    "../rtc_base:threading",
+    "../rtc_base/system:rtc_export",
+    "../rtc_base/third_party/sigslot",
+  ]
+  absl_deps = [
+    "//third_party/abseil-cpp/absl/strings:string_view",
+    "//third_party/abseil-cpp/absl/types:optional",
+  ]
+}
+
 rtc_library("port_interface") {
   sources = [
     "base/port_interface.cc",
@@ -505,8 +604,10 @@
   deps = [
     ":transport_description",
     "../api:candidate",
+    "../api:packet_socket_factory",
     "../rtc_base:async_packet_socket",
     "../rtc_base:callback_list",
+    "../rtc_base:proxy_info",
     "../rtc_base:socket_address",
   ]
   absl_deps = [
@@ -521,8 +622,9 @@
     "base/regathering_controller.h",
   ]
   deps = [
-    ":connection_and_port",
+    ":connection",
     ":ice_transport_internal",
+    ":port_allocator",
     "../api/task_queue:pending_task_safety_flag",
     "../api/units:time_delta",
     "../rtc_base:threading",
@@ -590,7 +692,7 @@
   deps = [
     ":active_ice_controller_interface",
     ":basic_ice_controller",
-    ":connection_and_port",
+    ":connection",
     ":ice_agent_interface",
     ":ice_controller_factory_interface",
     ":ice_controller_interface",
@@ -634,7 +736,7 @@
     visibility = [ "*" ]
     sources = [ "base/fake_port_allocator.h" ]
     deps = [
-      ":connection_and_port",
+      ":connection",
       ":rtc_p2p",
       "../rtc_base:net_helpers",
       "../rtc_base:net_test_helpers",
@@ -663,7 +765,7 @@
     deps = [
       ":active_ice_controller_factory_interface",
       ":active_ice_controller_interface",
-      ":connection_and_port",
+      ":connection",
       ":fake_ice_transport",
       ":fake_port_allocator",
       ":ice_agent_interface",
@@ -731,7 +833,7 @@
       ":active_ice_controller_interface",
       ":async_stun_tcp_socket",
       ":basic_ice_controller",
-      ":connection_and_port",
+      ":connection",
       ":fake_ice_transport",
       ":fake_port_allocator",
       ":ice_credentials_iterator",
diff --git a/p2p/base/candidate_pair_interface.h b/p2p/base/candidate_pair_interface.h
index 2b68fd7..a04ab13 100644
--- a/p2p/base/candidate_pair_interface.h
+++ b/p2p/base/candidate_pair_interface.h
@@ -23,6 +23,18 @@
   virtual const Candidate& remote_candidate() const = 0;
 };
 
+// Specific implementation of the interface, suitable for being a
+// data member of other structs.
+struct CandidatePair final : public CandidatePairInterface {
+  ~CandidatePair() override = default;
+
+  const Candidate& local_candidate() const override { return local; }
+  const Candidate& remote_candidate() const override { return remote; }
+
+  Candidate local;
+  Candidate remote;
+};
+
 }  // namespace cricket
 
 #endif  // P2P_BASE_CANDIDATE_PAIR_INTERFACE_H_
diff --git a/p2p/base/connection.cc b/p2p/base/connection.cc
index bf07dec..9dd8a80 100644
--- a/p2p/base/connection.cc
+++ b/p2p/base/connection.cc
@@ -19,25 +19,26 @@
 #include <vector>
 
 #include "absl/algorithm/container.h"
-#include "absl/strings/escaping.h"
-#include "absl/strings/match.h"
 #include "absl/strings/string_view.h"
 #include "absl/types/optional.h"
 #include "api/array_view.h"
 #include "api/units/timestamp.h"
-#include "p2p/base/port_allocator.h"
+#include "p2p/base/p2p_constants.h"
+#include "rtc_base/byte_buffer.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/crc32.h"
 #include "rtc_base/helpers.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/mdns_responder_interface.h"
-#include "rtc_base/message_digest.h"
+#include "rtc_base/net_helper.h"
 #include "rtc_base/network.h"
+#include "rtc_base/network/sent_packet.h"
+#include "rtc_base/network_constants.h"
 #include "rtc_base/numerics/safe_minmax.h"
+#include "rtc_base/socket.h"
+#include "rtc_base/socket_address.h"
 #include "rtc_base/string_encode.h"
 #include "rtc_base/string_utils.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/third_party/base64/base64.h"
+#include "rtc_base/time_utils.h"
 
 namespace cricket {
 namespace {
@@ -213,7 +214,7 @@
   return CONNECTION_RESPONSE_TIMEOUT;
 }
 
-Connection::Connection(rtc::WeakPtr<Port> port,
+Connection::Connection(rtc::WeakPtr<PortInterface> port,
                        size_t index,
                        const Candidate& remote_candidate)
     : network_thread_(port->thread()),
@@ -1784,7 +1785,7 @@
   pings_since_last_response_.clear();
 }
 
-ProxyConnection::ProxyConnection(rtc::WeakPtr<Port> port,
+ProxyConnection::ProxyConnection(rtc::WeakPtr<PortInterface> port,
                                  size_t index,
                                  const Candidate& remote_candidate)
     : Connection(std::move(port), index, remote_candidate) {}
diff --git a/p2p/base/connection.h b/p2p/base/connection.h
index cf54dc8..583e62e 100644
--- a/p2p/base/connection.h
+++ b/p2p/base/connection.h
@@ -11,19 +11,31 @@
 #ifndef P2P_BASE_CONNECTION_H_
 #define P2P_BASE_CONNECTION_H_
 
+#include <stddef.h>
+
+#include <cstdint>
+#include <functional>
 #include <memory>
 #include <string>
+#include <type_traits>
 #include <utility>
 #include <vector>
 
+#include "absl/functional/any_invocable.h"
 #include "absl/strings/string_view.h"
 #include "absl/types/optional.h"
 #include "api/candidate.h"
+#include "api/rtc_error.h"
+#include "api/sequence_checker.h"
+#include "api/task_queue/task_queue_base.h"
 #include "api/transport/stun.h"
+#include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair.h"
+#include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h"
 #include "logging/rtc_event_log/ice_logger.h"
 #include "p2p/base/candidate_pair_interface.h"
 #include "p2p/base/connection_info.h"
 #include "p2p/base/p2p_transport_channel_ice_field_trials.h"
+#include "p2p/base/port_interface.h"
 #include "p2p/base/stun_request.h"
 #include "p2p/base/transport_description.h"
 #include "rtc_base/async_packet_socket.h"
@@ -32,6 +44,8 @@
 #include "rtc_base/numerics/event_based_exponential_moving_average.h"
 #include "rtc_base/rate_tracker.h"
 #include "rtc_base/system/rtc_export.h"
+#include "rtc_base/third_party/sigslot/sigslot.h"
+#include "rtc_base/thread_annotations.h"
 #include "rtc_base/weak_ptr.h"
 
 namespace cricket {
@@ -40,23 +54,9 @@
 // adding other flavors in the future.
 constexpr int kGoogPingVersion = 1;
 
-// Connection and Port has circular dependencies.
-// So we use forward declaration rather than include.
-class Port;
-
 // Forward declaration so that a ConnectionRequest can contain a Connection.
 class Connection;
 
-struct CandidatePair final : public CandidatePairInterface {
-  ~CandidatePair() override = default;
-
-  const Candidate& local_candidate() const override { return local; }
-  const Candidate& remote_candidate() const override { return remote; }
-
-  Candidate local;
-  Candidate remote;
-};
-
 // Represents a communication link between a port on the local client and a
 // port on the remote client.
 class RTC_EXPORT Connection : public CandidatePairInterface {
@@ -102,7 +102,7 @@
   bool writable() const;
   bool receiving() const;
 
-  const Port* port() const {
+  const PortInterface* port() const {
     RTC_DCHECK_RUN_ON(network_thread_);
     return port_.get();
   }
@@ -326,8 +326,8 @@
   void SendResponseMessage(const StunMessage& response);
 
   // An accessor for unit tests.
-  Port* PortForTest() { return port_.get(); }
-  const Port* PortForTest() const { return port_.get(); }
+  PortInterface* PortForTest() { return port_.get(); }
+  const PortInterface* PortForTest() const { return port_.get(); }
 
   std::unique_ptr<IceMessage> BuildPingRequestForTest() {
     RTC_DCHECK_RUN_ON(network_thread_);
@@ -364,7 +364,9 @@
   class ConnectionRequest;
 
   // Constructs a new connection to the given remote port.
-  Connection(rtc::WeakPtr<Port> port, size_t index, const Candidate& candidate);
+  Connection(rtc::WeakPtr<PortInterface> port,
+             size_t index,
+             const Candidate& candidate);
 
   // Called back when StunRequestManager has a stun packet to send
   void OnSendStunPacket(const void* data, size_t size, StunRequest* req);
@@ -393,7 +395,7 @@
   void set_connected(bool value);
 
   // The local port where this connection sends and receives packets.
-  Port* port() { return port_.get(); }
+  PortInterface* port() { return port_.get(); }
 
   // NOTE: A pointer to the network thread is held by `port_` so in theory we
   // shouldn't need to hold on to this pointer here, but rather defer to
@@ -402,7 +404,7 @@
   // TODO(tommi): This ^^^ should be fixed.
   webrtc::TaskQueueBase* const network_thread_;
   const uint32_t id_;
-  rtc::WeakPtr<Port> port_;
+  rtc::WeakPtr<PortInterface> port_;
   Candidate local_candidate_ RTC_GUARDED_BY(network_thread_);
   Candidate remote_candidate_;
 
@@ -515,7 +517,7 @@
 // ProxyConnection defers all the interesting work to the port.
 class ProxyConnection : public Connection {
  public:
-  ProxyConnection(rtc::WeakPtr<Port> port,
+  ProxyConnection(rtc::WeakPtr<PortInterface> port,
                   size_t index,
                   const Candidate& remote_candidate);
 
diff --git a/p2p/base/p2p_constants.h b/p2p/base/p2p_constants.h
index 3af00bb..c5a2b70 100644
--- a/p2p/base/p2p_constants.h
+++ b/p2p/base/p2p_constants.h
@@ -111,6 +111,19 @@
 // it.
 extern const int MIN_CONNECTION_LIFETIME;
 
+// The type preference MUST be an integer from 0 to 126 inclusive.
+// https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.2.1
+enum IcePriorityValue : uint8_t {
+  ICE_TYPE_PREFERENCE_RELAY_TLS = 0,
+  ICE_TYPE_PREFERENCE_RELAY_TCP = 1,
+  ICE_TYPE_PREFERENCE_RELAY_UDP = 2,
+  ICE_TYPE_PREFERENCE_PRFLX_TCP = 80,
+  ICE_TYPE_PREFERENCE_HOST_TCP = 90,
+  ICE_TYPE_PREFERENCE_SRFLX = 100,
+  ICE_TYPE_PREFERENCE_PRFLX = 110,
+  ICE_TYPE_PREFERENCE_HOST = 126
+};
+
 }  // namespace cricket
 
 #endif  // P2P_BASE_P2P_CONSTANTS_H_
diff --git a/p2p/base/p2p_transport_channel.cc b/p2p/base/p2p_transport_channel.cc
index 35d7f85..0bccb67 100644
--- a/p2p/base/p2p_transport_channel.cc
+++ b/p2p/base/p2p_transport_channel.cc
@@ -1693,7 +1693,7 @@
   regathering_controller_->Start();
 }
 
-bool P2PTransportChannel::IsPortPruned(const Port* port) const {
+bool P2PTransportChannel::IsPortPruned(const PortInterface* port) const {
   RTC_DCHECK_RUN_ON(network_thread_);
   return !absl::c_linear_search(ports_, port);
 }
diff --git a/p2p/base/p2p_transport_channel.h b/p2p/base/p2p_transport_channel.h
index 6413d2d..da7933f 100644
--- a/p2p/base/p2p_transport_channel.h
+++ b/p2p/base/p2p_transport_channel.h
@@ -370,7 +370,7 @@
   }
 
   // Indicates if the given local port has been pruned.
-  bool IsPortPruned(const Port* port) const;
+  bool IsPortPruned(const PortInterface* port) const;
 
   // Indicates if the given remote candidate has been pruned.
   bool IsRemoteCandidatePruned(const Candidate& cand) const;
diff --git a/p2p/base/port.cc b/p2p/base/port.cc
index 1289d17..aacfef0 100644
--- a/p2p/base/port.cc
+++ b/p2p/base/port.cc
@@ -10,31 +10,34 @@
 
 #include "p2p/base/port.h"
 
-#include <math.h>
-
-#include <algorithm>
 #include <cstddef>
+#include <cstdint>
 #include <memory>
 #include <utility>
 #include <vector>
 
-#include "absl/algorithm/container.h"
 #include "absl/memory/memory.h"
 #include "absl/strings/match.h"
 #include "absl/strings/string_view.h"
+#include "api/array_view.h"
+#include "api/rtc_error.h"
+#include "api/units/time_delta.h"
+#include "p2p/base/p2p_constants.h"
+#include "p2p/base/stun_request.h"
+#include "rtc_base/byte_buffer.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/crc32.h"
 #include "rtc_base/helpers.h"
+#include "rtc_base/ip_address.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/mdns_responder_interface.h"
-#include "rtc_base/message_digest.h"
+#include "rtc_base/net_helper.h"
 #include "rtc_base/network.h"
-#include "rtc_base/numerics/safe_minmax.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/string_encode.h"
 #include "rtc_base/string_utils.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/third_party/base64/base64.h"
+#include "rtc_base/time_utils.h"
 #include "rtc_base/trace_event.h"
 
 namespace cricket {
diff --git a/p2p/base/port.h b/p2p/base/port.h
index 7b44e53..e0fccd3 100644
--- a/p2p/base/port.h
+++ b/p2p/base/port.h
@@ -11,10 +11,15 @@
 #ifndef P2P_BASE_PORT_H_
 #define P2P_BASE_PORT_H_
 
+#include <stddef.h>
+#include <stdint.h>
+
+#include <functional>
 #include <map>
 #include <memory>
 #include <set>
 #include <string>
+#include <type_traits>
 #include <utility>
 #include <vector>
 
@@ -25,6 +30,7 @@
 #include "api/field_trials_view.h"
 #include "api/packet_socket_factory.h"
 #include "api/rtc_error.h"
+#include "api/sequence_checker.h"
 #include "api/task_queue/task_queue_base.h"
 #include "api/transport/field_trial_based_config.h"
 #include "api/transport/stun.h"
@@ -37,18 +43,22 @@
 #include "p2p/base/p2p_constants.h"
 #include "p2p/base/port_interface.h"
 #include "p2p/base/stun_request.h"
+#include "p2p/base/transport_description.h"
 #include "rtc_base/async_packet_socket.h"
 #include "rtc_base/callback_list.h"
 #include "rtc_base/checks.h"
+#include "rtc_base/dscp.h"
 #include "rtc_base/memory/always_valid_pointer.h"
 #include "rtc_base/net_helper.h"
 #include "rtc_base/network.h"
 #include "rtc_base/network/received_packet.h"
+#include "rtc_base/network/sent_packet.h"
 #include "rtc_base/proxy_info.h"
 #include "rtc_base/rate_tracker.h"
 #include "rtc_base/socket_address.h"
 #include "rtc_base/system/rtc_export.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
+#include "rtc_base/thread_annotations.h"
 #include "rtc_base/weak_ptr.h"
 
 namespace cricket {
@@ -59,19 +69,6 @@
 extern const char TCPTYPE_PASSIVE_STR[];
 extern const char TCPTYPE_SIMOPEN_STR[];
 
-// The type preference MUST be an integer from 0 to 126 inclusive.
-// https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.2.1
-enum IcePriorityValue : uint8_t {
-  ICE_TYPE_PREFERENCE_RELAY_TLS = 0,
-  ICE_TYPE_PREFERENCE_RELAY_TCP = 1,
-  ICE_TYPE_PREFERENCE_RELAY_UDP = 2,
-  ICE_TYPE_PREFERENCE_PRFLX_TCP = 80,
-  ICE_TYPE_PREFERENCE_HOST_TCP = 90,
-  ICE_TYPE_PREFERENCE_SRFLX = 100,
-  ICE_TYPE_PREFERENCE_PRFLX = 110,
-  ICE_TYPE_PREFERENCE_HOST = 126
-};
-
 enum class MdnsNameRegistrationStatus {
   // IP concealment with mDNS is not enabled or the name registration process is
   // not started yet.
@@ -227,13 +224,13 @@
   void CancelPendingTasks();
 
   // The thread on which this port performs its I/O.
-  webrtc::TaskQueueBase* thread() { return thread_; }
+  webrtc::TaskQueueBase* thread() override { return thread_; }
 
   // The factory used to create the sockets of this port.
-  rtc::PacketSocketFactory* socket_factory() const { return factory_; }
+  rtc::PacketSocketFactory* socket_factory() const override { return factory_; }
 
   // For debugging purposes.
-  const std::string& content_name() const { return content_name_; }
+  const std::string& content_name() const override { return content_name_; }
   void set_content_name(absl::string_view content_name) {
     content_name_ = std::string(content_name);
   }
@@ -241,7 +238,7 @@
   int component() const { return component_; }
   void set_component(int component) { component_ = component; }
 
-  bool send_retransmit_count_attribute() const {
+  bool send_retransmit_count_attribute() const override {
     return send_retransmit_count_attribute_;
   }
   void set_send_retransmit_count_attribute(bool enable) {
@@ -249,8 +246,10 @@
   }
 
   // Identifies the generation that this port was created in.
-  uint32_t generation() const { return generation_; }
-  void set_generation(uint32_t generation) { generation_ = generation; }
+  uint32_t generation() const override { return generation_; }
+  void set_generation(uint32_t generation) override {
+    generation_ = generation;
+  }
 
   const std::string& username_fragment() const;
   const std::string& password() const { return password_; }
@@ -296,11 +295,11 @@
   // defers the `delete` operation to when the call stack has been unwound.
   // Async may be needed when deleting a connection object from within a
   // callback.
-  void DestroyConnection(Connection* conn) {
+  void DestroyConnection(Connection* conn) override {
     DestroyConnectionInternal(conn, false);
   }
 
-  void DestroyConnectionAsync(Connection* conn) {
+  void DestroyConnectionAsync(Connection* conn) override {
     DestroyConnectionInternal(conn, true);
   }
 
@@ -330,8 +329,8 @@
     user_agent_ = std::string(user_agent);
     proxy_ = proxy;
   }
-  const std::string& user_agent() { return user_agent_; }
-  const rtc::ProxyInfo& proxy() { return proxy_; }
+  const std::string& user_agent() override { return user_agent_; }
+  const rtc::ProxyInfo& proxy() override { return proxy_; }
 
   void EnablePortPackets() override;
 
@@ -350,12 +349,13 @@
   // stun username attribute if present.
   bool ParseStunUsername(const StunMessage* stun_msg,
                          std::string* local_username,
-                         std::string* remote_username) const;
-  std::string CreateStunUsername(absl::string_view remote_username) const;
+                         std::string* remote_username) const override;
+  std::string CreateStunUsername(
+      absl::string_view remote_username) const override;
 
   bool MaybeIceRoleConflict(const rtc::SocketAddress& addr,
                             IceMessage* stun_msg,
-                            absl::string_view remote_ufrag);
+                            absl::string_view remote_ufrag) override;
 
   // Called when a packet has been sent to the socket.
   // This is made pure virtual to notify subclasses of Port that they MUST
@@ -368,9 +368,9 @@
   void OnReadyToSend();
 
   // Called when the Connection discovers a local peer reflexive candidate.
-  void AddPrflxCandidate(const Candidate& local);
+  void AddPrflxCandidate(const Candidate& local) override;
 
-  int16_t network_cost() const { return network_cost_; }
+  int16_t network_cost() const override { return network_cost_; }
 
   void GetStunStats(absl::optional<StunStats>* stats) override {}
 
@@ -380,13 +380,14 @@
   //   then the foundation will be different.  Two candidate pairs with
   //   the same foundation pairs are likely to have similar network
   //   characteristics. Foundations are used in the frozen algorithm.
-  std::string ComputeFoundation(absl::string_view type,
-                                absl::string_view protocol,
-                                absl::string_view relay_protocol,
-                                const rtc::SocketAddress& base_address);
+  std::string ComputeFoundation(
+      absl::string_view type,
+      absl::string_view protocol,
+      absl::string_view relay_protocol,
+      const rtc::SocketAddress& base_address) override;
 
  protected:
-  virtual void UpdateNetworkCost();
+  void UpdateNetworkCost() override;
 
   rtc::WeakPtr<Port> NewWeakPtr() { return weak_factory_.GetWeakPtr(); }
 
@@ -438,13 +439,13 @@
                       size_t size,
                       const rtc::SocketAddress& addr,
                       std::unique_ptr<IceMessage>* out_msg,
-                      std::string* out_username);
+                      std::string* out_username) override;
 
   // Checks if the address in addr is compatible with the port's ip.
   bool IsCompatibleAddress(const rtc::SocketAddress& addr);
 
   // Returns DSCP value packets generated by the port itself should use.
-  virtual rtc::DiffServCodePoint StunDscpValue() const;
+  rtc::DiffServCodePoint StunDscpValue() const override;
 
   // Extra work to be done in subclasses when a connection is destroyed.
   virtual void HandleConnectionDestroyed(Connection* conn) {}
@@ -528,7 +529,6 @@
                              absl::string_view type,
                              bool is_final) RTC_RUN_ON(thread_);
 
-  friend class Connection;
   webrtc::CallbackList<PortInterface*> port_destroyed_callback_list_;
 };
 
diff --git a/p2p/base/port_allocator.cc b/p2p/base/port_allocator.cc
index 16f5afe..3745717 100644
--- a/p2p/base/port_allocator.cc
+++ b/p2p/base/port_allocator.cc
@@ -11,13 +11,13 @@
 #include "p2p/base/port_allocator.h"
 
 #include <iterator>
+#include <optional>
 #include <set>
 #include <utility>
 
 #include "absl/strings/string_view.h"
 #include "p2p/base/ice_credentials_iterator.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/logging.h"
 
 namespace cricket {
 
diff --git a/p2p/base/port_allocator.h b/p2p/base/port_allocator.h
index 11462f7..b8cffca 100644
--- a/p2p/base/port_allocator.h
+++ b/p2p/base/port_allocator.h
@@ -11,18 +11,26 @@
 #ifndef P2P_BASE_PORT_ALLOCATOR_H_
 #define P2P_BASE_PORT_ALLOCATOR_H_
 
+#include <stdint.h>
+
 #include <deque>
 #include <memory>
 #include <string>
 #include <vector>
 
 #include "absl/strings/string_view.h"
+#include "absl/types/optional.h"
+#include "api/candidate.h"
 #include "api/sequence_checker.h"
 #include "api/transport/enums.h"
 #include "p2p/base/port.h"
 #include "p2p/base/port_interface.h"
+#include "p2p/base/transport_description.h"
+#include "rtc_base/checks.h"
 #include "rtc_base/helpers.h"
+#include "rtc_base/network.h"
 #include "rtc_base/proxy_info.h"
+#include "rtc_base/socket_address.h"
 #include "rtc_base/ssl_certificate.h"
 #include "rtc_base/system/rtc_export.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
diff --git a/p2p/base/port_interface.h b/p2p/base/port_interface.h
index fe4b204..fb8023b 100644
--- a/p2p/base/port_interface.h
+++ b/p2p/base/port_interface.h
@@ -11,6 +11,7 @@
 #ifndef P2P_BASE_PORT_INTERFACE_H_
 #define P2P_BASE_PORT_INTERFACE_H_
 
+#include <memory>
 #include <string>
 #include <utility>
 #include <vector>
@@ -18,9 +19,11 @@
 #include "absl/strings/string_view.h"
 #include "absl/types/optional.h"
 #include "api/candidate.h"
+#include "api/packet_socket_factory.h"
 #include "p2p/base/transport_description.h"
 #include "rtc_base/async_packet_socket.h"
 #include "rtc_base/callback_list.h"
+#include "rtc_base/proxy_info.h"
 #include "rtc_base/socket_address.h"
 
 namespace rtc {
@@ -137,8 +140,80 @@
 
   virtual void GetStunStats(absl::optional<StunStats>* stats) = 0;
 
+  // Removes and deletes a connection object. `DestroyConnection` will
+  // delete the connection object directly whereas `DestroyConnectionAsync`
+  // defers the `delete` operation to when the call stack has been unwound.
+  // Async may be needed when deleting a connection object from within a
+  // callback.
+  virtual void DestroyConnection(Connection* conn) = 0;
+
+  virtual void DestroyConnectionAsync(Connection* conn) = 0;
+
+  // The thread on which this port performs its I/O.
+  virtual webrtc::TaskQueueBase* thread() = 0;
+
+  // The factory used to create the sockets of this port.
+  virtual rtc::PacketSocketFactory* socket_factory() const = 0;
+  virtual const std::string& user_agent() = 0;
+  virtual const rtc::ProxyInfo& proxy() = 0;
+
+  // Identifies the generation that this port was created in.
+  virtual uint32_t generation() const = 0;
+  virtual void set_generation(uint32_t generation) = 0;
+  virtual bool send_retransmit_count_attribute() const = 0;
+  // For debugging purposes.
+  virtual const std::string& content_name() const = 0;
+
+  // Called when the Connection discovers a local peer reflexive candidate.
+  virtual void AddPrflxCandidate(const Candidate& local) = 0;
+
+  // Foundation:  An arbitrary string that is the same for two candidates
+  //   that have the same type, base IP address, protocol (UDP, TCP,
+  //   etc.), and STUN or TURN server.  If any of these are different,
+  //   then the foundation will be different.  Two candidate pairs with
+  //   the same foundation pairs are likely to have similar network
+  //   characteristics. Foundations are used in the frozen algorithm.
+  virtual std::string ComputeFoundation(
+      absl::string_view type,
+      absl::string_view protocol,
+      absl::string_view relay_protocol,
+      const rtc::SocketAddress& base_address) = 0;
+
  protected:
   PortInterface();
+  virtual void UpdateNetworkCost() = 0;
+
+  // Returns DSCP value packets generated by the port itself should use.
+  virtual rtc::DiffServCodePoint StunDscpValue() const = 0;
+
+  // If the given data comprises a complete and correct STUN message then the
+  // return value is true, otherwise false. If the message username corresponds
+  // with this port's username fragment, msg will contain the parsed STUN
+  // message.  Otherwise, the function may send a STUN response internally.
+  // remote_username contains the remote fragment of the STUN username.
+  virtual bool GetStunMessage(const char* data,
+                              size_t size,
+                              const rtc::SocketAddress& addr,
+                              std::unique_ptr<IceMessage>* out_msg,
+                              std::string* out_username) = 0;
+
+  // This method will return local and remote username fragements from the
+  // stun username attribute if present.
+  virtual bool ParseStunUsername(const StunMessage* stun_msg,
+                                 std::string* local_username,
+                                 std::string* remote_username) const = 0;
+  virtual std::string CreateStunUsername(
+      absl::string_view remote_username) const = 0;
+
+  virtual bool MaybeIceRoleConflict(const rtc::SocketAddress& addr,
+                                    IceMessage* stun_msg,
+                                    absl::string_view remote_ufrag) = 0;
+
+  virtual int16_t network_cost() const = 0;
+
+  // Connection and Port are entangled; functions exposed to Port only
+  // should not be public.
+  friend class Connection;
 };
 
 }  // namespace cricket
diff --git a/pc/BUILD.gn b/pc/BUILD.gn
index 1f814cb..a426e9b 100644
--- a/pc/BUILD.gn
+++ b/pc/BUILD.gn
@@ -301,7 +301,7 @@
     "../api/transport:enums",
     "../api/transport:sctp_transport_factory_interface",
     "../media:rtc_data_sctp_transport_internal",
-    "../p2p:connection_and_port",
+    "../p2p:connection",
     "../p2p:ice_transport_internal",
     "../p2p:p2p_constants",
     "../p2p:p2p_transport_channel",
@@ -654,7 +654,7 @@
   deps = [
     "../api:dtls_transport_interface",
     "../api:libjingle_peerconnection_api",
-    "../p2p:connection_and_port",
+    "../p2p:connection",
     "../p2p:ice_transport_internal",
     "../p2p:rtc_p2p",
     "../rtc_base:ssl",
@@ -932,7 +932,7 @@
     "../modules/audio_device",
     "../modules/audio_processing:audio_processing_statistics",
     "../modules/rtp_rtcp:rtp_rtcp_format",
-    "../p2p:connection_and_port",
+    "../p2p:connection",
     "../p2p:connection_info",
     "../p2p:ice_transport_internal",
     "../p2p:p2p_constants",
@@ -1029,7 +1029,7 @@
     "../media:rid_description",
     "../media:rtc_media_base",
     "../media:stream_params",
-    "../p2p:connection_and_port",
+    "../p2p:connection",
     "../p2p:ice_transport_internal",
     "../p2p:p2p_constants",
     "../p2p:p2p_transport_channel",
@@ -1140,7 +1140,7 @@
     "../media:stream_params",
     "../modules/rtp_rtcp:rtp_rtcp_format",
     "../p2p:basic_async_resolver_factory",
-    "../p2p:connection_and_port",
+    "../p2p:connection",
     "../p2p:connection_info",
     "../p2p:ice_transport_internal",
     "../p2p:p2p_constants",
@@ -1237,7 +1237,7 @@
     "../media:media_channel",
     "../media:rtc_media_base",
     "../modules/audio_processing:audio_processing_statistics",
-    "../p2p:connection_and_port",
+    "../p2p:connection",
     "../p2p:connection_info",
     "../p2p:ice_transport_internal",
     "../p2p:p2p_constants",
@@ -1315,7 +1315,7 @@
     "../media:rtp_utils",
     "../media:stream_params",
     "../p2p:candidate_pair_interface",
-    "../p2p:connection_and_port",
+    "../p2p:connection",
     "../p2p:ice_transport_internal",
     "../p2p:p2p_constants",
     "../p2p:port_interface",
@@ -1382,7 +1382,7 @@
   deps = [
     "../api:libjingle_peerconnection_api",
     "../api:rtc_error",
-    "../p2p:connection_and_port",
+    "../p2p:connection",
     "../p2p:port_interface",
     "../p2p:rtc_p2p",
     "../rtc_base:checks",
@@ -1446,7 +1446,7 @@
     "../call:rtp_interfaces",
     "../call:rtp_sender",
     "../media:rtc_media_base",
-    "../p2p:connection_and_port",
+    "../p2p:connection",
     "../p2p:rtc_p2p",
     "../pc:audio_track",
     "../pc:connection_context",
@@ -2167,7 +2167,7 @@
       "../media:rtc_media_tests_utils",
       "../modules/audio_device:audio_device_api",
       "../modules/audio_processing:api",
-      "../p2p:connection_and_port",
+      "../p2p:connection",
       "../p2p:p2p_test_utils",
       "../p2p:port_interface",
       "../p2p:rtc_p2p",
@@ -2220,7 +2220,7 @@
       "../api:libjingle_peerconnection_api",
       "../api:scoped_refptr",
       "../api/units:time_delta",
-      "../p2p:connection_and_port",
+      "../p2p:connection",
       "../p2p:p2p_server_utils",
       "../p2p:p2p_test_utils",
       "../p2p:port_interface",
@@ -2403,7 +2403,7 @@
       "../modules/audio_device:audio_device_api",
       "../modules/audio_processing:audio_processing_statistics",
       "../modules/rtp_rtcp:rtp_rtcp_format",
-      "../p2p:connection_and_port",
+      "../p2p:connection",
       "../p2p:connection_info",
       "../p2p:fake_port_allocator",
       "../p2p:ice_transport_internal",
@@ -2619,7 +2619,7 @@
       "../modules/audio_processing:audio_processing_statistics",
       "../modules/audio_processing:audioproc_test_utils",
       "../modules/rtp_rtcp:rtp_rtcp_format",
-      "../p2p:connection_and_port",
+      "../p2p:connection",
       "../p2p:fake_ice_transport",
       "../p2p:fake_port_allocator",
       "../p2p:ice_transport_internal",
@@ -2773,7 +2773,7 @@
       "../modules/audio_processing",
       "../modules/audio_processing:api",
       "../modules/rtp_rtcp:rtp_rtcp_format",
-      "../p2p:connection_and_port",
+      "../p2p:connection",
       "../p2p:fake_port_allocator",
       "../p2p:p2p_test_utils",
       "../p2p:rtc_p2p",