2 * FreeRDP: A Remote Desktop Protocol Implementation
3 * Remote Assistance Virtual Channel
5 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
24 #include <winpr/crt.h>
25 #include <winpr/print.h>
26 #include <winpr/stream.h>
28 #include "remdesk_main.h"
30 int remdesk_virtual_channel_write(RemdeskServerContext* context, wStream* s)
33 ULONG BytesWritten = 0;
35 status = WTSVirtualChannelWrite(context->priv->ChannelHandle,
36 (PCHAR) Stream_Buffer(s), Stream_Length(s), &BytesWritten);
38 return (status) ? 1 : -1;
41 static int remdesk_read_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header)
44 UINT32 ChannelNameLen;
45 char* pChannelName = NULL;
47 if (Stream_GetRemainingLength(s) < 8)
50 Stream_Read_UINT32(s, ChannelNameLen); /* ChannelNameLen (4 bytes) */
51 Stream_Read_UINT32(s, header->DataLength); /* DataLen (4 bytes) */
53 if (ChannelNameLen > 64)
56 if ((ChannelNameLen % 2) != 0)
59 if (Stream_GetRemainingLength(s) < ChannelNameLen)
62 ZeroMemory(header->ChannelName, sizeof(header->ChannelName));
64 pChannelName = (char*) header->ChannelName;
65 status = ConvertFromUnicode(CP_UTF8, 0, (WCHAR*) Stream_Pointer(s),
66 ChannelNameLen / 2, &pChannelName, 32, NULL, NULL);
68 Stream_Seek(s, ChannelNameLen);
76 static int remdesk_write_channel_header(wStream* s, REMDESK_CHANNEL_HEADER* header)
79 UINT32 ChannelNameLen;
80 WCHAR ChannelNameW[32];
82 ZeroMemory(ChannelNameW, sizeof(ChannelNameW));
84 for (index = 0; index < 32; index++)
86 ChannelNameW[index] = (WCHAR) header->ChannelName[index];
89 ChannelNameLen = (strlen(header->ChannelName) + 1) * 2;
91 Stream_Write_UINT32(s, ChannelNameLen); /* ChannelNameLen (4 bytes) */
92 Stream_Write_UINT32(s, header->DataLength); /* DataLen (4 bytes) */
94 Stream_Write(s, ChannelNameW, ChannelNameLen); /* ChannelName (variable) */
99 static int remdesk_write_ctl_header(wStream* s, REMDESK_CTL_HEADER* ctlHeader)
101 remdesk_write_channel_header(s, (REMDESK_CHANNEL_HEADER*) ctlHeader);
102 Stream_Write_UINT32(s, ctlHeader->msgType); /* msgType (4 bytes) */
106 static int remdesk_prepare_ctl_header(REMDESK_CTL_HEADER* ctlHeader, UINT32 msgType, UINT32 msgSize)
108 ctlHeader->msgType = msgType;
109 strcpy(ctlHeader->ChannelName, REMDESK_CHANNEL_CTL_NAME);
110 ctlHeader->DataLength = 4 + msgSize;
114 static int remdesk_send_ctl_result_pdu(RemdeskServerContext* context, UINT32 result)
117 REMDESK_CTL_RESULT_PDU pdu;
121 remdesk_prepare_ctl_header(&(pdu.ctlHeader), REMDESK_CTL_RESULT, 4);
123 s = Stream_New(NULL, REMDESK_CHANNEL_CTL_SIZE + pdu.ctlHeader.DataLength);
125 remdesk_write_ctl_header(s, &(pdu.ctlHeader));
127 Stream_Write_UINT32(s, pdu.result); /* result (4 bytes) */
129 Stream_SealLength(s);
131 remdesk_virtual_channel_write(context, s);
133 Stream_Free(s, TRUE);
138 static int remdesk_send_ctl_version_info_pdu(RemdeskServerContext* context)
141 REMDESK_CTL_VERSION_INFO_PDU pdu;
143 remdesk_prepare_ctl_header(&(pdu.ctlHeader), REMDESK_CTL_VERSIONINFO, 8);
145 pdu.versionMajor = 1;
146 pdu.versionMinor = 2;
148 s = Stream_New(NULL, REMDESK_CHANNEL_CTL_SIZE + pdu.ctlHeader.DataLength);
150 remdesk_write_ctl_header(s, &(pdu.ctlHeader));
152 Stream_Write_UINT32(s, pdu.versionMajor); /* versionMajor (4 bytes) */
153 Stream_Write_UINT32(s, pdu.versionMinor); /* versionMinor (4 bytes) */
155 Stream_SealLength(s);
157 remdesk_virtual_channel_write(context, s);
162 static int remdesk_recv_ctl_version_info_pdu(RemdeskServerContext* context, wStream* s, REMDESK_CHANNEL_HEADER* header)
167 if (Stream_GetRemainingLength(s) < 8)
170 Stream_Read_UINT32(s, versionMajor); /* versionMajor (4 bytes) */
171 Stream_Read_UINT32(s, versionMinor); /* versionMinor (4 bytes) */
176 static int remdesk_recv_ctl_remote_control_desktop_pdu(RemdeskServerContext* context, wStream* s, REMDESK_CHANNEL_HEADER* header)
182 int cbRaConnectionStringW = 0;
183 WCHAR* raConnectionStringW = NULL;
184 REMDESK_CTL_REMOTE_CONTROL_DESKTOP_PDU pdu;
186 msgLength = header->DataLength - 4;
188 pStringW = (WCHAR*) Stream_Pointer(s);
189 raConnectionStringW = pStringW;
192 while ((msgLength > 0) && pStringW[cchStringW])
198 if (pStringW[cchStringW] || !cchStringW)
202 cbRaConnectionStringW = cchStringW * 2;
204 pdu.raConnectionString = NULL;
206 status = ConvertFromUnicode(CP_UTF8, 0, raConnectionStringW,
207 cbRaConnectionStringW / 2, &pdu.raConnectionString, 0, NULL, NULL);
212 WLog_INFO(TAG, "RaConnectionString: %s",
213 pdu.raConnectionString);
214 free(pdu.raConnectionString);
216 remdesk_send_ctl_result_pdu(context, 0);
221 static int remdesk_recv_ctl_authenticate_pdu(RemdeskServerContext* context, wStream* s, REMDESK_CHANNEL_HEADER* header)
227 int cbExpertBlobW = 0;
228 WCHAR* expertBlobW = NULL;
229 int cbRaConnectionStringW = 0;
230 WCHAR* raConnectionStringW = NULL;
231 REMDESK_CTL_AUTHENTICATE_PDU pdu;
233 msgLength = header->DataLength - 4;
235 pStringW = (WCHAR*) Stream_Pointer(s);
236 raConnectionStringW = pStringW;
239 while ((msgLength > 0) && pStringW[cchStringW])
245 if (pStringW[cchStringW] || !cchStringW)
249 cbRaConnectionStringW = cchStringW * 2;
251 pStringW += cchStringW;
252 expertBlobW = pStringW;
255 while ((msgLength > 0) && pStringW[cchStringW])
261 if (pStringW[cchStringW] || !cchStringW)
265 cbExpertBlobW = cchStringW * 2;
267 pdu.raConnectionString = NULL;
269 status = ConvertFromUnicode(CP_UTF8, 0, raConnectionStringW,
270 cbRaConnectionStringW / 2, &pdu.raConnectionString, 0, NULL, NULL);
275 pdu.expertBlob = NULL;
277 status = ConvertFromUnicode(CP_UTF8, 0, expertBlobW,
278 cbExpertBlobW / 2, &pdu.expertBlob, 0, NULL, NULL);
283 WLog_INFO(TAG, "RaConnectionString: %s ExpertBlob: %s",
284 pdu.raConnectionString, pdu.expertBlob);
285 free(pdu.raConnectionString);
286 free(pdu.expertBlob);
291 static int remdesk_recv_ctl_verify_password_pdu(RemdeskServerContext* context, wStream* s, REMDESK_CHANNEL_HEADER* header)
294 int cbExpertBlobW = 0;
295 WCHAR* expertBlobW = NULL;
296 REMDESK_CTL_VERIFY_PASSWORD_PDU pdu;
298 if (Stream_GetRemainingLength(s) < 8)
301 pdu.expertBlob = NULL;
302 expertBlobW = (WCHAR*) Stream_Pointer(s);
303 cbExpertBlobW = header->DataLength - 4;
305 status = ConvertFromUnicode(CP_UTF8, 0, expertBlobW, cbExpertBlobW / 2, &pdu.expertBlob, 0, NULL, NULL);
306 WLog_INFO(TAG, "ExpertBlob: %s", pdu.expertBlob);
307 remdesk_send_ctl_result_pdu(context, 0);
312 static int remdesk_recv_ctl_pdu(RemdeskServerContext* context, wStream* s, REMDESK_CHANNEL_HEADER* header)
317 if (Stream_GetRemainingLength(s) < 4)
320 Stream_Read_UINT32(s, msgType); /* msgType (4 bytes) */
321 WLog_INFO(TAG, "msgType: %d", msgType);
325 case REMDESK_CTL_REMOTE_CONTROL_DESKTOP:
326 status = remdesk_recv_ctl_remote_control_desktop_pdu(context, s, header);
329 case REMDESK_CTL_AUTHENTICATE:
330 status = remdesk_recv_ctl_authenticate_pdu(context, s, header);
333 case REMDESK_CTL_DISCONNECT:
336 case REMDESK_CTL_VERSIONINFO:
337 status = remdesk_recv_ctl_version_info_pdu(context, s, header);
340 case REMDESK_CTL_ISCONNECTED:
343 case REMDESK_CTL_VERIFY_PASSWORD:
344 status = remdesk_recv_ctl_verify_password_pdu(context, s, header);
347 case REMDESK_CTL_EXPERT_ON_VISTA:
350 case REMDESK_CTL_RANOVICE_NAME:
353 case REMDESK_CTL_RAEXPERT_NAME:
356 case REMDESK_CTL_TOKEN:
360 WLog_ERR(TAG, "remdesk_recv_control_pdu: unknown msgType: %d", msgType);
368 static int remdesk_server_receive_pdu(RemdeskServerContext* context, wStream* s)
371 REMDESK_CHANNEL_HEADER header;
374 WLog_INFO(TAG, "RemdeskReceive: %d", Stream_GetRemainingLength(s));
375 winpr_HexDump(Stream_Pointer(s), Stream_GetRemainingLength(s));
378 if (remdesk_read_channel_header(s, &header) < 0)
381 if (strcmp(header.ChannelName, "RC_CTL") == 0)
383 status = remdesk_recv_ctl_pdu(context, s, &header);
385 else if (strcmp(header.ChannelName, "70") == 0)
389 else if (strcmp(header.ChannelName, "71") == 0)
393 else if (strcmp(header.ChannelName, ".") == 0)
397 else if (strcmp(header.ChannelName, "1000.") == 0)
401 else if (strcmp(header.ChannelName, "RA_FX") == 0)
413 static void* remdesk_server_thread(void* arg)
424 RemdeskServerContext* context;
426 context = (RemdeskServerContext*) arg;
432 s = Stream_New(NULL, 4096);
434 if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer, &BytesReturned) == TRUE)
436 if (BytesReturned == sizeof(HANDLE))
437 CopyMemory(&ChannelEvent, buffer, sizeof(HANDLE));
439 WTSFreeMemory(buffer);
443 events[nCount++] = ChannelEvent;
444 events[nCount++] = context->priv->StopEvent;
446 remdesk_send_ctl_version_info_pdu(context);
450 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
452 if (WaitForSingleObject(context->priv->StopEvent, 0) == WAIT_OBJECT_0)
457 if (WTSVirtualChannelRead(context->priv->ChannelHandle, 0,
458 (PCHAR) Stream_Buffer(s), Stream_Capacity(s), &BytesReturned))
461 Stream_Seek(s, BytesReturned);
465 Stream_EnsureRemainingCapacity(s, BytesReturned);
468 if (Stream_GetPosition(s) >= 8)
470 pHeader = (UINT32*) Stream_Buffer(s);
471 PduLength = pHeader[0] + pHeader[1] + 8;
473 if (PduLength >= Stream_GetPosition(s))
475 Stream_SealLength(s);
476 Stream_SetPosition(s, 0);
477 remdesk_server_receive_pdu(context, s);
478 Stream_SetPosition(s, 0);
483 Stream_Free(s, TRUE);
488 static int remdesk_server_start(RemdeskServerContext* context)
490 context->priv->ChannelHandle = WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, "remdesk");
492 if (!context->priv->ChannelHandle)
495 context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
497 context->priv->Thread = CreateThread(NULL, 0,
498 (LPTHREAD_START_ROUTINE) remdesk_server_thread, (void*) context, 0, NULL);
503 static int remdesk_server_stop(RemdeskServerContext* context)
505 SetEvent(context->priv->StopEvent);
507 WaitForSingleObject(context->priv->Thread, INFINITE);
508 CloseHandle(context->priv->Thread);
513 RemdeskServerContext* remdesk_server_context_new(HANDLE vcm)
515 RemdeskServerContext* context;
517 context = (RemdeskServerContext*) calloc(1, sizeof(RemdeskServerContext));
523 context->Start = remdesk_server_start;
524 context->Stop = remdesk_server_stop;
526 context->priv = (RemdeskServerPrivate*) calloc(1, sizeof(RemdeskServerPrivate));
530 context->priv->Version = 1;
537 void remdesk_server_context_free(RemdeskServerContext* context)