OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "remoting/jingle_glue/chromium_socket_factory.h" |
| 6 |
| 7 #include "base/bind.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 |
| 17 namespace remoting { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Size of the buffer to allocate for RecvFrom(). |
| 22 const int kReceiveBufferSize = 65536; |
| 23 |
| 24 // Maximum amount of data in the send buffers. This is necessary to |
| 25 // prevent out-of-memory crashes if the caller sends data faster than |
| 26 // Pepper's UDP API can handle it. This maximum should never be |
| 27 // reached under normal conditions. |
| 28 const int kMaxSendBufferSize = 256 * 1024; |
| 29 |
| 30 class UdpPacketSocket : public talk_base::AsyncPacketSocket { |
| 31 public: |
| 32 UdpPacketSocket(); |
| 33 virtual ~UdpPacketSocket(); |
| 34 |
| 35 bool Init(const talk_base::SocketAddress& local_address, |
| 36 int min_port, int max_port); |
| 37 |
| 38 // talk_base::AsyncPacketSocket interface. |
| 39 virtual talk_base::SocketAddress GetLocalAddress() const; |
| 40 virtual talk_base::SocketAddress GetRemoteAddress() const; |
| 41 virtual int Send(const void* data, size_t data_size); |
| 42 virtual int SendTo(const void* data, size_t data_size, |
| 43 const talk_base::SocketAddress& address); |
| 44 virtual int Close(); |
| 45 virtual State GetState() const; |
| 46 virtual int GetOption(talk_base::Socket::Option option, int* value); |
| 47 virtual int SetOption(talk_base::Socket::Option option, int value); |
| 48 virtual int GetError() const; |
| 49 virtual void SetError(int error); |
| 50 |
| 51 private: |
| 52 struct PendingPacket { |
| 53 PendingPacket(const void* buffer, |
| 54 int buffer_size, |
| 55 const net::IPEndPoint& address); |
| 56 |
| 57 scoped_refptr<net::IOBufferWithSize> data; |
| 58 net::IPEndPoint address; |
| 59 }; |
| 60 |
| 61 void OnBindCompleted(int error); |
| 62 |
| 63 void DoSend(); |
| 64 void OnSendCompleted(int result); |
| 65 |
| 66 void DoRead(); |
| 67 void OnReadCompleted(int result); |
| 68 void HandleReadResult(int result); |
| 69 |
| 70 scoped_ptr<net::UDPServerSocket> socket_; |
| 71 |
| 72 State state_; |
| 73 int error_; |
| 74 |
| 75 talk_base::SocketAddress local_address_; |
| 76 |
| 77 // Receive buffer and address are populated by asynchronous reads. |
| 78 scoped_refptr<net::IOBuffer> receive_buffer_; |
| 79 net::IPEndPoint receive_address_; |
| 80 |
| 81 bool send_pending_; |
| 82 std::list<PendingPacket> send_queue_; |
| 83 int send_queue_size_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(UdpPacketSocket); |
| 86 }; |
| 87 |
| 88 UdpPacketSocket::PendingPacket::PendingPacket( |
| 89 const void* buffer, |
| 90 int buffer_size, |
| 91 const net::IPEndPoint& address) |
| 92 : data(new net::IOBufferWithSize(buffer_size)), |
| 93 address(address) { |
| 94 memcpy(data->data(), buffer, buffer_size); |
| 95 } |
| 96 |
| 97 UdpPacketSocket::UdpPacketSocket() |
| 98 : state_(STATE_CLOSED), |
| 99 error_(0), |
| 100 send_pending_(false), |
| 101 send_queue_size_(0) { |
| 102 } |
| 103 |
| 104 UdpPacketSocket::~UdpPacketSocket() { |
| 105 Close(); |
| 106 } |
| 107 |
| 108 bool UdpPacketSocket::Init(const talk_base::SocketAddress& local_address, |
| 109 int min_port, int max_port) { |
| 110 net::IPEndPoint local_endpoint; |
| 111 if (!jingle_glue::SocketAddressToIPEndPoint( |
| 112 local_address, &local_endpoint)) { |
| 113 return false; |
| 114 } |
| 115 |
| 116 for (int port = min_port; port <= max_port; ++port) { |
| 117 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source())); |
| 118 int result = socket_->Listen( |
| 119 net::IPEndPoint(local_endpoint.address(), port)); |
| 120 if (result == net::OK) { |
| 121 break; |
| 122 } else { |
| 123 socket_.reset(); |
| 124 } |
| 125 } |
| 126 |
| 127 if (!socket_.get()) { |
| 128 // Failed to bind the socket. |
| 129 return false; |
| 130 } |
| 131 |
| 132 if (socket_->GetLocalAddress(&local_endpoint) != net::OK || |
| 133 !jingle_glue::IPEndPointToSocketAddress(local_endpoint, |
| 134 &local_address_)) { |
| 135 return false; |
| 136 } |
| 137 |
| 138 state_ = STATE_BOUND; |
| 139 DoRead(); |
| 140 |
| 141 return true; |
| 142 } |
| 143 |
| 144 talk_base::SocketAddress UdpPacketSocket::GetLocalAddress() const { |
| 145 DCHECK_EQ(state_, STATE_BOUND); |
| 146 return local_address_; |
| 147 } |
| 148 |
| 149 talk_base::SocketAddress UdpPacketSocket::GetRemoteAddress() const { |
| 150 // UDP sockets are not connected - this method should never be called. |
| 151 NOTREACHED(); |
| 152 return talk_base::SocketAddress(); |
| 153 } |
| 154 |
| 155 int UdpPacketSocket::Send(const void* data, size_t data_size) { |
| 156 // UDP sockets are not connected - this method should never be called. |
| 157 NOTREACHED(); |
| 158 return EWOULDBLOCK; |
| 159 } |
| 160 |
| 161 int UdpPacketSocket::SendTo(const void* data, size_t data_size, |
| 162 const talk_base::SocketAddress& address) { |
| 163 if (state_ != STATE_BOUND) { |
| 164 NOTREACHED(); |
| 165 return EINVAL; |
| 166 } |
| 167 |
| 168 if (error_ != 0) { |
| 169 return error_; |
| 170 } |
| 171 |
| 172 net::IPEndPoint endpoint; |
| 173 if (!jingle_glue::SocketAddressToIPEndPoint(address, &endpoint)) { |
| 174 return EINVAL; |
| 175 } |
| 176 |
| 177 if (send_queue_size_ >= kMaxSendBufferSize) { |
| 178 return EWOULDBLOCK; |
| 179 } |
| 180 |
| 181 send_queue_.push_back(PendingPacket(data, data_size, endpoint)); |
| 182 send_queue_size_ += data_size; |
| 183 |
| 184 DoSend(); |
| 185 return data_size; |
| 186 } |
| 187 |
| 188 int UdpPacketSocket::Close() { |
| 189 state_ = STATE_CLOSED; |
| 190 socket_.reset(); |
| 191 return 0; |
| 192 } |
| 193 |
| 194 talk_base::AsyncPacketSocket::State UdpPacketSocket::GetState() const { |
| 195 return state_; |
| 196 } |
| 197 |
| 198 int UdpPacketSocket::GetOption(talk_base::Socket::Option option, int* value) { |
| 199 // This method is never called by libjingle. |
| 200 NOTIMPLEMENTED(); |
| 201 return -1; |
| 202 } |
| 203 |
| 204 int UdpPacketSocket::SetOption(talk_base::Socket::Option option, int value) { |
| 205 if (state_ != STATE_BOUND) { |
| 206 NOTREACHED(); |
| 207 return EINVAL; |
| 208 } |
| 209 |
| 210 switch (option) { |
| 211 case talk_base::Socket::OPT_DONTFRAGMENT: |
| 212 NOTIMPLEMENTED(); |
| 213 return -1; |
| 214 |
| 215 case talk_base::Socket::OPT_RCVBUF: { |
| 216 bool success = socket_->SetReceiveBufferSize(value); |
| 217 return success ? 0 : -1; |
| 218 } |
| 219 |
| 220 case talk_base::Socket::OPT_SNDBUF: { |
| 221 bool success = socket_->SetSendBufferSize(value); |
| 222 return success ? 0 : -1; |
| 223 } |
| 224 |
| 225 case talk_base::Socket::OPT_NODELAY: |
| 226 // OPT_NODELAY is only for TCP sockets. |
| 227 NOTREACHED(); |
| 228 return -1; |
| 229 |
| 230 case talk_base::Socket::OPT_IPV6_V6ONLY: |
| 231 NOTIMPLEMENTED(); |
| 232 return -1; |
| 233 } |
| 234 |
| 235 NOTREACHED(); |
| 236 return -1; |
| 237 } |
| 238 |
| 239 int UdpPacketSocket::GetError() const { |
| 240 return error_; |
| 241 } |
| 242 |
| 243 void UdpPacketSocket::SetError(int error) { |
| 244 error_ = error; |
| 245 } |
| 246 |
| 247 void UdpPacketSocket::DoSend() { |
| 248 if (send_pending_ || send_queue_.empty()) |
| 249 return; |
| 250 |
| 251 PendingPacket& packet = send_queue_.front(); |
| 252 int result = socket_->SendTo( |
| 253 packet.data, packet.data->size(), packet.address, |
| 254 base::Bind(&UdpPacketSocket::OnSendCompleted, |
| 255 base::Unretained(this))); |
| 256 if (result == net::ERR_IO_PENDING) { |
| 257 send_pending_ = true; |
| 258 } else { |
| 259 OnSendCompleted(result); |
| 260 } |
| 261 } |
| 262 |
| 263 void UdpPacketSocket::OnSendCompleted(int result) { |
| 264 send_pending_ = false; |
| 265 |
| 266 if (result < 0) { |
| 267 // Treat all errors except ERR_ADDRESS_UNREACHABLE as fatal. |
| 268 if (result != net::ERR_ADDRESS_UNREACHABLE) { |
| 269 LOG(ERROR) << "Send failed on a UDP socket: " << result; |
| 270 error_ = EINVAL; |
| 271 return; |
| 272 } |
| 273 } |
| 274 |
| 275 // Don't need to worry about partial sends because this is a datagram |
| 276 // socket. |
| 277 send_queue_size_ -= send_queue_.front().data->size(); |
| 278 send_queue_.pop_front(); |
| 279 DoSend(); |
| 280 } |
| 281 |
| 282 void UdpPacketSocket::DoRead() { |
| 283 int result = 0; |
| 284 while (result >= 0) { |
| 285 receive_buffer_ = new net::IOBuffer(kReceiveBufferSize); |
| 286 result = socket_->RecvFrom( |
| 287 receive_buffer_, kReceiveBufferSize, &receive_address_, |
| 288 base::Bind(&UdpPacketSocket::OnReadCompleted, base::Unretained(this))); |
| 289 HandleReadResult(result); |
| 290 } |
| 291 } |
| 292 |
| 293 void UdpPacketSocket::OnReadCompleted(int result) { |
| 294 HandleReadResult(result); |
| 295 if (result >= 0) { |
| 296 DoRead(); |
| 297 } |
| 298 } |
| 299 |
| 300 void UdpPacketSocket::HandleReadResult(int result) { |
| 301 if (result == net::ERR_IO_PENDING) { |
| 302 return; |
| 303 } |
| 304 |
| 305 if (result > 0) { |
| 306 talk_base::SocketAddress address; |
| 307 if (!jingle_glue::IPEndPointToSocketAddress(receive_address_, &address)) { |
| 308 NOTREACHED(); |
| 309 LOG(ERROR) << "Failed to convert address received from RecvFrom()."; |
| 310 return; |
| 311 } |
| 312 SignalReadPacket(this, receive_buffer_->data(), result, address); |
| 313 } else { |
| 314 LOG(ERROR) << "Received error when reading from UDP socket: " << result; |
| 315 } |
| 316 } |
| 317 |
| 318 } // namespace |
| 319 |
| 320 ChromiumPacketSocketFactory::ChromiumPacketSocketFactory() { |
| 321 } |
| 322 |
| 323 ChromiumPacketSocketFactory::~ChromiumPacketSocketFactory() { |
| 324 } |
| 325 |
| 326 talk_base::AsyncPacketSocket* ChromiumPacketSocketFactory::CreateUdpSocket( |
| 327 const talk_base::SocketAddress& local_address, |
| 328 int min_port, int max_port) { |
| 329 scoped_ptr<UdpPacketSocket> result(new UdpPacketSocket()); |
| 330 if (!result->Init(local_address, min_port, max_port)) |
| 331 return NULL; |
| 332 return result.release(); |
| 333 } |
| 334 |
| 335 talk_base::AsyncPacketSocket* |
| 336 ChromiumPacketSocketFactory::CreateServerTcpSocket( |
| 337 const talk_base::SocketAddress& local_address, |
| 338 int min_port, int max_port, |
| 339 bool ssl) { |
| 340 // We don't use TCP sockets for remoting connections. |
| 341 NOTREACHED(); |
| 342 return NULL; |
| 343 } |
| 344 |
| 345 talk_base::AsyncPacketSocket* |
| 346 ChromiumPacketSocketFactory::CreateClientTcpSocket( |
| 347 const talk_base::SocketAddress& local_address, |
| 348 const talk_base::SocketAddress& remote_address, |
| 349 const talk_base::ProxyInfo& proxy_info, |
| 350 const std::string& user_agent, |
| 351 bool ssl) { |
| 352 // We don't use TCP sockets for remoting connections. |
| 353 NOTREACHED(); |
| 354 return NULL; |
| 355 } |
| 356 |
| 357 } // namespace remoting |
OLD | NEW |