Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: content/browser/renderer_host/p2p/socket_host_tcp.cc

Issue 22452002: Adding TLS support to the TCP Client sockets. (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/renderer_host/p2p/socket_host_tcp.h ('k') | content/common/p2p_sockets.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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_)) {
juberti2 2013/08/09 23:49:26 I think it would be clearest to write this as belo
Mallinath (Gone from Chromium) 2013/08/10 00:07:27 Done.
135 state_ = STATE_TLS_CONNECTING;
136 return StartTls();
137 } else if (IsPseudoTlsClientSocket(type_)) {
138 socket_.reset(new jingle_glue::FakeSSLClientSocket(socket_.release()));
139 }
140
141 // If we are not doing TLS, we are ready to send data now.
142 // In case of TLS, SignalConnect will be sent only after TLS handshake is
143 // successfull. So no buffering will be done at socket handlers if any
144 // packets sent before that by the application.
145 state_ = STATE_OPEN;
146
147 DoSendSocketCreateMsg();
148 DoRead();
149 }
150
151 void P2PSocketHostTcpBase::StartTls() {
152 if (state_ != STATE_TLS_CONNECTING) {
153 LOG(DFATAL) << "StartTls() called in wrong state";
154 return;
155 }
156
157 DCHECK(socket_.get());
158
159 scoped_ptr<net::ClientSocketHandle> socket_handle(
160 new net::ClientSocketHandle());
161 socket_handle->set_socket(socket_.release());
162
163 net::SSLClientSocketContext context;
164 context.cert_verifier = url_context_->GetURLRequestContext()->cert_verifier();
165 context.transport_security_state =
166 url_context_->GetURLRequestContext()->transport_security_state();
167 DCHECK(context.transport_security_state);
168
169 // Default ssl config.
170 const net::SSLConfig ssl_config;
171 net::HostPortPair dest_host_port_pair =
172 net::HostPortPair::FromIPEndPoint(remote_address_);
173 net::ClientSocketFactory* socket_factory =
174 net::ClientSocketFactory::GetDefaultFactory();
175 DCHECK(socket_factory);
176
177 socket_.reset(socket_factory->CreateSSLClientSocket(
178 socket_handle.release(), dest_host_port_pair, ssl_config, context));
179 int status = socket_->Connect(
180 base::Bind(&P2PSocketHostTcpBase::ProcessTlsConnectDone,
181 base::Unretained(this)));
182 if (status != net::ERR_IO_PENDING) {
183 base::MessageLoop* message_loop = base::MessageLoop::current();
184 CHECK(message_loop);
185 message_loop->PostTask(
186 FROM_HERE,
187 base::Bind(&P2PSocketHostTcpBase::ProcessTlsConnectDone,
188 base::Unretained(this), status));
189 }
190 return;
juberti2 2013/08/09 23:49:26 return; not needed
Mallinath (Gone from Chromium) 2013/08/10 00:07:27 Done.
191 }
192
193 void P2PSocketHostTcpBase::ProcessTlsConnectDone(int status) {
194 DCHECK_NE(status, net::ERR_IO_PENDING);
195 DCHECK_EQ(state_, STATE_TLS_CONNECTING);
196 if (status != net::OK) {
197 OnError();
198 return;
199 }
200
201 state_ = STATE_OPEN;
202
203 DoSendSocketCreateMsg();
204 DoRead();
205 }
206
207 void P2PSocketHostTcpBase::DoSendSocketCreateMsg() {
208 DCHECK(socket_.get());
209
128 net::IPEndPoint address; 210 net::IPEndPoint address;
129 result = socket_->GetLocalAddress(&address); 211 int result = socket_->GetLocalAddress(&address);
130 if (result < 0) { 212 if (result < 0) {
131 LOG(ERROR) << "P2PSocket::Init(): unable to get local address: " 213 LOG(ERROR) << "P2PSocketHostTcpBase::OnConnected: unable to get local"
132 << result; 214 << " address: " << result;
133 OnError(); 215 OnError();
134 return; 216 return;
135 } 217 }
136 218
137 VLOG(1) << "Local address: " << address.ToString(); 219 VLOG(1) << "Local address: " << address.ToString();
138 state_ = STATE_OPEN; 220
221 // If we are not doing TLS, we are ready to send data now.
222 // In case of TLS SignalConnect will be sent only after TLS handshake is
223 // successfull. So no buffering will be done at socket handlers if any
224 // packets sent before that by the application.
139 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, address)); 225 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, address));
140 DoRead();
141 } 226 }
142 227
143 void P2PSocketHostTcpBase::DoRead() { 228 void P2PSocketHostTcpBase::DoRead() {
144 int result; 229 int result;
145 do { 230 do {
146 if (!read_buffer_.get()) { 231 if (!read_buffer_.get()) {
147 read_buffer_ = new net::GrowableIOBuffer(); 232 read_buffer_ = new net::GrowableIOBuffer();
148 read_buffer_->SetCapacity(kReadBufferSize); 233 read_buffer_->SetCapacity(kReadBufferSize);
149 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) { 234 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) {
150 // Make sure that we always have at least kReadBufferSize of 235 // Make sure that we always have at least kReadBufferSize of
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 } else { 510 } else {
426 packet_size += kTurnChannelDataHeaderSize; 511 packet_size += kTurnChannelDataHeaderSize;
427 // Calculate any padding if present. 512 // Calculate any padding if present.
428 if (packet_size % 4) 513 if (packet_size % 4)
429 *pad_bytes = 4 - packet_size % 4; 514 *pad_bytes = 4 - packet_size % 4;
430 } 515 }
431 return packet_size; 516 return packet_size;
432 } 517 }
433 518
434 } // namespace content 519 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/renderer_host/p2p/socket_host_tcp.h ('k') | content/common/p2p_sockets.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698