FreeRDP/libfreerdp/core/gateway/rpc_client.c

697 lines
16 KiB
C
Raw Normal View History

/**
* FreeRDP: A Remote Desktop Protocol Implementation
* RPC Client
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <freerdp/log.h>
#include <winpr/crt.h>
#include <winpr/print.h>
#include <winpr/synch.h>
#include <winpr/thread.h>
#include <winpr/stream.h>
#include "rpc_fault.h"
#include "rpc_client.h"
#include "../rdp.h"
#define TAG FREERDP_TAG("core.gateway")
#define SYNCHRONOUS_TIMEOUT 5000
2014-08-18 21:34:47 +04:00
RPC_PDU* rpc_client_receive_pool_take(rdpRpc* rpc)
{
2014-08-18 21:34:47 +04:00
RPC_PDU* pdu = NULL;
if (WaitForSingleObject(Queue_Event(rpc->client->ReceivePool), 0) == WAIT_OBJECT_0)
pdu = Queue_Dequeue(rpc->client->ReceivePool);
if (!pdu)
{
pdu = (RPC_PDU*) malloc(sizeof(RPC_PDU));
if (!pdu)
return NULL;
pdu->s = Stream_New(NULL, rpc->max_recv_frag);
if (!pdu->s)
{
free(pdu);
return NULL;
}
}
pdu->CallId = 0;
pdu->Flags = 0;
Stream_Length(pdu->s) = 0;
Stream_SetPosition(pdu->s, 0);
return pdu;
}
2014-08-18 21:34:47 +04:00
int rpc_client_receive_pool_return(rdpRpc* rpc, RPC_PDU* pdu)
{
return Queue_Enqueue(rpc->client->ReceivePool, pdu) == TRUE ? 0 : -1;
}
int rpc_client_receive_pipe_write(rdpRpc* rpc, const BYTE* buffer, size_t length)
{
int status = 0;
RpcClient* client = rpc->client;
EnterCriticalSection(&(rpc->client->PipeLock));
if (ringbuffer_write(&(client->ReceivePipe), buffer, length))
status += (int) length;
if (ringbuffer_used(&(client->ReceivePipe)) > 0)
SetEvent(client->PipeEvent);
LeaveCriticalSection(&(rpc->client->PipeLock));
return status;
}
int rpc_client_receive_pipe_read(rdpRpc* rpc, BYTE* buffer, size_t length)
{
int index = 0;
int status = 0;
int nchunks = 0;
DataChunk chunks[2];
RpcClient* client = rpc->client;
EnterCriticalSection(&(client->PipeLock));
nchunks = ringbuffer_peek(&(client->ReceivePipe), chunks, length);
for (index = 0; index < nchunks; index++)
{
CopyMemory(&buffer[status], chunks[index].data, chunks[index].size);
status += chunks[index].size;
}
if (status > 0)
ringbuffer_commit_read_bytes(&(client->ReceivePipe), status);
if (ringbuffer_used(&(client->ReceivePipe)) < 1)
ResetEvent(client->PipeEvent);
LeaveCriticalSection(&(client->PipeLock));
return status;
}
int rpc_client_on_pdu_received_event(rdpRpc* rpc, RPC_PDU* pdu)
{
RpcClientCall* call;
if (pdu->Type != PTYPE_RESPONSE)
{
Queue_Enqueue(rpc->client->ReceiveQueue, pdu);
return 1;
}
else
{
call = rpc_client_call_find_by_id(rpc, pdu->CallId);
if (call->OpNum != TsProxySetupReceivePipeOpnum)
{
Queue_Enqueue(rpc->client->ReceiveQueue, pdu);
}
else
{
rpc_client_receive_pipe_write(rpc, Stream_Buffer(pdu->s), Stream_Length(pdu->s));
rpc_client_receive_pool_return(rpc, pdu);
}
}
return 1;
}
int rpc_client_on_fragment_received_event(rdpRpc* rpc, wStream* fragment)
{
2014-08-18 21:34:47 +04:00
BYTE* buffer;
UINT32 StubOffset;
UINT32 StubLength;
2014-08-18 21:34:47 +04:00
rpcconn_hdr_t* header;
if (!rpc->client->pdu)
rpc->client->pdu = rpc_client_receive_pool_take(rpc);
2014-08-18 21:34:47 +04:00
buffer = (BYTE*) Stream_Buffer(fragment);
header = (rpcconn_hdr_t*) Stream_Buffer(fragment);
2012-12-12 08:34:51 +04:00
if (rpc->State < RPC_CLIENT_STATE_CONTEXT_NEGOTIATED)
{
rpc->client->pdu->Flags = 0;
rpc->client->pdu->Type = header->common.ptype;
rpc->client->pdu->CallId = header->common.call_id;
Stream_EnsureCapacity(rpc->client->pdu->s, Stream_Length(fragment));
Stream_Write(rpc->client->pdu->s, buffer, Stream_Length(fragment));
Stream_SealLength(rpc->client->pdu->s);
rpc_client_on_pdu_received_event(rpc, rpc->client->pdu);
rpc->client->pdu = NULL;
return 0;
}
switch (header->common.ptype)
{
case PTYPE_RTS:
2014-08-19 20:26:39 +04:00
if (rpc->VirtualConnection->State < VIRTUAL_CONNECTION_STATE_OPENED)
{
WLog_ERR(TAG, "warning: unhandled RTS PDU");
return 0;
}
WLog_DBG(TAG, "Receiving Out-of-Sequence RTS PDU");
2012-12-12 08:34:51 +04:00
rts_recv_out_of_sequence_pdu(rpc, buffer, header->common.frag_length);
return 0;
2015-01-13 21:50:46 +03:00
case PTYPE_FAULT:
rpc_recv_fault_pdu(header);
return -1;
2015-01-13 21:50:46 +03:00
case PTYPE_RESPONSE:
break;
2015-01-13 21:50:46 +03:00
default:
WLog_ERR(TAG, "unexpected RPC PDU type %d", header->common.ptype);
return -1;
2012-12-12 08:34:51 +04:00
}
/* PTYPE_RESPONSE */
2012-12-12 08:34:51 +04:00
rpc->VirtualConnection->DefaultOutChannel->BytesReceived += header->common.frag_length;
rpc->VirtualConnection->DefaultOutChannel->ReceiverAvailableWindow -= header->common.frag_length;
if (!rpc_get_stub_data_info(rpc, buffer, &StubOffset, &StubLength))
{
WLog_ERR(TAG, "expected stub");
2012-12-12 08:34:51 +04:00
return -1;
}
if (StubLength == 4)
{
2013-06-19 00:55:23 +04:00
/* received a disconnect request from the server? */
if ((header->common.call_id == rpc->PipeCallId) && (header->common.pfc_flags & PFC_LAST_FRAG))
{
2015-01-15 23:04:35 +03:00
TerminateEventArgs e;
rpc->result = *((UINT32*) &buffer[StubOffset]);
rpc->context->rdp->disconnect = TRUE;
2013-06-19 00:55:23 +04:00
rpc->transport->tsg->state = TSG_STATE_TUNNEL_CLOSE_PENDING;
EventArgsInit(&e, "freerdp");
e.code = 0;
PubSub_OnTerminate(rpc->context->pubSub, rpc->context, &e);
2013-06-19 00:55:23 +04:00
}
2012-12-12 08:34:51 +04:00
return 0;
}
if (rpc->StubFragCount == 0)
rpc->StubCallId = header->common.call_id;
if (rpc->StubCallId != header->common.call_id)
{
WLog_ERR(TAG, "invalid call_id: actual: %d, expected: %d, frag_count: %d",
rpc->StubCallId, header->common.call_id, rpc->StubFragCount);
}
Stream_EnsureCapacity(rpc->client->pdu->s, header->response.alloc_hint);
Stream_Write(rpc->client->pdu->s, &buffer[StubOffset], StubLength);
rpc->StubFragCount++;
if (rpc->VirtualConnection->DefaultOutChannel->ReceiverAvailableWindow < (rpc->ReceiveWindow / 2))
{
rts_send_flow_control_ack_pdu(rpc);
}
/**
* If alloc_hint is set to a nonzero value and a request or a response is fragmented into multiple
* PDUs, implementations of these extensions SHOULD set the alloc_hint field in every PDU to be the
* combined stub data length of all remaining fragment PDUs.
*/
2013-01-23 22:44:58 +04:00
if (header->response.alloc_hint == StubLength)
{
rpc->client->pdu->Flags = RPC_PDU_FLAG_STUB;
rpc->client->pdu->Type = PTYPE_RESPONSE;
rpc->client->pdu->CallId = rpc->StubCallId;
Stream_SealLength(rpc->client->pdu->s);
rpc->StubFragCount = 0;
rpc->StubCallId = 0;
rpc_client_on_pdu_received_event(rpc, rpc->client->pdu);
rpc->client->pdu = NULL;
return 0;
}
return 0;
}
2014-08-18 21:34:47 +04:00
int rpc_client_on_read_event(rdpRpc* rpc)
{
int position;
int status = -1;
2014-08-18 21:34:47 +04:00
rpcconn_common_hdr_t* header;
while (1)
{
position = Stream_GetPosition(rpc->client->ReceiveFragment);
while (Stream_GetPosition(rpc->client->ReceiveFragment) < RPC_COMMON_FIELDS_LENGTH)
{
status = rpc_out_read(rpc, Stream_Pointer(rpc->client->ReceiveFragment),
RPC_COMMON_FIELDS_LENGTH - Stream_GetPosition(rpc->client->ReceiveFragment));
if (status < 0)
return -1;
if (!status)
return 0;
Stream_Seek(rpc->client->ReceiveFragment, status);
}
if (Stream_GetPosition(rpc->client->ReceiveFragment) < RPC_COMMON_FIELDS_LENGTH)
return status;
header = (rpcconn_common_hdr_t*) Stream_Buffer(rpc->client->ReceiveFragment);
if (header->frag_length > rpc->max_recv_frag)
{
WLog_ERR(TAG, "rpc_client_frag_read: invalid fragment size: %d (max: %d)",
header->frag_length, rpc->max_recv_frag);
winpr_HexDump(TAG, WLOG_ERROR, Stream_Buffer(rpc->client->ReceiveFragment), Stream_GetPosition(rpc->client->ReceiveFragment));
return -1;
}
while (Stream_GetPosition(rpc->client->ReceiveFragment) < header->frag_length)
{
status = rpc_out_read(rpc, Stream_Pointer(rpc->client->ReceiveFragment),
header->frag_length - Stream_GetPosition(rpc->client->ReceiveFragment));
if (status < 0)
{
WLog_ERR(TAG, "error reading fragment body");
return -1;
}
if (!status)
return 0;
Stream_Seek(rpc->client->ReceiveFragment, status);
}
if (status < 0)
return -1;
status = Stream_GetPosition(rpc->client->ReceiveFragment) - position;
if (Stream_GetPosition(rpc->client->ReceiveFragment) >= header->frag_length)
{
/* complete fragment received */
Stream_SealLength(rpc->client->ReceiveFragment);
Stream_SetPosition(rpc->client->ReceiveFragment, 0);
status = rpc_client_on_fragment_received_event(rpc, rpc->client->ReceiveFragment);
if (status < 0)
return status;
Stream_SetPosition(rpc->client->ReceiveFragment, 0);
}
if (WaitForSingleObject(Queue_Event(rpc->client->SendQueue), 0) == WAIT_OBJECT_0)
break;
}
return 0;
}
/**
* [MS-RPCE] Client Call:
* http://msdn.microsoft.com/en-us/library/gg593159/
*/
2014-08-18 21:34:47 +04:00
RpcClientCall* rpc_client_call_find_by_id(rdpRpc* rpc, UINT32 CallId)
{
int index;
int count;
RpcClientCall* clientCall = NULL;
2012-12-12 08:34:51 +04:00
ArrayList_Lock(rpc->client->ClientCallList);
2012-12-12 08:34:51 +04:00
count = ArrayList_Count(rpc->client->ClientCallList);
for (index = 0; index < count; index++)
{
2014-08-18 21:34:47 +04:00
clientCall = (RpcClientCall*) ArrayList_GetItem(rpc->client->ClientCallList, index);
if (clientCall->CallId == CallId)
break;
}
2012-12-12 08:34:51 +04:00
ArrayList_Unlock(rpc->client->ClientCallList);
return clientCall;
}
2014-08-18 21:34:47 +04:00
RpcClientCall* rpc_client_call_new(UINT32 CallId, UINT32 OpNum)
{
2014-08-18 21:34:47 +04:00
RpcClientCall* clientCall;
clientCall = (RpcClientCall*) calloc(1, sizeof(RpcClientCall));
if (!clientCall)
return NULL;
clientCall->CallId = CallId;
clientCall->OpNum = OpNum;
clientCall->State = RPC_CLIENT_CALL_STATE_SEND_PDUS;
return clientCall;
}
2014-08-18 21:34:47 +04:00
void rpc_client_call_free(RpcClientCall* clientCall)
{
free(clientCall);
}
2014-08-18 21:34:47 +04:00
int rpc_send_enqueue_pdu(rdpRpc* rpc, BYTE* buffer, UINT32 length)
{
int status;
RPC_PDU* pdu;
2014-08-18 21:34:47 +04:00
pdu = (RPC_PDU*) malloc(sizeof(RPC_PDU));
if (!pdu)
{
free(buffer);
return -1;
}
pdu->s = Stream_New(buffer, length);
if (!pdu->s)
goto out_free;
if (!Queue_Enqueue(rpc->client->SendQueue, pdu))
goto out_free_stream;
if (rpc->client->SynchronousSend)
{
status = WaitForSingleObject(rpc->client->PduSentEvent, SYNCHRONOUS_TIMEOUT);
if (status == WAIT_TIMEOUT)
{
WLog_ERR(TAG, "timed out waiting for pdu sent event %p", rpc->client->PduSentEvent);
return -1;
}
ResetEvent(rpc->client->PduSentEvent);
}
return 0;
out_free_stream:
Stream_Free(pdu->s, TRUE);
out_free:
free(pdu);
WLog_ERR(TAG, "rpc_send_enqueue_pdu failure");
return -1;
}
2014-08-18 21:34:47 +04:00
int rpc_send_dequeue_pdu(rdpRpc* rpc)
{
int status;
2014-08-18 21:34:47 +04:00
RPC_PDU* pdu;
RpcClientCall* clientCall;
rpcconn_common_hdr_t* header;
RpcInChannel* inChannel;
2014-08-18 21:34:47 +04:00
pdu = (RPC_PDU*) Queue_Dequeue(rpc->client->SendQueue);
if (!pdu)
return 0;
inChannel = rpc->VirtualConnection->DefaultInChannel;
WaitForSingleObject(inChannel->Mutex, INFINITE);
status = rpc_in_write(rpc, Stream_Buffer(pdu->s), Stream_Length(pdu->s));
2014-08-18 21:34:47 +04:00
header = (rpcconn_common_hdr_t*) Stream_Buffer(pdu->s);
clientCall = rpc_client_call_find_by_id(rpc, header->call_id);
clientCall->State = RPC_CLIENT_CALL_STATE_DISPATCHED;
ReleaseMutex(inChannel->Mutex);
/*
* This protocol specifies that only RPC PDUs are subject to the flow control abstract
* data model. RTS PDUs and the HTTP request and response headers are not subject to flow control.
* Implementations of this protocol MUST NOT include them when computing any of the variables
* specified by this abstract data model.
*/
if (header->ptype == PTYPE_REQUEST)
{
inChannel->BytesSent += status;
inChannel->SenderAvailableWindow -= status;
}
Stream_Free(pdu->s, TRUE);
free(pdu);
if (rpc->client->SynchronousSend)
SetEvent(rpc->client->PduSentEvent);
return status;
}
2014-08-18 21:34:47 +04:00
RPC_PDU* rpc_recv_dequeue_pdu(rdpRpc* rpc)
{
2014-08-18 21:34:47 +04:00
RPC_PDU* pdu;
DWORD timeout;
DWORD waitStatus;
2015-01-13 21:50:46 +03:00
timeout = rpc->client->SynchronousReceive ? SYNCHRONOUS_TIMEOUT * 4 : 0;
waitStatus = WaitForSingleObject(Queue_Event(rpc->client->ReceiveQueue), timeout);
if (waitStatus == WAIT_TIMEOUT)
{
WLog_ERR(TAG, "timed out waiting for receive event");
return NULL;
}
if (waitStatus != WAIT_OBJECT_0)
return NULL;
2015-01-13 21:50:46 +03:00
pdu = (RPC_PDU*) Queue_Dequeue(rpc->client->ReceiveQueue);
return pdu;
}
2014-08-18 21:34:47 +04:00
static void* rpc_client_thread(void* arg)
{
DWORD nCount;
HANDLE events[8];
DWORD waitStatus;
HANDLE ReadEvent = NULL;
rdpRpc* rpc = (rdpRpc*) arg;
BIO_get_event(rpc->TlsOut->bio, &ReadEvent);
nCount = 0;
events[nCount++] = rpc->client->StopEvent;
2012-12-12 08:34:51 +04:00
events[nCount++] = Queue_Event(rpc->client->SendQueue);
events[nCount++] = ReadEvent;
while (rpc->transport->layer != TRANSPORT_LAYER_CLOSED)
{
waitStatus = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
if (WaitForSingleObject(rpc->client->StopEvent, 0) == WAIT_OBJECT_0)
break;
if (WaitForSingleObject(ReadEvent, 0) == WAIT_OBJECT_0)
{
if (rpc_client_on_read_event(rpc) < 0)
{
rpc->transport->layer = TRANSPORT_LAYER_CLOSED;
2012-12-12 08:34:51 +04:00
break;
}
}
if (WaitForSingleObject(Queue_Event(rpc->client->SendQueue), 0) == WAIT_OBJECT_0)
{
rpc_send_dequeue_pdu(rpc);
}
}
return NULL;
}
2014-08-18 21:34:47 +04:00
static void rpc_pdu_free(RPC_PDU* pdu)
2012-12-12 08:34:51 +04:00
{
if (!pdu)
return;
Stream_Free(pdu->s, TRUE);
2012-12-12 08:34:51 +04:00
free(pdu);
}
2014-08-18 21:34:47 +04:00
int rpc_client_new(rdpRpc* rpc)
{
RpcClient* client;
client = (RpcClient*) calloc(1, sizeof(RpcClient));
rpc->client = client;
if (!client)
return -1;
client->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!client->StopEvent)
return -1;
client->PduSentEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!client->PduSentEvent)
return -1;
client->SendQueue = Queue_New(TRUE, -1, -1);
if (!client->SendQueue)
return -1;
2012-12-12 08:34:51 +04:00
Queue_Object(client->SendQueue)->fnObjectFree = (OBJECT_FREE_FN) rpc_pdu_free;
client->pdu = NULL;
client->ReceivePool = Queue_New(TRUE, -1, -1);
if (!client->ReceivePool)
return -1;
Queue_Object(client->ReceivePool)->fnObjectFree = (OBJECT_FREE_FN) rpc_pdu_free;
client->ReceiveQueue = Queue_New(TRUE, -1, -1);
if (!client->ReceiveQueue)
return -1;
2012-12-12 08:34:51 +04:00
Queue_Object(client->ReceiveQueue)->fnObjectFree = (OBJECT_FREE_FN) rpc_pdu_free;
client->ReceiveFragment = Stream_New(NULL, rpc->max_recv_frag);
if (!client->ReceiveFragment)
return -1;
client->PipeEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!client->PipeEvent)
return -1;
if (!ringbuffer_init(&(client->ReceivePipe), 4096))
return -1;
if (!InitializeCriticalSectionAndSpinCount(&(client->PipeLock), 4000))
return -1;
client->ClientCallList = ArrayList_New(TRUE);
if (!client->ClientCallList)
return -1;
ArrayList_Object(client->ClientCallList)->fnObjectFree = (OBJECT_FREE_FN) rpc_client_call_free;
return 0;
}
2014-08-18 21:34:47 +04:00
int rpc_client_start(rdpRpc* rpc)
{
rpc->client->Thread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE) rpc_client_thread, rpc, 0, NULL);
return 0;
}
2012-12-12 08:34:51 +04:00
2014-08-18 21:34:47 +04:00
int rpc_client_stop(rdpRpc* rpc)
2012-12-12 08:34:51 +04:00
{
if (rpc->client->Thread)
{
SetEvent(rpc->client->StopEvent);
WaitForSingleObject(rpc->client->Thread, INFINITE);
rpc->client->Thread = NULL;
}
return 0;
2012-12-12 08:34:51 +04:00
}
2014-08-18 21:34:47 +04:00
int rpc_client_free(rdpRpc* rpc)
2012-12-12 08:34:51 +04:00
{
RpcClient* client = rpc->client;
if (!client)
return 0;
rpc_client_stop(rpc);
if (client->SendQueue)
Queue_Free(client->SendQueue);
if (client->ReceiveFragment)
Stream_Free(client->ReceiveFragment, TRUE);
if (client->PipeEvent)
CloseHandle(client->PipeEvent);
ringbuffer_destroy(&(client->ReceivePipe));
DeleteCriticalSection(&(client->PipeLock));
if (client->pdu)
rpc_pdu_free(client->pdu);
if (client->ReceivePool)
Queue_Free(client->ReceivePool);
if (client->ReceiveQueue)
Queue_Free(client->ReceiveQueue);
2012-12-12 08:34:51 +04:00
if (client->ClientCallList)
ArrayList_Free(client->ClientCallList);
if (client->StopEvent)
CloseHandle(client->StopEvent);
if (client->PduSentEvent)
CloseHandle(client->PduSentEvent);
if (client->Thread)
CloseHandle(client->Thread);
free(client);
rpc->client = NULL;
2012-12-12 08:34:51 +04:00
return 0;
}