2022-02-18 03:59:21 -05:00
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
#define CHBUF_SIZE (256 * 1024)
|
|
|
|
|
#define POLLVEC_SIZE (DrivenEngine::MAX_CHAN + 1)
|
2022-02-21 20:23:01 -05:00
|
|
|
|
2022-01-11 13:59:13 -05:00
|
|
|
static MonoClock monoclock;
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
namespace util
|
|
|
|
|
{
|
|
|
|
|
double profiling_clock()
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
return monoclock.get();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
static void if_error_print_and_exit(const std::string &str)
|
|
|
|
|
{
|
|
|
|
|
if (!str.empty())
|
|
|
|
|
{
|
|
|
|
|
std::cerr << std::endl
|
|
|
|
|
<< "error: " << str << std::endl;
|
2022-01-11 13:59:13 -05:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
static std::string_view read_file(const char *fn, char *buf, int bufsize, std::string &err)
|
|
|
|
|
{
|
2022-02-21 20:23:01 -05:00
|
|
|
FILE *f = fopen(fn, "r");
|
2022-03-18 16:25:20 -04:00
|
|
|
if (f == 0)
|
|
|
|
|
{
|
2022-02-25 19:57:23 -05:00
|
|
|
err = std::string("cannot read file") + fn;
|
2022-02-21 20:23:01 -05:00
|
|
|
buf[0] = 0;
|
2022-03-18 16:25:20 -04:00
|
|
|
return std::string_view(buf, 0);
|
2022-02-21 20:23:01 -05:00
|
|
|
}
|
|
|
|
|
int nread = fread(buf, 1, bufsize, f);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (nread < 0)
|
|
|
|
|
{
|
2022-02-25 19:57:23 -05:00
|
|
|
err = std::string("cannot read file: ") + fn;
|
2022-02-21 20:23:01 -05:00
|
|
|
buf[0] = 0;
|
2022-02-24 13:50:43 -05:00
|
|
|
return std::string_view(buf, 0);
|
2022-02-21 20:23:01 -05:00
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
if (nread == bufsize)
|
|
|
|
|
{
|
2022-02-25 19:57:23 -05:00
|
|
|
err = std::string("file too large: ") + fn;
|
2022-02-21 20:23:01 -05:00
|
|
|
buf[0] = 0;
|
2022-02-24 13:50:43 -05:00
|
|
|
return std::string_view(buf, 0);
|
2022-02-21 20:23:01 -05:00
|
|
|
}
|
|
|
|
|
err = "";
|
2022-02-24 13:50:43 -05:00
|
|
|
return std::string_view(buf, nread);
|
2022-02-21 20:23:01 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
struct SSL_CTX_Deleter
|
|
|
|
|
{
|
|
|
|
|
void operator()(SSL_CTX *ctx)
|
|
|
|
|
{
|
2022-03-12 14:56:33 -05:00
|
|
|
SSL_CTX_free(ctx);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using UniqueSSLCTX = std::unique_ptr<SSL_CTX, SSL_CTX_Deleter>;
|
2022-02-21 20:23:01 -05:00
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
static std::string ssl_errors_string(bool lastonly = true)
|
|
|
|
|
{
|
|
|
|
|
std::string err;
|
|
|
|
|
const char *file, *data, *func;
|
|
|
|
|
int line, flags;
|
|
|
|
|
|
2022-03-18 21:52:45 -04:00
|
|
|
// ERR_print_errors_fp(stderr);
|
|
|
|
|
// exit(1);
|
2022-03-18 16:25:20 -04:00
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
unsigned long code = ERR_get_error_all(&file, &line, &func, &data, &flags);
|
|
|
|
|
if (code == 0)
|
|
|
|
|
break;
|
|
|
|
|
std::string reason;
|
|
|
|
|
if (ERR_SYSTEM_ERROR(code))
|
|
|
|
|
{
|
|
|
|
|
reason = strerror_str(ERR_GET_REASON(code));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const char *rc = ERR_reason_error_string(code);
|
|
|
|
|
reason = (rc == nullptr) ? "unknown" : rc;
|
|
|
|
|
}
|
|
|
|
|
if (err.empty() || lastonly)
|
|
|
|
|
{
|
|
|
|
|
err = reason;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
err = err + ", " + reason;
|
|
|
|
|
}
|
|
|
|
|
if (data != nullptr)
|
|
|
|
|
{
|
|
|
|
|
err = err + " " + data;
|
|
|
|
|
}
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
return err;
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void assert_ssl_errors_empty()
|
|
|
|
|
{
|
|
|
|
|
int code = ERR_peek_error();
|
|
|
|
|
if (code != 0)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "SSL should not have errors at this point." << std::endl;
|
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
static int ssl_ctx_use_certificate_str(SSL_CTX *ctx, const char *str)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
BIO *bio = BIO_new(BIO_s_mem());
|
|
|
|
|
BIO_puts(bio, str);
|
|
|
|
|
X509 *certificate = PEM_read_bio_X509(bio, NULL, NULL, NULL);
|
|
|
|
|
BIO_free(bio);
|
|
|
|
|
int status = SSL_CTX_use_certificate(ctx, certificate);
|
|
|
|
|
X509_free(certificate);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
static int ssl_ctx_use_privatekey_str(SSL_CTX *ctx, const char *str)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
BIO *bio = BIO_new(BIO_s_mem());
|
|
|
|
|
BIO_puts(bio, str);
|
|
|
|
|
EVP_PKEY *pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
|
|
|
|
|
BIO_free(bio);
|
|
|
|
|
int status = SSL_CTX_use_PrivateKey(ctx, pkey);
|
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
static void ssl_ctx_use_dummycert(SSL_CTX *ctx)
|
|
|
|
|
{
|
|
|
|
|
if (ssl_ctx_use_certificate_str(ctx, dummycert::certificate) <= 0)
|
|
|
|
|
{
|
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
if (ssl_ctx_use_privatekey_str(ctx, dummycert::privatekey) <= 0)
|
|
|
|
|
{
|
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-17 20:02:08 -05:00
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
class Driver
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
public:
|
2022-03-18 16:25:20 -04:00
|
|
|
enum ChanState
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
CHAN_INACTIVE,
|
|
|
|
|
CHAN_PLAINTEXT,
|
|
|
|
|
CHAN_SSL_CONNECTING,
|
|
|
|
|
CHAN_SSL_ACCEPTING,
|
|
|
|
|
CHAN_SSL_READWRITE,
|
|
|
|
|
};
|
2022-03-18 16:25:20 -04:00
|
|
|
struct ChanInfo
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
int chid;
|
|
|
|
|
SOCKET socket;
|
|
|
|
|
SSL *ssl;
|
|
|
|
|
|
|
|
|
|
ChanState state;
|
|
|
|
|
int nbytes;
|
|
|
|
|
const char *bytes;
|
|
|
|
|
bool released;
|
|
|
|
|
bool just_released;
|
|
|
|
|
bool ready_now;
|
|
|
|
|
bool ready_on_pollin;
|
|
|
|
|
bool ready_on_pollout;
|
|
|
|
|
bool ready_on_outgoing;
|
|
|
|
|
int last_write_nbytes;
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-25 19:57:23 -05:00
|
|
|
std::vector<ChanInfo> chans_;
|
|
|
|
|
std::map<int, SOCKET> listen_sockets_;
|
2022-01-11 13:59:13 -05:00
|
|
|
bool read_console_recently_;
|
2022-03-11 18:35:51 -05:00
|
|
|
std::unique_ptr<char[]> chbuf_;
|
|
|
|
|
std::unique_ptr<struct pollfd[]> pollvec_;
|
|
|
|
|
drv::ReplayRecorder recorder_;
|
2022-01-11 13:59:13 -05:00
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
UniqueSSLCTX ssl_server_ctx_;
|
|
|
|
|
UniqueSSLCTX ssl_client_secure_ctx_;
|
|
|
|
|
UniqueSSLCTX ssl_client_insecure_ctx_;
|
2022-02-17 20:02:08 -05:00
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void handle_listen_ports()
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
const auto &listenports = recorder_.drv_get_listen_ports();
|
2022-03-18 16:25:20 -04:00
|
|
|
for (int port : listenports)
|
|
|
|
|
{
|
|
|
|
|
if (listen_sockets_.find(port) == listen_sockets_.end())
|
|
|
|
|
{
|
2022-02-25 19:57:23 -05:00
|
|
|
std::string err;
|
2022-01-11 13:59:13 -05:00
|
|
|
SOCKET sock = listen_on_port(port, err);
|
|
|
|
|
if_error_print_and_exit(err);
|
|
|
|
|
assert(sock != INVALID_SOCKET);
|
|
|
|
|
listen_sockets_[port] = sock;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void handle_lua_source()
|
|
|
|
|
{
|
|
|
|
|
if (recorder_.drv_get_rescan_lua_source())
|
|
|
|
|
{
|
2022-02-25 19:57:23 -05:00
|
|
|
std::string err;
|
2022-03-11 18:35:51 -05:00
|
|
|
std::string_view ctrl = read_file("lua/control.lst", chbuf_.get(), CHBUF_SIZE, err);
|
2022-02-18 03:59:21 -05:00
|
|
|
if_error_print_and_exit(err);
|
2022-02-25 19:57:23 -05:00
|
|
|
std::vector<std::string> names = drv::parse_control_lst(ctrl);
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_clear_lua_source();
|
2022-03-18 16:25:20 -04:00
|
|
|
for (const std::string &str : names)
|
|
|
|
|
{
|
2022-02-25 19:57:23 -05:00
|
|
|
std::string lfn = std::string("lua/") + str;
|
2022-03-11 18:35:51 -05:00
|
|
|
std::string_view data = read_file(lfn.c_str(), chbuf_.get(), CHBUF_SIZE, err);
|
2022-02-18 03:59:21 -05:00
|
|
|
if_error_print_and_exit(err);
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_add_lua_source(str, data);
|
2022-02-18 03:59:21 -05:00
|
|
|
}
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
|
|
|
|
|
void close_channel(ChanInfo &chan, std::string_view err)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
// std::cerr << "Closing channel " << chan.chid << std::endl;
|
|
|
|
|
assert(chan.state != CHAN_INACTIVE);
|
|
|
|
|
// Close and release the SSL channel.
|
2022-03-18 16:25:20 -04:00
|
|
|
if (chan.ssl != nullptr)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
SSL_free(chan.ssl);
|
|
|
|
|
chan.ssl = nullptr;
|
|
|
|
|
}
|
|
|
|
|
// Close and release the socket.
|
|
|
|
|
assert(chan.socket != INVALID_SOCKET);
|
|
|
|
|
assert(socket_close(chan.socket) == 0);
|
|
|
|
|
chan.socket = INVALID_SOCKET;
|
|
|
|
|
// Close everything else.
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_notify_close(chan.chid, err);
|
2022-01-11 13:59:13 -05:00
|
|
|
chan.state = CHAN_INACTIVE;
|
|
|
|
|
chan.chid = -1;
|
|
|
|
|
chan.nbytes = 0;
|
|
|
|
|
chan.bytes = 0;
|
|
|
|
|
chan.released = false;
|
|
|
|
|
chan.just_released = false;
|
|
|
|
|
chan.ready_now = false;
|
|
|
|
|
chan.ready_on_pollin = false;
|
|
|
|
|
chan.ready_on_pollout = false;
|
|
|
|
|
chan.ready_on_outgoing = false;
|
|
|
|
|
chan.last_write_nbytes = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void cleanup_channels()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < int(chans_.size());)
|
|
|
|
|
{
|
|
|
|
|
if (chans_[i].state == CHAN_INACTIVE)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
chans_[i] = chans_.back();
|
|
|
|
|
chans_.pop_back();
|
2022-03-18 16:25:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void handle_console_output()
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
std::string_view s = recorder_.drv_peek_outgoing(0);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (s.size() == 0)
|
|
|
|
|
break;
|
2022-03-04 16:45:47 -05:00
|
|
|
int nwrote = console_write(s.data(), s.size());
|
2022-03-18 16:25:20 -04:00
|
|
|
if (nwrote <= 0)
|
|
|
|
|
break;
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_sent_outgoing(0, nwrote);
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void handle_console_input()
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
char buffer[256];
|
|
|
|
|
read_console_recently_ = false;
|
2022-03-18 16:25:20 -04:00
|
|
|
while (true)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
int nread = console_read(buffer, 256);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (nread <= 0)
|
|
|
|
|
break;
|
2022-01-11 13:59:13 -05:00
|
|
|
read_console_recently_ = true;
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_recv_incoming(0, std::string_view(buffer, nread));
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void make_channel(SOCKET sock, int chid, SSL_CTX *ctx, ChanState state)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
ChanInfo newchan;
|
|
|
|
|
newchan.chid = chid;
|
|
|
|
|
newchan.socket = sock;
|
|
|
|
|
newchan.ssl = SSL_new(ctx);
|
|
|
|
|
newchan.state = state;
|
|
|
|
|
newchan.nbytes = 0;
|
|
|
|
|
newchan.bytes = 0;
|
|
|
|
|
newchan.released = false;
|
|
|
|
|
newchan.just_released = false;
|
|
|
|
|
newchan.ready_now = false;
|
|
|
|
|
newchan.ready_on_pollin = false;
|
|
|
|
|
newchan.ready_on_pollout = true;
|
|
|
|
|
newchan.ready_on_outgoing = false;
|
|
|
|
|
newchan.last_write_nbytes = 0;
|
|
|
|
|
SSL_set_fd(newchan.ssl, newchan.socket);
|
|
|
|
|
// SSL_set_msg_callback(newchan.ssl, SSL_trace);
|
|
|
|
|
// SSL_set_msg_callback_arg(newchan.ssl, BIO_new_fp(stderr,0));
|
|
|
|
|
chans_.push_back(newchan);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void handle_new_outgoing_sockets()
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
const auto &chans = recorder_.drv_get_new_outgoing();
|
2022-03-18 16:25:20 -04:00
|
|
|
for (int chid : chans)
|
|
|
|
|
{
|
|
|
|
|
std::string err, cert, host, port;
|
|
|
|
|
std::string target(recorder_.drv_get_target(chid));
|
|
|
|
|
drv::split_target(target, cert, host, port);
|
|
|
|
|
if (cert.empty() || host.empty() || port.empty()) {
|
|
|
|
|
recorder_.drv_notify_close(chid, std::string("invalid target: ") + target);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
SSL_CTX *ctx = nullptr;
|
|
|
|
|
if (cert == "cert") {
|
|
|
|
|
ctx = ssl_client_secure_ctx_.get();
|
|
|
|
|
} else if (cert == "nocert") {
|
|
|
|
|
ctx = ssl_client_insecure_ctx_.get();
|
2022-01-11 13:59:13 -05:00
|
|
|
} else {
|
2022-03-18 16:25:20 -04:00
|
|
|
recorder_.drv_notify_close(chid, std::string("invalid cert rule: ") + target);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
SOCKET sock = open_connection(host.c_str(), port.c_str(), err);
|
|
|
|
|
if (sock == INVALID_SOCKET)
|
|
|
|
|
{
|
|
|
|
|
recorder_.drv_notify_close(chid, err);
|
|
|
|
|
continue;
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
// std::cerr << "Opening channel " << chid << std::endl;
|
|
|
|
|
make_channel(sock, chid, ctx, CHAN_SSL_CONNECTING);
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
if (!chans.empty())
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_clear_new_outgoing();
|
2022-02-17 20:02:08 -05:00
|
|
|
}
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void accept_connection(int port, SOCKET sock)
|
|
|
|
|
{
|
2022-02-25 19:57:23 -05:00
|
|
|
std::string err;
|
2022-01-11 13:59:13 -05:00
|
|
|
SOCKET socket = accept_on_socket(sock, err);
|
|
|
|
|
if_error_print_and_exit(err);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (socket != INVALID_SOCKET)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
int chid = recorder_.drv_notify_accept(port);
|
2022-01-11 13:59:13 -05:00
|
|
|
// std::cerr << "Accepted channel " << chid << std::endl;
|
2022-03-18 16:25:20 -04:00
|
|
|
make_channel(socket, chid, ssl_server_ctx_.get(), CHAN_SSL_ACCEPTING);
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void advance_plaintext(ChanInfo &chan)
|
|
|
|
|
{
|
2022-02-25 19:57:23 -05:00
|
|
|
std::string err;
|
2022-01-11 13:59:13 -05:00
|
|
|
|
|
|
|
|
// If the channel has no outgoing bytes and has been released,
|
|
|
|
|
// just close it.
|
2022-03-18 16:25:20 -04:00
|
|
|
if (chan.released)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
close_channel(chan, "");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to write plaintext to the channel.
|
2022-03-11 18:35:51 -05:00
|
|
|
std::string_view s = recorder_.drv_peek_outgoing(chan.chid);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (s.size() > 0)
|
|
|
|
|
{
|
2022-03-04 16:45:47 -05:00
|
|
|
int sbytes = s.size();
|
2022-03-18 16:25:20 -04:00
|
|
|
if (sbytes > 65536)
|
|
|
|
|
sbytes = 65536;
|
2022-03-04 16:45:47 -05:00
|
|
|
int wbytes = socket_send(chan.socket, s.data(), sbytes, err);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (wbytes < 0)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
close_channel(chan, err);
|
2022-03-18 16:25:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_sent_outgoing(chan.chid, wbytes);
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to read plaintext from the channel.
|
|
|
|
|
// Someday, find a way to avoid this copy.
|
2022-03-11 18:35:51 -05:00
|
|
|
int nrecv = socket_recv(chan.socket, chbuf_.get(), 65536, err);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (nrecv < 0)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
close_channel(chan, err);
|
2022-03-18 16:25:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_recv_incoming(chan.chid, std::string_view(chbuf_.get(), nrecv));
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the ready-flags for next time.
|
|
|
|
|
chan.ready_on_outgoing = true;
|
|
|
|
|
chan.ready_on_pollin = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void process_ssl_error(ChanInfo &chan, int retval)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
int error = SSL_get_error(chan.ssl, retval);
|
|
|
|
|
// std::cerr << "SSL error code = " << error << " ";
|
2022-03-18 16:25:20 -04:00
|
|
|
if (error == SSL_ERROR_WANT_READ)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
chan.ready_on_pollin = true;
|
2022-03-18 16:25:20 -04:00
|
|
|
}
|
|
|
|
|
else if (error == SSL_ERROR_WANT_WRITE)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
chan.ready_on_pollout = true;
|
2022-03-18 16:25:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
close_channel(chan, ssl_errors_string());
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void advance_ssl_connecting(ChanInfo &chan)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
// std::cerr << "In advance_ssl_connecting" << std::endl;
|
|
|
|
|
int retval = SSL_connect(chan.ssl);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (retval == 1)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
// Connection successful.
|
|
|
|
|
chan.state = CHAN_SSL_READWRITE;
|
|
|
|
|
chan.ready_now = true;
|
2022-03-18 16:25:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
// std::cerr << "ssl_connect_error";
|
|
|
|
|
process_ssl_error(chan, retval);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
|
|
|
|
|
void advance_ssl_accepting(ChanInfo &chan)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
// std::cerr << "In advance_ssl_accepting" << std::endl;
|
|
|
|
|
int retval = SSL_accept(chan.ssl);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (retval == 1)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
// Connection successful.
|
|
|
|
|
chan.state = CHAN_SSL_READWRITE;
|
|
|
|
|
chan.ready_now = true;
|
2022-03-18 16:25:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
process_ssl_error(chan, retval);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
|
|
|
|
|
void advance_ssl_readwrite(ChanInfo &chan)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
// std::cerr << "In advance_ssl_readwrite" << std::endl;
|
|
|
|
|
// Try to read data.
|
2022-03-11 18:35:51 -05:00
|
|
|
int read_result = SSL_read(chan.ssl, chbuf_.get(), 65536);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (read_result > 0)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_recv_incoming(chan.chid, std::string_view(chbuf_.get(), read_result));
|
2022-01-11 13:59:13 -05:00
|
|
|
chan.ready_now = true;
|
2022-03-18 16:25:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
process_ssl_error(chan, read_result);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (chan.state == CHAN_INACTIVE)
|
|
|
|
|
return;
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to write data.
|
|
|
|
|
int wbytes;
|
2022-03-18 16:25:20 -04:00
|
|
|
if (chan.last_write_nbytes > 0)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
wbytes = chan.last_write_nbytes;
|
|
|
|
|
assert(wbytes < chan.nbytes);
|
2022-03-18 16:25:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
wbytes = chan.nbytes;
|
2022-03-18 16:25:20 -04:00
|
|
|
if (wbytes > 65536)
|
|
|
|
|
wbytes = 65536;
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
if (wbytes > 0)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
int write_result = SSL_write(chan.ssl, chan.bytes, wbytes);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (write_result > 0)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_sent_outgoing(chan.chid, write_result);
|
2022-01-11 13:59:13 -05:00
|
|
|
chan.last_write_nbytes = 0;
|
|
|
|
|
chan.ready_on_outgoing = true;
|
2022-03-18 16:25:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
chan.last_write_nbytes = wbytes;
|
|
|
|
|
process_ssl_error(chan, write_result);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (chan.state == CHAN_INACTIVE)
|
|
|
|
|
return;
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
chan.ready_on_outgoing = true;
|
|
|
|
|
}
|
|
|
|
|
// std::cerr << "rpi=" << chan.ready_on_pollin << ".rpo=" << chan.ready_on_pollout << ".rn=" << chan.ready_now << ".rog=" << chan.ready_on_outgoing << " ";
|
|
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
|
|
|
|
|
void advance_channel(ChanInfo &chan)
|
|
|
|
|
{
|
|
|
|
|
assert_ssl_errors_empty();
|
|
|
|
|
switch (chan.state)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
case CHAN_PLAINTEXT:
|
|
|
|
|
advance_plaintext(chan);
|
|
|
|
|
break;
|
|
|
|
|
case CHAN_SSL_CONNECTING:
|
|
|
|
|
advance_ssl_connecting(chan);
|
|
|
|
|
break;
|
|
|
|
|
case CHAN_SSL_ACCEPTING:
|
|
|
|
|
advance_ssl_accepting(chan);
|
|
|
|
|
break;
|
|
|
|
|
case CHAN_SSL_READWRITE:
|
|
|
|
|
advance_ssl_readwrite(chan);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
assert_ssl_errors_empty();
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
void handle_socket_input_output()
|
|
|
|
|
{
|
2022-02-25 19:57:23 -05:00
|
|
|
std::string err;
|
2022-02-17 20:02:08 -05:00
|
|
|
int mstimeout = read_console_recently_ ? 100 : 1000;
|
2022-01-11 13:59:13 -05:00
|
|
|
|
|
|
|
|
// Peek output buffers and determine channel release flags.
|
2022-03-18 16:25:20 -04:00
|
|
|
for (ChanInfo &chan : chans_)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
std::string_view s = recorder_.drv_peek_outgoing(chan.chid);
|
2022-03-04 16:45:47 -05:00
|
|
|
chan.nbytes = s.size();
|
|
|
|
|
chan.bytes = s.data();
|
2022-01-11 13:59:13 -05:00
|
|
|
chan.just_released = false;
|
2022-03-18 16:25:20 -04:00
|
|
|
if ((chan.nbytes == 0) && (!chan.released))
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
chan.released = recorder_.drv_get_channel_released(chan.chid);
|
2022-01-11 13:59:13 -05:00
|
|
|
chan.just_released = chan.released;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
|
2022-01-12 15:53:49 -05:00
|
|
|
// Construct the struct pollfd vector.
|
2022-02-17 20:02:08 -05:00
|
|
|
int pollsize = 0;
|
2022-03-18 16:25:20 -04:00
|
|
|
for (const auto &p : listen_sockets_)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
struct pollfd &pfd = pollvec_[pollsize++];
|
2022-01-11 13:59:13 -05:00
|
|
|
pfd.fd = p.second;
|
|
|
|
|
pfd.events = POLLIN;
|
2022-01-12 17:23:02 -05:00
|
|
|
pfd.revents = 0;
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
for (const ChanInfo &chan : chans_)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
struct pollfd &pfd = pollvec_[pollsize++];
|
2022-01-11 13:59:13 -05:00
|
|
|
assert(chan.socket != INVALID_SOCKET);
|
|
|
|
|
pfd.fd = chan.socket;
|
2022-01-12 17:23:02 -05:00
|
|
|
pfd.events = 0;
|
|
|
|
|
pfd.revents = 0;
|
2022-03-18 16:25:20 -04:00
|
|
|
if (chan.ready_now)
|
|
|
|
|
mstimeout = 0;
|
|
|
|
|
if (chan.just_released)
|
|
|
|
|
mstimeout = 0;
|
|
|
|
|
if (chan.ready_on_pollin)
|
|
|
|
|
pfd.events |= POLLIN;
|
|
|
|
|
if (chan.ready_on_pollout)
|
|
|
|
|
pfd.events |= POLLOUT;
|
|
|
|
|
if (chan.ready_on_outgoing && (chan.nbytes > 0))
|
|
|
|
|
pfd.events |= POLLOUT;
|
2022-01-11 13:59:13 -05:00
|
|
|
// std::cerr << "evt=" << pfd.events << ".nb=" << chan.nbytes << " ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do the poll.
|
2022-03-11 18:35:51 -05:00
|
|
|
socket_poll(pollvec_.get(), pollsize, mstimeout, err);
|
2022-01-12 15:53:49 -05:00
|
|
|
if_error_print_and_exit(err);
|
2022-03-18 16:25:20 -04:00
|
|
|
|
2022-01-11 13:59:13 -05:00
|
|
|
// Check listening sockets.
|
2022-02-17 20:02:08 -05:00
|
|
|
int index = 0;
|
2022-03-18 16:25:20 -04:00
|
|
|
for (auto &p : listen_sockets_)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
struct pollfd &pfd = pollvec_[index++];
|
2022-03-18 16:25:20 -04:00
|
|
|
if (pfd.revents & (POLLIN | POLLERR))
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
accept_connection(p.first, p.second);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Advance channels where possible.
|
2022-03-18 16:25:20 -04:00
|
|
|
for (ChanInfo &chan : chans_)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
struct pollfd &pfd = pollvec_[index++];
|
2022-01-11 13:59:13 -05:00
|
|
|
bool pollin = ((pfd.revents & POLLIN) != 0);
|
|
|
|
|
bool pollout = ((pfd.revents & POLLOUT) != 0);
|
2022-01-12 17:23:02 -05:00
|
|
|
bool pollerr = ((pfd.revents & (POLLERR | POLLHUP)) != 0);
|
2022-01-11 13:59:13 -05:00
|
|
|
if (chan.ready_now || pollerr || chan.just_released ||
|
|
|
|
|
(chan.ready_on_pollin && pollin) ||
|
|
|
|
|
(chan.ready_on_pollout && pollout) ||
|
2022-03-18 16:25:20 -04:00
|
|
|
(chan.ready_on_outgoing && (chan.nbytes > 0) && pollout))
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
chan.ready_now = false;
|
|
|
|
|
chan.ready_on_pollin = false;
|
|
|
|
|
chan.ready_on_pollout = false;
|
|
|
|
|
chan.ready_on_outgoing = false;
|
|
|
|
|
advance_channel(chan);
|
|
|
|
|
}
|
|
|
|
|
chan.nbytes = 0;
|
|
|
|
|
chan.bytes = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete any newly-inactive channels
|
|
|
|
|
cleanup_channels();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
int replay_logfile(const char *fn, bool verbose)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
drv::ReplayPlayer player;
|
|
|
|
|
player.open_logfile(fn);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (verbose)
|
|
|
|
|
{
|
2022-03-11 19:11:09 -05:00
|
|
|
player.enable_stdout();
|
|
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
while (true)
|
|
|
|
|
{
|
2022-03-11 19:01:02 -05:00
|
|
|
drv::ReplayPlayer::Status st = player.step();
|
2022-03-18 16:25:20 -04:00
|
|
|
if (st != drv::ReplayPlayer::ST_REPLAYING)
|
|
|
|
|
{
|
2022-03-11 19:01:02 -05:00
|
|
|
player.print_status(std::cerr);
|
|
|
|
|
return (st == drv::ReplayPlayer::ST_CLEAN_EXIT) ? 0 : 1;
|
2022-03-11 18:35:51 -05:00
|
|
|
}
|
2022-03-04 14:05:24 -05:00
|
|
|
}
|
2022-03-11 18:35:51 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
int drive(int argc, char *argv[])
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
// Remove the program name from argv.
|
2022-03-18 16:25:20 -04:00
|
|
|
if (argc < 1)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
DrivenEngine::print_usage(std::cerr, "<unknown>");
|
2022-03-04 14:05:24 -05:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2022-03-11 18:35:51 -05:00
|
|
|
std::string program = argv[0];
|
2022-03-18 16:25:20 -04:00
|
|
|
argc -= 1;
|
|
|
|
|
argv += 1;
|
2022-03-11 18:35:51 -05:00
|
|
|
|
|
|
|
|
// If argv contains "replay <filename>", do a replay,
|
|
|
|
|
// and then skip everything else.
|
2022-03-18 16:25:20 -04:00
|
|
|
if (argc >= 1)
|
|
|
|
|
{
|
2022-03-11 19:11:09 -05:00
|
|
|
std::string cmd(argv[0]);
|
2022-03-18 16:25:20 -04:00
|
|
|
if ((cmd == "replay") || (cmd == "vreplay"))
|
|
|
|
|
{
|
|
|
|
|
if (argc != 2)
|
|
|
|
|
{
|
2022-03-11 19:11:09 -05:00
|
|
|
std::cerr << "usage: " << program << " replay <filename>" << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return replay_logfile(argv[1], cmd == "vreplay");
|
2022-03-11 18:35:51 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If argv contains "record <filename>", start recording,
|
|
|
|
|
// and remove the "record <filename>" from argv.
|
2022-03-18 16:25:20 -04:00
|
|
|
if (argc >= 1)
|
|
|
|
|
{
|
2022-03-11 19:11:09 -05:00
|
|
|
std::string cmd = argv[0];
|
2022-03-18 16:25:20 -04:00
|
|
|
if (cmd == "record")
|
|
|
|
|
{
|
|
|
|
|
if (argc < 2)
|
|
|
|
|
{
|
2022-03-11 19:11:09 -05:00
|
|
|
DrivenEngine::print_usage(std::cerr, program);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
bool ok = recorder_.open_logfile(argv[1]);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (!ok)
|
|
|
|
|
{
|
2022-03-11 19:11:09 -05:00
|
|
|
std::cerr << "Could not open logfile: " << argv[1] << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2022-03-18 16:25:20 -04:00
|
|
|
argc -= 2;
|
|
|
|
|
argv += 2;
|
2022-03-11 18:35:51 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the engine.
|
2022-03-18 16:25:20 -04:00
|
|
|
if (argc < 1)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
DrivenEngine::print_usage(std::cerr, program);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
bool engine_made = recorder_.create_engine(argv[0]);
|
2022-03-18 16:25:20 -04:00
|
|
|
if (!engine_made)
|
|
|
|
|
{
|
2022-03-11 18:35:51 -05:00
|
|
|
DrivenEngine::print_usage(std::cerr, program);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2022-03-04 14:05:24 -05:00
|
|
|
|
2022-01-11 13:59:13 -05:00
|
|
|
read_console_recently_ = false;
|
|
|
|
|
|
2022-03-11 18:35:51 -05:00
|
|
|
chbuf_.reset(new char[CHBUF_SIZE]);
|
|
|
|
|
pollvec_.reset(new struct pollfd[POLLVEC_SIZE]);
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
ssl_server_ctx_.reset(new_ssl_server_context());
|
|
|
|
|
ssl_client_secure_ctx_.reset(new_ssl_client_context(SSL_VERIFY_PEER));
|
|
|
|
|
ssl_client_insecure_ctx_.reset(new_ssl_client_context(SSL_VERIFY_NONE));
|
|
|
|
|
assert_ssl_errors_empty();
|
2022-01-11 13:59:13 -05:00
|
|
|
|
2022-02-18 03:59:21 -05:00
|
|
|
handle_lua_source();
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_invoke_event_init(argc, argv);
|
2022-01-11 13:59:13 -05:00
|
|
|
handle_listen_ports();
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
while (!recorder_.drv_get_stop_driver())
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
handle_lua_source();
|
|
|
|
|
handle_console_output();
|
|
|
|
|
handle_new_outgoing_sockets();
|
|
|
|
|
handle_socket_input_output();
|
|
|
|
|
handle_console_input();
|
|
|
|
|
handle_console_output();
|
2022-03-11 18:35:51 -05:00
|
|
|
recorder_.drv_invoke_event_update(monoclock.get());
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 16:25:20 -04:00
|
|
|
for (ChanInfo &chan : chans_)
|
|
|
|
|
{
|
2022-01-11 13:59:13 -05:00
|
|
|
close_channel(chan, "");
|
|
|
|
|
}
|
2022-03-11 19:01:02 -05:00
|
|
|
|
2022-03-12 14:56:33 -05:00
|
|
|
DrivenEngine::set(nullptr);
|
2022-03-11 19:01:02 -05:00
|
|
|
recorder_.clean_exit();
|
2022-03-11 18:35:51 -05:00
|
|
|
return 0;
|
2022-01-11 13:59:13 -05:00
|
|
|
}
|
|
|
|
|
};
|