1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/network/tcp_bound_socket.h"
#include <optional>
#include <utility>
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/log/net_log.h"
#include "net/socket/tcp_client_socket.h"
#include "net/socket/tcp_server_socket.h"
#include "net/socket/tcp_socket.h"
#include "services/network/socket_factory.h"
#include "services/network/tcp_connected_socket.h"
namespace network {
TCPBoundSocket::TCPBoundSocket(
SocketFactory* socket_factory,
net::NetLog* net_log,
const net::NetworkTrafficAnnotationTag& traffic_annotation)
: socket_factory_(socket_factory),
socket_(net::TCPSocket::Create(nullptr /*socket_performance_watcher*/,
net_log,
net::NetLogSource())),
traffic_annotation_(traffic_annotation) {}
TCPBoundSocket::~TCPBoundSocket() = default;
int TCPBoundSocket::Bind(const net::IPEndPoint& local_addr,
net::IPEndPoint* local_addr_out) {
bind_address_ = local_addr;
int result = socket_->Open(local_addr.GetFamily());
if (result != net::OK)
return result;
// This is primarily intended for use with server sockets.
result = socket_->SetDefaultOptionsForServer();
if (result != net::OK)
return result;
result = socket_->Bind(local_addr);
if (result != net::OK)
return result;
return socket_->GetLocalAddress(local_addr_out);
}
void TCPBoundSocket::Listen(
uint32_t backlog,
mojo::PendingReceiver<mojom::TCPServerSocket> receiver,
ListenCallback callback) {
DCHECK(socket_->IsValid());
if (!socket_) {
NOTREACHED();
}
int result = ListenInternal(backlog);
// Succeed or fail, pass the result back to the caller.
std::move(callback).Run(result);
// Tear down everything on error.
if (result != net::OK) {
socket_factory_->DestroyBoundSocket(receiver_id_);
return;
}
// On success, also set up the TCPServerSocket.
std::unique_ptr<TCPServerSocket> server_socket =
std::make_unique<TCPServerSocket>(
std::make_unique<net::TCPServerSocket>(std::move(socket_)), backlog,
socket_factory_, traffic_annotation_);
socket_factory_->OnBoundSocketListening(
receiver_id_, std::move(server_socket), std::move(receiver));
// The above call will have destroyed |this|.
}
void TCPBoundSocket::Connect(
const net::AddressList& remote_addr_list,
mojom::TCPConnectedSocketOptionsPtr tcp_connected_socket_options,
mojo::PendingReceiver<mojom::TCPConnectedSocket> receiver,
mojo::PendingRemote<mojom::SocketObserver> observer,
ConnectCallback callback) {
DCHECK(socket_->IsValid());
if (!socket_) {
NOTREACHED();
}
DCHECK(!connect_callback_);
DCHECK(!connected_socket_receiver_);
DCHECK(!connecting_socket_);
connected_socket_receiver_ = std::move(receiver);
connect_callback_ = std::move(callback);
// Create a TCPConnectedSocket and have it do the work of connecting and
// configuring the socket. This saves a bit of code, and reduces the number of
// tests this class needs, since it can rely on TCPConnectedSocket's tests for
// a lot of cases.
connecting_socket_ = std::make_unique<TCPConnectedSocket>(
std::move(observer), socket_->net_log().net_log(),
socket_factory_->tls_socket_factory(),
nullptr /* client_socket_factory */, traffic_annotation_);
connecting_socket_->ConnectWithSocket(
net::TCPClientSocket::CreateFromBoundSocket(
std::move(socket_), remote_addr_list, bind_address_, nullptr),
std::move(tcp_connected_socket_options),
base::BindOnce(&TCPBoundSocket::OnConnectComplete,
base::Unretained(this)));
}
void TCPBoundSocket::OnConnectComplete(
int result,
const std::optional<net::IPEndPoint>& local_addr,
const std::optional<net::IPEndPoint>& peer_addr,
mojo::ScopedDataPipeConsumerHandle receive_stream,
mojo::ScopedDataPipeProducerHandle send_stream) {
DCHECK(connecting_socket_);
DCHECK(connect_callback_);
std::move(connect_callback_)
.Run(result, local_addr, peer_addr, std::move(receive_stream),
std::move(send_stream));
if (result != net::OK) {
socket_factory_->DestroyBoundSocket(receiver_id_);
// The above call will have destroyed |this|.
return;
}
socket_factory_->OnBoundSocketConnected(
receiver_id_, std::move(connecting_socket_),
std::move(connected_socket_receiver_));
// The above call will have destroyed |this|.
}
int TCPBoundSocket::ListenInternal(int backlog) {
return socket_->Listen(backlog);
}
} // namespace network
|