From 07d4e4470c4bdf15efdf46602f7941b4bf2b6920 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Sun, 6 Jun 2010 16:15:15 +0200 Subject: [PATCH] More work on the plugin API. --- GNUmakefile | 13 +++++++ src/core/pluginloader.c | 55 +++++++++++++++++++++++++-- src/core/pluginloader.h | 13 +++++-- src/plugin_api/auth_api.h | 78 --------------------------------------- src/plugins/mod_example.c | 24 ++++++++++++ src/uhub.h | 1 + 6 files changed, 100 insertions(+), 84 deletions(-) delete mode 100644 src/plugin_api/auth_api.h create mode 100644 src/plugins/mod_example.c diff --git a/GNUmakefile b/GNUmakefile index 5dc8f48..ce1e81e 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -192,6 +192,10 @@ autotest_SOURCES := \ autotest_OBJECTS = autotest.o +plugin_example_SOURCES := src/plugins/mod_example.c +plugin_example_TARGET := $(plugin_example_SOURCES:.c=.so) + + # Source to objects libuhub_OBJECTS := $(libuhub_SOURCES:.c=.o) libadc_client_OBJECTS := $(libadc_client_SOURCES:.c=.o) @@ -202,14 +206,23 @@ adcrush_OBJECTS := $(adcrush_SOURCES:.c=.o) admin_OBJECTS := $(admin_SOURCES:.c=.o) all_OBJECTS := $(libuhub_OBJECTS) $(uhub_OBJECTS) $(adcrush_OBJECTS) $(autotest_OBJECTS) $(admin_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS) +all_plugins := $(plugin_example_TARGET) uhub_BINARY=uhub$(BIN_EXT) adcrush_BINARY=adcrush$(BIN_EXT) admin_BINARY=uhub-admin$(BIN_EXT) autotest_BINARY=autotest/test$(BIN_EXT) +ifeq ($(USE_PLUGINS),YES) +all_OBJECTS += $(plugins) +endif + + .PHONY: revision.h.tmp +%.so: %.c + $(MSG_CC) $(CC) -shared -fPIC -o $@ $< $(CFLAGS) + %.o: %.c version.h revision.h $(MSG_CC) $(CC) -c $(CFLAGS) -o $@ $< diff --git a/src/core/pluginloader.c b/src/core/pluginloader.c index 8c37da9..5b48bc4 100644 --- a/src/core/pluginloader.c +++ b/src/core/pluginloader.c @@ -20,8 +20,9 @@ #include "uhub.h" #ifdef PLUGIN_SUPPORT +#include "plugin_api/handle.h" -struct uhub_plugin* uhub_plugin_open(const char* filename) +struct uhub_plugin* plugin_open(const char* filename) { #ifdef HAVE_DLOPEN struct uhub_plugin* plugin = (struct uhub_plugin*) hub_malloc_zero(sizeof(struct uhub_plugin)); @@ -45,7 +46,7 @@ struct uhub_plugin* uhub_plugin_open(const char* filename) #endif } -void uhub_plugin_close(struct uhub_plugin* plugin) +void plugin_close(struct uhub_plugin* plugin) { #ifdef HAVE_DLOPEN dlclose(plugin->handle); @@ -53,7 +54,7 @@ void uhub_plugin_close(struct uhub_plugin* plugin) #endif } -void* uhub_plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol) +void* plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol) { #ifdef HAVE_DLOPEN void* addr = dlsym(plugin->handle, symbol); @@ -63,4 +64,52 @@ void* uhub_plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol) #endif } +struct uhub_plugin_handle* plugin_load(const char* filename, const char* config) +{ + plugin_register_f register_f; + plugin_unregister_f unregister_f; + int ret; + struct uhub_plugin_handle* handle = hub_malloc_zero(sizeof(struct uhub_plugin_handle)); + struct uhub_plugin* plugin = plugin_open(filename); + + if (!plugin) + return NULL; + + if (!handle) + { + plugin_close(plugin); + return NULL; + } + + handle->handle = plugin; + register_f = plugin_lookup_symbol(plugin, "plugin_register"); + unregister_f = plugin_lookup_symbol(plugin, "plugin_unregister"); + + if (register_f && unregister_f) + { + ret = register_f(handle, config); + if (ret == 0) + { + if (handle->plugin_api_version == PLUGIN_API_VERSION && handle->plugin_funcs_size == sizeof(struct plugin_funcs)) + { + LOG_INFO("Loaded plugin: %s: \"%s\", version %s.", filename, handle->name, handle->version); + return handle; + } + else + { + LOG_ERROR("Unable to load plugin: %s - API version mistmatch", filename); + } + } + else + { + LOG_ERROR("Unable to load plugin: %s - Failed to initialize", filename); + } + } + + plugin_close(plugin); + hub_free(handle); + return NULL; +} + + #endif /* PLUGIN_SUPPORT */ diff --git a/src/core/pluginloader.h b/src/core/pluginloader.h index ecee3f5..5924157 100644 --- a/src/core/pluginloader.h +++ b/src/core/pluginloader.h @@ -22,6 +22,8 @@ #ifdef PLUGIN_SUPPORT +struct uhub_plugin_handle; + struct uhub_plugin { #ifdef HAVE_DLOPEN @@ -29,11 +31,16 @@ struct uhub_plugin #endif }; -extern struct uhub_plugin* uhub_plugin_open(const char* filename); +extern struct uhub_plugin* plugin_open(const char* filename); -extern void uhub_plugin_close(struct uhub_plugin*); +extern void plugin_close(struct uhub_plugin*); -extern void* uhub_plugin_lookup_symbol(struct uhub_plugin*, const char* symbol); +extern void* plugin_lookup_symbol(struct uhub_plugin*, const char* symbol); + + +// Load and register a plugin +extern struct uhub_plugin_handle* plugin_load(const char* filename, const char* config); +extern void plugin_unload(struct uhub_plugin_handle*); #endif /* PLUGIN_SUPPORT */ diff --git a/src/plugin_api/auth_api.h b/src/plugin_api/auth_api.h deleted file mode 100644 index 53871a7..0000000 --- a/src/plugin_api/auth_api.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - * uhub - A tiny ADC p2p connection hub - * Copyright (C) 2007-2010, Jan Vidar Krey - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -#ifndef HAVE_UHUB_PLUGIN_AUTH_H -#define HAVE_UHUB_PLUGIN_AUTH_H - -enum user_credentials -{ - cred_none, /**<<< "User has no credentials (not yet logged in)" */ - cred_bot, /**<<< "User is a robot" */ - cred_guest, /**<<< "User is a guest (unregistered user)" */ - cred_user, /**<<< "User is identified as a registered user" */ - cred_operator, /**<<< "User is identified as a hub operator" */ - cred_super, /**<<< "User is a super user" (not used) */ - cred_link, /**<<< "User is a link (not used currently)" */ - cred_admin, /**<<< "User is identified as a hub administrator/owner" */ -}; - -struct uhub_plugin_auth_info -{ - char* nickname; - char* password; - enum user_credentials credentials; -}; - -#define UHUB_AUTH_PLUGIN_VERSION 0 - -/** - * Returns the version number of the uhub auth plugin. - */ -extern int uhub_plugin_auth_version(); - -/** - * Returns a struct user_plugin_auth_info with nickname, password and credentials - * for a given user's nickname. - * In case the user is not registered NULL is returned. - * - * @returns A relevant uhub_plugin_auth_info with password and credentials, or NULL - * if the user is not found in the auth database. - */ -extern struct uhub_plugin_auth_info* uhub_plugin_auth_get_user(const char* nickname); - -/** - * Register a new user. - * - * @param user contains nickname, password and credentials for a user. - * @returns 0 on success - * <0 if an error occured - */ -extern int uhub_plugin_auth_register_user(struct uhub_plugin_auth_info* user); - -/** - * Update password and user credentials. - * - * @param user contains nickname and new password. - * @returns 0 on success - * <0 if an error occured - */ -extern int uhub_plugin_auth_update_info(struct uhub_plugin_auth_info* user); - - -#endif /* HAVE_UHUB_PLUGIN_AUTH_H */ \ No newline at end of file diff --git a/src/plugins/mod_example.c b/src/plugins/mod_example.c new file mode 100644 index 0000000..904219f --- /dev/null +++ b/src/plugins/mod_example.c @@ -0,0 +1,24 @@ +/** + * This is a minimal example plugin for uhub. + */ + +#include "uhub.h" +#include "plugin_api/handle.h" + +int plugin_register(struct uhub_plugin_handle* plugin, const char* config) +{ + plugin->name = "Example plugin"; + plugin->version = "1.0"; + plugin->description = "A simple example plugin"; + plugin->ptr = NULL; + plugin->plugin_api_version = PLUGIN_API_VERSION; + plugin->plugin_funcs_size = sizeof(struct plugin_funcs); + memset(&plugin->funcs, 0, sizeof(struct plugin_funcs)); + return 0; +} + +void plugin_unregister(struct uhub_plugin_handle* plugin) +{ + /* No need to do anything! */ +} + diff --git a/src/uhub.h b/src/uhub.h index 8903a9c..c0dfa29 100644 --- a/src/uhub.h +++ b/src/uhub.h @@ -45,6 +45,7 @@ #define MAX_CID_LEN 39 #define MAX_NICK_LEN 64 +#define MAX_PASS_LEN 64 #define MAX_UA_LEN 32 #define TIGERSIZE 24