proxygen
folly::SNIServer Class Reference

#include <AsyncSSLSocketTest.h>

Inheritance diagram for folly::SNIServer:
folly::AsyncSSLSocket::HandshakeCB folly::AsyncReader::ReadCallback

Public Member Functions

 SNIServer (AsyncSSLSocket::UniquePtr socket, const std::shared_ptr< folly::SSLContext > &ctx, const std::shared_ptr< folly::SSLContext > &sniCtx, const std::string &expectedServerName)
 

Public Attributes

bool serverNameMatch
 

Private Member Functions

void handshakeSuc (AsyncSSLSocket *) noexceptoverride
 
void handshakeErr (AsyncSSLSocket *, const AsyncSocketException &ex) noexceptoverride
 
void getReadBuffer (void **, size_t *lenReturn) override
 
void readDataAvailable (size_t) noexceptoverride
 
void readEOF () noexceptoverride
 
void readErr (const AsyncSocketException &ex) noexceptoverride
 
folly::SSLContext::ServerNameCallbackResult serverNameCallback (SSL *ssl)
 
- Private Member Functions inherited from folly::AsyncSSLSocket::HandshakeCB
virtual ~HandshakeCB ()=default
 
virtual bool handshakeVer (AsyncSSLSocket *, bool preverifyOk, X509_STORE_CTX *) noexcept
 
- Private Member Functions inherited from folly::AsyncReader::ReadCallback
virtual ~ReadCallback ()=default
 
virtual bool isBufferMovable () noexcept
 
virtual size_t maxBufferSize () const
 
virtual void readBufferAvailable (std::unique_ptr< IOBuf >) noexcept
 

Private Attributes

AsyncSSLSocket::UniquePtr socket_
 
std::shared_ptr< folly::SSLContextsniCtx_
 
std::string expectedServerName_
 

Detailed Description

Definition at line 1061 of file AsyncSSLSocketTest.h.

Constructor & Destructor Documentation

folly::SNIServer::SNIServer ( AsyncSSLSocket::UniquePtr  socket,
const std::shared_ptr< folly::SSLContext > &  ctx,
const std::shared_ptr< folly::SSLContext > &  sniCtx,
const std::string expectedServerName 
)
inlineexplicit

Definition at line 1064 of file AsyncSSLSocketTest.h.

References folly::netops::bind(), serverNameCallback(), and folly::SendMsgParamsCallbackBase::socket_.

1069  : serverNameMatch(false),
1071  sniCtx_(sniCtx),
1072  expectedServerName_(expectedServerName) {
1073  ctx->setServerNameCallback(
1074  std::bind(&SNIServer::serverNameCallback, this, std::placeholders::_1));
1075  socket_->sslAccept(this);
1076  }
std::string expectedServerName_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::SSLContext::ServerNameCallbackResult serverNameCallback(SSL *ssl)
std::shared_ptr< folly::SSLContext > sniCtx_
AsyncSSLSocket::UniquePtr socket_
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
int bind(NetworkSocket s, const sockaddr *name, socklen_t namelen)
Definition: NetOps.cpp:76

Member Function Documentation

void folly::SNIServer::getReadBuffer ( void **  bufReturn,
size_t *  lenReturn 
)
inlineoverrideprivatevirtual

When data becomes available, getReadBuffer() will be invoked to get the buffer into which data should be read.

This method allows the ReadCallback to delay buffer allocation until data becomes available. This allows applications to manage large numbers of idle connections, without having to maintain a separate read buffer for each idle connection.

It is possible that in some cases, getReadBuffer() may be called multiple times before readDataAvailable() is invoked. In this case, the data will be written to the buffer returned from the most recent call to readDataAvailable(). If the previous calls to readDataAvailable() returned different buffers, the ReadCallback is responsible for ensuring that they are not leaked.

If getReadBuffer() throws an exception, returns a nullptr buffer, or returns a 0 length, the ReadCallback will be uninstalled and its readError() method will be invoked.

getReadBuffer() is not allowed to change the transport state before it returns. (For example, it should never uninstall the read callback, or set a different read callback.)

Parameters
bufReturngetReadBuffer() should update *bufReturn to contain the address of the read buffer. This parameter will never be nullptr.
lenReturngetReadBuffer() should update *lenReturn to contain the maximum number of bytes that may be written to the read buffer. This parameter will never be nullptr.

Implements folly::AsyncReader::ReadCallback.

Definition at line 1087 of file AsyncSSLSocketTest.h.

1087  {
1088  *lenReturn = 0;
1089  }
void folly::SNIServer::handshakeErr ( AsyncSSLSocket sock,
const AsyncSocketException ex 
)
inlineoverrideprivatevirtualnoexcept

handshakeErr() is called if an error occurs while establishing the SSL connection.

The HandshakeCB will be uninstalled before handshakeErr() is called.

Parameters
sockSSL socket on which the handshake was initiated
exAn exception representing the error.

Implements folly::AsyncSSLSocket::HandshakeCB.

Definition at line 1082 of file AsyncSSLSocketTest.h.

References ADD_FAILURE.

1084  {
1085  ADD_FAILURE() << "server handshake error: " << ex.what();
1086  }
#define ADD_FAILURE()
Definition: gtest.h:1808
void folly::SNIServer::handshakeSuc ( AsyncSSLSocket sock)
inlineoverrideprivatevirtualnoexcept

handshakeSuc() is called when a new SSL connection is established, i.e., after SSL_accept/connect() returns successfully.

The HandshakeCB will be uninstalled before handshakeSuc() is called.

Parameters
sockSSL socket on which the handshake was initiated

Implements folly::AsyncSSLSocket::HandshakeCB.

Definition at line 1081 of file AsyncSSLSocketTest.h.

1081 {}
void folly::SNIServer::readDataAvailable ( size_t  len)
inlineoverrideprivatevirtualnoexcept

readDataAvailable() will be invoked when data has been successfully read into the buffer returned by the last call to getReadBuffer().

The read callback remains installed after readDataAvailable() returns. It must be explicitly uninstalled to stop receiving read events. getReadBuffer() will be called at least once before each call to readDataAvailable(). getReadBuffer() will also be called before any call to readEOF().

Parameters
lenThe number of bytes placed in the buffer.

Implements folly::AsyncReader::ReadCallback.

Definition at line 1090 of file AsyncSSLSocketTest.h.

1090 {}
void folly::SNIServer::readEOF ( )
inlineoverrideprivatevirtualnoexcept

readEOF() will be invoked when the transport is closed.

The read callback will be automatically uninstalled immediately before readEOF() is invoked.

Implements folly::AsyncReader::ReadCallback.

Definition at line 1091 of file AsyncSSLSocketTest.h.

References folly::SendMsgParamsCallbackBase::socket_.

1091  {
1092  socket_->close();
1093  }
AsyncSSLSocket::UniquePtr socket_
void folly::SNIServer::readErr ( const AsyncSocketException ex)
inlineoverrideprivatevirtualnoexcept

readError() will be invoked if an error occurs reading from the transport.

The read callback will be automatically uninstalled immediately before readError() is invoked.

Parameters
exAn exception describing the error that occurred.

Implements folly::AsyncReader::ReadCallback.

Definition at line 1094 of file AsyncSSLSocketTest.h.

References ADD_FAILURE.

1094  {
1095  ADD_FAILURE() << "server read error: " << ex.what();
1096  }
#define ADD_FAILURE()
Definition: gtest.h:1808
folly::SSLContext::ServerNameCallbackResult folly::SNIServer::serverNameCallback ( SSL *  ssl)
inlineprivate

Definition at line 1098 of file AsyncSSLSocketTest.h.

References folly::AsyncSSLSocket::getFromSSL().

Referenced by SNIServer().

1098  {
1099  const char* sn = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
1100  if (sniCtx_ && sn && !strcasecmp(expectedServerName_.c_str(), sn)) {
1101  AsyncSSLSocket* sslSocket = AsyncSSLSocket::getFromSSL(ssl);
1102  sslSocket->switchServerSSLContext(sniCtx_);
1103  serverNameMatch = true;
1104  return folly::SSLContext::SERVER_NAME_FOUND;
1105  } else {
1106  serverNameMatch = false;
1107  return folly::SSLContext::SERVER_NAME_NOT_FOUND;
1108  }
1109  }
std::string expectedServerName_
std::shared_ptr< folly::SSLContext > sniCtx_
static AsyncSSLSocket * getFromSSL(const SSL *ssl)

Member Data Documentation

std::string folly::SNIServer::expectedServerName_
private

Definition at line 1113 of file AsyncSSLSocketTest.h.

bool folly::SNIServer::serverNameMatch

Definition at line 1078 of file AsyncSSLSocketTest.h.

Referenced by folly::TEST().

std::shared_ptr<folly::SSLContext> folly::SNIServer::sniCtx_
private

Definition at line 1112 of file AsyncSSLSocketTest.h.

AsyncSSLSocket::UniquePtr folly::SNIServer::socket_
private

Definition at line 1111 of file AsyncSSLSocketTest.h.


The documentation for this class was generated from the following file: