Commit 663d3a7b authored by Subodh Iyengar's avatar Subodh Iyengar Committed by facebook-github-bot-1

Refactor HandshakeHelper and add a peeking handshake helper

Summary: This adds support to Acceptor to be able to switch between
multiple protcols when SSL is being negotiated using MSG_PEEK.

The motivation for this is to be able to try out multiple protocols.

Reviewed By: @djwatson

Differential Revision: D2327946
parent 942ad3f3
...@@ -1235,7 +1235,12 @@ ssize_t AsyncSocket::performRead(void** buf, size_t* buflen, size_t* offset) { ...@@ -1235,7 +1235,12 @@ ssize_t AsyncSocket::performRead(void** buf, size_t* buflen, size_t* offset) {
VLOG(5) << "AsyncSocket::performRead() this=" << this VLOG(5) << "AsyncSocket::performRead() this=" << this
<< ", buf=" << *buf << ", buflen=" << *buflen; << ", buf=" << *buf << ", buflen=" << *buflen;
ssize_t bytes = recv(fd_, *buf, *buflen, MSG_DONTWAIT); int recvFlags = 0;
if (peek_) {
recvFlags |= MSG_PEEK;
}
ssize_t bytes = recv(fd_, *buf, *buflen, MSG_DONTWAIT | recvFlags);
if (bytes < 0) { if (bytes < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// No more data to read right now. // No more data to read right now.
......
...@@ -475,6 +475,10 @@ class AsyncSocket : virtual public AsyncTransportWrapper { ...@@ -475,6 +475,10 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
return setsockopt(fd_, level, optname, optval, sizeof(T)); return setsockopt(fd_, level, optname, optval, sizeof(T));
} }
virtual void setPeek(bool peek) {
peek_ = peek;
}
enum class StateEnum : uint8_t { enum class StateEnum : uint8_t {
UNINIT, UNINIT,
CONNECTING, CONNECTING,
...@@ -764,6 +768,8 @@ class AsyncSocket : virtual public AsyncTransportWrapper { ...@@ -764,6 +768,8 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
size_t appBytesReceived_; ///< Num of bytes received from socket size_t appBytesReceived_; ///< Num of bytes received from socket
size_t appBytesWritten_; ///< Num of bytes written to socket size_t appBytesWritten_; ///< Num of bytes written to socket
bool isBufferMovable_{false}; bool isBufferMovable_{false};
bool peek_{false}; // Peek bytes.
}; };
......
...@@ -24,8 +24,9 @@ class MockAsyncSSLSocket : public AsyncSSLSocket { ...@@ -24,8 +24,9 @@ class MockAsyncSSLSocket : public AsyncSSLSocket {
public: public:
MockAsyncSSLSocket( MockAsyncSSLSocket(
const std::shared_ptr<SSLContext>& ctx, const std::shared_ptr<SSLContext>& ctx,
EventBase* base) : EventBase* base,
AsyncSSLSocket(ctx, base) { bool deferSecurityNegotiation = false) :
AsyncSSLSocket(ctx, base, deferSecurityNegotiation) {
} }
GMOCK_METHOD5_(, noexcept, , GMOCK_METHOD5_(, noexcept, ,
...@@ -47,6 +48,8 @@ class MockAsyncSSLSocket : public AsyncSSLSocket { ...@@ -47,6 +48,8 @@ class MockAsyncSSLSocket : public AsyncSSLSocket {
MOCK_CONST_METHOD2( MOCK_CONST_METHOD2(
getSelectedNextProtocolNoThrow, getSelectedNextProtocolNoThrow,
bool(const unsigned char**, unsigned*)); bool(const unsigned char**, unsigned*));
MOCK_METHOD1(setPeek, void(bool));
MOCK_METHOD1(setReadCB, void(ReadCallback*));
void sslConn( void sslConn(
AsyncSSLSocket::HandshakeCB* cb, AsyncSSLSocket::HandshakeCB* cb,
...@@ -63,10 +66,32 @@ class MockAsyncSSLSocket : public AsyncSSLSocket { ...@@ -63,10 +66,32 @@ class MockAsyncSSLSocket : public AsyncSSLSocket {
sslConnectMockable(cb, timeout, verify); sslConnectMockable(cb, timeout, verify);
} }
void sslAccept(
AsyncSSLSocket::HandshakeCB* cb,
uint32_t timeout,
const SSLContext::SSLVerifyPeerEnum& verify)
override {
if (timeout > 0) {
handshakeTimeout_.scheduleTimeout(timeout);
}
state_ = StateEnum::ESTABLISHED;
sslState_ = STATE_ACCEPTING;
handshakeCallback_ = cb;
sslAcceptMockable(cb, timeout, verify);
}
MOCK_METHOD3( MOCK_METHOD3(
sslConnectMockable, sslConnectMockable,
void(AsyncSSLSocket::HandshakeCB*, uint64_t, void(AsyncSSLSocket::HandshakeCB*, uint64_t,
const SSLContext::SSLVerifyPeerEnum&)); const SSLContext::SSLVerifyPeerEnum&));
MOCK_METHOD3(
sslAcceptMockable,
void(AsyncSSLSocket::HandshakeCB*, uint32_t,
const SSLContext::SSLVerifyPeerEnum&));
}; };
}} }}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment