From 170f543b5cf801d70bb02558f1062586ef98e1f3 Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Sat, 23 Nov 2024 19:24:44 +0000 Subject: [PATCH] [Add] Plugin crawler. --- base/Makefile | 2 + base/config/Makefile | 8 +++ base/config/api.h | 4 +- base/config/config.c | 2 + base/config/config.h | 2 +- base/config/plugin.c | 110 ++++++++++++++++++++++++++++++++++++++ base/config/plugin.h | 17 +++--- bin/base/network_plain.so | Bin 16240 -> 0 bytes 8 files changed, 133 insertions(+), 12 deletions(-) delete mode 100755 bin/base/network_plain.so 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 2eab0c20c59ca7bd91aea62ef7ebc643691d8299..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16240 zcmeHOeQX@X6(2heaY$G@At{DXa!noTl;p5O0-+`3Y@eNV>jXEp)l?+fobT4YBj-DJ zZ_lwoRoy1goJ^_|sa1uF8mN${3c{jPrIi|-CeVOr8bVc}HcBm8;7+8}h>w~A*}gaX z-rnAF*H-+|N@yp!d-HzpV`ksZu4iuEe7&Q)v%0EEaG57=7DY~o5D8{)U=74JiAC^z zmH4F8ttpB=R2!?cA*Q0<=CO_WSS8*|7m`z^EJr&j3ZOBRfc$o7ZMDrab=zG8qWn5r zp5I(bN62oBDBB^6c5`GmM|Mn4Q?QuQ<;p%{KOZ80UK#)ymVo>g!fSVu?7Xy*^qI<7 z0M`4m8T%;h^^l!O9r4m#)E-lA?@M5Zartix-{u|EUZwlcK-bNb!Ykv6K5)!z-QF)i zZ5sPBk2-+$WyGU=tV=yu7rFoD@YlX|&-avT9$0Zl*I86W{W|!dU+jNKh?%uD1D{50 z34AVv&$$KnogF{>+m_q@vFS6>Q>T71dikjGc-&BawM4lM#AVAPfG#Z~d8q=9*AghB z|5ye6cPikE;X=x_J5fRZKEU~2K-DQ2kATA!E8|ct=l{DE@U?&k;NvZw0Ic$Yz<(P) z<^1oJxFYtDa1C}4%B%2MDikq|Hwwz-HW4oXMf`{de~<7P!dH-fKj9(re=*^E2=5_$ zG2zDvKTLiW5H2)rXgHnHGG@duHBD$;ecQB{Zs$7)ILY$!6kW(9oe5*w^S@EgIje4Mq}4YOPnAZUt+*^&NU77LJ$^Vd&9O z*3nZomVpXm(k=`aDWb`AMiA&Zg6Vr zgBvlB)$p$ho=v1!t0KxABK)sfEQPVaV|ZC2aS1-v>j=wUkywcSZlLkP?|FW|Hq(8b zrS~}Fv-t2s=?O{VypNebxy8ouox$%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