[Add] Plugin crawler.

This commit is contained in:
Ritchie Cunningham 2024-11-23 19:24:44 +00:00
parent 2acf288ceb
commit 170f543b5c
8 changed files with 133 additions and 12 deletions

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -5,4 +5,6 @@ void* init() {
fprintf(stderr, "[CONFIG] First malloc failed. Let's just give up :D\n");
return NULL;
}
return NULL;
}

View File

@ -7,10 +7,10 @@
#include <dlfcn.h>
#include <signal.h>
#include "api.h"
#include "plugin.h"
typedef struct {
#include <dirent.h>
PLUGIN plugin;
} CONFIG;

View File

@ -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;

View File

@ -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;

Binary file not shown.