Chromium Code Reviews| 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/proxy_resolving_client_socket.h" | |
| 10 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 12 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
| 13 #include "net/socket/tcp_client_socket.h" | 14 #include "net/socket/tcp_client_socket.h" |
| 15 #include "net/url_request/url_request_context_getter.h" | |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 typedef uint16 PacketLength; | 19 typedef uint16 PacketLength; |
| 18 const int kPacketHeaderSize = sizeof(PacketLength); | 20 const int kPacketHeaderSize = sizeof(PacketLength); |
| 19 const int kReadBufferSize = 4096; | 21 const int kReadBufferSize = 4096; |
| 20 const int kPacketLengthOffset = 2; | 22 const int kPacketLengthOffset = 2; |
| 21 const int kTurnChannelDataHeaderSize = 4; | 23 const int kTurnChannelDataHeaderSize = 4; |
| 22 | 24 |
| 23 } // namespace | 25 } // namespace |
| 24 | 26 |
| 25 namespace content { | 27 namespace content { |
| 26 | 28 |
| 27 P2PSocketHostTcpBase::P2PSocketHostTcpBase(IPC::Sender* message_sender, | 29 P2PSocketHostTcpBase::P2PSocketHostTcpBase( |
| 28 int id) | 30 IPC::Sender* message_sender, int id, |
| 31 net::URLRequestContextGetter* url_context) | |
| 29 : P2PSocketHost(message_sender, id), | 32 : P2PSocketHost(message_sender, id), |
| 33 //weak_ptr_factory_(this), | |
|
juberti
2013/06/21 04:30:35
Remove?
Mallinath (Gone from Chromium)
2013/06/21 05:46:10
Done.
| |
| 30 write_pending_(false), | 34 write_pending_(false), |
| 31 connected_(false) { | 35 connected_(false), |
| 36 url_context_(url_context) { | |
| 32 } | 37 } |
| 33 | 38 |
| 34 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() { | 39 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() { |
| 35 if (state_ == STATE_OPEN) { | 40 if (state_ == STATE_OPEN) { |
| 36 DCHECK(socket_.get()); | 41 DCHECK(socket_.get()); |
| 37 socket_.reset(); | 42 socket_.reset(); |
| 38 } | 43 } |
| 39 } | 44 } |
| 40 | 45 |
| 41 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint& remote_address, | 46 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint& remote_address, |
| 42 net::StreamSocket* socket) { | 47 net::StreamSocket* socket) { |
| 43 DCHECK(socket); | 48 DCHECK(socket); |
| 44 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 49 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 45 | 50 |
| 46 remote_address_ = remote_address; | 51 remote_address_ = remote_address; |
| 47 socket_.reset(socket); | 52 socket_.reset(socket); |
| 48 state_ = STATE_OPEN; | 53 state_ = STATE_OPEN; |
| 49 DoRead(); | 54 DoRead(); |
| 50 return state_ != STATE_ERROR; | 55 return state_ != STATE_ERROR; |
| 51 } | 56 } |
| 52 | 57 |
| 53 bool P2PSocketHostTcpBase::Init(const net::IPEndPoint& local_address, | 58 bool P2PSocketHostTcpBase::Init(const net::IPEndPoint& local_address, |
| 54 const net::IPEndPoint& remote_address) { | 59 const net::IPEndPoint& remote_address) { |
| 55 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 60 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 56 | 61 |
| 57 remote_address_ = remote_address; | 62 remote_address_ = remote_address; |
| 58 state_ = STATE_CONNECTING; | 63 state_ = STATE_CONNECTING; |
| 59 scoped_ptr<net::TCPClientSocket> tcp_socket(new net::TCPClientSocket( | |
| 60 net::AddressList(remote_address), | |
| 61 NULL, net::NetLog::Source())); | |
| 62 if (tcp_socket->Bind(local_address) != net::OK) { | |
| 63 OnError(); | |
| 64 return false; | |
| 65 } | |
| 66 socket_.reset(tcp_socket.release()); | |
| 67 | 64 |
| 68 int result = socket_->Connect( | 65 net::HostPortPair dest_host_port_pair = |
| 69 base::Bind(&P2PSocketHostTcp::OnConnected, base::Unretained(this))); | 66 net::HostPortPair::FromIPEndPoint(remote_address); |
| 70 if (result != net::ERR_IO_PENDING) { | 67 // TODO(mallinath) - We are ignoring local_address altogether. We should |
| 71 OnConnected(result); | 68 // find a way to inject this into ProxyResolvingClientSocket. This could be |
| 69 // a problem on multi-homed host. | |
| 70 | |
| 71 // The default SSLConfig is good enough for us for now. | |
| 72 const net::SSLConfig ssl_config; | |
| 73 socket_.reset(new jingle_glue::ProxyResolvingClientSocket( | |
| 74 NULL, // Default socket pool provided by the net::Proxy. | |
| 75 url_context_, | |
| 76 ssl_config, | |
| 77 dest_host_port_pair)); | |
| 78 | |
| 79 int status = socket_->Connect( | |
| 80 base::Bind(&P2PSocketHostTcpBase::OnConnected, | |
| 81 base::Unretained(this))); | |
| 82 if (status != net::ERR_IO_PENDING) { | |
| 83 // We defer execution of ProcessConnectDone instead of calling it | |
| 84 // directly here as the caller may not expect an error/close to | |
| 85 // happen here. This is okay, as from the caller's point of view, | |
| 86 // the connect always happens asynchronously. | |
| 87 base::MessageLoop* message_loop = base::MessageLoop::current(); | |
| 88 CHECK(message_loop); | |
| 89 message_loop->PostTask( | |
| 90 FROM_HERE, | |
| 91 base::Bind(&P2PSocketHostTcpBase::OnConnected, | |
| 92 base::Unretained(this), status)); | |
| 72 } | 93 } |
| 73 | 94 |
| 74 return state_ != STATE_ERROR; | 95 return state_ != STATE_ERROR; |
| 75 } | 96 } |
| 76 | 97 |
| 77 void P2PSocketHostTcpBase::OnError() { | 98 void P2PSocketHostTcpBase::OnError() { |
| 78 socket_.reset(); | 99 socket_.reset(); |
| 79 | 100 |
| 80 if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING || | 101 if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING || |
| 81 state_ == STATE_OPEN) { | 102 state_ == STATE_OPEN) { |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 pos += consumed; | 286 pos += consumed; |
| 266 } | 287 } |
| 267 // We've consumed all complete packets from the buffer; now move any remaining | 288 // We've consumed all complete packets from the buffer; now move any remaining |
| 268 // bytes to the head of the buffer and set offset to reflect this. | 289 // bytes to the head of the buffer and set offset to reflect this. |
| 269 if (pos && pos <= read_buffer_->offset()) { | 290 if (pos && pos <= read_buffer_->offset()) { |
| 270 memmove(head, head + pos, read_buffer_->offset() - pos); | 291 memmove(head, head + pos, read_buffer_->offset() - pos); |
| 271 read_buffer_->set_offset(read_buffer_->offset() - pos); | 292 read_buffer_->set_offset(read_buffer_->offset() - pos); |
| 272 } | 293 } |
| 273 } | 294 } |
| 274 | 295 |
| 275 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender* message_sender, int id) | 296 P2PSocketHostTcp::P2PSocketHostTcp( |
| 276 : P2PSocketHostTcpBase(message_sender, id) { | 297 IPC::Sender* message_sender, int id, |
| 298 net::URLRequestContextGetter* url_context) | |
| 299 : P2PSocketHostTcpBase(message_sender, id, url_context) { | |
| 277 } | 300 } |
| 278 | 301 |
| 279 P2PSocketHostTcp::~P2PSocketHostTcp() { | 302 P2PSocketHostTcp::~P2PSocketHostTcp() { |
| 280 } | 303 } |
| 281 | 304 |
| 282 int P2PSocketHostTcp::ProcessInput(char* input, int input_len) { | 305 int P2PSocketHostTcp::ProcessInput(char* input, int input_len) { |
| 283 if (input_len < kPacketHeaderSize) | 306 if (input_len < kPacketHeaderSize) |
| 284 return 0; | 307 return 0; |
| 285 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input)); | 308 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input)); |
| 286 if (input_len < packet_size + kPacketHeaderSize) | 309 if (input_len < packet_size + kPacketHeaderSize) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 299 int size = kPacketHeaderSize + data.size(); | 322 int size = kPacketHeaderSize + data.size(); |
| 300 scoped_refptr<net::DrainableIOBuffer> buffer = | 323 scoped_refptr<net::DrainableIOBuffer> buffer = |
| 301 new net::DrainableIOBuffer(new net::IOBuffer(size), size); | 324 new net::DrainableIOBuffer(new net::IOBuffer(size), size); |
| 302 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size()); | 325 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size()); |
| 303 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size()); | 326 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size()); |
| 304 | 327 |
| 305 WriteOrQueue(buffer); | 328 WriteOrQueue(buffer); |
| 306 } | 329 } |
| 307 | 330 |
| 308 // P2PSocketHostStunTcp | 331 // P2PSocketHostStunTcp |
| 309 P2PSocketHostStunTcp::P2PSocketHostStunTcp(IPC::Sender* message_sender, | 332 P2PSocketHostStunTcp::P2PSocketHostStunTcp( |
| 310 int id) | 333 IPC::Sender* message_sender, int id, |
| 311 : P2PSocketHostTcpBase(message_sender, id) { | 334 net::URLRequestContextGetter* url_context) |
| 335 : P2PSocketHostTcpBase(message_sender, id, url_context) { | |
| 312 } | 336 } |
| 313 | 337 |
| 314 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() { | 338 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() { |
| 315 } | 339 } |
| 316 | 340 |
| 317 int P2PSocketHostStunTcp::ProcessInput(char* input, int input_len) { | 341 int P2PSocketHostStunTcp::ProcessInput(char* input, int input_len) { |
| 318 if (input_len < kPacketHeaderSize + kPacketLengthOffset) | 342 if (input_len < kPacketHeaderSize + kPacketLengthOffset) |
| 319 return 0; | 343 return 0; |
| 320 | 344 |
| 321 int pad_bytes; | 345 int pad_bytes; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 388 } else { | 412 } else { |
| 389 packet_size += kTurnChannelDataHeaderSize; | 413 packet_size += kTurnChannelDataHeaderSize; |
| 390 // Calculate any padding if present. | 414 // Calculate any padding if present. |
| 391 if (packet_size % 4) | 415 if (packet_size % 4) |
| 392 *pad_bytes = 4 - packet_size % 4; | 416 *pad_bytes = 4 - packet_size % 4; |
| 393 } | 417 } |
| 394 return packet_size; | 418 return packet_size; |
| 395 } | 419 } |
| 396 | 420 |
| 397 } // namespace content | 421 } // namespace content |
| OLD | NEW |