1 // Copyright (c) 2012 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/jingle_glue/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 "third_party/libjingle/source/talk/base/asyncpacketsocket.h"
16 #include "third_party/libjingle/source/talk/base/nethelpers.h"
22 // Size of the buffer to allocate for RecvFrom().
23 const int kReceiveBufferSize = 65536;
25 // Maximum amount of data in the send buffers. This is necessary to
26 // prevent out-of-memory crashes if the caller sends data faster than
27 // Pepper's UDP API can handle it. This maximum should never be
28 // reached under normal conditions.
29 const int kMaxSendBufferSize = 256 * 1024;
31 // Defines set of transient errors. These errors are ignored when we get them
32 // from sendto() calls.
33 bool IsTransientError(int error) {
34 return error == net::ERR_ADDRESS_UNREACHABLE ||
35 error == net::ERR_ADDRESS_INVALID;
38 class UdpPacketSocket : public talk_base::AsyncPacketSocket {
41 virtual ~UdpPacketSocket();
43 bool Init(const talk_base::SocketAddress& local_address,
44 int min_port, int max_port);
46 // talk_base::AsyncPacketSocket interface.
47 virtual talk_base::SocketAddress GetLocalAddress() const OVERRIDE;
48 virtual talk_base::SocketAddress GetRemoteAddress() const OVERRIDE;
49 virtual int Send(const void* data, size_t data_size,
50 talk_base::DiffServCodePoint dscp) OVERRIDE;
51 virtual int SendTo(const void* data, size_t data_size,
52 const talk_base::SocketAddress& address,
53 talk_base::DiffServCodePoint dscp) OVERRIDE;
54 virtual int Close() OVERRIDE;
55 virtual State GetState() const OVERRIDE;
56 virtual int GetOption(talk_base::Socket::Option option, int* value) OVERRIDE;
57 virtual int SetOption(talk_base::Socket::Option option, int value) OVERRIDE;
58 virtual int GetError() const OVERRIDE;
59 virtual void SetError(int error) OVERRIDE;
62 struct PendingPacket {
63 PendingPacket(const void* buffer,
65 const net::IPEndPoint& address);
67 scoped_refptr<net::IOBufferWithSize> data;
68 net::IPEndPoint address;
71 void OnBindCompleted(int error);
74 void OnSendCompleted(int result);
77 void OnReadCompleted(int result);
78 void HandleReadResult(int result);
80 scoped_ptr<net::UDPServerSocket> socket_;
85 talk_base::SocketAddress local_address_;
87 // Receive buffer and address are populated by asynchronous reads.
88 scoped_refptr<net::IOBuffer> receive_buffer_;
89 net::IPEndPoint receive_address_;
92 std::list<PendingPacket> send_queue_;
95 DISALLOW_COPY_AND_ASSIGN(UdpPacketSocket);
98 UdpPacketSocket::PendingPacket::PendingPacket(
101 const net::IPEndPoint& address)
102 : data(new net::IOBufferWithSize(buffer_size)),
104 memcpy(data->data(), buffer, buffer_size);
107 UdpPacketSocket::UdpPacketSocket()
108 : state_(STATE_CLOSED),
110 send_pending_(false),
111 send_queue_size_(0) {
114 UdpPacketSocket::~UdpPacketSocket() {
118 bool UdpPacketSocket::Init(const talk_base::SocketAddress& local_address,
119 int min_port, int max_port) {
120 net::IPEndPoint local_endpoint;
121 if (!jingle_glue::SocketAddressToIPEndPoint(
122 local_address, &local_endpoint)) {
126 for (int port = min_port; port <= max_port; ++port) {
127 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source()));
128 int result = socket_->Listen(
129 net::IPEndPoint(local_endpoint.address(), port));
130 if (result == net::OK) {
137 if (!socket_.get()) {
138 // Failed to bind the socket.
142 if (socket_->GetLocalAddress(&local_endpoint) != net::OK ||
143 !jingle_glue::IPEndPointToSocketAddress(local_endpoint,
148 state_ = STATE_BOUND;
154 talk_base::SocketAddress UdpPacketSocket::GetLocalAddress() const {
155 DCHECK_EQ(state_, STATE_BOUND);
156 return local_address_;
159 talk_base::SocketAddress UdpPacketSocket::GetRemoteAddress() const {
160 // UDP sockets are not connected - this method should never be called.
162 return talk_base::SocketAddress();
165 int UdpPacketSocket::Send(const void* data, size_t data_size,
166 talk_base::DiffServCodePoint dscp) {
167 // UDP sockets are not connected - this method should never be called.
172 int UdpPacketSocket::SendTo(const void* data, size_t data_size,
173 const talk_base::SocketAddress& address,
174 talk_base::DiffServCodePoint dscp) {
175 if (state_ != STATE_BOUND) {
184 net::IPEndPoint endpoint;
185 if (!jingle_glue::SocketAddressToIPEndPoint(address, &endpoint)) {
189 if (send_queue_size_ >= kMaxSendBufferSize) {
193 send_queue_.push_back(PendingPacket(data, data_size, endpoint));
194 send_queue_size_ += data_size;
200 int UdpPacketSocket::Close() {
201 state_ = STATE_CLOSED;
206 talk_base::AsyncPacketSocket::State UdpPacketSocket::GetState() const {
210 int UdpPacketSocket::GetOption(talk_base::Socket::Option option, int* value) {
211 // This method is never called by libjingle.
216 int UdpPacketSocket::SetOption(talk_base::Socket::Option option, int value) {
217 if (state_ != STATE_BOUND) {
223 case talk_base::Socket::OPT_DONTFRAGMENT:
227 case talk_base::Socket::OPT_RCVBUF: {
228 bool success = socket_->SetReceiveBufferSize(value);
229 return success ? 0 : -1;
232 case talk_base::Socket::OPT_SNDBUF: {
233 bool success = socket_->SetSendBufferSize(value);
234 return success ? 0 : -1;
237 case talk_base::Socket::OPT_NODELAY:
238 // OPT_NODELAY is only for TCP sockets.
242 case talk_base::Socket::OPT_IPV6_V6ONLY:
246 case talk_base::Socket::OPT_DSCP:
255 int UdpPacketSocket::GetError() const {
259 void UdpPacketSocket::SetError(int error) {
263 void UdpPacketSocket::DoSend() {
264 if (send_pending_ || send_queue_.empty())
267 PendingPacket& packet = send_queue_.front();
268 int result = socket_->SendTo(
272 base::Bind(&UdpPacketSocket::OnSendCompleted, base::Unretained(this)));
273 if (result == net::ERR_IO_PENDING) {
274 send_pending_ = true;
276 OnSendCompleted(result);
280 void UdpPacketSocket::OnSendCompleted(int result) {
281 send_pending_ = false;
284 if (!IsTransientError(result)) {
285 LOG(ERROR) << "Send failed on a UDP socket: " << result;
291 // Don't need to worry about partial sends because this is a datagram
293 send_queue_size_ -= send_queue_.front().data->size();
294 send_queue_.pop_front();
298 void UdpPacketSocket::DoRead() {
300 while (result >= 0) {
301 receive_buffer_ = new net::IOBuffer(kReceiveBufferSize);
302 result = socket_->RecvFrom(
303 receive_buffer_.get(),
306 base::Bind(&UdpPacketSocket::OnReadCompleted, base::Unretained(this)));
307 HandleReadResult(result);
311 void UdpPacketSocket::OnReadCompleted(int result) {
312 HandleReadResult(result);
318 void UdpPacketSocket::HandleReadResult(int result) {
319 if (result == net::ERR_IO_PENDING) {
324 talk_base::SocketAddress address;
325 if (!jingle_glue::IPEndPointToSocketAddress(receive_address_, &address)) {
327 LOG(ERROR) << "Failed to convert address received from RecvFrom().";
330 SignalReadPacket(this, receive_buffer_->data(), result, address,
331 talk_base::CreatePacketTime(0));
333 LOG(ERROR) << "Received error when reading from UDP socket: " << result;
339 ChromiumPacketSocketFactory::ChromiumPacketSocketFactory() {
342 ChromiumPacketSocketFactory::~ChromiumPacketSocketFactory() {
345 talk_base::AsyncPacketSocket* ChromiumPacketSocketFactory::CreateUdpSocket(
346 const talk_base::SocketAddress& local_address,
347 int min_port, int max_port) {
348 scoped_ptr<UdpPacketSocket> result(new UdpPacketSocket());
349 if (!result->Init(local_address, min_port, max_port))
351 return result.release();
354 talk_base::AsyncPacketSocket*
355 ChromiumPacketSocketFactory::CreateServerTcpSocket(
356 const talk_base::SocketAddress& local_address,
357 int min_port, int max_port,
359 // We don't use TCP sockets for remoting connections.
364 talk_base::AsyncPacketSocket*
365 ChromiumPacketSocketFactory::CreateClientTcpSocket(
366 const talk_base::SocketAddress& local_address,
367 const talk_base::SocketAddress& remote_address,
368 const talk_base::ProxyInfo& proxy_info,
369 const std::string& user_agent,
371 // We don't use TCP sockets for remoting connections.
376 talk_base::AsyncResolverInterface*
377 ChromiumPacketSocketFactory::CreateAsyncResolver() {
378 return new talk_base::AsyncResolver();
381 } // namespace remoting