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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/socket/udp_client_socket.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "net/base/net_errors.h"
#include "net/base/network_change_notifier.h"
#include "net/base/port_util.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace net {
namespace {
base::Value::Dict CreateNetLogUDPConnectParams(const IPEndPoint& address,
int net_error) {
DCHECK_NE(ERR_IO_PENDING, net_error);
auto params = base::Value::Dict().Set("address", address.ToString());
if (net_error < 0) {
params.Set("net_error", net_error);
}
return params;
}
base::Value::Dict CreateNetLogUDPBindToNetworkParams(
handles::NetworkHandle network,
int net_error) {
DCHECK_NE(ERR_IO_PENDING, net_error);
auto params = base::Value::Dict().Set("network", static_cast<int>(network));
if (net_error < 0) {
params.Set("net_error", net_error);
}
return params;
}
} // namespace
UDPClientSocket::UDPClientSocket(DatagramSocket::BindType bind_type,
net::NetLog* net_log,
const net::NetLogSource& source,
handles::NetworkHandle network)
: net_log_(
NetLogWithSource::Make(net_log, NetLogSourceType::UDP_CLIENT_SOCKET)),
socket_(bind_type, net_log, net_log_.source()),
connect_using_network_(network) {
net_log_.BeginEventReferencingSource(NetLogEventType::SOCKET_ALIVE, source);
}
UDPClientSocket::UDPClientSocket(DatagramSocket::BindType bind_type,
NetLogWithSource source_net_log,
handles::NetworkHandle network)
: net_log_(NetLogWithSource::Make(source_net_log.net_log(),
NetLogSourceType::UDP_CLIENT_SOCKET)),
socket_(bind_type, net_log_),
connect_using_network_(network) {
net_log_.BeginEventReferencingSource(NetLogEventType::SOCKET_ALIVE,
source_net_log.source());
}
UDPClientSocket::~UDPClientSocket() {
net_log_.EndEvent(NetLogEventType::SOCKET_ALIVE);
}
int UDPClientSocket::Connect(const IPEndPoint& address) {
CHECK(!connect_called_);
if (!IsPortAllowedForIpEndpoint(address)) {
return ERR_UNSAFE_PORT;
}
if (connect_using_network_ != handles::kInvalidNetworkHandle)
return ConnectUsingNetwork(connect_using_network_, address);
connect_called_ = true;
int rv = OK;
if (!adopted_opened_socket_) {
rv = socket_.Open(address.GetFamily());
net_log_.AddEventWithNetErrorCode(NetLogEventType::SOCKET_OPEN, rv);
}
if (rv != OK)
return rv;
rv = socket_.Connect(address);
net_log_.AddEvent(NetLogEventType::SOCKET_CONNECT,
[&] { return CreateNetLogUDPConnectParams(address, rv); });
return rv;
}
int UDPClientSocket::ConnectUsingNetwork(handles::NetworkHandle network,
const IPEndPoint& address) {
CHECK(!connect_called_);
connect_called_ = true;
if (!NetworkChangeNotifier::AreNetworkHandlesSupported())
return ERR_NOT_IMPLEMENTED;
int rv = OK;
if (!adopted_opened_socket_) {
rv = socket_.Open(address.GetFamily());
net_log_.AddEventWithNetErrorCode(NetLogEventType::SOCKET_OPEN, rv);
}
if (rv != OK) {
return rv;
}
rv = socket_.BindToNetwork(network);
net_log_.AddEvent(NetLogEventType::SOCKET_BIND_TO_NETWORK, [&] {
return CreateNetLogUDPBindToNetworkParams(network, rv);
});
if (rv != OK)
return rv;
network_ = network;
rv = socket_.Connect(address);
net_log_.AddEvent(NetLogEventType::SOCKET_CONNECT,
[&] { return CreateNetLogUDPConnectParams(address, rv); });
return rv;
}
int UDPClientSocket::ConnectUsingDefaultNetwork(const IPEndPoint& address) {
CHECK(!connect_called_);
connect_called_ = true;
if (!NetworkChangeNotifier::AreNetworkHandlesSupported())
return ERR_NOT_IMPLEMENTED;
int rv = OK;
if (!adopted_opened_socket_) {
rv = socket_.Open(address.GetFamily());
net_log_.AddEventWithNetErrorCode(NetLogEventType::SOCKET_OPEN, rv);
}
if (rv != OK)
return rv;
// Calling connect() will bind a socket to the default network, however there
// is no way to determine what network the socket got bound to. The
// alternative is to query what the default network is and bind the socket to
// that network explicitly, however this is racy because the default network
// can change in between when we query it and when we bind to it. This is
// rare but should be accounted for. Since changes of the default network
// should not come in quick succession, we can simply try again.
handles::NetworkHandle network;
for (int attempt = 0; attempt < 2; attempt++) {
network = NetworkChangeNotifier::GetDefaultNetwork();
if (network == handles::kInvalidNetworkHandle)
return ERR_INTERNET_DISCONNECTED;
rv = socket_.BindToNetwork(network);
net_log_.AddEvent(NetLogEventType::SOCKET_BIND_TO_NETWORK, [&] {
return CreateNetLogUDPBindToNetworkParams(network, rv);
});
// |network| may have disconnected between the call to GetDefaultNetwork()
// and the call to BindToNetwork(). Loop only if this is the case (|rv| will
// be ERR_NETWORK_CHANGED).
if (rv != ERR_NETWORK_CHANGED)
break;
}
if (rv != OK)
return rv;
network_ = network;
rv = socket_.Connect(address);
net_log_.AddEvent(NetLogEventType::SOCKET_CONNECT,
[&] { return CreateNetLogUDPConnectParams(address, rv); });
return rv;
}
int UDPClientSocket::ConnectAsync(const IPEndPoint& address,
CompletionOnceCallback callback) {
DCHECK(callback);
return Connect(address);
}
int UDPClientSocket::ConnectUsingNetworkAsync(handles::NetworkHandle network,
const IPEndPoint& address,
CompletionOnceCallback callback) {
DCHECK(callback);
return ConnectUsingNetwork(network, address);
}
int UDPClientSocket::ConnectUsingDefaultNetworkAsync(
const IPEndPoint& address,
CompletionOnceCallback callback) {
DCHECK(callback);
return ConnectUsingDefaultNetwork(address);
}
handles::NetworkHandle UDPClientSocket::GetBoundNetwork() const {
return network_;
}
void UDPClientSocket::ApplySocketTag(const SocketTag& tag) {
socket_.ApplySocketTag(tag);
}
int UDPClientSocket::Read(IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) {
return socket_.Read(buf, buf_len, std::move(callback));
}
int UDPClientSocket::Write(
IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback,
const NetworkTrafficAnnotationTag& traffic_annotation) {
return socket_.Write(buf, buf_len, std::move(callback), traffic_annotation);
}
void UDPClientSocket::Close() {
socket_.Close();
adopted_opened_socket_ = false;
}
int UDPClientSocket::GetPeerAddress(IPEndPoint* address) const {
return socket_.GetPeerAddress(address);
}
int UDPClientSocket::GetLocalAddress(IPEndPoint* address) const {
return socket_.GetLocalAddress(address);
}
int UDPClientSocket::SetReceiveBufferSize(int32_t size) {
return socket_.SetReceiveBufferSize(size);
}
int UDPClientSocket::SetSendBufferSize(int32_t size) {
return socket_.SetSendBufferSize(size);
}
int UDPClientSocket::SetDoNotFragment() {
return socket_.SetDoNotFragment();
}
int UDPClientSocket::SetRecvTos() {
return socket_.SetRecvTos();
}
int UDPClientSocket::SetTos(DiffServCodePoint dscp, EcnCodePoint ecn) {
return socket_.SetTos(dscp, ecn);
}
void UDPClientSocket::SetMsgConfirm(bool confirm) {
socket_.SetMsgConfirm(confirm);
}
const NetLogWithSource& UDPClientSocket::NetLog() const {
return socket_.NetLog();
}
void UDPClientSocket::UseNonBlockingIO() {
#if BUILDFLAG(IS_WIN)
socket_.UseNonBlockingIO();
#endif
}
int UDPClientSocket::SetMulticastInterface(uint32_t interface_index) {
return socket_.SetMulticastInterface(interface_index);
}
void UDPClientSocket::EnableRecvOptimization() {
#if BUILDFLAG(IS_POSIX)
socket_.enable_experimental_recv_optimization();
#endif
}
void UDPClientSocket::SetIOSNetworkServiceType(int ios_network_service_type) {
#if BUILDFLAG(IS_POSIX)
socket_.SetIOSNetworkServiceType(ios_network_service_type);
#endif
}
void UDPClientSocket::RegisterQuicConnectionClosePayload(
base::span<uint8_t> payload) {
#if BUILDFLAG(IS_POSIX)
socket_.RegisterQuicConnectionClosePayload(payload);
#endif
}
void UDPClientSocket::UnregisterQuicConnectionClosePayload() {
#if BUILDFLAG(IS_POSIX)
socket_.UnregisterQuicConnectionClosePayload();
#endif
}
int UDPClientSocket::AdoptOpenedSocket(AddressFamily address_family,
SocketDescriptor socket) {
int rv = socket_.AdoptOpenedSocket(address_family, socket);
if (rv == OK) {
adopted_opened_socket_ = true;
}
return rv;
}
DscpAndEcn UDPClientSocket::GetLastTos() const {
return socket_.GetLastTos();
}
} // namespace net
|