FreeRDP/libfreerdp-channels/libchannels.c

988 lines
23 KiB
C
Raw Normal View History

2011-07-08 17:36:40 +04:00
/**
* FreeRDP: A Remote Desktop Protocol client.
* Virtual Channel Manager
*
* 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.
*/
/**
* MS compatible plugin interface
* reference:
* http://msdn.microsoft.com/en-us/library/aa383580.aspx
*
* Notes on threads:
* Many virtual channel plugins are built using threads.
* Non main threads may call MyVirtualChannelOpen,
* MyVirtualChannelClose, or MyVirtualChannelWrite.
* Since the plugin's VirtualChannelEntry function is called
* from the main thread, MyVirtualChannelInit has to be called
* from the main thread.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <freerdp/freerdp.h>
#include <freerdp/constants.h>
#include <freerdp/channels/channels.h>
2011-07-08 17:36:40 +04:00
#include <freerdp/svc.h>
#include <freerdp/utils/memory.h>
#include <freerdp/utils/list.h>
#include <freerdp/utils/semaphore.h>
#include <freerdp/utils/mutex.h>
#include <freerdp/utils/wait_obj.h>
#include <freerdp/utils/load_plugin.h>
#include <freerdp/utils/event.h>
2011-07-08 17:36:40 +04:00
#include "libchannels.h"
2011-07-08 17:36:40 +04:00
#define CHANNEL_MAX_COUNT 30
struct lib_data
{
PVIRTUALCHANNELENTRY entry; /* the one and only exported function */
PCHANNEL_INIT_EVENT_FN init_event_proc;
void* init_handle;
};
struct channel_data
2011-07-08 17:36:40 +04:00
{
char name[CHANNEL_NAME_LEN + 1];
int open_handle;
int options;
int flags; /* 0 nothing 1 init 2 open */
PCHANNEL_OPEN_EVENT_FN open_event_proc;
};
struct sync_data
{
void* data;
uint32 data_length;
void* user_data;
int index;
};
2011-07-08 17:36:40 +04:00
typedef struct rdp_init_handle rdpInitHandle;
struct rdp_init_handle
{
2012-02-10 01:57:14 +04:00
rdpChannels* channels;
2011-07-08 17:36:40 +04:00
};
struct rdp_channels
2011-07-08 17:36:40 +04:00
{
/**
* Only the main thread alters these arrays, before any
* library thread is allowed in(post_connect is called)
* so no need to use mutex locking
* After post_connect, each library thread can only access it's
* own array items
* ie, no two threads can access index 0, ...
*/
2012-02-10 01:57:14 +04:00
struct lib_data libs_data[CHANNEL_MAX_COUNT];
int num_libs_data;
struct channel_data channels_data[CHANNEL_MAX_COUNT];
int num_channels_data;
2011-07-08 17:36:40 +04:00
rdpInitHandle init_handles[CHANNEL_MAX_COUNT];
int num_init_handles;
/* control for entry into MyVirtualChannelInit */
int can_call_init;
rdpSettings* settings;
/* true once freerdp_chanman_post_connect is called */
int is_connected;
2012-02-10 01:57:14 +04:00
/* used for locating the channels for a given instance */
freerdp* instance;
2011-07-08 17:36:40 +04:00
/* signal for incoming data or event */
struct wait_obj* signal;
/* used for sync write */
freerdp_mutex sync_data_mutex;
LIST* sync_data_list;
2011-07-08 17:36:40 +04:00
/* used for sync event */
freerdp_sem event_sem;
2011-08-18 01:28:26 +04:00
RDP_EVENT* event;
2011-07-08 17:36:40 +04:00
};
/**
* The current channel manager reference passes from VirtualChannelEntry to
* VirtualChannelInit for the pInitHandle.
*/
2012-02-10 01:57:14 +04:00
static rdpChannels* g_init_channels;
2011-07-08 17:36:40 +04:00
/* The list of all channel managers. */
typedef struct rdp_channels_list rdpChannelsList;
struct rdp_channels_list
2011-07-08 17:36:40 +04:00
{
rdpChannels* channels;
rdpChannelsList* next;
2011-07-08 17:36:40 +04:00
};
static rdpChannelsList* g_channels_list;
2011-07-08 17:36:40 +04:00
/* To generate unique sequence for all open handles */
static int g_open_handle_sequence;
/* For locking the global resources */
static freerdp_mutex g_mutex_init;
static freerdp_mutex g_mutex_list;
2012-02-10 01:57:14 +04:00
/* returns the channels for the open handle passed in */
static rdpChannels* freerdp_channels_find_by_open_handle(int open_handle, int* pindex)
2011-07-08 17:36:40 +04:00
{
int lindex;
2012-02-10 01:57:14 +04:00
rdpChannels* channels;
rdpChannelsList* channels_list;
2011-07-08 17:36:40 +04:00
freerdp_mutex_lock(g_mutex_list);
2012-02-10 01:57:14 +04:00
for (channels_list = g_channels_list; channels_list; channels_list = channels_list->next)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
channels = channels_list->channels;
for (lindex = 0; lindex < channels->num_channels_data; lindex++)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
if (channels->channels_data[lindex].open_handle == open_handle)
2011-07-08 17:36:40 +04:00
{
freerdp_mutex_unlock(g_mutex_list);
*pindex = lindex;
2012-02-10 01:57:14 +04:00
return channels;
2011-07-08 17:36:40 +04:00
}
}
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
freerdp_mutex_unlock(g_mutex_list);
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
return NULL;
}
2012-02-10 01:57:14 +04:00
/* returns the channels for the rdp instance passed in */
static rdpChannels* freerdp_channels_find_by_instance(freerdp* instance)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
rdpChannels* channels;
rdpChannelsList* channels_list;
2011-07-08 17:36:40 +04:00
freerdp_mutex_lock(g_mutex_list);
2012-02-10 01:57:14 +04:00
for (channels_list = g_channels_list; channels_list; channels_list = channels_list->next)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
channels = channels_list->channels;
if (channels->instance == instance)
2011-07-08 17:36:40 +04:00
{
freerdp_mutex_unlock(g_mutex_list);
2012-02-10 01:57:14 +04:00
return channels;
2011-07-08 17:36:40 +04:00
}
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
freerdp_mutex_unlock(g_mutex_list);
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
return NULL;
}
2012-02-10 01:57:14 +04:00
/* returns struct channel_data for the channel name passed in */
static struct channel_data* freerdp_channels_find_channel_data_by_name(rdpChannels* channels, const char* channel_name, int* pindex)
2011-07-08 17:36:40 +04:00
{
int lindex;
2012-02-10 01:57:14 +04:00
struct channel_data* lchannel_data;
2011-07-08 17:36:40 +04:00
2012-02-10 01:57:14 +04:00
for (lindex = 0; lindex < channels->num_channels_data; lindex++)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
lchannel_data = channels->channels_data + lindex;
if (strcmp(channel_name, lchannel_data->name) == 0)
2011-07-08 17:36:40 +04:00
{
if (pindex != 0)
*pindex = lindex;
2012-02-10 01:57:14 +04:00
return lchannel_data;
2011-07-08 17:36:40 +04:00
}
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
return NULL;
}
2012-02-10 01:57:14 +04:00
/* returns rdpChannel for the channel id passed in */
static rdpChannel* freerdp_channels_find_channel_by_id(rdpChannels* channels, rdpSettings* settings, int channel_id, int* pindex)
2011-07-08 17:36:40 +04:00
{
int lindex;
int lcount;
2012-02-10 01:57:14 +04:00
rdpChannel* lrdp_channel;
2011-07-08 17:36:40 +04:00
lcount = settings->num_channels;
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
for (lindex = 0; lindex < lcount; lindex++)
{
2012-02-10 01:57:14 +04:00
lrdp_channel = settings->channels + lindex;
if (lrdp_channel->channel_id == channel_id)
2011-07-08 17:36:40 +04:00
{
if (pindex != 0)
*pindex = lindex;
2012-02-10 01:57:14 +04:00
return lrdp_channel;
2011-07-08 17:36:40 +04:00
}
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
return NULL;
}
2012-02-10 01:57:14 +04:00
/* returns rdpChannel for the channel name passed in */
static rdpChannel* freerdp_channels_find_channel_by_name(rdpChannels* channels,
rdpSettings* settings, const char* channel_name, int* pindex)
2011-07-08 17:36:40 +04:00
{
int lindex;
int lcount;
2012-02-10 01:57:14 +04:00
rdpChannel* lrdp_channel;
2011-07-08 17:36:40 +04:00
lcount = settings->num_channels;
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
for (lindex = 0; lindex < lcount; lindex++)
{
2012-02-10 01:57:14 +04:00
lrdp_channel = settings->channels + lindex;
if (strcmp(channel_name, lrdp_channel->name) == 0)
2011-07-08 17:36:40 +04:00
{
if (pindex != 0)
*pindex = lindex;
2012-02-10 01:57:14 +04:00
return lrdp_channel;
2011-07-08 17:36:40 +04:00
}
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
return NULL;
}
/**
* must be called by same thread that calls freerdp_chanman_load_plugin
* according to MS docs
* only called from main thread
*/
static uint32 FREERDP_CC MyVirtualChannelInit(void** ppInitHandle, PCHANNEL_DEF pChannel,
2012-02-10 01:57:14 +04:00
int channelCount, uint32 versionRequested, PCHANNEL_INIT_EVENT_FN pChannelInitEventProc)
2011-07-08 17:36:40 +04:00
{
int index;
2012-02-10 01:57:14 +04:00
rdpChannels* channels;
2011-07-08 17:36:40 +04:00
struct lib_data* llib;
2012-02-10 01:57:14 +04:00
rdpChannel* lrdp_channel;
PCHANNEL_DEF lchannel_def;
struct channel_data* lchannel_data;
2011-07-08 17:36:40 +04:00
2012-02-10 01:57:14 +04:00
channels = g_init_channels;
channels->init_handles[channels->num_init_handles].channels = channels;
*ppInitHandle = &channels->init_handles[channels->num_init_handles];
channels->num_init_handles++;
2011-07-08 17:36:40 +04:00
DEBUG_CHANNELS("enter");
2012-02-10 01:57:14 +04:00
if (!channels->can_call_init)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error not in entry");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_NOT_IN_VIRTUALCHANNELENTRY;
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
if (ppInitHandle == 0)
{
DEBUG_CHANNELS("error bad pphan");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_BAD_INIT_HANDLE;
}
2012-02-10 01:57:14 +04:00
if (channels->num_channels_data + channelCount >= CHANNEL_MAX_COUNT)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error too many channels");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_TOO_MANY_CHANNELS;
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
if (pChannel == 0)
{
DEBUG_CHANNELS("error bad pchan");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_BAD_CHANNEL;
}
2012-02-10 01:57:14 +04:00
if (channels->is_connected)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error already connected");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_ALREADY_CONNECTED;
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
if (versionRequested != VIRTUAL_CHANNEL_VERSION_WIN2000)
{
DEBUG_CHANNELS("warning version");
2011-07-08 17:36:40 +04:00
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
for (index = 0; index < channelCount; index++)
{
2012-02-10 01:57:14 +04:00
lchannel_def = pChannel + index;
if (freerdp_channels_find_channel_data_by_name(channels, lchannel_def->name, 0) != 0)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error channel already used");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_BAD_CHANNEL;
}
}
2012-02-10 01:57:14 +04:00
llib = channels->libs_data + channels->num_libs_data;
2011-07-08 17:36:40 +04:00
llib->init_event_proc = pChannelInitEventProc;
llib->init_handle = *ppInitHandle;
2012-02-10 01:57:14 +04:00
channels->num_libs_data++;
2011-07-08 17:36:40 +04:00
for (index = 0; index < channelCount; index++)
{
2012-02-10 01:57:14 +04:00
lchannel_def = pChannel + index;
lchannel_data = channels->channels_data + channels->num_channels_data;
2011-07-08 17:36:40 +04:00
freerdp_mutex_lock(g_mutex_list);
2012-02-10 01:57:14 +04:00
lchannel_data->open_handle = g_open_handle_sequence++;
2011-07-08 17:36:40 +04:00
freerdp_mutex_unlock(g_mutex_list);
2012-02-10 01:57:14 +04:00
lchannel_data->flags = 1; /* init */
strncpy(lchannel_data->name, lchannel_def->name, CHANNEL_NAME_LEN);
lchannel_data->options = lchannel_def->options;
if (channels->settings->num_channels < 16)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
lrdp_channel = channels->settings->channels + channels->settings->num_channels;
strncpy(lrdp_channel->name, lchannel_def->name, 7);
lrdp_channel->options = lchannel_def->options;
channels->settings->num_channels++;
2011-07-08 17:36:40 +04:00
}
else
{
DEBUG_CHANNELS("warning more than 16 channels");
2011-07-08 17:36:40 +04:00
}
2012-02-10 01:57:14 +04:00
channels->num_channels_data++;
2011-07-08 17:36:40 +04:00
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_OK;
}
/**
* can be called from any thread
* thread safe because no 2 threads can have the same channel name registered
*/
static uint32 FREERDP_CC MyVirtualChannelOpen(void* pInitHandle, uint32* pOpenHandle,
char* pChannelName, PCHANNEL_OPEN_EVENT_FN pChannelOpenEventProc)
{
int index;
2012-02-10 01:57:14 +04:00
rdpChannels* channels;
struct channel_data* lchannel_data;
2011-07-08 17:36:40 +04:00
DEBUG_CHANNELS("enter");
2012-02-10 01:57:14 +04:00
channels = ((rdpInitHandle*) pInitHandle)->channels;
2011-07-08 17:36:40 +04:00
if (pOpenHandle == 0)
{
DEBUG_CHANNELS("error bad chanhan");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_BAD_CHANNEL_HANDLE;
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
if (pChannelOpenEventProc == 0)
{
DEBUG_CHANNELS("error bad proc");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_BAD_PROC;
}
2012-02-10 01:57:14 +04:00
if (!channels->is_connected)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error not connected");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_NOT_CONNECTED;
}
2012-02-10 01:57:14 +04:00
lchannel_data = freerdp_channels_find_channel_data_by_name(channels, pChannelName, &index);
if (lchannel_data == 0)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error chan name");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_UNKNOWN_CHANNEL_NAME;
}
2012-02-10 01:57:14 +04:00
if (lchannel_data->flags == 2)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error chan already open");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_ALREADY_OPEN;
}
2012-02-10 01:57:14 +04:00
lchannel_data->flags = 2; /* open */
lchannel_data->open_event_proc = pChannelOpenEventProc;
*pOpenHandle = lchannel_data->open_handle;
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_OK;
}
/**
* can be called from any thread
* thread safe because no 2 threads can have the same openHandle
*/
static uint32 FREERDP_CC MyVirtualChannelClose(uint32 openHandle)
{
int index;
2012-02-10 01:57:14 +04:00
rdpChannels* channels;
struct channel_data* lchannel_data;
2011-07-08 17:36:40 +04:00
DEBUG_CHANNELS("enter");
2012-02-10 01:57:14 +04:00
channels = freerdp_channels_find_by_open_handle(openHandle, &index);
if ((channels == NULL) || (index < 0) || (index >= CHANNEL_MAX_COUNT))
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
DEBUG_CHANNELS("error bad channels");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_BAD_CHANNEL_HANDLE;
}
2012-02-10 01:57:14 +04:00
lchannel_data = channels->channels_data + index;
if (lchannel_data->flags != 2)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error not open");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_NOT_OPEN;
}
2012-02-10 01:57:14 +04:00
lchannel_data->flags = 0;
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_OK;
}
/* can be called from any thread */
2012-02-10 01:57:14 +04:00
static uint32 FREERDP_CC MyVirtualChannelWrite(uint32 openHandle, void* pData, uint32 dataLength, void* pUserData)
2011-07-08 17:36:40 +04:00
{
int index;
2012-02-10 01:57:14 +04:00
rdpChannels* channels;
struct sync_data* item;
struct channel_data* lchannel_data;
channels = freerdp_channels_find_by_open_handle(openHandle, &index);
2011-07-08 17:36:40 +04:00
2012-02-10 01:57:14 +04:00
if ((channels == NULL) || (index < 0) || (index >= CHANNEL_MAX_COUNT))
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error bad chanhan");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_BAD_CHANNEL_HANDLE;
}
2012-02-10 01:57:14 +04:00
if (!channels->is_connected)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error not connected");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_NOT_CONNECTED;
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
if (pData == 0)
{
DEBUG_CHANNELS("error bad pData");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_NULL_DATA;
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
if (dataLength == 0)
{
DEBUG_CHANNELS("error bad dataLength");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_ZERO_LENGTH;
}
2012-02-10 01:57:14 +04:00
lchannel_data = channels->channels_data + index;
if (lchannel_data->flags != 2)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error not open");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_NOT_OPEN;
}
2012-02-10 01:57:14 +04:00
freerdp_mutex_lock(channels->sync_data_mutex); /* lock channels->sync* vars */
if (!channels->is_connected)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
freerdp_mutex_unlock(channels->sync_data_mutex);
DEBUG_CHANNELS("error not connected");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_NOT_CONNECTED;
}
2012-02-10 01:57:14 +04:00
item = xnew(struct sync_data);
item->data = pData;
item->data_length = dataLength;
item->user_data = pUserData;
item->index = index;
2012-02-10 01:57:14 +04:00
list_enqueue(channels->sync_data_list, item);
freerdp_mutex_unlock(channels->sync_data_mutex);
2011-07-08 17:36:40 +04:00
/* set the event */
2012-02-10 01:57:14 +04:00
wait_obj_set(channels->signal);
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_OK;
}
2011-08-18 01:28:26 +04:00
static uint32 FREERDP_CC MyVirtualChannelEventPush(uint32 openHandle, RDP_EVENT* event)
2011-07-08 17:36:40 +04:00
{
int index;
2012-02-10 01:57:14 +04:00
rdpChannels* channels;
struct channel_data* lchannel_data;
channels = freerdp_channels_find_by_open_handle(openHandle, &index);
2011-07-08 17:36:40 +04:00
2012-02-10 01:57:14 +04:00
if ((channels == NULL) || (index < 0) || (index >= CHANNEL_MAX_COUNT))
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error bad chanhan");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_BAD_CHANNEL_HANDLE;
}
2012-02-10 01:57:14 +04:00
if (!channels->is_connected)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error not connected");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_NOT_CONNECTED;
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
if (event == NULL)
{
DEBUG_CHANNELS("error bad event");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_NULL_DATA;
}
2012-02-10 01:57:14 +04:00
lchannel_data = channels->channels_data + index;
if (lchannel_data->flags != 2)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("error not open");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_NOT_OPEN;
}
2012-02-10 01:57:14 +04:00
freerdp_sem_wait(channels->event_sem); /* lock channels->event */
if (!channels->is_connected)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
freerdp_sem_signal(channels->event_sem);
DEBUG_CHANNELS("error not connected");
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_NOT_CONNECTED;
}
2012-02-10 01:57:14 +04:00
channels->event = event;
2011-07-08 17:36:40 +04:00
/* set the event */
2012-02-10 01:57:14 +04:00
wait_obj_set(channels->signal);
2011-07-08 17:36:40 +04:00
return CHANNEL_RC_OK;
}
/**
* this is called shortly after the application starts and
* before any other function in the file
* called only from main thread
*/
int freerdp_channels_global_init(void)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
g_init_channels = NULL;
g_channels_list = NULL;
2011-07-08 17:36:40 +04:00
g_open_handle_sequence = 1;
g_mutex_init = freerdp_mutex_new();
g_mutex_list = freerdp_mutex_new();
return 0;
}
int freerdp_channels_global_uninit(void)
2011-07-08 17:36:40 +04:00
{
while (g_channels_list)
freerdp_channels_free(g_channels_list->channels);
2011-07-08 17:36:40 +04:00
freerdp_mutex_free(g_mutex_init);
freerdp_mutex_free(g_mutex_list);
return 0;
}
rdpChannels* freerdp_channels_new(void)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
rdpChannels* channels;
rdpChannelsList* channels_list;
2011-07-08 17:36:40 +04:00
2012-02-10 01:57:14 +04:00
channels = xnew(rdpChannels);
2011-07-08 17:36:40 +04:00
2012-02-10 01:57:14 +04:00
channels->sync_data_mutex = freerdp_mutex_new();
channels->sync_data_list = list_new();
2012-02-10 01:57:14 +04:00
channels->event_sem = freerdp_sem_new(1);
channels->signal = wait_obj_new();
2011-07-08 17:36:40 +04:00
/* Add it to the global list */
2012-02-10 01:57:14 +04:00
channels_list = xnew(rdpChannelsList);
channels_list->channels = channels;
2011-07-08 17:36:40 +04:00
freerdp_mutex_lock(g_mutex_list);
2012-02-10 01:57:14 +04:00
channels_list->next = g_channels_list;
g_channels_list = channels_list;
2011-07-08 17:36:40 +04:00
freerdp_mutex_unlock(g_mutex_list);
2012-02-10 01:57:14 +04:00
return channels;
2011-07-08 17:36:40 +04:00
}
2012-02-10 01:57:14 +04:00
void freerdp_channels_free(rdpChannels* channels)
2011-07-08 17:36:40 +04:00
{
rdpChannelsList* list;
rdpChannelsList* prev;
2011-07-08 17:36:40 +04:00
2012-02-10 01:57:14 +04:00
freerdp_mutex_free(channels->sync_data_mutex);
list_free(channels->sync_data_list);
2012-02-10 01:57:14 +04:00
freerdp_sem_free(channels->event_sem);
wait_obj_free(channels->signal);
2011-07-08 17:36:40 +04:00
/* Remove from global list */
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
freerdp_mutex_lock(g_mutex_list);
2012-02-10 01:57:14 +04:00
for (prev = NULL, list = g_channels_list; list; prev = list, list = list->next)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
if (list->channels == channels)
2011-07-08 17:36:40 +04:00
break;
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
if (list)
{
if (prev)
prev->next = list->next;
else
g_channels_list = list->next;
2011-07-08 17:36:40 +04:00
xfree(list);
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
freerdp_mutex_unlock(g_mutex_list);
2012-02-10 01:57:14 +04:00
xfree(channels);
2011-07-08 17:36:40 +04:00
}
/**
* this is called when processing the command line parameters
* called only from main thread
*/
2012-02-10 01:57:14 +04:00
int freerdp_channels_load_plugin(rdpChannels* channels, rdpSettings* settings, const char* name, void* data)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
int ok;
2011-07-08 17:36:40 +04:00
struct lib_data* lib;
CHANNEL_ENTRY_POINTS_EX ep;
DEBUG_CHANNELS("%s", name);
2012-02-10 01:57:14 +04:00
if (channels->num_libs_data + 1 >= CHANNEL_MAX_COUNT)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("too many channels");
2011-07-08 17:36:40 +04:00
return 1;
}
2012-02-10 01:57:14 +04:00
lib = channels->libs_data + channels->num_libs_data;
lib->entry = (PVIRTUALCHANNELENTRY) freerdp_load_plugin(name, CHANNEL_EXPORT_FUNC_NAME);
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
if (lib->entry == NULL)
{
DEBUG_CHANNELS("failed to find export function");
2011-07-08 17:36:40 +04:00
return 1;
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
ep.cbSize = sizeof(ep);
ep.protocolVersion = VIRTUAL_CHANNEL_VERSION_WIN2000;
ep.pVirtualChannelInit = MyVirtualChannelInit;
ep.pVirtualChannelOpen = MyVirtualChannelOpen;
ep.pVirtualChannelClose = MyVirtualChannelClose;
ep.pVirtualChannelWrite = MyVirtualChannelWrite;
ep.pExtendedData = data;
ep.pVirtualChannelEventPush = MyVirtualChannelEventPush;
/* enable MyVirtualChannelInit */
2012-02-10 01:57:14 +04:00
channels->can_call_init = 1;
channels->settings = settings;
2011-07-08 17:36:40 +04:00
freerdp_mutex_lock(g_mutex_init);
2012-02-10 01:57:14 +04:00
g_init_channels = channels;
ok = lib->entry((PCHANNEL_ENTRY_POINTS) &ep);
2012-02-10 01:57:14 +04:00
g_init_channels = NULL;
2011-07-08 17:36:40 +04:00
freerdp_mutex_unlock(g_mutex_init);
/* disable MyVirtualChannelInit */
2012-02-10 01:57:14 +04:00
channels->settings = 0;
channels->can_call_init = 0;
2011-07-08 17:36:40 +04:00
if (!ok)
{
DEBUG_CHANNELS("export function call failed");
2011-07-08 17:36:40 +04:00
return 1;
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
return 0;
}
/**
* go through and inform all the libraries that we are initialized
* called only from main thread
*/
2012-02-10 01:57:14 +04:00
int freerdp_channels_pre_connect(rdpChannels* channels, freerdp* instance)
2011-07-08 17:36:40 +04:00
{
int index;
2012-02-10 01:57:14 +04:00
void* dummy;
2011-07-08 17:36:40 +04:00
struct lib_data* llib;
CHANNEL_DEF lchannel_def;
DEBUG_CHANNELS("enter");
2012-02-10 01:57:14 +04:00
channels->instance = instance;
2011-07-08 17:36:40 +04:00
/**
* If rdpsnd is registered but not rdpdr, it's necessary to register a fake
* rdpdr channel to make sound work. This is a workaround for Window 7 and
* Windows 2008
*/
2012-02-10 01:57:14 +04:00
if (freerdp_channels_find_channel_data_by_name(channels, "rdpsnd", 0) != 0 &&
freerdp_channels_find_channel_data_by_name(channels, "rdpdr", 0) == 0)
2011-07-08 17:36:40 +04:00
{
lchannel_def.options = CHANNEL_OPTION_INITIALIZED |
CHANNEL_OPTION_ENCRYPT_RDP;
strcpy(lchannel_def.name, "rdpdr");
2012-02-10 01:57:14 +04:00
channels->can_call_init = 1;
channels->settings = instance->settings;
2011-07-08 17:36:40 +04:00
freerdp_mutex_lock(g_mutex_init);
2012-02-10 01:57:14 +04:00
g_init_channels = channels;
2011-07-08 17:36:40 +04:00
MyVirtualChannelInit(&dummy, &lchannel_def, 1,
VIRTUAL_CHANNEL_VERSION_WIN2000, 0);
2012-02-10 01:57:14 +04:00
g_init_channels = NULL;
2011-07-08 17:36:40 +04:00
freerdp_mutex_unlock(g_mutex_init);
2012-02-10 01:57:14 +04:00
channels->can_call_init = 0;
channels->settings = 0;
DEBUG_CHANNELS("registered fake rdpdr for rdpsnd.");
2011-07-08 17:36:40 +04:00
}
2012-02-10 01:57:14 +04:00
for (index = 0; index < channels->num_libs_data; index++)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
llib = channels->libs_data + index;
2011-07-08 17:36:40 +04:00
if (llib->init_event_proc != 0)
2012-02-10 01:57:14 +04:00
llib->init_event_proc(llib->init_handle, CHANNEL_EVENT_INITIALIZED, 0, 0);
2011-07-08 17:36:40 +04:00
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
return 0;
}
/**
* go through and inform all the libraries that we are connected
* this will tell the libraries that its ok to call MyVirtualChannelOpen
* called only from main thread
*/
2012-02-10 01:57:14 +04:00
int freerdp_channels_post_connect(rdpChannels* channels, freerdp* instance)
2011-07-08 17:36:40 +04:00
{
int index;
char* hostname;
int hostname_len;
2012-02-10 01:57:14 +04:00
struct lib_data* llib;
2011-07-08 17:36:40 +04:00
2012-02-10 01:57:14 +04:00
channels->is_connected = 1;
hostname = instance->settings->hostname;
2011-07-08 17:36:40 +04:00
hostname_len = strlen(hostname);
2012-02-10 01:57:14 +04:00
DEBUG_CHANNELS("hostname [%s] channels->num_libs [%d]", hostname, channels->num_libs_data);
for (index = 0; index < channels->num_libs_data; index++)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
llib = channels->libs_data + index;
2011-07-08 17:36:40 +04:00
if (llib->init_event_proc != 0)
2012-02-10 01:57:14 +04:00
llib->init_event_proc(llib->init_handle, CHANNEL_EVENT_CONNECTED, hostname, hostname_len);
2011-07-08 17:36:40 +04:00
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
return 0;
}
/**
* data comming from the server to the client
* called only from main thread
*/
2012-02-10 01:57:14 +04:00
int freerdp_channels_data(freerdp* instance, int channel_id, void* data, int data_size, int flags, int total_size)
2011-07-08 17:36:40 +04:00
{
int index;
2012-02-10 01:57:14 +04:00
rdpChannels* channels;
rdpChannel* lrdp_channel;
struct channel_data* lchannel_data;
2011-07-08 17:36:40 +04:00
2012-02-10 01:57:14 +04:00
channels = freerdp_channels_find_by_instance(instance);
if (channels == 0)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("could not find channel manager");
2011-07-08 17:36:40 +04:00
return 1;
}
2012-02-10 01:57:14 +04:00
lrdp_channel = freerdp_channels_find_channel_by_id(channels, instance->settings,
channel_id, &index);
2012-02-10 01:57:14 +04:00
if (lrdp_channel == 0)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("could not find channel id");
2011-07-08 17:36:40 +04:00
return 1;
}
2012-02-10 01:57:14 +04:00
lchannel_data = freerdp_channels_find_channel_data_by_name(channels, lrdp_channel->name, &index);
if (lchannel_data == 0)
2011-07-08 17:36:40 +04:00
{
DEBUG_CHANNELS("could not find channel name");
2011-07-08 17:36:40 +04:00
return 1;
}
2012-02-10 01:57:14 +04:00
if (lchannel_data->open_event_proc != 0)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
lchannel_data->open_event_proc(lchannel_data->open_handle,
CHANNEL_EVENT_DATA_RECEIVED, data, data_size, total_size, flags);
2011-07-08 17:36:40 +04:00
}
2012-02-10 01:57:14 +04:00
2011-07-08 17:36:40 +04:00
return 0;
}
static const char* event_class_to_name_table[] =
{
2011-08-18 01:28:26 +04:00
"rdpdbg", /* RDP_EVENT_CLASS_DEBUG */
"cliprdr", /* RDP_EVENT_CLASS_CLIPRDR */
"tsmf", /* RDP_EVENT_CLASS_TSMF */
"rail", /* RDP_EVENT_CLASS_RAIL */
NULL
};
/**
* Send a plugin-defined event to the plugin.
* called only from main thread
* @param channels the channel manager instance
* @param event an event object created by freerdp_event_new()
*/
2012-02-10 01:57:14 +04:00
FREERDP_API int freerdp_channels_send_event(rdpChannels* channels, RDP_EVENT* event)
{
int index;
const char* name;
2012-02-10 01:57:14 +04:00
struct channel_data* lchannel_data;
name = event_class_to_name_table[event->event_class];
2012-02-10 01:57:14 +04:00
if (name == NULL)
{
DEBUG_CHANNELS("unknown event_class %d", event->event_class);
freerdp_event_free(event);
return 1;
}
2012-02-10 01:57:14 +04:00
lchannel_data = freerdp_channels_find_channel_data_by_name(channels, name, &index);
if (lchannel_data == NULL)
{
DEBUG_CHANNELS("could not find channel name %s", name);
freerdp_event_free(event);
return 1;
}
2012-02-10 01:57:14 +04:00
if (lchannel_data->open_event_proc != NULL)
{
2012-02-10 01:57:14 +04:00
lchannel_data->open_event_proc(lchannel_data->open_handle,
CHANNEL_EVENT_USER,
2011-08-18 01:28:26 +04:00
event, sizeof(RDP_EVENT), sizeof(RDP_EVENT), 0);
}
2012-02-10 01:57:14 +04:00
return 0;
}
2011-07-08 17:36:40 +04:00
/**
* called only from main thread
*/
2012-02-10 01:57:14 +04:00
static void freerdp_channels_process_sync(rdpChannels* channels, freerdp* instance)
2011-07-08 17:36:40 +04:00
{
struct sync_data* item;
2012-02-10 01:57:14 +04:00
rdpChannel* lrdp_channel;
struct channel_data* lchannel_data;
2011-07-08 17:36:40 +04:00
2012-02-10 01:57:14 +04:00
while (channels->sync_data_list->head != NULL)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
freerdp_mutex_lock(channels->sync_data_mutex);
item = (struct sync_data*)list_dequeue(channels->sync_data_list);
freerdp_mutex_unlock(channels->sync_data_mutex);
2012-02-10 01:57:14 +04:00
lchannel_data = channels->channels_data + item->index;
lrdp_channel = freerdp_channels_find_channel_by_name(channels, instance->settings,
lchannel_data->name, &item->index);
2012-02-10 01:57:14 +04:00
if (lrdp_channel != NULL)
instance->SendChannelData(instance, lrdp_channel->channel_id, item->data, item->data_length);
2012-02-10 01:57:14 +04:00
if (lchannel_data->open_event_proc != 0)
{
2012-02-10 01:57:14 +04:00
lchannel_data->open_event_proc(lchannel_data->open_handle,
CHANNEL_EVENT_WRITE_COMPLETE,
item->user_data, sizeof(void *), sizeof(void *), 0);
}
xfree(item);
2011-07-08 17:36:40 +04:00
}
}
/**
* called only from main thread
*/
2012-02-10 01:57:14 +04:00
boolean freerdp_channels_get_fds(rdpChannels* channels, freerdp* instance, void** read_fds,
2011-07-08 17:36:40 +04:00
int* read_count, void** write_fds, int* write_count)
{
2012-02-10 01:57:14 +04:00
wait_obj_get_fds(channels->signal, read_fds, read_count);
return true;
2011-07-08 17:36:40 +04:00
}
/**
* called only from main thread
*/
2012-02-10 01:57:14 +04:00
boolean freerdp_channels_check_fds(rdpChannels* channels, freerdp* instance)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
if (wait_obj_is_set(channels->signal))
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
wait_obj_clear(channels->signal);
freerdp_channels_process_sync(channels, instance);
2011-07-08 17:36:40 +04:00
}
return true;
2011-07-08 17:36:40 +04:00
}
2012-02-10 01:57:14 +04:00
RDP_EVENT* freerdp_channels_pop_event(rdpChannels* channels)
2011-07-08 17:36:40 +04:00
{
2011-08-18 01:28:26 +04:00
RDP_EVENT* event;
2011-07-08 17:36:40 +04:00
2012-02-10 01:57:14 +04:00
if (channels->event == NULL)
2011-07-08 17:36:40 +04:00
return NULL;
2012-02-10 01:57:14 +04:00
event = channels->event;
channels->event = NULL;
2012-02-10 01:57:14 +04:00
freerdp_sem_signal(channels->event_sem); /* release channels->event */
2011-07-08 17:36:40 +04:00
return event;
}
2012-02-10 01:57:14 +04:00
void freerdp_channels_close(rdpChannels* channels, freerdp* instance)
2011-07-08 17:36:40 +04:00
{
int index;
struct lib_data* llib;
DEBUG_CHANNELS("closing");
2012-02-10 01:57:14 +04:00
channels->is_connected = 0;
freerdp_channels_check_fds(channels, instance);
2011-07-08 17:36:40 +04:00
/* tell all libraries we are shutting down */
2012-02-10 01:57:14 +04:00
for (index = 0; index < channels->num_libs_data; index++)
2011-07-08 17:36:40 +04:00
{
2012-02-10 01:57:14 +04:00
llib = channels->libs_data + index;
2011-07-08 17:36:40 +04:00
if (llib->init_event_proc != 0)
2012-02-10 01:57:14 +04:00
llib->init_event_proc(llib->init_handle, CHANNEL_EVENT_TERMINATED, 0, 0);
2011-07-08 17:36:40 +04:00
}
}