* Use freerdp_settings_* for shadow and sample server

* Added freerdp_peer_set_local_and_hostname

* Code cleanups and WINPR_ASSERT

* Code cleanups

* Use CHANNEL_NAME_LEN where appropriate
* Use temporary variables in loop instead of direct array access
This commit is contained in:
akallabeth 2021-08-24 14:09:40 +02:00 committed by GitHub
parent 5afa592244
commit 617293e0d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 208 additions and 102 deletions

View File

@ -29,6 +29,7 @@
#include <winpr/sspi.h> #include <winpr/sspi.h>
#include <winpr/ntlm.h> #include <winpr/ntlm.h>
#include <winpr/winsock.h>
typedef BOOL (*psPeerContextNew)(freerdp_peer* peer, rdpContext* context); typedef BOOL (*psPeerContextNew)(freerdp_peer* peer, rdpContext* context);
typedef void (*psPeerContextFree)(freerdp_peer* peer, rdpContext* context); typedef void (*psPeerContextFree)(freerdp_peer* peer, rdpContext* context);
@ -145,6 +146,8 @@ extern "C"
FREERDP_API freerdp_peer* freerdp_peer_new(int sockfd); FREERDP_API freerdp_peer* freerdp_peer_new(int sockfd);
FREERDP_API void freerdp_peer_free(freerdp_peer* client); FREERDP_API void freerdp_peer_free(freerdp_peer* client);
FREERDP_API BOOL freerdp_peer_set_local_and_hostname(freerdp_peer* client,
const struct sockaddr_storage* peer_addr);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -57,13 +57,14 @@ BOOL freerdp_channel_send(rdpRdp* rdp, UINT16 channelId, const BYTE* data, size_
UINT32 flags; UINT32 flags;
size_t chunkSize; size_t chunkSize;
rdpMcs* mcs = rdp->mcs; rdpMcs* mcs = rdp->mcs;
rdpMcsChannel* channel = NULL; const rdpMcsChannel* channel = NULL;
for (i = 0; i < mcs->channelCount; i++) for (i = 0; i < mcs->channelCount; i++)
{ {
if (mcs->channels[i].ChannelId == channelId) const rdpMcsChannel* cur = &mcs->channels[i];
if (cur->ChannelId == channelId)
{ {
channel = &mcs->channels[i]; channel = cur;
break; break;
} }
} }

View File

@ -55,7 +55,7 @@ static CHANNEL_OPEN_DATA* freerdp_channels_find_channel_open_data_by_name(rdpCha
{ {
pChannelOpenData = &channels->openDataList[index]; pChannelOpenData = &channels->openDataList[index];
if (strncmp(name, pChannelOpenData->name, CHANNEL_NAME_LEN) == 0) if (strncmp(name, pChannelOpenData->name, CHANNEL_NAME_LEN + 1) == 0)
return pChannelOpenData; return pChannelOpenData;
} }
@ -66,7 +66,6 @@ static CHANNEL_OPEN_DATA* freerdp_channels_find_channel_open_data_by_name(rdpCha
static rdpMcsChannel* freerdp_channels_find_channel_by_name(rdpRdp* rdp, const char* name) static rdpMcsChannel* freerdp_channels_find_channel_by_name(rdpRdp* rdp, const char* name)
{ {
UINT32 index; UINT32 index;
rdpMcsChannel* channel = NULL;
rdpMcs* mcs = NULL; rdpMcs* mcs = NULL;
if (!rdp) if (!rdp)
@ -76,9 +75,9 @@ static rdpMcsChannel* freerdp_channels_find_channel_by_name(rdpRdp* rdp, const c
for (index = 0; index < mcs->channelCount; index++) for (index = 0; index < mcs->channelCount; index++)
{ {
channel = &mcs->channels[index]; rdpMcsChannel* channel = &mcs->channels[index];
if (strncmp(name, channel->Name, CHANNEL_NAME_LEN) == 0) if (strncmp(name, channel->Name, CHANNEL_NAME_LEN + 1) == 0)
{ {
return channel; return channel;
} }
@ -471,9 +470,11 @@ BOOL freerdp_channels_data(freerdp* instance, UINT16 channelId, const BYTE* cdat
for (index = 0; index < mcs->channelCount; index++) for (index = 0; index < mcs->channelCount; index++)
{ {
if (mcs->channels[index].ChannelId == channelId) rdpMcsChannel* cur = &mcs->channels[index];
if (cur->ChannelId == channelId)
{ {
channel = &mcs->channels[index]; channel = cur;
break; break;
} }
} }
@ -837,8 +838,9 @@ static UINT VCAPITYPE FreeRDP_VirtualChannelInitEx(
if (settings->ChannelCount < CHANNEL_MAX_COUNT) if (settings->ChannelCount < CHANNEL_MAX_COUNT)
{ {
CHANNEL_DEF* channel = &settings->ChannelDefArray[settings->ChannelCount]; CHANNEL_DEF* channel = freerdp_settings_get_pointer_array_writable(
strncpy(channel->name, pChannelDef->name, 7); settings, FreeRDP_ChannelDefArray, settings->ChannelCount);
strncpy(channel->name, pChannelDef->name, CHANNEL_NAME_LEN);
channel->options = pChannelDef->options; channel->options = pChannelDef->options;
settings->ChannelCount++; settings->ChannelCount++;
} }
@ -928,8 +930,9 @@ static UINT VCAPITYPE FreeRDP_VirtualChannelInit(LPVOID* ppInitHandle, PCHANNEL_
if (settings->ChannelCount < CHANNEL_MAX_COUNT) if (settings->ChannelCount < CHANNEL_MAX_COUNT)
{ {
channel = &settings->ChannelDefArray[settings->ChannelCount]; channel = freerdp_settings_get_pointer_array_writable(settings, FreeRDP_ChannelDefArray,
strncpy(channel->name, pChannelDef->name, 7); settings->ChannelCount);
strncpy(channel->name, pChannelDef->name, CHANNEL_NAME_LEN);
channel->options = pChannelDef->options; channel->options = pChannelDef->options;
settings->ChannelCount++; settings->ChannelCount++;
} }

View File

@ -22,6 +22,7 @@
#include <winpr/crt.h> #include <winpr/crt.h>
#include <winpr/stream.h> #include <winpr/stream.h>
#include <winpr/wtsapi.h>
#include <freerdp/freerdp.h> #include <freerdp/freerdp.h>
#include <freerdp/constants.h> #include <freerdp/constants.h>
@ -52,7 +53,7 @@ typedef struct rdp_channel_client_data CHANNEL_CLIENT_DATA;
struct rdp_channel_open_data struct rdp_channel_open_data
{ {
char name[8]; char name[CHANNEL_NAME_LEN + 1];
int OpenHandle; int OpenHandle;
int options; int options;
int flags; int flags;

View File

@ -908,7 +908,8 @@ BOOL rdp_client_connect_mcs_channel_join_confirm(rdpRdp* rdp, wStream* s)
{ {
if (mcs->channelCount > 0) if (mcs->channelCount > 0)
{ {
if (!mcs_send_channel_join_request(mcs, mcs->channels[0].ChannelId)) const rdpMcsChannel* cur = &mcs->channels[0];
if (!mcs_send_channel_join_request(mcs, cur->ChannelId))
return FALSE; return FALSE;
allJoined = FALSE; allJoined = FALSE;
@ -924,7 +925,8 @@ BOOL rdp_client_connect_mcs_channel_join_confirm(rdpRdp* rdp, wStream* s)
if (mcs->channelCount > 0) if (mcs->channelCount > 0)
{ {
if (!mcs_send_channel_join_request(mcs, mcs->channels[0].ChannelId)) const rdpMcsChannel* cur = &mcs->channels[0];
if (!mcs_send_channel_join_request(mcs, cur->ChannelId))
return FALSE; return FALSE;
allJoined = FALSE; allJoined = FALSE;
@ -934,19 +936,21 @@ BOOL rdp_client_connect_mcs_channel_join_confirm(rdpRdp* rdp, wStream* s)
{ {
for (i = 0; i < mcs->channelCount; i++) for (i = 0; i < mcs->channelCount; i++)
{ {
if (mcs->channels[i].joined) rdpMcsChannel* cur = &mcs->channels[i];
if (cur->joined)
continue; continue;
if (mcs->channels[i].ChannelId != channelId) if (cur->ChannelId != channelId)
return FALSE; return FALSE;
mcs->channels[i].joined = TRUE; cur->joined = TRUE;
break; break;
} }
if (i + 1 < mcs->channelCount) if (i + 1 < mcs->channelCount)
{ {
if (!mcs_send_channel_join_request(mcs, mcs->channels[i + 1].ChannelId)) const rdpMcsChannel* cur = &mcs->channels[i + 1];
if (!mcs_send_channel_join_request(mcs, cur->ChannelId))
return FALSE; return FALSE;
allJoined = FALSE; allJoined = FALSE;
@ -1327,7 +1331,8 @@ BOOL rdp_server_accept_mcs_connect_initial(rdpRdp* rdp, wStream* s)
for (i = 0; i < mcs->channelCount; i++) for (i = 0; i < mcs->channelCount; i++)
{ {
WLog_INFO(TAG, " %s", mcs->channels[i].Name); rdpMcsChannel* cur = &mcs->channels[i];
WLog_INFO(TAG, " %s", cur->Name);
} }
if (!mcs_send_connect_response(mcs)) if (!mcs_send_connect_response(mcs))
@ -1380,10 +1385,11 @@ BOOL rdp_server_accept_mcs_channel_join_request(rdpRdp* rdp, wStream* s)
for (i = 0; i < mcs->channelCount; i++) for (i = 0; i < mcs->channelCount; i++)
{ {
if (mcs->channels[i].ChannelId == channelId) rdpMcsChannel* cur = &mcs->channels[i];
mcs->channels[i].joined = TRUE; if (cur->ChannelId == channelId)
cur->joined = TRUE;
if (!mcs->channels[i].joined) if (!cur->joined)
allJoined = FALSE; allJoined = FALSE;
} }

View File

@ -1584,9 +1584,10 @@ BOOL gcc_read_client_network_data(wStream* s, rdpMcs* mcs, UINT16 blockLength)
* of seven ANSI characters that uniquely identify the channel. * of seven ANSI characters that uniquely identify the channel.
* - options: a 32-bit, unsigned integer. Channel option flags * - options: a 32-bit, unsigned integer. Channel option flags
*/ */
Stream_Read(s, mcs->channels[i].Name, 8); /* name (8 bytes) */ rdpMcsChannel* channel = &mcs->channels[i];
Stream_Read(s, channel->Name, CHANNEL_NAME_LEN + 1); /* name (8 bytes) */
if (!memchr(mcs->channels[i].Name, 0, 8)) if (!memchr(channel->Name, 0, CHANNEL_NAME_LEN + 1))
{ {
WLog_ERR( WLog_ERR(
TAG, TAG,
@ -1594,8 +1595,8 @@ BOOL gcc_read_client_network_data(wStream* s, rdpMcs* mcs, UINT16 blockLength)
return FALSE; return FALSE;
} }
Stream_Read_UINT32(s, mcs->channels[i].options); /* options (4 bytes) */ Stream_Read_UINT32(s, channel->options); /* options (4 bytes) */
mcs->channels[i].ChannelId = mcs->baseChannelId++; channel->ChannelId = mcs->baseChannelId++;
} }
return TRUE; return TRUE;
@ -1624,8 +1625,9 @@ BOOL gcc_write_client_network_data(wStream* s, const rdpMcs* mcs)
for (i = 0; i < mcs->channelCount; i++) for (i = 0; i < mcs->channelCount; i++)
{ {
/* CHANNEL_DEF */ /* CHANNEL_DEF */
Stream_Write(s, mcs->channels[i].Name, 8); /* name (8 bytes) */ rdpMcsChannel* channel = &mcs->channels[i];
Stream_Write_UINT32(s, mcs->channels[i].options); /* options (4 bytes) */ Stream_Write(s, channel->Name, CHANNEL_NAME_LEN + 1); /* name (8 bytes) */
Stream_Write_UINT32(s, channel->options); /* options (4 bytes) */
} }
} }
return TRUE; return TRUE;
@ -1662,8 +1664,9 @@ BOOL gcc_read_server_network_data(wStream* s, rdpMcs* mcs)
for (i = 0; i < parsedChannelCount; i++) for (i = 0; i < parsedChannelCount; i++)
{ {
rdpMcsChannel* channel = &mcs->channels[i];
Stream_Read_UINT16(s, channelId); /* channelId */ Stream_Read_UINT16(s, channelId); /* channelId */
mcs->channels[i].ChannelId = channelId; channel->ChannelId = channelId;
} }
if (channelCount % 2 == 1) if (channelCount % 2 == 1)
@ -1685,7 +1688,8 @@ BOOL gcc_write_server_network_data(wStream* s, const rdpMcs* mcs)
for (i = 0; i < mcs->channelCount; i++) for (i = 0; i < mcs->channelCount; i++)
{ {
Stream_Write_UINT16(s, mcs->channels[i].ChannelId); const rdpMcsChannel* channel = &mcs->channels[i];
Stream_Write_UINT16(s, channel->ChannelId);
} }
if (mcs->channelCount % 2 == 1) if (mcs->channelCount % 2 == 1)

View File

@ -295,16 +295,49 @@ static DWORD freerdp_listener_get_event_handles(freerdp_listener* instance, HAND
return listener->num_sockfds; return listener->num_sockfds;
} }
BOOL freerdp_peer_set_local_and_hostname(freerdp_peer* client,
const struct sockaddr_storage* peer_addr)
{
void* sin_addr = NULL;
const BYTE localhost6_bytes[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
WINPR_ASSERT(client);
WINPR_ASSERT(peer_addr);
if (peer_addr->ss_family == AF_INET)
{
sin_addr = &(((struct sockaddr_in*)&peer_addr)->sin_addr);
if ((*(UINT32*)sin_addr) == 0x0100007f)
client->local = TRUE;
}
else if (peer_addr->ss_family == AF_INET6)
{
sin_addr = &(((struct sockaddr_in6*)&peer_addr)->sin6_addr);
if (memcmp(sin_addr, localhost6_bytes, 16) == 0)
client->local = TRUE;
}
#ifndef _WIN32
else if (peer_addr->ss_family == AF_UNIX)
client->local = TRUE;
#endif
if (sin_addr)
inet_ntop(peer_addr->ss_family, sin_addr, client->hostname, sizeof(client->hostname));
return TRUE;
}
static BOOL freerdp_listener_check_fds(freerdp_listener* instance) static BOOL freerdp_listener_check_fds(freerdp_listener* instance)
{ {
int i; int i;
void* sin_addr;
int peer_sockfd; int peer_sockfd;
freerdp_peer* client = NULL; freerdp_peer* client = NULL;
int peer_addr_size; int peer_addr_size;
struct sockaddr_storage peer_addr; struct sockaddr_storage peer_addr;
rdpListener* listener = (rdpListener*)instance->listener; rdpListener* listener = (rdpListener*)instance->listener;
static const BYTE localhost6_bytes[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
BOOL peer_accepted; BOOL peer_accepted;
if (listener->num_sockfds < 1) if (listener->num_sockfds < 1)
@ -345,31 +378,12 @@ static BOOL freerdp_listener_check_fds(freerdp_listener* instance)
return FALSE; return FALSE;
} }
sin_addr = NULL; if (!freerdp_peer_set_local_and_hostname(client, &peer_addr))
if (peer_addr.ss_family == AF_INET)
{ {
sin_addr = &(((struct sockaddr_in*)&peer_addr)->sin_addr); closesocket((SOCKET)peer_sockfd);
freerdp_peer_free(client);
if ((*(UINT32*)sin_addr) == 0x0100007f) return FALSE;
client->local = TRUE;
} }
else if (peer_addr.ss_family == AF_INET6)
{
sin_addr = &(((struct sockaddr_in6*)&peer_addr)->sin6_addr);
if (memcmp(sin_addr, localhost6_bytes, 16) == 0)
client->local = TRUE;
}
#ifndef _WIN32
else if (peer_addr.ss_family == AF_UNIX)
client->local = TRUE;
#endif
if (sin_addr)
inet_ntop(peer_addr.ss_family, sin_addr, client->hostname, sizeof(client->hostname));
IFCALLRET(instance->PeerAccepted, peer_accepted, instance, client); IFCALLRET(instance->PeerAccepted, peer_accepted, instance, client);

View File

@ -218,8 +218,12 @@ static int mcs_initialize_client_channels(rdpMcs* mcs, rdpSettings* settings)
for (index = 0; index < mcs->channelCount; index++) for (index = 0; index < mcs->channelCount; index++)
{ {
CopyMemory(mcs->channels[index].Name, settings->ChannelDefArray[index].name, 8); const CHANNEL_DEF* defchannel =
mcs->channels[index].options = settings->ChannelDefArray[index].options; freerdp_settings_get_pointer_array(settings, FreeRDP_ChannelDefArray, index);
rdpMcsChannel* cur = &mcs->channels[index];
WINPR_ASSERT(defchannel);
CopyMemory(cur->Name, defchannel->name, CHANNEL_NAME_LEN);
cur->options = defchannel->options;
} }
return 0; return 0;

View File

@ -31,6 +31,7 @@ typedef struct rdp_mcs rdpMcs;
#include <freerdp/types.h> #include <freerdp/types.h>
#include <winpr/stream.h> #include <winpr/stream.h>
#include <winpr/wtsapi.h>
#define MCS_BASE_CHANNEL_ID 1001 #define MCS_BASE_CHANNEL_ID 1001
#define MCS_GLOBAL_CHANNEL_ID 1003 #define MCS_GLOBAL_CHANNEL_ID 1003
@ -118,7 +119,7 @@ typedef struct
struct rdp_mcs_channel struct rdp_mcs_channel
{ {
char Name[8]; char Name[CHANNEL_NAME_LEN + 1];
UINT32 options; UINT32 options;
int ChannelId; int ChannelId;
BOOL joined; BOOL joined;

View File

@ -67,13 +67,16 @@ static rdpPeerChannel* wts_get_dvc_channel_by_id(WTSVirtualChannelManager* vcm,
int count; int count;
BOOL found = FALSE; BOOL found = FALSE;
rdpPeerChannel* channel = NULL; rdpPeerChannel* channel = NULL;
WINPR_ASSERT(vcm);
ArrayList_Lock(vcm->dynamicVirtualChannels); ArrayList_Lock(vcm->dynamicVirtualChannels);
count = ArrayList_Count(vcm->dynamicVirtualChannels); count = ArrayList_Count(vcm->dynamicVirtualChannels);
for (index = 0; index < count; index++) for (index = 0; index < count; index++)
{ {
channel = (rdpPeerChannel*)ArrayList_GetItem(vcm->dynamicVirtualChannels, index); channel = (rdpPeerChannel*)ArrayList_GetItem(vcm->dynamicVirtualChannels, index);
WINPR_ASSERT(channel);
if (channel->channelId == ChannelId) if (channel->channelId == ChannelId)
{ {
found = TRUE; found = TRUE;
@ -89,6 +92,8 @@ static BOOL wts_queue_receive_data(rdpPeerChannel* channel, const BYTE* Buffer,
{ {
BYTE* buffer; BYTE* buffer;
wtsChannelMessage* messageCtx; wtsChannelMessage* messageCtx;
WINPR_ASSERT(channel);
messageCtx = (wtsChannelMessage*)malloc(sizeof(wtsChannelMessage) + Length); messageCtx = (wtsChannelMessage*)malloc(sizeof(wtsChannelMessage) + Length);
if (!messageCtx) if (!messageCtx)
@ -107,6 +112,8 @@ static BOOL wts_queue_send_item(rdpPeerChannel* channel, BYTE* Buffer, UINT32 Le
BYTE* buffer; BYTE* buffer;
UINT32 length; UINT32 length;
UINT16 channelId; UINT16 channelId;
WINPR_ASSERT(channel);
WINPR_ASSERT(channel->vcm);
buffer = Buffer; buffer = Buffer;
length = Length; length = Length;
channelId = channel->channelId; channelId = channel->channelId;
@ -116,6 +123,8 @@ static BOOL wts_queue_send_item(rdpPeerChannel* channel, BYTE* Buffer, UINT32 Le
static int wts_read_variable_uint(wStream* s, int cbLen, UINT32* val) static int wts_read_variable_uint(wStream* s, int cbLen, UINT32* val)
{ {
WINPR_ASSERT(s);
WINPR_ASSERT(val);
switch (cbLen) switch (cbLen)
{ {
case 0: case 0:
@ -145,6 +154,8 @@ static BOOL wts_read_drdynvc_capabilities_response(rdpPeerChannel* channel, UINT
{ {
UINT16 Version; UINT16 Version;
WINPR_ASSERT(channel);
WINPR_ASSERT(channel->vcm);
if (length < 3) if (length < 3)
return FALSE; return FALSE;
@ -159,6 +170,8 @@ static BOOL wts_read_drdynvc_create_response(rdpPeerChannel* channel, wStream* s
{ {
UINT32 CreationStatus; UINT32 CreationStatus;
WINPR_ASSERT(channel);
WINPR_ASSERT(s);
if (length < 4) if (length < 4)
return FALSE; return FALSE;
@ -183,6 +196,8 @@ static BOOL wts_read_drdynvc_data_first(rdpPeerChannel* channel, wStream* s, int
UINT32 length) UINT32 length)
{ {
int value; int value;
WINPR_ASSERT(channel);
WINPR_ASSERT(s);
value = wts_read_variable_uint(s, cbLen, &channel->dvc_total_length); value = wts_read_variable_uint(s, cbLen, &channel->dvc_total_length);
if (value == 0) if (value == 0)
@ -206,6 +221,8 @@ static BOOL wts_read_drdynvc_data(rdpPeerChannel* channel, wStream* s, UINT32 le
{ {
BOOL ret = FALSE; BOOL ret = FALSE;
WINPR_ASSERT(channel);
WINPR_ASSERT(s);
if (channel->dvc_total_length > 0) if (channel->dvc_total_length > 0)
{ {
if (Stream_GetPosition(channel->receiveData) + length > channel->dvc_total_length) if (Stream_GetPosition(channel->receiveData) + length > channel->dvc_total_length)
@ -236,6 +253,7 @@ static BOOL wts_read_drdynvc_data(rdpPeerChannel* channel, wStream* s, UINT32 le
static void wts_read_drdynvc_close_response(rdpPeerChannel* channel) static void wts_read_drdynvc_close_response(rdpPeerChannel* channel)
{ {
WINPR_ASSERT(channel);
DEBUG_DVC("ChannelId %" PRIu32 " close response", channel->channelId); DEBUG_DVC("ChannelId %" PRIu32 " close response", channel->channelId);
channel->dvc_open_state = DVC_OPEN_STATE_CLOSED; channel->dvc_open_state = DVC_OPEN_STATE_CLOSED;
MessageQueue_PostQuit(channel->queue, 0); MessageQueue_PostQuit(channel->queue, 0);
@ -250,6 +268,10 @@ static BOOL wts_read_drdynvc_pdu(rdpPeerChannel* channel)
int cbChId; int cbChId;
UINT32 ChannelId; UINT32 ChannelId;
rdpPeerChannel* dvc; rdpPeerChannel* dvc;
WINPR_ASSERT(channel);
WINPR_ASSERT(channel->vcm);
length = Stream_GetPosition(channel->receiveData); length = Stream_GetPosition(channel->receiveData);
if (length < 1) if (length < 1)
@ -316,6 +338,7 @@ static int wts_write_variable_uint(wStream* s, UINT32 val)
{ {
int cb; int cb;
WINPR_ASSERT(s);
if (val <= 0xFF) if (val <= 0xFF)
{ {
cb = 0; cb = 0;
@ -339,6 +362,9 @@ static void wts_write_drdynvc_header(wStream* s, BYTE Cmd, UINT32 ChannelId)
{ {
BYTE* bm; BYTE* bm;
int cbChId; int cbChId;
WINPR_ASSERT(s);
Stream_GetPointer(s, bm); Stream_GetPointer(s, bm);
Stream_Seek_UINT8(s); Stream_Seek_UINT8(s);
cbChId = wts_write_variable_uint(s, ChannelId); cbChId = wts_write_variable_uint(s, ChannelId);
@ -348,6 +374,10 @@ static void wts_write_drdynvc_header(wStream* s, BYTE Cmd, UINT32 ChannelId)
static BOOL wts_write_drdynvc_create_request(wStream* s, UINT32 ChannelId, const char* ChannelName) static BOOL wts_write_drdynvc_create_request(wStream* s, UINT32 ChannelId, const char* ChannelName)
{ {
size_t len; size_t len;
WINPR_ASSERT(s);
WINPR_ASSERT(ChannelName);
wts_write_drdynvc_header(s, CREATE_REQUEST_PDU, ChannelId); wts_write_drdynvc_header(s, CREATE_REQUEST_PDU, ChannelId);
len = strlen(ChannelName) + 1; len = strlen(ChannelName) + 1;
@ -365,6 +395,8 @@ static BOOL WTSProcessChannelData(rdpPeerChannel* channel, UINT16 channelId, con
const size_t size = (size_t)s; const size_t size = (size_t)s;
const size_t totalSize = (size_t)t; const size_t totalSize = (size_t)t;
WINPR_ASSERT(channel);
WINPR_ASSERT(channel->vcm);
WINPR_UNUSED(channelId); WINPR_UNUSED(channelId);
if (flags & CHANNEL_FLAG_FIRST) if (flags & CHANNEL_FLAG_FIRST)
@ -404,13 +436,20 @@ static BOOL WTSReceiveChannelData(freerdp_peer* client, UINT16 channelId, const
size_t size, UINT32 flags, size_t totalSize) size_t size, UINT32 flags, size_t totalSize)
{ {
UINT32 i; UINT32 i;
rdpMcs* mcs = client->context->rdp->mcs; rdpMcs* mcs;
WINPR_ASSERT(client);
WINPR_ASSERT(client->context);
WINPR_ASSERT(client->context->rdp);
mcs = client->context->rdp->mcs;
WINPR_ASSERT(mcs);
for (i = 0; i < mcs->channelCount; i++) for (i = 0; i < mcs->channelCount; i++)
{ {
if (mcs->channels[i].ChannelId == channelId) rdpMcsChannel* cur = &mcs->channels[i];
if (cur->ChannelId == channelId)
{ {
rdpPeerChannel* channel = (rdpPeerChannel*)mcs->channels[i].handle; rdpPeerChannel* channel = (rdpPeerChannel*)cur->handle;
if (channel) if (channel)
return WTSProcessChannelData(channel, channelId, data, size, flags, totalSize); return WTSProcessChannelData(channel, channelId, data, size, flags, totalSize);
@ -426,6 +465,10 @@ void WTSVirtualChannelManagerGetFileDescriptor(HANDLE hServer, void** fds, int*
{ {
void* fd; void* fd;
WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer; WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
WINPR_ASSERT(vcm);
WINPR_ASSERT(fds);
WINPR_ASSERT(fds_count);
fd = GetEventWaitObject(MessageQueue_Event(vcm->queue)); fd = GetEventWaitObject(MessageQueue_Event(vcm->queue));
if (fd) if (fd)
@ -455,6 +498,8 @@ static BOOL WTSVirtualChannelManagerOpen(WTSVirtualChannelManager* vcm)
if (!vcm) if (!vcm)
return FALSE; return FALSE;
WINPR_ASSERT(vcm->client);
if ((vcm->drdynvc_state == DRDYNVC_STATE_NONE) && vcm->client->activated) if ((vcm->drdynvc_state == DRDYNVC_STATE_NONE) && vcm->client->activated)
{ {
rdpPeerChannel* channel; rdpPeerChannel* channel;
@ -527,6 +572,7 @@ BOOL WTSVirtualChannelManagerCheckFileDescriptor(HANDLE hServer)
HANDLE WTSVirtualChannelManagerGetEventHandle(HANDLE hServer) HANDLE WTSVirtualChannelManagerGetEventHandle(HANDLE hServer)
{ {
WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer; WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
WINPR_ASSERT(vcm);
return MessageQueue_Event(vcm->queue); return MessageQueue_Event(vcm->queue);
} }
@ -534,16 +580,16 @@ static rdpMcsChannel* wts_get_joined_channel_by_name(rdpMcs* mcs, const char* ch
{ {
UINT32 index; UINT32 index;
if (!mcs || !channel_name || !strnlen(channel_name, CHANNEL_NAME_LEN)) if (!mcs || !channel_name || !strnlen(channel_name, CHANNEL_NAME_LEN + 1))
return NULL; return NULL;
for (index = 0; index < mcs->channelCount; index++) for (index = 0; index < mcs->channelCount; index++)
{ {
if (mcs->channels[index].joined) rdpMcsChannel* mchannel = &mcs->channels[index];
if (mchannel->joined)
{ {
if (_strnicmp(mcs->channels[index].Name, channel_name, if (_strnicmp(mchannel->Name, channel_name, CHANNEL_NAME_LEN + 1) == 0)
strnlen(channel_name, CHANNEL_NAME_LEN)) == 0) return mchannel;
return &mcs->channels[index];
} }
} }
@ -557,11 +603,13 @@ static rdpMcsChannel* wts_get_joined_channel_by_id(rdpMcs* mcs, const UINT16 cha
if (!mcs || !channel_id) if (!mcs || !channel_id)
return NULL; return NULL;
WINPR_ASSERT(mcs->channels);
for (index = 0; index < mcs->channelCount; index++) for (index = 0; index < mcs->channelCount; index++)
{ {
if (mcs->channels[index].joined) rdpMcsChannel* mchannel = &mcs->channels[index];
if (mchannel->joined)
{ {
if (mcs->channels[index].ChannelId == channel_id) if (mchannel->ChannelId == channel_id)
return &mcs->channels[index]; return &mcs->channels[index];
} }
} }
@ -600,6 +648,7 @@ BOOL WTSVirtualChannelManagerIsChannelJoined(HANDLE hServer, const char* name)
BYTE WTSVirtualChannelManagerGetDrdynvcState(HANDLE hServer) BYTE WTSVirtualChannelManagerGetDrdynvcState(HANDLE hServer)
{ {
WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer; WTSVirtualChannelManager* vcm = (WTSVirtualChannelManager*)hServer;
WINPR_ASSERT(vcm);
return vcm->drdynvc_state; return vcm->drdynvc_state;
} }
@ -607,6 +656,7 @@ UINT16 WTSChannelGetId(freerdp_peer* client, const char* channel_name)
{ {
rdpMcsChannel* channel; rdpMcsChannel* channel;
WINPR_ASSERT(channel_name);
if (!client || !client->context || !client->context->rdp) if (!client || !client->context || !client->context->rdp)
return 0; return 0;
@ -622,6 +672,7 @@ BOOL WTSChannelSetHandleByName(freerdp_peer* client, const char* channel_name, v
{ {
rdpMcsChannel* channel; rdpMcsChannel* channel;
WINPR_ASSERT(channel_name);
if (!client || !client->context || !client->context->rdp) if (!client || !client->context || !client->context->rdp)
return FALSE; return FALSE;
@ -654,6 +705,7 @@ void* WTSChannelGetHandleByName(freerdp_peer* client, const char* channel_name)
{ {
rdpMcsChannel* channel; rdpMcsChannel* channel;
WINPR_ASSERT(channel_name);
if (!client || !client->context || !client->context->rdp) if (!client || !client->context || !client->context->rdp)
return NULL; return NULL;
@ -704,7 +756,9 @@ char** WTSGetAcceptedChannelNames(freerdp_peer* client, size_t* count)
if (!client || !client->context || !count) if (!client || !client->context || !count)
return NULL; return NULL;
WINPR_ASSERT(client->context->rdp);
mcs = client->context->rdp->mcs; mcs = client->context->rdp->mcs;
WINPR_ASSERT(mcs);
*count = mcs->channelCount; *count = mcs->channelCount;
names = (char**)calloc(mcs->channelCount, sizeof(char*)); names = (char**)calloc(mcs->channelCount, sizeof(char*));
@ -712,7 +766,10 @@ char** WTSGetAcceptedChannelNames(freerdp_peer* client, size_t* count)
return NULL; return NULL;
for (index = 0; index < mcs->channelCount; index++) for (index = 0; index < mcs->channelCount; index++)
names[index] = mcs->channels[index].Name; {
rdpMcsChannel* mchannel = &mcs->channels[index];
names[index] = mchannel->Name;
}
return names; return names;
} }
@ -1068,6 +1125,9 @@ static rdpPeerChannel* channel_new(WTSVirtualChannelManager* vcm, freerdp_peer*
wObject queueCallbacks = { 0 }; wObject queueCallbacks = { 0 };
rdpPeerChannel* channel = (rdpPeerChannel*)calloc(1, sizeof(rdpPeerChannel)); rdpPeerChannel* channel = (rdpPeerChannel*)calloc(1, sizeof(rdpPeerChannel));
WINPR_ASSERT(vcm);
WINPR_ASSERT(client);
if (!channel) if (!channel)
goto fail; goto fail;
@ -1098,7 +1158,7 @@ HANDLE WINAPI FreeRDP_WTSVirtualChannelOpen(HANDLE hServer, DWORD SessionId, LPS
size_t length; size_t length;
UINT32 index; UINT32 index;
rdpMcs* mcs; rdpMcs* mcs;
BOOL joined = FALSE; rdpMcsChannel* joined_channel = NULL;
freerdp_peer* client; freerdp_peer* client;
rdpPeerChannel* channel; rdpPeerChannel* channel;
WTSVirtualChannelManager* vcm; WTSVirtualChannelManager* vcm;
@ -1123,31 +1183,31 @@ HANDLE WINAPI FreeRDP_WTSVirtualChannelOpen(HANDLE hServer, DWORD SessionId, LPS
for (index = 0; index < mcs->channelCount; index++) for (index = 0; index < mcs->channelCount; index++)
{ {
if (mcs->channels[index].joined && rdpMcsChannel* mchannel = &mcs->channels[index];
(strncmp(mcs->channels[index].Name, pVirtualName, length) == 0)) if (mchannel->joined && (strncmp(mchannel->Name, pVirtualName, length) == 0))
{ {
joined = TRUE; joined_channel = mchannel;
break; break;
} }
} }
if (!joined) if (!joined_channel)
{ {
SetLastError(ERROR_NOT_FOUND); SetLastError(ERROR_NOT_FOUND);
return NULL; return NULL;
} }
channel = (rdpPeerChannel*)mcs->channels[index].handle; channel = (rdpPeerChannel*)joined_channel->handle;
if (!channel) if (!channel)
{ {
channel = channel_new(vcm, client, mcs->channels[index].ChannelId, index, channel = channel_new(vcm, client, joined_channel->ChannelId, index,
RDP_PEER_CHANNEL_TYPE_SVC, client->settings->VirtualChannelChunkSize); RDP_PEER_CHANNEL_TYPE_SVC, client->settings->VirtualChannelChunkSize);
if (!channel) if (!channel)
goto fail; goto fail;
mcs->channels[index].handle = channel; joined_channel->handle = channel;
} }
hChannelHandle = (HANDLE)channel; hChannelHandle = (HANDLE)channel;
@ -1188,7 +1248,8 @@ HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenEx(DWORD SessionId, LPSTR pVirtualNam
for (index = 0; index < mcs->channelCount; index++) for (index = 0; index < mcs->channelCount; index++)
{ {
if (mcs->channels[index].joined && (strncmp(mcs->channels[index].Name, "drdynvc", 7) == 0)) rdpMcsChannel* mchannel = &mcs->channels[index];
if (mchannel->joined && (strncmp(mchannel->Name, "drdynvc", CHANNEL_NAME_LEN + 1) == 0))
{ {
joined = TRUE; joined = TRUE;
break; break;
@ -1248,19 +1309,27 @@ BOOL WINAPI FreeRDP_WTSVirtualChannelClose(HANDLE hChannelHandle)
{ {
wStream* s; wStream* s;
rdpMcs* mcs; rdpMcs* mcs;
WTSVirtualChannelManager* vcm;
rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle; rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
BOOL ret = TRUE; BOOL ret = TRUE;
if (channel) if (channel)
{ {
vcm = channel->vcm; WTSVirtualChannelManager* vcm = channel->vcm;
WINPR_ASSERT(vcm);
WINPR_ASSERT(vcm->client);
WINPR_ASSERT(vcm->client->context);
WINPR_ASSERT(vcm->client->context->rdp);
mcs = vcm->client->context->rdp->mcs; mcs = vcm->client->context->rdp->mcs;
if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC) if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
{ {
if (channel->index < mcs->channelCount) if (channel->index < mcs->channelCount)
mcs->channels[channel->index].handle = NULL; {
rdpMcsChannel* cur = &mcs->channels[channel->index];
cur->handle = NULL;
}
} }
else else
{ {
@ -1309,6 +1378,8 @@ BOOL WINAPI FreeRDP_WTSVirtualChannelRead(HANDLE hChannelHandle, ULONG TimeOut,
wtsChannelMessage* messageCtx; wtsChannelMessage* messageCtx;
rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle; rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
WINPR_ASSERT(channel);
if (!MessageQueue_Peek(channel->queue, &message, FALSE)) if (!MessageQueue_Peek(channel->queue, &message, FALSE))
{ {
SetLastError(ERROR_NO_DATA); SetLastError(ERROR_NO_DATA);
@ -1361,6 +1432,7 @@ BOOL WINAPI FreeRDP_WTSVirtualChannelWrite(HANDLE hChannelHandle, PCHAR Buffer,
if (!channel) if (!channel)
return FALSE; return FALSE;
WINPR_ASSERT(channel->vcm);
if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC) if (channel->channelType == RDP_PEER_CHANNEL_TYPE_SVC)
{ {
length = Length; length = Length;
@ -1384,7 +1456,8 @@ BOOL WINAPI FreeRDP_WTSVirtualChannelWrite(HANDLE hChannelHandle, PCHAR Buffer,
else else
{ {
first = TRUE; first = TRUE;
WINPR_ASSERT(channel->client);
WINPR_ASSERT(channel->client->settings);
while (Length > 0) while (Length > 0)
{ {
s = Stream_New(NULL, channel->client->settings->VirtualChannelChunkSize); s = Stream_New(NULL, channel->client->settings->VirtualChannelChunkSize);
@ -1447,12 +1520,14 @@ BOOL WINAPI FreeRDP_WTSVirtualChannelQuery(HANDLE hChannelHandle, WTS_VIRTUAL_CL
{ {
void* pfd; void* pfd;
BOOL bval; BOOL bval;
void* fds[10]; void* fds[10] = { 0 };
HANDLE hEvent; HANDLE hEvent;
int fds_count = 0; int fds_count = 0;
BOOL status = FALSE; BOOL status = FALSE;
rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle; rdpPeerChannel* channel = (rdpPeerChannel*)hChannelHandle;
ZeroMemory(fds, sizeof(fds));
WINPR_ASSERT(channel);
hEvent = MessageQueue_Event(channel->queue); hEvent = MessageQueue_Event(channel->queue);
switch ((UINT32)WtsVirtualClass) switch ((UINT32)WtsVirtualClass)

View File

@ -791,12 +791,9 @@ static DWORD WINAPI test_peer_mainloop(LPVOID arg)
} }
/* Initialize the real server settings here */ /* Initialize the real server settings here */
client->settings->CertificateFile = _strdup("server.crt"); if (!freerdp_settings_set_string(client->settings, FreeRDP_CertificateFile, "server.crt") ||
client->settings->PrivateKeyFile = _strdup("server.key"); !freerdp_settings_set_string(client->settings, FreeRDP_PrivateKeyFile, "server.key") ||
client->settings->RdpKeyFile = _strdup("server.key"); !freerdp_settings_set_string(client->settings, FreeRDP_RdpKeyFile, "server.key"))
if (!client->settings->CertificateFile || !client->settings->PrivateKeyFile ||
!client->settings->RdpKeyFile)
{ {
WLog_ERR(TAG, "Memory allocation failed (strdup)"); WLog_ERR(TAG, "Memory allocation failed (strdup)");
freerdp_peer_free(client); freerdp_peer_free(client);

View File

@ -195,13 +195,13 @@ static BOOL shadow_client_context_new(freerdp_peer* peer, rdpContext* context)
settings->DrawAllowDynamicColorFidelity = TRUE; settings->DrawAllowDynamicColorFidelity = TRUE;
settings->CompressionLevel = PACKET_COMPR_TYPE_RDP6; settings->CompressionLevel = PACKET_COMPR_TYPE_RDP6;
if (!(settings->CertificateFile = _strdup(server->CertificateFile))) if (!freerdp_settings_set_string(settings, FreeRDP_CertificateFile, server->CertificateFile))
goto fail_cert_file; goto fail_cert_file;
if (!(settings->PrivateKeyFile = _strdup(server->PrivateKeyFile))) if (!freerdp_settings_set_string(settings, FreeRDP_PrivateKeyFile, server->PrivateKeyFile))
goto fail_privkey_file; goto fail_privkey_file;
if (!(settings->RdpKeyFile = _strdup(settings->PrivateKeyFile))) if (!freerdp_settings_set_string(settings, FreeRDP_RdpKeyFile, server->PrivateKeyFile))
goto fail_rdpkey_file; goto fail_rdpkey_file;
if (server->ipcSocket && (strncmp(bind_address, server->ipcSocket, if (server->ipcSocket && (strncmp(bind_address, server->ipcSocket,
strnlen(bind_address, sizeof(bind_address))) != 0)) strnlen(bind_address, sizeof(bind_address))) != 0))
@ -243,14 +243,11 @@ fail_message_queue:
fail_open_server: fail_open_server:
DeleteCriticalSection(&(client->lock)); DeleteCriticalSection(&(client->lock));
fail_client_lock: fail_client_lock:
free(settings->RdpKeyFile); freerdp_settings_set_string(settings, FreeRDP_RdpKeyFile, NULL);
settings->RdpKeyFile = NULL;
fail_rdpkey_file: fail_rdpkey_file:
free(settings->PrivateKeyFile); freerdp_settings_set_string(settings, FreeRDP_PrivateKeyFile, NULL);
settings->PrivateKeyFile = NULL;
fail_privkey_file: fail_privkey_file:
free(settings->CertificateFile); freerdp_settings_set_string(settings, FreeRDP_CertificateFile, NULL);
settings->CertificateFile = NULL;
fail_cert_file: fail_cert_file:
return FALSE; return FALSE;
} }