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