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/location.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/sys_byteorder.h"
10 #include "content/common/p2p_messages.h"
11 #include "ipc/ipc_sender.h"
12 #include "jingle/glue/fake_ssl_client_socket.h"
13 #include "jingle/glue/proxy_resolving_client_socket.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/net_util.h"
17 #include "net/socket/client_socket_factory.h"
18 #include "net/socket/client_socket_handle.h"
19 #include "net/socket/ssl_client_socket.h"
20 #include "net/socket/tcp_client_socket.h"
21 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_context_getter.h"
23 #include "third_party/webrtc/base/asyncpacketsocket.h"
27 typedef uint16 PacketLength
;
28 const int kPacketHeaderSize
= sizeof(PacketLength
);
29 const int kReadBufferSize
= 4096;
30 const int kPacketLengthOffset
= 2;
31 const int kTurnChannelDataHeaderSize
= 4;
32 const int kRecvSocketBufferSize
= 128 * 1024;
33 const int kSendSocketBufferSize
= 128 * 1024;
35 bool IsTlsClientSocket(content::P2PSocketType type
) {
36 return (type
== content::P2P_SOCKET_STUN_TLS_CLIENT
||
37 type
== content::P2P_SOCKET_TLS_CLIENT
);
40 bool IsPseudoTlsClientSocket(content::P2PSocketType type
) {
41 return (type
== content::P2P_SOCKET_SSLTCP_CLIENT
||
42 type
== content::P2P_SOCKET_STUN_SSLTCP_CLIENT
);
49 P2PSocketHostTcpBase::P2PSocketHostTcpBase(
50 IPC::Sender
* message_sender
,
53 net::URLRequestContextGetter
* url_context
)
54 : P2PSocketHost(message_sender
, socket_id
, P2PSocketHost::TCP
),
55 write_pending_(false),
58 url_context_(url_context
) {
61 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() {
62 if (state_
== STATE_OPEN
) {
63 DCHECK(socket_
.get());
68 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint
& remote_address
,
69 net::StreamSocket
* socket
) {
71 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
73 remote_address_
.ip_address
= remote_address
;
74 // TODO(ronghuawu): Add FakeSSLServerSocket.
75 socket_
.reset(socket
);
78 return state_
!= STATE_ERROR
;
81 bool P2PSocketHostTcpBase::Init(const net::IPEndPoint
& local_address
,
82 const P2PHostAndIPEndPoint
& remote_address
) {
83 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
85 remote_address_
= remote_address
;
86 state_
= STATE_CONNECTING
;
88 net::HostPortPair dest_host_port_pair
;
89 // If there is a domain name, let's try it first, it's required by some proxy
90 // to only take hostname for CONNECT. If it has been DNS resolved, the result
91 // is likely cached and shouldn't cause 2nd DNS resolution in the case of
92 // direct connect (i.e. no proxy).
93 if (!remote_address
.hostname
.empty()) {
94 dest_host_port_pair
= net::HostPortPair(remote_address
.hostname
,
95 remote_address
.ip_address
.port());
97 DCHECK(!remote_address
.ip_address
.address().empty());
98 dest_host_port_pair
= net::HostPortPair::FromIPEndPoint(
99 remote_address
.ip_address
);
102 // TODO(mallinath) - We are ignoring local_address altogether. We should
103 // find a way to inject this into ProxyResolvingClientSocket. This could be
104 // a problem on multi-homed host.
106 // The default SSLConfig is good enough for us for now.
107 const net::SSLConfig ssl_config
;
108 socket_
.reset(new jingle_glue::ProxyResolvingClientSocket(
109 NULL
, // Default socket pool provided by the net::Proxy.
112 dest_host_port_pair
));
114 int status
= socket_
->Connect(
115 base::Bind(&P2PSocketHostTcpBase::OnConnected
,
116 base::Unretained(this)));
117 if (status
!= net::ERR_IO_PENDING
) {
118 // We defer execution of ProcessConnectDone instead of calling it
119 // directly here as the caller may not expect an error/close to
120 // happen here. This is okay, as from the caller's point of view,
121 // the connect always happens asynchronously.
122 base::MessageLoop
* message_loop
= base::MessageLoop::current();
124 message_loop
->task_runner()->PostTask(
125 FROM_HERE
, base::Bind(&P2PSocketHostTcpBase::OnConnected
,
126 base::Unretained(this), status
));
129 return state_
!= STATE_ERROR
;
132 void P2PSocketHostTcpBase::OnError() {
135 if (state_
== STATE_UNINITIALIZED
|| state_
== STATE_CONNECTING
||
136 state_
== STATE_TLS_CONNECTING
|| state_
== STATE_OPEN
) {
137 message_sender_
->Send(new P2PMsg_OnError(id_
));
140 state_
= STATE_ERROR
;
143 void P2PSocketHostTcpBase::OnConnected(int result
) {
144 DCHECK_EQ(state_
, STATE_CONNECTING
);
145 DCHECK_NE(result
, net::ERR_IO_PENDING
);
147 if (result
!= net::OK
) {
148 LOG(WARNING
) << "Error from connecting socket, result=" << result
;
153 if (IsTlsClientSocket(type_
)) {
154 state_
= STATE_TLS_CONNECTING
;
156 } else if (IsPseudoTlsClientSocket(type_
)) {
157 scoped_ptr
<net::StreamSocket
> transport_socket
= socket_
.Pass();
159 new jingle_glue::FakeSSLClientSocket(transport_socket
.Pass()));
160 state_
= STATE_TLS_CONNECTING
;
161 int status
= socket_
->Connect(
162 base::Bind(&P2PSocketHostTcpBase::ProcessTlsSslConnectDone
,
163 base::Unretained(this)));
164 if (status
!= net::ERR_IO_PENDING
) {
165 ProcessTlsSslConnectDone(status
);
168 // If we are not doing TLS, we are ready to send data now.
169 // In case of TLS, SignalConnect will be sent only after TLS handshake is
170 // successfull. So no buffering will be done at socket handlers if any
171 // packets sent before that by the application.
176 void P2PSocketHostTcpBase::StartTls() {
177 DCHECK_EQ(state_
, STATE_TLS_CONNECTING
);
178 DCHECK(socket_
.get());
180 scoped_ptr
<net::ClientSocketHandle
> socket_handle(
181 new net::ClientSocketHandle());
182 socket_handle
->SetSocket(socket_
.Pass());
184 net::SSLClientSocketContext context
;
185 context
.cert_verifier
= url_context_
->GetURLRequestContext()->cert_verifier();
186 context
.transport_security_state
=
187 url_context_
->GetURLRequestContext()->transport_security_state();
188 DCHECK(context
.transport_security_state
);
190 // Default ssl config.
191 const net::SSLConfig ssl_config
;
192 net::HostPortPair dest_host_port_pair
;
194 // Calling net::HostPortPair::FromIPEndPoint will crash if the IP address is
196 if (!remote_address_
.ip_address
.address().empty()) {
197 net::HostPortPair::FromIPEndPoint(remote_address_
.ip_address
);
199 dest_host_port_pair
.set_port(remote_address_
.ip_address
.port());
201 if (!remote_address_
.hostname
.empty())
202 dest_host_port_pair
.set_host(remote_address_
.hostname
);
204 net::ClientSocketFactory
* socket_factory
=
205 net::ClientSocketFactory::GetDefaultFactory();
206 DCHECK(socket_factory
);
208 socket_
= socket_factory
->CreateSSLClientSocket(
209 socket_handle
.Pass(), dest_host_port_pair
, ssl_config
, context
);
210 int status
= socket_
->Connect(
211 base::Bind(&P2PSocketHostTcpBase::ProcessTlsSslConnectDone
,
212 base::Unretained(this)));
214 if (status
!= net::ERR_IO_PENDING
) {
215 ProcessTlsSslConnectDone(status
);
219 void P2PSocketHostTcpBase::ProcessTlsSslConnectDone(int status
) {
220 DCHECK_NE(status
, net::ERR_IO_PENDING
);
221 DCHECK_EQ(state_
, STATE_TLS_CONNECTING
);
222 if (status
!= net::OK
) {
223 LOG(WARNING
) << "Error from connecting TLS socket, status=" << status
;
230 void P2PSocketHostTcpBase::OnOpen() {
232 // Setting socket send and receive buffer size.
233 if (net::OK
!= socket_
->SetReceiveBufferSize(kRecvSocketBufferSize
)) {
234 LOG(WARNING
) << "Failed to set socket receive buffer size to "
235 << kRecvSocketBufferSize
;
238 if (net::OK
!= socket_
->SetSendBufferSize(kSendSocketBufferSize
)) {
239 LOG(WARNING
) << "Failed to set socket send buffer size to "
240 << kSendSocketBufferSize
;
243 if (!DoSendSocketCreateMsg())
246 DCHECK_EQ(state_
, STATE_OPEN
);
250 bool P2PSocketHostTcpBase::DoSendSocketCreateMsg() {
251 DCHECK(socket_
.get());
253 net::IPEndPoint local_address
;
254 int result
= socket_
->GetLocalAddress(&local_address
);
256 LOG(ERROR
) << "P2PSocketHostTcpBase::OnConnected: unable to get local"
257 << " address: " << result
;
262 VLOG(1) << "Local address: " << local_address
.ToString();
264 net::IPEndPoint remote_address
;
266 // GetPeerAddress returns ERR_NAME_NOT_RESOLVED if the socket is connected
268 result
= socket_
->GetPeerAddress(&remote_address
);
269 if (result
< 0 && result
!= net::ERR_NAME_NOT_RESOLVED
) {
270 LOG(ERROR
) << "P2PSocketHostTcpBase::OnConnected: unable to get peer"
271 << " address: " << result
;
276 if (!remote_address
.address().empty()) {
277 VLOG(1) << "Remote address: " << remote_address
.ToString();
278 if (remote_address_
.ip_address
.address().empty()) {
279 // Save |remote_address| if address is empty.
280 remote_address_
.ip_address
= remote_address
;
283 VLOG(1) << "Remote address is unknown since connection is proxied";
286 // If we are not doing TLS, we are ready to send data now.
287 // In case of TLS SignalConnect will be sent only after TLS handshake is
288 // successful. So no buffering will be done at socket handlers if any
289 // packets sent before that by the application.
290 message_sender_
->Send(new P2PMsg_OnSocketCreated(
291 id_
, local_address
, remote_address
));
295 void P2PSocketHostTcpBase::DoRead() {
298 if (!read_buffer_
.get()) {
299 read_buffer_
= new net::GrowableIOBuffer();
300 read_buffer_
->SetCapacity(kReadBufferSize
);
301 } else if (read_buffer_
->RemainingCapacity() < kReadBufferSize
) {
302 // Make sure that we always have at least kReadBufferSize of
303 // remaining capacity in the read buffer. Normally all packets
304 // are smaller than kReadBufferSize, so this is not really
306 read_buffer_
->SetCapacity(read_buffer_
->capacity() + kReadBufferSize
-
307 read_buffer_
->RemainingCapacity());
309 result
= socket_
->Read(
311 read_buffer_
->RemainingCapacity(),
312 base::Bind(&P2PSocketHostTcp::OnRead
, base::Unretained(this)));
313 DidCompleteRead(result
);
314 } while (result
> 0);
317 void P2PSocketHostTcpBase::OnRead(int result
) {
318 DidCompleteRead(result
);
319 if (state_
== STATE_OPEN
) {
324 void P2PSocketHostTcpBase::OnPacket(const std::vector
<char>& data
) {
326 P2PSocketHost::StunMessageType type
;
327 bool stun
= GetStunPacketType(&*data
.begin(), data
.size(), &type
);
328 if (stun
&& IsRequestOrResponse(type
)) {
330 } else if (!stun
|| type
== STUN_DATA_INDICATION
) {
331 LOG(ERROR
) << "Received unexpected data packet from "
332 << remote_address_
.ip_address
.ToString()
333 << " before STUN binding is finished. "
334 << "Terminating connection.";
340 message_sender_
->Send(new P2PMsg_OnDataReceived(
341 id_
, remote_address_
.ip_address
, data
, base::TimeTicks::Now()));
343 if (dump_incoming_rtp_packet_
)
344 DumpRtpPacket(&data
[0], data
.size(), true);
347 // Note: dscp is not actually used on TCP sockets as this point,
348 // but may be honored in the future.
349 void P2PSocketHostTcpBase::Send(const net::IPEndPoint
& to
,
350 const std::vector
<char>& data
,
351 const rtc::PacketOptions
& options
,
354 // The Send message may be sent after the an OnError message was
355 // sent by hasn't been processed the renderer.
359 if (!(to
== remote_address_
.ip_address
)) {
360 // Renderer should use this socket only to send data to |remote_address_|.
367 P2PSocketHost::StunMessageType type
= P2PSocketHost::StunMessageType();
368 bool stun
= GetStunPacketType(&*data
.begin(), data
.size(), &type
);
369 if (!stun
|| type
== STUN_DATA_INDICATION
) {
370 LOG(ERROR
) << "Page tried to send a data packet to " << to
.ToString()
371 << " before STUN binding is finished.";
377 DoSend(to
, data
, options
);
380 void P2PSocketHostTcpBase::WriteOrQueue(
381 scoped_refptr
<net::DrainableIOBuffer
>& buffer
) {
382 IncrementTotalSentPackets();
383 if (write_buffer_
.get()) {
384 write_queue_
.push(buffer
);
385 IncrementDelayedPackets();
386 IncrementDelayedBytes(buffer
->size());
390 write_buffer_
= buffer
;
394 void P2PSocketHostTcpBase::DoWrite() {
395 while (write_buffer_
.get() && state_
== STATE_OPEN
&& !write_pending_
) {
396 int result
= socket_
->Write(
398 write_buffer_
->BytesRemaining(),
399 base::Bind(&P2PSocketHostTcp::OnWritten
, base::Unretained(this)));
400 HandleWriteResult(result
);
404 void P2PSocketHostTcpBase::OnWritten(int result
) {
405 DCHECK(write_pending_
);
406 DCHECK_NE(result
, net::ERR_IO_PENDING
);
408 write_pending_
= false;
409 HandleWriteResult(result
);
413 void P2PSocketHostTcpBase::HandleWriteResult(int result
) {
414 DCHECK(write_buffer_
.get());
416 write_buffer_
->DidConsume(result
);
417 if (write_buffer_
->BytesRemaining() == 0) {
418 message_sender_
->Send(
419 new P2PMsg_OnSendComplete(id_
, P2PSendPacketMetrics()));
420 if (write_queue_
.empty()) {
421 write_buffer_
= NULL
;
423 write_buffer_
= write_queue_
.front();
425 // Update how many bytes are still waiting to be sent.
426 DecrementDelayedBytes(write_buffer_
->size());
429 } else if (result
== net::ERR_IO_PENDING
) {
430 write_pending_
= true;
432 LOG(ERROR
) << "Error when sending data in TCP socket: " << result
;
437 P2PSocketHost
* P2PSocketHostTcpBase::AcceptIncomingTcpConnection(
438 const net::IPEndPoint
& remote_address
, int id
) {
444 void P2PSocketHostTcpBase::DidCompleteRead(int result
) {
445 DCHECK_EQ(state_
, STATE_OPEN
);
447 if (result
== net::ERR_IO_PENDING
) {
449 } else if (result
< 0) {
450 LOG(ERROR
) << "Error when reading from TCP socket: " << result
;
453 } else if (result
== 0) {
454 LOG(WARNING
) << "Remote peer has shutdown TCP socket.";
459 read_buffer_
->set_offset(read_buffer_
->offset() + result
);
460 char* head
= read_buffer_
->StartOfBuffer(); // Purely a convenience.
462 while (pos
<= read_buffer_
->offset() && state_
== STATE_OPEN
) {
463 int consumed
= ProcessInput(head
+ pos
, read_buffer_
->offset() - pos
);
468 // We've consumed all complete packets from the buffer; now move any remaining
469 // bytes to the head of the buffer and set offset to reflect this.
470 if (pos
&& pos
<= read_buffer_
->offset()) {
471 memmove(head
, head
+ pos
, read_buffer_
->offset() - pos
);
472 read_buffer_
->set_offset(read_buffer_
->offset() - pos
);
476 bool P2PSocketHostTcpBase::SetOption(P2PSocketOption option
, int value
) {
477 DCHECK_EQ(STATE_OPEN
, state_
);
479 case P2P_SOCKET_OPT_RCVBUF
:
480 return socket_
->SetReceiveBufferSize(value
) == net::OK
;
481 case P2P_SOCKET_OPT_SNDBUF
:
482 return socket_
->SetSendBufferSize(value
) == net::OK
;
483 case P2P_SOCKET_OPT_DSCP
:
484 return false; // For TCP sockets DSCP setting is not available.
491 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender
* message_sender
,
494 net::URLRequestContextGetter
* url_context
)
495 : P2PSocketHostTcpBase(message_sender
, socket_id
, type
, url_context
) {
496 DCHECK(type
== P2P_SOCKET_TCP_CLIENT
||
497 type
== P2P_SOCKET_SSLTCP_CLIENT
||
498 type
== P2P_SOCKET_TLS_CLIENT
);
501 P2PSocketHostTcp::~P2PSocketHostTcp() {
504 int P2PSocketHostTcp::ProcessInput(char* input
, int input_len
) {
505 if (input_len
< kPacketHeaderSize
)
507 int packet_size
= base::NetToHost16(*reinterpret_cast<uint16
*>(input
));
508 if (input_len
< packet_size
+ kPacketHeaderSize
)
511 int consumed
= kPacketHeaderSize
;
512 char* cur
= input
+ consumed
;
513 std::vector
<char> data(cur
, cur
+ packet_size
);
515 consumed
+= packet_size
;
519 void P2PSocketHostTcp::DoSend(const net::IPEndPoint
& to
,
520 const std::vector
<char>& data
,
521 const rtc::PacketOptions
& options
) {
522 int size
= kPacketHeaderSize
+ data
.size();
523 scoped_refptr
<net::DrainableIOBuffer
> buffer
=
524 new net::DrainableIOBuffer(new net::IOBuffer(size
), size
);
525 *reinterpret_cast<uint16
*>(buffer
->data()) = base::HostToNet16(data
.size());
526 memcpy(buffer
->data() + kPacketHeaderSize
, &data
[0], data
.size());
528 packet_processing_helpers::ApplyPacketOptions(
529 buffer
->data() + kPacketHeaderSize
,
530 buffer
->BytesRemaining() - kPacketHeaderSize
,
533 WriteOrQueue(buffer
);
536 // P2PSocketHostStunTcp
537 P2PSocketHostStunTcp::P2PSocketHostStunTcp(
538 IPC::Sender
* message_sender
,
541 net::URLRequestContextGetter
* url_context
)
542 : P2PSocketHostTcpBase(message_sender
, socket_id
, type
, url_context
) {
543 DCHECK(type
== P2P_SOCKET_STUN_TCP_CLIENT
||
544 type
== P2P_SOCKET_STUN_SSLTCP_CLIENT
||
545 type
== P2P_SOCKET_STUN_TLS_CLIENT
);
548 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() {
551 int P2PSocketHostStunTcp::ProcessInput(char* input
, int input_len
) {
552 if (input_len
< kPacketHeaderSize
+ kPacketLengthOffset
)
556 int packet_size
= GetExpectedPacketSize(
557 input
, input_len
, &pad_bytes
);
559 if (input_len
< packet_size
+ pad_bytes
)
562 // We have a complete packet. Read through it.
565 std::vector
<char> data(cur
, cur
+ packet_size
);
567 consumed
+= packet_size
;
568 consumed
+= pad_bytes
;
572 void P2PSocketHostStunTcp::DoSend(const net::IPEndPoint
& to
,
573 const std::vector
<char>& data
,
574 const rtc::PacketOptions
& options
) {
575 // Each packet is expected to have header (STUN/TURN ChannelData), where
576 // header contains message type and and length of message.
577 if (data
.size() < kPacketHeaderSize
+ kPacketLengthOffset
) {
584 size_t expected_len
= GetExpectedPacketSize(
585 &data
[0], data
.size(), &pad_bytes
);
587 // Accepts only complete STUN/TURN packets.
588 if (data
.size() != expected_len
) {
594 // Add any pad bytes to the total size.
595 int size
= data
.size() + pad_bytes
;
597 scoped_refptr
<net::DrainableIOBuffer
> buffer
=
598 new net::DrainableIOBuffer(new net::IOBuffer(size
), size
);
599 memcpy(buffer
->data(), &data
[0], data
.size());
601 packet_processing_helpers::ApplyPacketOptions(
602 buffer
->data(), data
.size(), options
, 0);
605 char padding
[4] = {0};
606 DCHECK_LE(pad_bytes
, 4);
607 memcpy(buffer
->data() + data
.size(), padding
, pad_bytes
);
609 WriteOrQueue(buffer
);
611 if (dump_outgoing_rtp_packet_
)
612 DumpRtpPacket(buffer
->data(), data
.size(), false);
615 int P2PSocketHostStunTcp::GetExpectedPacketSize(
616 const char* data
, int len
, int* pad_bytes
) {
617 DCHECK_LE(kTurnChannelDataHeaderSize
, len
);
618 // Both stun and turn had length at offset 2.
619 int packet_size
= base::NetToHost16(*reinterpret_cast<const uint16
*>(
620 data
+ kPacketLengthOffset
));
622 // Get packet type (STUN or TURN).
623 uint16 msg_type
= base::NetToHost16(*reinterpret_cast<const uint16
*>(data
));
626 // Add heder length to packet length.
627 if ((msg_type
& 0xC000) == 0) {
628 packet_size
+= kStunHeaderSize
;
630 packet_size
+= kTurnChannelDataHeaderSize
;
631 // Calculate any padding if present.
633 *pad_bytes
= 4 - packet_size
% 4;
638 } // namespace content