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 const talk_base::PacketOptions& options) OVERRIDE;
51 virtual int SendTo(const void* data, size_t data_size,
52 const talk_base::SocketAddress& address,
53 const talk_base::PacketOptions& options) 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 const talk_base::PacketOptions& options) {
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 const talk_base::PacketOptions& options) {
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:
250 case talk_base::Socket::OPT_RTP_SENDTIME_EXTN_ID:
259 int UdpPacketSocket::GetError() const {
263 void UdpPacketSocket::SetError(int error) {
267 void UdpPacketSocket::DoSend() {
268 if (send_pending_ || send_queue_.empty())
271 PendingPacket& packet = send_queue_.front();
272 int result = socket_->SendTo(
276 base::Bind(&UdpPacketSocket::OnSendCompleted, base::Unretained(this)));
277 if (result == net::ERR_IO_PENDING) {
278 send_pending_ = true;
280 OnSendCompleted(result);
284 void UdpPacketSocket::OnSendCompleted(int result) {
285 send_pending_ = false;
288 if (!IsTransientError(result)) {
289 LOG(ERROR) << "Send failed on a UDP socket: " << result;
295 // Don't need to worry about partial sends because this is a datagram
297 send_queue_size_ -= send_queue_.front().data->size();
298 send_queue_.pop_front();
302 void UdpPacketSocket::DoRead() {
304 while (result >= 0) {
305 receive_buffer_ = new net::IOBuffer(kReceiveBufferSize);
306 result = socket_->RecvFrom(
307 receive_buffer_.get(),
310 base::Bind(&UdpPacketSocket::OnReadCompleted, base::Unretained(this)));
311 HandleReadResult(result);
315 void UdpPacketSocket::OnReadCompleted(int result) {
316 HandleReadResult(result);
322 void UdpPacketSocket::HandleReadResult(int result) {
323 if (result == net::ERR_IO_PENDING) {
328 talk_base::SocketAddress address;
329 if (!jingle_glue::IPEndPointToSocketAddress(receive_address_, &address)) {
331 LOG(ERROR) << "Failed to convert address received from RecvFrom().";
334 SignalReadPacket(this, receive_buffer_->data(), result, address,
335 talk_base::CreatePacketTime(0));
337 LOG(ERROR) << "Received error when reading from UDP socket: " << result;
343 ChromiumPacketSocketFactory::ChromiumPacketSocketFactory() {
346 ChromiumPacketSocketFactory::~ChromiumPacketSocketFactory() {
349 talk_base::AsyncPacketSocket* ChromiumPacketSocketFactory::CreateUdpSocket(
350 const talk_base::SocketAddress& local_address,
351 int min_port, int max_port) {
352 scoped_ptr<UdpPacketSocket> result(new UdpPacketSocket());
353 if (!result->Init(local_address, min_port, max_port))
355 return result.release();
358 talk_base::AsyncPacketSocket*
359 ChromiumPacketSocketFactory::CreateServerTcpSocket(
360 const talk_base::SocketAddress& local_address,
361 int min_port, int max_port,
363 // We don't use TCP sockets for remoting connections.
368 talk_base::AsyncPacketSocket*
369 ChromiumPacketSocketFactory::CreateClientTcpSocket(
370 const talk_base::SocketAddress& local_address,
371 const talk_base::SocketAddress& remote_address,
372 const talk_base::ProxyInfo& proxy_info,
373 const std::string& user_agent,
375 // We don't use TCP sockets for remoting connections.
380 talk_base::AsyncResolverInterface*
381 ChromiumPacketSocketFactory::CreateAsyncResolver() {
382 return new talk_base::AsyncResolver();
385 } // namespace remoting