22#include <freerdp/config.h>
27#include <winpr/winpr.h>
29#include <winpr/cast.h>
30#include <winpr/assert.h>
32#include <winpr/synch.h>
33#include <winpr/file.h>
34#include <winpr/string.h>
35#include <winpr/path.h>
36#include <winpr/image.h>
37#include <winpr/winsock.h>
39#include <freerdp/streamdump.h>
40#include <freerdp/transport_io.h>
42#include <freerdp/channels/wtsvc.h>
43#include <freerdp/channels/channels.h>
44#include <freerdp/channels/drdynvc.h>
46#include <freerdp/freerdp.h>
47#include <freerdp/constants.h>
48#include <freerdp/server/rdpsnd.h>
49#include <freerdp/settings.h>
50#include <freerdp/build-config.h>
51#include <freerdp/version.h>
52#include <freerdp/utils/helpers.h>
57#include "sf_encomsp.h"
62#include <freerdp/log.h>
63#define TAG SERVER_TAG("sample")
65#define SAMPLE_SERVER_USE_CLIENT_RESOLUTION 1
66#define SAMPLE_SERVER_DEFAULT_WIDTH 1024
67#define SAMPLE_SERVER_DEFAULT_HEIGHT 768
71 BOOL test_dump_rfx_realtime;
72 const char* test_pcap_file;
73 const char* replay_dump;
76#if defined(WITH_RDPEUDP_SERVER)
81static void test_peer_context_free(freerdp_peer* client, rdpContext* ctx)
83 testPeerContext* context = (testPeerContext*)ctx;
89 winpr_image_free(context->image, TRUE);
90 if (context->debug_channel_thread)
92 WINPR_ASSERT(context->stopEvent);
93 (void)SetEvent(context->stopEvent);
94 (void)WaitForSingleObject(context->debug_channel_thread, INFINITE);
95 (void)CloseHandle(context->debug_channel_thread);
98 Stream_Free(context->s, TRUE);
99 free(context->bg_data);
100 rfx_context_free(context->rfx_context);
101 nsc_context_free(context->nsc_context);
103 if (context->debug_channel)
104 (void)WTSVirtualChannelClose(context->debug_channel);
106 sf_peer_audin_uninit(context);
108#if defined(WITH_RDPEUDP_SERVER)
109 sf_udp_free(context);
112#if defined(CHANNEL_AINPUT_SERVER)
113 sf_peer_ainput_uninit(context);
116 rdpsnd_server_context_free(context->rdpsnd);
117 encomsp_server_context_free(context->encomsp);
119 WTSCloseServer(context->vcm);
124static BOOL test_peer_context_new(freerdp_peer* client, rdpContext* ctx)
126 testPeerContext* context = (testPeerContext*)ctx;
128 WINPR_ASSERT(client);
129 WINPR_ASSERT(context);
130 WINPR_ASSERT(ctx->settings);
132 context->image = winpr_image_new();
135 if (!(context->rfx_context = rfx_context_new_ex(
139 if (!rfx_context_reset(context->rfx_context, SAMPLE_SERVER_DEFAULT_WIDTH,
140 SAMPLE_SERVER_DEFAULT_HEIGHT))
145 if (!rfx_context_set_mode(context->rfx_context, WINPR_ASSERTING_INT_CAST(RLGR_MODE, rlgr)))
149 if (!(context->nsc_context = nsc_context_new()))
152 if (!(context->s = Stream_New(
nullptr, 65536)))
155 context->icon_x = UINT32_MAX;
156 context->icon_y = UINT32_MAX;
157 context->vcm = WTSOpenServerA((LPSTR)client->context);
159 if (!context->vcm || context->vcm == INVALID_HANDLE_VALUE)
164 test_peer_context_free(client, ctx);
169static BOOL test_peer_init(freerdp_peer* client)
171 WINPR_ASSERT(client);
173 client->ContextSize =
sizeof(testPeerContext);
174 client->ContextNew = test_peer_context_new;
175 client->ContextFree = test_peer_context_free;
176 return freerdp_peer_context_new(client);
180static wStream* test_peer_stream_init(testPeerContext* context)
182 WINPR_ASSERT(context);
183 WINPR_ASSERT(context->s);
185 Stream_Clear(context->s);
186 Stream_ResetPosition(context->s);
190static void test_peer_begin_frame(freerdp_peer* client)
192 rdpUpdate* update =
nullptr;
194 testPeerContext* context =
nullptr;
196 WINPR_ASSERT(client);
197 WINPR_ASSERT(client->context);
199 update = client->context->update;
200 WINPR_ASSERT(update);
202 context = (testPeerContext*)client->context;
203 WINPR_ASSERT(context);
205 fm.frameAction = SURFACECMD_FRAMEACTION_BEGIN;
206 fm.frameId = context->frame_id;
207 WINPR_ASSERT(update->SurfaceFrameMarker);
208 if (!update->SurfaceFrameMarker(update->context, &fm))
209 WLog_WARN(TAG,
"SurfaceFrameMarker failed");
212static void test_peer_end_frame(freerdp_peer* client)
214 rdpUpdate* update =
nullptr;
216 testPeerContext* context =
nullptr;
218 WINPR_ASSERT(client);
220 context = (testPeerContext*)client->context;
221 WINPR_ASSERT(context);
223 update = client->context->update;
224 WINPR_ASSERT(update);
226 fm.frameAction = SURFACECMD_FRAMEACTION_END;
227 fm.frameId = context->frame_id;
228 WINPR_ASSERT(update->SurfaceFrameMarker);
229 if (!update->SurfaceFrameMarker(update->context, &fm))
230 WLog_WARN(TAG,
"SurfaceFrameMarker failed");
235static BOOL stream_surface_bits_supported(
const rdpSettings* settings)
237 const UINT32 supported =
239 return ((supported & SURFCMDS_STREAM_SURFACE_BITS) != 0);
242static BOOL test_peer_draw_background(freerdp_peer* client,
const RFX_RECT* rect)
246 const UINT32 colorFormat = PIXEL_FORMAT_RGB24;
247 const size_t bpp = FreeRDPGetBytesPerPixel(colorFormat);
249 WINPR_ASSERT(client);
251 testPeerContext* context = (testPeerContext*)client->context;
252 WINPR_ASSERT(context);
254 rdpSettings* settings = client->context->settings;
255 WINPR_ASSERT(settings);
257 rdpUpdate* update = client->context->update;
258 WINPR_ASSERT(update);
267 wStream* s = test_peer_stream_init(context);
268 const size_t size = bpp * rect->width * rect->height;
272 BYTE* rgb_data = malloc(size);
275 WLog_ERR(TAG,
"Problem allocating memory");
279 memset(rgb_data, 0xA0, size);
281 if (RemoteFxCodec && stream_surface_bits_supported(settings))
283 WLog_DBG(TAG,
"Using RemoteFX codec");
284 rfx_context_set_pixel_format(context->rfx_context, colorFormat);
286 WINPR_ASSERT(bpp <= UINT16_MAX);
287 RFX_RECT rrect = { .x = 0, .y = 0, .width = rect->width, .height = rect->height };
289 if (!rfx_compose_message(context->rfx_context, s, &rrect, 1, rgb_data, rect->width,
290 rect->height, (UINT32)(bpp * rect->width)))
295 const UINT32 RemoteFxCodecId =
297 WINPR_ASSERT(RemoteFxCodecId <= UINT16_MAX);
298 cmd.bmp.codecID = (UINT16)RemoteFxCodecId;
299 cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
303 WLog_DBG(TAG,
"Using NSCodec");
304 if (!nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, colorFormat))
307 WINPR_ASSERT(bpp <= UINT16_MAX);
308 if (!nsc_compose_message(context->nsc_context, s, rgb_data, rect->width, rect->height,
309 (UINT32)(bpp * rect->width)))
312 WINPR_ASSERT(NSCodecId <= UINT16_MAX);
313 cmd.bmp.codecID = (UINT16)NSCodecId;
314 cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
317 cmd.destLeft = rect->x;
318 cmd.destTop = rect->y;
319 cmd.destRight = rect->x + rect->width;
320 cmd.destBottom = rect->y + rect->height;
323 cmd.bmp.width = rect->width;
324 cmd.bmp.height = rect->height;
326 cmd.bmp.bitmapDataLength = WINPR_ASSERTING_INT_CAST(UINT32, Stream_GetPosition(s));
327 cmd.bmp.bitmapData = Stream_Buffer(s);
329 ret = update->SurfaceBits(update->context, &cmd);
336static int open_icon(
wImage* img)
338 char* paths[] = { SAMPLE_RESOURCE_ROOT,
"." };
339 const char* names[] = {
"test_icon.webp",
"test_icon.png",
"test_icon.jpg",
"test_icon.bmp" };
341 for (
size_t x = 0; x < ARRAYSIZE(paths); x++)
343 const char* path = paths[x];
344 if (!winpr_PathFileExists(path))
347 for (
size_t y = 0; y < ARRAYSIZE(names); y++)
349 const char* name = names[y];
350 char* file = GetCombinedPath(path, name);
351 int rc = winpr_image_read(img, file);
357 WLog_ERR(TAG,
"Unable to open test icon");
362static BOOL test_peer_load_icon(freerdp_peer* client)
364 testPeerContext* context =
nullptr;
365 rdpSettings* settings =
nullptr;
367 WINPR_ASSERT(client);
369 context = (testPeerContext*)client->context;
370 WINPR_ASSERT(context);
372 settings = client->context->settings;
373 WINPR_ASSERT(settings);
378 WLog_ERR(TAG,
"Client doesn't support RemoteFX or NSCodec");
382 int rc = open_icon(context->image);
387 if (!(context->bg_data = calloc(context->image->height, 3ULL * context->image->width)))
390 memset(context->bg_data, 0xA0, 3ULL * context->image->height * context->image->width);
393 context->bg_data =
nullptr;
397static void test_send_cursor_update(freerdp_peer* client, UINT32 x, UINT32 y)
399 WINPR_ASSERT(client);
401 testPeerContext* context = (testPeerContext*)client->context;
402 WINPR_ASSERT(context);
404 rdpSettings* settings = client->context->settings;
408 if (context->image->width < 1 || !context->activated)
413 .width = WINPR_ASSERTING_INT_CAST(UINT16, context->image->width),
414 .height = WINPR_ASSERTING_INT_CAST(UINT16, context->image->height) };
418 if (context->icon_x + context->image->width > w)
420 if (context->icon_y + context->image->height > h)
422 if (x + context->image->width > w)
424 if (y + context->image->height > h)
428 if (RemoteFxCodec && stream_surface_bits_supported(settings))
430 const UINT32 RemoteFxCodecId =
432 WINPR_ASSERT(RemoteFxCodecId <= UINT16_MAX);
433 cmd.bmp.codecID = (UINT16)RemoteFxCodecId;
434 cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
439 WINPR_ASSERT(NSCodecId <= UINT16_MAX);
440 cmd.bmp.codecID = (UINT16)NSCodecId;
441 cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
444 wStream* s = test_peer_stream_init(context);
447 const UINT32 colorFormat =
448 context->image->bitsPerPixel > 24 ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGR24;
452 rfx_context_set_pixel_format(context->rfx_context, colorFormat);
453 if (!rfx_compose_message(context->rfx_context, s, &rect, 1, context->image->data,
454 rect.width, rect.height, context->image->scanline))
455 WLog_WARN(TAG,
"rfx_compose_message failed");
459 if (!nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, colorFormat))
460 WLog_WARN(TAG,
"nsc_context_set_parameters failed");
461 else if (!nsc_compose_message(context->nsc_context, s, context->image->data, rect.width,
462 rect.height, context->image->scanline))
463 WLog_WARN(TAG,
"rfx_compose_message failed");
469 cmd.destRight = x + rect.width;
470 cmd.destBottom = y + rect.height;
472 cmd.bmp.width = rect.width;
473 cmd.bmp.height = rect.height;
474 cmd.bmp.bitmapDataLength = (UINT32)Stream_GetPosition(s);
475 cmd.bmp.bitmapData = Stream_Buffer(s);
477 rdpUpdate* update = client->context->update;
478 WINPR_ASSERT(update);
479 WINPR_ASSERT(update->SurfaceBits);
480 if (!update->SurfaceBits(update->context, &cmd))
482 WLog_WARN(TAG,
"update->SurfaceBits failed");
488static void test_peer_draw_icon(freerdp_peer* client, UINT32 x, UINT32 y)
490 WINPR_ASSERT(client);
492 rdpSettings* settings = client->context->settings;
493 WINPR_ASSERT(settings);
498 test_peer_begin_frame(client);
500 testPeerContext* context = (testPeerContext*)client->context;
501 if ((context->icon_x != UINT32_MAX) && (context->icon_y != UINT32_MAX))
503 RFX_RECT rect = { .x = WINPR_ASSERTING_INT_CAST(uint16_t, context->icon_x),
504 .y = WINPR_ASSERTING_INT_CAST(uint16_t, context->icon_y),
505 .width = WINPR_ASSERTING_INT_CAST(uint16_t, context->image->width),
506 .height = WINPR_ASSERTING_INT_CAST(uint16_t, context->image->height) };
508 test_peer_draw_background(client, &rect);
510 test_send_cursor_update(client, x, y);
511 test_peer_end_frame(client);
515static BOOL test_sleep_tsdiff(UINT32* old_sec, UINT32* old_usec, UINT32 new_sec, UINT32 new_usec)
520 WINPR_ASSERT(old_sec);
521 WINPR_ASSERT(old_usec);
523 if ((*old_sec == 0) && (*old_usec == 0))
526 *old_usec = new_usec;
530 sec = new_sec - *old_sec;
531 usec = new_usec - *old_usec;
533 if ((sec < 0) || ((sec == 0) && (usec < 0)))
535 WLog_ERR(TAG,
"Invalid time stamp detected.");
540 *old_usec = new_usec;
549 Sleep((DWORD)sec * 1000);
557static BOOL tf_peer_dump_rfx(freerdp_peer* client)
561 UINT32 prev_seconds = 0;
562 UINT32 prev_useconds = 0;
563 rdpUpdate* update =
nullptr;
564 rdpPcap* pcap_rfx =
nullptr;
565 pcap_record record = WINPR_C_ARRAY_INIT;
567 WINPR_ASSERT(client);
568 WINPR_ASSERT(client->context);
570 struct server_info* info = client->ContextExtra;
573 s = Stream_New(
nullptr, 512);
578 update = client->context->update;
579 WINPR_ASSERT(update);
581 pcap_rfx = pcap_open(info->test_pcap_file, FALSE);
585 prev_seconds = prev_useconds = 0;
587 while (pcap_has_next_record(pcap_rfx))
589 if (!pcap_get_next_record_header(pcap_rfx, &record))
592 if (!Stream_EnsureCapacity(s, record.length))
595 record.data = Stream_Buffer(s);
596 if (!pcap_get_next_record_content(pcap_rfx, &record))
598 if (!Stream_SetPosition(s, Stream_Capacity(s)))
601 if (info->test_dump_rfx_realtime &&
602 test_sleep_tsdiff(&prev_seconds, &prev_useconds, record.header.ts_sec,
603 record.header.ts_usec) == FALSE)
606 WINPR_ASSERT(update->SurfaceCommand);
607 if (!update->SurfaceCommand(update->context, s))
610 WINPR_ASSERT(client->CheckFileDescriptor);
611 if (client->CheckFileDescriptor(client) != TRUE)
617 Stream_Free(s, TRUE);
618 pcap_close(pcap_rfx);
623static DWORD WINAPI tf_debug_channel_thread_func(LPVOID arg)
626 void* buffer =
nullptr;
627 DWORD BytesReturned = 0;
629 testPeerContext* context = (testPeerContext*)arg;
631 WINPR_ASSERT(context);
632 if (WTSVirtualChannelQuery(context->debug_channel, WTSVirtualFileHandle, &buffer,
633 &BytesReturned) == TRUE)
635 fd = *((
void**)buffer);
636 WTSFreeMemory(buffer);
638 if (!(context->event = CreateWaitObjectEvent(
nullptr, TRUE, FALSE, fd)))
642 wStream* s = Stream_New(
nullptr, 4096);
646 if (!WTSVirtualChannelWrite(context->debug_channel, (PCHAR)
"test1", 5, &written))
653 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
655 handles[nCount++] = context->event;
656 handles[nCount++] = freerdp_abort_event(&context->_p);
657 handles[nCount++] = context->stopEvent;
658 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
667 Stream_ResetPosition(s);
669 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s,
char),
670 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
672 if (BytesReturned == 0)
675 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
678 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s,
char),
679 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
686 if (!Stream_SetPosition(s, BytesReturned))
688 WLog_DBG(TAG,
"got %" PRIu32
" bytes", BytesReturned);
691 Stream_Free(s, TRUE);
696static BOOL tf_peer_post_connect(freerdp_peer* client)
698 testPeerContext* context =
nullptr;
699 rdpSettings* settings =
nullptr;
701 WINPR_ASSERT(client);
703 context = (testPeerContext*)client->context;
704 WINPR_ASSERT(context);
706 settings = client->context->settings;
707 WINPR_ASSERT(settings);
715 WLog_DBG(TAG,
"Client %s is activated (osMajorType %" PRIu32
" osMinorType %" PRIu32
")",
716 client->local ?
"(local)" : client->hostname,
724 WLog_DBG(TAG,
" and wants to login automatically as %s\\%s", Domain ? Domain :
"",
729 WLog_DBG(TAG,
"Client requested desktop: %" PRIu32
"x%" PRIu32
"x%" PRIu32
"",
733#if (SAMPLE_SERVER_USE_CLIENT_RESOLUTION == 1)
735 if (!rfx_context_reset(context->rfx_context,
740 WLog_DBG(TAG,
"Using resolution requested by client.");
742 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) =
743 context->rfx_context->width;
744 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) =
745 context->rfx_context->height;
746 WLog_DBG(TAG,
"Resizing client to %" PRIu32
"x%" PRIu32
"",
747 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
748 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
749 client->update->DesktopResize(client->update->context);
754 if (!test_peer_load_icon(client))
756 WLog_DBG(TAG,
"Unable to load icon");
760 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm,
"rdpdbg"))
762 context->debug_channel = WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION,
"rdpdbg");
764 if (context->debug_channel !=
nullptr)
766 WLog_DBG(TAG,
"Open channel rdpdbg.");
768 if (!(context->stopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
770 WLog_ERR(TAG,
"Failed to create stop event");
774 if (!(context->debug_channel_thread = CreateThread(
775 nullptr, 0, tf_debug_channel_thread_func, (
void*)context, 0,
nullptr)))
777 WLog_ERR(TAG,
"Failed to create debug channel thread");
778 (void)CloseHandle(context->stopEvent);
779 context->stopEvent =
nullptr;
785 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, RDPSND_CHANNEL_NAME))
787 if (!sf_peer_rdpsnd_init(context))
791 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, ENCOMSP_SVC_CHANNEL_NAME))
793 if (!sf_peer_encomsp_init(context))
798 if (!sf_peer_audin_init(context))
801#if defined(CHANNEL_AINPUT_SERVER)
802 sf_peer_ainput_init(context);
810static BOOL tf_peer_activate(freerdp_peer* client)
813 WINPR_ASSERT(client);
815 testPeerContext* context = (testPeerContext*)client->context;
816 WINPR_ASSERT(context);
818 rdpSettings* settings = client->context->settings;
819 WINPR_ASSERT(settings);
821 struct server_info* info = client->ContextExtra;
824 context->activated = TRUE;
831 if (info->test_pcap_file !=
nullptr)
836 if (!tf_peer_dump_rfx(client))
847 test_peer_begin_frame(client);
848 rc = test_peer_draw_background(client, &rect);
849 test_peer_end_frame(client);
856static BOOL tf_peer_synchronize_event(rdpInput* input, UINT32 flags)
860 WLog_DBG(TAG,
"Client sent a synchronize event (flags:0x%" PRIX32
")", flags);
865static BOOL tf_peer_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
867 freerdp_peer* client =
nullptr;
868 rdpUpdate* update =
nullptr;
869 rdpContext* context =
nullptr;
870 testPeerContext* tcontext =
nullptr;
871 rdpSettings* settings =
nullptr;
875 context = input->context;
876 WINPR_ASSERT(context);
878 client = context->peer;
879 WINPR_ASSERT(client);
881 settings = context->settings;
882 WINPR_ASSERT(settings);
884 update = context->update;
885 WINPR_ASSERT(update);
887 tcontext = (testPeerContext*)context;
888 WINPR_ASSERT(tcontext);
890 WLog_DBG(TAG,
"Client sent a keyboard event (flags:0x%04" PRIX16
" code:0x%04" PRIX8
")", flags,
893 if (((flags & KBD_FLAGS_RELEASE) == 0) && (code == RDP_SCANCODE_KEY_G))
905 SAMPLE_SERVER_DEFAULT_WIDTH))
908 SAMPLE_SERVER_DEFAULT_HEIGHT))
912 if (!rfx_context_reset(tcontext->rfx_context,
917 WINPR_ASSERT(update->DesktopResize);
918 if (!update->DesktopResize(update->context))
920 tcontext->activated = FALSE;
922 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_C)
924 if (tcontext->debug_channel)
927 if (!WTSVirtualChannelWrite(tcontext->debug_channel, (PCHAR)
"test2", 5, &written))
931 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_X)
933 WINPR_ASSERT(client->Close);
934 if (!client->Close(client))
937 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_R)
939 tcontext->audin_open = !tcontext->audin_open;
941#if defined(CHANNEL_AINPUT_SERVER)
942 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_I)
944 tcontext->ainput_open = !tcontext->ainput_open;
947 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_S)
955static BOOL tf_peer_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
961 "Client sent a unicode keyboard event (flags:0x%04" PRIX16
" code:0x%04" PRIX16
")",
967static BOOL tf_peer_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
971 WINPR_ASSERT(input->context);
973 WLog_DBG(TAG,
"Client sent a mouse event (flags:0x%04" PRIX16
" pos:%" PRIu16
",%" PRIu16
")",
975 test_peer_draw_icon(input->context->peer, x + 10, y);
980static UINT32 add(UINT32 old, UINT32 max, INT16 diff)
988 return WINPR_ASSERTING_INT_CAST(uint32_t, val);
992static BOOL tf_peer_rel_mouse_event(rdpInput* input, UINT16 flags, INT16 xDelta, INT16 yDelta)
996 WINPR_ASSERT(input->context);
1001 static UINT32 xpos = 0;
1002 static UINT32 ypos = 0;
1004 xpos = add(xpos, w, xDelta);
1005 ypos = add(ypos, h, yDelta);
1008 "Client sent a relative mouse event (flags:0x%04" PRIX16
" pos:%" PRId16
",%" PRId16
1010 flags, xDelta, yDelta);
1011 test_peer_draw_icon(input->context->peer, xpos + 10, ypos);
1016static BOOL tf_peer_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
1018 WINPR_UNUSED(flags);
1019 WINPR_ASSERT(input);
1020 WINPR_ASSERT(input->context);
1023 "Client sent an extended mouse event (flags:0x%04" PRIX16
" pos:%" PRIu16
",%" PRIu16
1026 test_peer_draw_icon(input->context->peer, x + 10, y);
1031static BOOL tf_peer_refresh_rect(rdpContext* context, BYTE count,
const RECTANGLE_16* areas)
1033 WINPR_UNUSED(context);
1034 WINPR_ASSERT(context);
1035 WINPR_ASSERT(areas || (count == 0));
1037 WLog_DBG(TAG,
"Client requested to refresh:");
1039 for (BYTE i = 0; i < count; i++)
1041 WLog_DBG(TAG,
" (%" PRIu16
", %" PRIu16
") (%" PRIu16
", %" PRIu16
")", areas[i].left,
1042 areas[i].top, areas[i].right, areas[i].bottom);
1049static BOOL tf_peer_suppress_output(rdpContext* context, BYTE allow,
const RECTANGLE_16* area)
1051 WINPR_UNUSED(context);
1057 "Client restore output (%" PRIu16
", %" PRIu16
") (%" PRIu16
", %" PRIu16
").",
1058 area->left, area->top, area->right, area->bottom);
1062 WLog_DBG(TAG,
"Client minimized and suppress output.");
1069static int hook_peer_write_pdu(rdpTransport* transport,
wStream* s)
1076 rdpContext* context = transport_get_context(transport);
1078 WINPR_ASSERT(context);
1081 freerdp_peer* client = context->peer;
1082 WINPR_ASSERT(client);
1084 testPeerContext* peerCtx = (testPeerContext*)client->context;
1085 WINPR_ASSERT(peerCtx);
1086 WINPR_ASSERT(peerCtx->io.WritePdu);
1095 CONNECTION_STATE state = freerdp_get_state(context);
1096 if (state < CONNECTION_STATE_NEGO)
1097 return peerCtx->io.WritePdu(transport, s);
1099 ls = Stream_New(
nullptr, 4096);
1103 while (stream_dump_get(context, &flags, ls, &offset, &ts) > 0)
1107 if (flags & STREAM_MSG_SRV_TX)
1109 if ((last_ts > 0) && (ts > last_ts))
1111 UINT64 diff = ts - last_ts;
1114 UINT32 d = diff > UINT32_MAX ? UINT32_MAX : (UINT32)diff;
1120 rc = peerCtx->io.WritePdu(transport, ls);
1124 Stream_ResetPosition(ls);
1128 Stream_Free(ls, TRUE);
1133static DWORD WINAPI test_peer_mainloop(LPVOID arg)
1136 DWORD error = CHANNEL_RC_OK;
1137 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1140 testPeerContext* context =
nullptr;
1141 rdpSettings* settings =
nullptr;
1142 rdpInput* input =
nullptr;
1143 rdpUpdate* update =
nullptr;
1144 freerdp_peer* client = (freerdp_peer*)arg;
1146 WINPR_ASSERT(client);
1148 struct server_info* info = client->ContextExtra;
1151 if (!test_peer_init(client))
1153 freerdp_peer_free(client);
1158 WINPR_ASSERT(client->context);
1159 settings = client->context->settings;
1160 WINPR_ASSERT(settings);
1161 if (info->replay_dump)
1169 rdpPrivateKey* key = freerdp_key_new_from_file_enc(info->key,
nullptr);
1172 WLog_ERR(TAG,
"Failed to load private key '%s'", info->key);
1179 rdpCertificate* cert = freerdp_certificate_new_from_file(info->cert);
1182 WLog_ERR(TAG,
"Failed to load public cert '%s'", info->cert);
1198 ENCRYPTION_LEVEL_CLIENT_COMPATIBLE))
1216#if defined(WITH_RDPEUDP_SERVER)
1217 if (!sf_udp_configure(settings, info->udp))
1221 client->PostConnect = tf_peer_post_connect;
1222 client->Activate = tf_peer_activate;
1224 WINPR_ASSERT(client->context);
1225 input = client->context->input;
1226 WINPR_ASSERT(input);
1228 input->SynchronizeEvent = tf_peer_synchronize_event;
1229 input->KeyboardEvent = tf_peer_keyboard_event;
1230 input->UnicodeKeyboardEvent = tf_peer_unicode_keyboard_event;
1231 input->MouseEvent = tf_peer_mouse_event;
1232 input->RelMouseEvent = tf_peer_rel_mouse_event;
1233 input->ExtendedMouseEvent = tf_peer_extended_mouse_event;
1235 update = client->context->update;
1236 WINPR_ASSERT(update);
1238 update->RefreshRect = tf_peer_refresh_rect;
1239 update->SuppressOutput = tf_peer_suppress_output;
1244 WINPR_ASSERT(client->Initialize);
1245 rc = client->Initialize(client);
1249 context = (testPeerContext*)client->context;
1250 WINPR_ASSERT(context);
1252 if (info->replay_dump)
1254 const rdpTransportIo* cb = freerdp_get_io_callbacks(client->context);
1255 rdpTransportIo replay;
1260 replay.WritePdu = hook_peer_write_pdu;
1261 if (!freerdp_set_io_callbacks(client->context, &replay))
1265 WLog_INFO(TAG,
"We've got a client %s", client->local ?
"(local)" : client->hostname);
1267 while (error == CHANNEL_RC_OK)
1271 WINPR_ASSERT(client->GetEventHandles);
1272 DWORD tmp = client->GetEventHandles(client, &handles[count], 32 - count);
1276 WLog_ERR(TAG,
"Failed to get FreeRDP transport event handles");
1283 HANDLE channelHandle = WTSVirtualChannelManagerGetEventHandle(context->vcm);
1284 handles[count++] = channelHandle;
1285#if defined(WITH_RDPEUDP_SERVER)
1286 const DWORD timeout = sf_udp_wait_ms(info->udp);
1288 const DWORD timeout = INFINITE;
1290 status = WaitForMultipleObjects(count, handles, FALSE, timeout);
1292 if (status == WAIT_FAILED)
1294 WLog_ERR(TAG,
"WaitForMultipleObjects failed (errno: %d)", errno);
1298 WINPR_ASSERT(client->CheckFileDescriptor);
1299 if (client->CheckFileDescriptor(client) != TRUE)
1302#if defined(WITH_RDPEUDP_SERVER)
1303 if (sf_udp_check_channels(context->vcm, info->udp, context->activated) != TRUE)
1308 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, DRDYNVC_SVC_CHANNEL_NAME))
1310 switch (WTSVirtualChannelManagerGetDrdynvcState(context->vcm))
1312 case DRDYNVC_STATE_NONE:
1315 case DRDYNVC_STATE_INITIALIZED:
1318 case DRDYNVC_STATE_READY:
1321 if (sf_peer_audin_running(context) != context->audin_open)
1323 if (!sf_peer_audin_running(context))
1324 sf_peer_audin_start(context);
1326 sf_peer_audin_stop(context);
1329#if defined(CHANNEL_AINPUT_SERVER)
1330 if (sf_peer_ainput_running(context) != context->ainput_open)
1332 if (!sf_peer_ainput_running(context))
1333 sf_peer_ainput_start(context);
1335 sf_peer_ainput_stop(context);
1339#if defined(WITH_RDPEUDP_SERVER)
1340 sf_udp_tick(context, info->udp);
1344 case DRDYNVC_STATE_FAILED:
1351 WLog_INFO(TAG,
"Client %s disconnected.", client->local ?
"(local)" : client->hostname);
1353 WINPR_ASSERT(client->Disconnect);
1354 client->Disconnect(client);
1356 freerdp_peer_context_free(client);
1357 freerdp_peer_free(client);
1362static BOOL test_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
1364 HANDLE hThread =
nullptr;
1366 WINPR_UNUSED(instance);
1368 WINPR_ASSERT(instance);
1369 WINPR_ASSERT(client);
1371 struct server_info* info = instance->info;
1372 client->ContextExtra = info;
1374 if (!(hThread = CreateThread(
nullptr, 0, test_peer_mainloop, (
void*)client, 0,
nullptr)))
1377 (void)CloseHandle(hThread);
1381static void test_server_mainloop(freerdp_listener* instance)
1383 HANDLE handles[32] = WINPR_C_ARRAY_INIT;
1387 WINPR_ASSERT(instance);
1390 WINPR_ASSERT(instance->GetEventHandles);
1391 count = instance->GetEventHandles(instance, handles, 32);
1395 WLog_ERR(TAG,
"Failed to get FreeRDP event handles");
1399 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
1401 if (WAIT_FAILED == status)
1403 WLog_ERR(TAG,
"select failed");
1407 WINPR_ASSERT(instance->CheckFileDescriptor);
1408 if (instance->CheckFileDescriptor(instance) != TRUE)
1410 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1415 WINPR_ASSERT(instance->Close);
1416 instance->Close(instance);
1421 const char spcap[7];
1422 const char sfast[7];
1423 const char sport[7];
1424 const char slocal_only[13];
1425 const char scert[7];
1427} options = { {
'-',
'-',
'p',
'c',
'a',
'p',
'=' },
1428 {
'-',
'-',
'f',
'a',
's',
't' },
1429 {
'-',
'-',
'p',
'o',
'r',
't',
'=' },
1430 {
'-',
'-',
'l',
'o',
'c',
'a',
'l',
'-',
'o',
'n',
'l',
'y' },
1431 {
'-',
'-',
'c',
'e',
'r',
't',
'=' },
1432 {
'-',
'-',
'k',
'e',
'y',
'=' } };
1434WINPR_PRAGMA_DIAG_PUSH
1435WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
1436WINPR_ATTR_FORMAT_ARG(2, 0)
1437static
void print_entry(FILE* fp, WINPR_FORMAT_ARG const
char* fmt, const
char* what,
size_t size)
1439 char buffer[32] = WINPR_C_ARRAY_INIT;
1440 strncpy(buffer, what, MIN(size,
sizeof(buffer) - 1));
1441 (void)fprintf(fp, fmt, buffer);
1443WINPR_PRAGMA_DIAG_POP
1446static int usage(
const char* app,
const char* invalid)
1450 (void)fprintf(fp,
"Invalid argument '%s'\n", invalid);
1451 (void)fprintf(fp,
"Usage: %s <arg>[ <arg> ...]\n", app);
1452 (void)fprintf(fp,
"Arguments:\n");
1453 print_entry(fp,
"\t%s<pcap file>\n", options.spcap,
sizeof(options.spcap));
1454 print_entry(fp,
"\t%s<cert file>\n", options.scert,
sizeof(options.scert));
1455 print_entry(fp,
"\t%s<key file>\n", options.skey,
sizeof(options.skey));
1456 print_entry(fp,
"\t%s\n", options.sfast,
sizeof(options.sfast));
1457 print_entry(fp,
"\t%s<port>\n", options.sport,
sizeof(options.sport));
1458 print_entry(fp,
"\t%s\n", options.slocal_only,
sizeof(options.slocal_only));
1459#if defined(WITH_RDPEUDP_SERVER)
1465int main(
int argc,
char* argv[])
1468 BOOL started = FALSE;
1469 WSADATA wsaData = WINPR_C_ARRAY_INIT;
1470 freerdp_listener* instance =
nullptr;
1471 char* file =
nullptr;
1472 char name[MAX_PATH] = WINPR_C_ARRAY_INIT;
1474 BOOL localOnly = FALSE;
1475 struct server_info info = WINPR_C_ARRAY_INIT;
1476 const char* app = argv[0];
1478 info.test_dump_rfx_realtime = TRUE;
1491 if (!freerdp_setApplicationDetails(FREERDP_VENDOR_STRING,
"TFreeRDP", FREERDP_VERSION_MAJOR))
1496 for (
int i = 1; i < argc; i++)
1498 char* arg = argv[i];
1500 if (strncmp(arg, options.sfast,
sizeof(options.sfast)) == 0)
1501 info.test_dump_rfx_realtime = FALSE;
1502 else if (strncmp(arg, options.sport,
sizeof(options.sport)) == 0)
1504 const char* sport = &arg[
sizeof(options.sport)];
1505 port = strtol(sport,
nullptr, 10);
1507 if ((port < 1) || (port > UINT16_MAX) || (errno != 0))
1508 return usage(app, arg);
1510 else if (strncmp(arg, options.slocal_only,
sizeof(options.slocal_only)) == 0)
1512#if defined(WITH_RDPEUDP_SERVER)
1513 else if (sf_udp_parse_option(arg, &info.udp))
1517 else if (strncmp(arg, options.spcap,
sizeof(options.spcap)) == 0)
1519 info.test_pcap_file = &arg[
sizeof(options.spcap)];
1520 if (!winpr_PathFileExists(info.test_pcap_file))
1521 return usage(app, arg);
1523 else if (strncmp(arg, options.scert,
sizeof(options.scert)) == 0)
1525 info.cert = &arg[
sizeof(options.scert)];
1526 if (!winpr_PathFileExists(info.cert))
1527 return usage(app, arg);
1529 else if (strncmp(arg, options.skey,
sizeof(options.skey)) == 0)
1531 info.key = &arg[
sizeof(options.skey)];
1532 if (!winpr_PathFileExists(info.key))
1533 return usage(app, arg);
1536 return usage(app, arg);
1539 if (!WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi()))
1542 if (!winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT))
1545 instance = freerdp_listener_new();
1551 info.cert =
"server.crt";
1553 info.key =
"server.key";
1555 instance->info = (
void*)&info;
1556 instance->PeerAccepted = test_peer_accepted;
1558 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
1562 (void)sprintf_s(name,
sizeof(name),
"tfreerdp-server.%ld", port);
1563 file = GetKnownSubPath(KNOWN_PATH_TEMP, name);
1570 WINPR_ASSERT(instance->OpenLocal);
1571 started = instance->OpenLocal(instance, file);
1575 WINPR_ASSERT(instance->Open);
1576 started = instance->Open(instance,
nullptr, (UINT16)port);
1583 test_server_mainloop(instance);
1589 freerdp_listener_free(instance);
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_pointer_len(rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id, const void *data, size_t len)
Set a pointer to value data.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val)
Sets a string settings value. The param is copied.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.