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;
30 bool IsTlsClientSocket(content::P2PSocketType type) {
31 return (type == content::P2P_SOCKET_STUN_TLS_CLIENT ||
32 type == content::P2P_SOCKET_TLS_CLIENT);
35 bool IsPseudoTlsClientSocket(content::P2PSocketType type) {
36 return (type == content::P2P_SOCKET_SSLTCP_CLIENT ||
37 type == content::P2P_SOCKET_STUN_SSLTCP_CLIENT);
44 P2PSocketHostTcpBase::P2PSocketHostTcpBase(
45 IPC::Sender* message_sender, int id,
46 P2PSocketType type, net::URLRequestContextGetter* url_context)
47 : P2PSocketHost(message_sender, id),
48 write_pending_(false),
51 url_context_(url_context) {
54 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() {
55 if (state_ == STATE_OPEN) {
56 DCHECK(socket_.get());
61 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint& remote_address,
62 net::StreamSocket* socket) {
64 DCHECK_EQ(state_, STATE_UNINITIALIZED);
66 remote_address_ = remote_address;
67 // TODO(ronghuawu): Add FakeSSLServerSocket.
68 socket_.reset(socket);
71 return state_ != STATE_ERROR;
74 bool P2PSocketHostTcpBase::Init(const net::IPEndPoint& local_address,
75 const net::IPEndPoint& remote_address) {
76 DCHECK_EQ(state_, STATE_UNINITIALIZED);
78 remote_address_ = remote_address;
79 state_ = STATE_CONNECTING;
81 net::HostPortPair dest_host_port_pair =
82 net::HostPortPair::FromIPEndPoint(remote_address);
83 // TODO(mallinath) - We are ignoring local_address altogether. We should
84 // find a way to inject this into ProxyResolvingClientSocket. This could be
85 // a problem on multi-homed host.
87 // The default SSLConfig is good enough for us for now.
88 const net::SSLConfig ssl_config;
89 socket_.reset(new jingle_glue::ProxyResolvingClientSocket(
90 NULL, // Default socket pool provided by the net::Proxy.
93 dest_host_port_pair));
95 int status = socket_->Connect(
96 base::Bind(&P2PSocketHostTcpBase::OnConnected,
97 base::Unretained(this)));
98 if (status != net::ERR_IO_PENDING) {
99 // We defer execution of ProcessConnectDone instead of calling it
100 // directly here as the caller may not expect an error/close to
101 // happen here. This is okay, as from the caller's point of view,
102 // the connect always happens asynchronously.
103 base::MessageLoop* message_loop = base::MessageLoop::current();
105 message_loop->PostTask(
107 base::Bind(&P2PSocketHostTcpBase::OnConnected,
108 base::Unretained(this), status));
111 return state_ != STATE_ERROR;
114 void P2PSocketHostTcpBase::OnError() {
117 if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING ||
118 state_ == STATE_TLS_CONNECTING || state_ == STATE_OPEN) {
119 message_sender_->Send(new P2PMsg_OnError(id_));
122 state_ = STATE_ERROR;
125 void P2PSocketHostTcpBase::OnConnected(int result) {
126 DCHECK_EQ(state_, STATE_CONNECTING);
127 DCHECK_NE(result, net::ERR_IO_PENDING);
129 if (result != net::OK) {
134 if (IsTlsClientSocket(type_)) {
135 state_ = STATE_TLS_CONNECTING;
137 } else if (IsPseudoTlsClientSocket(type_)) {
138 scoped_ptr<net::StreamSocket> transport_socket = socket_.Pass();
140 new jingle_glue::FakeSSLClientSocket(transport_socket.Pass()));
141 state_ = STATE_TLS_CONNECTING;
142 int status = socket_->Connect(
143 base::Bind(&P2PSocketHostTcpBase::ProcessTlsSslConnectDone,
144 base::Unretained(this)));
145 if (status != net::ERR_IO_PENDING) {
146 ProcessTlsSslConnectDone(status);
149 // If we are not doing TLS, we are ready to send data now.
150 // In case of TLS, SignalConnect will be sent only after TLS handshake is
151 // successfull. So no buffering will be done at socket handlers if any
152 // packets sent before that by the application.
157 void P2PSocketHostTcpBase::StartTls() {
158 DCHECK_EQ(state_, STATE_TLS_CONNECTING);
159 DCHECK(socket_.get());
161 scoped_ptr<net::ClientSocketHandle> socket_handle(
162 new net::ClientSocketHandle());
163 socket_handle->SetSocket(socket_.Pass());
165 net::SSLClientSocketContext context;
166 context.cert_verifier = url_context_->GetURLRequestContext()->cert_verifier();
167 context.transport_security_state =
168 url_context_->GetURLRequestContext()->transport_security_state();
169 DCHECK(context.transport_security_state);
171 // Default ssl config.
172 const net::SSLConfig ssl_config;
173 net::HostPortPair dest_host_port_pair =
174 net::HostPortPair::FromIPEndPoint(remote_address_);
175 net::ClientSocketFactory* socket_factory =
176 net::ClientSocketFactory::GetDefaultFactory();
177 DCHECK(socket_factory);
179 socket_ = socket_factory->CreateSSLClientSocket(
180 socket_handle.Pass(), dest_host_port_pair, ssl_config, context);
181 int status = socket_->Connect(
182 base::Bind(&P2PSocketHostTcpBase::ProcessTlsSslConnectDone,
183 base::Unretained(this)));
184 if (status != net::ERR_IO_PENDING) {
185 ProcessTlsSslConnectDone(status);
189 void P2PSocketHostTcpBase::ProcessTlsSslConnectDone(int status) {
190 DCHECK_NE(status, net::ERR_IO_PENDING);
191 DCHECK_EQ(state_, STATE_TLS_CONNECTING);
192 if (status != net::OK) {
199 void P2PSocketHostTcpBase::OnOpen() {
201 DoSendSocketCreateMsg();
205 void P2PSocketHostTcpBase::DoSendSocketCreateMsg() {
206 DCHECK(socket_.get());
208 net::IPEndPoint address;
209 int result = socket_->GetLocalAddress(&address);
211 LOG(ERROR) << "P2PSocketHostTcpBase::OnConnected: unable to get local"
212 << " address: " << result;
217 VLOG(1) << "Local address: " << address.ToString();
219 // If we are not doing TLS, we are ready to send data now.
220 // In case of TLS SignalConnect will be sent only after TLS handshake is
221 // successfull. So no buffering will be done at socket handlers if any
222 // packets sent before that by the application.
223 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, address));
226 void P2PSocketHostTcpBase::DoRead() {
229 if (!read_buffer_.get()) {
230 read_buffer_ = new net::GrowableIOBuffer();
231 read_buffer_->SetCapacity(kReadBufferSize);
232 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) {
233 // Make sure that we always have at least kReadBufferSize of
234 // remaining capacity in the read buffer. Normally all packets
235 // are smaller than kReadBufferSize, so this is not really
237 read_buffer_->SetCapacity(read_buffer_->capacity() + kReadBufferSize -
238 read_buffer_->RemainingCapacity());
240 result = socket_->Read(
242 read_buffer_->RemainingCapacity(),
243 base::Bind(&P2PSocketHostTcp::OnRead, base::Unretained(this)));
244 DidCompleteRead(result);
245 } while (result > 0);
248 void P2PSocketHostTcpBase::OnRead(int result) {
249 DidCompleteRead(result);
250 if (state_ == STATE_OPEN) {
255 void P2PSocketHostTcpBase::OnPacket(const std::vector<char>& data) {
257 P2PSocketHost::StunMessageType type;
258 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
259 if (stun && IsRequestOrResponse(type)) {
261 } else if (!stun || type == STUN_DATA_INDICATION) {
262 LOG(ERROR) << "Received unexpected data packet from "
263 << remote_address_.ToString()
264 << " before STUN binding is finished. "
265 << "Terminating connection.";
271 message_sender_->Send(new P2PMsg_OnDataReceived(id_, remote_address_, data));
274 // Note: dscp is not actually used on TCP sockets as this point,
275 // but may be honored in the future.
276 void P2PSocketHostTcpBase::Send(const net::IPEndPoint& to,
277 const std::vector<char>& data,
278 net::DiffServCodePoint dscp,
281 // The Send message may be sent after the an OnError message was
282 // sent by hasn't been processed the renderer.
286 if (!(to == remote_address_)) {
287 // Renderer should use this socket only to send data to |remote_address_|.
294 P2PSocketHost::StunMessageType type = P2PSocketHost::StunMessageType();
295 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type);
296 if (!stun || type == STUN_DATA_INDICATION) {
297 LOG(ERROR) << "Page tried to send a data packet to " << to.ToString()
298 << " before STUN binding is finished.";
307 void P2PSocketHostTcpBase::WriteOrQueue(
308 scoped_refptr<net::DrainableIOBuffer>& buffer) {
309 if (write_buffer_.get()) {
310 write_queue_.push(buffer);
314 write_buffer_ = buffer;
318 void P2PSocketHostTcpBase::DoWrite() {
319 while (write_buffer_.get() && state_ == STATE_OPEN && !write_pending_) {
320 int result = socket_->Write(
322 write_buffer_->BytesRemaining(),
323 base::Bind(&P2PSocketHostTcp::OnWritten, base::Unretained(this)));
324 HandleWriteResult(result);
328 void P2PSocketHostTcpBase::OnWritten(int result) {
329 DCHECK(write_pending_);
330 DCHECK_NE(result, net::ERR_IO_PENDING);
332 write_pending_ = false;
333 HandleWriteResult(result);
337 void P2PSocketHostTcpBase::HandleWriteResult(int result) {
338 DCHECK(write_buffer_.get());
340 write_buffer_->DidConsume(result);
341 if (write_buffer_->BytesRemaining() == 0) {
342 message_sender_->Send(new P2PMsg_OnSendComplete(id_));
343 if (write_queue_.empty()) {
344 write_buffer_ = NULL;
346 write_buffer_ = write_queue_.front();
350 } else if (result == net::ERR_IO_PENDING) {
351 write_pending_ = true;
353 LOG(ERROR) << "Error when sending data in TCP socket: " << result;
358 P2PSocketHost* P2PSocketHostTcpBase::AcceptIncomingTcpConnection(
359 const net::IPEndPoint& remote_address, int id) {
365 void P2PSocketHostTcpBase::DidCompleteRead(int result) {
366 DCHECK_EQ(state_, STATE_OPEN);
368 if (result == net::ERR_IO_PENDING) {
370 } else if (result < 0) {
371 LOG(ERROR) << "Error when reading from TCP socket: " << result;
376 read_buffer_->set_offset(read_buffer_->offset() + result);
377 char* head = read_buffer_->StartOfBuffer(); // Purely a convenience.
379 while (pos <= read_buffer_->offset() && state_ == STATE_OPEN) {
380 int consumed = ProcessInput(head + pos, read_buffer_->offset() - pos);
385 // We've consumed all complete packets from the buffer; now move any remaining
386 // bytes to the head of the buffer and set offset to reflect this.
387 if (pos && pos <= read_buffer_->offset()) {
388 memmove(head, head + pos, read_buffer_->offset() - pos);
389 read_buffer_->set_offset(read_buffer_->offset() - pos);
393 P2PSocketHostTcp::P2PSocketHostTcp(
394 IPC::Sender* message_sender, int id,
395 P2PSocketType type, net::URLRequestContextGetter* url_context)
396 : P2PSocketHostTcpBase(message_sender, id, type, url_context) {
397 DCHECK(type == P2P_SOCKET_TCP_CLIENT ||
398 type == P2P_SOCKET_SSLTCP_CLIENT ||
399 type == P2P_SOCKET_TLS_CLIENT);
402 P2PSocketHostTcp::~P2PSocketHostTcp() {
405 int P2PSocketHostTcp::ProcessInput(char* input, int input_len) {
406 if (input_len < kPacketHeaderSize)
408 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input));
409 if (input_len < packet_size + kPacketHeaderSize)
412 int consumed = kPacketHeaderSize;
413 char* cur = input + consumed;
414 std::vector<char> data(cur, cur + packet_size);
416 consumed += packet_size;
420 void P2PSocketHostTcp::DoSend(const net::IPEndPoint& to,
421 const std::vector<char>& data) {
422 int size = kPacketHeaderSize + data.size();
423 scoped_refptr<net::DrainableIOBuffer> buffer =
424 new net::DrainableIOBuffer(new net::IOBuffer(size), size);
425 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size());
426 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size());
428 WriteOrQueue(buffer);
431 // P2PSocketHostStunTcp
432 P2PSocketHostStunTcp::P2PSocketHostStunTcp(
433 IPC::Sender* message_sender, int id,
434 P2PSocketType type, net::URLRequestContextGetter* url_context)
435 : P2PSocketHostTcpBase(message_sender, id, type, url_context) {
436 DCHECK(type == P2P_SOCKET_STUN_TCP_CLIENT ||
437 type == P2P_SOCKET_STUN_SSLTCP_CLIENT ||
438 type == P2P_SOCKET_STUN_TLS_CLIENT);
441 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() {
444 int P2PSocketHostStunTcp::ProcessInput(char* input, int input_len) {
445 if (input_len < kPacketHeaderSize + kPacketLengthOffset)
449 int packet_size = GetExpectedPacketSize(
450 input, input_len, &pad_bytes);
452 if (input_len < packet_size + pad_bytes)
455 // We have a complete packet. Read through it.
458 std::vector<char> data(cur, cur + packet_size);
460 consumed += packet_size;
461 consumed += pad_bytes;
465 void P2PSocketHostStunTcp::DoSend(const net::IPEndPoint& to,
466 const std::vector<char>& data) {
467 // Each packet is expected to have header (STUN/TURN ChannelData), where
468 // header contains message type and and length of message.
469 if (data.size() < kPacketHeaderSize + kPacketLengthOffset) {
476 size_t expected_len = GetExpectedPacketSize(
477 &data[0], data.size(), &pad_bytes);
479 // Accepts only complete STUN/TURN packets.
480 if (data.size() != expected_len) {
486 // Add any pad bytes to the total size.
487 int size = data.size() + pad_bytes;
489 scoped_refptr<net::DrainableIOBuffer> buffer =
490 new net::DrainableIOBuffer(new net::IOBuffer(size), size);
491 memcpy(buffer->data(), &data[0], data.size());
494 char padding[4] = {0};
495 DCHECK_LE(pad_bytes, 4);
496 memcpy(buffer->data() + data.size(), padding, pad_bytes);
498 WriteOrQueue(buffer);
501 int P2PSocketHostStunTcp::GetExpectedPacketSize(
502 const char* data, int len, int* pad_bytes) {
503 DCHECK_LE(kTurnChannelDataHeaderSize, len);
504 // Both stun and turn had length at offset 2.
505 int packet_size = base::NetToHost16(*reinterpret_cast<const uint16*>(
506 data + kPacketLengthOffset));
508 // Get packet type (STUN or TURN).
509 uint16 msg_type = base::NetToHost16(*reinterpret_cast<const uint16*>(data));
512 // Add heder length to packet length.
513 if ((msg_type & 0xC000) == 0) {
514 packet_size += kStunHeaderSize;
516 packet_size += kTurnChannelDataHeaderSize;
517 // Calculate any padding if present.
519 *pad_bytes = 4 - packet_size % 4;
524 } // namespace content