From f1600194c1ca506f419bdfa780ba6e8c791f7477 Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Sat, 23 Nov 2024 15:38:48 +0000 Subject: [PATCH] [Add] Added the SSL plugin. Can't be certain if it even works yet though. --- Makefile | 3 +- base/Makefile | 1 + base/config/api.h | 17 ++++++- base/network_plain/Makefile | 8 ++++ base/{ => network_plain}/network.c | 4 +- base/{ => network_plain}/network.h | 0 base/network_ssl/Makefile | 6 +++ base/network_ssl/network.c | 73 +++++++++++++++++++++++++++++ base/network_ssl/network.h | 16 +++++++ bin/base/network_plain.so | Bin 0 -> 16240 bytes 10 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 base/network_plain/Makefile rename base/{ => network_plain}/network.c (94%) rename base/{ => network_plain}/network.h (100%) create mode 100644 base/network_ssl/network.c create mode 100644 base/network_ssl/network.h create mode 100755 bin/base/network_plain.so diff --git a/Makefile b/Makefile index a9ce293..1dfe0a0 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,8 @@ default: mkdir -p bin mkdir -p bin/base - cd base && make clean + cd base && make + clean: rm -Rf bin/ cd base && make clean diff --git a/base/Makefile b/base/Makefile index d39f963..22f9ee8 100644 --- a/base/Makefile +++ b/base/Makefile @@ -1,6 +1,7 @@ default: cd network_plain && make cd network_ssl && make + clean: cd network_plain && make clean cd network_ssl && make clean diff --git a/base/config/api.h b/base/config/api.h index fb2c56f..ed13655 100644 --- a/base/config/api.h +++ b/base/config/api.h @@ -1,5 +1,20 @@ #pragma once -#define PLUGIN_TYPE_NETWOR 0x1 +#define PLUGIN_TYPE_NETWORK 0x1 void configErrorPush(const char* err); + +/* This is the common API for plugins. */ +unsigned int pluginType(); +const char* pluginName(); + +#if 0 +#ifndef __NETWORK_H__ + /* This one's for network plugins. */ + void* pluginConnect(const char* host, int port); + int pluginSocketGet(void* connection); + int pluginReadData(void* connection, char* buffer, int buffer_len); + int pluginSendData(void* connection, const char* buffer, int buffer_len); + void* pluginSocketDone(void* connection); +#endif +#endif diff --git a/base/network_plain/Makefile b/base/network_plain/Makefile new file mode 100644 index 0000000..345ccb1 --- /dev/null +++ b/base/network_plain/Makefile @@ -0,0 +1,8 @@ +SRC = network.c +OUTPUT = network_plain.so +CFLAGS += -fPIC -shared -Wl,-soname,network_plain.so -O3 -Wall -I../ + +default: + $(CC) $(CFLAGS) $(SRC) -o ../../bin/base/$(OUTPUT) $(LDFLAGS) + +clean: diff --git a/base/network.c b/base/network_plain/network.c similarity index 94% rename from base/network.c rename to base/network_plain/network.c index 6995240..b729d7b 100644 --- a/base/network.c +++ b/base/network_plain/network.c @@ -6,7 +6,7 @@ unsigned int pluginType() { } const char* pluginName() { - return* "plain"; + return "plain"; } void* pluginConnect(const char* host, int port) { @@ -53,7 +53,7 @@ int pluginReadData(NETWORK_PLAIN* connection, char* buffer, int buffer_len) { return recv(connection->socket, buffer, buffer_len, 0); } -int pluginSendData(NETWORK_PLAI* connection, char* buffer, int buffer_len) { +int pluginSendData(NETWORK_PLAIN* connection, char* buffer, int buffer_len) { return send(connection->socket, buffer, buffer_len, 0); } diff --git a/base/network.h b/base/network_plain/network.h similarity index 100% rename from base/network.h rename to base/network_plain/network.h diff --git a/base/network_ssl/Makefile b/base/network_ssl/Makefile index fce69a1..b27ce25 100644 --- a/base/network_ssl/Makefile +++ b/base/network_ssl/Makefile @@ -1,3 +1,9 @@ +SRC = network.c +OUTPUT = ../../bin/base/network_ssl.so +CFLAGS += -fPIC -shared -Wl,-soname,network_ssl.so -O3 -Wall -I../ +LDFlags += -lssl + default: + $(CC) $(SRC) -o $(OUTPUT) $(CFLAGS) $(LDFLAGS) clean: diff --git a/base/network_ssl/network.c b/base/network_ssl/network.c new file mode 100644 index 0000000..e846fe7 --- /dev/null +++ b/base/network_ssl/network.c @@ -0,0 +1,73 @@ +#include +#include "network.h" + +unsigned int pluginType() { + return PLUGIN_TYPE_NETWORK; +} + +const char* pluginName() { + return "ssl"; +} + +void* pluginConnect(const char* host, int port) { + NETWORK_SSL* connection; + + if((connection = malloc(sizeof(BIO))) == NULL) { + configErrorPush("Unable to malloc"); + return NULL; + } + + connection->ctx == SSL_CTX_new(SSLv23_client_method()); + + if((connection->bio = BIO_new_ssl_connect(connection->ctx)) == NULL) { + configErrorPush("Unable to allocate a BIO"); + free(connection); + return NULL; + } + + BIO_get_SSL(connection->bio, &connection->ssl); + SSL_set_mode(connection->ssl, SSL_MODE_AUTO_RETRY); + BIO_set_conn_hostname(connection->bio, host); + BIO_set_conn_int_port(connection->bio, &port); + + if(BIO_do_connect(connection->bio) <= 0) { + configErrorPush("Unable to connect"); + free(connection); + return NULL; + } + + return connection; +} + +int pluginSocketGet(NETWORK_SSL* connection) { + return BIO_get_fd(connection->bio, NULL); +} + +int pluginReadData(NETWORK_SSL* connection, char* buffer, int buffer_len) { + int ret; + + if((ret == BIO_read(connection->bio, buffer, buffer_len)) == 0) + return ECONNRESET; + else if(ret == -1) + return (!BIO_should_retry(connection->bio)) ? ECONNRESET : EWOULDBLOCK; + return ret; +} + +int pluginSendData(NETWORK_SSL* connection, const char* buffer, int buffer_len) { + int ret; + if((ret = BIO_write(connection->bio, buffer, buffer_len)) <= 0) { + if(!BIO_should_retry(connection->bio)) + return ECONNRESET; + return EWOULDBLOCK; + } + return ret; +} + +void* pluginSocketDone(NETWORK_SSL* connection) { + BIO_free_all(connection->bio); + SSL_CTX_free(connection->ctx); + + free(connection); + + return NULL; +} diff --git a/base/network_ssl/network.h b/base/network_ssl/network.h new file mode 100644 index 0000000..843abe3 --- /dev/null +++ b/base/network_ssl/network.h @@ -0,0 +1,16 @@ +#pragma once +#include +#include +#include + +#include +#include +#include + +#include + +typedef struct { + BIO* bio; + SSL_CTX* ctx; + SSL* ssl; +} NETWORK_SSL; diff --git a/bin/base/network_plain.so b/bin/base/network_plain.so new file mode 100755 index 0000000000000000000000000000000000000000..2eab0c20c59ca7bd91aea62ef7ebc643691d8299 GIT binary patch literal 16240 zcmeHOeQX@X6(2heaY$G@At{DXa!noTl;p5O0-+`3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