Ignore non-active fullscreen windows for shelf state.
[chromium-blink-merge.git] / content / browser / renderer_host / p2p / socket_host_tcp.cc
blob2900541dcb26890ba1ad7bee75029b7bf74eaf6c
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"
22 namespace {
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);
42 } // namespace
44 namespace content {
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),
51 connected_(false),
52 type_(type),
53 url_context_(url_context) {
56 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() {
57 if (state_ == STATE_OPEN) {
58 DCHECK(socket_.get());
59 socket_.reset();
63 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint& remote_address,
64 net::StreamSocket* socket) {
65 DCHECK(socket);
66 DCHECK_EQ(state_, STATE_UNINITIALIZED);
68 remote_address_ = remote_address;
69 // TODO(ronghuawu): Add FakeSSLServerSocket.
70 socket_.reset(socket);
71 state_ = STATE_OPEN;
72 DoRead();
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.
93 url_context_,
94 ssl_config,
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();
106 CHECK(message_loop);
107 message_loop->PostTask(
108 FROM_HERE,
109 base::Bind(&P2PSocketHostTcpBase::OnConnected,
110 base::Unretained(this), status));
113 return state_ != STATE_ERROR;
116 void P2PSocketHostTcpBase::OnError() {
117 socket_.reset();
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) {
132 OnError();
133 return;
136 if (IsTlsClientSocket(type_)) {
137 state_ = STATE_TLS_CONNECTING;
138 StartTls();
139 } else if (IsPseudoTlsClientSocket(type_)) {
140 scoped_ptr<net::StreamSocket> transport_socket = socket_.Pass();
141 socket_.reset(
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);
150 } else {
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.
155 OnOpen();
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) {
195 OnError();
196 return;
198 OnOpen();
201 void P2PSocketHostTcpBase::OnOpen() {
202 state_ = STATE_OPEN;
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();
215 DoRead();
218 void P2PSocketHostTcpBase::DoSendSocketCreateMsg() {
219 DCHECK(socket_.get());
221 net::IPEndPoint address;
222 int result = socket_->GetLocalAddress(&address);
223 if (result < 0) {
224 LOG(ERROR) << "P2PSocketHostTcpBase::OnConnected: unable to get local"
225 << " address: " << result;
226 OnError();
227 return;
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() {
240 int result;
241 do {
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
249 // required.
250 read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize -
251 read_buffer_->RemainingCapacity());
253 result = socket_->Read(
254 read_buffer_.get(),
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) {
264 DoRead();
268 void P2PSocketHostTcpBase::OnPacket(const std::vector<char>& data) {
269 if (!connected_) {
270 P2PSocketHost::StunMessageType type;
271 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
272 if (stun && IsRequestOrResponse(type)) {
273 connected_ = true;
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.";
279 OnError();
280 return;
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,
292 uint64 packet_id) {
293 if (!socket_) {
294 // The Send message may be sent after the an OnError message was
295 // sent by hasn't been processed the renderer.
296 return;
299 if (!(to == remote_address_)) {
300 // Renderer should use this socket only to send data to |remote_address_|.
301 NOTREACHED();
302 OnError();
303 return;
306 if (!connected_) {
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.";
312 OnError();
313 return;
317 DoSend(to, data);
320 void P2PSocketHostTcpBase::WriteOrQueue(
321 scoped_refptr<net::DrainableIOBuffer>& buffer) {
322 if (write_buffer_.get()) {
323 write_queue_.push(buffer);
324 return;
327 write_buffer_ = buffer;
328 DoWrite();
331 void P2PSocketHostTcpBase::DoWrite() {
332 while (write_buffer_.get() && state_ == STATE_OPEN && !write_pending_) {
333 int result = socket_->Write(
334 write_buffer_.get(),
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);
347 DoWrite();
350 void P2PSocketHostTcpBase::HandleWriteResult(int result) {
351 DCHECK(write_buffer_.get());
352 if (result >= 0) {
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;
358 } else {
359 write_buffer_ = write_queue_.front();
360 write_queue_.pop();
363 } else if (result == net::ERR_IO_PENDING) {
364 write_pending_ = true;
365 } else {
366 LOG(ERROR) << "Error when sending data in TCP socket: " << result;
367 OnError();
371 P2PSocketHost* P2PSocketHostTcpBase::AcceptIncomingTcpConnection(
372 const net::IPEndPoint& remote_address, int id) {
373 NOTREACHED();
374 OnError();
375 return NULL;
378 void P2PSocketHostTcpBase::DidCompleteRead(int result) {
379 DCHECK_EQ(state_, STATE_OPEN);
381 if (result == net::ERR_IO_PENDING) {
382 return;
383 } else if (result < 0) {
384 LOG(ERROR) << "Error when reading from TCP socket: " << result;
385 OnError();
386 return;
389 read_buffer_->set_offset(read_buffer_->offset() + result);
390 char* head = read_buffer_->StartOfBuffer(); // Purely a convenience.
391 int pos = 0;
392 while (pos <= read_buffer_->offset() && state_ == STATE_OPEN) {
393 int consumed = ProcessInput(head + pos, read_buffer_->offset() - pos);
394 if (!consumed)
395 break;
396 pos += consumed;
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)
420 return 0;
421 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input));
422 if (input_len < packet_size + kPacketHeaderSize)
423 return 0;
425 int consumed = kPacketHeaderSize;
426 char* cur = input + consumed;
427 std::vector<char> data(cur, cur + packet_size);
428 OnPacket(data);
429 consumed += packet_size;
430 return consumed;
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)
459 return 0;
461 int pad_bytes;
462 int packet_size = GetExpectedPacketSize(
463 input, input_len, &pad_bytes);
465 if (input_len < packet_size + pad_bytes)
466 return 0;
468 // We have a complete packet. Read through it.
469 int consumed = 0;
470 char* cur = input;
471 std::vector<char> data(cur, cur + packet_size);
472 OnPacket(data);
473 consumed += packet_size;
474 consumed += pad_bytes;
475 return consumed;
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) {
483 NOTREACHED();
484 OnError();
485 return;
488 int pad_bytes;
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) {
494 NOTREACHED();
495 OnError();
496 return;
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());
506 if (pad_bytes) {
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));
524 *pad_bytes = 0;
525 // Add heder length to packet length.
526 if ((msg_type & 0xC000) == 0) {
527 packet_size += kStunHeaderSize;
528 } else {
529 packet_size += kTurnChannelDataHeaderSize;
530 // Calculate any padding if present.
531 if (packet_size % 4)
532 *pad_bytes = 4 - packet_size % 4;
534 return packet_size;
537 } // namespace content