2011-11-10 00:33:58 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-11-10 00:33:58 +04:00
|
|
|
* FreeRDP Extension Plugin Interface
|
|
|
|
*
|
|
|
|
* Copyright 2010-2011 Vic Lee
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-11-10 00:33:58 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-11-22 05:21:08 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2011-11-10 00:33:58 +04:00
|
|
|
#include <freerdp/freerdp.h>
|
2011-11-12 09:51:41 +04:00
|
|
|
#include <freerdp/utils/print.h>
|
2011-11-10 00:33:58 +04:00
|
|
|
|
|
|
|
#include "extension.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define DLOPEN(f) LoadLibraryA(f)
|
|
|
|
#define DLSYM(f, n) GetProcAddress(f, n)
|
|
|
|
#define DLCLOSE(f) FreeLibrary(f)
|
|
|
|
#define PATH_SEPARATOR '\\'
|
|
|
|
#define PLUGIN_EXT "dll"
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#define DLOPEN(f) dlopen(f, RTLD_LOCAL | RTLD_LAZY)
|
|
|
|
#define DLSYM(f, n) dlsym(f, n)
|
|
|
|
#define DLCLOSE(f) dlclose(f)
|
|
|
|
#define PATH_SEPARATOR '/'
|
2011-12-14 05:28:12 +04:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define PLUGIN_EXT "dylib"
|
|
|
|
#else
|
2011-11-10 00:33:58 +04:00
|
|
|
#define PLUGIN_EXT "so"
|
2011-12-14 05:28:12 +04:00
|
|
|
#endif
|
|
|
|
|
2011-11-10 00:33:58 +04:00
|
|
|
#endif
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
static UINT32 FREERDP_CC extension_register_plugin(rdpExtPlugin* plugin)
|
2011-11-10 00:33:58 +04:00
|
|
|
{
|
2011-11-10 00:59:03 +04:00
|
|
|
rdpExtension* ext = (rdpExtension*) plugin->ext;
|
2011-11-10 00:33:58 +04:00
|
|
|
|
|
|
|
if (ext->num_plugins >= FREERDP_EXT_MAX_COUNT)
|
|
|
|
{
|
2011-11-10 00:59:03 +04:00
|
|
|
printf("extension_register_extension: maximum number of plugins reached.\n");
|
2011-11-10 00:33:58 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ext->plugins[ext->num_plugins++] = plugin;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
static UINT32 FREERDP_CC extension_register_pre_connect_hook(rdpExtPlugin* plugin, PFREERDP_EXTENSION_HOOK hook)
|
2011-11-10 00:33:58 +04:00
|
|
|
{
|
2011-11-10 00:59:03 +04:00
|
|
|
rdpExtension* ext = (rdpExtension*) plugin->ext;
|
2011-11-10 00:33:58 +04:00
|
|
|
|
|
|
|
if (ext->num_pre_connect_hooks >= FREERDP_EXT_MAX_COUNT)
|
|
|
|
{
|
2011-11-10 00:59:03 +04:00
|
|
|
printf("extension_register_pre_connect_hook: maximum plugin reached.\n");
|
2011-11-10 00:33:58 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ext->pre_connect_hooks[ext->num_pre_connect_hooks] = hook;
|
|
|
|
ext->pre_connect_hooks_instances[ext->num_pre_connect_hooks] = plugin;
|
|
|
|
ext->num_pre_connect_hooks++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
static UINT32 FREERDP_CC extension_register_post_connect_hook(rdpExtPlugin* plugin, PFREERDP_EXTENSION_HOOK hook)
|
2011-11-10 00:33:58 +04:00
|
|
|
{
|
2011-11-10 00:59:03 +04:00
|
|
|
rdpExtension* ext = (rdpExtension*) plugin->ext;
|
2011-11-10 00:33:58 +04:00
|
|
|
|
|
|
|
if (ext->num_post_connect_hooks >= FREERDP_EXT_MAX_COUNT)
|
|
|
|
{
|
2011-11-10 00:59:03 +04:00
|
|
|
printf("extension_register_post_connect_hook: maximum plugin reached.\n");
|
2011-11-10 00:33:58 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ext->post_connect_hooks[ext->num_post_connect_hooks] = hook;
|
|
|
|
ext->post_connect_hooks_instances[ext->num_post_connect_hooks] = plugin;
|
|
|
|
ext->num_post_connect_hooks++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
static int extension_load_plugins(rdpExtension* extension)
|
2011-11-10 00:33:58 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
void* han;
|
|
|
|
char path[256];
|
|
|
|
rdpSettings* settings;
|
|
|
|
PFREERDP_EXTENSION_ENTRY entry;
|
|
|
|
FREERDP_EXTENSION_ENTRY_POINTS entryPoints;
|
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
settings = extension->instance->settings;
|
2011-11-10 00:33:58 +04:00
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
entryPoints.ext = extension;
|
|
|
|
entryPoints.pRegisterExtension = extension_register_plugin;
|
|
|
|
entryPoints.pRegisterPreConnectHook = extension_register_pre_connect_hook;
|
|
|
|
entryPoints.pRegisterPostConnectHook = extension_register_post_connect_hook;
|
2011-11-10 00:33:58 +04:00
|
|
|
|
|
|
|
for (i = 0; settings->extensions[i].name[0]; i++)
|
|
|
|
{
|
|
|
|
if (strchr(settings->extensions[i].name, PATH_SEPARATOR) == NULL)
|
|
|
|
snprintf(path, sizeof(path), EXT_PATH "/%s." PLUGIN_EXT, settings->extensions[i].name);
|
|
|
|
else
|
|
|
|
snprintf(path, sizeof(path), "%s", settings->extensions[i].name);
|
|
|
|
|
|
|
|
han = DLOPEN(path);
|
2011-11-10 00:59:03 +04:00
|
|
|
printf("extension_load_plugins: %s\n", path);
|
2011-11-10 00:33:58 +04:00
|
|
|
if (han == NULL)
|
|
|
|
{
|
2011-11-10 00:59:03 +04:00
|
|
|
printf("extension_load_plugins: failed to load %s\n", path);
|
2011-11-10 00:33:58 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = (PFREERDP_EXTENSION_ENTRY) DLSYM(han, FREERDP_EXT_EXPORT_FUNC_NAME);
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
DLCLOSE(han);
|
2011-11-10 00:59:03 +04:00
|
|
|
printf("extension_load_plugins: failed to find export function in %s\n", path);
|
2011-11-10 00:33:58 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
entryPoints.data = extension->instance->settings->extensions[i].data;
|
2011-11-10 00:33:58 +04:00
|
|
|
if (entry(&entryPoints) != 0)
|
|
|
|
{
|
|
|
|
DLCLOSE(han);
|
2011-11-10 00:59:03 +04:00
|
|
|
printf("extension_load_plugins: %s entry returns error.\n", path);
|
2011-11-10 00:33:58 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
static int extension_init_plugins(rdpExtension* extension)
|
2011-11-10 00:33:58 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
for (i = 0; i < extension->num_plugins; i++)
|
|
|
|
extension->plugins[i]->init(extension->plugins[i], extension->instance);
|
2011-11-10 00:33:58 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
static int extension_uninit_plugins(rdpExtension* extension)
|
2011-11-10 00:33:58 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
for (i = 0; i < extension->num_plugins; i++)
|
|
|
|
extension->plugins[i]->uninit(extension->plugins[i], extension->instance);
|
2011-11-10 00:33:58 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-25 13:57:49 +04:00
|
|
|
/** Gets through all registered pre-connect hooks and executes them.
|
|
|
|
* @param extension - pointer to a rdpExtension structure
|
|
|
|
* @return 0 always
|
|
|
|
*/
|
2011-11-10 00:59:03 +04:00
|
|
|
int extension_pre_connect(rdpExtension* extension)
|
2011-11-10 00:33:58 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
for (i = 0; i < extension->num_pre_connect_hooks; i++)
|
|
|
|
extension->pre_connect_hooks[i](extension->pre_connect_hooks_instances[i], extension->instance);
|
2011-11-10 00:33:58 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-26 00:55:59 +04:00
|
|
|
/** Gets through all registered post-connect hooks and executes them.
|
|
|
|
* @param extension - pointer to a rdpExtension structure
|
|
|
|
* @return 0 always
|
|
|
|
*/
|
2011-11-10 00:59:03 +04:00
|
|
|
int extension_post_connect(rdpExtension* ext)
|
2011-11-10 00:33:58 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ext->num_post_connect_hooks; i++)
|
|
|
|
ext->post_connect_hooks[i](ext->post_connect_hooks_instances[i], ext->instance);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-03 12:40:05 +04:00
|
|
|
void extension_load_and_init_plugins(rdpExtension* extension)
|
|
|
|
{
|
|
|
|
extension_load_plugins(extension);
|
|
|
|
extension_init_plugins(extension);
|
|
|
|
}
|
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
rdpExtension* extension_new(freerdp* instance)
|
2011-11-10 00:33:58 +04:00
|
|
|
{
|
2011-11-17 06:41:42 +04:00
|
|
|
rdpExtension* extension = NULL;
|
2011-11-10 00:33:58 +04:00
|
|
|
|
2011-11-17 06:41:42 +04:00
|
|
|
if (instance != NULL)
|
|
|
|
{
|
2012-11-22 05:21:08 +04:00
|
|
|
extension = (rdpExtension*) malloc(sizeof(rdpExtension));
|
|
|
|
ZeroMemory(extension, sizeof(rdpExtension));
|
2011-11-10 00:33:58 +04:00
|
|
|
|
2011-11-17 06:41:42 +04:00
|
|
|
extension->instance = instance;
|
|
|
|
}
|
2011-11-10 00:33:58 +04:00
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
return extension;
|
2011-11-10 00:33:58 +04:00
|
|
|
}
|
|
|
|
|
2011-11-10 00:59:03 +04:00
|
|
|
void extension_free(rdpExtension* extension)
|
2011-11-10 00:33:58 +04:00
|
|
|
{
|
2011-11-17 06:41:42 +04:00
|
|
|
if (extension != NULL)
|
|
|
|
{
|
|
|
|
extension_uninit_plugins(extension);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(extension);
|
2011-11-17 06:41:42 +04:00
|
|
|
}
|
2011-11-10 00:33:58 +04:00
|
|
|
}
|