From c09cc4f961c22e2a53cb3a60f9afdd7fd67a25d9 Mon Sep 17 00:00:00 2001 From: Per K Date: Mon, 20 Nov 2023 12:21:34 +0100 Subject: [PATCH] rtc::Port use Socket::RegisterReceivedPacketCallback Change Stun, Turn, TCP and UDP ports to use Socket::RegisterReceivedPacketCallback and be notified of received packets using the rtc::ReceivedPacket class. Change-Id: I7eba2ffdd83ae6f6181f765474aab7c57ff25507 Bug: webrtc:15368, webrtc:11943 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/327941 Commit-Queue: Per Kjellander Reviewed-by: Jonas Oreland Cr-Commit-Position: refs/heads/main@{#41195} --- p2p/base/port.cc | 15 ++++++----- p2p/base/port.h | 23 +++++++++++------ p2p/base/stun_port.cc | 34 ++++++++++++------------- p2p/base/stun_port.h | 11 +++----- p2p/base/stun_port_unittest.cc | 22 ++++++++-------- p2p/base/tcp_port.cc | 30 +++++++++++----------- p2p/base/tcp_port.h | 11 +++----- p2p/base/turn_port.cc | 40 +++++++++++++++++------------- p2p/base/turn_port.h | 11 +++----- p2p/base/turn_port_unittest.cc | 25 +++++++++++-------- p2p/client/basic_port_allocator.cc | 22 ++++++++-------- p2p/client/basic_port_allocator.h | 8 +++--- 12 files changed, 126 insertions(+), 126 deletions(-) diff --git a/p2p/base/port.cc b/p2p/base/port.cc index afd998c3ab..ff11481138 100644 --- a/p2p/base/port.cc +++ b/p2p/base/port.cc @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -31,6 +32,7 @@ #include "rtc_base/message_digest.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" @@ -359,10 +361,10 @@ void Port::AddOrReplaceConnection(Connection* conn) { } } -void Port::OnReadPacket(const char* data, - size_t size, - const rtc::SocketAddress& addr, - ProtocolType proto) { +void Port::OnReadPacket(const rtc::ReceivedPacket& packet, ProtocolType proto) { + const char* data = reinterpret_cast(packet.payload().data()); + size_t size = packet.payload().size(); + const rtc::SocketAddress& addr = packet.source_address(); // If the user has enabled port packets, just hand this over. if (enable_port_packets_) { SignalReadPacket(this, data, size, addr); @@ -725,10 +727,7 @@ std::string Port::CreateStunUsername(absl::string_view remote_username) const { } bool Port::HandleIncomingPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - int64_t packet_time_us) { + const rtc::ReceivedPacket& packet) { RTC_DCHECK_NOTREACHED(); return false; } diff --git a/p2p/base/port.h b/p2p/base/port.h index 78a4f91a93..796e1e1d5b 100644 --- a/p2p/base/port.h +++ b/p2p/base/port.h @@ -43,6 +43,7 @@ #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/proxy_info.h" #include "rtc_base/rate_tracker.h" #include "rtc_base/socket_address.h" @@ -313,10 +314,7 @@ class RTC_EXPORT Port : public PortInterface, public sigslot::has_slots<> { // port implemented this method. // TODO(mallinath) - Make it pure virtual. virtual bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - int64_t packet_time_us); + const rtc::ReceivedPacket& packet); // Shall the port handle packet from this `remote_addr`. // This method is overridden by TurnPort. @@ -422,10 +420,19 @@ class RTC_EXPORT Port : public PortInterface, public sigslot::has_slots<> { // Called when a packet is received from an unknown address that is not // currently a connection. If this is an authenticated STUN binding request, // then we will signal the client. - void OnReadPacket(const char* data, - size_t size, - const rtc::SocketAddress& addr, - ProtocolType proto); + void OnReadPacket(const rtc::ReceivedPacket& packet, ProtocolType proto); + + [[deprecated( + "Use OnReadPacket(const rtc::ReceivedPacket& packet, ProtocolType " + "proto)")]] void + OnReadPacket(const char* data, + size_t size, + const rtc::SocketAddress& addr, + ProtocolType proto) { + OnReadPacket(rtc::ReceivedPacket::CreateFromLegacy( + data, size, /*packet_time_us = */ -1, addr), + proto); + } // If the given data comprises a complete and correct STUN message then the // return value is true, otherwise false. If the message username corresponds diff --git a/p2p/base/stun_port.cc b/p2p/base/stun_port.cc index 44776d701b..c64d19282e 100644 --- a/p2p/base/stun_port.cc +++ b/p2p/base/stun_port.cc @@ -25,6 +25,7 @@ #include "rtc_base/helpers.h" #include "rtc_base/ip_address.h" #include "rtc_base/logging.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/strings/string_builder.h" namespace cricket { @@ -222,7 +223,10 @@ bool UDPPort::Init() { RTC_LOG(LS_WARNING) << ToString() << ": UDP socket creation failed"; return false; } - socket_->SignalReadPacket.connect(this, &UDPPort::OnReadPacket); + socket_->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + OnReadPacket(socket, packet); + }); } socket_->SignalSentPacket.connect(this, &UDPPort::OnSentPacket); socket_->SignalReadyToSend.connect(this, &UDPPort::OnReadyToSend); @@ -342,12 +346,9 @@ int UDPPort::GetError() { } bool UDPPort::HandleIncomingPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - int64_t packet_time_us) { + const rtc::ReceivedPacket& packet) { // All packets given to UDP port will be consumed. - OnReadPacket(socket, data, size, remote_addr, packet_time_us); + OnReadPacket(socket, packet); return true; } @@ -389,27 +390,26 @@ void UDPPort::PostAddAddress(bool is_final) { } void UDPPort::OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us) { + const rtc::ReceivedPacket& packet) { RTC_DCHECK(socket == socket_); - RTC_DCHECK(!remote_addr.IsUnresolvedIP()); + RTC_DCHECK(!packet.source_address().IsUnresolvedIP()); // Look for a response from the STUN server. // Even if the response doesn't match one of our outstanding requests, we // will eat it because it might be a response to a retransmitted packet, and // we already cleared the request when we got the first response. - if (server_addresses_.find(remote_addr) != server_addresses_.end()) { - request_manager_.CheckResponse(data, size); + if (server_addresses_.find(packet.source_address()) != + server_addresses_.end()) { + request_manager_.CheckResponse( + reinterpret_cast(packet.payload().data()), + packet.payload().size()); return; } - if (Connection* conn = GetConnection(remote_addr)) { - conn->OnReadPacket( - rtc::ReceivedPacket::CreateFromLegacy(data, size, packet_time_us)); + if (Connection* conn = GetConnection(packet.source_address())) { + conn->OnReadPacket(packet); } else { - Port::OnReadPacket(data, size, remote_addr, PROTO_UDP); + Port::OnReadPacket(packet, PROTO_UDP); } } diff --git a/p2p/base/stun_port.h b/p2p/base/stun_port.h index 3df725eaf9..a28983b040 100644 --- a/p2p/base/stun_port.h +++ b/p2p/base/stun_port.h @@ -22,6 +22,7 @@ #include "p2p/base/port.h" #include "p2p/base/stun_request.h" #include "rtc_base/async_packet_socket.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/system/rtc_export.h" namespace cricket { @@ -97,10 +98,7 @@ class RTC_EXPORT UDPPort : public Port { int GetError() override; bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - int64_t packet_time_us) override; + const rtc::ReceivedPacket& packet) override; bool SupportsProtocol(absl::string_view protocol) const override; ProtocolType GetProtocol() const override; @@ -158,10 +156,7 @@ class RTC_EXPORT UDPPort : public Port { void PostAddAddress(bool is_final) override; void OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us); + const rtc::ReceivedPacket& packet); void OnSentPacket(rtc::AsyncPacketSocket* socket, const rtc::SentPacket& sent_packet) override; diff --git a/p2p/base/stun_port_unittest.cc b/p2p/base/stun_port_unittest.cc index bf51151536..5b10618337 100644 --- a/p2p/base/stun_port_unittest.cc +++ b/p2p/base/stun_port_unittest.cc @@ -16,8 +16,10 @@ #include "p2p/base/basic_packet_socket_factory.h" #include "p2p/base/mock_dns_resolving_packet_socket_factory.h" #include "p2p/base/test_stun_server.h" +#include "rtc_base/async_packet_socket.h" #include "rtc_base/gunit.h" #include "rtc_base/helpers.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/socket_address.h" #include "rtc_base/ssl_adapter.h" #include "rtc_base/virtual_socket_server.h" @@ -160,7 +162,10 @@ class StunPortTestBase : public ::testing::Test, public sigslot::has_slots<> { rtc::SocketAddress(kLocalAddr.ipaddr(), 0), 0, 0)); } ASSERT_TRUE(socket_ != NULL); - socket_->SignalReadPacket.connect(this, &StunPortTestBase::OnReadPacket); + socket_->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + OnReadPacket(socket, packet); + }); stun_port_ = cricket::UDPPort::Create( rtc::Thread::Current(), socket_factory(), &network_, socket_.get(), rtc::CreateRandomString(16), rtc::CreateRandomString(22), false, @@ -178,18 +183,15 @@ class StunPortTestBase : public ::testing::Test, public sigslot::has_slots<> { void PrepareAddress() { stun_port_->PrepareAddress(); } void OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& /* packet_time_us */) { - stun_port_->HandleIncomingPacket(socket, data, size, remote_addr, - /* packet_time_us */ -1); + const rtc::ReceivedPacket& packet) { + stun_port_->HandleIncomingPacket(socket, packet); } void SendData(const char* data, size_t len) { - stun_port_->HandleIncomingPacket(socket_.get(), data, len, - rtc::SocketAddress("22.22.22.22", 0), - /* packet_time_us */ -1); + stun_port_->HandleIncomingPacket(socket_.get(), + rtc::ReceivedPacket::CreateFromLegacy( + data, len, /* packet_time_us */ -1, + rtc::SocketAddress("22.22.22.22", 0))); } void EnableMdnsObfuscation() { diff --git a/p2p/base/tcp_port.cc b/p2p/base/tcp_port.cc index edf0ffdabe..fc5d54bd9f 100644 --- a/p2p/base/tcp_port.cc +++ b/p2p/base/tcp_port.cc @@ -81,6 +81,7 @@ #include "rtc_base/ip_address.h" #include "rtc_base/logging.h" #include "rtc_base/net_helper.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/rate_tracker.h" namespace cricket { @@ -159,7 +160,7 @@ Connection* TCPPort::CreateConnection(const Candidate& address, // Incoming connection; we already created a socket and connected signals, // so we need to hand off the "read packet" responsibility to // TCPConnection. - socket->SignalReadPacket.disconnect(this); + socket->DeregisterReceivedPacketCallback(); conn = new TCPConnection(NewWeakPtr(), address, socket); } else { // Outgoing connection, which will create a new socket for which we still @@ -288,7 +289,10 @@ void TCPPort::OnNewConnection(rtc::AsyncListenSocket* socket, Incoming incoming; incoming.addr = new_socket->GetRemoteAddress(); incoming.socket = new_socket; - incoming.socket->SignalReadPacket.connect(this, &TCPPort::OnReadPacket); + incoming.socket->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + OnReadPacket(socket, packet); + }); incoming.socket->SignalReadyToSend.connect(this, &TCPPort::OnReadyToSend); incoming.socket->SignalSentPacket.connect(this, &TCPPort::OnSentPacket); @@ -326,11 +330,8 @@ rtc::AsyncPacketSocket* TCPPort::GetIncoming(const rtc::SocketAddress& addr, } void TCPPort::OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us) { - Port::OnReadPacket(data, size, remote_addr, PROTO_TCP); + const rtc::ReceivedPacket& packet) { + Port::OnReadPacket(packet, PROTO_TCP); } void TCPPort::OnSentPacket(rtc::AsyncPacketSocket* socket, @@ -559,14 +560,10 @@ void TCPConnection::MaybeReconnect() { } void TCPConnection::OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us) { + const rtc::ReceivedPacket& packet) { RTC_DCHECK_RUN_ON(network_thread()); RTC_DCHECK_EQ(socket, socket_.get()); - Connection::OnReadPacket( - rtc::ReceivedPacket::CreateFromLegacy(data, size, packet_time_us)); + Connection::OnReadPacket(packet); } void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) { @@ -624,7 +621,10 @@ void TCPConnection::ConnectSocketSignals(rtc::AsyncPacketSocket* socket) { if (outgoing_) { socket->SignalConnect.connect(this, &TCPConnection::OnConnect); } - socket->SignalReadPacket.connect(this, &TCPConnection::OnReadPacket); + socket->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + OnReadPacket(socket, packet); + }); socket->SignalReadyToSend.connect(this, &TCPConnection::OnReadyToSend); socket->SubscribeCloseEvent(this, [this, safety = network_safety_.flag()]( rtc::AsyncPacketSocket* s, int err) { @@ -637,7 +637,7 @@ void TCPConnection::DisconnectSocketSignals(rtc::AsyncPacketSocket* socket) { if (outgoing_) { socket->SignalConnect.disconnect(this); } - socket->SignalReadPacket.disconnect(this); + socket->DeregisterReceivedPacketCallback(); socket->SignalReadyToSend.disconnect(this); socket->UnsubscribeCloseEvent(this); } diff --git a/p2p/base/tcp_port.h b/p2p/base/tcp_port.h index a1bbaa9f35..bd7ed4c110 100644 --- a/p2p/base/tcp_port.h +++ b/p2p/base/tcp_port.h @@ -22,6 +22,7 @@ #include "p2p/base/port.h" #include "rtc_base/async_packet_socket.h" #include "rtc_base/containers/flat_map.h" +#include "rtc_base/network/received_packet.h" namespace cricket { @@ -101,10 +102,7 @@ class TCPPort : public Port { // Receives packet signal from the local TCP Socket. void OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us); + const rtc::ReceivedPacket& packet); void OnSentPacket(rtc::AsyncPacketSocket* socket, const rtc::SentPacket& sent_packet) override; @@ -170,10 +168,7 @@ class TCPConnection : public Connection, public sigslot::has_slots<> { void OnConnect(rtc::AsyncPacketSocket* socket); void OnClose(rtc::AsyncPacketSocket* socket, int error); void OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us); + const rtc::ReceivedPacket& packet); void OnReadyToSend(rtc::AsyncPacketSocket* socket); void OnDestroyed(Connection* c); diff --git a/p2p/base/turn_port.cc b/p2p/base/turn_port.cc index 042727ff67..51529c8159 100644 --- a/p2p/base/turn_port.cc +++ b/p2p/base/turn_port.cc @@ -10,6 +10,7 @@ #include "p2p/base/turn_port.h" +#include #include #include #include @@ -29,6 +30,7 @@ #include "rtc_base/experiments/field_trial_parser.h" #include "rtc_base/logging.h" #include "rtc_base/net_helpers.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/socket_address.h" #include "rtc_base/strings/string_builder.h" @@ -435,7 +437,10 @@ bool TurnPort::CreateTurnClientSocket() { if (!SharedSocket()) { // If socket is shared, AllocationSequence will receive the packet. - socket_->SignalReadPacket.connect(this, &TurnPort::OnReadPacket); + socket_->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + OnReadPacket(socket, packet); + }); } socket_->SignalReadyToSend.connect(this, &TurnPort::OnReadyToSend); @@ -679,10 +684,7 @@ void TurnPort::SendBindingErrorResponse(StunMessage* message, } bool TurnPort::HandleIncomingPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - int64_t packet_time_us) { + const rtc::ReceivedPacket& packet) { if (socket != socket_) { // The packet was received on a shared socket after we've allocated a new // socket for this TURN port. @@ -692,16 +694,17 @@ bool TurnPort::HandleIncomingPacket(rtc::AsyncPacketSocket* socket, // This is to guard against a STUN response from previous server after // alternative server redirection. TODO(guoweis): add a unit test for this // race condition. - if (remote_addr != server_address_.address) { + if (packet.source_address() != server_address_.address) { RTC_LOG(LS_WARNING) << ToString() << ": Discarding TURN message from unknown address: " - << remote_addr.ToSensitiveNameAndAddressString() << " server_address_: " + << packet.source_address().ToSensitiveNameAndAddressString() + << " server_address_: " << server_address_.address.ToSensitiveNameAndAddressString(); return false; } // The message must be at least the size of a channel header. - if (size < TURN_CHANNEL_HEADER_SIZE) { + if (packet.payload().size() < TURN_CHANNEL_HEADER_SIZE) { RTC_LOG(LS_WARNING) << ToString() << ": Received TURN message that was too short"; return false; @@ -714,10 +717,15 @@ bool TurnPort::HandleIncomingPacket(rtc::AsyncPacketSocket* socket, return false; } + const char* data = reinterpret_cast(packet.payload().data()); + int size = packet.payload().size(); + int64_t packet_time_us = + packet.arrival_time() ? packet.arrival_time()->us() : -1; + // Check the message type, to see if is a Channel Data message. // The message will either be channel data, a TURN data indication, or // a response to a previous request. - uint16_t msg_type = rtc::GetBE16(data); + uint16_t msg_type = rtc::GetBE16(packet.payload().data()); if (IsTurnChannelData(msg_type)) { HandleChannelData(msg_type, data, size, packet_time_us); return true; @@ -742,11 +750,8 @@ bool TurnPort::HandleIncomingPacket(rtc::AsyncPacketSocket* socket, } void TurnPort::OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us) { - HandleIncomingPacket(socket, data, size, remote_addr, packet_time_us); + const rtc::ReceivedPacket& packet) { + HandleIncomingPacket(socket, packet); } void TurnPort::OnSentPacket(rtc::AsyncPacketSocket* socket, @@ -1073,11 +1078,12 @@ void TurnPort::DispatchPacket(const char* data, const rtc::SocketAddress& remote_addr, ProtocolType proto, int64_t packet_time_us) { + rtc::ReceivedPacket packet = rtc::ReceivedPacket::CreateFromLegacy( + data, size, packet_time_us, remote_addr); if (Connection* conn = GetConnection(remote_addr)) { - conn->OnReadPacket( - rtc::ReceivedPacket::CreateFromLegacy(data, size, packet_time_us)); + conn->OnReadPacket(packet); } else { - Port::OnReadPacket(data, size, remote_addr, proto); + Port::OnReadPacket(packet, proto); } } diff --git a/p2p/base/turn_port.h b/p2p/base/turn_port.h index 8fa4607e51..686edaf595 100644 --- a/p2p/base/turn_port.h +++ b/p2p/base/turn_port.h @@ -27,6 +27,7 @@ #include "p2p/base/port.h" #include "p2p/client/basic_port_allocator.h" #include "rtc_base/async_packet_socket.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/ssl_certificate.h" namespace webrtc { @@ -144,10 +145,7 @@ class TurnPort : public Port { int GetError() override; bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - int64_t packet_time_us) override; + const rtc::ReceivedPacket& packet) override; bool CanHandleIncomingPacketsFrom( const rtc::SocketAddress& addr) const override; @@ -159,10 +157,7 @@ class TurnPort : public Port { absl::string_view reason) override; virtual void OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us); + const rtc::ReceivedPacket& packet); void OnSentPacket(rtc::AsyncPacketSocket* socket, const rtc::SentPacket& sent_packet) override; diff --git a/p2p/base/turn_port_unittest.cc b/p2p/base/turn_port_unittest.cc index 55706e142b..5c11ea3ee7 100644 --- a/p2p/base/turn_port_unittest.cc +++ b/p2p/base/turn_port_unittest.cc @@ -7,6 +7,10 @@ * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ +#include + +#include "api/array_view.h" +#include "rtc_base/network/received_packet.h" #if defined(WEBRTC_POSIX) #include @@ -218,12 +222,8 @@ class TurnPortTest : public ::testing::Test, } void OnUdpPortComplete(Port* port) { udp_ready_ = true; } void OnSocketReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us) { - turn_port_->HandleIncomingPacket(socket, data, size, remote_addr, - packet_time_us); + const rtc::ReceivedPacket& packet) { + turn_port_->HandleIncomingPacket(socket, packet); } void OnTurnPortDestroyed(PortInterface* port) { turn_port_destroyed_ = true; } @@ -323,8 +323,11 @@ class TurnPortTest : public ::testing::Test, socket_.reset(socket_factory()->CreateUdpSocket( rtc::SocketAddress(kLocalAddr1.ipaddr(), 0), 0, 0)); ASSERT_TRUE(socket_ != NULL); - socket_->SignalReadPacket.connect(this, - &TurnPortTest::OnSocketReadPacket); + socket_->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, + const rtc::ReceivedPacket& packet) { + OnSocketReadPacket(socket, packet); + }); } RelayServerConfig config; @@ -1193,8 +1196,10 @@ TEST_F(TurnPortTest, TestTurnAllocateMismatch) { // Verify that all packets received from the shared socket are ignored. std::string test_packet = "Test packet"; EXPECT_FALSE(turn_port_->HandleIncomingPacket( - socket_.get(), test_packet.data(), test_packet.size(), - rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0), rtc::TimeMicros())); + socket_.get(), + rtc::ReceivedPacket::CreateFromLegacy( + test_packet.data(), test_packet.size(), rtc::TimeMicros(), + rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)))); } // Tests that a shared-socket-TurnPort creates its own socket after diff --git a/p2p/client/basic_port_allocator.cc b/p2p/client/basic_port_allocator.cc index b6cbf1fff9..e8255f1fd5 100644 --- a/p2p/client/basic_port_allocator.cc +++ b/p2p/client/basic_port_allocator.cc @@ -1308,8 +1308,11 @@ void AllocationSequence::Init() { rtc::SocketAddress(network_->GetBestIP(), 0), session_->allocator()->min_port(), session_->allocator()->max_port())); if (udp_socket_) { - udp_socket_->SignalReadPacket.connect(this, - &AllocationSequence::OnReadPacket); + udp_socket_->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, + const rtc::ReceivedPacket& packet) { + OnReadPacket(socket, packet); + }); } // Continuing if `udp_socket_` is NULL, as local TCP and RelayPort using TCP // are next available options to setup a communication channel. @@ -1668,10 +1671,7 @@ void AllocationSequence::CreateTurnPort(const RelayServerConfig& config, } void AllocationSequence::OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us) { + const rtc::ReceivedPacket& packet) { RTC_DCHECK(socket == udp_socket_.get()); bool turn_port_found = false; @@ -1683,9 +1683,8 @@ void AllocationSequence::OnReadPacket(rtc::AsyncPacketSocket* socket, // the message type. The TurnPort will just ignore the message since it will // not find any request by transaction ID. for (auto* port : relay_ports_) { - if (port->CanHandleIncomingPacketsFrom(remote_addr)) { - if (port->HandleIncomingPacket(socket, data, size, remote_addr, - packet_time_us)) { + if (port->CanHandleIncomingPacketsFrom(packet.source_address())) { + if (port->HandleIncomingPacket(socket, packet)) { return; } turn_port_found = true; @@ -1698,10 +1697,9 @@ void AllocationSequence::OnReadPacket(rtc::AsyncPacketSocket* socket, // Pass the packet to the UdpPort if there is no matching TurnPort, or if // the TURN server is also a STUN server. if (!turn_port_found || - stun_servers.find(remote_addr) != stun_servers.end()) { + stun_servers.find(packet.source_address()) != stun_servers.end()) { RTC_DCHECK(udp_port_->SharedSocket()); - udp_port_->HandleIncomingPacket(socket, data, size, remote_addr, - packet_time_us); + udp_port_->HandleIncomingPacket(socket, packet); } } } diff --git a/p2p/client/basic_port_allocator.h b/p2p/client/basic_port_allocator.h index 95bbdb183e..643904ab27 100644 --- a/p2p/client/basic_port_allocator.h +++ b/p2p/client/basic_port_allocator.h @@ -25,6 +25,7 @@ #include "rtc_base/checks.h" #include "rtc_base/memory/always_valid_pointer.h" #include "rtc_base/network.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/system/rtc_export.h" #include "rtc_base/thread.h" #include "rtc_base/thread_annotations.h" @@ -330,7 +331,7 @@ class TurnPort; // Performs the allocation of ports, in a sequenced (timed) manner, for a given // network and IP address. // This class is thread-compatible. -class AllocationSequence : public sigslot::has_slots<> { +class AllocationSequence { public: enum State { kInit, // Initial state. @@ -386,10 +387,7 @@ class AllocationSequence : public sigslot::has_slots<> { void CreateRelayPorts(); void OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us); + const rtc::ReceivedPacket& packet); void OnPortDestroyed(PortInterface* port);