2012-11-28 21:47:04 +04:00
|
|
|
/**
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2012-12-13 00:55:42 +04:00
|
|
|
#include <winpr/print.h>
|
2012-11-28 21:47:04 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
2012-12-11 00:43:07 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-12-12 04:17:57 +04:00
|
|
|
#include "rpc_fault.h"
|
|
|
|
|
2012-11-28 21:47:04 +04:00
|
|
|
#include "rpc_client.h"
|
|
|
|
|
2012-12-12 04:17:57 +04:00
|
|
|
wStream* rpc_client_fragment_pool_take(rdpRpc* rpc)
|
|
|
|
{
|
|
|
|
wStream* fragment = NULL;
|
|
|
|
|
|
|
|
if (WaitForSingleObject(Queue_Event(rpc->client->FragmentPool), 0) == WAIT_OBJECT_0)
|
|
|
|
fragment = Queue_Dequeue(rpc->client->FragmentPool);
|
|
|
|
|
|
|
|
if (!fragment)
|
|
|
|
fragment = Stream_New(NULL, rpc->max_recv_frag);
|
|
|
|
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_client_fragment_pool_return(rdpRpc* rpc, wStream* fragment)
|
|
|
|
{
|
|
|
|
Queue_Enqueue(rpc->client->FragmentPool, fragment);
|
2012-12-12 09:49:15 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RPC_PDU* rpc_client_receive_pool_take(rdpRpc* rpc)
|
|
|
|
{
|
|
|
|
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));
|
2012-12-13 00:55:42 +04:00
|
|
|
pdu->s = Stream_New(NULL, rpc->max_recv_frag);
|
2012-12-12 09:49:15 +04:00
|
|
|
}
|
2012-12-12 04:17:57 +04:00
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
pdu->CallId = 0;
|
|
|
|
pdu->Flags = 0;
|
2012-12-13 00:55:42 +04:00
|
|
|
|
|
|
|
Stream_Length(pdu->s) = 0;
|
|
|
|
Stream_SetPosition(pdu->s, 0);
|
2012-12-12 09:49:15 +04:00
|
|
|
|
|
|
|
return pdu;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_client_receive_pool_return(rdpRpc* rpc, RPC_PDU* pdu)
|
|
|
|
{
|
|
|
|
Queue_Enqueue(rpc->client->ReceivePool, pdu);
|
2012-12-12 04:17:57 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_client_on_fragment_received_event(rdpRpc* rpc)
|
|
|
|
{
|
|
|
|
BYTE* buffer;
|
|
|
|
UINT32 StubOffset;
|
|
|
|
UINT32 StubLength;
|
|
|
|
wStream* fragment;
|
|
|
|
rpcconn_hdr_t* header;
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
if (!rpc->client->pdu)
|
|
|
|
rpc->client->pdu = rpc_client_receive_pool_take(rpc);
|
|
|
|
|
2012-12-12 04:17:57 +04:00
|
|
|
fragment = Queue_Dequeue(rpc->client->FragmentQueue);
|
|
|
|
|
|
|
|
buffer = (BYTE*) Stream_Buffer(fragment);
|
|
|
|
header = (rpcconn_hdr_t*) Stream_Buffer(fragment);
|
2012-12-12 08:34:51 +04:00
|
|
|
|
2012-12-12 04:17:57 +04:00
|
|
|
if (rpc->State < RPC_CLIENT_STATE_CONTEXT_NEGOTIATED)
|
|
|
|
{
|
2012-12-13 07:03:40 +04:00
|
|
|
rpc->client->pdu->Flags = 0;
|
|
|
|
rpc->client->pdu->CallId = header->common.call_id;
|
2012-12-12 04:17:57 +04:00
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
Stream_EnsureCapacity(rpc->client->pdu->s, Stream_Length(fragment));
|
|
|
|
Stream_Write(rpc->client->pdu->s, buffer, Stream_Length(fragment));
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Length(rpc->client->pdu->s) = Stream_GetPosition(rpc->client->pdu->s);
|
2012-12-12 09:49:15 +04:00
|
|
|
|
|
|
|
rpc_client_fragment_pool_return(rpc, fragment);
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
Queue_Enqueue(rpc->client->ReceiveQueue, rpc->client->pdu);
|
2013-03-21 22:45:03 +04:00
|
|
|
SetEvent(rpc->transport->ReceiveEvent);
|
2012-12-13 07:03:40 +04:00
|
|
|
rpc->client->pdu = NULL;
|
2012-12-12 04:17:57 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
if (header->common.ptype == PTYPE_RTS)
|
2012-12-12 04:17:57 +04:00
|
|
|
{
|
2012-12-12 08:34:51 +04:00
|
|
|
if (rpc->VirtualConnection->State >= VIRTUAL_CONNECTION_STATE_OPENED)
|
2012-12-12 04:17:57 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
//fprintf(stderr, "Receiving Out-of-Sequence RTS PDU\n");
|
2012-12-12 08:34:51 +04:00
|
|
|
rts_recv_out_of_sequence_pdu(rpc, buffer, header->common.frag_length);
|
2012-12-12 04:17:57 +04:00
|
|
|
|
|
|
|
rpc_client_fragment_pool_return(rpc, fragment);
|
|
|
|
}
|
2012-12-12 09:49:15 +04:00
|
|
|
else
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "warning: unhandled RTS PDU\n");
|
2012-12-12 09:49:15 +04:00
|
|
|
}
|
2012-12-12 04:17:57 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (header->common.ptype == PTYPE_FAULT)
|
|
|
|
{
|
|
|
|
rpc_recv_fault_pdu(header);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
if (header->common.ptype != PTYPE_RESPONSE)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Unexpected RPC PDU type: %d\n", header->common.ptype);
|
2012-12-12 08:34:51 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "rpc_recv_pdu_fragment: expected stub\n");
|
2012-12-12 08:34:51 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StubLength == 4)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
//fprintf(stderr, "Ignoring TsProxySendToServer Response\n");
|
2012-12-12 08:34:51 +04:00
|
|
|
rpc_client_fragment_pool_return(rpc, fragment);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
Stream_EnsureCapacity(rpc->client->pdu->s, header->response.alloc_hint);
|
2012-12-13 00:55:42 +04:00
|
|
|
buffer = (BYTE*) Stream_Buffer(fragment);
|
|
|
|
header = (rpcconn_hdr_t*) Stream_Buffer(fragment);
|
2012-12-12 04:17:57 +04:00
|
|
|
|
|
|
|
if (rpc->StubFragCount == 0)
|
|
|
|
rpc->StubCallId = header->common.call_id;
|
|
|
|
|
|
|
|
if (rpc->StubCallId != header->common.call_id)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "invalid call_id: actual: %d, expected: %d, frag_count: %d\n",
|
2012-12-12 04:17:57 +04:00
|
|
|
rpc->StubCallId, header->common.call_id, rpc->StubFragCount);
|
|
|
|
}
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
Stream_Write(rpc->client->pdu->s, &buffer[StubOffset], StubLength);
|
2012-12-12 04:17:57 +04:00
|
|
|
rpc->StubFragCount++;
|
|
|
|
|
|
|
|
rpc_client_fragment_pool_return(rpc, fragment);
|
|
|
|
|
|
|
|
if (rpc->VirtualConnection->DefaultOutChannel->ReceiverAvailableWindow < (rpc->ReceiveWindow / 2))
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
//fprintf(stderr, "Sending Flow Control Ack PDU\n");
|
2012-12-12 04:17:57 +04:00
|
|
|
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)
|
2012-12-12 04:17:57 +04:00
|
|
|
{
|
2012-12-13 07:03:40 +04:00
|
|
|
rpc->client->pdu->Flags = RPC_PDU_FLAG_STUB;
|
|
|
|
rpc->client->pdu->CallId = rpc->StubCallId;
|
2012-12-12 04:17:57 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Length(rpc->client->pdu->s) = Stream_GetPosition(rpc->client->pdu->s);
|
2012-12-12 04:17:57 +04:00
|
|
|
|
2013-03-14 20:57:45 +04:00
|
|
|
rpc->StubFragCount = 0;
|
|
|
|
rpc->StubCallId = 0;
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
Queue_Enqueue(rpc->client->ReceiveQueue, rpc->client->pdu);
|
2013-03-14 20:57:45 +04:00
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
rpc->client->pdu = NULL;
|
2012-12-12 04:17:57 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-11 03:56:53 +04:00
|
|
|
int rpc_client_on_read_event(rdpRpc* rpc)
|
2012-12-11 00:43:07 +04:00
|
|
|
{
|
|
|
|
int position;
|
|
|
|
int status = -1;
|
|
|
|
rpcconn_common_hdr_t* header;
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
if (!rpc->client->RecvFrag)
|
|
|
|
rpc->client->RecvFrag = rpc_client_fragment_pool_take(rpc);
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
position = Stream_GetPosition(rpc->client->RecvFrag);
|
2012-12-11 00:43:07 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if (Stream_GetPosition(rpc->client->RecvFrag) < RPC_COMMON_FIELDS_LENGTH)
|
2012-12-11 00:43:07 +04:00
|
|
|
{
|
2012-12-13 07:03:40 +04:00
|
|
|
status = rpc_out_read(rpc, Stream_Pointer(rpc->client->RecvFrag),
|
2013-04-30 06:35:15 +04:00
|
|
|
RPC_COMMON_FIELDS_LENGTH - Stream_GetPosition(rpc->client->RecvFrag));
|
2012-12-11 00:43:07 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "rpc_client_frag_read: error reading header\n");
|
2012-12-11 00:43:07 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
Stream_Seek(rpc->client->RecvFrag, status);
|
2012-12-11 00:43:07 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if (Stream_GetPosition(rpc->client->RecvFrag) >= RPC_COMMON_FIELDS_LENGTH)
|
2012-12-11 00:43:07 +04:00
|
|
|
{
|
2012-12-13 07:03:40 +04:00
|
|
|
header = (rpcconn_common_hdr_t*) Stream_Buffer(rpc->client->RecvFrag);
|
2012-12-11 00:43:07 +04:00
|
|
|
|
|
|
|
if (header->frag_length > rpc->max_recv_frag)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "rpc_client_frag_read: invalid fragment size: %d (max: %d)\n",
|
2012-12-11 00:43:07 +04:00
|
|
|
header->frag_length, rpc->max_recv_frag);
|
2013-04-30 06:35:15 +04:00
|
|
|
winpr_HexDump(Stream_Buffer(rpc->client->RecvFrag), Stream_GetPosition(rpc->client->RecvFrag));
|
2012-12-11 00:43:07 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if (Stream_GetPosition(rpc->client->RecvFrag) < header->frag_length)
|
2012-12-11 00:43:07 +04:00
|
|
|
{
|
2012-12-13 07:03:40 +04:00
|
|
|
status = rpc_out_read(rpc, Stream_Pointer(rpc->client->RecvFrag),
|
2013-04-30 06:35:15 +04:00
|
|
|
header->frag_length - Stream_GetPosition(rpc->client->RecvFrag));
|
2012-12-11 00:43:07 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "rpc_client_frag_read: error reading fragment body\n");
|
2012-12-11 00:43:07 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
Stream_Seek(rpc->client->RecvFrag, status);
|
2012-12-11 00:43:07 +04:00
|
|
|
}
|
|
|
|
}
|
2012-12-11 03:56:53 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return status;
|
|
|
|
}
|
2012-12-11 00:43:07 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
status = Stream_GetPosition(rpc->client->RecvFrag) - position;
|
2012-12-11 00:43:07 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if (Stream_GetPosition(rpc->client->RecvFrag) >= header->frag_length)
|
2012-12-11 00:43:07 +04:00
|
|
|
{
|
|
|
|
/* complete fragment received */
|
2012-12-11 03:56:53 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Length(rpc->client->RecvFrag) = Stream_GetPosition(rpc->client->RecvFrag);
|
2012-12-13 07:03:40 +04:00
|
|
|
Stream_SetPosition(rpc->client->RecvFrag, 0);
|
2012-12-11 03:56:53 +04:00
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
Queue_Enqueue(rpc->client->FragmentQueue, rpc->client->RecvFrag);
|
|
|
|
rpc->client->RecvFrag = NULL;
|
2012-12-12 04:17:57 +04:00
|
|
|
|
|
|
|
rpc_client_on_fragment_received_event(rpc);
|
2012-12-11 00:43:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-12-08 06:09:55 +04:00
|
|
|
/**
|
|
|
|
* [MS-RPCE] Client Call:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/gg593159/
|
|
|
|
*/
|
|
|
|
|
|
|
|
RpcClientCall* rpc_client_call_find_by_id(rdpRpc* rpc, UINT32 CallId)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int count;
|
2012-12-12 09:49:15 +04:00
|
|
|
RpcClientCall* clientCall;
|
2012-12-08 06:09:55 +04:00
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
ArrayList_Lock(rpc->client->ClientCallList);
|
2012-12-08 06:09:55 +04:00
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
clientCall = NULL;
|
2012-12-12 08:34:51 +04:00
|
|
|
count = ArrayList_Count(rpc->client->ClientCallList);
|
2012-12-08 06:09:55 +04:00
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
2012-12-12 09:49:15 +04:00
|
|
|
clientCall = (RpcClientCall*) ArrayList_GetItem(rpc->client->ClientCallList, index);
|
2012-12-08 06:09:55 +04:00
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
if (clientCall->CallId == CallId)
|
2012-12-08 06:09:55 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
ArrayList_Unlock(rpc->client->ClientCallList);
|
2012-12-08 06:09:55 +04:00
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
return clientCall;
|
2012-12-08 06:09:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
RpcClientCall* rpc_client_call_new(UINT32 CallId, UINT32 OpNum)
|
|
|
|
{
|
2012-12-12 09:49:15 +04:00
|
|
|
RpcClientCall* clientCall;
|
2012-12-08 06:09:55 +04:00
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
clientCall = (RpcClientCall*) malloc(sizeof(RpcClientCall));
|
2012-12-08 06:09:55 +04:00
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
if (clientCall)
|
2012-12-08 06:09:55 +04:00
|
|
|
{
|
2012-12-12 09:49:15 +04:00
|
|
|
clientCall->CallId = CallId;
|
|
|
|
clientCall->OpNum = OpNum;
|
|
|
|
clientCall->State = RPC_CLIENT_CALL_STATE_SEND_PDUS;
|
2012-12-08 06:09:55 +04:00
|
|
|
}
|
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
return clientCall;
|
2012-12-08 06:09:55 +04:00
|
|
|
}
|
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
void rpc_client_call_free(RpcClientCall* clientCall)
|
2012-12-08 06:09:55 +04:00
|
|
|
{
|
2012-12-12 09:49:15 +04:00
|
|
|
free(clientCall);
|
2012-12-08 06:09:55 +04:00
|
|
|
}
|
|
|
|
|
2012-11-28 21:47:04 +04:00
|
|
|
int rpc_send_enqueue_pdu(rdpRpc* rpc, BYTE* buffer, UINT32 length)
|
|
|
|
{
|
2012-11-29 05:30:03 +04:00
|
|
|
RPC_PDU* pdu;
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-12-06 01:04:01 +04:00
|
|
|
pdu = (RPC_PDU*) malloc(sizeof(RPC_PDU));
|
2012-12-13 00:55:42 +04:00
|
|
|
pdu->s = Stream_New(buffer, length);
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
Queue_Enqueue(rpc->client->SendQueue, pdu);
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-11-29 05:30:03 +04:00
|
|
|
if (rpc->client->SynchronousSend)
|
|
|
|
{
|
|
|
|
WaitForSingleObject(rpc->client->PduSentEvent, INFINITE);
|
|
|
|
ResetEvent(rpc->client->PduSentEvent);
|
|
|
|
}
|
|
|
|
|
2012-11-28 21:47:04 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_send_dequeue_pdu(rdpRpc* rpc)
|
|
|
|
{
|
|
|
|
int status;
|
2012-11-29 05:30:03 +04:00
|
|
|
RPC_PDU* pdu;
|
2012-12-12 09:49:15 +04:00
|
|
|
RpcClientCall* clientCall;
|
2012-12-08 06:09:55 +04:00
|
|
|
rpcconn_common_hdr_t* header;
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
pdu = (RPC_PDU*) Queue_Dequeue(rpc->client->SendQueue);
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-11-29 05:30:03 +04:00
|
|
|
if (!pdu)
|
2012-11-28 22:38:01 +04:00
|
|
|
return 0;
|
|
|
|
|
2012-11-29 05:30:03 +04:00
|
|
|
WaitForSingleObject(rpc->VirtualConnection->DefaultInChannel->Mutex, INFINITE);
|
|
|
|
|
2012-12-13 00:55:42 +04:00
|
|
|
status = rpc_in_write(rpc, Stream_Buffer(pdu->s), Stream_Length(pdu->s));
|
2012-11-29 05:30:03 +04:00
|
|
|
|
2012-12-13 00:55:42 +04:00
|
|
|
header = (rpcconn_common_hdr_t*) Stream_Buffer(pdu->s);
|
2012-12-12 09:49:15 +04:00
|
|
|
clientCall = rpc_client_call_find_by_id(rpc, header->call_id);
|
|
|
|
clientCall->State = RPC_CLIENT_CALL_STATE_DISPATCHED;
|
2012-12-08 06:09:55 +04:00
|
|
|
|
2012-11-29 05:30:03 +04:00
|
|
|
ReleaseMutex(rpc->VirtualConnection->DefaultInChannel->Mutex);
|
2012-11-28 21:47:04 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-12-13 07:03:40 +04:00
|
|
|
|
|
|
|
if (header->ptype == PTYPE_REQUEST)
|
|
|
|
{
|
|
|
|
rpc->VirtualConnection->DefaultInChannel->BytesSent += status;
|
|
|
|
rpc->VirtualConnection->DefaultInChannel->SenderAvailableWindow -= status;
|
|
|
|
}
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-12-13 00:55:42 +04:00
|
|
|
Stream_Free(pdu->s, TRUE);
|
2012-12-06 01:04:01 +04:00
|
|
|
free(pdu);
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-11-29 05:30:03 +04:00
|
|
|
if (rpc->client->SynchronousSend)
|
|
|
|
SetEvent(rpc->client->PduSentEvent);
|
2012-11-28 22:38:01 +04:00
|
|
|
|
2012-11-28 21:47:04 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-11-29 07:03:18 +04:00
|
|
|
RPC_PDU* rpc_recv_dequeue_pdu(rdpRpc* rpc)
|
2012-11-29 05:30:03 +04:00
|
|
|
{
|
2012-11-29 07:03:18 +04:00
|
|
|
RPC_PDU* pdu;
|
|
|
|
DWORD dwMilliseconds;
|
|
|
|
|
|
|
|
pdu = NULL;
|
|
|
|
dwMilliseconds = rpc->client->SynchronousReceive ? INFINITE : 0;
|
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
if (WaitForSingleObject(Queue_Event(rpc->client->ReceiveQueue), dwMilliseconds) == WAIT_OBJECT_0)
|
2012-11-29 07:03:18 +04:00
|
|
|
{
|
2012-12-12 08:34:51 +04:00
|
|
|
pdu = (RPC_PDU*) Queue_Dequeue(rpc->client->ReceiveQueue);
|
2013-03-14 20:57:45 +04:00
|
|
|
|
|
|
|
#ifdef WITH_DEBUG_TSG
|
|
|
|
if (pdu)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Receiving PDU (length: %d, CallId: %d)\n", pdu->s->length, pdu->CallId);
|
2013-03-14 20:57:45 +04:00
|
|
|
winpr_HexDump(pdu->s->buffer, pdu->s->length);
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "\n");
|
2013-03-14 20:57:45 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-29 07:03:18 +04:00
|
|
|
return pdu;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pdu;
|
2012-11-29 05:30:03 +04:00
|
|
|
}
|
|
|
|
|
2013-03-28 04:06:10 +04:00
|
|
|
RPC_PDU* rpc_recv_peek_pdu(rdpRpc* rpc)
|
|
|
|
{
|
|
|
|
RPC_PDU* pdu;
|
|
|
|
DWORD dwMilliseconds;
|
|
|
|
|
|
|
|
pdu = NULL;
|
|
|
|
dwMilliseconds = rpc->client->SynchronousReceive ? INFINITE : 0;
|
|
|
|
|
|
|
|
if (WaitForSingleObject(Queue_Event(rpc->client->ReceiveQueue), dwMilliseconds) == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
pdu = (RPC_PDU*) Queue_Peek(rpc->client->ReceiveQueue);
|
|
|
|
return pdu;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pdu;
|
|
|
|
}
|
|
|
|
|
2012-11-28 21:47:04 +04:00
|
|
|
static void* rpc_client_thread(void* arg)
|
|
|
|
{
|
|
|
|
rdpRpc* rpc;
|
|
|
|
DWORD status;
|
2012-11-28 22:38:01 +04:00
|
|
|
DWORD nCount;
|
2012-11-29 05:30:03 +04:00
|
|
|
HANDLE events[3];
|
|
|
|
HANDLE ReadEvent;
|
2012-11-28 21:47:04 +04:00
|
|
|
|
|
|
|
rpc = (rdpRpc*) arg;
|
|
|
|
|
2012-11-29 05:30:03 +04:00
|
|
|
ReadEvent = CreateFileDescriptorEvent(NULL, TRUE, FALSE, rpc->TlsOut->sockfd);
|
|
|
|
|
2012-11-28 22:38:01 +04:00
|
|
|
nCount = 0;
|
|
|
|
events[nCount++] = rpc->client->StopEvent;
|
2012-12-12 08:34:51 +04:00
|
|
|
events[nCount++] = Queue_Event(rpc->client->SendQueue);
|
2012-11-29 05:30:03 +04:00
|
|
|
events[nCount++] = ReadEvent;
|
2012-11-28 21:47:04 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2012-11-28 22:38:01 +04:00
|
|
|
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
|
2012-11-28 21:47:04 +04:00
|
|
|
|
|
|
|
if (WaitForSingleObject(rpc->client->StopEvent, 0) == WAIT_OBJECT_0)
|
2012-11-29 05:30:03 +04:00
|
|
|
{
|
2012-11-28 21:47:04 +04:00
|
|
|
break;
|
2012-11-29 05:30:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (WaitForSingleObject(ReadEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
2012-12-12 08:34:51 +04:00
|
|
|
if (rpc_client_on_read_event(rpc) < 0)
|
|
|
|
break;
|
2012-11-29 05:30:03 +04:00
|
|
|
}
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
if (WaitForSingleObject(Queue_Event(rpc->client->SendQueue), 0) == WAIT_OBJECT_0)
|
2012-12-11 03:56:53 +04:00
|
|
|
{
|
|
|
|
rpc_send_dequeue_pdu(rpc);
|
|
|
|
}
|
2012-11-28 21:47:04 +04:00
|
|
|
}
|
|
|
|
|
2012-11-29 05:30:03 +04:00
|
|
|
CloseHandle(ReadEvent);
|
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
rpc_client_free(rpc);
|
|
|
|
|
2012-11-28 21:47:04 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
static void rpc_pdu_free(RPC_PDU* pdu)
|
|
|
|
{
|
2012-12-13 00:55:42 +04:00
|
|
|
Stream_Free(pdu->s, TRUE);
|
2012-12-12 08:34:51 +04:00
|
|
|
free(pdu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpc_fragment_free(wStream* fragment)
|
|
|
|
{
|
|
|
|
Stream_Free(fragment, TRUE);
|
|
|
|
}
|
|
|
|
|
2012-11-29 05:30:03 +04:00
|
|
|
int rpc_client_new(rdpRpc* rpc)
|
2012-11-28 21:47:04 +04:00
|
|
|
{
|
2012-12-13 05:02:56 +04:00
|
|
|
RpcClient* client = NULL;
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-12-13 05:02:56 +04:00
|
|
|
client = (RpcClient*) malloc(sizeof(RpcClient));
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-12-13 05:02:56 +04:00
|
|
|
if (client)
|
|
|
|
{
|
|
|
|
client->Thread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) rpc_client_thread,
|
|
|
|
rpc, CREATE_SUSPENDED, NULL);
|
|
|
|
|
|
|
|
client->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
client->PduSentEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2012-11-28 21:47:04 +04:00
|
|
|
|
2012-12-13 05:02:56 +04:00
|
|
|
client->SendQueue = Queue_New(TRUE, -1, -1);
|
|
|
|
Queue_Object(client->SendQueue)->fnObjectFree = (OBJECT_FREE_FN) rpc_pdu_free;
|
2012-12-12 09:49:15 +04:00
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
client->pdu = NULL;
|
2012-12-13 05:02:56 +04:00
|
|
|
client->ReceivePool = Queue_New(TRUE, -1, -1);
|
|
|
|
client->ReceiveQueue = Queue_New(TRUE, -1, -1);
|
|
|
|
Queue_Object(client->ReceivePool)->fnObjectFree = (OBJECT_FREE_FN) rpc_pdu_free;
|
|
|
|
Queue_Object(client->ReceiveQueue)->fnObjectFree = (OBJECT_FREE_FN) rpc_pdu_free;
|
2012-12-12 08:34:51 +04:00
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
client->RecvFrag = NULL;
|
2012-12-13 05:02:56 +04:00
|
|
|
client->FragmentPool = Queue_New(TRUE, -1, -1);
|
|
|
|
client->FragmentQueue = Queue_New(TRUE, -1, -1);
|
2012-12-11 03:56:53 +04:00
|
|
|
|
2012-12-13 05:02:56 +04:00
|
|
|
Queue_Object(client->FragmentPool)->fnObjectFree = (OBJECT_FREE_FN) rpc_fragment_free;
|
|
|
|
Queue_Object(client->FragmentQueue)->fnObjectFree = (OBJECT_FREE_FN) rpc_fragment_free;
|
2012-12-12 08:34:51 +04:00
|
|
|
|
2012-12-13 05:02:56 +04:00
|
|
|
client->ClientCallList = ArrayList_New(TRUE);
|
|
|
|
ArrayList_Object(client->ClientCallList)->fnObjectFree = (OBJECT_FREE_FN) rpc_client_call_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpc->client = client;
|
2012-12-12 08:34:51 +04:00
|
|
|
|
2012-11-29 05:30:03 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_client_start(rdpRpc* rpc)
|
|
|
|
{
|
2012-11-28 21:47:04 +04:00
|
|
|
ResumeThread(rpc->client->Thread);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-12 08:34:51 +04:00
|
|
|
|
|
|
|
int rpc_client_stop(rdpRpc* rpc)
|
|
|
|
{
|
|
|
|
SetEvent(rpc->client->StopEvent);
|
|
|
|
|
2012-12-13 05:02:56 +04:00
|
|
|
WaitForSingleObject(rpc->client->Thread, INFINITE);
|
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_client_free(rdpRpc* rpc)
|
|
|
|
{
|
2012-12-13 05:02:56 +04:00
|
|
|
RpcClient* client;
|
2012-12-12 08:34:51 +04:00
|
|
|
|
2012-12-13 05:02:56 +04:00
|
|
|
client = rpc->client;
|
2012-12-12 09:49:15 +04:00
|
|
|
|
2012-12-13 05:02:56 +04:00
|
|
|
if (client)
|
|
|
|
{
|
|
|
|
Queue_Free(client->SendQueue);
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
if (client->RecvFrag)
|
|
|
|
rpc_fragment_free(client->RecvFrag);
|
|
|
|
|
|
|
|
Queue_Free(client->FragmentPool);
|
|
|
|
Queue_Free(client->FragmentQueue);
|
|
|
|
|
|
|
|
if (client->pdu)
|
|
|
|
rpc_pdu_free(client->pdu);
|
2012-12-13 05:02:56 +04:00
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
Queue_Free(client->ReceivePool);
|
2012-12-13 05:02:56 +04:00
|
|
|
Queue_Free(client->ReceiveQueue);
|
2012-12-12 08:34:51 +04:00
|
|
|
|
2012-12-13 05:02:56 +04:00
|
|
|
ArrayList_Free(client->ClientCallList);
|
2012-12-12 09:49:15 +04:00
|
|
|
|
2012-12-13 05:02:56 +04:00
|
|
|
CloseHandle(client->StopEvent);
|
|
|
|
CloseHandle(client->PduSentEvent);
|
|
|
|
|
2012-12-13 07:03:40 +04:00
|
|
|
CloseHandle(client->Thread);
|
|
|
|
|
2012-12-13 05:02:56 +04:00
|
|
|
free(client);
|
|
|
|
}
|
2012-12-12 09:49:15 +04:00
|
|
|
|
2012-12-12 08:34:51 +04:00
|
|
|
return 0;
|
|
|
|
}
|