From fc8965f1c57c8543651cc0dbcf7fd6cf05a1ce98 Mon Sep 17 00:00:00 2001
From: Jan Vidar Krey
Date: Sun, 30 May 2010 13:02:27 +0200
Subject: [PATCH 1/6] Started working on an authentication plugin API.
---
src/plugin_api/auth_api.h | 78 +++++++++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+)
create mode 100644 src/plugin_api/auth_api.h
diff --git a/src/plugin_api/auth_api.h b/src/plugin_api/auth_api.h
new file mode 100644
index 0000000..ab5df8d
--- /dev/null
+++ b/src/plugin_api/auth_api.h
@@ -0,0 +1,78 @@
+/*
+ * 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
From ed53034ad558d7966893012b496045193b1397d2 Mon Sep 17 00:00:00 2001
From: Jan Vidar Krey
Date: Sun, 30 May 2010 23:33:06 +0200
Subject: [PATCH 2/6] 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
}
From 07d4e4470c4bdf15efdf46602f7941b4bf2b6920 Mon Sep 17 00:00:00 2001
From: Jan Vidar Krey
Date: Sun, 6 Jun 2010 16:15:15 +0200
Subject: [PATCH 3/6] 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
From baeba01835e959c89e2780133bcb4e7819515cd6 Mon Sep 17 00:00:00 2001
From: Jan Vidar Krey
Date: Sun, 6 Jun 2010 16:32:43 +0200
Subject: [PATCH 4/6] Added file_plugins directive to configuration file.
---
src/core/config.xml | 13 +++++++++++++
src/core/gen_config.c | 16 ++++++++++++++++
src/core/gen_config.h | 1 +
3 files changed, 30 insertions(+)
diff --git a/src/core/config.xml b/src/core/config.xml
index 2b8b0b8..b8066ea 100644
--- a/src/core/config.xml
+++ b/src/core/config.xml
@@ -490,6 +490,19 @@
]]>
+