Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / renderer_host / p2p / socket_host_tcp.cc
blob1ee1627b5d72d9f1175415c1db67b61052b5aa25
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"
21 #include "third_party/webrtc/base/asyncpacketsocket.h"
23 namespace {
25 typedef uint16 PacketLength;
26 const int kPacketHeaderSize = sizeof(PacketLength);
27 const int kReadBufferSize = 4096;
28 const int kPacketLengthOffset = 2;
29 const int kTurnChannelDataHeaderSize = 4;
30 const int kRecvSocketBufferSize = 128 * 1024;
31 const int kSendSocketBufferSize = 128 * 1024;
33 bool IsTlsClientSocket(content::P2PSocketType type) {
34 return (type == content::P2P_SOCKET_STUN_TLS_CLIENT ||
35 type == content::P2P_SOCKET_TLS_CLIENT);
38 bool IsPseudoTlsClientSocket(content::P2PSocketType type) {
39 return (type == content::P2P_SOCKET_SSLTCP_CLIENT ||
40 type == content::P2P_SOCKET_STUN_SSLTCP_CLIENT);
43 } // namespace
45 namespace content {
47 P2PSocketHostTcpBase::P2PSocketHostTcpBase(
48 IPC::Sender* message_sender,
49 int socket_id,
50 P2PSocketType type,
51 net::URLRequestContextGetter* url_context)
52 : P2PSocketHost(message_sender, socket_id),
53 write_pending_(false),
54 connected_(false),
55 type_(type),
56 url_context_(url_context) {
59 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() {
60 if (state_ == STATE_OPEN) {
61 DCHECK(socket_.get());
62 socket_.reset();
66 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint& remote_address,
67 net::StreamSocket* socket) {
68 DCHECK(socket);
69 DCHECK_EQ(state_, STATE_UNINITIALIZED);
71 remote_address_.ip_address = remote_address;
72 // TODO(ronghuawu): Add FakeSSLServerSocket.
73 socket_.reset(socket);
74 state_ = STATE_OPEN;
75 DoRead();
76 return state_ != STATE_ERROR;
79 bool P2PSocketHostTcpBase::Init(const net::IPEndPoint& local_address,
80 const P2PHostAndIPEndPoint& remote_address) {
81 DCHECK_EQ(state_, STATE_UNINITIALIZED);
83 remote_address_ = remote_address;
84 state_ = STATE_CONNECTING;
86 net::HostPortPair dest_host_port_pair;
87 // If there is no resolved address, let's try with domain name, assuming
88 // socket layer will do the DNS resolve.
89 if (remote_address.ip_address.address().empty()) {
90 DCHECK(!remote_address.hostname.empty());
91 dest_host_port_pair = net::HostPortPair::FromString(
92 remote_address.hostname);
93 } else {
94 dest_host_port_pair = net::HostPortPair::FromIPEndPoint(
95 remote_address.ip_address);
98 // TODO(mallinath) - We are ignoring local_address altogether. We should
99 // find a way to inject this into ProxyResolvingClientSocket. This could be
100 // a problem on multi-homed host.
102 // The default SSLConfig is good enough for us for now.
103 const net::SSLConfig ssl_config;
104 socket_.reset(new jingle_glue::ProxyResolvingClientSocket(
105 NULL, // Default socket pool provided by the net::Proxy.
106 url_context_,
107 ssl_config,
108 dest_host_port_pair));
110 int status = socket_->Connect(
111 base::Bind(&P2PSocketHostTcpBase::OnConnected,
112 base::Unretained(this)));
113 if (status != net::ERR_IO_PENDING) {
114 // We defer execution of ProcessConnectDone instead of calling it
115 // directly here as the caller may not expect an error/close to
116 // happen here. This is okay, as from the caller's point of view,
117 // the connect always happens asynchronously.
118 base::MessageLoop* message_loop = base::MessageLoop::current();
119 CHECK(message_loop);
120 message_loop->PostTask(
121 FROM_HERE,
122 base::Bind(&P2PSocketHostTcpBase::OnConnected,
123 base::Unretained(this), status));
126 return state_ != STATE_ERROR;
129 void P2PSocketHostTcpBase::OnError() {
130 socket_.reset();
132 if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING ||
133 state_ == STATE_TLS_CONNECTING || state_ == STATE_OPEN) {
134 message_sender_->Send(new P2PMsg_OnError(id_));
137 state_ = STATE_ERROR;
140 void P2PSocketHostTcpBase::OnConnected(int result) {
141 DCHECK_EQ(state_, STATE_CONNECTING);
142 DCHECK_NE(result, net::ERR_IO_PENDING);
144 if (result != net::OK) {
145 OnError();
146 return;
149 if (IsTlsClientSocket(type_)) {
150 state_ = STATE_TLS_CONNECTING;
151 StartTls();
152 } else if (IsPseudoTlsClientSocket(type_)) {
153 scoped_ptr<net::StreamSocket> transport_socket = socket_.Pass();
154 socket_.reset(
155 new jingle_glue::FakeSSLClientSocket(transport_socket.Pass()));
156 state_ = STATE_TLS_CONNECTING;
157 int status = socket_->Connect(
158 base::Bind(&P2PSocketHostTcpBase::ProcessTlsSslConnectDone,
159 base::Unretained(this)));
160 if (status != net::ERR_IO_PENDING) {
161 ProcessTlsSslConnectDone(status);
163 } else {
164 // If we are not doing TLS, we are ready to send data now.
165 // In case of TLS, SignalConnect will be sent only after TLS handshake is
166 // successfull. So no buffering will be done at socket handlers if any
167 // packets sent before that by the application.
168 OnOpen();
172 void P2PSocketHostTcpBase::StartTls() {
173 DCHECK_EQ(state_, STATE_TLS_CONNECTING);
174 DCHECK(socket_.get());
176 scoped_ptr<net::ClientSocketHandle> socket_handle(
177 new net::ClientSocketHandle());
178 socket_handle->SetSocket(socket_.Pass());
180 net::SSLClientSocketContext context;
181 context.cert_verifier = url_context_->GetURLRequestContext()->cert_verifier();
182 context.transport_security_state =
183 url_context_->GetURLRequestContext()->transport_security_state();
184 DCHECK(context.transport_security_state);
186 // Default ssl config.
187 const net::SSLConfig ssl_config;
188 net::HostPortPair dest_host_port_pair;
190 // Calling net::HostPortPair::FromIPEndPoint will crash if the IP address is
191 // empty.
192 if (!remote_address_.ip_address.address().empty()) {
193 net::HostPortPair::FromIPEndPoint(remote_address_.ip_address);
194 } else {
195 dest_host_port_pair.set_port(remote_address_.ip_address.port());
197 if (!remote_address_.hostname.empty())
198 dest_host_port_pair.set_host(remote_address_.hostname);
200 net::ClientSocketFactory* socket_factory =
201 net::ClientSocketFactory::GetDefaultFactory();
202 DCHECK(socket_factory);
204 socket_ = socket_factory->CreateSSLClientSocket(
205 socket_handle.Pass(), dest_host_port_pair, ssl_config, context);
206 int status = socket_->Connect(
207 base::Bind(&P2PSocketHostTcpBase::ProcessTlsSslConnectDone,
208 base::Unretained(this)));
209 if (status != net::ERR_IO_PENDING) {
210 ProcessTlsSslConnectDone(status);
214 void P2PSocketHostTcpBase::ProcessTlsSslConnectDone(int status) {
215 DCHECK_NE(status, net::ERR_IO_PENDING);
216 DCHECK_EQ(state_, STATE_TLS_CONNECTING);
217 if (status != net::OK) {
218 OnError();
219 return;
221 OnOpen();
224 void P2PSocketHostTcpBase::OnOpen() {
225 state_ = STATE_OPEN;
226 // Setting socket send and receive buffer size.
227 if (net::OK != socket_->SetReceiveBufferSize(kRecvSocketBufferSize)) {
228 LOG(WARNING) << "Failed to set socket receive buffer size to "
229 << kRecvSocketBufferSize;
232 if (net::OK != socket_->SetSendBufferSize(kSendSocketBufferSize)) {
233 LOG(WARNING) << "Failed to set socket send buffer size to "
234 << kSendSocketBufferSize;
237 DoSendSocketCreateMsg();
238 DoRead();
241 void P2PSocketHostTcpBase::DoSendSocketCreateMsg() {
242 DCHECK(socket_.get());
244 net::IPEndPoint local_address;
245 int result = socket_->GetLocalAddress(&local_address);
246 if (result < 0) {
247 LOG(ERROR) << "P2PSocketHostTcpBase::OnConnected: unable to get local"
248 << " address: " << result;
249 OnError();
250 return;
253 VLOG(1) << "Local address: " << local_address.ToString();
255 net::IPEndPoint remote_address;
256 result = socket_->GetPeerAddress(&remote_address);
257 if (result < 0) {
258 LOG(ERROR) << "P2PSocketHostTcpBase::OnConnected: unable to get peer"
259 << " address: " << result;
260 OnError();
261 return;
263 VLOG(1) << "Remote address: " << remote_address.ToString();
264 if (remote_address_.ip_address.address().empty()) {
265 // Save |remote_address| if address is empty.
266 remote_address_.ip_address = remote_address;
269 // If we are not doing TLS, we are ready to send data now.
270 // In case of TLS SignalConnect will be sent only after TLS handshake is
271 // successfull. So no buffering will be done at socket handlers if any
272 // packets sent before that by the application.
273 message_sender_->Send(new P2PMsg_OnSocketCreated(
274 id_, local_address, remote_address));
277 void P2PSocketHostTcpBase::DoRead() {
278 int result;
279 do {
280 if (!read_buffer_.get()) {
281 read_buffer_ = new net::GrowableIOBuffer();
282 read_buffer_->SetCapacity(kReadBufferSize);
283 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) {
284 // Make sure that we always have at least kReadBufferSize of
285 // remaining capacity in the read buffer. Normally all packets
286 // are smaller than kReadBufferSize, so this is not really
287 // required.
288 read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize -
289 read_buffer_->RemainingCapacity());
291 result = socket_->Read(
292 read_buffer_.get(),
293 read_buffer_->RemainingCapacity(),
294 base::Bind(&P2PSocketHostTcp::OnRead, base::Unretained(this)));
295 DidCompleteRead(result);
296 } while (result > 0);
299 void P2PSocketHostTcpBase::OnRead(int result) {
300 DidCompleteRead(result);
301 if (state_ == STATE_OPEN) {
302 DoRead();
306 void P2PSocketHostTcpBase::OnPacket(const std::vector<char>& data) {
307 if (!connected_) {
308 P2PSocketHost::StunMessageType type;
309 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
310 if (stun && IsRequestOrResponse(type)) {
311 connected_ = true;
312 } else if (!stun || type == STUN_DATA_INDICATION) {
313 LOG(ERROR) << "Received unexpected data packet from "
314 << remote_address_.ip_address.ToString()
315 << " before STUN binding is finished. "
316 << "Terminating connection.";
317 OnError();
318 return;
322 message_sender_->Send(new P2PMsg_OnDataReceived(
323 id_, remote_address_.ip_address, data, base::TimeTicks::Now()));
325 if (dump_incoming_rtp_packet_)
326 DumpRtpPacket(&data[0], data.size(), true);
329 // Note: dscp is not actually used on TCP sockets as this point,
330 // but may be honored in the future.
331 void P2PSocketHostTcpBase::Send(const net::IPEndPoint& to,
332 const std::vector<char>& data,
333 const rtc::PacketOptions& options,
334 uint64 packet_id) {
335 if (!socket_) {
336 // The Send message may be sent after the an OnError message was
337 // sent by hasn't been processed the renderer.
338 return;
341 if (!(to == remote_address_.ip_address)) {
342 // Renderer should use this socket only to send data to |remote_address_|.
343 NOTREACHED();
344 OnError();
345 return;
348 if (!connected_) {
349 P2PSocketHost::StunMessageType type = P2PSocketHost::StunMessageType();
350 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
351 if (!stun || type == STUN_DATA_INDICATION) {
352 LOG(ERROR) << "Page tried to send a data packet to " << to.ToString()
353 << " before STUN binding is finished.";
354 OnError();
355 return;
359 DoSend(to, data, options);
362 void P2PSocketHostTcpBase::WriteOrQueue(
363 scoped_refptr<net::DrainableIOBuffer>& buffer) {
364 if (write_buffer_.get()) {
365 write_queue_.push(buffer);
366 return;
369 write_buffer_ = buffer;
370 DoWrite();
373 void P2PSocketHostTcpBase::DoWrite() {
374 while (write_buffer_.get() && state_ == STATE_OPEN && !write_pending_) {
375 int result = socket_->Write(
376 write_buffer_.get(),
377 write_buffer_->BytesRemaining(),
378 base::Bind(&P2PSocketHostTcp::OnWritten, base::Unretained(this)));
379 HandleWriteResult(result);
383 void P2PSocketHostTcpBase::OnWritten(int result) {
384 DCHECK(write_pending_);
385 DCHECK_NE(result, net::ERR_IO_PENDING);
387 write_pending_ = false;
388 HandleWriteResult(result);
389 DoWrite();
392 void P2PSocketHostTcpBase::HandleWriteResult(int result) {
393 DCHECK(write_buffer_.get());
394 if (result >= 0) {
395 write_buffer_->DidConsume(result);
396 if (write_buffer_->BytesRemaining() == 0) {
397 message_sender_->Send(new P2PMsg_OnSendComplete(id_));
398 if (write_queue_.empty()) {
399 write_buffer_ = NULL;
400 } else {
401 write_buffer_ = write_queue_.front();
402 write_queue_.pop();
405 } else if (result == net::ERR_IO_PENDING) {
406 write_pending_ = true;
407 } else {
408 LOG(ERROR) << "Error when sending data in TCP socket: " << result;
409 OnError();
413 P2PSocketHost* P2PSocketHostTcpBase::AcceptIncomingTcpConnection(
414 const net::IPEndPoint& remote_address, int id) {
415 NOTREACHED();
416 OnError();
417 return NULL;
420 void P2PSocketHostTcpBase::DidCompleteRead(int result) {
421 DCHECK_EQ(state_, STATE_OPEN);
423 if (result == net::ERR_IO_PENDING) {
424 return;
425 } else if (result < 0) {
426 LOG(ERROR) << "Error when reading from TCP socket: " << result;
427 OnError();
428 return;
431 read_buffer_->set_offset(read_buffer_->offset() + result);
432 char* head = read_buffer_->StartOfBuffer(); // Purely a convenience.
433 int pos = 0;
434 while (pos <= read_buffer_->offset() && state_ == STATE_OPEN) {
435 int consumed = ProcessInput(head + pos, read_buffer_->offset() - pos);
436 if (!consumed)
437 break;
438 pos += consumed;
440 // We've consumed all complete packets from the buffer; now move any remaining
441 // bytes to the head of the buffer and set offset to reflect this.
442 if (pos && pos <= read_buffer_->offset()) {
443 memmove(head, head + pos, read_buffer_->offset() - pos);
444 read_buffer_->set_offset(read_buffer_->offset() - pos);
448 bool P2PSocketHostTcpBase::SetOption(P2PSocketOption option, int value) {
449 DCHECK_EQ(STATE_OPEN, state_);
450 switch (option) {
451 case P2P_SOCKET_OPT_RCVBUF:
452 return socket_->SetReceiveBufferSize(value) == net::OK;
453 case P2P_SOCKET_OPT_SNDBUF:
454 return socket_->SetSendBufferSize(value) == net::OK;
455 case P2P_SOCKET_OPT_DSCP:
456 return false; // For TCP sockets DSCP setting is not available.
457 default:
458 NOTREACHED();
459 return false;
463 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender* message_sender,
464 int socket_id,
465 P2PSocketType type,
466 net::URLRequestContextGetter* url_context)
467 : P2PSocketHostTcpBase(message_sender, socket_id, type, url_context) {
468 DCHECK(type == P2P_SOCKET_TCP_CLIENT ||
469 type == P2P_SOCKET_SSLTCP_CLIENT ||
470 type == P2P_SOCKET_TLS_CLIENT);
473 P2PSocketHostTcp::~P2PSocketHostTcp() {
476 int P2PSocketHostTcp::ProcessInput(char* input, int input_len) {
477 if (input_len < kPacketHeaderSize)
478 return 0;
479 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input));
480 if (input_len < packet_size + kPacketHeaderSize)
481 return 0;
483 int consumed = kPacketHeaderSize;
484 char* cur = input + consumed;
485 std::vector<char> data(cur, cur + packet_size);
486 OnPacket(data);
487 consumed += packet_size;
488 return consumed;
491 void P2PSocketHostTcp::DoSend(const net::IPEndPoint& to,
492 const std::vector<char>& data,
493 const rtc::PacketOptions& options) {
494 int size = kPacketHeaderSize + data.size();
495 scoped_refptr<net::DrainableIOBuffer> buffer =
496 new net::DrainableIOBuffer(new net::IOBuffer(size), size);
497 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size());
498 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size());
500 packet_processing_helpers::ApplyPacketOptions(
501 buffer->data() + kPacketHeaderSize,
502 buffer->BytesRemaining() - kPacketHeaderSize,
503 options, 0);
505 WriteOrQueue(buffer);
508 // P2PSocketHostStunTcp
509 P2PSocketHostStunTcp::P2PSocketHostStunTcp(
510 IPC::Sender* message_sender,
511 int socket_id,
512 P2PSocketType type,
513 net::URLRequestContextGetter* url_context)
514 : P2PSocketHostTcpBase(message_sender, socket_id, type, url_context) {
515 DCHECK(type == P2P_SOCKET_STUN_TCP_CLIENT ||
516 type == P2P_SOCKET_STUN_SSLTCP_CLIENT ||
517 type == P2P_SOCKET_STUN_TLS_CLIENT);
520 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() {
523 int P2PSocketHostStunTcp::ProcessInput(char* input, int input_len) {
524 if (input_len < kPacketHeaderSize + kPacketLengthOffset)
525 return 0;
527 int pad_bytes;
528 int packet_size = GetExpectedPacketSize(
529 input, input_len, &pad_bytes);
531 if (input_len < packet_size + pad_bytes)
532 return 0;
534 // We have a complete packet. Read through it.
535 int consumed = 0;
536 char* cur = input;
537 std::vector<char> data(cur, cur + packet_size);
538 OnPacket(data);
539 consumed += packet_size;
540 consumed += pad_bytes;
541 return consumed;
544 void P2PSocketHostStunTcp::DoSend(const net::IPEndPoint& to,
545 const std::vector<char>& data,
546 const rtc::PacketOptions& options) {
547 // Each packet is expected to have header (STUN/TURN ChannelData), where
548 // header contains message type and and length of message.
549 if (data.size() < kPacketHeaderSize + kPacketLengthOffset) {
550 NOTREACHED();
551 OnError();
552 return;
555 int pad_bytes;
556 size_t expected_len = GetExpectedPacketSize(
557 &data[0], data.size(), &pad_bytes);
559 // Accepts only complete STUN/TURN packets.
560 if (data.size() != expected_len) {
561 NOTREACHED();
562 OnError();
563 return;
566 // Add any pad bytes to the total size.
567 int size = data.size() + pad_bytes;
569 scoped_refptr<net::DrainableIOBuffer> buffer =
570 new net::DrainableIOBuffer(new net::IOBuffer(size), size);
571 memcpy(buffer->data(), &data[0], data.size());
573 packet_processing_helpers::ApplyPacketOptions(
574 buffer->data(), data.size(), options, 0);
576 if (pad_bytes) {
577 char padding[4] = {0};
578 DCHECK_LE(pad_bytes, 4);
579 memcpy(buffer->data() + data.size(), padding, pad_bytes);
581 WriteOrQueue(buffer);
583 if (dump_outgoing_rtp_packet_)
584 DumpRtpPacket(buffer->data(), data.size(), false);
587 int P2PSocketHostStunTcp::GetExpectedPacketSize(
588 const char* data, int len, int* pad_bytes) {
589 DCHECK_LE(kTurnChannelDataHeaderSize, len);
590 // Both stun and turn had length at offset 2.
591 int packet_size = base::NetToHost16(*reinterpret_cast<const uint16*>(
592 data + kPacketLengthOffset));
594 // Get packet type (STUN or TURN).
595 uint16 msg_type = base::NetToHost16(*reinterpret_cast<const uint16*>(data));
597 *pad_bytes = 0;
598 // Add heder length to packet length.
599 if ((msg_type & 0xC000) == 0) {
600 packet_size += kStunHeaderSize;
601 } else {
602 packet_size += kTurnChannelDataHeaderSize;
603 // Calculate any padding if present.
604 if (packet_size % 4)
605 *pad_bytes = 4 - packet_size % 4;
607 return packet_size;
610 } // namespace content