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 "content/browser/renderer_host/p2p/socket_host_tcp.h"
7 #include "base/sys_byteorder.h"
8 #include "content/common/p2p_messages.h"
9 #include "ipc/ipc_sender.h"
10 #include "jingle/glue/fake_ssl_client_socket.h"
11 #include "jingle/glue/proxy_resolving_client_socket.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/net_util.h"
15 #include "net/socket/client_socket_factory.h"
16 #include "net/socket/client_socket_handle.h"
17 #include "net/socket/ssl_client_socket.h"
18 #include "net/socket/tcp_client_socket.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_getter.h"
24 typedef uint16 PacketLength
;
25 const int kPacketHeaderSize
= sizeof(PacketLength
);
26 const int kReadBufferSize
= 4096;
27 const int kPacketLengthOffset
= 2;
28 const int kTurnChannelDataHeaderSize
= 4;
29 const int kRecvSocketBufferSize
= 128 * 1024;
30 const int kSendSocketBufferSize
= 128 * 1024;
32 bool IsTlsClientSocket(content::P2PSocketType type
) {
33 return (type
== content::P2P_SOCKET_STUN_TLS_CLIENT
||
34 type
== content::P2P_SOCKET_TLS_CLIENT
);
37 bool IsPseudoTlsClientSocket(content::P2PSocketType type
) {
38 return (type
== content::P2P_SOCKET_SSLTCP_CLIENT
||
39 type
== content::P2P_SOCKET_STUN_SSLTCP_CLIENT
);
46 P2PSocketHostTcpBase::P2PSocketHostTcpBase(
47 IPC::Sender
* message_sender
, int id
,
48 P2PSocketType type
, net::URLRequestContextGetter
* url_context
)
49 : P2PSocketHost(message_sender
, id
),
50 write_pending_(false),
53 url_context_(url_context
) {
56 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() {
57 if (state_
== STATE_OPEN
) {
58 DCHECK(socket_
.get());
63 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint
& remote_address
,
64 net::StreamSocket
* socket
) {
66 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
68 remote_address_
= remote_address
;
69 // TODO(ronghuawu): Add FakeSSLServerSocket.
70 socket_
.reset(socket
);
73 return state_
!= STATE_ERROR
;
76 bool P2PSocketHostTcpBase::Init(const net::IPEndPoint
& local_address
,
77 const net::IPEndPoint
& remote_address
) {
78 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
80 remote_address_
= remote_address
;
81 state_
= STATE_CONNECTING
;
83 net::HostPortPair dest_host_port_pair
=
84 net::HostPortPair::FromIPEndPoint(remote_address
);
85 // TODO(mallinath) - We are ignoring local_address altogether. We should
86 // find a way to inject this into ProxyResolvingClientSocket. This could be
87 // a problem on multi-homed host.
89 // The default SSLConfig is good enough for us for now.
90 const net::SSLConfig ssl_config
;
91 socket_
.reset(new jingle_glue::ProxyResolvingClientSocket(
92 NULL
, // Default socket pool provided by the net::Proxy.
95 dest_host_port_pair
));
97 int status
= socket_
->Connect(
98 base::Bind(&P2PSocketHostTcpBase::OnConnected
,
99 base::Unretained(this)));
100 if (status
!= net::ERR_IO_PENDING
) {
101 // We defer execution of ProcessConnectDone instead of calling it
102 // directly here as the caller may not expect an error/close to
103 // happen here. This is okay, as from the caller's point of view,
104 // the connect always happens asynchronously.
105 base::MessageLoop
* message_loop
= base::MessageLoop::current();
107 message_loop
->PostTask(
109 base::Bind(&P2PSocketHostTcpBase::OnConnected
,
110 base::Unretained(this), status
));
113 return state_
!= STATE_ERROR
;
116 void P2PSocketHostTcpBase::OnError() {
119 if (state_
== STATE_UNINITIALIZED
|| state_
== STATE_CONNECTING
||
120 state_
== STATE_TLS_CONNECTING
|| state_
== STATE_OPEN
) {
121 message_sender_
->Send(new P2PMsg_OnError(id_
));
124 state_
= STATE_ERROR
;
127 void P2PSocketHostTcpBase::OnConnected(int result
) {
128 DCHECK_EQ(state_
, STATE_CONNECTING
);
129 DCHECK_NE(result
, net::ERR_IO_PENDING
);
131 if (result
!= net::OK
) {
136 if (IsTlsClientSocket(type_
)) {
137 state_
= STATE_TLS_CONNECTING
;
139 } else if (IsPseudoTlsClientSocket(type_
)) {
140 scoped_ptr
<net::StreamSocket
> transport_socket
= socket_
.Pass();
142 new jingle_glue::FakeSSLClientSocket(transport_socket
.Pass()));
143 state_
= STATE_TLS_CONNECTING
;
144 int status
= socket_
->Connect(
145 base::Bind(&P2PSocketHostTcpBase::ProcessTlsSslConnectDone
,
146 base::Unretained(this)));
147 if (status
!= net::ERR_IO_PENDING
) {
148 ProcessTlsSslConnectDone(status
);
151 // If we are not doing TLS, we are ready to send data now.
152 // In case of TLS, SignalConnect will be sent only after TLS handshake is
153 // successfull. So no buffering will be done at socket handlers if any
154 // packets sent before that by the application.
159 void P2PSocketHostTcpBase::StartTls() {
160 DCHECK_EQ(state_
, STATE_TLS_CONNECTING
);
161 DCHECK(socket_
.get());
163 scoped_ptr
<net::ClientSocketHandle
> socket_handle(
164 new net::ClientSocketHandle());
165 socket_handle
->SetSocket(socket_
.Pass());
167 net::SSLClientSocketContext context
;
168 context
.cert_verifier
= url_context_
->GetURLRequestContext()->cert_verifier();
169 context
.transport_security_state
=
170 url_context_
->GetURLRequestContext()->transport_security_state();
171 DCHECK(context
.transport_security_state
);
173 // Default ssl config.
174 const net::SSLConfig ssl_config
;
175 net::HostPortPair dest_host_port_pair
=
176 net::HostPortPair::FromIPEndPoint(remote_address_
);
177 net::ClientSocketFactory
* socket_factory
=
178 net::ClientSocketFactory::GetDefaultFactory();
179 DCHECK(socket_factory
);
181 socket_
= socket_factory
->CreateSSLClientSocket(
182 socket_handle
.Pass(), dest_host_port_pair
, ssl_config
, context
);
183 int status
= socket_
->Connect(
184 base::Bind(&P2PSocketHostTcpBase::ProcessTlsSslConnectDone
,
185 base::Unretained(this)));
186 if (status
!= net::ERR_IO_PENDING
) {
187 ProcessTlsSslConnectDone(status
);
191 void P2PSocketHostTcpBase::ProcessTlsSslConnectDone(int status
) {
192 DCHECK_NE(status
, net::ERR_IO_PENDING
);
193 DCHECK_EQ(state_
, STATE_TLS_CONNECTING
);
194 if (status
!= net::OK
) {
201 void P2PSocketHostTcpBase::OnOpen() {
203 // Setting socket send and receive buffer size.
204 if (!socket_
->SetReceiveBufferSize(kRecvSocketBufferSize
)) {
205 LOG(WARNING
) << "Failed to set socket receive buffer size to "
206 << kRecvSocketBufferSize
;
209 if (!socket_
->SetSendBufferSize(kSendSocketBufferSize
)) {
210 LOG(WARNING
) << "Failed to set socket send buffer size to "
211 << kSendSocketBufferSize
;
214 DoSendSocketCreateMsg();
218 void P2PSocketHostTcpBase::DoSendSocketCreateMsg() {
219 DCHECK(socket_
.get());
221 net::IPEndPoint address
;
222 int result
= socket_
->GetLocalAddress(&address
);
224 LOG(ERROR
) << "P2PSocketHostTcpBase::OnConnected: unable to get local"
225 << " address: " << result
;
230 VLOG(1) << "Local address: " << address
.ToString();
232 // If we are not doing TLS, we are ready to send data now.
233 // In case of TLS SignalConnect will be sent only after TLS handshake is
234 // successfull. So no buffering will be done at socket handlers if any
235 // packets sent before that by the application.
236 message_sender_
->Send(new P2PMsg_OnSocketCreated(id_
, address
));
239 void P2PSocketHostTcpBase::DoRead() {
242 if (!read_buffer_
.get()) {
243 read_buffer_
= new net::GrowableIOBuffer();
244 read_buffer_
->SetCapacity(kReadBufferSize
);
245 } else if (read_buffer_
->RemainingCapacity() < kReadBufferSize
) {
246 // Make sure that we always have at least kReadBufferSize of
247 // remaining capacity in the read buffer. Normally all packets
248 // are smaller than kReadBufferSize, so this is not really
250 read_buffer_
->SetCapacity(read_buffer_
->capacity() + kReadBufferSize
-
251 read_buffer_
->RemainingCapacity());
253 result
= socket_
->Read(
255 read_buffer_
->RemainingCapacity(),
256 base::Bind(&P2PSocketHostTcp::OnRead
, base::Unretained(this)));
257 DidCompleteRead(result
);
258 } while (result
> 0);
261 void P2PSocketHostTcpBase::OnRead(int result
) {
262 DidCompleteRead(result
);
263 if (state_
== STATE_OPEN
) {
268 void P2PSocketHostTcpBase::OnPacket(const std::vector
<char>& data
) {
270 P2PSocketHost::StunMessageType type
;
271 bool stun
= GetStunPacketType(&*data
.begin(), data
.size(), &type
);
272 if (stun
&& IsRequestOrResponse(type
)) {
274 } else if (!stun
|| type
== STUN_DATA_INDICATION
) {
275 LOG(ERROR
) << "Received unexpected data packet from "
276 << remote_address_
.ToString()
277 << " before STUN binding is finished. "
278 << "Terminating connection.";
284 message_sender_
->Send(new P2PMsg_OnDataReceived(id_
, remote_address_
, data
));
287 // Note: dscp is not actually used on TCP sockets as this point,
288 // but may be honored in the future.
289 void P2PSocketHostTcpBase::Send(const net::IPEndPoint
& to
,
290 const std::vector
<char>& data
,
291 net::DiffServCodePoint dscp
,
294 // The Send message may be sent after the an OnError message was
295 // sent by hasn't been processed the renderer.
299 if (!(to
== remote_address_
)) {
300 // Renderer should use this socket only to send data to |remote_address_|.
307 P2PSocketHost::StunMessageType type
= P2PSocketHost::StunMessageType();
308 bool stun
= GetStunPacketType(&*data
.begin(), data
.size(), &type
);
309 if (!stun
|| type
== STUN_DATA_INDICATION
) {
310 LOG(ERROR
) << "Page tried to send a data packet to " << to
.ToString()
311 << " before STUN binding is finished.";
320 void P2PSocketHostTcpBase::WriteOrQueue(
321 scoped_refptr
<net::DrainableIOBuffer
>& buffer
) {
322 if (write_buffer_
.get()) {
323 write_queue_
.push(buffer
);
327 write_buffer_
= buffer
;
331 void P2PSocketHostTcpBase::DoWrite() {
332 while (write_buffer_
.get() && state_
== STATE_OPEN
&& !write_pending_
) {
333 int result
= socket_
->Write(
335 write_buffer_
->BytesRemaining(),
336 base::Bind(&P2PSocketHostTcp::OnWritten
, base::Unretained(this)));
337 HandleWriteResult(result
);
341 void P2PSocketHostTcpBase::OnWritten(int result
) {
342 DCHECK(write_pending_
);
343 DCHECK_NE(result
, net::ERR_IO_PENDING
);
345 write_pending_
= false;
346 HandleWriteResult(result
);
350 void P2PSocketHostTcpBase::HandleWriteResult(int result
) {
351 DCHECK(write_buffer_
.get());
353 write_buffer_
->DidConsume(result
);
354 if (write_buffer_
->BytesRemaining() == 0) {
355 message_sender_
->Send(new P2PMsg_OnSendComplete(id_
));
356 if (write_queue_
.empty()) {
357 write_buffer_
= NULL
;
359 write_buffer_
= write_queue_
.front();
363 } else if (result
== net::ERR_IO_PENDING
) {
364 write_pending_
= true;
366 LOG(ERROR
) << "Error when sending data in TCP socket: " << result
;
371 P2PSocketHost
* P2PSocketHostTcpBase::AcceptIncomingTcpConnection(
372 const net::IPEndPoint
& remote_address
, int id
) {
378 void P2PSocketHostTcpBase::DidCompleteRead(int result
) {
379 DCHECK_EQ(state_
, STATE_OPEN
);
381 if (result
== net::ERR_IO_PENDING
) {
383 } else if (result
< 0) {
384 LOG(ERROR
) << "Error when reading from TCP socket: " << result
;
389 read_buffer_
->set_offset(read_buffer_
->offset() + result
);
390 char* head
= read_buffer_
->StartOfBuffer(); // Purely a convenience.
392 while (pos
<= read_buffer_
->offset() && state_
== STATE_OPEN
) {
393 int consumed
= ProcessInput(head
+ pos
, read_buffer_
->offset() - pos
);
398 // We've consumed all complete packets from the buffer; now move any remaining
399 // bytes to the head of the buffer and set offset to reflect this.
400 if (pos
&& pos
<= read_buffer_
->offset()) {
401 memmove(head
, head
+ pos
, read_buffer_
->offset() - pos
);
402 read_buffer_
->set_offset(read_buffer_
->offset() - pos
);
406 P2PSocketHostTcp::P2PSocketHostTcp(
407 IPC::Sender
* message_sender
, int id
,
408 P2PSocketType type
, net::URLRequestContextGetter
* url_context
)
409 : P2PSocketHostTcpBase(message_sender
, id
, type
, url_context
) {
410 DCHECK(type
== P2P_SOCKET_TCP_CLIENT
||
411 type
== P2P_SOCKET_SSLTCP_CLIENT
||
412 type
== P2P_SOCKET_TLS_CLIENT
);
415 P2PSocketHostTcp::~P2PSocketHostTcp() {
418 int P2PSocketHostTcp::ProcessInput(char* input
, int input_len
) {
419 if (input_len
< kPacketHeaderSize
)
421 int packet_size
= base::NetToHost16(*reinterpret_cast<uint16
*>(input
));
422 if (input_len
< packet_size
+ kPacketHeaderSize
)
425 int consumed
= kPacketHeaderSize
;
426 char* cur
= input
+ consumed
;
427 std::vector
<char> data(cur
, cur
+ packet_size
);
429 consumed
+= packet_size
;
433 void P2PSocketHostTcp::DoSend(const net::IPEndPoint
& to
,
434 const std::vector
<char>& data
) {
435 int size
= kPacketHeaderSize
+ data
.size();
436 scoped_refptr
<net::DrainableIOBuffer
> buffer
=
437 new net::DrainableIOBuffer(new net::IOBuffer(size
), size
);
438 *reinterpret_cast<uint16
*>(buffer
->data()) = base::HostToNet16(data
.size());
439 memcpy(buffer
->data() + kPacketHeaderSize
, &data
[0], data
.size());
441 WriteOrQueue(buffer
);
444 // P2PSocketHostStunTcp
445 P2PSocketHostStunTcp::P2PSocketHostStunTcp(
446 IPC::Sender
* message_sender
, int id
,
447 P2PSocketType type
, net::URLRequestContextGetter
* url_context
)
448 : P2PSocketHostTcpBase(message_sender
, id
, type
, url_context
) {
449 DCHECK(type
== P2P_SOCKET_STUN_TCP_CLIENT
||
450 type
== P2P_SOCKET_STUN_SSLTCP_CLIENT
||
451 type
== P2P_SOCKET_STUN_TLS_CLIENT
);
454 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() {
457 int P2PSocketHostStunTcp::ProcessInput(char* input
, int input_len
) {
458 if (input_len
< kPacketHeaderSize
+ kPacketLengthOffset
)
462 int packet_size
= GetExpectedPacketSize(
463 input
, input_len
, &pad_bytes
);
465 if (input_len
< packet_size
+ pad_bytes
)
468 // We have a complete packet. Read through it.
471 std::vector
<char> data(cur
, cur
+ packet_size
);
473 consumed
+= packet_size
;
474 consumed
+= pad_bytes
;
478 void P2PSocketHostStunTcp::DoSend(const net::IPEndPoint
& to
,
479 const std::vector
<char>& data
) {
480 // Each packet is expected to have header (STUN/TURN ChannelData), where
481 // header contains message type and and length of message.
482 if (data
.size() < kPacketHeaderSize
+ kPacketLengthOffset
) {
489 size_t expected_len
= GetExpectedPacketSize(
490 &data
[0], data
.size(), &pad_bytes
);
492 // Accepts only complete STUN/TURN packets.
493 if (data
.size() != expected_len
) {
499 // Add any pad bytes to the total size.
500 int size
= data
.size() + pad_bytes
;
502 scoped_refptr
<net::DrainableIOBuffer
> buffer
=
503 new net::DrainableIOBuffer(new net::IOBuffer(size
), size
);
504 memcpy(buffer
->data(), &data
[0], data
.size());
507 char padding
[4] = {0};
508 DCHECK_LE(pad_bytes
, 4);
509 memcpy(buffer
->data() + data
.size(), padding
, pad_bytes
);
511 WriteOrQueue(buffer
);
514 int P2PSocketHostStunTcp::GetExpectedPacketSize(
515 const char* data
, int len
, int* pad_bytes
) {
516 DCHECK_LE(kTurnChannelDataHeaderSize
, len
);
517 // Both stun and turn had length at offset 2.
518 int packet_size
= base::NetToHost16(*reinterpret_cast<const uint16
*>(
519 data
+ kPacketLengthOffset
));
521 // Get packet type (STUN or TURN).
522 uint16 msg_type
= base::NetToHost16(*reinterpret_cast<const uint16
*>(data
));
525 // Add heder length to packet length.
526 if ((msg_type
& 0xC000) == 0) {
527 packet_size
+= kStunHeaderSize
;
529 packet_size
+= kTurnChannelDataHeaderSize
;
530 // Calculate any padding if present.
532 *pad_bytes
= 4 - packet_size
% 4;
537 } // namespace content