| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
| 6 | 6 |
| 7 #include "base/sys_byteorder.h" | 7 #include "base/sys_byteorder.h" |
| 8 #include "content/common/p2p_messages.h" | 8 #include "content/common/p2p_messages.h" |
| 9 #include "ipc/ipc_sender.h" | 9 #include "ipc/ipc_sender.h" |
| 10 #include "jingle/glue/fake_ssl_client_socket.h" | 10 #include "jingle/glue/fake_ssl_client_socket.h" |
| 11 #include "jingle/glue/proxy_resolving_client_socket.h" | 11 #include "jingle/glue/proxy_resolving_client_socket.h" |
| 12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/base/net_util.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" |
| 15 #include "net/socket/tcp_client_socket.h" | 18 #include "net/socket/tcp_client_socket.h" |
| 19 #include "net/url_request/url_request_context.h" |
| 16 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 17 | 21 |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 typedef uint16 PacketLength; | 24 typedef uint16 PacketLength; |
| 21 const int kPacketHeaderSize = sizeof(PacketLength); | 25 const int kPacketHeaderSize = sizeof(PacketLength); |
| 22 const int kReadBufferSize = 4096; | 26 const int kReadBufferSize = 4096; |
| 23 const int kPacketLengthOffset = 2; | 27 const int kPacketLengthOffset = 2; |
| 24 const int kTurnChannelDataHeaderSize = 4; | 28 const int kTurnChannelDataHeaderSize = 4; |
| 25 | 29 |
| 26 bool IsSslClientSocket(content::P2PSocketType type) { | 30 bool IsTlsClientSocket(content::P2PSocketType type) { |
| 31 return (type == content::P2P_SOCKET_STUN_TLS_CLIENT || |
| 32 type == content::P2P_SOCKET_TLS_CLIENT); |
| 33 } |
| 34 |
| 35 bool IsPseudoTlsClientSocket(content::P2PSocketType type) { |
| 27 return (type == content::P2P_SOCKET_SSLTCP_CLIENT || | 36 return (type == content::P2P_SOCKET_SSLTCP_CLIENT || |
| 28 type == content::P2P_SOCKET_STUN_SSLTCP_CLIENT); | 37 type == content::P2P_SOCKET_STUN_SSLTCP_CLIENT); |
| 29 } | 38 } |
| 30 | 39 |
| 31 } // namespace | 40 } // namespace |
| 32 | 41 |
| 33 namespace content { | 42 namespace content { |
| 34 | 43 |
| 35 P2PSocketHostTcpBase::P2PSocketHostTcpBase( | 44 P2PSocketHostTcpBase::P2PSocketHostTcpBase( |
| 36 IPC::Sender* message_sender, int id, | 45 IPC::Sender* message_sender, int id, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 // find a way to inject this into ProxyResolvingClientSocket. This could be | 84 // find a way to inject this into ProxyResolvingClientSocket. This could be |
| 76 // a problem on multi-homed host. | 85 // a problem on multi-homed host. |
| 77 | 86 |
| 78 // The default SSLConfig is good enough for us for now. | 87 // The default SSLConfig is good enough for us for now. |
| 79 const net::SSLConfig ssl_config; | 88 const net::SSLConfig ssl_config; |
| 80 socket_.reset(new jingle_glue::ProxyResolvingClientSocket( | 89 socket_.reset(new jingle_glue::ProxyResolvingClientSocket( |
| 81 NULL, // Default socket pool provided by the net::Proxy. | 90 NULL, // Default socket pool provided by the net::Proxy. |
| 82 url_context_, | 91 url_context_, |
| 83 ssl_config, | 92 ssl_config, |
| 84 dest_host_port_pair)); | 93 dest_host_port_pair)); |
| 85 if (IsSslClientSocket(type_)) { | |
| 86 socket_.reset(new jingle_glue::FakeSSLClientSocket(socket_.release())); | |
| 87 } | |
| 88 | 94 |
| 89 int status = socket_->Connect( | 95 int status = socket_->Connect( |
| 90 base::Bind(&P2PSocketHostTcpBase::OnConnected, | 96 base::Bind(&P2PSocketHostTcpBase::OnConnected, |
| 91 base::Unretained(this))); | 97 base::Unretained(this))); |
| 92 if (status != net::ERR_IO_PENDING) { | 98 if (status != net::ERR_IO_PENDING) { |
| 93 // We defer execution of ProcessConnectDone instead of calling it | 99 // We defer execution of ProcessConnectDone instead of calling it |
| 94 // directly here as the caller may not expect an error/close to | 100 // directly here as the caller may not expect an error/close to |
| 95 // happen here. This is okay, as from the caller's point of view, | 101 // happen here. This is okay, as from the caller's point of view, |
| 96 // the connect always happens asynchronously. | 102 // the connect always happens asynchronously. |
| 97 base::MessageLoop* message_loop = base::MessageLoop::current(); | 103 base::MessageLoop* message_loop = base::MessageLoop::current(); |
| 98 CHECK(message_loop); | 104 CHECK(message_loop); |
| 99 message_loop->PostTask( | 105 message_loop->PostTask( |
| 100 FROM_HERE, | 106 FROM_HERE, |
| 101 base::Bind(&P2PSocketHostTcpBase::OnConnected, | 107 base::Bind(&P2PSocketHostTcpBase::OnConnected, |
| 102 base::Unretained(this), status)); | 108 base::Unretained(this), status)); |
| 103 } | 109 } |
| 104 | 110 |
| 105 return state_ != STATE_ERROR; | 111 return state_ != STATE_ERROR; |
| 106 } | 112 } |
| 107 | 113 |
| 108 void P2PSocketHostTcpBase::OnError() { | 114 void P2PSocketHostTcpBase::OnError() { |
| 109 socket_.reset(); | 115 socket_.reset(); |
| 110 | 116 |
| 111 if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING || | 117 if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING || |
| 112 state_ == STATE_OPEN) { | 118 state_ == STATE_TLS_CONNECTING || state_ == STATE_OPEN) { |
| 113 message_sender_->Send(new P2PMsg_OnError(id_)); | 119 message_sender_->Send(new P2PMsg_OnError(id_)); |
| 114 } | 120 } |
| 115 | 121 |
| 116 state_ = STATE_ERROR; | 122 state_ = STATE_ERROR; |
| 117 } | 123 } |
| 118 | 124 |
| 119 void P2PSocketHostTcpBase::OnConnected(int result) { | 125 void P2PSocketHostTcpBase::OnConnected(int result) { |
| 120 DCHECK_EQ(state_, STATE_CONNECTING); | 126 DCHECK_EQ(state_, STATE_CONNECTING); |
| 121 DCHECK_NE(result, net::ERR_IO_PENDING); | 127 DCHECK_NE(result, net::ERR_IO_PENDING); |
| 122 | 128 |
| 123 if (result != net::OK) { | 129 if (result != net::OK) { |
| 124 OnError(); | 130 OnError(); |
| 125 return; | 131 return; |
| 126 } | 132 } |
| 127 | 133 |
| 134 if (IsTlsClientSocket(type_)) { |
| 135 state_ = STATE_TLS_CONNECTING; |
| 136 StartTls(); |
| 137 } else { |
| 138 if (IsPseudoTlsClientSocket(type_)) { |
| 139 socket_.reset(new jingle_glue::FakeSSLClientSocket(socket_.release())); |
| 140 } |
| 141 |
| 142 // If we are not doing TLS, we are ready to send data now. |
| 143 // In case of TLS, SignalConnect will be sent only after TLS handshake is |
| 144 // successfull. So no buffering will be done at socket handlers if any |
| 145 // packets sent before that by the application. |
| 146 state_ = STATE_OPEN; |
| 147 DoSendSocketCreateMsg(); |
| 148 DoRead(); |
| 149 } |
| 150 } |
| 151 |
| 152 void P2PSocketHostTcpBase::StartTls() { |
| 153 DCHECK_EQ(state_, STATE_TLS_CONNECTING); |
| 154 DCHECK(socket_.get()); |
| 155 |
| 156 scoped_ptr<net::ClientSocketHandle> socket_handle( |
| 157 new net::ClientSocketHandle()); |
| 158 socket_handle->set_socket(socket_.release()); |
| 159 |
| 160 net::SSLClientSocketContext context; |
| 161 context.cert_verifier = url_context_->GetURLRequestContext()->cert_verifier(); |
| 162 context.transport_security_state = |
| 163 url_context_->GetURLRequestContext()->transport_security_state(); |
| 164 DCHECK(context.transport_security_state); |
| 165 |
| 166 // Default ssl config. |
| 167 const net::SSLConfig ssl_config; |
| 168 net::HostPortPair dest_host_port_pair = |
| 169 net::HostPortPair::FromIPEndPoint(remote_address_); |
| 170 net::ClientSocketFactory* socket_factory = |
| 171 net::ClientSocketFactory::GetDefaultFactory(); |
| 172 DCHECK(socket_factory); |
| 173 |
| 174 socket_.reset(socket_factory->CreateSSLClientSocket( |
| 175 socket_handle.release(), dest_host_port_pair, ssl_config, context)); |
| 176 int status = socket_->Connect( |
| 177 base::Bind(&P2PSocketHostTcpBase::ProcessTlsConnectDone, |
| 178 base::Unretained(this))); |
| 179 if (status != net::ERR_IO_PENDING) { |
| 180 ProcessTlsConnectDone(status); |
| 181 } |
| 182 } |
| 183 |
| 184 void P2PSocketHostTcpBase::ProcessTlsConnectDone(int status) { |
| 185 DCHECK_NE(status, net::ERR_IO_PENDING); |
| 186 DCHECK_EQ(state_, STATE_TLS_CONNECTING); |
| 187 if (status != net::OK) { |
| 188 OnError(); |
| 189 return; |
| 190 } |
| 191 |
| 192 state_ = STATE_OPEN; |
| 193 DoSendSocketCreateMsg(); |
| 194 DoRead(); |
| 195 } |
| 196 |
| 197 void P2PSocketHostTcpBase::DoSendSocketCreateMsg() { |
| 198 DCHECK(socket_.get()); |
| 199 |
| 128 net::IPEndPoint address; | 200 net::IPEndPoint address; |
| 129 result = socket_->GetLocalAddress(&address); | 201 int result = socket_->GetLocalAddress(&address); |
| 130 if (result < 0) { | 202 if (result < 0) { |
| 131 LOG(ERROR) << "P2PSocket::Init(): unable to get local address: " | 203 LOG(ERROR) << "P2PSocketHostTcpBase::OnConnected: unable to get local" |
| 132 << result; | 204 << " address: " << result; |
| 133 OnError(); | 205 OnError(); |
| 134 return; | 206 return; |
| 135 } | 207 } |
| 136 | 208 |
| 137 VLOG(1) << "Local address: " << address.ToString(); | 209 VLOG(1) << "Local address: " << address.ToString(); |
| 138 state_ = STATE_OPEN; | 210 |
| 211 // If we are not doing TLS, we are ready to send data now. |
| 212 // In case of TLS SignalConnect will be sent only after TLS handshake is |
| 213 // successfull. So no buffering will be done at socket handlers if any |
| 214 // packets sent before that by the application. |
| 139 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, address)); | 215 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, address)); |
| 140 DoRead(); | |
| 141 } | 216 } |
| 142 | 217 |
| 143 void P2PSocketHostTcpBase::DoRead() { | 218 void P2PSocketHostTcpBase::DoRead() { |
| 144 int result; | 219 int result; |
| 145 do { | 220 do { |
| 146 if (!read_buffer_.get()) { | 221 if (!read_buffer_.get()) { |
| 147 read_buffer_ = new net::GrowableIOBuffer(); | 222 read_buffer_ = new net::GrowableIOBuffer(); |
| 148 read_buffer_->SetCapacity(kReadBufferSize); | 223 read_buffer_->SetCapacity(kReadBufferSize); |
| 149 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) { | 224 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) { |
| 150 // Make sure that we always have at least kReadBufferSize of | 225 // Make sure that we always have at least kReadBufferSize of |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 if (pos && pos <= read_buffer_->offset()) { | 375 if (pos && pos <= read_buffer_->offset()) { |
| 301 memmove(head, head + pos, read_buffer_->offset() - pos); | 376 memmove(head, head + pos, read_buffer_->offset() - pos); |
| 302 read_buffer_->set_offset(read_buffer_->offset() - pos); | 377 read_buffer_->set_offset(read_buffer_->offset() - pos); |
| 303 } | 378 } |
| 304 } | 379 } |
| 305 | 380 |
| 306 P2PSocketHostTcp::P2PSocketHostTcp( | 381 P2PSocketHostTcp::P2PSocketHostTcp( |
| 307 IPC::Sender* message_sender, int id, | 382 IPC::Sender* message_sender, int id, |
| 308 P2PSocketType type, net::URLRequestContextGetter* url_context) | 383 P2PSocketType type, net::URLRequestContextGetter* url_context) |
| 309 : P2PSocketHostTcpBase(message_sender, id, type, url_context) { | 384 : P2PSocketHostTcpBase(message_sender, id, type, url_context) { |
| 310 DCHECK(type == P2P_SOCKET_TCP_CLIENT || type == P2P_SOCKET_SSLTCP_CLIENT); | 385 DCHECK(type == P2P_SOCKET_TCP_CLIENT || |
| 386 type == P2P_SOCKET_SSLTCP_CLIENT || |
| 387 type == P2P_SOCKET_TLS_CLIENT); |
| 311 } | 388 } |
| 312 | 389 |
| 313 P2PSocketHostTcp::~P2PSocketHostTcp() { | 390 P2PSocketHostTcp::~P2PSocketHostTcp() { |
| 314 } | 391 } |
| 315 | 392 |
| 316 int P2PSocketHostTcp::ProcessInput(char* input, int input_len) { | 393 int P2PSocketHostTcp::ProcessInput(char* input, int input_len) { |
| 317 if (input_len < kPacketHeaderSize) | 394 if (input_len < kPacketHeaderSize) |
| 318 return 0; | 395 return 0; |
| 319 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input)); | 396 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input)); |
| 320 if (input_len < packet_size + kPacketHeaderSize) | 397 if (input_len < packet_size + kPacketHeaderSize) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 338 | 415 |
| 339 WriteOrQueue(buffer); | 416 WriteOrQueue(buffer); |
| 340 } | 417 } |
| 341 | 418 |
| 342 // P2PSocketHostStunTcp | 419 // P2PSocketHostStunTcp |
| 343 P2PSocketHostStunTcp::P2PSocketHostStunTcp( | 420 P2PSocketHostStunTcp::P2PSocketHostStunTcp( |
| 344 IPC::Sender* message_sender, int id, | 421 IPC::Sender* message_sender, int id, |
| 345 P2PSocketType type, net::URLRequestContextGetter* url_context) | 422 P2PSocketType type, net::URLRequestContextGetter* url_context) |
| 346 : P2PSocketHostTcpBase(message_sender, id, type, url_context) { | 423 : P2PSocketHostTcpBase(message_sender, id, type, url_context) { |
| 347 DCHECK(type == P2P_SOCKET_STUN_TCP_CLIENT || | 424 DCHECK(type == P2P_SOCKET_STUN_TCP_CLIENT || |
| 348 type == P2P_SOCKET_STUN_SSLTCP_CLIENT); | 425 type == P2P_SOCKET_STUN_SSLTCP_CLIENT || |
| 426 type == P2P_SOCKET_STUN_TLS_CLIENT); |
| 349 } | 427 } |
| 350 | 428 |
| 351 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() { | 429 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() { |
| 352 } | 430 } |
| 353 | 431 |
| 354 int P2PSocketHostStunTcp::ProcessInput(char* input, int input_len) { | 432 int P2PSocketHostStunTcp::ProcessInput(char* input, int input_len) { |
| 355 if (input_len < kPacketHeaderSize + kPacketLengthOffset) | 433 if (input_len < kPacketHeaderSize + kPacketLengthOffset) |
| 356 return 0; | 434 return 0; |
| 357 | 435 |
| 358 int pad_bytes; | 436 int pad_bytes; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 } else { | 503 } else { |
| 426 packet_size += kTurnChannelDataHeaderSize; | 504 packet_size += kTurnChannelDataHeaderSize; |
| 427 // Calculate any padding if present. | 505 // Calculate any padding if present. |
| 428 if (packet_size % 4) | 506 if (packet_size % 4) |
| 429 *pad_bytes = 4 - packet_size % 4; | 507 *pad_bytes = 4 - packet_size % 4; |
| 430 } | 508 } |
| 431 return packet_size; | 509 return packet_size; |
| 432 } | 510 } |
| 433 | 511 |
| 434 } // namespace content | 512 } // namespace content |
| OLD | NEW |