Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / renderer_host / p2p / socket_host_tcp.cc
blob0273b646cbc3872a39cdb816adc48b391b095a94
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"
25 namespace {
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);
45 } // namespace
47 namespace content {
49 P2PSocketHostTcpBase::P2PSocketHostTcpBase(
50 IPC::Sender* message_sender,
51 int socket_id,
52 P2PSocketType type,
53 net::URLRequestContextGetter* url_context)
54 : P2PSocketHost(message_sender, socket_id, P2PSocketHost::TCP),
55 write_pending_(false),
56 connected_(false),
57 type_(type),
58 url_context_(url_context) {
61 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() {
62 if (state_ == STATE_OPEN) {
63 DCHECK(socket_.get());
64 socket_.reset();
68 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint& remote_address,
69 net::StreamSocket* socket) {
70 DCHECK(socket);
71 DCHECK_EQ(state_, STATE_UNINITIALIZED);
73 remote_address_.ip_address = remote_address;
74 // TODO(ronghuawu): Add FakeSSLServerSocket.
75 socket_.reset(socket);
76 state_ = STATE_OPEN;
77 DoRead();
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());
96 } else {
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.
110 url_context_,
111 ssl_config,
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();
123 CHECK(message_loop);
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() {
133 socket_.reset();
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;
149 OnError();
150 return;
153 if (IsTlsClientSocket(type_)) {
154 state_ = STATE_TLS_CONNECTING;
155 StartTls();
156 } else if (IsPseudoTlsClientSocket(type_)) {
157 scoped_ptr<net::StreamSocket> transport_socket = socket_.Pass();
158 socket_.reset(
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);
167 } else {
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.
172 OnOpen();
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
195 // empty.
196 if (!remote_address_.ip_address.address().empty()) {
197 net::HostPortPair::FromIPEndPoint(remote_address_.ip_address);
198 } else {
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;
224 OnError();
225 return;
227 OnOpen();
230 void P2PSocketHostTcpBase::OnOpen() {
231 state_ = STATE_OPEN;
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())
244 return;
246 DCHECK_EQ(state_, STATE_OPEN);
247 DoRead();
250 bool P2PSocketHostTcpBase::DoSendSocketCreateMsg() {
251 DCHECK(socket_.get());
253 net::IPEndPoint local_address;
254 int result = socket_->GetLocalAddress(&local_address);
255 if (result < 0) {
256 LOG(ERROR) << "P2PSocketHostTcpBase::OnConnected: unable to get local"
257 << " address: " << result;
258 OnError();
259 return false;
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
267 // through a proxy.
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;
272 OnError();
273 return false;
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;
282 } else {
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));
292 return true;
295 void P2PSocketHostTcpBase::DoRead() {
296 int result;
297 do {
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
305 // required.
306 read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize -
307 read_buffer_->RemainingCapacity());
309 result = socket_->Read(
310 read_buffer_.get(),
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) {
320 DoRead();
324 void P2PSocketHostTcpBase::OnPacket(const std::vector<char>& data) {
325 if (!connected_) {
326 P2PSocketHost::StunMessageType type;
327 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
328 if (stun && IsRequestOrResponse(type)) {
329 connected_ = true;
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.";
335 OnError();
336 return;
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,
352 uint64 packet_id) {
353 if (!socket_) {
354 // The Send message may be sent after the an OnError message was
355 // sent by hasn't been processed the renderer.
356 return;
359 if (!(to == remote_address_.ip_address)) {
360 // Renderer should use this socket only to send data to |remote_address_|.
361 NOTREACHED();
362 OnError();
363 return;
366 if (!connected_) {
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.";
372 OnError();
373 return;
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());
387 return;
390 write_buffer_ = buffer;
391 DoWrite();
394 void P2PSocketHostTcpBase::DoWrite() {
395 while (write_buffer_.get() && state_ == STATE_OPEN && !write_pending_) {
396 int result = socket_->Write(
397 write_buffer_.get(),
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);
410 DoWrite();
413 void P2PSocketHostTcpBase::HandleWriteResult(int result) {
414 DCHECK(write_buffer_.get());
415 if (result >= 0) {
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;
422 } else {
423 write_buffer_ = write_queue_.front();
424 write_queue_.pop();
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;
431 } else {
432 LOG(ERROR) << "Error when sending data in TCP socket: " << result;
433 OnError();
437 P2PSocketHost* P2PSocketHostTcpBase::AcceptIncomingTcpConnection(
438 const net::IPEndPoint& remote_address, int id) {
439 NOTREACHED();
440 OnError();
441 return NULL;
444 void P2PSocketHostTcpBase::DidCompleteRead(int result) {
445 DCHECK_EQ(state_, STATE_OPEN);
447 if (result == net::ERR_IO_PENDING) {
448 return;
449 } else if (result < 0) {
450 LOG(ERROR) << "Error when reading from TCP socket: " << result;
451 OnError();
452 return;
453 } else if (result == 0) {
454 LOG(WARNING) << "Remote peer has shutdown TCP socket.";
455 OnError();
456 return;
459 read_buffer_->set_offset(read_buffer_->offset() + result);
460 char* head = read_buffer_->StartOfBuffer(); // Purely a convenience.
461 int pos = 0;
462 while (pos <= read_buffer_->offset() && state_ == STATE_OPEN) {
463 int consumed = ProcessInput(head + pos, read_buffer_->offset() - pos);
464 if (!consumed)
465 break;
466 pos += consumed;
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_);
478 switch (option) {
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.
485 default:
486 NOTREACHED();
487 return false;
491 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender* message_sender,
492 int socket_id,
493 P2PSocketType type,
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)
506 return 0;
507 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input));
508 if (input_len < packet_size + kPacketHeaderSize)
509 return 0;
511 int consumed = kPacketHeaderSize;
512 char* cur = input + consumed;
513 std::vector<char> data(cur, cur + packet_size);
514 OnPacket(data);
515 consumed += packet_size;
516 return consumed;
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,
531 options, 0);
533 WriteOrQueue(buffer);
536 // P2PSocketHostStunTcp
537 P2PSocketHostStunTcp::P2PSocketHostStunTcp(
538 IPC::Sender* message_sender,
539 int socket_id,
540 P2PSocketType type,
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)
553 return 0;
555 int pad_bytes;
556 int packet_size = GetExpectedPacketSize(
557 input, input_len, &pad_bytes);
559 if (input_len < packet_size + pad_bytes)
560 return 0;
562 // We have a complete packet. Read through it.
563 int consumed = 0;
564 char* cur = input;
565 std::vector<char> data(cur, cur + packet_size);
566 OnPacket(data);
567 consumed += packet_size;
568 consumed += pad_bytes;
569 return consumed;
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) {
578 NOTREACHED();
579 OnError();
580 return;
583 int pad_bytes;
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) {
589 NOTREACHED();
590 OnError();
591 return;
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);
604 if (pad_bytes) {
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));
625 *pad_bytes = 0;
626 // Add heder length to packet length.
627 if ((msg_type & 0xC000) == 0) {
628 packet_size += kStunHeaderSize;
629 } else {
630 packet_size += kTurnChannelDataHeaderSize;
631 // Calculate any padding if present.
632 if (packet_size % 4)
633 *pad_bytes = 4 - packet_size % 4;
635 return packet_size;
638 } // namespace content