| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
| 6 | 6 |
| 7 #include "base/sys_byteorder.h" | 7 #include "base/sys_byteorder.h" |
| 8 #include "content/common/p2p_messages.h" | 8 #include "content/common/p2p_messages.h" |
| 9 #include "ipc/ipc_sender.h" | 9 #include "ipc/ipc_sender.h" |
| 10 #include "jingle/glue/fake_ssl_client_socket.h" | 10 #include "jingle/glue/fake_ssl_client_socket.h" |
| 11 #include "jingle/glue/proxy_resolving_client_socket.h" |
| 11 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 13 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 14 #include "net/socket/tcp_client_socket.h" | 15 #include "net/socket/tcp_client_socket.h" |
| 16 #include "net/url_request/url_request_context_getter.h" |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 typedef uint16 PacketLength; | 20 typedef uint16 PacketLength; |
| 19 const int kPacketHeaderSize = sizeof(PacketLength); | 21 const int kPacketHeaderSize = sizeof(PacketLength); |
| 20 const int kReadBufferSize = 4096; | 22 const int kReadBufferSize = 4096; |
| 21 const int kPacketLengthOffset = 2; | 23 const int kPacketLengthOffset = 2; |
| 22 const int kTurnChannelDataHeaderSize = 4; | 24 const int kTurnChannelDataHeaderSize = 4; |
| 23 | 25 |
| 24 bool IsSslClientSocket(content::P2PSocketType type) { | 26 bool IsSslClientSocket(content::P2PSocketType type) { |
| 25 return (type == content::P2P_SOCKET_SSLTCP_CLIENT || | 27 return (type == content::P2P_SOCKET_SSLTCP_CLIENT || |
| 26 type == content::P2P_SOCKET_STUN_SSLTCP_CLIENT); | 28 type == content::P2P_SOCKET_STUN_SSLTCP_CLIENT); |
| 27 } | 29 } |
| 28 | 30 |
| 29 } // namespace | 31 } // namespace |
| 30 | 32 |
| 31 namespace content { | 33 namespace content { |
| 32 | 34 |
| 33 P2PSocketHostTcpBase::P2PSocketHostTcpBase(IPC::Sender* message_sender, | 35 P2PSocketHostTcpBase::P2PSocketHostTcpBase( |
| 34 int id, | 36 IPC::Sender* message_sender, int id, |
| 35 P2PSocketType type) | 37 P2PSocketType type, net::URLRequestContextGetter* url_context) |
| 36 : P2PSocketHost(message_sender, id), | 38 : P2PSocketHost(message_sender, id), |
| 37 write_pending_(false), | 39 write_pending_(false), |
| 38 connected_(false), | 40 connected_(false), |
| 39 type_(type) { | 41 type_(type), |
| 42 url_context_(url_context) { |
| 40 } | 43 } |
| 41 | 44 |
| 42 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() { | 45 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() { |
| 43 if (state_ == STATE_OPEN) { | 46 if (state_ == STATE_OPEN) { |
| 44 DCHECK(socket_.get()); | 47 DCHECK(socket_.get()); |
| 45 socket_.reset(); | 48 socket_.reset(); |
| 46 } | 49 } |
| 47 } | 50 } |
| 48 | 51 |
| 49 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint& remote_address, | 52 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint& remote_address, |
| 50 net::StreamSocket* socket) { | 53 net::StreamSocket* socket) { |
| 51 DCHECK(socket); | 54 DCHECK(socket); |
| 52 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 55 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 53 | 56 |
| 54 remote_address_ = remote_address; | 57 remote_address_ = remote_address; |
| 55 // TODO(ronghuawu): Add FakeSSLServerSocket. | 58 // TODO(ronghuawu): Add FakeSSLServerSocket. |
| 56 socket_.reset(socket); | 59 socket_.reset(socket); |
| 57 state_ = STATE_OPEN; | 60 state_ = STATE_OPEN; |
| 58 DoRead(); | 61 DoRead(); |
| 59 return state_ != STATE_ERROR; | 62 return state_ != STATE_ERROR; |
| 60 } | 63 } |
| 61 | 64 |
| 62 bool P2PSocketHostTcpBase::Init(const net::IPEndPoint& local_address, | 65 bool P2PSocketHostTcpBase::Init(const net::IPEndPoint& local_address, |
| 63 const net::IPEndPoint& remote_address) { | 66 const net::IPEndPoint& remote_address) { |
| 64 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 67 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 65 | 68 |
| 66 remote_address_ = remote_address; | 69 remote_address_ = remote_address; |
| 67 state_ = STATE_CONNECTING; | 70 state_ = STATE_CONNECTING; |
| 68 scoped_ptr<net::TCPClientSocket> tcp_socket(new net::TCPClientSocket( | 71 |
| 69 net::AddressList(remote_address), | 72 net::HostPortPair dest_host_port_pair = |
| 70 NULL, net::NetLog::Source())); | 73 net::HostPortPair::FromIPEndPoint(remote_address); |
| 71 if (tcp_socket->Bind(local_address) != net::OK) { | 74 // TODO(mallinath) - We are ignoring local_address altogether. We should |
| 72 OnError(); | 75 // find a way to inject this into ProxyResolvingClientSocket. This could be |
| 73 return false; | 76 // a problem on multi-homed host. |
| 74 } | 77 |
| 78 // The default SSLConfig is good enough for us for now. |
| 79 const net::SSLConfig ssl_config; |
| 80 socket_.reset(new jingle_glue::ProxyResolvingClientSocket( |
| 81 NULL, // Default socket pool provided by the net::Proxy. |
| 82 url_context_, |
| 83 ssl_config, |
| 84 dest_host_port_pair)); |
| 75 if (IsSslClientSocket(type_)) { | 85 if (IsSslClientSocket(type_)) { |
| 76 socket_.reset(new jingle_glue::FakeSSLClientSocket(tcp_socket.release())); | 86 socket_.reset(new jingle_glue::FakeSSLClientSocket(socket_.release())); |
| 77 } else { | |
| 78 socket_ = tcp_socket.PassAs<net::StreamSocket>(); | |
| 79 } | 87 } |
| 80 | 88 |
| 81 int result = socket_->Connect( | 89 int status = socket_->Connect( |
| 82 base::Bind(&P2PSocketHostTcp::OnConnected, base::Unretained(this))); | 90 base::Bind(&P2PSocketHostTcpBase::OnConnected, |
| 83 if (result != net::ERR_IO_PENDING) { | 91 base::Unretained(this))); |
| 84 OnConnected(result); | 92 if (status != net::ERR_IO_PENDING) { |
| 93 // We defer execution of ProcessConnectDone instead of calling it |
| 94 // directly here as the caller may not expect an error/close to |
| 95 // happen here. This is okay, as from the caller's point of view, |
| 96 // the connect always happens asynchronously. |
| 97 base::MessageLoop* message_loop = base::MessageLoop::current(); |
| 98 CHECK(message_loop); |
| 99 message_loop->PostTask( |
| 100 FROM_HERE, |
| 101 base::Bind(&P2PSocketHostTcpBase::OnConnected, |
| 102 base::Unretained(this), status)); |
| 85 } | 103 } |
| 86 | 104 |
| 87 return state_ != STATE_ERROR; | 105 return state_ != STATE_ERROR; |
| 88 } | 106 } |
| 89 | 107 |
| 90 void P2PSocketHostTcpBase::OnError() { | 108 void P2PSocketHostTcpBase::OnError() { |
| 91 socket_.reset(); | 109 socket_.reset(); |
| 92 | 110 |
| 93 if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING || | 111 if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING || |
| 94 state_ == STATE_OPEN) { | 112 state_ == STATE_OPEN) { |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 pos += consumed; | 296 pos += consumed; |
| 279 } | 297 } |
| 280 // We've consumed all complete packets from the buffer; now move any remaining | 298 // We've consumed all complete packets from the buffer; now move any remaining |
| 281 // bytes to the head of the buffer and set offset to reflect this. | 299 // bytes to the head of the buffer and set offset to reflect this. |
| 282 if (pos && pos <= read_buffer_->offset()) { | 300 if (pos && pos <= read_buffer_->offset()) { |
| 283 memmove(head, head + pos, read_buffer_->offset() - pos); | 301 memmove(head, head + pos, read_buffer_->offset() - pos); |
| 284 read_buffer_->set_offset(read_buffer_->offset() - pos); | 302 read_buffer_->set_offset(read_buffer_->offset() - pos); |
| 285 } | 303 } |
| 286 } | 304 } |
| 287 | 305 |
| 288 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender* message_sender, | 306 P2PSocketHostTcp::P2PSocketHostTcp( |
| 289 int id, | 307 IPC::Sender* message_sender, int id, |
| 290 P2PSocketType type) | 308 P2PSocketType type, net::URLRequestContextGetter* url_context) |
| 291 : P2PSocketHostTcpBase(message_sender, id, type) { | 309 : P2PSocketHostTcpBase(message_sender, id, type, url_context) { |
| 292 DCHECK(type == P2P_SOCKET_TCP_CLIENT || type == P2P_SOCKET_SSLTCP_CLIENT); | 310 DCHECK(type == P2P_SOCKET_TCP_CLIENT || type == P2P_SOCKET_SSLTCP_CLIENT); |
| 293 } | 311 } |
| 294 | 312 |
| 295 P2PSocketHostTcp::~P2PSocketHostTcp() { | 313 P2PSocketHostTcp::~P2PSocketHostTcp() { |
| 296 } | 314 } |
| 297 | 315 |
| 298 int P2PSocketHostTcp::ProcessInput(char* input, int input_len) { | 316 int P2PSocketHostTcp::ProcessInput(char* input, int input_len) { |
| 299 if (input_len < kPacketHeaderSize) | 317 if (input_len < kPacketHeaderSize) |
| 300 return 0; | 318 return 0; |
| 301 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input)); | 319 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 315 int size = kPacketHeaderSize + data.size(); | 333 int size = kPacketHeaderSize + data.size(); |
| 316 scoped_refptr<net::DrainableIOBuffer> buffer = | 334 scoped_refptr<net::DrainableIOBuffer> buffer = |
| 317 new net::DrainableIOBuffer(new net::IOBuffer(size), size); | 335 new net::DrainableIOBuffer(new net::IOBuffer(size), size); |
| 318 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size()); | 336 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size()); |
| 319 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size()); | 337 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size()); |
| 320 | 338 |
| 321 WriteOrQueue(buffer); | 339 WriteOrQueue(buffer); |
| 322 } | 340 } |
| 323 | 341 |
| 324 // P2PSocketHostStunTcp | 342 // P2PSocketHostStunTcp |
| 325 P2PSocketHostStunTcp::P2PSocketHostStunTcp(IPC::Sender* message_sender, | 343 P2PSocketHostStunTcp::P2PSocketHostStunTcp( |
| 326 int id, | 344 IPC::Sender* message_sender, int id, |
| 327 P2PSocketType type) | 345 P2PSocketType type, net::URLRequestContextGetter* url_context) |
| 328 : P2PSocketHostTcpBase(message_sender, id, type) { | 346 : P2PSocketHostTcpBase(message_sender, id, type, url_context) { |
| 329 DCHECK(type == P2P_SOCKET_STUN_TCP_CLIENT || | 347 DCHECK(type == P2P_SOCKET_STUN_TCP_CLIENT || |
| 330 type == P2P_SOCKET_STUN_SSLTCP_CLIENT); | 348 type == P2P_SOCKET_STUN_SSLTCP_CLIENT); |
| 331 } | 349 } |
| 332 | 350 |
| 333 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() { | 351 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() { |
| 334 } | 352 } |
| 335 | 353 |
| 336 int P2PSocketHostStunTcp::ProcessInput(char* input, int input_len) { | 354 int P2PSocketHostStunTcp::ProcessInput(char* input, int input_len) { |
| 337 if (input_len < kPacketHeaderSize + kPacketLengthOffset) | 355 if (input_len < kPacketHeaderSize + kPacketLengthOffset) |
| 338 return 0; | 356 return 0; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 } else { | 425 } else { |
| 408 packet_size += kTurnChannelDataHeaderSize; | 426 packet_size += kTurnChannelDataHeaderSize; |
| 409 // Calculate any padding if present. | 427 // Calculate any padding if present. |
| 410 if (packet_size % 4) | 428 if (packet_size % 4) |
| 411 *pad_bytes = 4 - packet_size % 4; | 429 *pad_bytes = 4 - packet_size % 4; |
| 412 } | 430 } |
| 413 return packet_size; | 431 return packet_size; |
| 414 } | 432 } |
| 415 | 433 |
| 416 } // namespace content | 434 } // namespace content |
| OLD | NEW |