OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
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 "mojo/services/network/udp_socket_impl.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 #include <string.h> | |
10 #include <algorithm> | |
11 #include <limits> | |
12 #include <utility> | |
13 | |
14 #include "base/logging.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/stl_util.h" | |
17 #include "mojo/services/network/net_adapters.h" | |
18 #include "mojo/services/network/net_address_type_converters.h" | |
19 #include "net/base/io_buffer.h" | |
20 #include "net/base/net_errors.h" | |
21 #include "net/base/rand_callback.h" | |
22 #include "net/udp/datagram_socket.h" | |
23 | |
24 namespace mojo { | |
25 | |
26 namespace { | |
27 | |
28 const int kMaxReadSize = 128 * 1024; | |
29 const size_t kMaxWriteSize = 128 * 1024; | |
30 const size_t kMaxPendingSendRequestsUpperbound = 128; | |
31 const size_t kDefaultMaxPendingSendRequests = 32; | |
32 | |
33 } // namespace | |
34 | |
35 UDPSocketImpl::PendingSendRequest::PendingSendRequest() {} | |
36 | |
37 UDPSocketImpl::PendingSendRequest::~PendingSendRequest() {} | |
38 | |
39 UDPSocketImpl::UDPSocketImpl(InterfaceRequest<UDPSocket> request, | |
40 scoped_ptr<mojo::MessageLoopRef> app_refcount) | |
41 : binding_(this, std::move(request)), | |
42 socket_(net::DatagramSocket::DEFAULT_BIND, | |
43 net::RandIntCallback(), | |
44 nullptr, | |
45 net::NetLog::Source()), | |
46 state_(NOT_BOUND_OR_CONNECTED), | |
47 allow_address_reuse_(false), | |
48 remaining_recv_slots_(0), | |
49 max_pending_send_requests_(kDefaultMaxPendingSendRequests), | |
50 app_refcount_(std::move(app_refcount)) {} | |
51 | |
52 UDPSocketImpl::~UDPSocketImpl() { | |
53 STLDeleteElements(&pending_send_requests_); | |
54 } | |
55 | |
56 void UDPSocketImpl::AllowAddressReuse( | |
57 const Callback<void(NetworkErrorPtr)>& callback) { | |
58 if (IsBoundOrConnected()) { | |
59 callback.Run(MakeNetworkError(net::ERR_FAILED)); | |
60 return; | |
61 } | |
62 | |
63 allow_address_reuse_ = true; | |
64 callback.Run(MakeNetworkError(net::OK)); | |
65 } | |
66 | |
67 void UDPSocketImpl::Bind( | |
68 NetAddressPtr addr, | |
69 const Callback<void(NetworkErrorPtr, | |
70 NetAddressPtr, | |
71 InterfaceRequest<UDPSocketReceiver>)>& callback) { | |
72 int net_result = net::OK; | |
73 bool opened = false; | |
74 | |
75 do { | |
76 if (IsBoundOrConnected()) { | |
77 net_result = net::ERR_FAILED; | |
78 break; | |
79 } | |
80 | |
81 net::IPEndPoint ip_end_point = addr.To<net::IPEndPoint>(); | |
82 if (ip_end_point.GetFamily() == net::ADDRESS_FAMILY_UNSPECIFIED) { | |
83 net_result = net::ERR_ADDRESS_INVALID; | |
84 break; | |
85 } | |
86 | |
87 net_result = socket_.Open(ip_end_point.GetFamily()); | |
88 if (net_result != net::OK) | |
89 break; | |
90 opened = true; | |
91 | |
92 if (allow_address_reuse_) { | |
93 net_result = socket_.AllowAddressReuse(); | |
94 if (net_result != net::OK) | |
95 break; | |
96 } | |
97 | |
98 net_result = socket_.Bind(ip_end_point); | |
99 if (net_result != net::OK) | |
100 break; | |
101 | |
102 net::IPEndPoint bound_ip_end_point; | |
103 net_result = socket_.GetLocalAddress(&bound_ip_end_point); | |
104 if (net_result != net::OK) | |
105 break; | |
106 | |
107 state_ = BOUND; | |
108 callback.Run(MakeNetworkError(net_result), | |
109 NetAddress::From(bound_ip_end_point), GetProxy(&receiver_)); | |
110 | |
111 if (remaining_recv_slots_ > 0) { | |
112 DCHECK(!recvfrom_buffer_.get()); | |
113 DoRecvFrom(); | |
114 } | |
115 return; | |
116 } while (false); | |
117 | |
118 DCHECK(net_result != net::OK); | |
119 if (opened) | |
120 socket_.Close(); | |
121 callback.Run(MakeNetworkError(net_result), nullptr, nullptr); | |
122 } | |
123 | |
124 void UDPSocketImpl::Connect( | |
125 NetAddressPtr remote_addr, | |
126 const Callback<void(NetworkErrorPtr, | |
127 NetAddressPtr, | |
128 InterfaceRequest<UDPSocketReceiver>)>& callback) { | |
129 int net_result = net::OK; | |
130 bool opened = false; | |
131 | |
132 do { | |
133 if (IsBoundOrConnected()) { | |
134 net_result = net::ERR_FAILED; | |
135 break; | |
136 } | |
137 | |
138 net::IPEndPoint ip_end_point = remote_addr.To<net::IPEndPoint>(); | |
139 if (ip_end_point.GetFamily() == net::ADDRESS_FAMILY_UNSPECIFIED) { | |
140 net_result = net::ERR_ADDRESS_INVALID; | |
141 break; | |
142 } | |
143 | |
144 net_result = socket_.Open(ip_end_point.GetFamily()); | |
145 if (net_result != net::OK) | |
146 break; | |
147 opened = true; | |
148 | |
149 net_result = socket_.Connect(ip_end_point); | |
150 if (net_result != net::OK) | |
151 break; | |
152 | |
153 net::IPEndPoint local_ip_end_point; | |
154 net_result = socket_.GetLocalAddress(&local_ip_end_point); | |
155 if (net_result != net::OK) | |
156 break; | |
157 | |
158 state_ = CONNECTED; | |
159 callback.Run(MakeNetworkError(net_result), | |
160 NetAddress::From(local_ip_end_point), GetProxy(&receiver_)); | |
161 | |
162 if (remaining_recv_slots_ > 0) { | |
163 DCHECK(!recvfrom_buffer_.get()); | |
164 DoRecvFrom(); | |
165 } | |
166 return; | |
167 } while (false); | |
168 | |
169 DCHECK(net_result != net::OK); | |
170 if (opened) | |
171 socket_.Close(); | |
172 callback.Run(MakeNetworkError(net_result), nullptr, nullptr); | |
173 } | |
174 | |
175 void UDPSocketImpl::SetSendBufferSize( | |
176 uint32_t size, | |
177 const Callback<void(NetworkErrorPtr)>& callback) { | |
178 if (!IsBoundOrConnected()) { | |
179 callback.Run(MakeNetworkError(net::ERR_FAILED)); | |
180 return; | |
181 } | |
182 | |
183 if (size > static_cast<uint32_t>(std::numeric_limits<int32_t>::max())) | |
184 size = std::numeric_limits<int32_t>::max(); | |
185 | |
186 int net_result = socket_.SetSendBufferSize(static_cast<int32_t>(size)); | |
187 callback.Run(MakeNetworkError(net_result)); | |
188 } | |
189 | |
190 void UDPSocketImpl::SetReceiveBufferSize( | |
191 uint32_t size, | |
192 const Callback<void(NetworkErrorPtr)>& callback) { | |
193 if (!IsBoundOrConnected()) { | |
194 callback.Run(MakeNetworkError(net::ERR_FAILED)); | |
195 return; | |
196 } | |
197 | |
198 if (size > static_cast<uint32_t>(std::numeric_limits<int32_t>::max())) | |
199 size = std::numeric_limits<int32_t>::max(); | |
200 | |
201 int net_result = socket_.SetReceiveBufferSize(static_cast<int32_t>(size)); | |
202 callback.Run(MakeNetworkError(net_result)); | |
203 } | |
204 | |
205 void UDPSocketImpl::NegotiateMaxPendingSendRequests( | |
206 uint32_t requested_size, | |
207 const Callback<void(uint32_t)>& callback) { | |
208 if (requested_size != 0) { | |
209 max_pending_send_requests_ = | |
210 std::min(kMaxPendingSendRequestsUpperbound, | |
211 static_cast<size_t>(requested_size)); | |
212 } | |
213 callback.Run(static_cast<uint32_t>(max_pending_send_requests_)); | |
214 | |
215 if (pending_send_requests_.size() > max_pending_send_requests_) { | |
216 std::deque<PendingSendRequest*> discarded_requests( | |
217 pending_send_requests_.begin() + max_pending_send_requests_, | |
218 pending_send_requests_.end()); | |
219 pending_send_requests_.resize(max_pending_send_requests_); | |
220 for (auto& discarded_request : discarded_requests) { | |
221 discarded_request->callback.Run( | |
222 MakeNetworkError(net::ERR_INSUFFICIENT_RESOURCES)); | |
223 delete discarded_request; | |
224 } | |
225 } | |
226 } | |
227 | |
228 void UDPSocketImpl::ReceiveMore(uint32_t datagram_number) { | |
229 if (!receiver_) | |
230 return; | |
231 if (datagram_number == 0) | |
232 return; | |
233 if (std::numeric_limits<size_t>::max() - remaining_recv_slots_ < | |
234 datagram_number) { | |
235 return; | |
236 } | |
237 | |
238 remaining_recv_slots_ += datagram_number; | |
239 | |
240 if (IsBoundOrConnected() && !recvfrom_buffer_.get()) { | |
241 DCHECK_EQ(datagram_number, remaining_recv_slots_); | |
242 DoRecvFrom(); | |
243 } | |
244 } | |
245 | |
246 void UDPSocketImpl::SendTo(NetAddressPtr dest_addr, | |
247 Array<uint8_t> data, | |
248 const Callback<void(NetworkErrorPtr)>& callback) { | |
249 if (!IsBoundOrConnected()) { | |
250 callback.Run(MakeNetworkError(net::ERR_FAILED)); | |
251 return; | |
252 } | |
253 if (state_ == BOUND && !dest_addr) { | |
254 callback.Run(MakeNetworkError(net::ERR_INVALID_ARGUMENT)); | |
255 return; | |
256 } | |
257 | |
258 if (sendto_buffer_.get()) { | |
259 if (pending_send_requests_.size() >= max_pending_send_requests_) { | |
260 callback.Run(MakeNetworkError(net::ERR_INSUFFICIENT_RESOURCES)); | |
261 return; | |
262 } | |
263 | |
264 PendingSendRequest* request = new PendingSendRequest; | |
265 request->addr = std::move(dest_addr); | |
266 request->data = std::move(data); | |
267 request->callback = callback; | |
268 pending_send_requests_.push_back(request); | |
269 return; | |
270 } | |
271 | |
272 DCHECK_EQ(0u, pending_send_requests_.size()); | |
273 | |
274 DoSendTo(std::move(dest_addr), std::move(data), callback); | |
275 } | |
276 | |
277 void UDPSocketImpl::DoRecvFrom() { | |
278 DCHECK(IsBoundOrConnected()); | |
279 DCHECK(receiver_); | |
280 DCHECK(!recvfrom_buffer_.get()); | |
281 DCHECK_GT(remaining_recv_slots_, 0u); | |
282 | |
283 recvfrom_buffer_ = new net::IOBuffer(kMaxReadSize); | |
284 | |
285 // It is safe to use base::Unretained(this) because |socket_| is owned by this | |
286 // object. If this object gets destroyed (and so does |socket_|), the callback | |
287 // won't be called. | |
288 int net_result = socket_.RecvFrom( | |
289 recvfrom_buffer_.get(), | |
290 kMaxReadSize, | |
291 state_ == BOUND ? &recvfrom_address_ : nullptr, | |
292 base::Bind(&UDPSocketImpl::OnRecvFromCompleted, base::Unretained(this))); | |
293 if (net_result != net::ERR_IO_PENDING) | |
294 OnRecvFromCompleted(net_result); | |
295 } | |
296 | |
297 void UDPSocketImpl::DoSendTo(NetAddressPtr addr, | |
298 Array<uint8_t> data, | |
299 const Callback<void(NetworkErrorPtr)>& callback) { | |
300 DCHECK(IsBoundOrConnected()); | |
301 DCHECK(!sendto_buffer_.get()); | |
302 | |
303 if (data.size() > kMaxWriteSize) { | |
304 callback.Run(MakeNetworkError(net::ERR_INVALID_ARGUMENT)); | |
305 return; | |
306 } | |
307 sendto_buffer_ = new net::IOBufferWithSize(static_cast<int>(data.size())); | |
308 if (data.size() > 0) | |
309 memcpy(sendto_buffer_->data(), &data.storage()[0], data.size()); | |
310 | |
311 int net_result = net::OK; | |
312 if (addr) { | |
313 net::IPEndPoint ip_end_point = addr.To<net::IPEndPoint>(); | |
314 if (ip_end_point.GetFamily() == net::ADDRESS_FAMILY_UNSPECIFIED) { | |
315 callback.Run(MakeNetworkError(net::ERR_ADDRESS_INVALID)); | |
316 return; | |
317 } | |
318 | |
319 // It is safe to use base::Unretained(this) because |socket_| is owned by | |
320 // this object. If this object gets destroyed (and so does |socket_|), the | |
321 // callback won't be called. | |
322 net_result = socket_.SendTo(sendto_buffer_.get(), sendto_buffer_->size(), | |
323 ip_end_point, | |
324 base::Bind(&UDPSocketImpl::OnSendToCompleted, | |
325 base::Unretained(this), callback)); | |
326 } else { | |
327 DCHECK(state_ == CONNECTED); | |
328 net_result = socket_.Write(sendto_buffer_.get(), sendto_buffer_->size(), | |
329 base::Bind(&UDPSocketImpl::OnSendToCompleted, | |
330 base::Unretained(this), callback)); | |
331 } | |
332 if (net_result != net::ERR_IO_PENDING) | |
333 OnSendToCompleted(callback, net_result); | |
334 } | |
335 | |
336 void UDPSocketImpl::OnRecvFromCompleted(int net_result) { | |
337 DCHECK(recvfrom_buffer_.get()); | |
338 | |
339 NetAddressPtr net_address; | |
340 Array<uint8_t> array; | |
341 if (net_result >= 0) { | |
342 if (state_ == BOUND) | |
343 net_address = NetAddress::From(recvfrom_address_); | |
344 | |
345 std::vector<uint8_t> data(net_result); | |
346 if (net_result > 0) | |
347 memcpy(&data[0], recvfrom_buffer_->data(), net_result); | |
348 | |
349 array.Swap(&data); | |
350 } | |
351 recvfrom_buffer_ = nullptr; | |
352 | |
353 receiver_->OnReceived(MakeNetworkError(net_result), std::move(net_address), | |
354 std::move(array)); | |
355 DCHECK_GT(remaining_recv_slots_, 0u); | |
356 remaining_recv_slots_--; | |
357 if (remaining_recv_slots_ > 0) | |
358 DoRecvFrom(); | |
359 } | |
360 | |
361 void UDPSocketImpl::OnSendToCompleted( | |
362 const Callback<void(NetworkErrorPtr)>& callback, | |
363 int net_result) { | |
364 DCHECK(sendto_buffer_.get()); | |
365 | |
366 sendto_buffer_ = nullptr; | |
367 | |
368 callback.Run(MakeNetworkError(net_result)); | |
369 | |
370 if (pending_send_requests_.empty()) | |
371 return; | |
372 | |
373 scoped_ptr<PendingSendRequest> request(pending_send_requests_.front()); | |
374 pending_send_requests_.pop_front(); | |
375 | |
376 DoSendTo(std::move(request->addr), std::move(request->data), | |
377 request->callback); | |
378 } | |
379 | |
380 } // namespace mojo | |
OLD | NEW |