0

Spanify production code in services/network/p2p/

This largely involves migrating to the newer span-based net::IOBuffer
APIs.

Bug: 351564777
Change-Id: I88e9717ba51593e18d11f070b2ac9210a8dda01b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6506171
Commit-Queue: Harald Alvestrand <hta@chromium.org>
Reviewed-by: Harald Alvestrand <hta@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1456966}
This commit is contained in:
Matt Menke
2025-05-07 06:51:52 -07:00
committed by Chromium LUCI CQ
parent f2a1313fb3
commit 167bb9f9e9
3 changed files with 22 additions and 48 deletions

@ -2,11 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // 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 "services/network/p2p/socket_manager.h" #include "services/network/p2p/socket_manager.h"
#include <stddef.h> #include <stddef.h>
@ -270,8 +265,8 @@ void P2PSocketManager::DumpPacket(base::span<const uint8_t> packet,
NOTREACHED(); NOTREACHED();
} }
std::vector<uint8_t> header_buffer(rtp_packet.data(), std::vector<uint8_t> header_buffer(rtp_packet.begin(),
rtp_packet.data() + header_size); rtp_packet.begin() + header_size);
trusted_socket_manager_client_->DumpPacket(header_buffer, rtp_packet.size(), trusted_socket_manager_client_->DumpPacket(header_buffer, rtp_packet.size(),
incoming); incoming);
} }

@ -2,16 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // 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 "services/network/p2p/socket_tcp.h" #include "services/network/p2p/socket_tcp.h"
#include <stddef.h> #include <stddef.h>
#include <utility> #include <utility>
#include <vector>
#include "base/containers/span.h" #include "base/containers/span.h"
#include "base/containers/span_writer.h" #include "base/containers/span_writer.h"
@ -466,10 +462,11 @@ void P2PSocketTcp::DoSend(const net::IPEndPoint& to,
CHECK_EQ(writer.remaining(), 0u); CHECK_EQ(writer.remaining(), 0u);
} }
webrtc::ApplyPacketOptions( base::span<uint8_t> send_buffer_without_header =
send_buffer.buffer->bytes() + kPacketHeaderSize, send_buffer.buffer->span().subspan(kPacketHeaderSize);
send_buffer.buffer->BytesRemaining() - kPacketHeaderSize, webrtc::ApplyPacketOptions(send_buffer_without_header.data(),
options.packet_time_params, webrtc::TimeMicros()); send_buffer_without_header.size(),
options.packet_time_params, webrtc::TimeMicros());
WriteOrQueue(send_buffer); WriteOrQueue(send_buffer);
} }
@ -527,28 +524,25 @@ void P2PSocketStunTcp::DoSend(const net::IPEndPoint& to,
// Add any pad bytes to the total size. // Add any pad bytes to the total size.
int buffer_size = data.size() + pad_bytes; int buffer_size = data.size() + pad_bytes;
std::vector<uint8_t> buffer;
buffer.reserve(buffer_size);
buffer.assign(data.begin(), data.end());
if (pad_bytes) {
DCHECK_LE(pad_bytes, 4);
buffer.insert(buffer.end(), pad_bytes, 0);
}
SendBuffer send_buffer( SendBuffer send_buffer(
options.packet_id, options.packet_id,
base::MakeRefCounted<net::DrainableIOBuffer>( base::MakeRefCounted<net::DrainableIOBuffer>(
base::MakeRefCounted<net::IOBufferWithSize>(buffer_size), base::MakeRefCounted<net::VectorIOBuffer>(std::move(buffer)),
buffer_size)); buffer_size));
memcpy(send_buffer.buffer->data(), data.data(), data.size());
webrtc::ApplyPacketOptions(send_buffer.buffer->bytes(), data.size(), webrtc::ApplyPacketOptions(send_buffer.buffer->bytes(), data.size(),
options.packet_time_params, webrtc::TimeMicros()); options.packet_time_params, webrtc::TimeMicros());
if (pad_bytes) {
char padding[4] = {};
DCHECK_LE(pad_bytes, 4);
memcpy(send_buffer.buffer->data() + data.size(), padding, pad_bytes);
}
// WriteOrQueue may free the memory, so dump it first. // WriteOrQueue may free the memory, so dump it first.
delegate_->DumpPacket( delegate_->DumpPacket(send_buffer.buffer->span(), false);
base::span(reinterpret_cast<const uint8_t*>(send_buffer.buffer->data()),
data.size()),
false);
WriteOrQueue(send_buffer); WriteOrQueue(send_buffer);
} }

@ -2,11 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // 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 "services/network/p2p/socket_udp.h" #include "services/network/p2p/socket_udp.h"
#include <tuple> #include <tuple>
@ -115,12 +110,10 @@ P2PPendingPacket::P2PPendingPacket(
const webrtc::AsyncSocketPacketOptions& options, const webrtc::AsyncSocketPacketOptions& options,
uint64_t id) uint64_t id)
: to(to), : to(to),
data(base::MakeRefCounted<net::IOBufferWithSize>(content.size())), data(base::MakeRefCounted<net::VectorIOBuffer>(content)),
size(content.size()), size(content.size()),
packet_options(options), packet_options(options),
id(id) { id(id) {}
memcpy(data->data(), content.data(), content.size());
}
P2PPendingPacket::P2PPendingPacket(const P2PPendingPacket& other) = default; P2PPendingPacket::P2PPendingPacket(const P2PPendingPacket& other) = default;
P2PPendingPacket::~P2PPendingPacket() = default; P2PPendingPacket::~P2PPendingPacket() = default;
@ -298,9 +291,7 @@ void P2PSocketUdp::MaybeDrainReceivedPackets(bool force) {
bool P2PSocketUdp::HandleReadResult(int result) { bool P2PSocketUdp::HandleReadResult(int result) {
if (result > 0) { if (result > 0) {
auto data = auto data = recv_buffer_->first(static_cast<size_t>(result));
base::span(reinterpret_cast<const uint8_t*>(recv_buffer_->data()),
static_cast<size_t>(result));
if (!base::Contains(connected_peers_, recv_address_)) { if (!base::Contains(connected_peers_, recv_address_)) {
P2PSocket::StunMessageType type; P2PSocket::StunMessageType type;
@ -364,10 +355,7 @@ bool P2PSocketUdp::DoSend(const P2PPendingPacket& packet) {
// messages are sent in correct order. // messages are sent in correct order.
if (!base::Contains(connected_peers_, packet.to)) { if (!base::Contains(connected_peers_, packet.to)) {
P2PSocket::StunMessageType type = P2PSocket::StunMessageType(); P2PSocket::StunMessageType type = P2PSocket::StunMessageType();
bool stun = GetStunPacketType( bool stun = GetStunPacketType(packet.data->first(packet.size), &type);
base::span(reinterpret_cast<const uint8_t*>(packet.data->data()),
packet.size),
&type);
if (!stun || type == STUN_DATA_INDICATION) { if (!stun || type == STUN_DATA_INDICATION) {
LOG(ERROR) << "Page tried to send a data packet to " LOG(ERROR) << "Page tried to send a data packet to "
<< packet.to.ToString() << " before STUN binding is finished."; << packet.to.ToString() << " before STUN binding is finished.";
@ -437,10 +425,7 @@ bool P2PSocketUdp::DoSend(const P2PPendingPacket& packet) {
} }
} }
delegate_->DumpPacket( delegate_->DumpPacket(packet.data->first(packet.size), false);
base::span(reinterpret_cast<const uint8_t*>(packet.data->data()),
packet.size),
false);
return true; return true;
} }