Updated copyright header.

Added error checks and assertions.
This commit is contained in:
Armin Novak 2014-05-26 09:01:52 +02:00
parent 32ef77738f
commit 4b0b94d04d
1 changed files with 164 additions and 46 deletions

View File

@ -3,6 +3,8 @@
* Dynamic Virtual Channel Manager
*
* Copyright 2010-2011 Vic Lee
* Copyright 2014 Thincast Technologies GmbH
* Copyright 2014 Armin Novak <armin.novak@thincast.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,6 +24,7 @@
#endif
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@ -45,6 +48,12 @@ struct cb_value
static int dvcman_get_configuration(IWTSListener *pListener, void **ppPropertyBag)
{
if (!ppPropertyBag)
{
DEBUG_WARN("ppPropertyBag=%p", ppPropertyBag);
return 0;
}
*ppPropertyBag = NULL;
return 1;
}
@ -55,19 +64,29 @@ static int dvcman_create_listener(IWTSVirtualChannelManager *pChannelMgr,
{
DVCMAN *dvcman = (DVCMAN *) pChannelMgr;
DVCMAN_LISTENER *listener;
if(dvcman->num_listeners < MAX_PLUGINS)
assert(dvcman);
if (dvcman->num_listeners < MAX_PLUGINS)
{
DEBUG_DVC("%d.%s.", dvcman->num_listeners, pszChannelName);
listener = (DVCMAN_LISTENER *) malloc(sizeof(DVCMAN_LISTENER));
ZeroMemory(listener, sizeof(DVCMAN_LISTENER));
listener = (DVCMAN_LISTENER *) calloc(1, sizeof(DVCMAN_LISTENER));
if (!listener)
{
DEBUG_WARN("calloc failed %s (%d)!", strerror(errno), errno);
return 2;
}
listener->iface.GetConfiguration = dvcman_get_configuration;
listener->iface.pInterface = NULL;
listener->dvcman = dvcman;
listener->channel_name = _strdup(pszChannelName);
listener->flags = ulFlags;
listener->listener_callback = pListenerCallback;
if(ppListener)
if (ppListener)
*ppListener = (IWTSListener *) listener;
dvcman->listeners[dvcman->num_listeners++] = (IWTSListener *) listener;
return 0;
}
@ -82,8 +101,10 @@ static int dvcman_push_event(IWTSVirtualChannelManager *pChannelMgr, wMessage *p
{
int status;
DVCMAN *dvcman = (DVCMAN *) pChannelMgr;
assert(dvcman);
status = drdynvc_push_event(dvcman->drdynvc, pEvent);
if(status == 0)
if (status == 0)
{
DEBUG_DVC("event_type %d pushed.", GetMessageType(pEvent->id));
}
@ -91,13 +112,17 @@ static int dvcman_push_event(IWTSVirtualChannelManager *pChannelMgr, wMessage *p
{
DEBUG_WARN("event_type %d push failed.", GetMessageType(pEvent->id));
}
return status;
}
static int dvcman_register_plugin(IDRDYNVC_ENTRY_POINTS *pEntryPoints, const char *name, IWTSPlugin *pPlugin)
{
DVCMAN *dvcman = ((DVCMAN_ENTRY_POINTS *) pEntryPoints)->dvcman;
if(dvcman->num_plugins < MAX_PLUGINS)
assert(pEntryPoints);
assert(dvcman);
if (dvcman->num_plugins < MAX_PLUGINS)
{
DEBUG_DVC("num_plugins %d", dvcman->num_plugins);
dvcman->plugin_names[dvcman->num_plugins] = name;
@ -115,24 +140,30 @@ IWTSPlugin *dvcman_get_plugin(IDRDYNVC_ENTRY_POINTS *pEntryPoints, const char *n
{
int i;
DVCMAN *dvcman = ((DVCMAN_ENTRY_POINTS *) pEntryPoints)->dvcman;
for(i = 0; i < dvcman->num_plugins; i++)
assert(pEntryPoints);
assert(dvcman);
for (i = 0; i < dvcman->num_plugins; i++)
{
if(dvcman->plugin_names[i] == name ||
if (dvcman->plugin_names[i] == name ||
strcmp(dvcman->plugin_names[i], name) == 0)
{
return dvcman->plugins[i];
}
}
return NULL;
}
ADDIN_ARGV *dvcman_get_plugin_data(IDRDYNVC_ENTRY_POINTS *pEntryPoints)
{
assert(pEntryPoints);
return ((DVCMAN_ENTRY_POINTS *) pEntryPoints)->args;
}
UINT32 dvcman_get_channel_id(IWTSVirtualChannel *channel)
{
assert(channel);
return ((DVCMAN_CHANNEL *) channel)->channel_id;
}
@ -142,18 +173,22 @@ IWTSVirtualChannel *dvcman_find_channel_by_id(IWTSVirtualChannelManager *pChanne
BOOL found = FALSE;
DVCMAN_CHANNEL *channel;
DVCMAN *dvcman = (DVCMAN *) pChannelMgr;
assert(dvcman);
ArrayList_Lock(dvcman->channels);
index = 0;
channel = (DVCMAN_CHANNEL *) ArrayList_GetItem(dvcman->channels, index++);
while(channel)
while (channel)
{
if(channel->channel_id == ChannelId)
if (channel->channel_id == ChannelId)
{
found = TRUE;
break;
}
channel = (DVCMAN_CHANNEL *) ArrayList_GetItem(dvcman->channels, index++);
}
ArrayList_Unlock(dvcman->channels);
return (found) ? ((IWTSVirtualChannel *) channel) : NULL;
}
@ -165,24 +200,35 @@ void *dvcman_get_channel_interface_by_name(IWTSVirtualChannelManager *pChannelMg
void *pInterface = NULL;
DVCMAN_LISTENER *listener;
DVCMAN *dvcman = (DVCMAN *) pChannelMgr;
for(i = 0; i < dvcman->num_listeners; i++)
assert(dvcman);
for (i = 0; i < dvcman->num_listeners; i++)
{
listener = (DVCMAN_LISTENER *) dvcman->listeners[i];
if(strcmp(listener->channel_name, ChannelName) == 0)
assert(listener);
if (strcmp(listener->channel_name, ChannelName) == 0)
{
pInterface = listener->iface.pInterface;
found = TRUE;
break;
}
}
return (found) ? pInterface : NULL;
}
IWTSVirtualChannelManager *dvcman_new(drdynvcPlugin *plugin)
{
DVCMAN *dvcman;
dvcman = (DVCMAN *) malloc(sizeof(DVCMAN));
ZeroMemory(dvcman, sizeof(DVCMAN));
dvcman = (DVCMAN *) calloc(1,sizeof(DVCMAN));
if (!dvcman)
{
DEBUG_WARN("calloc failed %s (%d)!", strerror(errno), errno);
return NULL;
}
dvcman->iface.CreateListener = dvcman_create_listener;
dvcman->iface.PushEvent = dvcman_push_event;
dvcman->iface.FindChannelById = dvcman_find_channel_by_id;
@ -196,10 +242,12 @@ int dvcman_load_addin(IWTSVirtualChannelManager *pChannelMgr, ADDIN_ARGV *args)
{
DVCMAN_ENTRY_POINTS entryPoints;
PDVC_PLUGIN_ENTRY pDVCPluginEntry = NULL;
assert(args);
fprintf(stderr, "Loading Dynamic Virtual Channel %s\n", args->argv[0]);
pDVCPluginEntry = (PDVC_PLUGIN_ENTRY) freerdp_load_channel_addin_entry(args->argv[0],
NULL, NULL, FREERDP_ADDIN_CHANNEL_DYNAMIC);
if(pDVCPluginEntry)
if (pDVCPluginEntry)
{
entryPoints.iface.RegisterPlugin = dvcman_register_plugin;
entryPoints.iface.GetPlugin = dvcman_get_plugin;
@ -208,16 +256,20 @@ int dvcman_load_addin(IWTSVirtualChannelManager *pChannelMgr, ADDIN_ARGV *args)
entryPoints.args = args;
pDVCPluginEntry((IDRDYNVC_ENTRY_POINTS *) &entryPoints);
}
return 0;
}
static void dvcman_channel_free(DVCMAN_CHANNEL *channel)
{
assert(channel);
if(channel->channel_callback)
if (channel->channel_callback)
channel->channel_callback->OnClose(channel->channel_callback);
if(channel->channel_name)
if (channel->channel_name)
free(channel->channel_name);
free(channel);
}
@ -229,27 +281,34 @@ void dvcman_free(IWTSVirtualChannelManager *pChannelMgr)
DVCMAN_LISTENER *listener;
DVCMAN_CHANNEL *channel;
DVCMAN *dvcman = (DVCMAN *) pChannelMgr;
assert(dvcman);
ArrayList_Lock(dvcman->channels);
count = ArrayList_Count(dvcman->channels);
for(i = 0; i < count; i++)
for (i = 0; i < count; i++)
{
channel = (DVCMAN_CHANNEL *) ArrayList_GetItem(dvcman->channels, i);
dvcman_channel_free(channel);
}
ArrayList_Unlock(dvcman->channels);
ArrayList_Free(dvcman->channels);
for(i = 0; i < dvcman->num_listeners; i++)
for (i = 0; i < dvcman->num_listeners; i++)
{
listener = (DVCMAN_LISTENER *) dvcman->listeners[i];
free(listener->channel_name);
free(listener);
}
for(i = 0; i < dvcman->num_plugins; i++)
for (i = 0; i < dvcman->num_plugins; i++)
{
pPlugin = dvcman->plugins[i];
if(pPlugin->Terminated)
if (pPlugin->Terminated)
pPlugin->Terminated(pPlugin);
}
free(dvcman);
}
@ -258,12 +317,16 @@ int dvcman_init(IWTSVirtualChannelManager *pChannelMgr)
int i;
IWTSPlugin *pPlugin;
DVCMAN *dvcman = (DVCMAN *) pChannelMgr;
for(i = 0; i < dvcman->num_plugins; i++)
assert(dvcman);
for (i = 0; i < dvcman->num_plugins; i++)
{
pPlugin = dvcman->plugins[i];
if(pPlugin->Initialize)
if (pPlugin->Initialize)
pPlugin->Initialize(pPlugin, pChannelMgr);
}
return 0;
}
@ -271,6 +334,7 @@ static int dvcman_write_channel(IWTSVirtualChannel *pChannel, UINT32 cbSize, BYT
{
int status;
DVCMAN_CHANNEL *channel = (DVCMAN_CHANNEL *) pChannel;
assert(channel);
WaitForSingleObject(channel->dvc_chan_mutex, INFINITE);
status = drdynvc_write_data(channel->dvcman->drdynvc, channel->channel_id, pBuffer, cbSize);
ReleaseMutex(channel->dvc_chan_mutex);
@ -281,6 +345,8 @@ static int dvcman_close_channel_iface(IWTSVirtualChannel *pChannel)
{
DVCMAN_CHANNEL *channel = (DVCMAN_CHANNEL *) pChannel;
DVCMAN *dvcman = channel->dvcman;
assert(channel);
assert(dvcman);
DEBUG_DVC("id=%d", channel->channel_id);
ArrayList_Remove(dvcman->channels, channel);
dvcman_channel_free(channel);
@ -296,22 +362,35 @@ int dvcman_create_channel(IWTSVirtualChannelManager *pChannelMgr, UINT32 Channel
DrdynvcClientContext *context;
IWTSVirtualChannelCallback *pCallback;
DVCMAN *dvcman = (DVCMAN *) pChannelMgr;
channel = (DVCMAN_CHANNEL *) malloc(sizeof(DVCMAN_CHANNEL));
channel = (DVCMAN_CHANNEL *) calloc(1, sizeof(DVCMAN_CHANNEL));
if (!channel)
{
DEBUG_WARN("calloc failed %s (%d)!", strerror(errno), errno);
return -1;
}
ZeroMemory(channel, sizeof(DVCMAN_CHANNEL));
channel->dvcman = dvcman;
channel->channel_id = ChannelId;
channel->channel_name = _strdup(ChannelName);
for(i = 0; i < dvcman->num_listeners; i++)
for (i = 0; i < dvcman->num_listeners; i++)
{
listener = (DVCMAN_LISTENER *) dvcman->listeners[i];
if(strcmp(listener->channel_name, ChannelName) == 0)
assert(listener);
if (strcmp(listener->channel_name, ChannelName) == 0)
{
channel->iface.Write = dvcman_write_channel;
channel->iface.Close = dvcman_close_channel_iface;
channel->dvc_chan_mutex = CreateMutex(NULL, FALSE, NULL);
bAccept = 1;
pCallback = NULL;
if(listener->listener_callback->OnNewChannelConnection(listener->listener_callback,
assert(listener->listener_callback);
assert(listener->listener_callback->OnNewChannelConnection);
if (listener->listener_callback->OnNewChannelConnection(listener->listener_callback,
(IWTSVirtualChannel *) channel, NULL, &bAccept, &pCallback) == 0 && bAccept == 1)
{
DEBUG_DVC("listener %s created new channel %d",
@ -319,6 +398,7 @@ int dvcman_create_channel(IWTSVirtualChannelManager *pChannelMgr, UINT32 Channel
channel->status = 0;
channel->channel_callback = pCallback;
channel->pInterface = listener->iface.pInterface;
assert(dvcman->drdynvc);
ArrayList_Add(dvcman->channels, channel);
context = dvcman->drdynvc->context;
IFCALL(context->OnChannelConnected, context, ChannelName, listener->iface.pInterface);
@ -332,6 +412,7 @@ int dvcman_create_channel(IWTSVirtualChannelManager *pChannelMgr, UINT32 Channel
}
}
}
free(channel);
return 1;
}
@ -342,25 +423,31 @@ int dvcman_close_channel(IWTSVirtualChannelManager *pChannelMgr, UINT32 ChannelI
IWTSVirtualChannel *ichannel;
DrdynvcClientContext *context;
DVCMAN *dvcman = (DVCMAN *) pChannelMgr;
assert(dvcman);
channel = (DVCMAN_CHANNEL *) dvcman_find_channel_by_id(pChannelMgr, ChannelId);
if(!channel)
if (!channel)
{
DEBUG_WARN("ChannelId %d not found!", ChannelId);
return 1;
}
if(channel->dvc_data)
if (channel->dvc_data)
{
Stream_Free(channel->dvc_data, TRUE);
channel->dvc_data = NULL;
}
if(channel->status == 0)
if (channel->status == 0)
{
assert(dvcman->drdynvc);
context = dvcman->drdynvc->context;
IFCALL(context->OnChannelDisconnected, context, channel->channel_name, channel->pInterface);
DEBUG_DVC("dvcman_close_channel: channel %d closed", ChannelId);
ichannel = (IWTSVirtualChannel *) channel;
ichannel->Close(ichannel);
}
return 0;
}
@ -368,13 +455,16 @@ int dvcman_receive_channel_data_first(IWTSVirtualChannelManager *pChannelMgr, UI
{
DVCMAN_CHANNEL *channel;
channel = (DVCMAN_CHANNEL *) dvcman_find_channel_by_id(pChannelMgr, ChannelId);
if(!channel)
if (!channel)
{
DEBUG_WARN("ChannelId %d not found!", ChannelId);
return 1;
}
if(channel->dvc_data)
if (channel->dvc_data)
Stream_Free(channel->dvc_data, TRUE);
channel->dvc_data = Stream_New(NULL, length);
return 0;
}
@ -384,23 +474,27 @@ int dvcman_receive_channel_data(IWTSVirtualChannelManager *pChannelMgr, UINT32 C
int error = 0;
DVCMAN_CHANNEL *channel;
channel = (DVCMAN_CHANNEL *) dvcman_find_channel_by_id(pChannelMgr, ChannelId);
if(!channel)
if (!channel)
{
DEBUG_WARN("ChannelId %d not found!", ChannelId);
return 1;
}
if(channel->dvc_data)
if (channel->dvc_data)
{
/* Fragmented data */
if(Stream_GetPosition(channel->dvc_data) + data_size > (UINT32) Stream_Capacity(channel->dvc_data))
if (Stream_GetPosition(channel->dvc_data) + data_size > (UINT32) Stream_Capacity(channel->dvc_data))
{
DEBUG_WARN("data exceeding declared length!");
Stream_Free(channel->dvc_data, TRUE);
channel->dvc_data = NULL;
return 1;
}
Stream_Write(channel->dvc_data, data, data_size);
if(((size_t) Stream_GetPosition(channel->dvc_data)) >= Stream_Capacity(channel->dvc_data))
if (((size_t) Stream_GetPosition(channel->dvc_data)) >= Stream_Capacity(channel->dvc_data))
{
error = channel->channel_callback->OnDataReceived(channel->channel_callback,
Stream_Capacity(channel->dvc_data), Stream_Buffer(channel->dvc_data));
@ -410,8 +504,11 @@ int dvcman_receive_channel_data(IWTSVirtualChannelManager *pChannelMgr, UINT32 C
}
else
{
assert(channel->channel_callback);
assert(channel->channel_callback->OnDataReceived);
error = channel->channel_callback->OnDataReceived(channel->channel_callback, data_size, data);
}
return error;
}
@ -419,13 +516,16 @@ static void dump_callbacks(void)
{
wListDictionaryItem *cur;
DEBUG_DVC("Dumping all currently registered callbacks");
if(!cb_dict)
if (!cb_dict)
{
DEBUG_DVC("cb_dict=NULL");
return;
}
cur = cb_dict->head;
while(cur)
while (cur)
{
DEBUG_DVC("cb_dict %s:%p", cur->key, cur->value);
cur = cur->next;
@ -435,21 +535,26 @@ static void dump_callbacks(void)
void *get_callback_by_name(const char *name, void **context)
{
struct cb_value *rc;
if(!cb_dict)
if (!cb_dict)
{
DEBUG_WARN("'%s' not found, function list does not exist.",
name);
return NULL;
}
if(!ListDictionary_Contains(cb_dict, (void *)name))
if (!ListDictionary_Contains(cb_dict, (void *)name))
{
DEBUG_WARN("'%s' not found", name);
return NULL;
}
rc = ListDictionary_GetItemValue(cb_dict, (void *)name);
DEBUG_DVC("'%s'=%p found", name, rc);
assert(context);
*context = rc->context;
if (context)
*context = rc->context;
return rc->fkt;
}
@ -461,12 +566,21 @@ static BOOL callback_key_cmp(void *a, void *b)
void add_callback_by_name(const char *name, void *fkt, void *context)
{
struct cb_value *value = calloc(1, sizeof(struct cb_value));
if(!cb_dict)
if (!value)
{
DEBUG_WARN("calloc failed %s (%d)!", strerror(errno), errno);
assert(FALSE);
return;
}
if (!cb_dict)
{
DEBUG_DVC("Function list is empty, allocating new.");
cb_dict = ListDictionary_New(TRUE);
ListDictionary_KeyObject(cb_dict)->fnObjectEquals = callback_key_cmp;
}
value->fkt = fkt;
value->context = context;
DEBUG_DVC("Adding '%s'=%p to function list.", name, fkt);
@ -476,25 +590,29 @@ void add_callback_by_name(const char *name, void *fkt, void *context)
void remove_callback_by_name(const char *name, void *context)
{
if(!cb_dict)
if (!cb_dict)
{
DEBUG_WARN("trying to remove '%s', but function list does not exist.",
name);
return;
}
if(!ListDictionary_Contains(cb_dict, (void *)name))
if (!ListDictionary_Contains(cb_dict, (void *)name))
{
DEBUG_WARN("trying to remove '%s', which is not in function list.",
name);
return;
}
DEBUG_DVC("Removing '%s' from function list.", name);
ListDictionary_Remove(cb_dict, (void *)name);
if(ListDictionary_Count(cb_dict) < 1)
if (ListDictionary_Count(cb_dict) < 1)
{
DEBUG_DVC("Function list is empty, freeing resources.");
ListDictionary_Free(cb_dict);
cb_dict = NULL;
}
dump_callbacks();
}