diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:27 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:27 +0000 |
commit | 40a355a42d4a9444dc753c04c6608dade2f06a23 (patch) | |
tree | 871fc667d2de662f171103ce5ec067014ef85e61 /third_party/libwebrtc/p2p/base | |
parent | Adding upstream version 124.0.1. (diff) | |
download | firefox-adbda400be353e676059e335c3c0aaf99e719475.tar.xz firefox-adbda400be353e676059e335c3c0aaf99e719475.zip |
Adding upstream version 125.0.1.upstream/125.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/libwebrtc/p2p/base')
40 files changed, 550 insertions, 640 deletions
diff --git a/third_party/libwebrtc/p2p/base/async_stun_tcp_socket.cc b/third_party/libwebrtc/p2p/base/async_stun_tcp_socket.cc index 4a35903dfe..dcaf7d285d 100644 --- a/third_party/libwebrtc/p2p/base/async_stun_tcp_socket.cc +++ b/third_party/libwebrtc/p2p/base/async_stun_tcp_socket.cc @@ -14,9 +14,15 @@ #include <stdint.h> #include <string.h> +#include <cstddef> +#include <cstdint> + +#include "api/array_view.h" #include "api/transport/stun.h" +#include "api/units/timestamp.h" #include "rtc_base/byte_order.h" #include "rtc_base/checks.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/network/sent_packet.h" #include "rtc_base/time_utils.h" @@ -89,7 +95,7 @@ int AsyncStunTCPSocket::Send(const void* pv, return static_cast<int>(cb); } -void AsyncStunTCPSocket::ProcessInput(char* data, size_t* len) { +size_t AsyncStunTCPSocket::ProcessInput(rtc::ArrayView<const uint8_t> data) { rtc::SocketAddress remote_addr(GetRemoteAddress()); // STUN packet - First 4 bytes. Total header size is 20 bytes. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -101,26 +107,27 @@ void AsyncStunTCPSocket::ProcessInput(char* data, size_t* len) { // | Channel Number | Length | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + size_t processed_bytes = 0; while (true) { + size_t bytes_left = data.size() - processed_bytes; // We need at least 4 bytes to read the STUN or ChannelData packet length. - if (*len < kPacketLenOffset + kPacketLenSize) - return; + if (bytes_left < kPacketLenOffset + kPacketLenSize) + return processed_bytes; int pad_bytes; - size_t expected_pkt_len = GetExpectedLength(data, *len, &pad_bytes); + size_t expected_pkt_len = GetExpectedLength(data.data() + processed_bytes, + bytes_left, &pad_bytes); size_t actual_length = expected_pkt_len + pad_bytes; - if (*len < actual_length) { - return; + if (bytes_left < actual_length) { + return processed_bytes; } - SignalReadPacket(this, data, expected_pkt_len, remote_addr, - rtc::TimeMicros()); - - *len -= actual_length; - if (*len > 0) { - memmove(data, data + actual_length, *len); - } + rtc::ReceivedPacket received_packet( + data.subview(processed_bytes, expected_pkt_len), remote_addr, + webrtc::Timestamp::Micros(rtc::TimeMicros())); + NotifyPacketReceived(received_packet); + processed_bytes += actual_length; } } diff --git a/third_party/libwebrtc/p2p/base/async_stun_tcp_socket.h b/third_party/libwebrtc/p2p/base/async_stun_tcp_socket.h index f0df42b52a..2c43d554fe 100644 --- a/third_party/libwebrtc/p2p/base/async_stun_tcp_socket.h +++ b/third_party/libwebrtc/p2p/base/async_stun_tcp_socket.h @@ -37,7 +37,7 @@ class AsyncStunTCPSocket : public rtc::AsyncTCPSocketBase { int Send(const void* pv, size_t cb, const rtc::PacketOptions& options) override; - void ProcessInput(char* data, size_t* len) override; + size_t ProcessInput(rtc::ArrayView<const uint8_t> data) override; private: // This method returns the message hdr + length written in the header. diff --git a/third_party/libwebrtc/p2p/base/async_stun_tcp_socket_unittest.cc b/third_party/libwebrtc/p2p/base/async_stun_tcp_socket_unittest.cc index 72d6a7fde0..853fbb471f 100644 --- a/third_party/libwebrtc/p2p/base/async_stun_tcp_socket_unittest.cc +++ b/third_party/libwebrtc/p2p/base/async_stun_tcp_socket_unittest.cc @@ -13,12 +13,17 @@ #include <stdint.h> #include <string.h> +#include <cstdint> #include <list> #include <memory> #include <string> #include <utility> #include "absl/memory/memory.h" +#include "api/array_view.h" +#include "rtc_base/buffer.h" +#include "rtc_base/byte_buffer.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/network/sent_packet.h" #include "rtc_base/socket.h" #include "rtc_base/third_party/sigslot/sigslot.h" @@ -96,11 +101,10 @@ class AsyncStunTCPSocketTest : public ::testing::Test, } void OnReadPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t len, - const rtc::SocketAddress& remote_addr, - const int64_t& /* packet_time_us */) { - recv_packets_.push_back(std::string(data, len)); + const rtc::ReceivedPacket& packet) { + recv_packets_.push_back( + std::string(reinterpret_cast<const char*>(packet.payload().data()), + packet.payload().size())); } void OnSentPacket(rtc::AsyncPacketSocket* socket, @@ -111,8 +115,10 @@ class AsyncStunTCPSocketTest : public ::testing::Test, void OnNewConnection(rtc::AsyncListenSocket* /*server*/, rtc::AsyncPacketSocket* new_socket) { recv_socket_ = absl::WrapUnique(new_socket); - new_socket->SignalReadPacket.connect(this, - &AsyncStunTCPSocketTest::OnReadPacket); + new_socket->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + OnReadPacket(socket, packet); + }); } bool Send(const void* data, size_t len) { @@ -164,6 +170,30 @@ TEST_F(AsyncStunTCPSocketTest, TestMultipleStunPackets) { EXPECT_EQ(4u, recv_packets_.size()); } +TEST_F(AsyncStunTCPSocketTest, ProcessInputHandlesMultiplePackets) { + send_socket_->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + recv_packets_.push_back( + std::string(reinterpret_cast<const char*>(packet.payload().data()), + packet.payload().size())); + }); + rtc::Buffer buffer; + buffer.AppendData(kStunMessageWithZeroLength, + sizeof(kStunMessageWithZeroLength)); + // ChannelData message MUST be padded to + // a multiple of four bytes. + const unsigned char kTurnChannelData[] = { + 0x40, 0x00, 0x00, 0x04, 0x21, 0x12, 0xA4, 0x42, + }; + buffer.AppendData(kTurnChannelData, sizeof(kTurnChannelData)); + + send_socket_->ProcessInput(buffer); + EXPECT_EQ(2u, recv_packets_.size()); + EXPECT_TRUE(CheckData(kStunMessageWithZeroLength, + sizeof(kStunMessageWithZeroLength))); + EXPECT_TRUE(CheckData(kTurnChannelData, sizeof(kTurnChannelData))); +} + // Verifying TURN channel data message with zero length. TEST_F(AsyncStunTCPSocketTest, TestTurnChannelDataWithZeroLength) { EXPECT_TRUE(Send(kTurnChannelDataMessageWithZeroLength, diff --git a/third_party/libwebrtc/p2p/base/basic_async_resolver_factory.cc b/third_party/libwebrtc/p2p/base/basic_async_resolver_factory.cc index 5a8c7a27a7..b69e066039 100644 --- a/third_party/libwebrtc/p2p/base/basic_async_resolver_factory.cc +++ b/third_party/libwebrtc/p2p/base/basic_async_resolver_factory.cc @@ -15,20 +15,11 @@ #include "absl/memory/memory.h" #include "api/async_dns_resolver.h" -#include "api/wrapping_async_dns_resolver.h" #include "rtc_base/async_dns_resolver.h" -#include "rtc_base/async_resolver.h" #include "rtc_base/logging.h" namespace webrtc { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" -rtc::AsyncResolverInterface* BasicAsyncResolverFactory::Create() { - return new rtc::AsyncResolver(); -} -#pragma clang diagnostic pop - std::unique_ptr<webrtc::AsyncDnsResolverInterface> BasicAsyncDnsResolverFactory::Create() { return std::make_unique<AsyncDnsResolver>(); @@ -53,28 +44,4 @@ BasicAsyncDnsResolverFactory::CreateAndResolve( return resolver; } -std::unique_ptr<webrtc::AsyncDnsResolverInterface> -WrappingAsyncDnsResolverFactory::Create() { - return std::make_unique<WrappingAsyncDnsResolver>(wrapped_factory_->Create()); -} - -std::unique_ptr<webrtc::AsyncDnsResolverInterface> -WrappingAsyncDnsResolverFactory::CreateAndResolve( - const rtc::SocketAddress& addr, - absl::AnyInvocable<void()> callback) { - std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver = Create(); - resolver->Start(addr, std::move(callback)); - return resolver; -} - -std::unique_ptr<webrtc::AsyncDnsResolverInterface> -WrappingAsyncDnsResolverFactory::CreateAndResolve( - const rtc::SocketAddress& addr, - int family, - absl::AnyInvocable<void()> callback) { - std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver = Create(); - resolver->Start(addr, family, std::move(callback)); - return resolver; -} - } // namespace webrtc diff --git a/third_party/libwebrtc/p2p/base/basic_async_resolver_factory.h b/third_party/libwebrtc/p2p/base/basic_async_resolver_factory.h index 1a94fb9679..f99fb3e6b5 100644 --- a/third_party/libwebrtc/p2p/base/basic_async_resolver_factory.h +++ b/third_party/libwebrtc/p2p/base/basic_async_resolver_factory.h @@ -16,21 +16,9 @@ #include <utility> #include "api/async_dns_resolver.h" -#include "api/async_resolver_factory.h" -#include "rtc_base/async_resolver_interface.h" namespace webrtc { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" -class [[deprecated( - "Use BasicAsyncDnsResolverFactory")]] BasicAsyncResolverFactory final - : public AsyncResolverFactory { - public: - rtc::AsyncResolverInterface* Create() override; -}; -#pragma clang diagnostic pop - // A factory that vends AsyncDnsResolver instances. class BasicAsyncDnsResolverFactory final : public AsyncDnsResolverFactoryInterface { @@ -49,40 +37,6 @@ class BasicAsyncDnsResolverFactory final std::unique_ptr<webrtc::AsyncDnsResolverInterface> Create() override; }; -// This class wraps a factory using the older webrtc::AsyncResolverFactory API, -// and produces webrtc::AsyncDnsResolver objects that contain an -// rtc::AsyncResolver object. -class [[deprecated]] WrappingAsyncDnsResolverFactory final - : public AsyncDnsResolverFactoryInterface { - public: -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - explicit WrappingAsyncDnsResolverFactory( - std::unique_ptr<AsyncResolverFactory> wrapped_factory) - : owned_factory_(std::move(wrapped_factory)), - wrapped_factory_(owned_factory_.get()) {} - - explicit WrappingAsyncDnsResolverFactory( - AsyncResolverFactory* non_owned_factory) - : wrapped_factory_(non_owned_factory) {} -#pragma clang diagnostic pop - - std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve( - const rtc::SocketAddress& addr, - absl::AnyInvocable<void()> callback) override; - - std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve( - const rtc::SocketAddress& addr, - int family, - absl::AnyInvocable<void()> callback) override; - - std::unique_ptr<webrtc::AsyncDnsResolverInterface> Create() override; - - private: - const std::unique_ptr<AsyncResolverFactory> owned_factory_; - AsyncResolverFactory* const wrapped_factory_; -}; - } // namespace webrtc #endif // P2P_BASE_BASIC_ASYNC_RESOLVER_FACTORY_H_ diff --git a/third_party/libwebrtc/p2p/base/basic_async_resolver_factory_unittest.cc b/third_party/libwebrtc/p2p/base/basic_async_resolver_factory_unittest.cc index 313907abb9..39b7c2d1d0 100644 --- a/third_party/libwebrtc/p2p/base/basic_async_resolver_factory_unittest.cc +++ b/third_party/libwebrtc/p2p/base/basic_async_resolver_factory_unittest.cc @@ -11,8 +11,6 @@ #include "p2p/base/basic_async_resolver_factory.h" #include "api/test/mock_async_dns_resolver.h" -#include "p2p/base/mock_async_resolver.h" -#include "rtc_base/async_resolver.h" #include "rtc_base/gunit.h" #include "rtc_base/socket_address.h" #include "rtc_base/third_party/sigslot/sigslot.h" @@ -22,95 +20,6 @@ namespace webrtc { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - -class BasicAsyncResolverFactoryTest : public ::testing::Test, - public sigslot::has_slots<> { - public: - void TestCreate() { - BasicAsyncResolverFactory factory; - rtc::AsyncResolverInterface* resolver = factory.Create(); - ASSERT_TRUE(resolver); - resolver->SignalDone.connect( - this, &BasicAsyncResolverFactoryTest::SetAddressResolved); - - rtc::SocketAddress address("", 0); - resolver->Start(address); - ASSERT_TRUE_WAIT(address_resolved_, 10000 /*ms*/); - resolver->Destroy(false); - } - - void SetAddressResolved(rtc::AsyncResolverInterface* resolver) { - address_resolved_ = true; - } - - private: - bool address_resolved_ = false; -}; - -// This test is primarily intended to let tools check that the created resolver -// doesn't leak. -TEST_F(BasicAsyncResolverFactoryTest, TestCreate) { - rtc::AutoThread main_thread; - TestCreate(); -} - -TEST(WrappingAsyncDnsResolverFactoryTest, TestCreateAndResolve) { - rtc::AutoThread main_thread; - WrappingAsyncDnsResolverFactory factory( - std::make_unique<BasicAsyncResolverFactory>()); - - std::unique_ptr<AsyncDnsResolverInterface> resolver(factory.Create()); - ASSERT_TRUE(resolver); - - bool address_resolved = false; - rtc::SocketAddress address("", 0); - resolver->Start(address, [&address_resolved]() { address_resolved = true; }); - ASSERT_TRUE_WAIT(address_resolved, 10000 /*ms*/); - resolver.reset(); -} - -TEST(WrappingAsyncDnsResolverFactoryTest, WrapOtherResolver) { - rtc::AutoThread main_thread; - BasicAsyncResolverFactory non_owned_factory; - WrappingAsyncDnsResolverFactory factory(&non_owned_factory); - std::unique_ptr<AsyncDnsResolverInterface> resolver(factory.Create()); - ASSERT_TRUE(resolver); - - bool address_resolved = false; - rtc::SocketAddress address("", 0); - resolver->Start(address, [&address_resolved]() { address_resolved = true; }); - ASSERT_TRUE_WAIT(address_resolved, 10000 /*ms*/); - resolver.reset(); -} - -#if GTEST_HAS_DEATH_TEST && defined(WEBRTC_LINUX) -// Tests that the prohibition against deleting the resolver from the callback -// is enforced. This is required by the use of sigslot in the wrapped resolver. -// Checking the error message fails on a number of platforms, so run this -// test only on the platforms where it works. -void CallResolver(WrappingAsyncDnsResolverFactory& factory) { - rtc::SocketAddress address("", 0); - std::unique_ptr<AsyncDnsResolverInterface> resolver(factory.Create()); - resolver->Start(address, [&resolver]() { resolver.reset(); }); - WAIT(!resolver.get(), 10000 /*ms*/); -} - -TEST(WrappingAsyncDnsResolverFactoryDeathTest, DestroyResolverInCallback) { - rtc::AutoThread main_thread; - // TODO(bugs.webrtc.org/12652): Rewrite as death test in loop style when it - // works. - WrappingAsyncDnsResolverFactory factory( - std::make_unique<BasicAsyncResolverFactory>()); - - // Since EXPECT_DEATH is thread sensitive, and the resolver creates a thread, - // we wrap the whole creation section in EXPECT_DEATH. - RTC_EXPECT_DEATH(CallResolver(factory), - "Check failed: !within_resolve_result_"); -} -#endif - -#pragma clang diagnostic pop +// all tests were on deleted APIs } // namespace webrtc diff --git a/third_party/libwebrtc/p2p/base/basic_packet_socket_factory.cc b/third_party/libwebrtc/p2p/base/basic_packet_socket_factory.cc index 7d87e12859..6a811af71a 100644 --- a/third_party/libwebrtc/p2p/base/basic_packet_socket_factory.cc +++ b/third_party/libwebrtc/p2p/base/basic_packet_socket_factory.cc @@ -16,7 +16,6 @@ #include "absl/memory/memory.h" #include "api/async_dns_resolver.h" -#include "api/wrapping_async_dns_resolver.h" #include "p2p/base/async_stun_tcp_socket.h" #include "rtc_base/async_dns_resolver.h" #include "rtc_base/async_tcp_socket.h" @@ -181,10 +180,6 @@ AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket( return tcp_socket; } -AsyncResolverInterface* BasicPacketSocketFactory::CreateAsyncResolver() { - return new AsyncResolver(); -} - std::unique_ptr<webrtc::AsyncDnsResolverInterface> BasicPacketSocketFactory::CreateAsyncDnsResolver() { return std::make_unique<webrtc::AsyncDnsResolver>(); diff --git a/third_party/libwebrtc/p2p/base/basic_packet_socket_factory.h b/third_party/libwebrtc/p2p/base/basic_packet_socket_factory.h index 396a8ba4eb..f9bdf7b2c7 100644 --- a/third_party/libwebrtc/p2p/base/basic_packet_socket_factory.h +++ b/third_party/libwebrtc/p2p/base/basic_packet_socket_factory.h @@ -48,10 +48,6 @@ class RTC_EXPORT BasicPacketSocketFactory : public PacketSocketFactory { const std::string& user_agent, const PacketSocketTcpOptions& tcp_options) override; - // TODO(bugs.webrtc.org/12598) Remove when downstream stops using it. - ABSL_DEPRECATED("Use CreateAsyncDnsResolver") - AsyncResolverInterface* CreateAsyncResolver() override; - std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAsyncDnsResolver() override; diff --git a/third_party/libwebrtc/p2p/base/connection.cc b/third_party/libwebrtc/p2p/base/connection.cc index 3bdba0e445..d0e6f1bff8 100644 --- a/third_party/libwebrtc/p2p/base/connection.cc +++ b/third_party/libwebrtc/p2p/base/connection.cc @@ -466,32 +466,25 @@ void Connection::DeregisterReceivedPacketCallback() { void Connection::OnReadPacket(const char* data, size_t size, int64_t packet_time_us) { + OnReadPacket( + rtc::ReceivedPacket::CreateFromLegacy(data, size, packet_time_us)); +} +void Connection::OnReadPacket(const rtc::ReceivedPacket& packet) { RTC_DCHECK_RUN_ON(network_thread_); std::unique_ptr<IceMessage> msg; std::string remote_ufrag; const rtc::SocketAddress& addr(remote_candidate_.address()); - if (!port_->GetStunMessage(data, size, addr, &msg, &remote_ufrag)) { + if (!port_->GetStunMessage( + reinterpret_cast<const char*>(packet.payload().data()), + packet.payload().size(), addr, &msg, &remote_ufrag)) { // The packet did not parse as a valid STUN message // This is a data packet, pass it along. last_data_received_ = rtc::TimeMillis(); UpdateReceiving(last_data_received_); - recv_rate_tracker_.AddSamples(size); + recv_rate_tracker_.AddSamples(packet.payload().size()); stats_.packets_received++; if (received_packet_callback_) { - RTC_DCHECK(packet_time_us == -1 || packet_time_us >= 0); - RTC_DCHECK(SignalReadPacket.is_empty()); - received_packet_callback_( - this, rtc::ReceivedPacket( - rtc::reinterpret_array_view<const uint8_t>( - rtc::MakeArrayView(data, size)), - (packet_time_us >= 0) - ? absl::optional<webrtc::Timestamp>( - webrtc::Timestamp::Micros(packet_time_us)) - : absl::nullopt)); - } else { - // TODO(webrtc:11943): Remove SignalReadPacket once upstream projects have - // switched to use RegisterReceivedPacket. - SignalReadPacket(this, data, size, packet_time_us); + received_packet_callback_(this, packet); } // If timed out sending writability checks, start up again if (!pruned_ && (write_state_ == STATE_WRITE_TIMEOUT)) { diff --git a/third_party/libwebrtc/p2p/base/connection.h b/third_party/libwebrtc/p2p/base/connection.h index 3481c69cd9..cf54dc800f 100644 --- a/third_party/libwebrtc/p2p/base/connection.h +++ b/third_party/libwebrtc/p2p/base/connection.h @@ -147,10 +147,6 @@ class RTC_EXPORT Connection : public CandidatePairInterface { // Error if Send() returns < 0 virtual int GetError() = 0; - // TODO(webrtc:11943): Remove SignalReadPacket once upstream projects have - // switched to use RegisterReceivedPacket. - sigslot::signal4<Connection*, const char*, size_t, int64_t> SignalReadPacket; - // Register as a recipient of received packets. There can only be one. void RegisterReceivedPacketCallback( absl::AnyInvocable<void(Connection*, const rtc::ReceivedPacket&)> @@ -160,7 +156,9 @@ class RTC_EXPORT Connection : public CandidatePairInterface { sigslot::signal1<Connection*> SignalReadyToSend; // Called when a packet is received on this connection. - void OnReadPacket(const char* data, size_t size, int64_t packet_time_us); + void OnReadPacket(const rtc::ReceivedPacket& packet); + [[deprecated("Pass a rtc::ReceivedPacket")]] void + OnReadPacket(const char* data, size_t size, int64_t packet_time_us); // Called when the socket is currently able to send. void OnReadyToSend(); diff --git a/third_party/libwebrtc/p2p/base/fake_ice_transport.h b/third_party/libwebrtc/p2p/base/fake_ice_transport.h index ae7bf8947e..6172ebb15b 100644 --- a/third_party/libwebrtc/p2p/base/fake_ice_transport.h +++ b/third_party/libwebrtc/p2p/base/fake_ice_transport.h @@ -124,7 +124,7 @@ class FakeIceTransport : public IceTransportInternal { RTC_DCHECK_RUN_ON(network_thread_); if (gathering_state_ != kIceGatheringComplete) { gathering_state_ = kIceGatheringComplete; - SignalGatheringState(this); + SendGatheringStateEvent(); } } @@ -232,7 +232,7 @@ class FakeIceTransport : public IceTransportInternal { RTC_DCHECK_RUN_ON(network_thread_); if (gathering_state_ == kIceGatheringNew) { gathering_state_ = kIceGatheringGathering; - SignalGatheringState(this); + SendGatheringStateEvent(); } } diff --git a/third_party/libwebrtc/p2p/base/ice_transport_internal.cc b/third_party/libwebrtc/p2p/base/ice_transport_internal.cc index fab6f2037a..0dc7e50af2 100644 --- a/third_party/libwebrtc/p2p/base/ice_transport_internal.cc +++ b/third_party/libwebrtc/p2p/base/ice_transport_internal.cc @@ -123,7 +123,13 @@ int IceConfig::stun_keepalive_interval_or_default() const { return stun_keepalive_interval.value_or(STUN_KEEPALIVE_INTERVAL); } -IceTransportInternal::IceTransportInternal() = default; +IceTransportInternal::IceTransportInternal() { + // Set up detector for use of SignalGatheringState rather than + // SetGatheringStateCallback, and behave accordingly + // TODO(bugs.webrtc.org/11943): remove when Signal removed + SignalGatheringState.connect( + this, &IceTransportInternal::SignalGatheringStateFired); +} IceTransportInternal::~IceTransportInternal() = default; diff --git a/third_party/libwebrtc/p2p/base/ice_transport_internal.h b/third_party/libwebrtc/p2p/base/ice_transport_internal.h index eb21596612..38b6bfe303 100644 --- a/third_party/libwebrtc/p2p/base/ice_transport_internal.h +++ b/third_party/libwebrtc/p2p/base/ice_transport_internal.h @@ -304,17 +304,31 @@ class RTC_EXPORT IceTransportInternal : public rtc::PacketTransportInternal { return absl::nullopt; } + // Signal Exposed for backwards compatibility. sigslot::signal1<IceTransportInternal*> SignalGatheringState; + void SetGatheringStateCallback( + absl::AnyInvocable<void(IceTransportInternal*)> callback) { + RTC_DCHECK(!gathering_state_callback_); + gathering_state_callback_ = std::move(callback); + } // Handles sending and receiving of candidates. sigslot::signal2<IceTransportInternal*, const Candidate&> SignalCandidateGathered; - sigslot::signal2<IceTransportInternal*, const IceCandidateErrorEvent&> - SignalCandidateError; + void SetCandidateErrorCallback( + absl::AnyInvocable<void(IceTransportInternal*, + const IceCandidateErrorEvent&)> callback) { + RTC_DCHECK(!candidate_error_callback_); + candidate_error_callback_ = std::move(callback); + } - sigslot::signal2<IceTransportInternal*, const Candidates&> - SignalCandidatesRemoved; + void SetCandidatesRemovedCallback( + absl::AnyInvocable<void(IceTransportInternal*, const Candidates&)> + callback) { + RTC_DCHECK(!candidates_removed_callback_); + candidates_removed_callback_ = std::move(callback); + } // Deprecated by PacketTransportInternal::SignalNetworkRouteChanged. // This signal occurs when there is a change in the way that packets are @@ -324,8 +338,12 @@ class RTC_EXPORT IceTransportInternal : public rtc::PacketTransportInternal { // SignalNetworkRouteChanged. sigslot::signal2<IceTransportInternal*, const Candidate&> SignalRouteChange; - sigslot::signal1<const cricket::CandidatePairChangeEvent&> - SignalCandidatePairChanged; + void SetCandidatePairChangeCallback( + absl::AnyInvocable<void(const cricket::CandidatePairChangeEvent&)> + callback) { + RTC_DCHECK(!candidate_pair_change_callback_); + candidate_pair_change_callback_ = std::move(callback); + } // Invoked when there is conflict in the ICE role between local and remote // agents. @@ -366,12 +384,33 @@ class RTC_EXPORT IceTransportInternal : public rtc::PacketTransportInternal { } protected: + void SendGatheringStateEvent() { SignalGatheringState(this); } + webrtc::CallbackList<IceTransportInternal*, const StunDictionaryView&, rtc::ArrayView<uint16_t>> dictionary_view_updated_callback_list_; webrtc::CallbackList<IceTransportInternal*, const StunDictionaryWriter&> dictionary_writer_synced_callback_list_; + + absl::AnyInvocable<void(IceTransportInternal*)> gathering_state_callback_; + + absl::AnyInvocable<void(IceTransportInternal*, const IceCandidateErrorEvent&)> + candidate_error_callback_; + + absl::AnyInvocable<void(IceTransportInternal*, const Candidates&)> + candidates_removed_callback_; + + absl::AnyInvocable<void(const cricket::CandidatePairChangeEvent&)> + candidate_pair_change_callback_; + + private: + // TODO(bugs.webrtc.org/11943): remove when removing Signal + void SignalGatheringStateFired(IceTransportInternal* transport) { + if (gathering_state_callback_) { + gathering_state_callback_(transport); + } + } }; } // namespace cricket diff --git a/third_party/libwebrtc/p2p/base/mock_async_resolver.h b/third_party/libwebrtc/p2p/base/mock_async_resolver.h deleted file mode 100644 index 73ad1a4c34..0000000000 --- a/third_party/libwebrtc/p2p/base/mock_async_resolver.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2018 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef P2P_BASE_MOCK_ASYNC_RESOLVER_H_ -#define P2P_BASE_MOCK_ASYNC_RESOLVER_H_ - -#include "api/async_resolver_factory.h" -#include "rtc_base/async_resolver_interface.h" -#include "test/gmock.h" - -namespace rtc { - -using ::testing::_; -using ::testing::InvokeWithoutArgs; - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" -class [[deprecated]] MockAsyncResolver : public AsyncResolverInterface { -#pragma clang diagnostic pop - public: - MockAsyncResolver() { - ON_CALL(*this, Start(_)).WillByDefault(InvokeWithoutArgs([this] { - SignalDone(this); - })); - } - ~MockAsyncResolver() = default; - - MOCK_METHOD(void, Start, (const rtc::SocketAddress&), (override)); - MOCK_METHOD(void, Start, (const rtc::SocketAddress&, int family), (override)); - MOCK_METHOD(bool, - GetResolvedAddress, - (int family, SocketAddress* addr), - (const, override)); - MOCK_METHOD(int, GetError, (), (const, override)); - - // Note that this won't delete the object like AsyncResolverInterface says in - // order to avoid sanitizer failures caused by this being a synchronous - // implementation. The test code should delete the object instead. - MOCK_METHOD(void, Destroy, (bool), (override)); -}; - -} // namespace rtc - -namespace webrtc { - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" -class [[deprecated]] MockAsyncResolverFactory : public AsyncResolverFactory { - public: - MOCK_METHOD(rtc::AsyncResolverInterface*, Create, (), (override)); -}; -#pragma clang diagnostic pop -} // namespace webrtc - -#endif // P2P_BASE_MOCK_ASYNC_RESOLVER_H_ diff --git a/third_party/libwebrtc/p2p/base/p2p_transport_channel.cc b/third_party/libwebrtc/p2p/base/p2p_transport_channel.cc index 5ddab77de3..0c869ff622 100644 --- a/third_party/libwebrtc/p2p/base/p2p_transport_channel.cc +++ b/third_party/libwebrtc/p2p/base/p2p_transport_channel.cc @@ -28,7 +28,6 @@ #include "api/field_trials_view.h" #include "api/units/time_delta.h" #include "logging/rtc_event_log/ice_logger.h" -#include "p2p/base/basic_async_resolver_factory.h" #include "p2p/base/basic_ice_controller.h" #include "p2p/base/connection.h" #include "p2p/base/connection_info.h" @@ -117,25 +116,11 @@ std::unique_ptr<P2PTransportChannel> P2PTransportChannel::Create( absl::string_view transport_name, int component, webrtc::IceTransportInit init) { - // TODO(bugs.webrtc.org/12598): Remove pragma and fallback once - // async_resolver_factory is gone -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - if (init.async_resolver_factory()) { - return absl::WrapUnique(new P2PTransportChannel( - transport_name, component, init.port_allocator(), nullptr, - std::make_unique<webrtc::WrappingAsyncDnsResolverFactory>( - init.async_resolver_factory()), - init.event_log(), init.ice_controller_factory(), - init.active_ice_controller_factory(), init.field_trials())); -#pragma clang diagnostic pop - } else { - return absl::WrapUnique(new P2PTransportChannel( - transport_name, component, init.port_allocator(), - init.async_dns_resolver_factory(), nullptr, init.event_log(), - init.ice_controller_factory(), init.active_ice_controller_factory(), - init.field_trials())); - } + return absl::WrapUnique(new P2PTransportChannel( + transport_name, component, init.port_allocator(), + init.async_dns_resolver_factory(), nullptr, init.event_log(), + init.ice_controller_factory(), init.active_ice_controller_factory(), + init.field_trials())); } P2PTransportChannel::P2PTransportChannel( @@ -884,7 +869,7 @@ void P2PTransportChannel::MaybeStartGathering() { ice_parameters_.ufrag, ice_parameters_.pwd)) { if (gathering_state_ != kIceGatheringGathering) { gathering_state_ = kIceGatheringGathering; - SignalGatheringState(this); + SendGatheringStateEvent(); } if (!allocator_sessions_.empty()) { @@ -996,7 +981,9 @@ void P2PTransportChannel::OnCandidateError( PortAllocatorSession* session, const IceCandidateErrorEvent& event) { RTC_DCHECK(network_thread_ == rtc::Thread::Current()); - SignalCandidateError(this, event); + if (candidate_error_callback_) { + candidate_error_callback_(this, event); + } } void P2PTransportChannel::OnCandidatesAllocationDone( @@ -1012,7 +999,7 @@ void P2PTransportChannel::OnCandidatesAllocationDone( gathering_state_ = kIceGatheringComplete; RTC_LOG(LS_INFO) << "P2PTransportChannel: " << transport_name() << ", component " << component() << " gathering complete"; - SignalGatheringState(this); + SendGatheringStateEvent(); } // Handle stun packets @@ -1869,8 +1856,9 @@ void P2PTransportChannel::SwitchSelectedConnectionInternal( } else { pair_change.estimated_disconnected_time_ms = 0; } - - SignalCandidatePairChanged(pair_change); + if (candidate_pair_change_callback_) { + candidate_pair_change_callback_(pair_change); + } } ++selected_candidate_pair_changes_; @@ -2200,7 +2188,9 @@ void P2PTransportChannel::OnCandidatesRemoved( candidate.set_transport_name(transport_name()); candidates_to_remove.push_back(candidate); } - SignalCandidatesRemoved(this, candidates_to_remove); + if (candidates_removed_callback_) { + candidates_removed_callback_(this, candidates_to_remove); + } } void P2PTransportChannel::PruneAllPorts() { diff --git a/third_party/libwebrtc/p2p/base/p2p_transport_channel.h b/third_party/libwebrtc/p2p/base/p2p_transport_channel.h index 7f85018cd8..84325b8bef 100644 --- a/third_party/libwebrtc/p2p/base/p2p_transport_channel.h +++ b/third_party/libwebrtc/p2p/base/p2p_transport_channel.h @@ -36,7 +36,6 @@ #include "absl/types/optional.h" #include "api/array_view.h" #include "api/async_dns_resolver.h" -#include "api/async_resolver_factory.h" #include "api/candidate.h" #include "api/ice_transport_interface.h" #include "api/rtc_error.h" @@ -47,7 +46,6 @@ #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h" #include "logging/rtc_event_log/ice_logger.h" #include "p2p/base/active_ice_controller_factory_interface.h" -#include "p2p/base/basic_async_resolver_factory.h" #include "p2p/base/candidate_pair_interface.h" #include "p2p/base/connection.h" #include "p2p/base/ice_agent_interface.h" diff --git a/third_party/libwebrtc/p2p/base/p2p_transport_channel_unittest.cc b/third_party/libwebrtc/p2p/base/p2p_transport_channel_unittest.cc index e414e3f558..44b1bfc5e3 100644 --- a/third_party/libwebrtc/p2p/base/p2p_transport_channel_unittest.cc +++ b/third_party/libwebrtc/p2p/base/p2p_transport_channel_unittest.cc @@ -26,7 +26,6 @@ #include "p2p/base/fake_port_allocator.h" #include "p2p/base/ice_transport_internal.h" #include "p2p/base/mock_active_ice_controller.h" -#include "p2p/base/mock_async_resolver.h" #include "p2p/base/mock_ice_controller.h" #include "p2p/base/packet_transport_internal.h" #include "p2p/base/test_stun_server.h" @@ -45,11 +44,13 @@ #include "rtc_base/mdns_responder_interface.h" #include "rtc_base/nat_server.h" #include "rtc_base/nat_socket_factory.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/proxy_server.h" #include "rtc_base/socket_address.h" #include "rtc_base/ssl_adapter.h" #include "rtc_base/strings/string_builder.h" #include "rtc_base/thread.h" +#include "rtc_base/time_utils.h" #include "rtc_base/virtual_socket_server.h" #include "system_wrappers/include/metrics.h" #include "test/scoped_key_value_config.h" @@ -283,7 +284,7 @@ class P2PTransportChannelTestBase : public ::testing::Test, ss_(new rtc::FirewallSocketServer(nss_.get())), socket_factory_(new rtc::BasicPacketSocketFactory(ss_.get())), main_(ss_.get()), - stun_server_(TestStunServer::Create(ss_.get(), kStunAddr)), + stun_server_(TestStunServer::Create(ss_.get(), kStunAddr, main_)), turn_server_(&main_, ss_.get(), kTurnUdpIntAddr, kTurnUdpExtAddr), socks_server1_(ss_.get(), kSocksProxyAddrs[0], @@ -486,8 +487,10 @@ class P2PTransportChannelTestBase : public ::testing::Test, this, &P2PTransportChannelTestBase::OnReadyToSend); channel->SignalCandidateGathered.connect( this, &P2PTransportChannelTestBase::OnCandidateGathered); - channel->SignalCandidatesRemoved.connect( - this, &P2PTransportChannelTestBase::OnCandidatesRemoved); + channel->SetCandidatesRemovedCallback( + [this](IceTransportInternal* transport, const Candidates& candidates) { + OnCandidatesRemoved(transport, candidates); + }); channel->SignalReadPacket.connect( this, &P2PTransportChannelTestBase::OnReadPacket); channel->SignalRoleConflict.connect( @@ -1023,7 +1026,7 @@ class P2PTransportChannelTestBase : public ::testing::Test, rtc::AutoSocketServerThread main_; rtc::scoped_refptr<PendingTaskSafetyFlag> safety_ = PendingTaskSafetyFlag::Create(); - std::unique_ptr<TestStunServer> stun_server_; + TestStunServer::StunServerPtr stun_server_; TestTurnServer turn_server_; rtc::SocksProxyServer socks_server1_; rtc::SocksProxyServer socks_server2_; @@ -3479,8 +3482,10 @@ class P2PTransportChannelPingTest : public ::testing::Test, &P2PTransportChannelPingTest::OnReadyToSend); ch->SignalStateChanged.connect( this, &P2PTransportChannelPingTest::OnChannelStateChanged); - ch->SignalCandidatePairChanged.connect( - this, &P2PTransportChannelPingTest::OnCandidatePairChanged); + ch->SetCandidatePairChangeCallback( + [this](const cricket::CandidatePairChangeEvent& event) { + OnCandidatePairChanged(event); + }); } Connection* WaitForConnectionTo( @@ -3595,7 +3600,8 @@ class P2PTransportChannelPingTest : public ::testing::Test, msg.AddFingerprint(); rtc::ByteBufferWriter buf; msg.Write(&buf); - conn->OnReadPacket(buf.Data(), buf.Length(), rtc::TimeMicros()); + conn->OnReadPacket(rtc::ReceivedPacket::CreateFromLegacy( + buf.Data(), buf.Length(), rtc::TimeMicros())); } void ReceivePingOnConnection(Connection* conn, @@ -4060,7 +4066,9 @@ TEST_F(P2PTransportChannelPingTest, TestReceivingStateChange) { clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); conn1->ReceivedPing(); - conn1->OnReadPacket("ABC", 3, rtc::TimeMicros()); + conn1->OnReadPacket( + rtc::ReceivedPacket::CreateFromLegacy("ABC", 3, rtc::TimeMicros())); + EXPECT_TRUE_SIMULATED_WAIT(ch.receiving(), kShortTimeout, clock); EXPECT_TRUE_SIMULATED_WAIT(!ch.receiving(), kShortTimeout, clock); } @@ -4370,7 +4378,8 @@ TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) { Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); ASSERT_TRUE(conn2 != nullptr); conn2->ReceivedPingResponse(LOW_RTT, "id"); // Become writable and receiving. - conn2->OnReadPacket("ABC", 3, rtc::TimeMicros()); + conn2->OnReadPacket( + rtc::ReceivedPacket::CreateFromLegacy("ABC", 3, rtc::TimeMicros())); EXPECT_EQ(conn2, ch.selected_connection()); conn2->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. @@ -4397,7 +4406,8 @@ TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) { // selected connection was nominated by the controlling side. conn2->ReceivedPing(); conn2->ReceivedPingResponse(LOW_RTT, "id"); - conn2->OnReadPacket("XYZ", 3, rtc::TimeMicros()); + conn2->OnReadPacket( + rtc::ReceivedPacket::CreateFromLegacy("XYZ", 3, rtc::TimeMicros())); EXPECT_EQ_WAIT(conn3, ch.selected_connection(), kDefaultTimeout); } @@ -4427,12 +4437,15 @@ TEST_F(P2PTransportChannelPingTest, // Advance the clock by 1ms so that the last data receiving timestamp of // conn2 is larger. SIMULATED_WAIT(false, 1, clock); - conn2->OnReadPacket("XYZ", 3, rtc::TimeMicros()); + + conn2->OnReadPacket( + rtc::ReceivedPacket::CreateFromLegacy("XYZ", 3, rtc::TimeMicros())); EXPECT_EQ(1, reset_selected_candidate_pair_switches()); EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); // conn1 also receives data; it becomes selected due to priority again. - conn1->OnReadPacket("XYZ", 3, rtc::TimeMicros()); + conn1->OnReadPacket( + rtc::ReceivedPacket::CreateFromLegacy("ABC", 3, rtc::TimeMicros())); EXPECT_EQ(1, reset_selected_candidate_pair_switches()); EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); @@ -4441,7 +4454,8 @@ TEST_F(P2PTransportChannelPingTest, SIMULATED_WAIT(false, 1, clock); // Need to become writable again because it was pruned. conn2->ReceivedPingResponse(LOW_RTT, "id"); - conn2->OnReadPacket("XYZ", 3, rtc::TimeMicros()); + conn2->OnReadPacket( + rtc::ReceivedPacket::CreateFromLegacy("ABC", 3, rtc::TimeMicros())); EXPECT_EQ(1, reset_selected_candidate_pair_switches()); EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); @@ -4472,7 +4486,9 @@ TEST_F(P2PTransportChannelPingTest, // conn1 received data; it is the selected connection. // Advance the clock to have a non-zero last-data-receiving time. SIMULATED_WAIT(false, 1, clock); - conn1->OnReadPacket("XYZ", 3, rtc::TimeMicros()); + + conn1->OnReadPacket( + rtc::ReceivedPacket::CreateFromLegacy("XYZ", 3, rtc::TimeMicros())); EXPECT_EQ(1, reset_selected_candidate_pair_switches()); EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); @@ -4572,7 +4588,8 @@ TEST_F(P2PTransportChannelPingTest, TestEstimatedDisconnectedTime) { { clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); // This will not parse as STUN, and is considered data - conn1->OnReadPacket("XYZ", 3, rtc::TimeMicros()); + conn1->OnReadPacket( + rtc::ReceivedPacket::CreateFromLegacy("XYZ", 3, rtc::TimeMicros())); clock.AdvanceTime(webrtc::TimeDelta::Seconds(2)); // conn2 is nominated; it becomes selected. @@ -4584,8 +4601,8 @@ TEST_F(P2PTransportChannelPingTest, TestEstimatedDisconnectedTime) { { clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - conn2->OnReadPacket("XYZ", 3, rtc::TimeMicros()); - + conn2->OnReadPacket( + rtc::ReceivedPacket::CreateFromLegacy("XYZ", 3, rtc::TimeMicros())); clock.AdvanceTime(webrtc::TimeDelta::Seconds(2)); ReceivePingOnConnection(conn2, kIceUfrag[1], 1, nomination++); @@ -4758,7 +4775,8 @@ TEST_F(P2PTransportChannelPingTest, TestDontPruneHighPriorityConnections) { // conn2. NominateConnection(conn1); SIMULATED_WAIT(false, 1, clock); - conn1->OnReadPacket("XYZ", 3, rtc::TimeMicros()); + conn1->OnReadPacket( + rtc::ReceivedPacket::CreateFromLegacy("XYZ", 3, rtc::TimeMicros())); SIMULATED_WAIT(conn2->pruned(), 100, clock); EXPECT_FALSE(conn2->pruned()); } diff --git a/third_party/libwebrtc/p2p/base/port.cc b/third_party/libwebrtc/p2p/base/port.cc index afd998c3ab..3069799f7b 100644 --- a/third_party/libwebrtc/p2p/base/port.cc +++ b/third_party/libwebrtc/p2p/base/port.cc @@ -13,6 +13,7 @@ #include <math.h> #include <algorithm> +#include <cstddef> #include <memory> #include <utility> #include <vector> @@ -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<const char*>(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); @@ -452,7 +454,8 @@ bool Port::GetStunMessage(const char* data, // Parse the request message. If the packet is not a complete and correct // STUN message, then ignore it. std::unique_ptr<IceMessage> stun_msg(new IceMessage()); - rtc::ByteBufferReader buf(data, size); + rtc::ByteBufferReader buf( + rtc::MakeArrayView(reinterpret_cast<const uint8_t*>(data), size)); if (!stun_msg->Read(&buf) || (buf.Length() > 0)) { return false; } @@ -725,10 +728,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/third_party/libwebrtc/p2p/base/port.h b/third_party/libwebrtc/p2p/base/port.h index 78a4f91a93..796e1e1d5b 100644 --- a/third_party/libwebrtc/p2p/base/port.h +++ b/third_party/libwebrtc/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/third_party/libwebrtc/p2p/base/port_unittest.cc b/third_party/libwebrtc/p2p/base/port_unittest.cc index b27afe2f39..96c1bd5ee1 100644 --- a/third_party/libwebrtc/p2p/base/port_unittest.cc +++ b/third_party/libwebrtc/p2p/base/port_unittest.cc @@ -23,6 +23,7 @@ #include "absl/memory/memory.h" #include "absl/strings/string_view.h" #include "absl/types/optional.h" +#include "api/array_view.h" #include "api/candidate.h" #include "api/packet_socket_factory.h" #include "api/transport/stun.h" @@ -55,6 +56,7 @@ #include "rtc_base/nat_types.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/network_constants.h" #include "rtc_base/proxy_info.h" @@ -162,8 +164,11 @@ class TestPort : public Port { using cricket::Port::GetStunMessage; // The last StunMessage that was sent on this Port. - // TODO(?): Make these const; requires changes to SendXXXXResponse. - rtc::BufferT<uint8_t>* last_stun_buf() { return last_stun_buf_.get(); } + rtc::ArrayView<const uint8_t> last_stun_buf() { + if (!last_stun_buf_) + return rtc::ArrayView<const uint8_t>(); + return *last_stun_buf_; + } IceMessage* last_stun_msg() { return last_stun_msg_.get(); } int last_stun_error_code() { int code = 0; @@ -260,14 +265,15 @@ static void SendPingAndReceiveResponse(Connection* lconn, int64_t ms) { lconn->Ping(rtc::TimeMillis()); ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout); - ASSERT_TRUE(lport->last_stun_buf()); - rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), - lport->last_stun_buf()->size(), /* packet_time_us */ -1); + ASSERT_GT(lport->last_stun_buf().size(), 0u); + rconn->OnReadPacket(rtc::ReceivedPacket(lport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); + clock->AdvanceTime(webrtc::TimeDelta::Millis(ms)); ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout); - ASSERT_TRUE(rport->last_stun_buf()); - lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), - rport->last_stun_buf()->size(), /* packet_time_us */ -1); + ASSERT_GT(rport->last_stun_buf().size(), 0u); + lconn->OnReadPacket(rtc::ReceivedPacket(rport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); } class TestChannel : public sigslot::has_slots<> { @@ -415,7 +421,7 @@ class PortTest : public ::testing::Test, public sigslot::has_slots<> { nat_factory2_(ss_.get(), kNatAddr2, SocketAddress()), nat_socket_factory1_(&nat_factory1_), nat_socket_factory2_(&nat_factory2_), - stun_server_(TestStunServer::Create(ss_.get(), kStunAddr)), + stun_server_(TestStunServer::Create(ss_.get(), kStunAddr, main_)), turn_server_(&main_, ss_.get(), kTurnUdpIntAddr, kTurnUdpExtAddr), username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), password_(rtc::CreateRandomString(ICE_PWD_LENGTH)), @@ -867,7 +873,7 @@ class PortTest : public ::testing::Test, public sigslot::has_slots<> { rtc::NATSocketFactory nat_factory2_; rtc::BasicPacketSocketFactory nat_socket_factory1_; rtc::BasicPacketSocketFactory nat_socket_factory2_; - std::unique_ptr<TestStunServer> stun_server_; + TestStunServer::StunServerPtr stun_server_; TestTurnServer turn_server_; std::string username_; std::string password_; @@ -1488,8 +1494,8 @@ TEST_F(PortTest, TestLoopbackCall) { ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); IceMessage* msg = lport->last_stun_msg(); EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); - conn->OnReadPacket(lport->last_stun_buf()->data<char>(), - lport->last_stun_buf()->size(), /* packet_time_us */ -1); + conn->OnReadPacket(rtc::ReceivedPacket(lport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); msg = lport->last_stun_msg(); EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); @@ -1522,7 +1528,8 @@ TEST_F(PortTest, TestLoopbackCall) { lport->Reset(); auto buf = std::make_unique<ByteBufferWriter>(); WriteStunMessage(*modified_req, buf.get()); - conn1->OnReadPacket(buf->Data(), buf->Length(), /* packet_time_us */ -1); + conn1->OnReadPacket(rtc::ReceivedPacket::CreateFromLegacy( + buf->Data(), buf->Length(), /*packet_time_us=*/-1)); ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); msg = lport->last_stun_msg(); EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type()); @@ -1555,8 +1562,8 @@ TEST_F(PortTest, TestIceRoleConflict) { IceMessage* msg = rport->last_stun_msg(); EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); // Send rport binding request to lport. - lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), - rport->last_stun_buf()->size(), /* packet_time_us */ -1); + lconn->OnReadPacket(rtc::ReceivedPacket(rport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type()); @@ -1878,7 +1885,8 @@ TEST_F(PortTest, TestSendStunMessage) { EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); EXPECT_TRUE(StunMessage::ValidateFingerprint( - lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); + reinterpret_cast<const char*>(lport->last_stun_buf().data()), + lport->last_stun_buf().size())); // Request should not include ping count. ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL); @@ -1887,14 +1895,15 @@ TEST_F(PortTest, TestSendStunMessage) { std::unique_ptr<IceMessage> request = CopyStunMessage(*msg); // Receive the BINDING-REQUEST and respond with BINDING-RESPONSE. - rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), - lport->last_stun_buf()->size(), /* packet_time_us */ -1); + rconn->OnReadPacket(rtc::ReceivedPacket(lport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); msg = rport->last_stun_msg(); ASSERT_TRUE(msg != NULL); EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); // Received a BINDING-RESPONSE. - lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), - rport->last_stun_buf()->size(), /* packet_time_us */ -1); + lconn->OnReadPacket(rtc::ReceivedPacket(rport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); + // Verify the STUN Stats. EXPECT_EQ(1U, lconn->stats().sent_ping_requests_total); EXPECT_EQ(1U, lconn->stats().sent_ping_requests_before_first_response); @@ -1912,7 +1921,8 @@ TEST_F(PortTest, TestSendStunMessage) { msg->ValidateMessageIntegrity("rpass")); EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); EXPECT_TRUE(StunMessage::ValidateFingerprint( - lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); + reinterpret_cast<const char*>(lport->last_stun_buf().data()), + lport->last_stun_buf().size())); // No USERNAME or PRIORITY in ICE responses. EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); @@ -1942,7 +1952,8 @@ TEST_F(PortTest, TestSendStunMessage) { msg->ValidateMessageIntegrity("rpass")); EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); EXPECT_TRUE(StunMessage::ValidateFingerprint( - lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); + reinterpret_cast<const char*>(lport->last_stun_buf().data()), + lport->last_stun_buf().size())); // No USERNAME with ICE. EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); @@ -1971,12 +1982,12 @@ TEST_F(PortTest, TestSendStunMessage) { // Respond with a BINDING-RESPONSE. request = CopyStunMessage(*msg); - lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), - rport->last_stun_buf()->size(), /* packet_time_us */ -1); + lconn->OnReadPacket(rtc::ReceivedPacket(rport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); msg = lport->last_stun_msg(); // Receive the BINDING-RESPONSE. - rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), - lport->last_stun_buf()->size(), /* packet_time_us */ -1); + rconn->OnReadPacket(rtc::ReceivedPacket(lport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); // Verify the Stun ping stats. EXPECT_EQ(3U, rconn->stats().sent_ping_requests_total); @@ -2026,9 +2037,10 @@ TEST_F(PortTest, TestNomination) { // should set the remote nomination of `rconn`. lconn->Ping(0); ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout); - ASSERT_TRUE(lport->last_stun_buf()); - rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), - lport->last_stun_buf()->size(), /* packet_time_us */ -1); + ASSERT_GT(lport->last_stun_buf().size(), 0u); + rconn->OnReadPacket(rtc::ReceivedPacket(lport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); + EXPECT_EQ(nomination, rconn->remote_nomination()); EXPECT_FALSE(lconn->nominated()); EXPECT_TRUE(rconn->nominated()); @@ -2038,9 +2050,10 @@ TEST_F(PortTest, TestNomination) { // This should result in an acknowledgment sent back from `rconn` to `lconn`, // updating the acknowledged nomination of `lconn`. ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout); - ASSERT_TRUE(rport->last_stun_buf()); - lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), - rport->last_stun_buf()->size(), /* packet_time_us */ -1); + ASSERT_GT(rport->last_stun_buf().size(), 0u); + lconn->OnReadPacket(rtc::ReceivedPacket(rport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); + EXPECT_EQ(nomination, lconn->acked_nomination()); EXPECT_TRUE(lconn->nominated()); EXPECT_TRUE(rconn->nominated()); @@ -2166,8 +2179,9 @@ TEST_F(PortTest, TestNetworkCostChange) { IceMessage* msg = lport->last_stun_msg(); EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); // Pass the binding request to rport. - rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), - lport->last_stun_buf()->size(), /* packet_time_us */ -1); + rconn->OnReadPacket(rtc::ReceivedPacket(lport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); + // Wait until rport sends the response and then check the remote network cost. ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); EXPECT_EQ(rtc::kNetworkCostHigh, rconn->remote_candidate().network_cost()); @@ -2496,8 +2510,8 @@ TEST_F(PortTest, // Send request. lconn->Ping(0); ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); - rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), - lport->last_stun_buf()->size(), /* packet_time_us */ -1); + rconn->OnReadPacket(rtc::ReceivedPacket(lport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); // Intercept request and add comprehension required attribute. ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); @@ -2507,7 +2521,8 @@ TEST_F(PortTest, modified_response->AddFingerprint(); ByteBufferWriter buf; WriteStunMessage(*modified_response, &buf); - lconn->OnReadPacket(buf.Data(), buf.Length(), /* packet_time_us */ -1); + lconn->OnReadPacket(rtc::ReceivedPacket::CreateFromLegacy( + buf.Data(), buf.Length(), /*packet_time_us=*/-1)); // Response should have been ignored, leaving us unwritable still. EXPECT_FALSE(lconn->writable()); } @@ -2535,7 +2550,8 @@ TEST_F(PortTest, in_msg->AddFingerprint(); ByteBufferWriter buf; WriteStunMessage(*in_msg, &buf); - lconn->OnReadPacket(buf.Data(), buf.Length(), /* packet_time_us */ -1); + lconn->OnReadPacket(rtc::ReceivedPacket::CreateFromLegacy( + buf.Data(), buf.Length(), /*packet_time_us=*/-1)); EXPECT_EQ(0u, lconn->last_ping_received()); } @@ -2581,8 +2597,9 @@ TEST_F(PortTest, TestHandleStunBindingIndication) { IceMessage* msg = rport->last_stun_msg(); EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); // Send rport binding request to lport. - lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), - rport->last_stun_buf()->size(), /* packet_time_us */ -1); + lconn->OnReadPacket(rtc::ReceivedPacket(rport->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); + ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type()); int64_t last_ping_received1 = lconn->last_ping_received(); @@ -2590,7 +2607,8 @@ TEST_F(PortTest, TestHandleStunBindingIndication) { // Adding a delay of 100ms. rtc::Thread::Current()->ProcessMessages(100); // Pinging lconn using stun indication message. - lconn->OnReadPacket(buf->Data(), buf->Length(), /* packet_time_us */ -1); + lconn->OnReadPacket(rtc::ReceivedPacket::CreateFromLegacy( + buf->Data(), buf->Length(), /*packet_time_us=*/-1)); int64_t last_ping_received2 = lconn->last_ping_received(); EXPECT_GT(last_ping_received2, last_ping_received1); } @@ -3087,9 +3105,9 @@ TEST_F(PortTest, TestIceLiteConnectivity) { con->SendStunBindingResponse(request.get()); // Feeding the respone message from litemode to the full mode connection. - ch1.conn()->OnReadPacket(ice_lite_port->last_stun_buf()->data<char>(), - ice_lite_port->last_stun_buf()->size(), - /* packet_time_us */ -1); + ch1.conn()->OnReadPacket(rtc::ReceivedPacket( + ice_lite_port->last_stun_buf(), rtc::SocketAddress(), absl::nullopt)); + // Verifying full mode connection becomes writable from the response. EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(), kDefaultTimeout); @@ -3205,9 +3223,8 @@ TEST_P(GoogPingTest, TestGoogPingAnnounceEnable) { GetSupportedGoogPingVersion(response) >= kGoogPingVersion); // Feeding the respone message back. - ch1.conn()->OnReadPacket(port2->last_stun_buf()->data<char>(), - port2->last_stun_buf()->size(), - /* packet_time_us */ -1); + ch1.conn()->OnReadPacket(rtc::ReceivedPacket( + port2->last_stun_buf(), rtc::SocketAddress(), absl::nullopt)); port1->Reset(); port2->Reset(); @@ -3390,7 +3407,8 @@ TEST_F(PortTest, TestGoogPingUnsupportedVersionInStunBindingResponse) { modified_response->Write(&buf); // Feeding the modified respone message back. - ch1.conn()->OnReadPacket(buf.Data(), buf.Length(), /* packet_time_us */ -1); + ch1.conn()->OnReadPacket(rtc::ReceivedPacket::CreateFromLegacy( + buf.Data(), buf.Length(), /*packet_time_us=*/-1)); port1->Reset(); port2->Reset(); @@ -3462,9 +3480,8 @@ TEST_F(PortTest, TestChangeInAttributeMakesGoogPingFallsbackToStunBinding) { ASSERT_TRUE(GetSupportedGoogPingVersion(response) >= kGoogPingVersion); // Feeding the respone message back. - ch1.conn()->OnReadPacket(port2->last_stun_buf()->data<char>(), - port2->last_stun_buf()->size(), - /* packet_time_us */ -1); + ch1.conn()->OnReadPacket(rtc::ReceivedPacket( + port2->last_stun_buf(), rtc::SocketAddress(), absl::nullopt)); port1->Reset(); port2->Reset(); @@ -3548,9 +3565,8 @@ TEST_F(PortTest, TestErrorResponseMakesGoogPingFallBackToStunBinding) { ASSERT_TRUE(GetSupportedGoogPingVersion(response) >= kGoogPingVersion); // Feeding the respone message back. - ch1.conn()->OnReadPacket(port2->last_stun_buf()->data<char>(), - port2->last_stun_buf()->size(), - /* packet_time_us */ -1); + ch1.conn()->OnReadPacket(rtc::ReceivedPacket( + port2->last_stun_buf(), rtc::SocketAddress(), absl::nullopt)); port1->Reset(); port2->Reset(); @@ -3576,8 +3592,8 @@ TEST_F(PortTest, TestErrorResponseMakesGoogPingFallBackToStunBinding) { rtc::ByteBufferWriter buf; error_response.Write(&buf); - ch1.conn()->OnReadPacket(buf.Data(), buf.Length(), - /* packet_time_us */ -1); + ch1.conn()->OnReadPacket(rtc::ReceivedPacket::CreateFromLegacy( + buf.Data(), buf.Length(), /*packet_time_us=*/-1)); // And now the third ping...this should be a binding. port1->Reset(); @@ -3813,14 +3829,14 @@ class ConnectionTest : public PortTest { rconn->PortForTest() == rport_.get() ? rport_.get() : lport_.get(); lconn->Ping(rtc::TimeMillis()); ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout); - ASSERT_TRUE(lport->last_stun_buf()); - rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), - lport->last_stun_buf()->size(), - /* packet_time_us */ -1); + ASSERT_GT(lport->last_stun_buf().size(), 0u); + rconn->OnReadPacket(rtc::ReceivedPacket( + lport->last_stun_buf(), rtc::SocketAddress(), absl::nullopt)); + clock_.AdvanceTime(webrtc::TimeDelta::Millis(ms)); ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout); - ASSERT_TRUE(rport->last_stun_buf()); - *reply = std::move(*rport->last_stun_buf()); + ASSERT_GT(rport->last_stun_buf().size(), 0u); + reply->SetData(rport->last_stun_buf()); } void SendPingAndReceiveResponse(Connection* lconn, @@ -3828,8 +3844,9 @@ class ConnectionTest : public PortTest { int64_t ms) { rtc::BufferT<uint8_t> reply; SendPingAndCaptureReply(lconn, rconn, ms, &reply); - lconn->OnReadPacket(reply.data<char>(), reply.size(), - /* packet_time_us */ -1); + + lconn->OnReadPacket( + rtc::ReceivedPacket(reply, rtc::SocketAddress(), absl::nullopt)); } void OnConnectionStateChange(Connection* connection) { num_state_changes_++; } @@ -3890,8 +3907,8 @@ TEST_F(ConnectionTest, ConnectionForgetLearnedStateDiscardsPendingPings) { EXPECT_FALSE(lconn->writable()); EXPECT_FALSE(lconn->receiving()); - lconn->OnReadPacket(reply.data<char>(), reply.size(), - /* packet_time_us */ -1); + lconn->OnReadPacket( + rtc::ReceivedPacket(reply, rtc::SocketAddress(), absl::nullopt)); // That reply was discarded due to the ForgetLearnedState() while it was // outstanding. @@ -3962,16 +3979,17 @@ TEST_F(ConnectionTest, SendReceiveGoogDelta) { lconn->Ping(rtc::TimeMillis(), std::move(delta)); ASSERT_TRUE_WAIT(lport_->last_stun_msg(), kDefaultTimeout); - ASSERT_TRUE(lport_->last_stun_buf()); - rconn->OnReadPacket(lport_->last_stun_buf()->data<char>(), - lport_->last_stun_buf()->size(), /* packet_time_us */ -1); + ASSERT_GT(lport_->last_stun_buf().size(), 0u); + rconn->OnReadPacket(rtc::ReceivedPacket(lport_->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); EXPECT_TRUE(received_goog_delta); clock_.AdvanceTime(webrtc::TimeDelta::Millis(ms)); ASSERT_TRUE_WAIT(rport_->last_stun_msg(), kDefaultTimeout); - ASSERT_TRUE(rport_->last_stun_buf()); - lconn->OnReadPacket(rport_->last_stun_buf()->data<char>(), - rport_->last_stun_buf()->size(), /* packet_time_us */ -1); + ASSERT_GT(rport_->last_stun_buf().size(), 0u); + lconn->OnReadPacket(rtc::ReceivedPacket(rport_->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); + EXPECT_TRUE(received_goog_delta_ack); } @@ -3999,15 +4017,15 @@ TEST_F(ConnectionTest, SendGoogDeltaNoReply) { lconn->Ping(rtc::TimeMillis(), std::move(delta)); ASSERT_TRUE_WAIT(lport_->last_stun_msg(), kDefaultTimeout); - ASSERT_TRUE(lport_->last_stun_buf()); - rconn->OnReadPacket(lport_->last_stun_buf()->data<char>(), - lport_->last_stun_buf()->size(), /* packet_time_us */ -1); + ASSERT_GT(lport_->last_stun_buf().size(), 0u); + rconn->OnReadPacket(rtc::ReceivedPacket(lport_->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); clock_.AdvanceTime(webrtc::TimeDelta::Millis(ms)); ASSERT_TRUE_WAIT(rport_->last_stun_msg(), kDefaultTimeout); - ASSERT_TRUE(rport_->last_stun_buf()); - lconn->OnReadPacket(rport_->last_stun_buf()->data<char>(), - rport_->last_stun_buf()->size(), /* packet_time_us */ -1); + ASSERT_GT(rport_->last_stun_buf().size(), 0u); + lconn->OnReadPacket(rtc::ReceivedPacket(rport_->last_stun_buf(), + rtc::SocketAddress(), absl::nullopt)); EXPECT_TRUE(received_goog_delta_ack_error); } diff --git a/third_party/libwebrtc/p2p/base/pseudo_tcp.cc b/third_party/libwebrtc/p2p/base/pseudo_tcp.cc index eff86e849e..5a5ce0392b 100644 --- a/third_party/libwebrtc/p2p/base/pseudo_tcp.cc +++ b/third_party/libwebrtc/p2p/base/pseudo_tcp.cc @@ -1191,7 +1191,8 @@ void PseudoTcp::parseOptions(const char* data, uint32_t len) { // See http://www.freesoft.org/CIE/Course/Section4/8.htm for // parsing the options list. - rtc::ByteBufferReader buf(data, len); + rtc::ByteBufferReader buf( + rtc::MakeArrayView(reinterpret_cast<const uint8_t*>(data), len)); while (buf.Length()) { uint8_t kind = TCP_OPT_EOL; buf.ReadUInt8(&kind); diff --git a/third_party/libwebrtc/p2p/base/stun_dictionary.cc b/third_party/libwebrtc/p2p/base/stun_dictionary.cc index bf6a1e49c2..318bed0ad1 100644 --- a/third_party/libwebrtc/p2p/base/stun_dictionary.cc +++ b/third_party/libwebrtc/p2p/base/stun_dictionary.cc @@ -80,7 +80,7 @@ const StunAttribute* StunDictionaryView::GetOrNull( webrtc::RTCErrorOr< std::pair<uint64_t, std::deque<std::unique_ptr<StunAttribute>>>> StunDictionaryView::ParseDelta(const StunByteStringAttribute& delta) { - rtc::ByteBufferReader buf(delta.bytes(), delta.length()); + rtc::ByteBufferReader buf(delta.array_view()); uint16_t magic; if (!buf.ReadUInt16(&magic)) { return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, diff --git a/third_party/libwebrtc/p2p/base/stun_port.cc b/third_party/libwebrtc/p2p/base/stun_port.cc index 3b024bdfea..7acaac6dbe 100644 --- a/third_party/libwebrtc/p2p/base/stun_port.cc +++ b/third_party/libwebrtc/p2p/base/stun_port.cc @@ -19,12 +19,12 @@ #include "p2p/base/connection.h" #include "p2p/base/p2p_constants.h" #include "p2p/base/port_allocator.h" -#include "rtc_base/async_resolver_interface.h" #include "rtc_base/checks.h" #include "rtc_base/experiments/field_trial_parser.h" #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 { @@ -46,7 +46,9 @@ class StunBindingRequest : public StunRequest { std::make_unique<StunMessage>(STUN_BINDING_REQUEST)), port_(port), server_addr_(addr), - start_time_(start_time) {} + start_time_(start_time) { + SetAuthenticationRequired(false); + } const rtc::SocketAddress& server_addr() const { return server_addr_; } @@ -220,7 +222,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); @@ -340,12 +345,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; } @@ -387,26 +389,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<const char*>(packet.payload().data()), + packet.payload().size()); return; } - if (Connection* conn = GetConnection(remote_addr)) { - conn->OnReadPacket(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/third_party/libwebrtc/p2p/base/stun_port.h b/third_party/libwebrtc/p2p/base/stun_port.h index 3df725eaf9..a28983b040 100644 --- a/third_party/libwebrtc/p2p/base/stun_port.h +++ b/third_party/libwebrtc/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/third_party/libwebrtc/p2p/base/stun_port_unittest.cc b/third_party/libwebrtc/p2p/base/stun_port_unittest.cc index bf51151536..04505d26ff 100644 --- a/third_party/libwebrtc/p2p/base/stun_port_unittest.cc +++ b/third_party/libwebrtc/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" @@ -94,8 +96,10 @@ class StunPortTestBase : public ::testing::Test, public sigslot::has_slots<> { thread_(ss_.get()), network_(network), socket_factory_(ss_.get()), - stun_server_1_(cricket::TestStunServer::Create(ss_.get(), kStunAddr1)), - stun_server_2_(cricket::TestStunServer::Create(ss_.get(), kStunAddr2)), + stun_server_1_( + cricket::TestStunServer::Create(ss_.get(), kStunAddr1, thread_)), + stun_server_2_( + cricket::TestStunServer::Create(ss_.get(), kStunAddr2, thread_)), mdns_responder_provider_(new FakeMdnsResponderProvider()), done_(false), error_(false), @@ -160,7 +164,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 +185,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() { @@ -224,14 +228,16 @@ class StunPortTestBase : public ::testing::Test, public sigslot::has_slots<> { cricket::TestStunServer* stun_server_1() { return stun_server_1_.get(); } cricket::TestStunServer* stun_server_2() { return stun_server_2_.get(); } + rtc::AutoSocketServerThread& thread() { return thread_; } + private: std::unique_ptr<rtc::VirtualSocketServer> ss_; rtc::AutoSocketServerThread thread_; rtc::Network network_; rtc::BasicPacketSocketFactory socket_factory_; std::unique_ptr<cricket::UDPPort> stun_port_; - std::unique_ptr<cricket::TestStunServer> stun_server_1_; - std::unique_ptr<cricket::TestStunServer> stun_server_2_; + cricket::TestStunServer::StunServerPtr stun_server_1_; + cricket::TestStunServer::StunServerPtr stun_server_2_; std::unique_ptr<rtc::AsyncPacketSocket> socket_; std::unique_ptr<rtc::MdnsResponderProvider> mdns_responder_provider_; bool done_; @@ -618,12 +624,12 @@ class StunIPv6PortTestBase : public StunPortTestBase { kIPv6LocalAddr.ipaddr(), 128), kIPv6LocalAddr.ipaddr()) { - stun_server_ipv6_1_.reset( - cricket::TestStunServer::Create(ss(), kIPv6StunAddr1)); + stun_server_ipv6_1_ = + cricket::TestStunServer::Create(ss(), kIPv6StunAddr1, thread()); } protected: - std::unique_ptr<cricket::TestStunServer> stun_server_ipv6_1_; + cricket::TestStunServer::StunServerPtr stun_server_ipv6_1_; }; class StunIPv6PortTestWithRealClock : public StunIPv6PortTestBase {}; diff --git a/third_party/libwebrtc/p2p/base/stun_request.cc b/third_party/libwebrtc/p2p/base/stun_request.cc index 15c1e6a269..fc5d43dc81 100644 --- a/third_party/libwebrtc/p2p/base/stun_request.cc +++ b/third_party/libwebrtc/p2p/base/stun_request.cc @@ -57,6 +57,10 @@ void StunRequestManager::Send(StunRequest* request) { void StunRequestManager::SendDelayed(StunRequest* request, int delay) { RTC_DCHECK_RUN_ON(thread_); RTC_DCHECK_EQ(this, request->manager()); + RTC_DCHECK(!request->AuthenticationRequired() || + request->msg()->integrity() != + StunMessage::IntegrityStatus::kNotSet) + << "Sending request w/o integrity!"; auto [iter, was_inserted] = requests_.emplace(request->id(), absl::WrapUnique(request)); RTC_DCHECK(was_inserted); @@ -104,15 +108,23 @@ bool StunRequestManager::CheckResponse(StunMessage* msg) { StunRequest* request = iter->second.get(); // Now that we know the request, we can see if the response is - // integrity-protected or not. - // For some tests, the message integrity is not set in the request. - // Complain, and then don't check. + // integrity-protected or not. Some requests explicitly disables + // integrity checks using SetAuthenticationRequired. + // TODO(chromium:1177125): Remove below! + // And we suspect that for some tests, the message integrity is not set in the + // request. Complain, and then don't check. bool skip_integrity_checking = (request->msg()->integrity() == StunMessage::IntegrityStatus::kNotSet); - if (skip_integrity_checking) { + if (!request->AuthenticationRequired()) { + // This is a STUN_BINDING to from stun_port.cc or + // the initial (unauthenticated) TURN_ALLOCATE_REQUEST. + } else if (skip_integrity_checking) { + // TODO(chromium:1177125): Remove below! // This indicates lazy test writing (not adding integrity attribute). // Complain, but only in debug mode (while developing). - RTC_DLOG(LS_ERROR) + RTC_LOG(LS_ERROR) + << "CheckResponse called on a passwordless request. Fix test!"; + RTC_DCHECK(false) << "CheckResponse called on a passwordless request. Fix test!"; } else { if (msg->integrity() == StunMessage::IntegrityStatus::kNotSet) { @@ -190,7 +202,8 @@ bool StunRequestManager::CheckResponse(const char* data, size_t size) { // Parse the STUN message and continue processing as usual. - rtc::ByteBufferReader buf(data, size); + rtc::ByteBufferReader buf( + rtc::MakeArrayView(reinterpret_cast<const uint8_t*>(data), size)); std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew()); if (!response->Read(&buf)) { RTC_LOG(LS_WARNING) << "Failed to read STUN response " diff --git a/third_party/libwebrtc/p2p/base/stun_request.h b/third_party/libwebrtc/p2p/base/stun_request.h index 6e83be3830..f2fd7e5211 100644 --- a/third_party/libwebrtc/p2p/base/stun_request.h +++ b/third_party/libwebrtc/p2p/base/stun_request.h @@ -115,6 +115,12 @@ class StunRequest { // Time elapsed since last send (in ms) int Elapsed() const; + // Add method to explitly allow requests w/o password. + // - STUN_BINDINGs from StunPort to a stun server + // - The initial TURN_ALLOCATE_REQUEST + void SetAuthenticationRequired(bool val) { authentication_required_ = val; } + bool AuthenticationRequired() const { return authentication_required_; } + protected: friend class StunRequestManager; @@ -155,6 +161,7 @@ class StunRequest { bool timeout_ RTC_GUARDED_BY(network_thread()); webrtc::ScopedTaskSafety task_safety_{ webrtc::PendingTaskSafetyFlag::CreateDetachedInactive()}; + bool authentication_required_ = true; }; } // namespace cricket diff --git a/third_party/libwebrtc/p2p/base/stun_request_unittest.cc b/third_party/libwebrtc/p2p/base/stun_request_unittest.cc index 2f88ab1fd3..8ce89f8a00 100644 --- a/third_party/libwebrtc/p2p/base/stun_request_unittest.cc +++ b/third_party/libwebrtc/p2p/base/stun_request_unittest.cc @@ -79,7 +79,9 @@ class StunRequestThunker : public StunRequest { public: StunRequestThunker(StunRequestManager& manager, StunRequestTest* test) : StunRequest(manager, CreateStunMessage(STUN_BINDING_REQUEST)), - test_(test) {} + test_(test) { + SetAuthenticationRequired(false); + } std::unique_ptr<StunMessage> CreateResponseMessage(StunMessageType type) { return CreateStunMessage(type, msg()); diff --git a/third_party/libwebrtc/p2p/base/stun_server.cc b/third_party/libwebrtc/p2p/base/stun_server.cc index 7827a0bb81..e37c5baf26 100644 --- a/third_party/libwebrtc/p2p/base/stun_server.cc +++ b/third_party/libwebrtc/p2p/base/stun_server.cc @@ -14,42 +14,49 @@ #include <utility> #include "absl/strings/string_view.h" +#include "api/sequence_checker.h" +#include "rtc_base/async_packet_socket.h" #include "rtc_base/byte_buffer.h" #include "rtc_base/logging.h" +#include "rtc_base/network/received_packet.h" namespace cricket { StunServer::StunServer(rtc::AsyncUDPSocket* socket) : socket_(socket) { - socket_->SignalReadPacket.connect(this, &StunServer::OnPacket); + socket_->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + OnPacket(socket, packet); + }); } StunServer::~StunServer() { - socket_->SignalReadPacket.disconnect(this); + RTC_DCHECK_RUN_ON(&sequence_checker_); + socket_->DeregisterReceivedPacketCallback(); } void StunServer::OnPacket(rtc::AsyncPacketSocket* socket, - const char* buf, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& /* packet_time_us */) { + const rtc::ReceivedPacket& packet) { + RTC_DCHECK_RUN_ON(&sequence_checker_); // Parse the STUN message; eat any messages that fail to parse. - rtc::ByteBufferReader bbuf(buf, size); + rtc::ByteBufferReader bbuf(packet.payload()); StunMessage msg; if (!msg.Read(&bbuf)) { return; } - // TODO(?): If unknown non-optional (<= 0x7fff) attributes are found, send a + // TODO(?): If unknown non-optional (<= 0x7fff) attributes are found, + // send a // 420 "Unknown Attribute" response. // Send the message to the appropriate handler function. switch (msg.type()) { case STUN_BINDING_REQUEST: - OnBindingRequest(&msg, remote_addr); + OnBindingRequest(&msg, packet.source_address()); break; default: - SendErrorResponse(msg, remote_addr, 600, "Operation Not Supported"); + SendErrorResponse(msg, packet.source_address(), 600, + "Operation Not Supported"); } } diff --git a/third_party/libwebrtc/p2p/base/stun_server.h b/third_party/libwebrtc/p2p/base/stun_server.h index 505773b052..f6a776c5f7 100644 --- a/third_party/libwebrtc/p2p/base/stun_server.h +++ b/third_party/libwebrtc/p2p/base/stun_server.h @@ -12,7 +12,6 @@ #define P2P_BASE_STUN_SERVER_H_ #include <stddef.h> -#include <stdint.h> #include <memory> @@ -21,26 +20,22 @@ #include "rtc_base/async_packet_socket.h" #include "rtc_base/async_udp_socket.h" #include "rtc_base/socket_address.h" -#include "rtc_base/third_party/sigslot/sigslot.h" namespace cricket { const int STUN_SERVER_PORT = 3478; -class StunServer : public sigslot::has_slots<> { +class StunServer { public: // Creates a STUN server, which will listen on the given socket. explicit StunServer(rtc::AsyncUDPSocket* socket); // Removes the STUN server from the socket and deletes the socket. - ~StunServer() override; + virtual ~StunServer(); protected: - // Slot for Socket.PacketRead: + // Callback for packets from socket. void OnPacket(rtc::AsyncPacketSocket* socket, - const char* buf, - size_t size, - const rtc::SocketAddress& remote_addr, - const int64_t& packet_time_us); + const rtc::ReceivedPacket& packet); // Handlers for the different types of STUN/TURN requests: virtual void OnBindingRequest(StunMessage* msg, @@ -64,6 +59,7 @@ class StunServer : public sigslot::has_slots<> { StunMessage* response) const; private: + webrtc::SequenceChecker sequence_checker_; std::unique_ptr<rtc::AsyncUDPSocket> socket_; }; diff --git a/third_party/libwebrtc/p2p/base/stun_server_unittest.cc b/third_party/libwebrtc/p2p/base/stun_server_unittest.cc index 5d3f31fb98..e4ea30cba4 100644 --- a/third_party/libwebrtc/p2p/base/stun_server_unittest.cc +++ b/third_party/libwebrtc/p2p/base/stun_server_unittest.cc @@ -33,15 +33,13 @@ const rtc::SocketAddress client_addr("1.2.3.4", 1234); class StunServerTest : public ::testing::Test { public: - StunServerTest() : ss_(new rtc::VirtualSocketServer()), network_(ss_.get()) { + StunServerTest() : ss_(new rtc::VirtualSocketServer()) { + ss_->SetMessageQueue(&main_thread); server_.reset( new StunServer(rtc::AsyncUDPSocket::Create(ss_.get(), server_addr))); client_.reset(new rtc::TestClient( absl::WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr)))); - - network_.Start(); } - ~StunServerTest() override { network_.Stop(); } void Send(const StunMessage& msg) { rtc::ByteBufferWriter buf; @@ -57,7 +55,7 @@ class StunServerTest : public ::testing::Test { std::unique_ptr<rtc::TestClient::Packet> packet = client_->NextPacket(rtc::TestClient::kTimeoutMs); if (packet) { - rtc::ByteBufferReader buf(packet->buf, packet->size); + rtc::ByteBufferReader buf(packet->buf); msg = new StunMessage(); msg->Read(&buf); } @@ -67,7 +65,6 @@ class StunServerTest : public ::testing::Test { private: rtc::AutoThread main_thread; std::unique_ptr<rtc::VirtualSocketServer> ss_; - rtc::Thread network_; std::unique_ptr<StunServer> server_; std::unique_ptr<rtc::TestClient> client_; }; diff --git a/third_party/libwebrtc/p2p/base/tcp_port.cc b/third_party/libwebrtc/p2p/base/tcp_port.cc index 5f25624d00..ce61416979 100644 --- a/third_party/libwebrtc/p2p/base/tcp_port.cc +++ b/third_party/libwebrtc/p2p/base/tcp_port.cc @@ -81,7 +81,9 @@ #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" +#include "rtc_base/thread.h" namespace cricket { using ::webrtc::SafeTask; @@ -159,7 +161,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 +290,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 +331,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,13 +561,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(data, size, packet_time_us); + Connection::OnReadPacket(packet); } void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) { @@ -623,7 +622,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) { @@ -636,7 +638,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/third_party/libwebrtc/p2p/base/tcp_port.h b/third_party/libwebrtc/p2p/base/tcp_port.h index a1bbaa9f35..bd7ed4c110 100644 --- a/third_party/libwebrtc/p2p/base/tcp_port.h +++ b/third_party/libwebrtc/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/third_party/libwebrtc/p2p/base/test_stun_server.cc b/third_party/libwebrtc/p2p/base/test_stun_server.cc index d4c3b2d851..a8a5c46f8c 100644 --- a/third_party/libwebrtc/p2p/base/test_stun_server.cc +++ b/third_party/libwebrtc/p2p/base/test_stun_server.cc @@ -10,21 +10,32 @@ #include "p2p/base/test_stun_server.h" +#include <memory> + #include "rtc_base/socket.h" #include "rtc_base/socket_server.h" namespace cricket { -TestStunServer* TestStunServer::Create(rtc::SocketServer* ss, - const rtc::SocketAddress& addr) { +std::unique_ptr<TestStunServer, std::function<void(TestStunServer*)>> +TestStunServer::Create(rtc::SocketServer* ss, + const rtc::SocketAddress& addr, + rtc::Thread& network_thread) { rtc::Socket* socket = ss->CreateSocket(addr.family(), SOCK_DGRAM); rtc::AsyncUDPSocket* udp_socket = rtc::AsyncUDPSocket::Create(socket, addr); - - return new TestStunServer(udp_socket); + TestStunServer* server = nullptr; + network_thread.BlockingCall( + [&]() { server = new TestStunServer(udp_socket, network_thread); }); + std::unique_ptr<TestStunServer, std::function<void(TestStunServer*)>> result( + server, [&](TestStunServer* server) { + network_thread.BlockingCall([server]() { delete server; }); + }); + return result; } void TestStunServer::OnBindingRequest(StunMessage* msg, const rtc::SocketAddress& remote_addr) { + RTC_DCHECK_RUN_ON(&network_thread_); if (fake_stun_addr_.IsNil()) { StunServer::OnBindingRequest(msg, remote_addr); } else { diff --git a/third_party/libwebrtc/p2p/base/test_stun_server.h b/third_party/libwebrtc/p2p/base/test_stun_server.h index 11ac620bb8..7bf7dc1dba 100644 --- a/third_party/libwebrtc/p2p/base/test_stun_server.h +++ b/third_party/libwebrtc/p2p/base/test_stun_server.h @@ -11,19 +11,25 @@ #ifndef P2P_BASE_TEST_STUN_SERVER_H_ #define P2P_BASE_TEST_STUN_SERVER_H_ +#include <memory> + #include "api/transport/stun.h" #include "p2p/base/stun_server.h" #include "rtc_base/async_udp_socket.h" #include "rtc_base/socket_address.h" #include "rtc_base/socket_server.h" +#include "rtc_base/thread.h" namespace cricket { // A test STUN server. Useful for unit tests. class TestStunServer : StunServer { public: - static TestStunServer* Create(rtc::SocketServer* ss, - const rtc::SocketAddress& addr); + using StunServerPtr = + std::unique_ptr<TestStunServer, std::function<void(TestStunServer*)>>; + static StunServerPtr Create(rtc::SocketServer* ss, + const rtc::SocketAddress& addr, + rtc::Thread& network_thread); // Set a fake STUN address to return to the client. void set_fake_stun_addr(const rtc::SocketAddress& addr) { @@ -31,13 +37,17 @@ class TestStunServer : StunServer { } private: - explicit TestStunServer(rtc::AsyncUDPSocket* socket) : StunServer(socket) {} + static void DeleteOnNetworkThread(TestStunServer* server); + + TestStunServer(rtc::AsyncUDPSocket* socket, rtc::Thread& network_thread) + : StunServer(socket), network_thread_(network_thread) {} void OnBindingRequest(StunMessage* msg, const rtc::SocketAddress& remote_addr) override; private: rtc::SocketAddress fake_stun_addr_; + rtc::Thread& network_thread_; }; } // namespace cricket diff --git a/third_party/libwebrtc/p2p/base/turn_port.cc b/third_party/libwebrtc/p2p/base/turn_port.cc index 1b05d82b89..e6f5e77114 100644 --- a/third_party/libwebrtc/p2p/base/turn_port.cc +++ b/third_party/libwebrtc/p2p/base/turn_port.cc @@ -10,6 +10,7 @@ #include "p2p/base/turn_port.h" +#include <cstdint> #include <functional> #include <memory> #include <utility> @@ -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<const char*>(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, @@ -986,7 +991,8 @@ void TurnPort::HandleDataIndication(const char* data, size_t size, int64_t packet_time_us) { // Read in the message, and process according to RFC5766, Section 10.4. - rtc::ByteBufferReader buf(data, size); + rtc::ByteBufferReader buf( + rtc::MakeArrayView(reinterpret_cast<const uint8_t*>(data), size)); TurnMessage msg; if (!msg.Read(&buf)) { RTC_LOG(LS_WARNING) << ToString() @@ -1021,9 +1027,10 @@ void TurnPort::HandleDataIndication(const char* data, "peer address, addr: " << ext_addr.ToSensitiveString(); } - - DispatchPacket(data_attr->bytes(), data_attr->length(), ext_addr, PROTO_UDP, - packet_time_us); + // TODO(bugs.webrtc.org/14870): rebuild DispatchPacket to take an + // ArrayView<uint8_t> + DispatchPacket(reinterpret_cast<const char*>(data_attr->array_view().data()), + data_attr->length(), ext_addr, PROTO_UDP, packet_time_us); } void TurnPort::HandleChannelData(int channel_id, @@ -1073,10 +1080,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(data, size, packet_time_us); + conn->OnReadPacket(packet); } else { - Port::OnReadPacket(data, size, remote_addr, proto); + Port::OnReadPacket(packet, proto); } } @@ -1313,6 +1322,8 @@ TurnAllocateRequest::TurnAllocateRequest(TurnPort* port) message->AddAttribute(std::move(transport_attr)); if (!port_->hash().empty()) { port_->AddRequestAuthInfo(message); + } else { + SetAuthenticationRequired(false); } port_->MaybeAddTurnLoggingId(message); port_->TurnCustomizerMaybeModifyOutgoingStunMessage(message); diff --git a/third_party/libwebrtc/p2p/base/turn_port.h b/third_party/libwebrtc/p2p/base/turn_port.h index 8fa4607e51..686edaf595 100644 --- a/third_party/libwebrtc/p2p/base/turn_port.h +++ b/third_party/libwebrtc/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/third_party/libwebrtc/p2p/base/turn_port_unittest.cc b/third_party/libwebrtc/p2p/base/turn_port_unittest.cc index 55706e142b..e7efb5e594 100644 --- a/third_party/libwebrtc/p2p/base/turn_port_unittest.cc +++ b/third_party/libwebrtc/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 <cstdint> + +#include "api/array_view.h" +#include "rtc_base/network/received_packet.h" #if defined(WEBRTC_POSIX) #include <dirent.h> @@ -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; @@ -929,7 +932,7 @@ class TurnLoggingIdValidator : public StunMessageObserver { } } } - void ReceivedChannelData(const char* data, size_t size) override {} + void ReceivedChannelData(rtc::ArrayView<const uint8_t> packet) override {} private: const char* expect_val_; @@ -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 @@ -1722,14 +1727,14 @@ class MessageObserver : public StunMessageObserver { const StunByteStringAttribute* attr = msg->GetByteString(TestTurnCustomizer::STUN_ATTR_COUNTER); if (attr != nullptr && attr_counter_ != nullptr) { - rtc::ByteBufferReader buf(attr->bytes(), attr->length()); + rtc::ByteBufferReader buf(attr->array_view()); unsigned int val = ~0u; buf.ReadUInt32(&val); (*attr_counter_)++; } } - void ReceivedChannelData(const char* data, size_t size) override { + void ReceivedChannelData(rtc::ArrayView<const uint8_t> payload) override { if (channel_data_counter_ != nullptr) { (*channel_data_counter_)++; } diff --git a/third_party/libwebrtc/p2p/base/turn_server.cc b/third_party/libwebrtc/p2p/base/turn_server.cc index b362bfa5cd..3d633110a7 100644 --- a/third_party/libwebrtc/p2p/base/turn_server.cc +++ b/third_party/libwebrtc/p2p/base/turn_server.cc @@ -102,7 +102,11 @@ void TurnServer::AddInternalSocket(rtc::AsyncPacketSocket* socket, RTC_DCHECK_RUN_ON(thread_); RTC_DCHECK(server_sockets_.end() == server_sockets_.find(socket)); server_sockets_[socket] = proto; - socket->SignalReadPacket.connect(this, &TurnServer::OnInternalPacket); + socket->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + RTC_DCHECK_RUN_ON(thread_); + OnInternalPacket(socket, packet); + }); } void TurnServer::AddInternalServerSocket( @@ -163,39 +167,35 @@ void TurnServer::OnInternalSocketClose(rtc::AsyncPacketSocket* socket, } void TurnServer::OnInternalPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& addr, - const int64_t& /* packet_time_us */) { + const rtc::ReceivedPacket& packet) { RTC_DCHECK_RUN_ON(thread_); // Fail if the packet is too small to even contain a channel header. - if (size < TURN_CHANNEL_HEADER_SIZE) { + if (packet.payload().size() < TURN_CHANNEL_HEADER_SIZE) { return; } InternalSocketMap::iterator iter = server_sockets_.find(socket); RTC_DCHECK(iter != server_sockets_.end()); - TurnServerConnection conn(addr, iter->second, socket); - uint16_t msg_type = rtc::GetBE16(data); + TurnServerConnection conn(packet.source_address(), iter->second, socket); + uint16_t msg_type = rtc::GetBE16(packet.payload().data()); if (!IsTurnChannelData(msg_type)) { // This is a STUN message. - HandleStunMessage(&conn, data, size); + HandleStunMessage(&conn, packet.payload()); } else { // This is a channel message; let the allocation handle it. TurnServerAllocation* allocation = FindAllocation(&conn); if (allocation) { - allocation->HandleChannelData(data, size); + allocation->HandleChannelData(packet.payload()); } if (stun_message_observer_ != nullptr) { - stun_message_observer_->ReceivedChannelData(data, size); + stun_message_observer_->ReceivedChannelData(packet.payload()); } } } void TurnServer::HandleStunMessage(TurnServerConnection* conn, - const char* data, - size_t size) { + rtc::ArrayView<const uint8_t> payload) { TurnMessage msg; - rtc::ByteBufferReader buf(data, size); + rtc::ByteBufferReader buf(payload); if (!msg.Read(&buf) || (buf.Length() > 0)) { RTC_LOG(LS_WARNING) << "Received invalid STUN message"; return; @@ -231,7 +231,7 @@ void TurnServer::HandleStunMessage(TurnServerConnection* conn, // Ensure the message is authorized; only needed for requests. if (IsStunRequestType(msg.type())) { - if (!CheckAuthorization(conn, &msg, data, size, key)) { + if (!CheckAuthorization(conn, &msg, key)) { return; } } @@ -272,8 +272,6 @@ bool TurnServer::GetKey(const StunMessage* msg, std::string* key) { bool TurnServer::CheckAuthorization(TurnServerConnection* conn, StunMessage* msg, - const char* data, - size_t size, absl::string_view key) { // RFC 5389, 10.2.2. RTC_DCHECK(IsStunRequestType(msg->type())); @@ -516,7 +514,7 @@ void TurnServer::DestroyInternalSocket(rtc::AsyncPacketSocket* socket) { if (iter != server_sockets_.end()) { rtc::AsyncPacketSocket* socket = iter->first; socket->UnsubscribeCloseEvent(this); - socket->SignalReadPacket.disconnect(this); + socket->DeregisterReceivedPacketCallback(); server_sockets_.erase(iter); std::unique_ptr<rtc::AsyncPacketSocket> socket_to_delete = absl::WrapUnique(socket); @@ -561,8 +559,11 @@ TurnServerAllocation::TurnServerAllocation(TurnServer* server, conn_(conn), external_socket_(socket), key_(key) { - external_socket_->SignalReadPacket.connect( - this, &TurnServerAllocation::OnExternalPacket); + external_socket_->RegisterReceivedPacketCallback( + [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { + RTC_DCHECK_RUN_ON(thread_); + OnExternalPacket(socket, packet); + }); } TurnServerAllocation::~TurnServerAllocation() { @@ -669,8 +670,8 @@ void TurnServerAllocation::HandleSendIndication(const TurnMessage* msg) { // If a permission exists, send the data on to the peer. if (HasPermission(peer_attr->GetAddress().ipaddr())) { - SendExternal(data_attr->bytes(), data_attr->length(), - peer_attr->GetAddress()); + SendExternal(reinterpret_cast<char*>(data_attr->array_view().data()), + data_attr->length(), peer_attr->GetAddress()); } else { RTC_LOG(LS_WARNING) << ToString() << ": Received send indication without permission" @@ -758,14 +759,15 @@ void TurnServerAllocation::HandleChannelBindRequest(const TurnMessage* msg) { SendResponse(&response); } -void TurnServerAllocation::HandleChannelData(const char* data, size_t size) { +void TurnServerAllocation::HandleChannelData( + rtc::ArrayView<const uint8_t> payload) { // Extract the channel number from the data. - uint16_t channel_id = rtc::GetBE16(data); + uint16_t channel_id = rtc::GetBE16(payload.data()); auto channel = FindChannel(channel_id); if (channel != channels_.end()) { // Send the data to the peer address. - SendExternal(data + TURN_CHANNEL_HEADER_SIZE, - size - TURN_CHANNEL_HEADER_SIZE, channel->peer); + SendExternal(payload.data() + TURN_CHANNEL_HEADER_SIZE, + payload.size() - TURN_CHANNEL_HEADER_SIZE, channel->peer); } else { RTC_LOG(LS_WARNING) << ToString() << ": Received channel data for invalid channel, id=" @@ -773,34 +775,31 @@ void TurnServerAllocation::HandleChannelData(const char* data, size_t size) { } } -void TurnServerAllocation::OnExternalPacket( - rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& addr, - const int64_t& /* packet_time_us */) { +void TurnServerAllocation::OnExternalPacket(rtc::AsyncPacketSocket* socket, + const rtc::ReceivedPacket& packet) { RTC_DCHECK(external_socket_.get() == socket); - auto channel = FindChannel(addr); + auto channel = FindChannel(packet.source_address()); if (channel != channels_.end()) { // There is a channel bound to this address. Send as a channel message. rtc::ByteBufferWriter buf; buf.WriteUInt16(channel->id); - buf.WriteUInt16(static_cast<uint16_t>(size)); - buf.WriteBytes(data, size); + buf.WriteUInt16(static_cast<uint16_t>(packet.payload().size())); + buf.WriteBytes(reinterpret_cast<const char*>(packet.payload().data()), + packet.payload().size()); server_->Send(&conn_, buf); } else if (!server_->enable_permission_checks_ || - HasPermission(addr.ipaddr())) { + HasPermission(packet.source_address().ipaddr())) { // No channel, but a permission exists. Send as a data indication. TurnMessage msg(TURN_DATA_INDICATION); msg.AddAttribute(std::make_unique<StunXorAddressAttribute>( - STUN_ATTR_XOR_PEER_ADDRESS, addr)); - msg.AddAttribute( - std::make_unique<StunByteStringAttribute>(STUN_ATTR_DATA, data, size)); + STUN_ATTR_XOR_PEER_ADDRESS, packet.source_address())); + msg.AddAttribute(std::make_unique<StunByteStringAttribute>( + STUN_ATTR_DATA, packet.payload().data(), packet.payload().size())); server_->SendStun(&conn_, &msg); } else { RTC_LOG(LS_WARNING) << ToString() << ": Received external packet without permission, peer=" - << addr.ToSensitiveString(); + << packet.source_address().ToSensitiveString(); } } diff --git a/third_party/libwebrtc/p2p/base/turn_server.h b/third_party/libwebrtc/p2p/base/turn_server.h index e951d089af..be42338a3b 100644 --- a/third_party/libwebrtc/p2p/base/turn_server.h +++ b/third_party/libwebrtc/p2p/base/turn_server.h @@ -26,6 +26,7 @@ #include "api/units/time_delta.h" #include "p2p/base/port_interface.h" #include "rtc_base/async_packet_socket.h" +#include "rtc_base/network/received_packet.h" #include "rtc_base/socket_address.h" #include "rtc_base/ssl_adapter.h" #include "rtc_base/third_party/sigslot/sigslot.h" @@ -69,14 +70,14 @@ class TurnServerConnection { // handles TURN messages (via HandleTurnMessage) and channel data messages // (via HandleChannelData) for this allocation when received by the server. // The object informs the server when its lifetime timer expires. -class TurnServerAllocation : public sigslot::has_slots<> { +class TurnServerAllocation { public: TurnServerAllocation(TurnServer* server_, webrtc::TaskQueueBase* thread, const TurnServerConnection& conn, rtc::AsyncPacketSocket* server_socket, absl::string_view key); - ~TurnServerAllocation() override; + virtual ~TurnServerAllocation(); TurnServerConnection* conn() { return &conn_; } const std::string& key() const { return key_; } @@ -90,7 +91,7 @@ class TurnServerAllocation : public sigslot::has_slots<> { std::string ToString() const; void HandleTurnMessage(const TurnMessage* msg); - void HandleChannelData(const char* data, size_t size); + void HandleChannelData(rtc::ArrayView<const uint8_t> payload); private: struct Channel { @@ -114,10 +115,7 @@ class TurnServerAllocation : public sigslot::has_slots<> { void HandleChannelBindRequest(const TurnMessage* msg); void OnExternalPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& addr, - const int64_t& packet_time_us); + const rtc::ReceivedPacket& packet); static webrtc::TimeDelta ComputeLifetime(const TurnMessage& msg); bool HasPermission(const rtc::IPAddress& addr); @@ -171,7 +169,7 @@ class TurnRedirectInterface { class StunMessageObserver { public: virtual void ReceivedMessage(const TurnMessage* msg) = 0; - virtual void ReceivedChannelData(const char* data, size_t size) = 0; + virtual void ReceivedChannelData(rtc::ArrayView<const uint8_t> payload) = 0; virtual ~StunMessageObserver() {} }; @@ -266,14 +264,11 @@ class TurnServer : public sigslot::has_slots<> { private: // All private member functions and variables should have access restricted to // thread_. But compile-time annotations are missing for members access from - // TurnServerAllocation (via friend declaration), and the On* methods, which - // are called via sigslot. + // TurnServerAllocation (via friend declaration). + std::string GenerateNonce(int64_t now) const RTC_RUN_ON(thread_); void OnInternalPacket(rtc::AsyncPacketSocket* socket, - const char* data, - size_t size, - const rtc::SocketAddress& address, - const int64_t& packet_time_us); + const rtc::ReceivedPacket& packet) RTC_RUN_ON(thread_); void OnNewInternalConnection(rtc::Socket* socket); @@ -282,8 +277,8 @@ class TurnServer : public sigslot::has_slots<> { void OnInternalSocketClose(rtc::AsyncPacketSocket* socket, int err); void HandleStunMessage(TurnServerConnection* conn, - const char* data, - size_t size) RTC_RUN_ON(thread_); + rtc::ArrayView<const uint8_t> payload) + RTC_RUN_ON(thread_); void HandleBindingRequest(TurnServerConnection* conn, const StunMessage* msg) RTC_RUN_ON(thread_); void HandleAllocateRequest(TurnServerConnection* conn, @@ -293,8 +288,6 @@ class TurnServer : public sigslot::has_slots<> { bool GetKey(const StunMessage* msg, std::string* key) RTC_RUN_ON(thread_); bool CheckAuthorization(TurnServerConnection* conn, StunMessage* msg, - const char* data, - size_t size, absl::string_view key) RTC_RUN_ON(thread_); bool ValidateNonce(absl::string_view nonce) const RTC_RUN_ON(thread_); |