Release v0.3.0
Some checks failed
Docker Build & Publish / build (push) Has been cancelled

This commit is contained in:
Nicholas Orlowsky 2025-02-24 19:29:43 -05:00
parent c07f3ebf81
commit 9b5719f9be
17 changed files with 612 additions and 308 deletions

View file

@ -6,6 +6,7 @@
#include <iostream>
#include <malloc.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <string>
@ -16,85 +17,101 @@
namespace anthracite::socket {
SSL_CTX* openssl_socket::_context = nullptr;
openssl_socket::openssl_socket(int port, int max_queue)
: anthracite_socket(port, max_queue)
openssl_listener::openssl_listener(std::string& key_path, std::string& cert_path, int port, int max_queue, bool nonblocking)
: listener(port, max_queue, nonblocking)
{
const SSL_METHOD* method = TLS_server_method();
if (_context == nullptr) {
_context = SSL_CTX_new(method);
}
_context = SSL_CTX_new(method);
if (!_context) {
log::err << "Unable to initialize SSL" << std::endl;
throw std::exception();
}
if (SSL_CTX_use_certificate_file(_context, "cert.pem", SSL_FILETYPE_PEM) <= 0) {
log::err << "Unable to open cert.pem" << std::endl;
if (SSL_CTX_use_certificate_file(_context, cert_path.c_str(), SSL_FILETYPE_PEM) <= 0) {
log::err << "Unable to open Cert file at: " << cert_path << std::endl;
throw std::exception();
}
if (SSL_CTX_use_PrivateKey_file(_context, "key.pem", SSL_FILETYPE_PEM) <= 0) {
log::err << "Unable to open key.pem" << std::endl;
if (SSL_CTX_use_PrivateKey_file(_context, key_path.c_str(), SSL_FILETYPE_PEM) <= 0) {
log::err << "Unable to open Key file at: " << key_path << std::endl;
throw std::exception();
}
}
openssl_socket::~openssl_socket() = default;
bool openssl_socket::wait_for_conn()
bool openssl_listener::wait_for_conn(server** client_sock_p)
{
client_ip = "";
struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
fd_set read_fd;
FD_ZERO(&read_fd);
FD_SET(server_socket, &read_fd);
if (select(server_socket + 1, &read_fd, NULL, NULL, &wait_timeout)) {
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
std::array<char, INET_ADDRSTRLEN> ip_str { 0 };
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN);
client_ip = std::string(ip_str.data());
_ssl = SSL_new(_context);
SSL_set_fd(_ssl, client_socket);
if (SSL_accept(_ssl) <= 0) {
log::warn << "Unable to open SSL connection with client" << std::endl;
client_ip = "";
close(client_socket);
client_socket = -1;
struct sockaddr_in client_addr {};
socklen_t client_addr_len;
int csock = accept(_sock_fd, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
if (csock > 0) {
std::array<char, INET6_ADDRSTRLEN> ip_str { 0 };
if (inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN) == NULL) {
if (inet_ntop(AF_INET6, &client_addr.sin_addr, ip_str.data(), INET6_ADDRSTRLEN) == NULL) {
log::warn << "Unable to decode client's IP address" << std::endl;
}
}
SSL* ssl = SSL_new(_context);
if (ssl == NULL) {
for (int i = 0; i < 5 && close(csock) != 0; ++i)
;
return false;
}
if (SSL_set_fd(ssl, csock) == 0) {
SSL_free(ssl);
for (int i = 0; i < 5 && close(csock) != 0; ++i)
;
return false;
}
if (SSL_accept(ssl) <= 0) {
log::warn << "Unable to open SSL connection with client" << std::endl;
SSL_free(ssl);
for (int i = 0; i < 5 && close(csock) != 0; ++i)
;
return false;
}
std::string client_ip = std::string(ip_str.data());
*client_sock_p = new openssl_server(csock, client_ip, _nonblocking, ssl);
return true;
} else {
return false;
}
}
void openssl_socket::close_conn()
openssl_listener::~openssl_listener() {}
openssl_server::openssl_server(int sock_fd, std::string client_ip, bool nonblocking, SSL* ssl)
: server(sock_fd, client_ip, nonblocking)
, _ssl(ssl)
{
}
openssl_server::~openssl_server()
{
SSL_shutdown(_ssl);
SSL_free(_ssl);
close(client_socket);
client_socket = -1;
}
void openssl_socket::send_message(std::string& msg)
void openssl_server::send_message(const std::string& msg)
{
if (client_socket == -1) {
return;
}
SSL_write(_ssl, &msg[0], msg.length());
}
std::string openssl_socket::recv_message(int buffer_size)
std::string openssl_server::recv_message(int buffer_size)
{
if (client_socket == -1) {
return "";
}
// Ignored because it's nonfatal, just slower
int nodelay_opt = 1;
(void)setsockopt(_sock_fd, SOL_TCP, TCP_NODELAY, &nodelay_opt, sizeof(nodelay_opt));
setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout_tv, sizeof timeout_tv);
std::vector<char> response(buffer_size + 1);
ssize_t result = SSL_read(_ssl, response.data(), buffer_size + 1);

View file

@ -5,18 +5,25 @@
#include <openssl/err.h>
namespace anthracite::socket {
class openssl_socket : public anthracite_socket {
class openssl_server : public server{
private:
static SSL_CTX* _context;
SSL* _ssl;
public:
openssl_socket(int port, int max_queue = MAX_QUEUE_LENGTH);
~openssl_socket();
openssl_server(int sock_fd, std::string client_ip, bool nonblocking, SSL* ssl);
~openssl_server();
bool wait_for_conn() override;
void close_conn() override;
void send_message(std::string& msg) override;
void send_message(const std::string& msg) override;
std::string recv_message(int buffer_size) override;
};
class openssl_listener : public listener {
private:
SSL_CTX* _context;
public:
openssl_listener(std::string& key_path, std::string& cert_path, int port, int max_queue_length, bool nonblocking);
~openssl_listener();
bool wait_for_conn(server** client_sock_) override;
};
};

View file

@ -1,98 +1,120 @@
#include "./socket.hpp"
#include "../log/log.hpp"
#include "assert.h"
#include <arpa/inet.h>
#include <array>
#include <exception>
#include <fcntl.h>
#include <iostream>
#include <malloc.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <vector>
#include <unistd.h>
#include <fcntl.h>
#include "assert.h"
#include <netinet/in.h>
#include <netinet/tcp.h>
namespace anthracite::socket {
const struct timeval anthracite_socket::timeout_tv = { .tv_sec = 5, .tv_usec = 0 };
anthracite_socket::anthracite_socket(int port, int max_queue, bool nonblocking)
: server_socket(::socket(AF_INET, SOCK_STREAM, 0))
, client_ip("")
, _nonblocking(nonblocking)
socket::socket(bool nonblocking)
: _nonblocking(nonblocking)
{
}
listener::listener(int port, int max_queue, bool nonblocking)
: socket(nonblocking)
, _port(port)
{
_sock_fd = ::socket(AF_INET, SOCK_STREAM, 0);
if (_sock_fd == -1) {
log::err << "Listener was unable to open a socket" << std::endl;
throw std::exception();
}
struct sockaddr_in address {};
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_port = htons(_port);
address.sin_addr.s_addr = INADDR_ANY;
int reuse_opt = 1;
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &reuse_opt, sizeof(reuse_opt));
bind(server_socket, reinterpret_cast<struct sockaddr*>(&address), sizeof(address));
if (_nonblocking) {
fcntl(server_socket, F_SETFL, O_NONBLOCK);
if (setsockopt(_sock_fd, SOL_SOCKET, SO_REUSEADDR, &reuse_opt, sizeof(reuse_opt)) < 0) {
log::err << "Listener was unable to set SO_REUSEADDR" << std::endl;
throw std::exception();
}
listen(server_socket, max_queue);
if (bind(_sock_fd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) != 0) {
log::err << "Listener was unable to bind to address" << std::endl;
throw std::exception();
}
if (fcntl(_sock_fd, F_SETFL, O_NONBLOCK) == -1) {
log::err << "Listener was unable to fcntl(O_NONBLOCK)" << std::endl;
throw std::exception();
}
if (listen(_sock_fd, max_queue) == -1) {
log::err << "Listener was unable to begin listening" << std::endl;
throw std::exception();
}
}
bool anthracite_socket::wait_for_conn()
bool listener::wait_for_conn(server** client_sock_p)
{
client_ip = "";
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
if (client_socket > 0) {
if (_nonblocking) {
fcntl(client_socket, F_SETFL, O_NONBLOCK);
struct sockaddr_in client_addr {};
socklen_t client_addr_len;
int csock = accept(_sock_fd, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
if (csock > 0) {
std::array<char, INET6_ADDRSTRLEN> ip_str { 0 };
if (inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN) == NULL) {
if (inet_ntop(AF_INET6, &client_addr.sin_addr, ip_str.data(), INET6_ADDRSTRLEN) == NULL) {
log::warn << "Unable to decode client's IP address" << std::endl;
}
}
std::array<char, INET_ADDRSTRLEN> ip_str { 0 };
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN);
client_ip = std::string(ip_str.data());
std::string client_ip = std::string(ip_str.data());
*client_sock_p = new server(csock, client_ip, _nonblocking);
return true;
} else {
return false;
}
}
const std::string& anthracite_socket::get_client_ip()
server::server(int sock_fd, std::string client_ip, bool nonblocking)
: _sock_fd(sock_fd)
, _client_ip(std::move(client_ip))
, socket(nonblocking)
{
return client_ip;
}
void anthracite_socket::close_conn() {
close(client_socket);
client_socket = -1;
}
void anthracite_socket::send_message(std::string& msg)
{
if (client_socket == -1) {
return;
if (_nonblocking) {
if (fcntl(_sock_fd, F_SETFL, O_NONBLOCK) == -1) {
log::err << "Server was unable to fcntl(O_NONBLOCK)" << std::endl;
throw std::exception();
}
}
send(client_socket, &msg[0], msg.length(), 0);
}
bool anthracite_socket::has_client() {
return client_socket > 0;
}
std::string anthracite_socket::recv_message(int buffer_size)
void server::send_message(const std::string& msg)
{
if (client_socket == -1) {
return "";
}
//setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout_tv, sizeof timeout_tv);
// Ignored because if we fail to send, it probably means
// a HUP will occur and it'll be closed. TODO: Just close
// it here and add a return value
(void)send(_sock_fd, &msg[0], msg.length(), 0);
}
std::string server::recv_message(int buffer_size)
{
// Ignored because it's nonfatal, just slower
int nodelay_opt = 1;
assert(setsockopt(client_socket, SOL_TCP, TCP_NODELAY, &nodelay_opt, sizeof(nodelay_opt)) == 0);
(void)setsockopt(_sock_fd, SOL_TCP, TCP_NODELAY, &nodelay_opt, sizeof(nodelay_opt));
std::vector<char> response(buffer_size + 1);
ssize_t result = recv(client_socket, response.data(), buffer_size + 1, 0);
ssize_t result = recv(_sock_fd, response.data(), buffer_size + 1, 0);
if (result < 1) {
return "";
@ -102,4 +124,21 @@ std::string anthracite_socket::recv_message(int buffer_size)
return { response.data() };
}
server::~server()
{
for (int i = 0; i < 5 && close(_sock_fd) != 0; ++i)
;
}
listener::~listener()
{
for (int i = 0; i < 5 && close(_sock_fd) != 0; ++i)
;
}
const std::string& server::client_ip()
{
return _client_ip;
}
};

View file

@ -10,32 +10,42 @@
namespace anthracite::socket {
class socket {
protected:
bool _nonblocking;
socket(bool nonblocking);
public:
socket(){}
virtual ~socket(){}
};
class anthracite_socket {
class server : public socket {
protected:
int _sock_fd;
std::string _client_ip;
public:
server(int sock_fd, std::string client_ip, bool nonblocking);
~server();
protected:
bool _nonblocking;
struct timeval wait_timeout = { .tv_sec = 1, .tv_usec = 0};
int server_socket;
int client_socket {};
std::string client_ip;
struct sockaddr_in client_addr {};
socklen_t client_addr_len {};
static const struct timeval timeout_tv;
static const int MAX_QUEUE_LENGTH = 100;
virtual void send_message(const std::string& msg);
virtual std::string recv_message(int buffer_size);
const std::string& client_ip();
public:
anthracite_socket(int port, int max_queue = MAX_QUEUE_LENGTH, bool nonblocking = false);
int fd() { return _sock_fd; }
};
virtual const std::string& get_client_ip() final;
class listener : public socket {
protected:
uint16_t _port;
int _sock_fd;
public:
listener(int port, int max_queue_length, bool nonblocking);
~listener();
virtual bool has_client();
virtual bool wait_for_conn();
virtual void close_conn();
virtual void send_message(std::string& msg);
virtual std::string recv_message(int buffer_size);
virtual bool wait_for_conn(server** client_sock_p);
int csock() { return client_socket; }
int fd() { return _sock_fd; }
int port() { return _port; }
};
};