Skip to content

Commit

Permalink
libfreerdp-core: update gateway code for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
awakecoding committed Jan 28, 2015
1 parent 5bf8b15 commit a9df86a
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 156 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ RelWithDebInfo
*.resource.txt
*.embed.manifest*
*.intermediate.manifest*
version.rc

# Binaries
*.a
Expand Down
41 changes: 23 additions & 18 deletions libfreerdp/core/gateway/rpc_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ RPC_PDU* rpc_client_receive_pool_take(rdpRpc* rpc)

if (!pdu)
{
pdu = (RPC_PDU*)malloc(sizeof(RPC_PDU));
pdu = (RPC_PDU*) malloc(sizeof(RPC_PDU));

if (!pdu)
return NULL;
Expand Down Expand Up @@ -311,9 +311,10 @@ RpcClientCall* rpc_client_call_find_by_id(rdpRpc* rpc, UINT32 CallId)
{
int index;
int count;
RpcClientCall* clientCall;
RpcClientCall* clientCall = NULL;

ArrayList_Lock(rpc->client->ClientCallList);
clientCall = NULL;

count = ArrayList_Count(rpc->client->ClientCallList);

for (index = 0; index < count; index++)
Expand All @@ -325,20 +326,23 @@ RpcClientCall* rpc_client_call_find_by_id(rdpRpc* rpc, UINT32 CallId)
}

ArrayList_Unlock(rpc->client->ClientCallList);

return clientCall;
}

RpcClientCall* rpc_client_call_new(UINT32 CallId, UINT32 OpNum)
{
RpcClientCall* clientCall;
clientCall = (RpcClientCall*) malloc(sizeof(RpcClientCall));

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;
}

Expand All @@ -349,8 +353,9 @@ void rpc_client_call_free(RpcClientCall* clientCall)

int rpc_send_enqueue_pdu(rdpRpc* rpc, BYTE* buffer, UINT32 length)
{
RPC_PDU* pdu;
int status;
RPC_PDU* pdu;

pdu = (RPC_PDU*) malloc(sizeof(RPC_PDU));

if (!pdu)
Expand Down Expand Up @@ -465,22 +470,26 @@ RPC_PDU* rpc_recv_peek_pdu(rdpRpc* rpc)
if (result != WAIT_OBJECT_0)
return NULL;

return (RPC_PDU*)Queue_Peek(rpc->client->ReceiveQueue);
return (RPC_PDU*) Queue_Peek(rpc->client->ReceiveQueue);
}

static void* rpc_client_thread(void* arg)
{
DWORD status;
DWORD nCount;
DWORD timeout;
HANDLE events[3];
HANDLE ReadEvent;
HANDLE ReadEvent = NULL;
rdpRpc* rpc = (rdpRpc*) arg;

if (!BIO_get_event(rpc->TlsOut->bio, &ReadEvent))
{
WLog_ERR(TAG, "rpc_client_thread: failed to obtain read event from underlying BIO");
goto out;
}
BIO_get_event(rpc->TlsOut->bio, &ReadEvent);

#ifndef _WIN32
timeout = INFINITE;
#else
timeout = 100;
BIO_set_nonblock(rpc->TlsOut->bio, TRUE);
#endif

nCount = 0;
events[nCount++] = rpc->client->StopEvent;
Expand All @@ -489,15 +498,12 @@ static void* rpc_client_thread(void* arg)

while (rpc->transport->layer != TRANSPORT_LAYER_CLOSED)
{
status = WaitForMultipleObjects(nCount, events, FALSE, 100);

if (status == WAIT_TIMEOUT)
continue;
status = WaitForMultipleObjects(nCount, events, FALSE, timeout);

if (WaitForSingleObject(rpc->client->StopEvent, 0) == WAIT_OBJECT_0)
break;

if (WaitForSingleObject(ReadEvent, 0) == WAIT_OBJECT_0)
if ((WaitForSingleObject(ReadEvent, 0) == WAIT_OBJECT_0) || (status == WAIT_TIMEOUT))
{
if (rpc_client_on_read_event(rpc) < 0)
{
Expand All @@ -512,7 +518,6 @@ static void* rpc_client_thread(void* arg)
}
}

out:
return NULL;
}

Expand Down
106 changes: 106 additions & 0 deletions libfreerdp/core/gateway/tsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1617,3 +1617,109 @@ void tsg_free(rdpTsg* tsg)
free(tsg);
}
}

long transport_bio_tsg_callback(BIO* bio, int mode, const char* argp, int argi, long argl, long ret)
{
return 1;
}

static int transport_bio_tsg_write(BIO* bio, const char* buf, int num)
{
int status;
rdpTsg* tsg = (rdpTsg*) bio->ptr;
BIO_clear_flags(bio, BIO_FLAGS_WRITE);
status = tsg_write(tsg, (BYTE*) buf, num);

if (status < 0)
{
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
}
else if (status == 0)
{
BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
WSASetLastError(WSAEWOULDBLOCK);
}
else
{
BIO_set_flags(bio, BIO_FLAGS_WRITE);
}

return status >= 0 ? status : -1;
}

static int transport_bio_tsg_read(BIO* bio, char* buf, int size)
{
int status;
rdpTsg* tsg = (rdpTsg*) bio->ptr;
BIO_clear_flags(bio, BIO_FLAGS_READ);
status = tsg_read(tsg, (BYTE*) buf, size);

if (status < 0)
{
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
}
else if (status == 0)
{
BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
WSASetLastError(WSAEWOULDBLOCK);
}
else
{
BIO_set_flags(bio, BIO_FLAGS_READ);
}

return status > 0 ? status : -1;
}

static int transport_bio_tsg_puts(BIO* bio, const char* str)
{
return 1;
}

static int transport_bio_tsg_gets(BIO* bio, char* str, int size)
{
return 1;
}

static long transport_bio_tsg_ctrl(BIO* bio, int cmd, long arg1, void* arg2)
{
if (cmd == BIO_CTRL_FLUSH)
{
return 1;
}

return 0;
}

static int transport_bio_tsg_new(BIO* bio)
{
bio->init = 1;
bio->num = 0;
bio->ptr = NULL;
bio->flags = BIO_FLAGS_SHOULD_RETRY;
return 1;
}

static int transport_bio_tsg_free(BIO* bio)
{
return 1;
}

static BIO_METHOD transport_bio_tsg_methods =
{
BIO_TYPE_TSG,
"TSGateway",
transport_bio_tsg_write,
transport_bio_tsg_read,
transport_bio_tsg_puts,
transport_bio_tsg_gets,
transport_bio_tsg_ctrl,
transport_bio_tsg_new,
transport_bio_tsg_free,
NULL,
};

BIO_METHOD* BIO_s_tsg(void)
{
return &transport_bio_tsg_methods;
}
2 changes: 2 additions & 0 deletions libfreerdp/core/gateway/tsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,6 @@ BOOL tsg_set_blocking_mode(rdpTsg* tsg, BOOL blocking);
rdpTsg* tsg_new(rdpTransport* transport);
void tsg_free(rdpTsg* tsg);

BIO_METHOD* BIO_s_tsg(void);

#endif /* FREERDP_CORE_TSG_H */
2 changes: 1 addition & 1 deletion libfreerdp/core/listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static BOOL freerdp_listener_open(freerdp_listener* instance, const char* bind_a
if (status != 0)
{
#ifdef _WIN32
WLog_ERR("bind() failed with error: %u", WSAGetLastError());
WLog_ERR(TAG, "bind() failed with error: %d", (int) WSAGetLastError());
WSACleanup();
#else
WLog_ERR(TAG, "bind");
Expand Down
Loading

0 comments on commit a9df86a

Please sign in to comment.