From: Christopher Dykes Date: Fri, 23 Dec 2016 21:09:25 +0000 (-0800) Subject: Use std::chrono for timeouts to sslAccept and sslConn in AsyncSSLSocket X-Git-Tag: v2017.03.06.00~164 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=3caa34081624e419a9c230de190df6d2b7c63bd6;p=folly.git Use std::chrono for timeouts to sslAccept and sslConn in AsyncSSLSocket Summary: Because `std::chrono` makes it clear exactly what unit of time is in use. Reviewed By: yfeldblum Differential Revision: D4363560 fbshipit-source-id: 47aeef21f842f39d8e886bec441897ecf1f3761b --- diff --git a/folly/io/async/AsyncSSLSocket.cpp b/folly/io/async/AsyncSSLSocket.cpp index fefd9d01..7b22c3d2 100644 --- a/folly/io/async/AsyncSSLSocket.cpp +++ b/folly/io/async/AsyncSSLSocket.cpp @@ -110,7 +110,7 @@ class AsyncSSLSocketConnector: public AsyncSocket::ConnectCallback, return; } } - sslSocket_->sslConn(this, timeoutLeft); + sslSocket_->sslConn(this, std::chrono::milliseconds(timeoutLeft)); } void connectErr(const AsyncSocketException& ex) noexcept override { @@ -417,8 +417,10 @@ void AsyncSSLSocket::invalidState(HandshakeCB* callback) { } } -void AsyncSSLSocket::sslAccept(HandshakeCB* callback, uint32_t timeout, - const SSLContext::SSLVerifyPeerEnum& verifyPeer) { +void AsyncSSLSocket::sslAccept( + HandshakeCB* callback, + std::chrono::milliseconds timeout, + const SSLContext::SSLVerifyPeerEnum& verifyPeer) { DestructorGuard dg(this); assert(eventBase_->isInEventBaseThread()); verifyPeer_ = verifyPeer; @@ -443,7 +445,7 @@ void AsyncSSLSocket::sslAccept(HandshakeCB* callback, uint32_t timeout, sslState_ = STATE_ACCEPTING; handshakeCallback_ = callback; - if (timeout > 0) { + if (timeout > std::chrono::milliseconds::zero()) { handshakeTimeout_.scheduleTimeout(timeout); } @@ -680,8 +682,10 @@ bool AsyncSSLSocket::setupSSLBio() { return true; } -void AsyncSSLSocket::sslConn(HandshakeCB* callback, uint64_t timeout, - const SSLContext::SSLVerifyPeerEnum& verifyPeer) { +void AsyncSSLSocket::sslConn( + HandshakeCB* callback, + std::chrono::milliseconds timeout, + const SSLContext::SSLVerifyPeerEnum& verifyPeer) { DestructorGuard dg(this); assert(eventBase_->isInEventBaseThread()); @@ -747,9 +751,8 @@ void AsyncSSLSocket::startSSLConnect() { handshakeStartTime_ = std::chrono::steady_clock::now(); // Make end time at least >= start time. handshakeEndTime_ = handshakeStartTime_; - if (handshakeConnectTimeout_ > 0) { - handshakeTimeout_.scheduleTimeout( - std::chrono::milliseconds(handshakeConnectTimeout_)); + if (handshakeConnectTimeout_ > std::chrono::milliseconds::zero()) { + handshakeTimeout_.scheduleTimeout(handshakeConnectTimeout_); } handleConnect(); } diff --git a/folly/io/async/AsyncSSLSocket.h b/folly/io/async/AsyncSSLSocket.h index 5140a824..17bdd9c9 100644 --- a/folly/io/async/AsyncSSLSocket.h +++ b/folly/io/async/AsyncSSLSocket.h @@ -292,9 +292,11 @@ class AsyncSSLSocket : public virtual AsyncSocket { * context by default, can be set explcitly to override the * method in the context */ - virtual void sslAccept(HandshakeCB* callback, uint32_t timeout = 0, + virtual void sslAccept( + HandshakeCB* callback, + std::chrono::milliseconds timeout = std::chrono::milliseconds::zero(), const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer = - folly::SSLContext::SSLVerifyPeerEnum::USE_CTX); + folly::SSLContext::SSLVerifyPeerEnum::USE_CTX); /** * Invoke SSL accept following an asynchronous session cache lookup @@ -332,9 +334,11 @@ class AsyncSSLSocket : public virtual AsyncSocket { * SSL_VERIFY_PEER and invokes * HandshakeCB::handshakeVer(). */ - virtual void sslConn(HandshakeCB *callback, uint64_t timeout = 0, - const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer = - folly::SSLContext::SSLVerifyPeerEnum::USE_CTX); + virtual void sslConn( + HandshakeCB* callback, + std::chrono::milliseconds timeout = std::chrono::milliseconds::zero(), + const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer = + folly::SSLContext::SSLVerifyPeerEnum::USE_CTX); enum SSLStateEnum { STATE_UNINIT, @@ -810,7 +814,7 @@ class AsyncSSLSocket : public virtual AsyncSocket { // Time taken to complete the ssl handshake. std::chrono::steady_clock::time_point handshakeStartTime_; std::chrono::steady_clock::time_point handshakeEndTime_; - uint64_t handshakeConnectTimeout_{0}; + std::chrono::milliseconds handshakeConnectTimeout_{0}; bool sessionResumptionAttempted_{false}; }; diff --git a/folly/io/async/test/AsyncSSLSocketTest.h b/folly/io/async/test/AsyncSSLSocketTest.h index d66f5088..ecf3aa51 100644 --- a/folly/io/async/test/AsyncSSLSocketTest.h +++ b/folly/io/async/test/AsyncSSLSocketTest.h @@ -451,7 +451,7 @@ public: std::cerr << "SSLServerAcceptCallback::connAccepted" << std::endl; hcb_->setSocket(sock); - sock->sslAccept(hcb_, timeout_); + sock->sslAccept(hcb_, std::chrono::milliseconds(timeout_)); EXPECT_EQ(sock->getSSLState(), AsyncSSLSocket::STATE_ACCEPTING); @@ -515,7 +515,7 @@ public: std::cerr << "SSLServerAcceptCallback::connAccepted" << std::endl; hcb_->setSocket(sock); - sock->sslAccept(hcb_, timeout_); + sock->sslAccept(hcb_, std::chrono::milliseconds(timeout_)); ASSERT_TRUE((sock->getSSLState() == AsyncSSLSocket::STATE_ACCEPTING) || (sock->getSSLState() == @@ -748,7 +748,7 @@ class BlockingWriteClient : } } - socket_->sslConn(this, 100); + socket_->sslConn(this, std::chrono::milliseconds(100)); } struct iovec* getIovec() const { @@ -794,7 +794,7 @@ class BlockingWriteServer : bufSize_(2500 * 2000), bytesRead_(0) { buf_.reset(new uint8_t[bufSize_]); - socket_->sslAccept(this, 100); + socket_->sslAccept(this, std::chrono::milliseconds(100)); } void checkBuffer(struct iovec* iov, uint32_t count) const { @@ -1293,7 +1293,7 @@ class SSLHandshakeClient : public SSLHandshakeBase { bool preverifyResult, bool verifyResult) : SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) { - socket_->sslConn(this, 0); + socket_->sslConn(this, std::chrono::milliseconds::zero()); } }; @@ -1304,8 +1304,10 @@ class SSLHandshakeClientNoVerify : public SSLHandshakeBase { bool preverifyResult, bool verifyResult) : SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) { - socket_->sslConn(this, 0, - folly::SSLContext::SSLVerifyPeerEnum::NO_VERIFY); + socket_->sslConn( + this, + std::chrono::milliseconds::zero(), + folly::SSLContext::SSLVerifyPeerEnum::NO_VERIFY); } }; @@ -1316,8 +1318,10 @@ class SSLHandshakeClientDoVerify : public SSLHandshakeBase { bool preverifyResult, bool verifyResult) : SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) { - socket_->sslConn(this, 0, - folly::SSLContext::SSLVerifyPeerEnum::VERIFY); + socket_->sslConn( + this, + std::chrono::milliseconds::zero(), + folly::SSLContext::SSLVerifyPeerEnum::VERIFY); } }; @@ -1328,7 +1332,7 @@ class SSLHandshakeServer : public SSLHandshakeBase { bool preverifyResult, bool verifyResult) : SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) { - socket_->sslAccept(this, 0); + socket_->sslAccept(this, std::chrono::milliseconds::zero()); } }; @@ -1340,7 +1344,7 @@ class SSLHandshakeServerParseClientHello : public SSLHandshakeBase { bool verifyResult) : SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) { socket_->enableClientHelloParsing(); - socket_->sslAccept(this, 0); + socket_->sslAccept(this, std::chrono::milliseconds::zero()); } std::string clientCiphers_, sharedCiphers_, serverCiphers_, chosenCipher_; @@ -1363,8 +1367,10 @@ class SSLHandshakeServerNoVerify : public SSLHandshakeBase { bool preverifyResult, bool verifyResult) : SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) { - socket_->sslAccept(this, 0, - folly::SSLContext::SSLVerifyPeerEnum::NO_VERIFY); + socket_->sslAccept( + this, + std::chrono::milliseconds::zero(), + folly::SSLContext::SSLVerifyPeerEnum::NO_VERIFY); } }; @@ -1375,8 +1381,10 @@ class SSLHandshakeServerDoVerify : public SSLHandshakeBase { bool preverifyResult, bool verifyResult) : SSLHandshakeBase(std::move(socket), preverifyResult, verifyResult) { - socket_->sslAccept(this, 0, - folly::SSLContext::SSLVerifyPeerEnum::VERIFY_REQ_CLIENT_CERT); + socket_->sslAccept( + this, + std::chrono::milliseconds::zero(), + folly::SSLContext::SSLVerifyPeerEnum::VERIFY_REQ_CLIENT_CERT); } }; diff --git a/folly/io/async/test/MockAsyncSSLSocket.h b/folly/io/async/test/MockAsyncSSLSocket.h index 699fa42f..84ff6c7b 100644 --- a/folly/io/async/test/MockAsyncSSLSocket.h +++ b/folly/io/async/test/MockAsyncSSLSocket.h @@ -54,12 +54,11 @@ class MockAsyncSSLSocket : public AsyncSSLSocket { MOCK_METHOD1(setReadCB, void(ReadCallback*)); void sslConn( - AsyncSSLSocket::HandshakeCB* cb, - uint64_t timeout, - const SSLContext::SSLVerifyPeerEnum& verify) - override { - if (timeout > 0) { - handshakeTimeout_.scheduleTimeout((uint32_t)timeout); + AsyncSSLSocket::HandshakeCB* cb, + std::chrono::milliseconds timeout, + const SSLContext::SSLVerifyPeerEnum& verify) override { + if (timeout > std::chrono::milliseconds::zero()) { + handshakeTimeout_.scheduleTimeout(timeout); } state_ = StateEnum::ESTABLISHED; @@ -70,11 +69,10 @@ class MockAsyncSSLSocket : public AsyncSSLSocket { } void sslAccept( - AsyncSSLSocket::HandshakeCB* cb, - uint32_t timeout, - const SSLContext::SSLVerifyPeerEnum& verify) - override { - if (timeout > 0) { + AsyncSSLSocket::HandshakeCB* cb, + std::chrono::milliseconds timeout, + const SSLContext::SSLVerifyPeerEnum& verify) override { + if (timeout > std::chrono::milliseconds::zero()) { handshakeTimeout_.scheduleTimeout(timeout); } @@ -86,14 +84,18 @@ class MockAsyncSSLSocket : public AsyncSSLSocket { } MOCK_METHOD3( - sslConnectMockable, - void(AsyncSSLSocket::HandshakeCB*, uint64_t, - const SSLContext::SSLVerifyPeerEnum&)); + sslConnectMockable, + void( + AsyncSSLSocket::HandshakeCB*, + std::chrono::milliseconds, + const SSLContext::SSLVerifyPeerEnum&)); MOCK_METHOD3( - sslAcceptMockable, - void(AsyncSSLSocket::HandshakeCB*, uint32_t, - const SSLContext::SSLVerifyPeerEnum&)); + sslAcceptMockable, + void( + AsyncSSLSocket::HandshakeCB*, + std::chrono::milliseconds, + const SSLContext::SSLVerifyPeerEnum&)); }; }}