diff --git a/base/Makefile b/base/Makefile index e2110b8..dd61f80 100644 --- a/base/Makefile +++ b/base/Makefile @@ -1,9 +1,11 @@ default: cd c3po && make + cd config && make cd network_plain && make cd network_ssl && make clean: cd c3po && make clean + cd config && make clean cd network_plain && make clean cd network_ssl && make clean diff --git a/base/config/Makefile b/base/config/Makefile index e69de29..7cb9f8e 100644 --- a/base/config/Makefile +++ b/base/config/Makefile @@ -0,0 +1,8 @@ +SRC = config.c plugin.c +CFLAGS += -fPIC -shared -Wl,-soname,config.so -O3 +LDFLAGS += -ldl + +default: + $(CC) $(CFLAGS) $(SRC) -o ../../bin/base/config.so $(LDFLAGS) + +clean: diff --git a/base/config/api.h b/base/config/api.h index ed13655..3315727 100644 --- a/base/config/api.h +++ b/base/config/api.h @@ -1,14 +1,13 @@ #pragma once #define PLUGIN_TYPE_NETWORK 0x1 - +#define PLUGIN_TYPE_FILTER 0x2 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); @@ -17,4 +16,3 @@ const char* pluginName(); int pluginSendData(void* connection, const char* buffer, int buffer_len); void* pluginSocketDone(void* connection); #endif -#endif diff --git a/base/config/config.c b/base/config/config.c index 5c3690d..82febc1 100644 --- a/base/config/config.c +++ b/base/config/config.c @@ -5,4 +5,6 @@ void* init() { fprintf(stderr, "[CONFIG] First malloc failed. Let's just give up :D\n"); return NULL; } + + return NULL; } diff --git a/base/config/config.h b/base/config/config.h index 83134f6..046c919 100644 --- a/base/config/config.h +++ b/base/config/config.h @@ -7,10 +7,10 @@ #include #include +#include "api.h" #include "plugin.h" typedef struct { -#include PLUGIN plugin; } CONFIG; diff --git a/base/config/plugin.c b/base/config/plugin.c index 4e5cec1..6b04e2f 100644 --- a/base/config/plugin.c +++ b/base/config/plugin.c @@ -6,6 +6,116 @@ void pluginInit() { return; } +void pluginAddNetwork(void* lib_handle, const char* name) { + struct PLUGIN_NETWORK_ENTRY* plugin; + + if((plugin = malloc(sizeof(struct PLUGIN_NETWORK_ENTRY))) == NULL) { + dlclose(lib_handle); + return; + } + + plugin->lib_handle = lib_handle; + plugin->name = name; + plugin->connect = dlsym(lib_handle, "pluginConnect"); + plugin->socket = dlsym(lib_handle, "pluginSocketGet"); + plugin->read = dlsym(lib_handle, "pluginReadData"); + plugin->write = dlsym(lib_handle, "pluginSendData"); + plugin->disconnect = dlsym(lib_handle, "pluginSocketDone"); + + if(!plugin->connect || !plugin->socket || !plugin->read || + !plugin->write || !plugin->disconnect) { + dlclose(lib_handle); + free(plugin); + return; + } + + plugin->next = config->plugin.network_plug; + config->plugin.network_plug = plugin; + + fprintf(stderr, "Debug: Network plugin %s added\n", name); + + /* This should be good right? */ + return; +} + +void pluginAddFilter(void* lib_handle, const char* name) { + struct PLUGIN_FILTER_ENTRY* plugin; + int(*trig_type)(); + + if((trig_type = dlsym(lib_handle, "pluginFilterType")) == NULL) { + dlclose(lib_handle); + return; + } + + if((plugin = malloc(sizeof(struct PLUGIN_NETWORK_ENTRY))) == NULL) { + dlclose(lib_handle); + return; + } + + plugin->lib_handle = lib_handle; + plugin->name = name; + plugin->trig_type = (trig_type)(); + + plugin->init = dlsym(lib_handle, "pluginDoInit"); + plugin->filter = dlsym(lib_handle, "pluginFilter"); + plugin->destroy = dlsym(lib_handle, "pluginDestroy"); + + if(!plugin->init || !plugin->filter || !plugin->destroy) { + dlclose(lib_handle); + free(plugin); + return; + } + + plugin->next = config->plugin.filter_plug; + config->plugin.filter_plug = plugin; + + fprintf(stderr, "Debug: Filter plugin %s added\n", name); + return; +} + +void pluginProcess(const char* path, const char* name) { + void* lib_handle; + char fname[512], *longname, *usename; + unsigned int (*pluginType)(); + const char* (*pluginName)(); + + longname = NULL; + if(strlen(path) + strlen(name) + 2 > 512) { + longname = malloc(strlen(path) + strlen(name) + 2); + usename = longname; + } else + usename = fname; + + sprintf(usename, "%s/%s", path,name); + if((lib_handle = dlopen(usename, RTLD_LOCAL | RTLD_NOW)) == NULL) { + /* It's not a plugin. Ignore. */ + free(longname); + return; + } + + free(longname); + pluginType = dlsym(lib_handle, "pluginType"); + pluginName = dlsym(lib_handle, "pluginName"); + + if(pluginType == NULL || pluginName == NULL) { + dlclose(lib_handle); + return; + } + + switch((pluginType)()) { + case PLUGIN_TYPE_NETWORK: + pluginAddNetwork(lib_handle, (pluginName)()); + break; + case PLUGIN_TYPE_FILTER: + pluginAddFilter(lib_handle, (pluginName)()); + break; + default: + dlclose(lib_handle); + fprintf(stderr, "[CONFIG] %s/%s is of type not implemented\n", path, name); + break; + } +} + void pluginCrawl(const char* path) { DIR* dir; struct dirent* file; diff --git a/base/config/plugin.h b/base/config/plugin.h index fd10689..4a0fbb0 100644 --- a/base/config/plugin.h +++ b/base/config/plugin.h @@ -12,16 +12,17 @@ struct PLUGIN_NETWORK_ENTRY { }; struct PLUGIN_FILTER_ENTRY { - void* lib_handle; - const char* name; - int trig_type; - void *(*init)(); - void (*filter)(void* handle, const char* from, const char* host, - const char* channel, const char* message); - void *(*destroy)(void* handle); + void* lib_handle; + const char* name; + int trig_type; + void *(*init)(); + void (*filter)(void* handle, const char* from, const char* host, + const char* channel, const char* message); + void *(*destroy)(void* handle); + struct PLUGIN_FILTER_ENTRY* next; }; typedef struct { - struct PLUGIN_NETWORK_ENTRY* network_plugin; + struct PLUGIN_NETWORK_ENTRY* network_plug; struct PLUGIN_FILTER_ENTRY* filter_plug; } PLUGIN; diff --git a/bin/base/network_plain.so b/bin/base/network_plain.so deleted file mode 100755 index 2eab0c2..0000000 Binary files a/bin/base/network_plain.so and /dev/null differ