libfreerdp-core: TSG cleanup

This commit is contained in:
Marc-André Moreau 2012-12-13 20:23:37 -05:00
parent 85b023bb08
commit b53bdba143
6 changed files with 90 additions and 78 deletions

View File

@ -23,6 +23,7 @@
#include <winpr/crt.h>
#include <winpr/print.h>
#include <winpr/stream.h>
#include "http.h"
@ -200,10 +201,10 @@ char* http_encode_authorization_line(char* AuthScheme, char* AuthParam)
return line;
}
STREAM* http_request_write(HttpContext* http_context, HttpRequest* http_request)
wStream* http_request_write(HttpContext* http_context, HttpRequest* http_request)
{
int i;
STREAM* s;
wStream* s;
int length = 0;
http_request->count = 9;
@ -234,21 +235,21 @@ STREAM* http_request_write(HttpContext* http_context, HttpRequest* http_request)
length += 2; /* empty line "\r\n" at end of header */
length += 1; /* null terminator */
s = stream_new(length);
s = Stream_New(NULL, length);
for (i = 0; i < http_request->count; i++)
{
stream_write(s, http_request->lines[i], strlen(http_request->lines[i]));
stream_write(s, "\r\n", 2);
Stream_Write(s, http_request->lines[i], strlen(http_request->lines[i]));
Stream_Write(s, "\r\n", 2);
free(http_request->lines[i]);
}
stream_write(s, "\r\n", 2);
Stream_Write(s, "\r\n", 2);
free(http_request->lines);
stream_write(s, "\0", 1); /* append null terminator */
stream_rewind(s, 1); /* don't include null terminator in length */
stream_seal(s);
Stream_Write(s, "\0", 1); /* append null terminator */
Stream_Rewind(s, 1); /* don't include null terminator in length */
Stream_Length(s) = Stream_Position(s);
return s;
}

View File

@ -26,7 +26,8 @@ typedef struct _http_response HttpResponse;
#include <freerdp/types.h>
#include <freerdp/crypto/tls.h>
#include <freerdp/utils/stream.h>
#include <winpr/stream.h>
struct _http_context
{
@ -71,7 +72,7 @@ void http_request_set_uri(HttpRequest* http_request, char* uri);
void http_request_set_auth_scheme(HttpRequest* http_request, char* auth_scheme);
void http_request_set_auth_param(HttpRequest* http_request, char* auth_param);
STREAM* http_request_write(HttpContext* http_context, HttpRequest* http_request);
wStream* http_request_write(HttpContext* http_context, HttpRequest* http_request);
HttpRequest* http_request_new();
void http_request_free(HttpRequest* http_request);

View File

@ -25,13 +25,14 @@
#include <winpr/crt.h>
#include <winpr/tchar.h>
#include <winpr/stream.h>
#include <winpr/dsparse.h>
#include <openssl/rand.h>
STREAM* rpc_ntlm_http_request(rdpRpc* rpc, SecBuffer* ntlm_token, int content_length, TSG_CHANNEL channel)
wStream* rpc_ntlm_http_request(rdpRpc* rpc, SecBuffer* ntlm_token, int content_length, TSG_CHANNEL channel)
{
STREAM* s;
wStream* s;
char* base64_ntlm_token;
HttpContext* http_context;
HttpRequest* http_request;
@ -70,7 +71,7 @@ STREAM* rpc_ntlm_http_request(rdpRpc* rpc, SecBuffer* ntlm_token, int content_le
int rpc_ncacn_http_send_in_channel_request(rdpRpc* rpc)
{
STREAM* s;
wStream* s;
int content_length;
BOOL continue_needed;
rdpNtlm* ntlm = rpc->NtlmHttpIn->ntlm;
@ -81,9 +82,9 @@ int rpc_ncacn_http_send_in_channel_request(rdpRpc* rpc)
s = rpc_ntlm_http_request(rpc, &ntlm->outputBuffer, content_length, TSG_CHANNEL_IN);
DEBUG_RPC("\n%s", s->data);
rpc_in_write(rpc, s->data, s->size);
stream_free(s);
DEBUG_RPC("\n%s", Stream_Buffer(s));
rpc_in_write(rpc, Stream_Buffer(s), Stream_Length(s));
Stream_Free(s, TRUE);
return 0;
}
@ -166,7 +167,7 @@ BOOL rpc_ntlm_http_in_connect(rdpRpc* rpc)
int rpc_ncacn_http_send_out_channel_request(rdpRpc* rpc)
{
STREAM* s;
wStream* s;
int content_length;
BOOL continue_needed;
rdpNtlm* ntlm = rpc->NtlmHttpOut->ntlm;
@ -177,9 +178,9 @@ int rpc_ncacn_http_send_out_channel_request(rdpRpc* rpc)
s = rpc_ntlm_http_request(rpc, &ntlm->outputBuffer, content_length, TSG_CHANNEL_OUT);
DEBUG_RPC("\n%s", s->data);
rpc_out_write(rpc, s->data, s->size);
stream_free(s);
DEBUG_RPC("\n%s", Stream_Buffer(s));
rpc_out_write(rpc, Stream_Buffer(s), Stream_Length(s));
Stream_Free(s, TRUE);
return 0;
}

View File

@ -22,12 +22,12 @@
#include <freerdp/types.h>
#include <freerdp/settings.h>
#include <freerdp/crypto/tls.h>
#include <freerdp/crypto/crypto.h>
#include <freerdp/utils/sleep.h>
#include <freerdp/utils/debug.h>
#include <freerdp/utils/stream.h>
#include <freerdp/utils/hexdump.h>
#include "rpc.h"

View File

@ -26,14 +26,13 @@
#include <stdlib.h>
#include <string.h>
#include <freerdp/utils/sleep.h>
#include <freerdp/utils/stream.h>
#include <freerdp/utils/hexdump.h>
#include <freerdp/utils/unicode.h>
#include <winpr/crt.h>
#include <winpr/ndr.h>
#include <winpr/error.h>
#include <winpr/print.h>
#include <winpr/stream.h>
#include "rpc_client.h"
@ -57,10 +56,11 @@ BYTE TsProxyCreateTunnelUnknownTrailerBytes[60] =
DWORD TsProxySendToServer(handle_t IDL_handle, byte pRpcMessage[], UINT32 count, UINT32* lengths)
{
STREAM* s;
wStream* s;
int status;
int length;
rdpTsg* tsg;
BYTE* buffer;
UINT32 length;
byte* buffer1 = NULL;
byte* buffer2 = NULL;
byte* buffer3 = NULL;
@ -97,35 +97,37 @@ DWORD TsProxySendToServer(handle_t IDL_handle, byte pRpcMessage[], UINT32 count,
totalDataBytes += lengths[2] + 4;
}
s = stream_new(28 + totalDataBytes);
length = 28 + totalDataBytes;
buffer = (BYTE*) malloc(length);
s = Stream_New(buffer, length);
/* PCHANNEL_CONTEXT_HANDLE_NOSERIALIZE_NR (20 bytes) */
stream_write(s, &tsg->ChannelContext.ContextType, 4); /* ContextType (4 bytes) */
stream_write(s, tsg->ChannelContext.ContextUuid, 16); /* ContextUuid (16 bytes) */
Stream_Write(s, &tsg->ChannelContext.ContextType, 4); /* ContextType (4 bytes) */
Stream_Write(s, tsg->ChannelContext.ContextUuid, 16); /* ContextUuid (16 bytes) */
stream_write_UINT32_be(s, totalDataBytes); /* totalDataBytes (4 bytes) */
stream_write_UINT32_be(s, numBuffers); /* numBuffers (4 bytes) */
Stream_Write_UINT32_BE(s, totalDataBytes); /* totalDataBytes (4 bytes) */
Stream_Write_UINT32_BE(s, numBuffers); /* numBuffers (4 bytes) */
if (buffer1Length > 0)
stream_write_UINT32_be(s, buffer1Length); /* buffer1Length (4 bytes) */
Stream_Write_UINT32_BE(s, buffer1Length); /* buffer1Length (4 bytes) */
if (buffer2Length > 0)
stream_write_UINT32_be(s, buffer2Length); /* buffer2Length (4 bytes) */
Stream_Write_UINT32_BE(s, buffer2Length); /* buffer2Length (4 bytes) */
if (buffer3Length > 0)
stream_write_UINT32_be(s, buffer3Length); /* buffer3Length (4 bytes) */
Stream_Write_UINT32_BE(s, buffer3Length); /* buffer3Length (4 bytes) */
if (buffer1Length > 0)
stream_write(s, buffer1, buffer1Length); /* buffer1 (variable) */
Stream_Write(s, buffer1, buffer1Length); /* buffer1 (variable) */
if (buffer2Length > 0)
stream_write(s, buffer2, buffer2Length); /* buffer2 (variable) */
Stream_Write(s, buffer2, buffer2Length); /* buffer2 (variable) */
if (buffer3Length > 0)
stream_write(s, buffer3, buffer3Length); /* buffer3 (variable) */
Stream_Write(s, buffer3, buffer3Length); /* buffer3 (variable) */
stream_seal(s);
Stream_Length(s) = Stream_Position(s);
length = s->size;
status = rpc_write(tsg->rpc, s->data, s->size, TsProxySendToServerOpnum);
status = rpc_write(tsg->rpc, Stream_Buffer(s), Stream_Length(s), TsProxySendToServerOpnum);
stream_free(s);
Stream_Free(s, TRUE);
if (status <= 0)
{
@ -248,7 +250,7 @@ BOOL TsProxyCreateTunnelReadResponse(rdpTsg* tsg)
Pointer = *((UINT32*) &buffer[offset]); /* Ptr */
offset += 4;
if (Pointer == 0x0002000C)
if ((Pointer == 0x0002000C) || (Pointer == 0x00020008))
{
/* Not sure exactly what this is */
offset += 4; /* 0x00000001 (4 bytes) */
@ -257,7 +259,7 @@ BOOL TsProxyCreateTunnelReadResponse(rdpTsg* tsg)
offset += 4; /* 0x00000001 (4 bytes) */
}
if (packetCapsResponse->pktQuarEncResponse.certChainLen)
if (packetCapsResponse->pktQuarEncResponse.certChainLen > 0)
{
Pointer = *((UINT32*) &buffer[offset]); /* Ptr (4 bytes): 0x00020014 */
offset += 4;
@ -267,12 +269,10 @@ BOOL TsProxyCreateTunnelReadResponse(rdpTsg* tsg)
count = *((UINT32*) &buffer[offset]); /* ActualCount (4 bytes) */
offset += 4;
freerdp_hexdump(&buffer[offset], (count * 2));
/*
* CertChainData is a wide character string, and the count is
* given in characters excluding the null terminator, therefore:
* size = ((count + 1) * 2)
* size = (count * 2)
*/
offset += (count * 2); /* CertChainData */
@ -338,11 +338,9 @@ BOOL TsProxyCreateTunnelReadResponse(rdpTsg* tsg)
CopyMemory(tsg->TunnelContext.ContextUuid, &buffer[offset + 4], 16); /* ContextUuid */
offset += 20;
/* TODO: trailing bytes */
#ifdef WITH_DEBUG_TSG
printf("TSG TunnelContext:\n");
freerdp_hexdump((void*) &tsg->TunnelContext, 20);
winpr_HexDump((void*) &tsg->TunnelContext, 20);
printf("\n");
#endif
@ -376,7 +374,7 @@ BOOL TsProxyCreateTunnelReadResponse(rdpTsg* tsg)
/*
* CertChainData is a wide character string, and the count is
* given in characters excluding the null terminator, therefore:
* size = ((count + 1) * 2)
* size = (count * 2)
*/
offset += (count * 2); /* CertChainData */
@ -425,11 +423,9 @@ BOOL TsProxyCreateTunnelReadResponse(rdpTsg* tsg)
CopyMemory(tsg->TunnelContext.ContextUuid, &buffer[offset + 4], 16); /* ContextUuid */
offset += 20;
/* TODO: trailing bytes */
#ifdef WITH_DEBUG_TSG
printf("TSG TunnelContext:\n");
freerdp_hexdump((void*) &tsg->TunnelContext, 20);
winpr_HexDump((void*) &tsg->TunnelContext, 20);
printf("\n");
#endif
@ -480,7 +476,7 @@ BOOL TsProxyCreateTunnel(rdpTsg* tsg, PTSG_PACKET tsgPacket, PTSG_PACKET* tsgPac
return TRUE;
}
BOOL TsProxyAuthorizeTunnelWriteRequest(rdpTsg* tsg)
BOOL TsProxyAuthorizeTunnelWriteRequest(rdpTsg* tsg, PTUNNEL_CONTEXT_HANDLE_NOSERIALIZE tunnelContext)
{
UINT32 pad;
int status;
@ -488,6 +484,7 @@ BOOL TsProxyAuthorizeTunnelWriteRequest(rdpTsg* tsg)
UINT32 count;
UINT32 length;
UINT32 offset;
CONTEXT_HANDLE* handle;
rdpRpc* rpc = tsg->rpc;
count = _wcslen(tsg->MachineName) + 1;
@ -500,8 +497,9 @@ BOOL TsProxyAuthorizeTunnelWriteRequest(rdpTsg* tsg)
buffer = (BYTE*) malloc(length);
/* TunnelContext */
CopyMemory(&buffer[0], &tsg->TunnelContext.ContextType, 4); /* ContextType */
CopyMemory(&buffer[4], tsg->TunnelContext.ContextUuid, 16); /* ContextUuid */
handle = (CONTEXT_HANDLE*) tunnelContext;
CopyMemory(&buffer[0], &handle->ContextType, 4); /* ContextType */
CopyMemory(&buffer[4], handle->ContextUuid, 16); /* ContextUuid */
/* 4-byte alignment */
@ -620,8 +618,6 @@ BOOL TsProxyAuthorizeTunnelReadResponse(rdpTsg* tsg)
offset += SizeValue; /* ResponseData */
/* TODO: trailing bytes */
rpc_client_receive_pool_return(rpc, pdu);
free(packetResponse);
free(packet);
@ -645,7 +641,7 @@ BOOL TsProxyAuthorizeTunnel(rdpTsg* tsg, PTUNNEL_CONTEXT_HANDLE_NOSERIALIZE tunn
DEBUG_TSG("TsProxyAuthorizeTunnel");
if (!TsProxyAuthorizeTunnelWriteRequest(tsg))
if (!TsProxyAuthorizeTunnelWriteRequest(tsg, tunnelContext))
{
printf("TsProxyAuthorizeTunnel: error writing request\n");
return FALSE;
@ -660,19 +656,21 @@ BOOL TsProxyAuthorizeTunnel(rdpTsg* tsg, PTUNNEL_CONTEXT_HANDLE_NOSERIALIZE tunn
return TRUE;
}
BOOL TsProxyMakeTunnelCallWriteRequest(rdpTsg* tsg, unsigned long procId)
BOOL TsProxyMakeTunnelCallWriteRequest(rdpTsg* tsg, PTUNNEL_CONTEXT_HANDLE_NOSERIALIZE tunnelContext, unsigned long procId)
{
int status;
BYTE* buffer;
UINT32 length;
CONTEXT_HANDLE* handle;
rdpRpc* rpc = tsg->rpc;
length = 40;
buffer = (BYTE*) malloc(length);
/* TunnelContext */
CopyMemory(&buffer[0], &tsg->TunnelContext.ContextType, 4); /* ContextType */
CopyMemory(&buffer[4], tsg->TunnelContext.ContextUuid, 16); /* ContextUuid */
handle = (CONTEXT_HANDLE*) tunnelContext;
CopyMemory(&buffer[0], &handle->ContextType, 4); /* ContextType */
CopyMemory(&buffer[4], handle->ContextUuid, 16); /* ContextUuid */
*((UINT32*) &buffer[20]) = procId; /* ProcId */
@ -718,7 +716,7 @@ BOOL TsProxyMakeTunnelCall(rdpTsg* tsg, PTUNNEL_CONTEXT_HANDLE_NOSERIALIZE tunne
DEBUG_TSG("TsProxyMakeTunnelCall");
if (!TsProxyMakeTunnelCallWriteRequest(tsg, procId))
if (!TsProxyMakeTunnelCallWriteRequest(tsg, tunnelContext, procId))
{
printf("TsProxyMakeTunnelCall: error writing request\n");
return FALSE;
@ -733,19 +731,20 @@ BOOL TsProxyMakeTunnelCall(rdpTsg* tsg, PTUNNEL_CONTEXT_HANDLE_NOSERIALIZE tunne
return TRUE;
}
BOOL TsProxyCreateChannelWriteRequest(rdpTsg* tsg)
BOOL TsProxyCreateChannelWriteRequest(rdpTsg* tsg, PTUNNEL_CONTEXT_HANDLE_NOSERIALIZE tunnelContext)
{
int status;
UINT32 count;
BYTE* buffer;
UINT32 length;
CONTEXT_HANDLE* handle;
rdpRpc* rpc = tsg->rpc;
count = _wcslen(tsg->Hostname) + 1;
#ifdef WITH_DEBUG_TSG
printf("ResourceName:\n");
freerdp_hexdump((BYTE*) tsg->Hostname, (count - 1) * 2);
winpr_HexDump((BYTE*) tsg->Hostname, (count - 1) * 2);
printf("\n");
#endif
@ -754,8 +753,9 @@ BOOL TsProxyCreateChannelWriteRequest(rdpTsg* tsg)
buffer = (BYTE*) malloc(length);
/* TunnelContext */
CopyMemory(&buffer[0], &tsg->TunnelContext.ContextType, 4); /* ContextType */
CopyMemory(&buffer[4], tsg->TunnelContext.ContextUuid, 16); /* ContextUuid */
handle = (CONTEXT_HANDLE*) tunnelContext;
CopyMemory(&buffer[0], &handle->ContextType, 4); /* ContextType */
CopyMemory(&buffer[4], handle->ContextUuid, 16); /* ContextUuid */
/* TSENDPOINTINFO */
@ -811,11 +811,9 @@ BOOL TsProxyCreateChannelReadResponse(rdpTsg* tsg)
CopyMemory(&tsg->ChannelContext.ContextType, &buffer[offset], 4); /* ContextType (4 bytes) */
CopyMemory(tsg->ChannelContext.ContextUuid, &buffer[offset + 4], 16); /* ContextUuid (16 bytes) */
/* TODO: trailing bytes */
#ifdef WITH_DEBUG_TSG
printf("ChannelContext:\n");
freerdp_hexdump((void*) &tsg->ChannelContext, 20);
winpr_HexDump((void*) &tsg->ChannelContext, 20);
printf("\n");
#endif
@ -840,7 +838,7 @@ BOOL TsProxyCreateChannel(rdpTsg* tsg, PTUNNEL_CONTEXT_HANDLE_NOSERIALIZE tunnel
DEBUG_TSG("TsProxyCreateChannel");
if (!TsProxyCreateChannelWriteRequest(tsg))
if (!TsProxyCreateChannelWriteRequest(tsg, tunnelContext))
{
printf("TsProxyCreateChannel: error writing request\n");
return FALSE;
@ -907,6 +905,12 @@ BOOL TsProxyCloseChannelReadResponse(rdpTsg* tsg)
HRESULT TsProxyCloseChannel(rdpTsg* tsg, PCHANNEL_CONTEXT_HANDLE_NOSERIALIZE* context)
{
/**
* HRESULT TsProxyCloseChannel(
* [in, out] PCHANNEL_CONTEXT_HANDLE_NOSERIALIZE* context
* );
*/
DEBUG_TSG("TsProxyCloseChannel");
if (!TsProxyCloseChannelWriteRequest(tsg, context))
@ -976,6 +980,12 @@ BOOL TsProxyCloseTunnelReadResponse(rdpTsg* tsg)
HRESULT TsProxyCloseTunnel(rdpTsg* tsg, PTUNNEL_CONTEXT_HANDLE_SERIALIZE* context)
{
/**
* HRESULT TsProxyCloseTunnel(
* [in, out] PTUNNEL_CONTEXT_HANDLE_SERIALIZE* context
* );
*/
DEBUG_TSG("TsProxyCloseTunnel");
if (!TsProxyCloseTunnelWriteRequest(tsg, context))
@ -1156,7 +1166,7 @@ BOOL tsg_connect(rdpTsg* tsg, const char* hostname, UINT16 port)
* section 3.6.2.1.1 and SHOULD end the protocol when the connection has been idle for the specified Idle Timeout Value.
*/
if (!TsProxyAuthorizeTunnel(tsg, NULL, NULL, NULL))
if (!TsProxyAuthorizeTunnel(tsg, &tsg->TunnelContext, NULL, NULL))
{
tsg->state = TSG_STATE_TUNNEL_CLOSE_PENDING;
return FALSE;
@ -1172,7 +1182,7 @@ BOOL tsg_connect(rdpTsg* tsg, const char* hostname, UINT16 port)
*
*/
if (!TsProxyMakeTunnelCall(tsg, NULL, TSG_TUNNEL_CALL_ASYNC_MSG_REQUEST, NULL, NULL))
if (!TsProxyMakeTunnelCall(tsg, &tsg->TunnelContext, TSG_TUNNEL_CALL_ASYNC_MSG_REQUEST, NULL, NULL))
return FALSE;
/**
@ -1191,7 +1201,7 @@ BOOL tsg_connect(rdpTsg* tsg, const char* hostname, UINT16 port)
* out parameter. This Channel Context Handle is used for subsequent channel-related calls.
*/
if (!TsProxyCreateChannel(tsg, NULL, NULL, NULL, NULL))
if (!TsProxyCreateChannel(tsg, &tsg->TunnelContext, NULL, NULL, NULL))
return FALSE;
tsg->state = TSG_STATE_CHANNEL_CREATED;
@ -1249,7 +1259,7 @@ BOOL tsg_disconnect(rdpTsg* tsg)
if (!TsProxyCloseChannel(tsg, NULL))
return FALSE;
if (!TsProxyMakeTunnelCall(tsg, NULL, TSG_TUNNEL_CANCEL_ASYNC_MSG_REQUEST, NULL, NULL))
if (!TsProxyMakeTunnelCall(tsg, &tsg->TunnelContext, TSG_TUNNEL_CANCEL_ASYNC_MSG_REQUEST, NULL, NULL))
return FALSE;
if (!TsProxyCloseTunnel(tsg, NULL))

View File

@ -37,7 +37,6 @@ typedef struct rdp_tsg rdpTsg;
#include <freerdp/types.h>
#include <freerdp/settings.h>
#include <freerdp/utils/stream.h>
#include <freerdp/utils/debug.h>
enum _TSG_STATE