From ed53034ad558d7966893012b496045193b1397d2 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Sun, 30 May 2010 23:33:06 +0200 Subject: [PATCH] Added plugin loader code, merely a wrapper around dlopen() and friends. --- GNUmakefile | 9 ++++++ src/core/pluginloader.c | 66 +++++++++++++++++++++++++++++++++++++++ src/core/pluginloader.h | 41 ++++++++++++++++++++++++ src/plugin_api/auth_api.h | 4 +-- src/system.h | 2 ++ src/uhub.h | 1 + 6 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 src/core/pluginloader.c create mode 100644 src/core/pluginloader.h diff --git a/GNUmakefile b/GNUmakefile index 7427b7d..5dc8f48 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -12,6 +12,7 @@ RANLIB := ranlib CFLAGS += -pipe -Wall USE_SSL ?= NO USE_BIGENDIAN ?= AUTO +USE_PLUGINS ?= YES BITS ?= AUTO SILENT ?= YES TERSE ?= NO @@ -42,6 +43,7 @@ UHUB_PREFIX ?= c:/uhub/ CFLAGS += -mno-cygwin LDFLAGS += -mno-cygwin BIN_EXT ?= .exe +USE_PLUGINS := NO else DESTDIR ?= / UHUB_CONF_DIR ?= $(DESTDIR)/etc/uhub @@ -118,6 +120,12 @@ CFLAGS += -DSSL_SUPPORT LDLIBS += -lssl endif +ifeq ($(USE_PLUGINS),YES) +CFLAGS += -DPLUGIN_SUPPORT +LDLIBS += -ldl +endif + + GIT_VERSION=$(shell git describe --tags 2>/dev/null || echo "") GIT_REVISION=$(shell git show --abbrev-commit 2>/dev/null | head -n 1 | cut -f 2 -d " " || echo "") OLD_REVISION=$(shell grep GIT_REVISION revision.h 2>/dev/null | cut -f 3 -d " " | tr -d "\"") @@ -138,6 +146,7 @@ libuhub_SOURCES := \ src/core/route.c \ src/core/user.c \ src/core/usermanager.c \ + src/core/pluginloader.c \ src/network/backend.c \ src/network/connection.c \ src/network/epoll.c \ diff --git a/src/core/pluginloader.c b/src/core/pluginloader.c new file mode 100644 index 0000000..8c37da9 --- /dev/null +++ b/src/core/pluginloader.c @@ -0,0 +1,66 @@ +/* + * 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 . + * + */ + +#include "uhub.h" + +#ifdef PLUGIN_SUPPORT + +struct uhub_plugin* uhub_plugin_open(const char* filename) +{ +#ifdef HAVE_DLOPEN + struct uhub_plugin* plugin = (struct uhub_plugin*) hub_malloc_zero(sizeof(struct uhub_plugin)); + if (!plugin) + { + return 0; + } + + plugin->handle = dlopen(filename, RTLD_LAZY); + + if (!plugin->handle) + { + LOG_ERROR("Unable to open plugin %s: %s", filename, dlerror()); + hub_free(plugin); + return 0; + } + + return plugin; +#else + return 0; +#endif +} + +void uhub_plugin_close(struct uhub_plugin* plugin) +{ +#ifdef HAVE_DLOPEN + dlclose(plugin->handle); + hub_free(plugin); +#endif +} + +void* uhub_plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol) +{ +#ifdef HAVE_DLOPEN + void* addr = dlsym(plugin->handle, symbol); + return addr; +#else + return 0; +#endif +} + +#endif /* PLUGIN_SUPPORT */ diff --git a/src/core/pluginloader.h b/src/core/pluginloader.h new file mode 100644 index 0000000..ecee3f5 --- /dev/null +++ b/src/core/pluginloader.h @@ -0,0 +1,41 @@ +/* + * 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_LOADER_H +#define HAVE_UHUB_PLUGIN_LOADER_H + +#ifdef PLUGIN_SUPPORT + +struct uhub_plugin +{ +#ifdef HAVE_DLOPEN + void* handle; +#endif +}; + +extern struct uhub_plugin* uhub_plugin_open(const char* filename); + +extern void uhub_plugin_close(struct uhub_plugin*); + +extern void* uhub_plugin_lookup_symbol(struct uhub_plugin*, const char* symbol); + +#endif /* PLUGIN_SUPPORT */ + +#endif /* HAVE_UHUB_PLUGIN_LOADER_H */ + diff --git a/src/plugin_api/auth_api.h b/src/plugin_api/auth_api.h index ab5df8d..53871a7 100644 --- a/src/plugin_api/auth_api.h +++ b/src/plugin_api/auth_api.h @@ -23,9 +23,9 @@ enum user_credentials { cred_none, /**<<< "User has no credentials (not yet logged in)" */ - cred_bot, /**<<< "User is a robot" */ + cred_bot, /**<<< "User is a robot" */ cred_guest, /**<<< "User is a guest (unregistered user)" */ - cred_user, /**<<< "User is identified as a registered 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)" */ diff --git a/src/system.h b/src/system.h index 58aeee1..716741c 100644 --- a/src/system.h +++ b/src/system.h @@ -89,6 +89,8 @@ #include #include #define HAVE_STRNDUP +#define HAVE_DLOPEN +#include #ifndef __HAIKU__ #define HAVE_MEMMEM #endif diff --git a/src/uhub.h b/src/uhub.h index cc91942..8903a9c 100644 --- a/src/uhub.h +++ b/src/uhub.h @@ -84,6 +84,7 @@ extern "C" { #include "core/commands.h" #include "core/inf.h" #include "core/hubevent.h" +#include "core/pluginloader.h" #ifdef __cplusplus }