diff options
author | zork@chromium.org <zork@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-29 21:06:54 +0000 |
---|---|---|
committer | zork@chromium.org <zork@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-29 21:06:54 +0000 |
commit | 8ea92c2b636f716eb3f5904eef51f6bd366a599c (patch) | |
tree | a5febf23b4c15006dac408888f5e599ec60f01c8 | |
parent | bac799a4c7c9f5cb6e72ae574b2bf795fba5a0b1 (diff) | |
download | chromium_src-8ea92c2b636f716eb3f5904eef51f6bd366a599c.zip chromium_src-8ea92c2b636f716eb3f5904eef51f6bd366a599c.tar.gz chromium_src-8ea92c2b636f716eb3f5904eef51f6bd366a599c.tar.bz2 |
Fix SSL Socket adapter to handle the case where StartSSL is called before the socket is connected.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/515018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35346 0039d316-1c4b-4281-b951-d872f2087c98
-rwxr-xr-x | chrome/browser/sync/notifier/communicator/ssl_socket_adapter.cc | 35 | ||||
-rwxr-xr-x | chrome/browser/sync/notifier/communicator/ssl_socket_adapter.h | 9 |
2 files changed, 37 insertions, 7 deletions
diff --git a/chrome/browser/sync/notifier/communicator/ssl_socket_adapter.cc b/chrome/browser/sync/notifier/communicator/ssl_socket_adapter.cc index afdb099..42f6eb8 100755 --- a/chrome/browser/sync/notifier/communicator/ssl_socket_adapter.cc +++ b/chrome/browser/sync/notifier/communicator/ssl_socket_adapter.cc @@ -64,20 +64,30 @@ SSLSocketAdapter::SSLSocketAdapter(AsyncSocket* socket) io_callback_(this, &SSLSocketAdapter::OnIO)), ssl_connected_(false), state_(STATE_NONE) { - socket_ = new TransportSocket(socket, this); + transport_socket_ = new TransportSocket(socket, this); } int SSLSocketAdapter::StartSSL(const char* hostname, bool restartable) { DCHECK(!restartable); + hostname_ = hostname; + if (socket_->GetState() != Socket::CS_CONNECTED) { + state_ = STATE_SSL_WAIT; + return 0; + } else { + return BeginSSL(); + } +} + +int SSLSocketAdapter::BeginSSL() { // SSLConfigService is not thread-safe, and the default values for SSLConfig // are correct for us, so we don't use the config service to initialize this // object. net::SSLConfig ssl_config; - socket_->set_addr(talk_base::SocketAddress(hostname)); + transport_socket_->set_addr(talk_base::SocketAddress(hostname_.c_str())); ssl_socket_.reset( net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( - socket_, hostname, ssl_config)); + transport_socket_, hostname_.c_str(), ssl_config)); int result = ssl_socket_->Connect(&connected_callback_, NULL); @@ -138,6 +148,7 @@ int SSLSocketAdapter::Recv(void* buf, size_t len) { case STATE_READ: case STATE_WRITE: case STATE_WRITE_COMPLETE: + case STATE_SSL_WAIT: SetError(EWOULDBLOCK); return -1; @@ -170,6 +181,7 @@ void SSLSocketAdapter::OnIO(int result) { case STATE_NONE: case STATE_READ_COMPLETE: case STATE_WRITE_COMPLETE: + case STATE_SSL_WAIT: default: NOTREACHED(); break; @@ -177,15 +189,28 @@ void SSLSocketAdapter::OnIO(int result) { } void SSLSocketAdapter::OnReadEvent(talk_base::AsyncSocket* socket) { - if (!socket_->OnReadEvent(socket)) + if (!transport_socket_->OnReadEvent(socket)) AsyncSocketAdapter::OnReadEvent(socket); } void SSLSocketAdapter::OnWriteEvent(talk_base::AsyncSocket* socket) { - if (!socket_->OnWriteEvent(socket)) + if (!transport_socket_->OnWriteEvent(socket)) AsyncSocketAdapter::OnWriteEvent(socket); } +void SSLSocketAdapter::OnConnectEvent(talk_base::AsyncSocket* socket) { + if (state_ != STATE_SSL_WAIT) { + AsyncSocketAdapter::OnConnectEvent(socket); + } else { + state_ = STATE_NONE; + int result = BeginSSL(); + if (0 != result) { + // TODO(zork): Handle this case gracefully. + LOG(WARNING) << "BeginSSL() failed with " << result; + } + } +} + TransportSocket::TransportSocket(talk_base::AsyncSocket* socket, SSLSocketAdapter *ssl_adapter) : connect_callback_(NULL), diff --git a/chrome/browser/sync/notifier/communicator/ssl_socket_adapter.h b/chrome/browser/sync/notifier/communicator/ssl_socket_adapter.h index c58652e..550132f 100755 --- a/chrome/browser/sync/notifier/communicator/ssl_socket_adapter.h +++ b/chrome/browser/sync/notifier/communicator/ssl_socket_adapter.h @@ -105,7 +105,8 @@ class SSLSocketAdapter : public talk_base::AsyncSocketAdapter { STATE_READ, STATE_READ_COMPLETE, STATE_WRITE, - STATE_WRITE_COMPLETE + STATE_WRITE_COMPLETE, + STATE_SSL_WAIT }; void OnConnected(int result); @@ -113,9 +114,13 @@ class SSLSocketAdapter : public talk_base::AsyncSocketAdapter { void OnReadEvent(talk_base::AsyncSocket * socket); void OnWriteEvent(talk_base::AsyncSocket * socket); + void OnConnectEvent(talk_base::AsyncSocket * socket); + + int BeginSSL(); bool ignore_bad_cert_; - TransportSocket* socket_; + std::string hostname_; + TransportSocket* transport_socket_; scoped_ptr<net::SSLClientSocket> ssl_socket_; net::CompletionCallbackImpl<SSLSocketAdapter> connected_callback_; net::CompletionCallbackImpl<SSLSocketAdapter> io_callback_; |