proxygen
folly::SSLClient Class Reference

#include <AsyncSSLSocketTest.h>

Inheritance diagram for folly::SSLClient:
folly::AsyncSocket::ConnectCallback folly::AsyncWriter::WriteCallback folly::AsyncReader::ReadCallback

Public Member Functions

 SSLClient (EventBase *eventBase, const folly::SocketAddress &address, uint32_t requests, uint32_t timeout=0)
 
 ~SSLClient () override
 
uint32_t getHit () const
 
uint32_t getMiss () const
 
uint32_t getErrors () const
 
uint32_t getWriteAfterConnectErrors () const
 
void connect (bool writeNow=false)
 
void connectSuccess () noexceptoverride
 
void connectErr (const AsyncSocketException &ex) noexceptoverride
 
void writeSuccess () noexceptoverride
 
void writeErr (size_t, const AsyncSocketException &ex) noexceptoverride
 
void getReadBuffer (void **bufReturn, size_t *lenReturn) override
 
void readEOF () noexceptoverride
 
void readErr (const AsyncSocketException &ex) noexceptoverride
 
void readDataAvailable (size_t len) noexceptoverride
 
- Public Member Functions inherited from folly::AsyncSocket::ConnectCallback
virtual ~ConnectCallback ()=default
 
- Public Member Functions inherited from folly::AsyncWriter::WriteCallback
virtual ~WriteCallback ()=default
 
- Public 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

EventBaseeventBase_
 
std::shared_ptr< AsyncSSLSocketsslSocket_
 
SSL_SESSION * session_
 
std::shared_ptr< folly::SSLContextctx_
 
uint32_t requests_
 
folly::SocketAddress address_
 
uint32_t timeout_
 
char buf_ [128]
 
char readbuf_ [128]
 
uint32_t bytesRead_
 
uint32_t hit_
 
uint32_t miss_
 
uint32_t errors_
 
uint32_t writeAfterConnectErrors_
 

Static Private Attributes

static constexpr size_t kMaxReadsPerEvent = 2
 
static constexpr size_t kMaxReadBufferSz
 

Detailed Description

Definition at line 1117 of file AsyncSSLSocketTest.h.

Constructor & Destructor Documentation

folly::SSLClient::SSLClient ( EventBase eventBase,
const folly::SocketAddress address,
uint32_t  requests,
uint32_t  timeout = 0 
)
inline

Definition at line 1145 of file AsyncSSLSocketTest.h.

References folly::SocketAddress::reset(), and folly::detail::timeout.

1150  : eventBase_(eventBase),
1151  session_(nullptr),
1153  address_(address),
1154  timeout_(timeout),
1155  bytesRead_(0),
1156  hit_(0),
1157  miss_(0),
1158  errors_(0),
1160  ctx_.reset(new folly::SSLContext());
1161  ctx_->setOptions(SSL_OP_NO_TICKET);
1162  ctx_->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
1163  memset(buf_, 'a', sizeof(buf_));
1164  }
uint32_t writeAfterConnectErrors_
const struct message requests[]
Definition: test.c:80
SSL_SESSION * session_
folly::SocketAddress address_
std::shared_ptr< folly::SSLContext > ctx_
folly::SSLClient::~SSLClient ( )
inlineoverride

Definition at line 1166 of file AsyncSSLSocketTest.h.

References EXPECT_EQ.

1166  {
1167  if (session_) {
1168  SSL_SESSION_free(session_);
1169  }
1170  if (errors_ == 0) {
1171  EXPECT_EQ(bytesRead_, sizeof(buf_));
1172  }
1173  }
SSL_SESSION * session_
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922

Member Function Documentation

void folly::SSLClient::connect ( bool  writeNow = false)
inline

Definition at line 1191 of file AsyncSSLSocketTest.h.

References folly::AsyncSSLSocket::newSocket().

1191  {
1193  if (session_ != nullptr) {
1194  sslSocket_->setSSLSession(session_);
1195  }
1196  requests_--;
1197  sslSocket_->connect(this, address_, timeout_);
1198  if (sslSocket_ && writeNow) {
1199  // write some junk, used in an error test
1200  sslSocket_->write(this, buf_, sizeof(buf_));
1201  }
1202  }
SSL_SESSION * session_
static std::shared_ptr< AsyncSSLSocket > newSocket(const std::shared_ptr< folly::SSLContext > &ctx, EventBase *evb, int fd, bool server=true, bool deferSecurityNegotiation=false)
folly::SocketAddress address_
std::shared_ptr< AsyncSSLSocket > sslSocket_
std::shared_ptr< folly::SSLContext > ctx_
void folly::SSLClient::connectErr ( const AsyncSocketException ex)
inlineoverridevirtualnoexcept

connectErr() will be invoked if the connection attempt fails.

Parameters
exAn exception describing the error that occurred.

Implements folly::AsyncSocket::ConnectCallback.

Definition at line 1224 of file AsyncSSLSocketTest.h.

1224  {
1225  std::cerr << "SSLClient::connectError: " << ex.what() << std::endl;
1226  errors_++;
1227  sslSocket_.reset();
1228  }
std::shared_ptr< AsyncSSLSocket > sslSocket_
void folly::SSLClient::connectSuccess ( )
inlineoverridevirtualnoexcept

connectSuccess() will be invoked when the connection has been successfully established.

Implements folly::AsyncSocket::ConnectCallback.

Definition at line 1204 of file AsyncSSLSocketTest.h.

1204  {
1205  std::cerr << "client SSL socket connected" << std::endl;
1206  if (sslSocket_->getSSLSessionReused()) {
1207  hit_++;
1208  } else {
1209  miss_++;
1210  if (session_ != nullptr) {
1211  SSL_SESSION_free(session_);
1212  }
1213  session_ = sslSocket_->getSSLSession();
1214  }
1215 
1216  // write()
1217  sslSocket_->setMaxReadsPerEvent(kMaxReadsPerEvent);
1218  sslSocket_->write(this, buf_, sizeof(buf_));
1219  sslSocket_->setReadCB(this);
1220  memset(readbuf_, 'b', sizeof(readbuf_));
1221  bytesRead_ = 0;
1222  }
SSL_SESSION * session_
static constexpr size_t kMaxReadsPerEvent
std::shared_ptr< AsyncSSLSocket > sslSocket_
uint32_t folly::SSLClient::getErrors ( ) const
inline

Definition at line 1183 of file AsyncSSLSocketTest.h.

1183  {
1184  return errors_;
1185  }
uint32_t folly::SSLClient::getHit ( ) const
inline

Definition at line 1175 of file AsyncSSLSocketTest.h.

1175  {
1176  return hit_;
1177  }
uint32_t folly::SSLClient::getMiss ( ) const
inline

Definition at line 1179 of file AsyncSSLSocketTest.h.

1179  {
1180  return miss_;
1181  }
void folly::SSLClient::getReadBuffer ( void **  bufReturn,
size_t *  lenReturn 
)
inlineoverridevirtual

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 1243 of file AsyncSSLSocketTest.h.

References min.

1243  {
1244  *bufReturn = readbuf_ + bytesRead_;
1245  *lenReturn = std::min(kMaxReadBufferSz, sizeof(readbuf_) - bytesRead_);
1246  }
LogLevel min
Definition: LogLevel.cpp:30
static constexpr size_t kMaxReadBufferSz
uint32_t folly::SSLClient::getWriteAfterConnectErrors ( ) const
inline

Definition at line 1187 of file AsyncSSLSocketTest.h.

1187  {
1188  return writeAfterConnectErrors_;
1189  }
uint32_t writeAfterConnectErrors_
void folly::SSLClient::readDataAvailable ( size_t  len)
inlineoverridevirtualnoexcept

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 1256 of file AsyncSSLSocketTest.h.

References folly::netops::connect(), and EXPECT_EQ.

1256  {
1257  std::cerr << "client read data: " << len << std::endl;
1258  bytesRead_ += len;
1259  if (bytesRead_ == sizeof(buf_)) {
1260  EXPECT_EQ(memcmp(buf_, readbuf_, bytesRead_), 0);
1261  sslSocket_->closeNow();
1262  sslSocket_.reset();
1263  if (requests_ != 0) {
1264  connect();
1265  }
1266  }
1267  }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::shared_ptr< AsyncSSLSocket > sslSocket_
void connect(bool writeNow=false)
void folly::SSLClient::readEOF ( )
inlineoverridevirtualnoexcept

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 1248 of file AsyncSSLSocketTest.h.

1248  {
1249  std::cerr << "client readEOF" << std::endl;
1250  }
void folly::SSLClient::readErr ( const AsyncSocketException ex)
inlineoverridevirtualnoexcept

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 1252 of file AsyncSSLSocketTest.h.

1252  {
1253  std::cerr << "client readError: " << ex.what() << std::endl;
1254  }
void folly::SSLClient::writeErr ( size_t  bytesWritten,
const AsyncSocketException ex 
)
inlineoverridevirtualnoexcept

writeError() will be invoked if an error occurs writing the data.

Parameters
bytesWrittenThe number of bytes that were successfull
exAn exception describing the error that occurred.

Implements folly::AsyncWriter::WriteCallback.

Definition at line 1234 of file AsyncSSLSocketTest.h.

1236  {
1237  std::cerr << "client writeError: " << ex.what() << std::endl;
1238  if (!sslSocket_) {
1240  }
1241  }
uint32_t writeAfterConnectErrors_
std::shared_ptr< AsyncSSLSocket > sslSocket_
void folly::SSLClient::writeSuccess ( )
inlineoverridevirtualnoexcept

writeSuccess() will be invoked when all of the data has been successfully written.

Note that this mainly signals that the buffer containing the data to write is no longer needed and may be freed or re-used. It does not guarantee that the data has been fully transmitted to the remote endpoint. For example, on socket-based transports, writeSuccess() only indicates that the data has been given to the kernel for eventual transmission.

Implements folly::AsyncWriter::WriteCallback.

Definition at line 1230 of file AsyncSSLSocketTest.h.

1230  {
1231  std::cerr << "client write success" << std::endl;
1232  }

Member Data Documentation

folly::SocketAddress folly::SSLClient::address_
private

Definition at line 1126 of file AsyncSSLSocketTest.h.

char folly::SSLClient::buf_[128]
private

Definition at line 1128 of file AsyncSSLSocketTest.h.

uint32_t folly::SSLClient::bytesRead_
private

Definition at line 1130 of file AsyncSSLSocketTest.h.

std::shared_ptr<folly::SSLContext> folly::SSLClient::ctx_
private

Definition at line 1124 of file AsyncSSLSocketTest.h.

uint32_t folly::SSLClient::errors_
private

Definition at line 1133 of file AsyncSSLSocketTest.h.

EventBase* folly::SSLClient::eventBase_
private

Definition at line 1121 of file AsyncSSLSocketTest.h.

uint32_t folly::SSLClient::hit_
private

Definition at line 1131 of file AsyncSSLSocketTest.h.

constexpr size_t folly::SSLClient::kMaxReadBufferSz
staticprivate
Initial value:
=
sizeof(decltype(readbuf_)) / kMaxReadsPerEvent / 2

Definition at line 1141 of file AsyncSSLSocketTest.h.

constexpr size_t folly::SSLClient::kMaxReadsPerEvent = 2
staticprivate

Definition at line 1139 of file AsyncSSLSocketTest.h.

uint32_t folly::SSLClient::miss_
private

Definition at line 1132 of file AsyncSSLSocketTest.h.

char folly::SSLClient::readbuf_[128]
private

Definition at line 1129 of file AsyncSSLSocketTest.h.

uint32_t folly::SSLClient::requests_
private

Definition at line 1125 of file AsyncSSLSocketTest.h.

SSL_SESSION* folly::SSLClient::session_
private

Definition at line 1123 of file AsyncSSLSocketTest.h.

std::shared_ptr<AsyncSSLSocket> folly::SSLClient::sslSocket_
private

Definition at line 1122 of file AsyncSSLSocketTest.h.

uint32_t folly::SSLClient::timeout_
private

Definition at line 1127 of file AsyncSSLSocketTest.h.

uint32_t folly::SSLClient::writeAfterConnectErrors_
private

Definition at line 1134 of file AsyncSSLSocketTest.h.


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