chromium/third_party/blink/renderer/platform/p2p/ipc_socket_factory.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "third_party/blink/renderer/platform/p2p/ipc_socket_factory.h"

#include <stddef.h>

#include <algorithm>
#include <list>
#include <memory>

#include "base/compiler_specific.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/task/bind_post_task.h"
#include "base/threading/thread_checker.h"
#include "base/trace_event/trace_event.h"
#include "base/unguessable_token.h"
#include "components/webrtc/net_address_utils.h"
#include "net/base/ip_address.h"
#include "net/base/port_util.h"
#include "third_party/blink/public/platform/modules/webrtc/webrtc_logging.h"
#include "third_party/blink/renderer/platform/heap/cross_thread_persistent.h"
#include "third_party/blink/renderer/platform/p2p/host_address_request.h"
#include "third_party/blink/renderer/platform/p2p/socket_client_delegate.h"
#include "third_party/blink/renderer/platform/p2p/socket_client_impl.h"
#include "third_party/blink/renderer/platform/p2p/socket_dispatcher.h"
#include "third_party/blink/renderer/platform/wtf/casting.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "third_party/webrtc/api/async_dns_resolver.h"
#include "third_party/webrtc/rtc_base/async_packet_socket.h"
#include "third_party/webrtc/rtc_base/network/received_packet.h"

namespace blink {

namespace {

const int kDefaultNonSetOptionValue =;

bool IsTcpClientSocket(network::P2PSocketType type) {}

bool JingleSocketOptionToP2PSocketOption(rtc::Socket::Option option,
                                         network::P2PSocketOption* ipc_option) {}

// 640KB, 10x max UDP packet size. This controls the maximum size we can write
// to the IPC buffer, which is consumed by the shared network service process.
//
// If this buffer is too small, we'll see more MaxPendingBytesWouldBlock
// events and text log entries from WebRTC (search for kSendErrorLogLimit).
// As is, rate limiting in the layer in WebRTC that calls this layer, isn't very
// sophisticated and the cost of being blocked by this limit can be quite high.
// After being blocked, this implementation will fire an event once bytes have
// been freed up, which is then fanned out to all potentially waiting writers.
// That can create a storm of calls to `Send[To]` which may then cause multiple
// blocking errors again, both wasting CPU and spamming the log.
// The network service is single threaded and shared with other render
// processes. So having this max value large enough to accommodate multiple
// buffers, allows for more efficient bulk processing and less back-and-forth
// synchronizing between the render processes and network service.
// See also: bugs.webrtc.org/9622 and crbug/856088.
const size_t kDefaultMaximumInFlightBytes =;

// IpcPacketSocket implements rtc::AsyncPacketSocket interface
// using P2PSocketClient that works over IPC-channel. It must be used
// on the thread it was created.
class IpcPacketSocket : public rtc::AsyncPacketSocket,
                        public blink::P2PSocketClientDelegate {};

// Simple wrapper around P2PAsyncAddressResolver. The main purpose of this
// class is to call the right callback after OnDone callback from
// P2PAsyncAddressResolver, and keep track of the result.
// Thread jumping is handled by P2PAsyncAddressResolver.
class AsyncDnsAddressResolverImpl : public webrtc::AsyncDnsResolverInterface,
                                    public webrtc::AsyncDnsResolverResult {};

IpcPacketSocket::IpcPacketSocket()
    :{}

IpcPacketSocket::~IpcPacketSocket() {}

void IpcPacketSocket::TraceSendThrottlingState() const {}

void IpcPacketSocket::IncrementDiscardCounters(size_t bytes_discarded) {}

bool IpcPacketSocket::Init(
    P2PSocketDispatcher* dispatcher,
    const net::NetworkTrafficAnnotationTag& traffic_annotation,
    network::P2PSocketType type,
    std::unique_ptr<P2PSocketClientImpl> client,
    const rtc::SocketAddress& local_address,
    uint16_t min_port,
    uint16_t max_port,
    const rtc::SocketAddress& remote_address,
    WTF::CrossThreadFunction<
        void(base::OnceCallback<void(std::optional<base::UnguessableToken>)>)>&
        devtools_token_getter) {}

void IpcPacketSocket::DoCreateSocket(
    network::P2PSocketType type,
    P2PSocketDispatcher* dispatcher,
    net::IPEndPoint local_endpoint,
    uint16_t min_port,
    uint16_t max_port,
    network::P2PHostAndIPEndPoint remote_info,
    net::NetworkTrafficAnnotationTag traffic_annotation,
    mojo::PendingRemote<network::mojom::blink::P2PSocketClient> remote,
    mojo::PendingReceiver<network::mojom::blink::P2PSocket> receiver,
    std::optional<base::UnguessableToken> devtools_token) {}

// rtc::AsyncPacketSocket interface.
rtc::SocketAddress IpcPacketSocket::GetLocalAddress() const {}

rtc::SocketAddress IpcPacketSocket::GetRemoteAddress() const {}

int IpcPacketSocket::Send(const void* data,
                          size_t data_size,
                          const rtc::PacketOptions& options) {}

int IpcPacketSocket::SendTo(const void* data,
                            size_t data_size,
                            const rtc::SocketAddress& address,
                            const rtc::PacketOptions& options) {}

int IpcPacketSocket::SendToInternal(const void* data,
                                    size_t data_size,
                                    const rtc::SocketAddress& address,
                                    const rtc::PacketOptions& options) {}

int IpcPacketSocket::Close() {}

rtc::AsyncPacketSocket::State IpcPacketSocket::GetState() const {}

int IpcPacketSocket::GetOption(rtc::Socket::Option option, int* value) {}

int IpcPacketSocket::SetOption(rtc::Socket::Option option, int value) {}

int IpcPacketSocket::DoSetOption(network::P2PSocketOption option, int value) {}

int IpcPacketSocket::GetError() const {}

void IpcPacketSocket::SetError(int error) {}

void IpcPacketSocket::OnOpen(const net::IPEndPoint& local_address,
                             const net::IPEndPoint& remote_address) {}

void IpcPacketSocket::OnSendComplete(
    const network::P2PSendPacketMetrics& send_metrics) {}

void IpcPacketSocket::OnError() {}

void IpcPacketSocket::OnDataReceived(const net::IPEndPoint& address,
                                     base::span<const uint8_t> data,
                                     const base::TimeTicks& timestamp,
                                     rtc::EcnMarking ecn) {}

AsyncDnsAddressResolverImpl::AsyncDnsAddressResolverImpl(
    P2PSocketDispatcher* dispatcher)
    :{}

AsyncDnsAddressResolverImpl::~AsyncDnsAddressResolverImpl() {}

void AsyncDnsAddressResolverImpl::Start(const rtc::SocketAddress& addr,
                                        absl::AnyInvocable<void()> callback) {}

void AsyncDnsAddressResolverImpl::Start(const rtc::SocketAddress& addr,
                                        int address_family,
                                        absl::AnyInvocable<void()> callback) {}

bool AsyncDnsAddressResolverImpl::GetResolvedAddress(
    int family,
    rtc::SocketAddress* addr) const {}

int AsyncDnsAddressResolverImpl::GetError() const {}

void AsyncDnsAddressResolverImpl::OnAddressResolved(
    const Vector<net::IPAddress>& addresses) {}

}  // namespace

IpcPacketSocketFactory::IpcPacketSocketFactory(
    WTF::CrossThreadFunction<
        void(base::OnceCallback<void(std::optional<base::UnguessableToken>)>)>
        devtools_token_getter,
    P2PSocketDispatcher* socket_dispatcher,
    const net::NetworkTrafficAnnotationTag& traffic_annotation,
    bool batch_udp_packets)
    :{}

IpcPacketSocketFactory::~IpcPacketSocketFactory() {}

rtc::AsyncPacketSocket* IpcPacketSocketFactory::CreateUdpSocket(
    const rtc::SocketAddress& local_address,
    uint16_t min_port,
    uint16_t max_port) {}

rtc::AsyncListenSocket* IpcPacketSocketFactory::CreateServerTcpSocket(
    const rtc::SocketAddress& local_address,
    uint16_t min_port,
    uint16_t max_port,
    int opts) {}

rtc::AsyncPacketSocket* IpcPacketSocketFactory::CreateClientTcpSocket(
    const rtc::SocketAddress& local_address,
    const rtc::SocketAddress& remote_address,
    const rtc::PacketSocketTcpOptions& opts) {}

std::unique_ptr<webrtc::AsyncDnsResolverInterface>
IpcPacketSocketFactory::CreateAsyncDnsResolver() {}

}  // namespace blink