Lines
97.2 %
Functions
100 %
#include "source/common/tls/io_handle_bio.h"
#include "envoy/buffer/buffer.h"
#include "envoy/network/io_handle.h"
#include "openssl/bio.h"
#include "openssl/err.h"
namespace Envoy {
namespace Extensions {
namespace TransportSockets {
namespace Tls {
namespace {
// NOLINTNEXTLINE(readability-identifier-naming)
inline Envoy::Network::IoHandle* bio_io_handle(BIO* bio) {
return reinterpret_cast<Envoy::Network::IoHandle*>(BIO_get_data(bio));
}
int io_handle_read(BIO* b, char* out, int outl) {
if (out == nullptr) {
return 0;
Envoy::Buffer::RawSlice slice;
slice.mem_ = out;
slice.len_ = outl;
auto result = bio_io_handle(b)->readv(outl, &slice, 1);
BIO_clear_retry_flags(b);
if (!result.ok()) {
auto err = result.err_->getErrorCode();
if (err == Api::IoError::IoErrorCode::Again || err == Api::IoError::IoErrorCode::Interrupt) {
BIO_set_retry_read(b);
} else {
ERR_put_error(ERR_LIB_SYS, 0, result.err_->getSystemErrorCode(), __FILE__, __LINE__);
return -1;
return result.return_value_;
int io_handle_write(BIO* b, const char* in, int inl) {
slice.mem_ = const_cast<char*>(in);
slice.len_ = inl;
auto result = bio_io_handle(b)->writev(&slice, 1);
BIO_set_retry_write(b);
long io_handle_ctrl(BIO*, int cmd, long, void*) {
long ret = 1;
switch (cmd) {
case BIO_CTRL_FLUSH:
ret = 1;
break;
default:
ret = 0;
return ret;
const BIO_METHOD* BIO_s_io_handle(void) {
static const BIO_METHOD* method = [&] {
BIO_METHOD* ret = BIO_meth_new(BIO_TYPE_SOCKET, "io_handle");
RELEASE_ASSERT(ret != nullptr, "");
RELEASE_ASSERT(BIO_meth_set_read(ret, io_handle_read), "");
RELEASE_ASSERT(BIO_meth_set_write(ret, io_handle_write), "");
RELEASE_ASSERT(BIO_meth_set_ctrl(ret, io_handle_ctrl), "");
}();
return method;
} // namespace
BIO* BIO_new_io_handle(Envoy::Network::IoHandle* io_handle) {
BIO* b;
b = BIO_new(BIO_s_io_handle());
RELEASE_ASSERT(b != nullptr, "");
// Initialize the BIO
BIO_set_data(b, io_handle);
BIO_set_init(b, 1);
return b;
} // namespace Tls
} // namespace TransportSockets
} // namespace Extensions
} // namespace Envoy