FreeRDP/libfreerdp/utils/svc_plugin.c

444 lines
10 KiB
C
Raw Normal View History

2011-07-09 19:29:04 +04:00
/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
2011-07-09 19:29:04 +04:00
* Static Virtual Channel Interface
*
* Copyright 2009-2011 Jay Sorg
* Copyright 2010-2011 Vic Lee
*
* 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.
*/
#ifdef HAVE_CONFIG_H
2011-07-09 19:29:04 +04:00
#include "config.h"
#endif
2011-07-09 19:29:04 +04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <freerdp/constants.h>
2011-07-09 19:29:04 +04:00
#include <freerdp/utils/memory.h>
#include <freerdp/utils/debug.h>
#include <freerdp/utils/stream.h>
#include <freerdp/utils/list.h>
#include <freerdp/utils/thread.h>
#include <freerdp/utils/event.h>
2011-07-09 19:29:04 +04:00
#include <freerdp/utils/svc_plugin.h>
/* The list of all plugin instances. */
typedef struct rdp_svc_plugin_list rdpSvcPluginList;
2011-07-09 19:29:04 +04:00
struct rdp_svc_plugin_list
{
rdpSvcPlugin* plugin;
rdpSvcPluginList* next;
};
static rdpSvcPluginList* g_svc_plugin_list = NULL;
/* For locking the global resources */
static HANDLE g_mutex = NULL;
2011-07-09 19:29:04 +04:00
/* Queue for receiving packets */
struct _svc_data_in_item
{
STREAM* data_in;
2011-08-18 01:28:26 +04:00
RDP_EVENT* event_in;
};
typedef struct _svc_data_in_item svc_data_in_item;
static void svc_data_in_item_free(svc_data_in_item* item)
{
if (item->data_in)
{
stream_free(item->data_in);
item->data_in = NULL;
}
if (item->event_in)
{
freerdp_event_free(item->event_in);
item->event_in = NULL;
}
free(item);
}
2011-07-09 19:29:04 +04:00
struct rdp_svc_plugin_private
{
void* init_handle;
2012-10-09 11:26:39 +04:00
UINT32 open_handle;
STREAM* data_in;
LIST* data_in_list;
freerdp_thread* thread;
2011-07-09 19:29:04 +04:00
};
static rdpSvcPlugin* svc_plugin_find_by_init_handle(void* init_handle)
{
rdpSvcPluginList* list;
rdpSvcPlugin* plugin;
WaitForSingleObject(g_mutex, INFINITE);
2011-07-09 19:29:04 +04:00
for (list = g_svc_plugin_list; list; list = list->next)
{
plugin = list->plugin;
2011-07-09 19:29:04 +04:00
if (plugin->priv->init_handle == init_handle)
{
ReleaseMutex(g_mutex);
2011-07-09 19:29:04 +04:00
return plugin;
}
}
ReleaseMutex(g_mutex);
2011-07-09 19:29:04 +04:00
return NULL;
}
2012-10-09 11:26:39 +04:00
static rdpSvcPlugin* svc_plugin_find_by_open_handle(UINT32 open_handle)
2011-07-09 19:29:04 +04:00
{
rdpSvcPluginList * list;
rdpSvcPlugin * plugin;
WaitForSingleObject(g_mutex, INFINITE);
2011-07-09 19:29:04 +04:00
for (list = g_svc_plugin_list; list; list = list->next)
{
plugin = list->plugin;
2011-07-09 19:29:04 +04:00
if (plugin->priv->open_handle == open_handle)
{
ReleaseMutex(g_mutex);
2011-07-09 19:29:04 +04:00
return plugin;
}
}
ReleaseMutex(g_mutex);
2011-07-09 19:29:04 +04:00
return NULL;
}
static void svc_plugin_remove(rdpSvcPlugin* plugin)
{
rdpSvcPluginList* list;
rdpSvcPluginList* prev;
/* Remove from global list */
WaitForSingleObject(g_mutex, INFINITE);
2011-07-09 19:29:04 +04:00
for (prev = NULL, list = g_svc_plugin_list; list; prev = list, list = list->next)
{
if (list->plugin == plugin)
break;
}
if (list)
{
if (prev)
prev->next = list->next;
else
g_svc_plugin_list = list->next;
free(list);
2011-07-09 19:29:04 +04:00
}
ReleaseMutex(g_mutex);
2011-07-09 19:29:04 +04:00
}
2012-10-09 11:26:39 +04:00
static void svc_plugin_process_received(rdpSvcPlugin* plugin, void* pData, UINT32 dataLength,
UINT32 totalLength, UINT32 dataFlags)
2011-07-09 19:29:04 +04:00
{
STREAM* data_in;
svc_data_in_item* item;
if ( (dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
{
/* According to MS-RDPBCGR 2.2.6.1, "All virtual channel traffic MUST be suspended.
This flag is only valid in server-to-client virtual channel traffic. It MUST be
ignored in client-to-server data." Thus it would be best practice to cease data
transmission. However, simply returning here avoids a crash. */
return;
}
if (dataFlags & CHANNEL_FLAG_FIRST)
{
if (plugin->priv->data_in != NULL)
stream_free(plugin->priv->data_in);
plugin->priv->data_in = stream_new(totalLength);
}
data_in = plugin->priv->data_in;
2011-08-17 05:08:14 +04:00
stream_check_size(data_in, (int) dataLength);
stream_write(data_in, pData, dataLength);
if (dataFlags & CHANNEL_FLAG_LAST)
{
if (stream_get_size(data_in) != stream_get_length(data_in))
{
printf("svc_plugin_process_received: read error\n");
}
plugin->priv->data_in = NULL;
stream_set_pos(data_in, 0);
item = xnew(svc_data_in_item);
item->data_in = data_in;
freerdp_thread_lock(plugin->priv->thread);
list_enqueue(plugin->priv->data_in_list, item);
freerdp_thread_unlock(plugin->priv->thread);
freerdp_thread_signal(plugin->priv->thread);
}
2011-07-09 19:29:04 +04:00
}
2011-08-18 01:28:26 +04:00
static void svc_plugin_process_event(rdpSvcPlugin* plugin, RDP_EVENT* event_in)
{
svc_data_in_item* item;
item = xnew(svc_data_in_item);
item->event_in = event_in;
freerdp_thread_lock(plugin->priv->thread);
list_enqueue(plugin->priv->data_in_list, item);
freerdp_thread_unlock(plugin->priv->thread);
freerdp_thread_signal(plugin->priv->thread);
}
2012-10-09 11:26:39 +04:00
static void svc_plugin_open_event(UINT32 openHandle, UINT32 event, void* pData, UINT32 dataLength,
UINT32 totalLength, UINT32 dataFlags)
2011-07-09 19:29:04 +04:00
{
rdpSvcPlugin* plugin;
DEBUG_SVC("openHandle %d event %d dataLength %d totalLength %d dataFlags %d",
openHandle, event, dataLength, totalLength, dataFlags);
2011-07-09 19:29:04 +04:00
plugin = (rdpSvcPlugin*) svc_plugin_find_by_open_handle(openHandle);
2011-07-09 19:29:04 +04:00
if (plugin == NULL)
{
printf("svc_plugin_open_event: error no match\n");
return;
}
switch (event)
2011-07-09 19:29:04 +04:00
{
case CHANNEL_EVENT_DATA_RECEIVED:
svc_plugin_process_received(plugin, pData, dataLength, totalLength, dataFlags);
break;
case CHANNEL_EVENT_WRITE_COMPLETE:
stream_free((STREAM*) pData);
break;
case CHANNEL_EVENT_USER:
svc_plugin_process_event(plugin, (RDP_EVENT*) pData);
break;
2011-07-09 19:29:04 +04:00
}
}
static void svc_plugin_process_data_in(rdpSvcPlugin* plugin)
{
svc_data_in_item* item;
while (1)
{
/* terminate signal */
if (freerdp_thread_is_stopped(plugin->priv->thread))
break;
freerdp_thread_lock(plugin->priv->thread);
item = list_dequeue(plugin->priv->data_in_list);
freerdp_thread_unlock(plugin->priv->thread);
if (item != NULL)
{
/* the ownership of the data is passed to the callback */
if (item->data_in)
IFCALL(plugin->receive_callback, plugin, item->data_in);
if (item->event_in)
IFCALL(plugin->event_callback, plugin, item->event_in);
free(item);
}
else
break;
}
}
static void* svc_plugin_thread_func(void* arg)
{
rdpSvcPlugin* plugin = (rdpSvcPlugin*)arg;
DEBUG_SVC("in");
IFCALL(plugin->connect_callback, plugin);
while (1)
{
if (plugin->interval_ms > 0)
freerdp_thread_wait_timeout(plugin->priv->thread, plugin->interval_ms);
else
freerdp_thread_wait(plugin->priv->thread);
if (freerdp_thread_is_stopped(plugin->priv->thread))
break;
freerdp_thread_reset(plugin->priv->thread);
svc_plugin_process_data_in(plugin);
if (plugin->interval_ms > 0)
IFCALL(plugin->interval_callback, plugin);
}
freerdp_thread_quit(plugin->priv->thread);
DEBUG_SVC("out");
return 0;
}
2012-10-09 11:26:39 +04:00
static void svc_plugin_process_connected(rdpSvcPlugin* plugin, void* pData, UINT32 dataLength)
2011-07-09 19:29:04 +04:00
{
2012-10-09 11:26:39 +04:00
UINT32 error;
2011-07-09 19:29:04 +04:00
error = plugin->channel_entry_points.pVirtualChannelOpen(plugin->priv->init_handle,
&plugin->priv->open_handle, plugin->channel_def.name, svc_plugin_open_event);
2011-07-09 19:29:04 +04:00
if (error != CHANNEL_RC_OK)
{
printf("svc_plugin_process_connected: open failed\n");
return;
}
plugin->priv->data_in_list = list_new();
plugin->priv->thread = freerdp_thread_new();
freerdp_thread_start(plugin->priv->thread, svc_plugin_thread_func, plugin);
2011-07-09 19:29:04 +04:00
}
static void svc_plugin_process_terminated(rdpSvcPlugin* plugin)
{
svc_data_in_item* item;
freerdp_thread_stop(plugin->priv->thread);
freerdp_thread_free(plugin->priv->thread);
2011-07-09 19:29:04 +04:00
plugin->channel_entry_points.pVirtualChannelClose(plugin->priv->open_handle);
//free(plugin->channel_entry_points.pExtendedData);
2011-07-09 19:29:04 +04:00
svc_plugin_remove(plugin);
while ((item = list_dequeue(plugin->priv->data_in_list)) != NULL)
svc_data_in_item_free(item);
list_free(plugin->priv->data_in_list);
if (plugin->priv->data_in != NULL)
{
stream_free(plugin->priv->data_in);
plugin->priv->data_in = NULL;
}
free(plugin->priv);
2011-07-09 19:29:04 +04:00
plugin->priv = NULL;
IFCALL(plugin->terminate_callback, plugin);
2011-07-09 19:29:04 +04:00
}
2012-10-09 11:26:39 +04:00
static void svc_plugin_init_event(void* pInitHandle, UINT32 event, void* pData, UINT32 dataLength)
2011-07-09 19:29:04 +04:00
{
rdpSvcPlugin* plugin;
DEBUG_SVC("event %d", event);
plugin = (rdpSvcPlugin*) svc_plugin_find_by_init_handle(pInitHandle);
if (!plugin)
2011-07-09 19:29:04 +04:00
{
printf("svc_plugin_init_event: error no match\n");
return;
}
2011-07-09 19:29:04 +04:00
switch (event)
{
case CHANNEL_EVENT_CONNECTED:
svc_plugin_process_connected(plugin, pData, dataLength);
break;
2011-07-09 19:29:04 +04:00
case CHANNEL_EVENT_DISCONNECTED:
break;
2011-07-09 19:29:04 +04:00
case CHANNEL_EVENT_TERMINATED:
svc_plugin_process_terminated(plugin);
break;
}
}
void svc_plugin_init(rdpSvcPlugin* plugin, CHANNEL_ENTRY_POINTS* pEntryPoints)
2011-07-09 19:29:04 +04:00
{
rdpSvcPluginList* list;
/**
* The channel manager will guarantee only one thread can call
* VirtualChannelInit at a time. So this should be safe.
*/
if (g_mutex == NULL)
g_mutex = CreateMutex(NULL, FALSE, NULL);
2011-07-09 19:29:04 +04:00
memcpy(&plugin->channel_entry_points, pEntryPoints, pEntryPoints->cbSize);
2011-07-13 18:23:55 +04:00
plugin->priv = xnew(rdpSvcPluginPrivate);
2011-07-09 19:29:04 +04:00
/* Add it to the global list */
2011-07-13 18:23:55 +04:00
list = xnew(rdpSvcPluginList);
2011-07-09 19:29:04 +04:00
list->plugin = plugin;
WaitForSingleObject(g_mutex, INFINITE);
2011-07-09 19:29:04 +04:00
list->next = g_svc_plugin_list;
g_svc_plugin_list = list;
ReleaseMutex(g_mutex);
2011-07-09 19:29:04 +04:00
plugin->channel_entry_points.pVirtualChannelInit(&plugin->priv->init_handle,
&plugin->channel_def, 1, VIRTUAL_CHANNEL_VERSION_WIN2000, svc_plugin_init_event);
}
int svc_plugin_send(rdpSvcPlugin* plugin, STREAM* data_out)
2011-07-09 19:29:04 +04:00
{
2012-10-09 11:26:39 +04:00
UINT32 error = 0;
2011-07-09 19:29:04 +04:00
2012-09-08 23:27:00 +04:00
DEBUG_SVC("length %d", (int) stream_get_length(data_out));
2011-07-09 19:29:04 +04:00
2012-04-25 22:27:38 +04:00
if (!plugin || !plugin->priv)
error = CHANNEL_RC_BAD_INIT_HANDLE;
else
error = plugin->channel_entry_points.pVirtualChannelWrite(plugin->priv->open_handle,
stream_get_data(data_out), stream_get_length(data_out), data_out);
2011-07-09 19:29:04 +04:00
if (error != CHANNEL_RC_OK)
{
stream_free(data_out);
printf("svc_plugin_send: VirtualChannelWrite failed %d\n", error);
}
return error;
}
2011-08-18 01:28:26 +04:00
int svc_plugin_send_event(rdpSvcPlugin* plugin, RDP_EVENT* event)
{
2012-10-09 11:26:39 +04:00
UINT32 error = 0;
DEBUG_SVC("event_type %d", event->event_type);
2011-08-10 10:24:06 +04:00
error = plugin->channel_entry_points.pVirtualChannelEventPush(plugin->priv->open_handle, event);
if (error != CHANNEL_RC_OK)
printf("svc_plugin_send_event: VirtualChannelEventPush failed %d\n", error);
2011-07-09 19:29:04 +04:00
return error;
}
2012-10-03 00:32:41 +04:00