1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/socket/udp_client_socket.h"
6
7 #include "base/task/single_thread_task_runner.h"
8 #include "build/build_config.h"
9 #include "net/base/net_errors.h"
10 #include "net/base/network_change_notifier.h"
11 #include "net/traffic_annotation/network_traffic_annotation.h"
12
13 namespace net {
14
15 namespace {
16
CreateNetLogUDPConnectParams(const IPEndPoint & address,int net_error)17 base::Value::Dict CreateNetLogUDPConnectParams(const IPEndPoint& address,
18 int net_error) {
19 DCHECK_NE(ERR_IO_PENDING, net_error);
20 auto params = base::Value::Dict().Set("address", address.ToString());
21 if (net_error < 0) {
22 params.Set("net_error", net_error);
23 }
24 return params;
25 }
26
CreateNetLogUDPBindToNetworkParams(handles::NetworkHandle network,int net_error)27 base::Value::Dict CreateNetLogUDPBindToNetworkParams(
28 handles::NetworkHandle network,
29 int net_error) {
30 DCHECK_NE(ERR_IO_PENDING, net_error);
31 auto params = base::Value::Dict().Set("network", static_cast<int>(network));
32 if (net_error < 0) {
33 params.Set("net_error", net_error);
34 }
35 return params;
36 }
37
38 } // namespace
39
UDPClientSocket(DatagramSocket::BindType bind_type,net::NetLog * net_log,const net::NetLogSource & source,handles::NetworkHandle network)40 UDPClientSocket::UDPClientSocket(DatagramSocket::BindType bind_type,
41 net::NetLog* net_log,
42 const net::NetLogSource& source,
43 handles::NetworkHandle network)
44 : net_log_(
45 NetLogWithSource::Make(net_log, NetLogSourceType::UDP_CLIENT_SOCKET)),
46 socket_(bind_type, net_log, net_log_.source()),
47 connect_using_network_(network) {
48 net_log_.BeginEventReferencingSource(NetLogEventType::SOCKET_ALIVE, source);
49 }
50
UDPClientSocket(DatagramSocket::BindType bind_type,NetLogWithSource source_net_log,handles::NetworkHandle network)51 UDPClientSocket::UDPClientSocket(DatagramSocket::BindType bind_type,
52 NetLogWithSource source_net_log,
53 handles::NetworkHandle network)
54 : net_log_(NetLogWithSource::Make(source_net_log.net_log(),
55 NetLogSourceType::UDP_CLIENT_SOCKET)),
56 socket_(bind_type, net_log_),
57 connect_using_network_(network) {
58 net_log_.BeginEventReferencingSource(NetLogEventType::SOCKET_ALIVE,
59 source_net_log.source());
60 }
61
~UDPClientSocket()62 UDPClientSocket::~UDPClientSocket() {
63 net_log_.EndEvent(NetLogEventType::SOCKET_ALIVE);
64 }
65
Connect(const IPEndPoint & address)66 int UDPClientSocket::Connect(const IPEndPoint& address) {
67 CHECK(!connect_called_);
68 if (connect_using_network_ != handles::kInvalidNetworkHandle)
69 return ConnectUsingNetwork(connect_using_network_, address);
70
71 connect_called_ = true;
72 int rv = OK;
73 if (!adopted_opened_socket_) {
74 rv = socket_.Open(address.GetFamily());
75 net_log_.AddEventWithNetErrorCode(NetLogEventType::SOCKET_OPEN, rv);
76 }
77 if (rv != OK)
78 return rv;
79 rv = socket_.Connect(address);
80 net_log_.AddEvent(NetLogEventType::SOCKET_CONNECT,
81 [&] { return CreateNetLogUDPConnectParams(address, rv); });
82 return rv;
83 }
84
ConnectUsingNetwork(handles::NetworkHandle network,const IPEndPoint & address)85 int UDPClientSocket::ConnectUsingNetwork(handles::NetworkHandle network,
86 const IPEndPoint& address) {
87 CHECK(!connect_called_);
88 connect_called_ = true;
89 if (!NetworkChangeNotifier::AreNetworkHandlesSupported())
90 return ERR_NOT_IMPLEMENTED;
91 int rv = OK;
92 if (!adopted_opened_socket_) {
93 rv = socket_.Open(address.GetFamily());
94 net_log_.AddEventWithNetErrorCode(NetLogEventType::SOCKET_OPEN, rv);
95 }
96 if (rv != OK) {
97 return rv;
98 }
99 rv = socket_.BindToNetwork(network);
100 net_log_.AddEvent(NetLogEventType::SOCKET_BIND_TO_NETWORK, [&] {
101 return CreateNetLogUDPBindToNetworkParams(network, rv);
102 });
103 if (rv != OK)
104 return rv;
105 network_ = network;
106 rv = socket_.Connect(address);
107 net_log_.AddEvent(NetLogEventType::SOCKET_CONNECT,
108 [&] { return CreateNetLogUDPConnectParams(address, rv); });
109 return rv;
110 }
111
ConnectUsingDefaultNetwork(const IPEndPoint & address)112 int UDPClientSocket::ConnectUsingDefaultNetwork(const IPEndPoint& address) {
113 CHECK(!connect_called_);
114 connect_called_ = true;
115 if (!NetworkChangeNotifier::AreNetworkHandlesSupported())
116 return ERR_NOT_IMPLEMENTED;
117 int rv = OK;
118 if (!adopted_opened_socket_) {
119 rv = socket_.Open(address.GetFamily());
120 net_log_.AddEventWithNetErrorCode(NetLogEventType::SOCKET_OPEN, rv);
121 }
122 if (rv != OK)
123 return rv;
124 // Calling connect() will bind a socket to the default network, however there
125 // is no way to determine what network the socket got bound to. The
126 // alternative is to query what the default network is and bind the socket to
127 // that network explicitly, however this is racy because the default network
128 // can change in between when we query it and when we bind to it. This is
129 // rare but should be accounted for. Since changes of the default network
130 // should not come in quick succession, we can simply try again.
131 handles::NetworkHandle network;
132 for (int attempt = 0; attempt < 2; attempt++) {
133 network = NetworkChangeNotifier::GetDefaultNetwork();
134 if (network == handles::kInvalidNetworkHandle)
135 return ERR_INTERNET_DISCONNECTED;
136 rv = socket_.BindToNetwork(network);
137 net_log_.AddEvent(NetLogEventType::SOCKET_BIND_TO_NETWORK, [&] {
138 return CreateNetLogUDPBindToNetworkParams(network, rv);
139 });
140 // |network| may have disconnected between the call to GetDefaultNetwork()
141 // and the call to BindToNetwork(). Loop only if this is the case (|rv| will
142 // be ERR_NETWORK_CHANGED).
143 if (rv != ERR_NETWORK_CHANGED)
144 break;
145 }
146 if (rv != OK)
147 return rv;
148 network_ = network;
149 rv = socket_.Connect(address);
150 net_log_.AddEvent(NetLogEventType::SOCKET_CONNECT,
151 [&] { return CreateNetLogUDPConnectParams(address, rv); });
152 return rv;
153 }
154
ConnectAsync(const IPEndPoint & address,CompletionOnceCallback callback)155 int UDPClientSocket::ConnectAsync(const IPEndPoint& address,
156 CompletionOnceCallback callback) {
157 DCHECK(callback);
158 return Connect(address);
159 }
160
ConnectUsingNetworkAsync(handles::NetworkHandle network,const IPEndPoint & address,CompletionOnceCallback callback)161 int UDPClientSocket::ConnectUsingNetworkAsync(handles::NetworkHandle network,
162 const IPEndPoint& address,
163 CompletionOnceCallback callback) {
164 DCHECK(callback);
165 return ConnectUsingNetwork(network, address);
166 }
167
ConnectUsingDefaultNetworkAsync(const IPEndPoint & address,CompletionOnceCallback callback)168 int UDPClientSocket::ConnectUsingDefaultNetworkAsync(
169 const IPEndPoint& address,
170 CompletionOnceCallback callback) {
171 DCHECK(callback);
172 return ConnectUsingDefaultNetwork(address);
173 }
174
GetBoundNetwork() const175 handles::NetworkHandle UDPClientSocket::GetBoundNetwork() const {
176 return network_;
177 }
178
ApplySocketTag(const SocketTag & tag)179 void UDPClientSocket::ApplySocketTag(const SocketTag& tag) {
180 socket_.ApplySocketTag(tag);
181 }
182
Read(IOBuffer * buf,int buf_len,CompletionOnceCallback callback)183 int UDPClientSocket::Read(IOBuffer* buf,
184 int buf_len,
185 CompletionOnceCallback callback) {
186 return socket_.Read(buf, buf_len, std::move(callback));
187 }
188
Write(IOBuffer * buf,int buf_len,CompletionOnceCallback callback,const NetworkTrafficAnnotationTag & traffic_annotation)189 int UDPClientSocket::Write(
190 IOBuffer* buf,
191 int buf_len,
192 CompletionOnceCallback callback,
193 const NetworkTrafficAnnotationTag& traffic_annotation) {
194 return socket_.Write(buf, buf_len, std::move(callback), traffic_annotation);
195 }
196
Close()197 void UDPClientSocket::Close() {
198 socket_.Close();
199 adopted_opened_socket_ = false;
200 }
201
GetPeerAddress(IPEndPoint * address) const202 int UDPClientSocket::GetPeerAddress(IPEndPoint* address) const {
203 return socket_.GetPeerAddress(address);
204 }
205
GetLocalAddress(IPEndPoint * address) const206 int UDPClientSocket::GetLocalAddress(IPEndPoint* address) const {
207 return socket_.GetLocalAddress(address);
208 }
209
SetReceiveBufferSize(int32_t size)210 int UDPClientSocket::SetReceiveBufferSize(int32_t size) {
211 return socket_.SetReceiveBufferSize(size);
212 }
213
SetSendBufferSize(int32_t size)214 int UDPClientSocket::SetSendBufferSize(int32_t size) {
215 return socket_.SetSendBufferSize(size);
216 }
217
SetDoNotFragment()218 int UDPClientSocket::SetDoNotFragment() {
219 return socket_.SetDoNotFragment();
220 }
221
SetRecvTos()222 int UDPClientSocket::SetRecvTos() {
223 return socket_.SetRecvTos();
224 }
225
SetTos(DiffServCodePoint dscp,EcnCodePoint ecn)226 int UDPClientSocket::SetTos(DiffServCodePoint dscp, EcnCodePoint ecn) {
227 return socket_.SetTos(dscp, ecn);
228 }
229
SetMsgConfirm(bool confirm)230 void UDPClientSocket::SetMsgConfirm(bool confirm) {
231 socket_.SetMsgConfirm(confirm);
232 }
233
NetLog() const234 const NetLogWithSource& UDPClientSocket::NetLog() const {
235 return socket_.NetLog();
236 }
237
UseNonBlockingIO()238 void UDPClientSocket::UseNonBlockingIO() {
239 #if BUILDFLAG(IS_WIN)
240 socket_.UseNonBlockingIO();
241 #endif
242 }
243
SetMulticastInterface(uint32_t interface_index)244 int UDPClientSocket::SetMulticastInterface(uint32_t interface_index) {
245 return socket_.SetMulticastInterface(interface_index);
246 }
247
EnableRecvOptimization()248 void UDPClientSocket::EnableRecvOptimization() {
249 #if BUILDFLAG(IS_POSIX)
250 socket_.enable_experimental_recv_optimization();
251 #endif
252 }
253
SetIOSNetworkServiceType(int ios_network_service_type)254 void UDPClientSocket::SetIOSNetworkServiceType(int ios_network_service_type) {
255 #if BUILDFLAG(IS_POSIX)
256 socket_.SetIOSNetworkServiceType(ios_network_service_type);
257 #endif
258 }
259
AdoptOpenedSocket(AddressFamily address_family,SocketDescriptor socket)260 int UDPClientSocket::AdoptOpenedSocket(AddressFamily address_family,
261 SocketDescriptor socket) {
262 int rv = socket_.AdoptOpenedSocket(address_family, socket);
263 if (rv == OK) {
264 adopted_opened_socket_ = true;
265 }
266 return rv;
267 }
268
GetLastTos() const269 DscpAndEcn UDPClientSocket::GetLastTos() const {
270 return socket_.GetLastTos();
271 }
272
273 } // namespace net
274