File: brokered_udp_client_socket.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (334 lines) | stat: -rw-r--r-- 11,073 bytes parent folder | download | duplicates (6)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2023 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/brokered_udp_client_socket.h"

#include "base/component_export.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "mojo/public/cpp/platform/platform_handle.h"
#include "net/base/address_list.h"
#include "net/base/completion_once_callback.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_handle.h"
#include "net/log/net_log_source.h"
#include "net/nqe/network_quality_estimator.h"
#include "net/socket/datagram_client_socket.h"
#include "net/socket/datagram_socket.h"
#include "net/socket/socket_tag.h"
#include "net/socket/stream_socket.h"
#include "net/socket/udp_client_socket.h"
#include "net/socket/udp_socket.h"
#include "net/socket/udp_socket_global_limits.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/brokered_client_socket_factory.h"
#include "services/network/public/cpp/transferable_socket.h"

namespace network {

BrokeredUdpClientSocket::BrokeredUdpClientSocket(
    net::DatagramSocket::BindType bind_type,
    net::NetLog* net_log,
    const net::NetLogSource& source,
    BrokeredClientSocketFactory* client_socket_factory,
    net::handles::NetworkHandle network)
    : bind_type_(bind_type),
      network_(network),
      net_log_source_(
          net::NetLogWithSource::Make(net_log, net::NetLogSourceType::SOCKET)),
      client_socket_factory_(client_socket_factory) {
  net_log_source_.BeginEventReferencingSource(
      net::NetLogEventType::BROKERED_SOCKET_ALIVE, source);
}

BrokeredUdpClientSocket::~BrokeredUdpClientSocket() {
  net_log_source_.EndEvent(net::NetLogEventType::BROKERED_SOCKET_ALIVE);
}

int BrokeredUdpClientSocket::Connect(const net::IPEndPoint& address) {
  if (!client_socket_factory_->ShouldBroker(address.address())) {
    return ConnectInternal(address);
  }
  // Brokered sockets can only support asynchronous connections so this does not
  // need to be implemented. However, this path can still be hit if the sandbox
  // is enabled and a caller attempts to call a synchronous Connect. Callers are
  // expected to handle Connect failures themselves so we just return
  // ERR_NOT_IMPLEMENTED.
  return net::ERR_NOT_IMPLEMENTED;
}

int BrokeredUdpClientSocket::ConnectUsingNetwork(
    net::handles::NetworkHandle network,
    const net::IPEndPoint& address) {
  // NetworkHandles are not supported on Windows, so this method and the
  // following Connect*Network() methods don't need to return anything.
  return net::ERR_NOT_IMPLEMENTED;
}

int BrokeredUdpClientSocket::ConnectUsingDefaultNetwork(
    const net::IPEndPoint& address) {
  return net::ERR_NOT_IMPLEMENTED;
}

int BrokeredUdpClientSocket::ConnectAsync(
    const net::IPEndPoint& address,
    net::CompletionOnceCallback callback) {
  return ConnectAsyncInternal(address, std::move(callback));
}

int BrokeredUdpClientSocket::ConnectUsingNetworkAsync(
    net::handles::NetworkHandle network,
    const net::IPEndPoint& address,
    net::CompletionOnceCallback callback) {
  return net::ERR_NOT_IMPLEMENTED;
}

int BrokeredUdpClientSocket::ConnectUsingDefaultNetworkAsync(
    const net::IPEndPoint& address,
    net::CompletionOnceCallback callback) {
  return net::ERR_NOT_IMPLEMENTED;
}

int BrokeredUdpClientSocket::ConnectAsyncInternal(
    const net::IPEndPoint& address,
    net::CompletionOnceCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(callback);
  DCHECK(!socket_);
  CHECK(!connect_called_);
  connect_called_ = true;
  if (!client_socket_factory_->ShouldBroker(address.address())) {
    return DidCompleteCreate(/*should_broker=*/false, address,
                             std::move(callback), network::TransferableSocket(),
                             net::OK);
  }
  net_log_source_.BeginEvent(net::NetLogEventType::BROKERED_CREATE_SOCKET);
  client_socket_factory_->BrokerCreateUdpSocket(
      address.GetFamily(),
      base::BindOnce(
          base::IgnoreResult(&BrokeredUdpClientSocket::DidCompleteCreate),
          brokered_weak_ptr_factory_.GetWeakPtr(), /*should_broker=*/true,
          address, std::move(callback)));
  return net::ERR_IO_PENDING;
}

int BrokeredUdpClientSocket::ConnectInternal(const net::IPEndPoint& address) {
  socket_ = std::make_unique<net::UDPClientSocket>(bind_type_, net_log_source_,
                                                   network_);

  // These options must be set before opening a socket or adopting an opened
  // socket.
  if (use_non_blocking_io_) {
    socket_->UseNonBlockingIO();
  }
  if (recv_optimization_) {
    socket_->EnableRecvOptimization();
  }

  int set_multicast_rv = socket_->SetMulticastInterface(interface_index_);
  if (set_multicast_rv != net::OK) {
    return set_multicast_rv;
  }
  socket_->ApplySocketTag(tag_);
  socket_->SetMsgConfirm(set_msg_confirm_);

  int connect_rv = socket_->Connect(address);
  return connect_rv;
}

int BrokeredUdpClientSocket::DidCompleteCreate(
    bool should_broker,
    const net::IPEndPoint& address,
    net::CompletionOnceCallback callback,
    network::TransferableSocket socket,
    int result) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (should_broker) {
    net_log_source_.EndEventWithNetErrorCode(
        net::NetLogEventType::BROKERED_CREATE_SOCKET, result);
    if (result != net::OK) {
      std::move(callback).Run(result);
      return result;
    }
  }
  socket_ = std::make_unique<net::UDPClientSocket>(bind_type_, net_log_source_,
                                                   network_);
  // These options must be set before opening a socket or adopting an opened
  // socket.
  if (use_non_blocking_io_) {
    socket_->UseNonBlockingIO();
  }
  if (recv_optimization_) {
    socket_->EnableRecvOptimization();
  }

  if (should_broker) {
    int adopt_socket_rv =
        socket_->AdoptOpenedSocket(address.GetFamily(), socket.TakeSocket());
    if (adopt_socket_rv != net::OK) {
      Close();
      std::move(callback).Run(adopt_socket_rv);
      return adopt_socket_rv;
    }
  }

  int set_multicast_rv = socket_->SetMulticastInterface(interface_index_);
  if (set_multicast_rv != net::OK) {
    if (should_broker) {
      std::move(callback).Run(set_multicast_rv);
    }
    return set_multicast_rv;
  }
  socket_->ApplySocketTag(tag_);
  socket_->SetMsgConfirm(set_msg_confirm_);

  auto split_callback = base::SplitOnceCallback(std::move(callback));
  const int connect_rv =
      socket_->ConnectAsync(address, std::move(split_callback.first));
  if (should_broker && connect_rv != net::ERR_IO_PENDING) {
    std::move(split_callback.second).Run(connect_rv);
  }
  return connect_rv;
}

net::handles::NetworkHandle BrokeredUdpClientSocket::GetBoundNetwork() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!socket_) {
    return net::handles::kInvalidNetworkHandle;
  }
  return socket_->GetBoundNetwork();
}

void BrokeredUdpClientSocket::ApplySocketTag(const net::SocketTag& tag) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (socket_) {
    socket_->ApplySocketTag(tag);
  }
  tag_ = tag;
}

int BrokeredUdpClientSocket::Read(net::IOBuffer* buf,
                                  int buf_len,
                                  net::CompletionOnceCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!socket_) {
    return net::ERR_SOCKET_NOT_CONNECTED;
  }
  return socket_->Read(buf, buf_len, std::move(callback));
}

int BrokeredUdpClientSocket::Write(
    net::IOBuffer* buf,
    int buf_len,
    net::CompletionOnceCallback callback,
    const net::NetworkTrafficAnnotationTag& traffic_annotation) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!socket_) {
    return net::ERR_SOCKET_NOT_CONNECTED;
  }
  return socket_->Write(buf, buf_len, std::move(callback), traffic_annotation);
}

void BrokeredUdpClientSocket::Close() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  socket_.reset();
  brokered_weak_ptr_factory_.InvalidateWeakPtrs();
}

int BrokeredUdpClientSocket::GetPeerAddress(net::IPEndPoint* address) const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!socket_) {
    return net::ERR_SOCKET_NOT_CONNECTED;
  }
  return socket_->GetPeerAddress(address);
}

int BrokeredUdpClientSocket::GetLocalAddress(net::IPEndPoint* address) const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!socket_) {
    return net::ERR_SOCKET_NOT_CONNECTED;
  }
  return socket_->GetLocalAddress(address);
}

int BrokeredUdpClientSocket::SetReceiveBufferSize(int32_t size) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(socket_);
  return socket_->SetReceiveBufferSize(size);
}

int BrokeredUdpClientSocket::SetSendBufferSize(int32_t size) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(socket_);
  return socket_->SetSendBufferSize(size);
}

int BrokeredUdpClientSocket::SetDoNotFragment() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(socket_);
  return socket_->SetDoNotFragment();
}

int BrokeredUdpClientSocket::SetRecvTos() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(socket_);
  return socket_->SetRecvTos();
}

int BrokeredUdpClientSocket::SetTos(net::DiffServCodePoint dscp,
                                    net::EcnCodePoint ecn) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(socket_);
  return socket_->SetTos(dscp, ecn);
}

void BrokeredUdpClientSocket::SetMsgConfirm(bool confirm) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  set_msg_confirm_ = confirm;
}

const net::NetLogWithSource& BrokeredUdpClientSocket::NetLog() const {
  return net_log_source_;
}

void BrokeredUdpClientSocket::UseNonBlockingIO() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  use_non_blocking_io_ = true;
}

int BrokeredUdpClientSocket::SetMulticastInterface(uint32_t interface_index) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!socket_) {
    interface_index_ = interface_index;
    return net::OK;
  }
  return socket_->SetMulticastInterface(interface_index);
}

void BrokeredUdpClientSocket::EnableRecvOptimization() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  recv_optimization_ = true;
}

void BrokeredUdpClientSocket::SetIOSNetworkServiceType(
    int ios_network_service_type) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  socket_->SetIOSNetworkServiceType(ios_network_service_type);
}

net::DscpAndEcn BrokeredUdpClientSocket::GetLastTos() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!socket_) {
    return net::DscpAndEcn(net::DSCP_DEFAULT, net::ECN_DEFAULT);
  }
  return socket_->GetLastTos();
}

}  // namespace network