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.
5 #include "remoting/protocol/chromium_socket_factory.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "jingle/glue/utils.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/base/net_errors.h"
14 #include "net/udp/udp_server_socket.h"
15 #include "remoting/protocol/socket_util.h"
16 #include "third_party/webrtc/base/asyncpacketsocket.h"
17 #include "third_party/webrtc/base/nethelpers.h"
24 // Size of the buffer to allocate for RecvFrom().
25 const int kReceiveBufferSize
= 65536;
27 // Maximum amount of data in the send buffers. This is necessary to
28 // prevent out-of-memory crashes if the caller sends data faster than
29 // Pepper's UDP API can handle it. This maximum should never be
30 // reached under normal conditions.
31 const int kMaxSendBufferSize
= 256 * 1024;
33 class UdpPacketSocket
: public rtc::AsyncPacketSocket
{
36 virtual ~UdpPacketSocket();
38 bool Init(const rtc::SocketAddress
& local_address
,
39 int min_port
, int max_port
);
41 // rtc::AsyncPacketSocket interface.
42 virtual rtc::SocketAddress
GetLocalAddress() const OVERRIDE
;
43 virtual rtc::SocketAddress
GetRemoteAddress() const OVERRIDE
;
44 virtual int Send(const void* data
, size_t data_size
,
45 const rtc::PacketOptions
& options
) OVERRIDE
;
46 virtual int SendTo(const void* data
, size_t data_size
,
47 const rtc::SocketAddress
& address
,
48 const rtc::PacketOptions
& options
) OVERRIDE
;
49 virtual int Close() OVERRIDE
;
50 virtual State
GetState() const OVERRIDE
;
51 virtual int GetOption(rtc::Socket::Option option
, int* value
) OVERRIDE
;
52 virtual int SetOption(rtc::Socket::Option option
, int value
) OVERRIDE
;
53 virtual int GetError() const OVERRIDE
;
54 virtual void SetError(int error
) OVERRIDE
;
57 struct PendingPacket
{
58 PendingPacket(const void* buffer
,
60 const net::IPEndPoint
& address
);
62 scoped_refptr
<net::IOBufferWithSize
> data
;
63 net::IPEndPoint address
;
67 void OnBindCompleted(int error
);
70 void OnSendCompleted(int result
);
73 void OnReadCompleted(int result
);
74 void HandleReadResult(int result
);
76 scoped_ptr
<net::UDPServerSocket
> socket_
;
81 rtc::SocketAddress local_address_
;
83 // Receive buffer and address are populated by asynchronous reads.
84 scoped_refptr
<net::IOBuffer
> receive_buffer_
;
85 net::IPEndPoint receive_address_
;
88 std::list
<PendingPacket
> send_queue_
;
91 DISALLOW_COPY_AND_ASSIGN(UdpPacketSocket
);
94 UdpPacketSocket::PendingPacket::PendingPacket(
97 const net::IPEndPoint
& address
)
98 : data(new net::IOBufferWithSize(buffer_size
)),
101 memcpy(data
->data(), buffer
, buffer_size
);
104 UdpPacketSocket::UdpPacketSocket()
105 : state_(STATE_CLOSED
),
107 send_pending_(false),
108 send_queue_size_(0) {
111 UdpPacketSocket::~UdpPacketSocket() {
115 bool UdpPacketSocket::Init(const rtc::SocketAddress
& local_address
,
116 int min_port
, int max_port
) {
117 net::IPEndPoint local_endpoint
;
118 if (!jingle_glue::SocketAddressToIPEndPoint(
119 local_address
, &local_endpoint
)) {
123 for (int port
= min_port
; port
<= max_port
; ++port
) {
124 socket_
.reset(new net::UDPServerSocket(NULL
, net::NetLog::Source()));
125 int result
= socket_
->Listen(
126 net::IPEndPoint(local_endpoint
.address(), port
));
127 if (result
== net::OK
) {
134 if (!socket_
.get()) {
135 // Failed to bind the socket.
139 if (socket_
->GetLocalAddress(&local_endpoint
) != net::OK
||
140 !jingle_glue::IPEndPointToSocketAddress(local_endpoint
,
145 state_
= STATE_BOUND
;
151 rtc::SocketAddress
UdpPacketSocket::GetLocalAddress() const {
152 DCHECK_EQ(state_
, STATE_BOUND
);
153 return local_address_
;
156 rtc::SocketAddress
UdpPacketSocket::GetRemoteAddress() const {
157 // UDP sockets are not connected - this method should never be called.
159 return rtc::SocketAddress();
162 int UdpPacketSocket::Send(const void* data
, size_t data_size
,
163 const rtc::PacketOptions
& options
) {
164 // UDP sockets are not connected - this method should never be called.
169 int UdpPacketSocket::SendTo(const void* data
, size_t data_size
,
170 const rtc::SocketAddress
& address
,
171 const rtc::PacketOptions
& options
) {
172 if (state_
!= STATE_BOUND
) {
181 net::IPEndPoint endpoint
;
182 if (!jingle_glue::SocketAddressToIPEndPoint(address
, &endpoint
)) {
186 if (send_queue_size_
>= kMaxSendBufferSize
) {
190 send_queue_
.push_back(PendingPacket(data
, data_size
, endpoint
));
191 send_queue_size_
+= data_size
;
197 int UdpPacketSocket::Close() {
198 state_
= STATE_CLOSED
;
203 rtc::AsyncPacketSocket::State
UdpPacketSocket::GetState() const {
207 int UdpPacketSocket::GetOption(rtc::Socket::Option option
, int* value
) {
208 // This method is never called by libjingle.
213 int UdpPacketSocket::SetOption(rtc::Socket::Option option
, int value
) {
214 if (state_
!= STATE_BOUND
) {
220 case rtc::Socket::OPT_DONTFRAGMENT
:
224 case rtc::Socket::OPT_RCVBUF
: {
225 int net_error
= socket_
->SetReceiveBufferSize(value
);
226 return (net_error
== net::OK
) ? 0 : -1;
229 case rtc::Socket::OPT_SNDBUF
: {
230 int net_error
= socket_
->SetSendBufferSize(value
);
231 return (net_error
== net::OK
) ? 0 : -1;
234 case rtc::Socket::OPT_NODELAY
:
235 // OPT_NODELAY is only for TCP sockets.
239 case rtc::Socket::OPT_IPV6_V6ONLY
:
243 case rtc::Socket::OPT_DSCP
:
247 case rtc::Socket::OPT_RTP_SENDTIME_EXTN_ID
:
256 int UdpPacketSocket::GetError() const {
260 void UdpPacketSocket::SetError(int error
) {
264 void UdpPacketSocket::DoSend() {
265 if (send_pending_
|| send_queue_
.empty())
268 PendingPacket
& packet
= send_queue_
.front();
269 int result
= socket_
->SendTo(
273 base::Bind(&UdpPacketSocket::OnSendCompleted
, base::Unretained(this)));
274 if (result
== net::ERR_IO_PENDING
) {
275 send_pending_
= true;
277 OnSendCompleted(result
);
281 void UdpPacketSocket::OnSendCompleted(int result
) {
282 send_pending_
= false;
285 SocketErrorAction action
= GetSocketErrorAction(result
);
287 case SOCKET_ERROR_ACTION_FAIL
:
288 LOG(ERROR
) << "Send failed on a UDP socket: " << result
;
292 case SOCKET_ERROR_ACTION_RETRY
:
293 // Retry resending only once.
294 if (!send_queue_
.front().retried
) {
295 send_queue_
.front().retried
= true;
301 case SOCKET_ERROR_ACTION_IGNORE
:
306 // Don't need to worry about partial sends because this is a datagram
308 send_queue_size_
-= send_queue_
.front().data
->size();
309 send_queue_
.pop_front();
313 void UdpPacketSocket::DoRead() {
315 while (result
>= 0) {
316 receive_buffer_
= new net::IOBuffer(kReceiveBufferSize
);
317 result
= socket_
->RecvFrom(
318 receive_buffer_
.get(),
321 base::Bind(&UdpPacketSocket::OnReadCompleted
, base::Unretained(this)));
322 HandleReadResult(result
);
326 void UdpPacketSocket::OnReadCompleted(int result
) {
327 HandleReadResult(result
);
333 void UdpPacketSocket::HandleReadResult(int result
) {
334 if (result
== net::ERR_IO_PENDING
) {
339 rtc::SocketAddress address
;
340 if (!jingle_glue::IPEndPointToSocketAddress(receive_address_
, &address
)) {
342 LOG(ERROR
) << "Failed to convert address received from RecvFrom().";
345 SignalReadPacket(this, receive_buffer_
->data(), result
, address
,
346 rtc::CreatePacketTime(0));
348 LOG(ERROR
) << "Received error when reading from UDP socket: " << result
;
354 ChromiumPacketSocketFactory::ChromiumPacketSocketFactory() {
357 ChromiumPacketSocketFactory::~ChromiumPacketSocketFactory() {
360 rtc::AsyncPacketSocket
* ChromiumPacketSocketFactory::CreateUdpSocket(
361 const rtc::SocketAddress
& local_address
,
362 int min_port
, int max_port
) {
363 scoped_ptr
<UdpPacketSocket
> result(new UdpPacketSocket());
364 if (!result
->Init(local_address
, min_port
, max_port
))
366 return result
.release();
369 rtc::AsyncPacketSocket
*
370 ChromiumPacketSocketFactory::CreateServerTcpSocket(
371 const rtc::SocketAddress
& local_address
,
372 int min_port
, int max_port
,
374 // We don't use TCP sockets for remoting connections.
379 rtc::AsyncPacketSocket
*
380 ChromiumPacketSocketFactory::CreateClientTcpSocket(
381 const rtc::SocketAddress
& local_address
,
382 const rtc::SocketAddress
& remote_address
,
383 const rtc::ProxyInfo
& proxy_info
,
384 const std::string
& user_agent
,
386 // We don't use TCP sockets for remoting connections.
391 rtc::AsyncResolverInterface
*
392 ChromiumPacketSocketFactory::CreateAsyncResolver() {
393 return new rtc::AsyncResolver();
396 } // namespace protocol
397 } // namespace remoting