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 0000000..2eab0c2 Binary files /dev/null and b/bin/base/network_plain.so differ