More work on removing old determinism-in-driver code

This commit is contained in:
2022-02-21 20:23:01 -05:00
parent 19b6951e0f
commit 6a9bf4ca42
5 changed files with 72 additions and 70 deletions

View File

@@ -27,6 +27,7 @@
#include <sys/types.h>
#include <sys/personality.h>
#include <netdb.h>
#include <malloc.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
@@ -36,18 +37,12 @@
#include <openssl/pem.h>
#include <openssl/conf.h>
#define OPENSSL_HEAP_SIZE (4*1024*1024)
#define CHBUF_SIZE (256*1024)
#define POLLVEC_SIZE (DrivenEngine::MAX_CHAN+1)
using SOCKET=int;
const int INVALID_SOCKET = -1;
struct termios orig_termios;
static std::unique_ptr<char[]> chbuf;
static std::unique_ptr<struct pollfd[]> pollvec;
static UmmString strerror_str(int err) {
static std::string strerror_str(int err) {
char errbuf[256];
return strerror_r(errno, errbuf, 256);
}
@@ -77,7 +72,7 @@ static void enable_tty_raw() {
assert(status >= 0);
}
static SOCKET open_connection(std::string_view target, UmmString &err) {
static SOCKET open_connection(std::string_view target, std::string &err) {
struct addrinfo *addrs = nullptr;
struct addrinfo *goodaddr = nullptr;
struct addrinfo hints;
@@ -90,7 +85,7 @@ static SOCKET open_connection(std::string_view target, UmmString &err) {
hints.ai_flags = AI_NUMERICSERV;
err.clear();
UmmString host, port;
std::string host, port;
drv::split_host_port(target, host, port);
int status = getaddrinfo(host.c_str(), port.c_str(), &hints, &addrs);
if (status != 0) {
@@ -123,7 +118,7 @@ error_general:
return INVALID_SOCKET;
}
static SOCKET listen_on_port(int port, UmmString &err) {
static SOCKET listen_on_port(int port, std::string &err) {
int status, enable;
err.clear();
@@ -154,7 +149,7 @@ error_errno:
return INVALID_SOCKET;
}
static SOCKET accept_on_socket(SOCKET listen_socket, UmmString &err) {
static SOCKET accept_on_socket(SOCKET listen_socket, std::string &err) {
err.clear();
SOCKET chsock = accept(listen_socket, nullptr, nullptr);
if (chsock >= 0) {
@@ -174,7 +169,7 @@ static SOCKET accept_on_socket(SOCKET listen_socket, UmmString &err) {
// zero: would block
// negative: channel closed, possibly cleanly or possibly with error
//
static int socket_send(SOCKET socket, const char *bytes, int nbytes, UmmString &err) {
static int socket_send(SOCKET socket, const char *bytes, int nbytes, std::string &err) {
err.clear();
int wbytes = send(socket, bytes, nbytes, 0);
if (wbytes < 0) {
@@ -189,7 +184,7 @@ static int socket_send(SOCKET socket, const char *bytes, int nbytes, UmmString &
}
}
static int socket_recv(SOCKET socket, char *bytes, int nbytes, UmmString &err) {
static int socket_recv(SOCKET socket, char *bytes, int nbytes, std::string &err) {
err.clear();
int nrecv = recv(socket, bytes, nbytes, 0);
if (nrecv < 0) {
@@ -210,7 +205,7 @@ static int socket_close(SOCKET socket) {
return close(socket);
}
static int socket_poll(struct pollfd *pollvec, int pollcount, int mstimeout, UmmString &err) {
static int socket_poll(struct pollfd *pollvec, int pollcount, int mstimeout, std::string &err) {
// socket_poll is implicitly expected to also poll stdin,
// if the OS allows that. Linux does, so we add stdin to the
// poll vector. The poll vector is required to have at
@@ -236,25 +231,6 @@ static int console_read(char *bytes, int nbytes) {
return read(0, bytes, nbytes);
}
static std::string_view read_file(const char *fn, char *buf, int bufsize, UmmString &err) {
int nread;
int fd = open(fn, O_RDONLY);
if (fd < 0) goto error_errno;
nread = read(fd, buf, bufsize);
if (nread < 0) goto error_errno;
if (nread == bufsize) {
err = "file too large";
goto error;
}
buf[nread] = 0;
err = "";
return std::string_view(buf, nread);
error_errno:
err = strerror_str(errno);
error:
buf[0] = 0;
return std::string_view(buf, 0);
}
static void disable_randomization(int argc, char *argv[]) {
const int old_personality = personality(ADDR_NO_RANDOMIZE);
@@ -290,10 +266,10 @@ public:
int main(int argc, char **argv)
{
mallopt(M_MMAP_MAX, 0); // Keep malloc in the 'brk' area.
disable_randomization(argc, argv);
allocate_buffers();
enable_tty_raw();
chbuf.reset(new char[CHBUF_SIZE]);
pollvec.reset(new struct pollfd[POLLVEC_SIZE]);
OPENSSL_init_ssl(0, NULL);
initialize_engine();
driver_drive(argc, argv);