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(
285 id_
, remote_address_
, data
, base::TimeTicks::Now()));
288 // Note: dscp is not actually used on TCP sockets as this point,
289 // but may be honored in the future.
290 void P2PSocketHostTcpBase::Send(const net::IPEndPoint
& to
,
291 const std::vector
<char>& data
,
292 net::DiffServCodePoint dscp
,
295 // The Send message may be sent after the an OnError message was
296 // sent by hasn't been processed the renderer.
300 if (!(to
== remote_address_
)) {
301 // Renderer should use this socket only to send data to |remote_address_|.
308 P2PSocketHost::StunMessageType type
= P2PSocketHost::StunMessageType();
309 bool stun
= GetStunPacketType(&*data
.begin(), data
.size(), &type
);
310 if (!stun
|| type
== STUN_DATA_INDICATION
) {
311 LOG(ERROR
) << "Page tried to send a data packet to " << to
.ToString()
312 << " before STUN binding is finished.";
321 void P2PSocketHostTcpBase::WriteOrQueue(
322 scoped_refptr
<net::DrainableIOBuffer
>& buffer
) {
323 if (write_buffer_
.get()) {
324 write_queue_
.push(buffer
);
328 write_buffer_
= buffer
;
332 void P2PSocketHostTcpBase::DoWrite() {
333 while (write_buffer_
.get() && state_
== STATE_OPEN
&& !write_pending_
) {
334 int result
= socket_
->Write(
336 write_buffer_
->BytesRemaining(),
337 base::Bind(&P2PSocketHostTcp::OnWritten
, base::Unretained(this)));
338 HandleWriteResult(result
);
342 void P2PSocketHostTcpBase::OnWritten(int result
) {
343 DCHECK(write_pending_
);
344 DCHECK_NE(result
, net::ERR_IO_PENDING
);
346 write_pending_
= false;
347 HandleWriteResult(result
);
351 void P2PSocketHostTcpBase::HandleWriteResult(int result
) {
352 DCHECK(write_buffer_
.get());
354 write_buffer_
->DidConsume(result
);
355 if (write_buffer_
->BytesRemaining() == 0) {
356 message_sender_
->Send(new P2PMsg_OnSendComplete(id_
));
357 if (write_queue_
.empty()) {
358 write_buffer_
= NULL
;
360 write_buffer_
= write_queue_
.front();
364 } else if (result
== net::ERR_IO_PENDING
) {
365 write_pending_
= true;
367 LOG(ERROR
) << "Error when sending data in TCP socket: " << result
;
372 P2PSocketHost
* P2PSocketHostTcpBase::AcceptIncomingTcpConnection(
373 const net::IPEndPoint
& remote_address
, int id
) {
379 void P2PSocketHostTcpBase::DidCompleteRead(int result
) {
380 DCHECK_EQ(state_
, STATE_OPEN
);
382 if (result
== net::ERR_IO_PENDING
) {
384 } else if (result
< 0) {
385 LOG(ERROR
) << "Error when reading from TCP socket: " << result
;
390 read_buffer_
->set_offset(read_buffer_
->offset() + result
);
391 char* head
= read_buffer_
->StartOfBuffer(); // Purely a convenience.
393 while (pos
<= read_buffer_
->offset() && state_
== STATE_OPEN
) {
394 int consumed
= ProcessInput(head
+ pos
, read_buffer_
->offset() - pos
);
399 // We've consumed all complete packets from the buffer; now move any remaining
400 // bytes to the head of the buffer and set offset to reflect this.
401 if (pos
&& pos
<= read_buffer_
->offset()) {
402 memmove(head
, head
+ pos
, read_buffer_
->offset() - pos
);
403 read_buffer_
->set_offset(read_buffer_
->offset() - pos
);
407 P2PSocketHostTcp::P2PSocketHostTcp(
408 IPC::Sender
* message_sender
, int id
,
409 P2PSocketType type
, net::URLRequestContextGetter
* url_context
)
410 : P2PSocketHostTcpBase(message_sender
, id
, type
, url_context
) {
411 DCHECK(type
== P2P_SOCKET_TCP_CLIENT
||
412 type
== P2P_SOCKET_SSLTCP_CLIENT
||
413 type
== P2P_SOCKET_TLS_CLIENT
);
416 P2PSocketHostTcp::~P2PSocketHostTcp() {
419 int P2PSocketHostTcp::ProcessInput(char* input
, int input_len
) {
420 if (input_len
< kPacketHeaderSize
)
422 int packet_size
= base::NetToHost16(*reinterpret_cast<uint16
*>(input
));
423 if (input_len
< packet_size
+ kPacketHeaderSize
)
426 int consumed
= kPacketHeaderSize
;
427 char* cur
= input
+ consumed
;
428 std::vector
<char> data(cur
, cur
+ packet_size
);
430 consumed
+= packet_size
;
434 void P2PSocketHostTcp::DoSend(const net::IPEndPoint
& to
,
435 const std::vector
<char>& data
) {
436 int size
= kPacketHeaderSize
+ data
.size();
437 scoped_refptr
<net::DrainableIOBuffer
> buffer
=
438 new net::DrainableIOBuffer(new net::IOBuffer(size
), size
);
439 *reinterpret_cast<uint16
*>(buffer
->data()) = base::HostToNet16(data
.size());
440 memcpy(buffer
->data() + kPacketHeaderSize
, &data
[0], data
.size());
442 WriteOrQueue(buffer
);
445 // P2PSocketHostStunTcp
446 P2PSocketHostStunTcp::P2PSocketHostStunTcp(
447 IPC::Sender
* message_sender
, int id
,
448 P2PSocketType type
, net::URLRequestContextGetter
* url_context
)
449 : P2PSocketHostTcpBase(message_sender
, id
, type
, url_context
) {
450 DCHECK(type
== P2P_SOCKET_STUN_TCP_CLIENT
||
451 type
== P2P_SOCKET_STUN_SSLTCP_CLIENT
||
452 type
== P2P_SOCKET_STUN_TLS_CLIENT
);
455 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() {
458 int P2PSocketHostStunTcp::ProcessInput(char* input
, int input_len
) {
459 if (input_len
< kPacketHeaderSize
+ kPacketLengthOffset
)
463 int packet_size
= GetExpectedPacketSize(
464 input
, input_len
, &pad_bytes
);
466 if (input_len
< packet_size
+ pad_bytes
)
469 // We have a complete packet. Read through it.
472 std::vector
<char> data(cur
, cur
+ packet_size
);
474 consumed
+= packet_size
;
475 consumed
+= pad_bytes
;
479 void P2PSocketHostStunTcp::DoSend(const net::IPEndPoint
& to
,
480 const std::vector
<char>& data
) {
481 // Each packet is expected to have header (STUN/TURN ChannelData), where
482 // header contains message type and and length of message.
483 if (data
.size() < kPacketHeaderSize
+ kPacketLengthOffset
) {
490 size_t expected_len
= GetExpectedPacketSize(
491 &data
[0], data
.size(), &pad_bytes
);
493 // Accepts only complete STUN/TURN packets.
494 if (data
.size() != expected_len
) {
500 // Add any pad bytes to the total size.
501 int size
= data
.size() + pad_bytes
;
503 scoped_refptr
<net::DrainableIOBuffer
> buffer
=
504 new net::DrainableIOBuffer(new net::IOBuffer(size
), size
);
505 memcpy(buffer
->data(), &data
[0], data
.size());
508 char padding
[4] = {0};
509 DCHECK_LE(pad_bytes
, 4);
510 memcpy(buffer
->data() + data
.size(), padding
, pad_bytes
);
512 WriteOrQueue(buffer
);
515 int P2PSocketHostStunTcp::GetExpectedPacketSize(
516 const char* data
, int len
, int* pad_bytes
) {
517 DCHECK_LE(kTurnChannelDataHeaderSize
, len
);
518 // Both stun and turn had length at offset 2.
519 int packet_size
= base::NetToHost16(*reinterpret_cast<const uint16
*>(
520 data
+ kPacketLengthOffset
));
522 // Get packet type (STUN or TURN).
523 uint16 msg_type
= base::NetToHost16(*reinterpret_cast<const uint16
*>(data
));
526 // Add heder length to packet length.
527 if ((msg_type
& 0xC000) == 0) {
528 packet_size
+= kStunHeaderSize
;
530 packet_size
+= kTurnChannelDataHeaderSize
;
531 // Calculate any padding if present.
533 *pad_bytes
= 4 - packet_size
% 4;
538 } // namespace content