Misc plugin work

* Fix crash when unloading plugins.
* Fixed plugin loader and plugin unload handler.
* Added a new example plugin: mod_logging which logs users logging in and out of the hub to stdout.
This commit is contained in:
Jan Vidar Krey
2010-06-22 16:04:33 +02:00
parent ccaa4860b4
commit aa26052479
11 changed files with 265 additions and 29 deletions

View File

@@ -826,6 +826,11 @@ void hub_plugins_load(struct hub_info* hub)
void hub_plugins_unload(struct hub_info* hub)
{
if (!hub->plugins || !hub->plugins->loaded)
{
return;
}
struct uhub_plugin_handle* plugin = (struct uhub_plugin_handle*) list_get_first(hub->plugins->loaded);
while (plugin)
{

View File

@@ -18,6 +18,7 @@
*/
#include "uhub.h"
#include "plugin_api/handle.h"
static void log_user_login(struct hub_user* u)
{
@@ -67,6 +68,10 @@ void on_login_success(struct hub_info* hub, struct hub_user* u)
/* Print log message */
log_user_login(u);
#ifdef PLUGIN_SUPPORT
plugin_log_user_login_success(hub, u);
#endif
/* Announce new user to all connected users */
if (user_is_logged_in(u))
route_info_message(hub, u);
@@ -109,6 +114,11 @@ void on_logout_user(struct hub_info* hub, struct hub_user* user)
{
const char* reason = user_get_quit_reason_string(user->quit_reason);
log_user_logout(user, reason);
#ifdef PLUGIN_SUPPORT
plugin_log_user_logout(hub, user);
#endif
hub_logout_log(hub, user);
}

View File

@@ -175,8 +175,10 @@ void net_on_accept(struct net_connection* con, int event, void *arg)
struct hub_info* hub = (struct hub_info*) arg;
struct hub_probe* probe = 0;
struct ip_addr_encap ipaddr;
const char* addr;
int server_fd = net_con_get_sd(con);
#ifdef PLUGIN_SUPPORT
plugin_st status;
#endif
for (;;)
{
@@ -194,19 +196,18 @@ void net_on_accept(struct net_connection* con, int event, void *arg)
}
}
addr = ip_convert_to_string(&ipaddr);
/* FIXME: Should have a plugin log this */
LOG_TRACE("Got connection from %s", addr);
/* FIXME: A plugin should perform this check: is IP banned? */
if (acl_is_ip_banned(hub->acl, addr))
#ifdef PLUGIN_SUPPORT
status = plugin_check_ip_early(hub, &ipaddr);
if (status == st_deny)
{
LOG_INFO("Denied [%s] (IP banned)", addr);
plugin_log_connection_denied(hub, &ipaddr);
net_close(fd);
continue;
}
plugin_log_connection_accepted(hub, &ipaddr);
#endif
probe = probe_create(hub, fd, &ipaddr);
if (!probe)
{

89
src/core/plugininvoke.c Normal file
View File

@@ -0,0 +1,89 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#include "uhub.h"
#include "plugin_api/handle.h"
#define PLUGIN_INVOKE(HUB, FUNCNAME, CODE) \
if (HUB->plugins && HUB->plugins->loaded) \
{ \
struct uhub_plugin_handle* plugin = (struct uhub_plugin_handle*) list_get_first(HUB->plugins->loaded); \
while (plugin) \
{ \
if (plugin->funcs.FUNCNAME) \
CODE \
plugin = (struct uhub_plugin_handle*) list_get_next(HUB->plugins->loaded); \
} \
}
plugin_st plugin_check_ip_early(struct hub_info* hub, struct ip_addr_encap* addr)
{
plugin_st status = st_default;
PLUGIN_INVOKE(hub, login_check_ip_early, {
status = plugin->funcs.login_check_ip_early(addr);
if (status != st_default)
break;
});
return status;
}
plugin_st plugin_check_ip_late(struct hub_info* hub, struct ip_addr_encap* addr)
{
plugin_st status = st_default;
PLUGIN_INVOKE(hub, login_check_ip_late, {
status = plugin->funcs.login_check_ip_late(addr);
if (status != st_default)
break;
});
return status;
}
void plugin_log_connection_accepted(struct hub_info* hub, struct ip_addr_encap* ipaddr)
{
const char* addr = ip_convert_to_string(ipaddr);
LOG_TRACE("Got connection from %s", addr);
}
void plugin_log_connection_denied(struct hub_info* hub, struct ip_addr_encap* ipaddr)
{
const char* addr = ip_convert_to_string(ipaddr);
LOG_INFO("Denied connection from %s", addr);
}
void plugin_log_user_login_success(struct hub_info* hub, struct hub_user* user)
{
}
void plugin_log_user_login_error(struct hub_info* hub, struct hub_user* user)
{
}
void plugin_log_user_logout(struct hub_info* hub, struct hub_user* user)
{
}
void convert_user_to_plugin_user(struct plugin_user* puser, struct hub_user* user)
{
puser->sid = user->id.sid;
puser->nick = user->id.nick;
puser->cid = user->id.cid;
puser->addr = user->id.addr;
}

46
src/core/plugininvoke.h Normal file
View File

@@ -0,0 +1,46 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#ifndef HAVE_UHUB_PLUGIN_INVOKE_H
#define HAVE_UHUB_PLUGIN_INVOKE_H
#include "uhub.h"
#include "plugin_api/handle.h"
#ifdef PLUGIN_SUPPORT
struct hub_info;
struct ip_addr_encap;
void plugin_log_connection_accepted(struct hub_info* hub, struct ip_addr_encap* addr);
void plugin_log_connection_denied(struct hub_info* hub, struct ip_addr_encap* addr);
void plugin_log_user_login_success(struct hub_info* hub, struct hub_user* user);
void plugin_log_user_login_error(struct hub_info* hub, struct hub_user* user);
void plugin_log_user_logout(struct hub_info* hub, struct hub_user* user);
plugin_st plugin_check_ip_early(struct hub_info* hub, struct ip_addr_encap* addr);
plugin_st plugin_check_ip_late(struct hub_info* hub, struct ip_addr_encap* addr);
plugin_st plugin_check_nickname_valid(struct hub_info* hub, const char* nick);
plugin_st plugin_check_nickname_reserved(struct hub_info* hub, const char* nick);
#endif
#endif // HAVE_UHUB_PLUGIN_INVOKE_H

View File

@@ -24,6 +24,7 @@
struct uhub_plugin* plugin_open(const char* filename)
{
LOG_TRACE("plugin_open: \"%s\"", filename);
#ifdef HAVE_DLOPEN
struct uhub_plugin* plugin = (struct uhub_plugin*) hub_malloc_zero(sizeof(struct uhub_plugin));
if (!plugin)
@@ -92,7 +93,9 @@ struct uhub_plugin_handle* plugin_load(const char* filename, const char* config)
{
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);
LOG_INFO("Loaded plugin: %s: %s, version %s.", filename, handle->name, handle->version);
LOG_TRACE("Plugin API version: %d (func table size: " PRINTF_SIZE_T ")", handle->plugin_api_version, handle->plugin_funcs_size);
plugin->unregister = unregister_f;
return handle;
}
else
@@ -113,6 +116,7 @@ struct uhub_plugin_handle* plugin_load(const char* filename, const char* config)
void plugin_unload(struct uhub_plugin_handle* plugin)
{
plugin->handle->unregister(plugin);
plugin_close(plugin->handle);
}

View File

@@ -20,6 +20,8 @@
#ifndef HAVE_UHUB_PLUGIN_LOADER_H
#define HAVE_UHUB_PLUGIN_LOADER_H
#include "plugin_api/handle.h"
#ifdef PLUGIN_SUPPORT
struct hub_config;
@@ -31,6 +33,7 @@ struct uhub_plugin
#ifdef HAVE_DLOPEN
void* handle;
#endif
plugin_unregister_f unregister;
};
struct uhub_plugins