FreeRDP
Loading...
Searching...
No Matches
sfreerdp.c
1
22#include <freerdp/config.h>
23
24#include <errno.h>
25#include <signal.h>
26
27#include <winpr/winpr.h>
28#include <winpr/crt.h>
29#include <winpr/cast.h>
30#include <winpr/assert.h>
31#include <winpr/ssl.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>
38
39#include <freerdp/streamdump.h>
40#include <freerdp/transport_io.h>
41
42#include <freerdp/channels/wtsvc.h>
43#include <freerdp/channels/channels.h>
44#include <freerdp/channels/drdynvc.h>
45
46#include <freerdp/freerdp.h>
47#include <freerdp/constants.h>
48#include <freerdp/server/rdpsnd.h>
49#include <freerdp/settings.h>
50
51#include "sf_ainput.h"
52#include "sf_audin.h"
53#include "sf_rdpsnd.h"
54#include "sf_encomsp.h"
55
56#include "sfreerdp.h"
57
58#include <freerdp/log.h>
59#define TAG SERVER_TAG("sample")
60
61#define SAMPLE_SERVER_USE_CLIENT_RESOLUTION 1
62#define SAMPLE_SERVER_DEFAULT_WIDTH 1024
63#define SAMPLE_SERVER_DEFAULT_HEIGHT 768
64
65struct server_info
66{
67 BOOL test_dump_rfx_realtime;
68 const char* test_pcap_file;
69 const char* replay_dump;
70 const char* cert;
71 const char* key;
72};
73
74static void test_peer_context_free(freerdp_peer* client, rdpContext* ctx)
75{
76 testPeerContext* context = (testPeerContext*)ctx;
77
78 WINPR_UNUSED(client);
79
80 if (context)
81 {
82 winpr_image_free(context->image, TRUE);
83 if (context->debug_channel_thread)
84 {
85 WINPR_ASSERT(context->stopEvent);
86 (void)SetEvent(context->stopEvent);
87 (void)WaitForSingleObject(context->debug_channel_thread, INFINITE);
88 (void)CloseHandle(context->debug_channel_thread);
89 }
90
91 Stream_Free(context->s, TRUE);
92 free(context->bg_data);
93 rfx_context_free(context->rfx_context);
94 nsc_context_free(context->nsc_context);
95
96 if (context->debug_channel)
97 (void)WTSVirtualChannelClose(context->debug_channel);
98
99 sf_peer_audin_uninit(context);
100
101#if defined(CHANNEL_AINPUT_SERVER)
102 sf_peer_ainput_uninit(context);
103#endif
104
105 rdpsnd_server_context_free(context->rdpsnd);
106 encomsp_server_context_free(context->encomsp);
107
108 WTSCloseServer(context->vcm);
109 }
110}
111
112static BOOL test_peer_context_new(freerdp_peer* client, rdpContext* ctx)
113{
114 testPeerContext* context = (testPeerContext*)ctx;
115
116 WINPR_ASSERT(client);
117 WINPR_ASSERT(context);
118 WINPR_ASSERT(ctx->settings);
119
120 context->image = winpr_image_new();
121 if (!context->image)
122 goto fail;
123 if (!(context->rfx_context = rfx_context_new_ex(
124 TRUE, freerdp_settings_get_uint32(ctx->settings, FreeRDP_ThreadingFlags))))
125 goto fail;
126
127 if (!rfx_context_reset(context->rfx_context, SAMPLE_SERVER_DEFAULT_WIDTH,
128 SAMPLE_SERVER_DEFAULT_HEIGHT))
129 goto fail;
130
131 {
132 const UINT32 rlgr = freerdp_settings_get_uint32(ctx->settings, FreeRDP_RemoteFxRlgrMode);
133 rfx_context_set_mode(context->rfx_context, rlgr);
134 }
135
136 if (!(context->nsc_context = nsc_context_new()))
137 goto fail;
138
139 if (!(context->s = Stream_New(NULL, 65536)))
140 goto fail;
141
142 context->icon_x = UINT32_MAX;
143 context->icon_y = UINT32_MAX;
144 context->vcm = WTSOpenServerA((LPSTR)client->context);
145
146 if (!context->vcm || context->vcm == INVALID_HANDLE_VALUE)
147 goto fail;
148
149 return TRUE;
150fail:
151 test_peer_context_free(client, ctx);
152 return FALSE;
153}
154
155static BOOL test_peer_init(freerdp_peer* client)
156{
157 WINPR_ASSERT(client);
158
159 client->ContextSize = sizeof(testPeerContext);
160 client->ContextNew = test_peer_context_new;
161 client->ContextFree = test_peer_context_free;
162 return freerdp_peer_context_new(client);
163}
164
165static wStream* test_peer_stream_init(testPeerContext* context)
166{
167 WINPR_ASSERT(context);
168 WINPR_ASSERT(context->s);
169
170 Stream_Clear(context->s);
171 Stream_SetPosition(context->s, 0);
172 return context->s;
173}
174
175static void test_peer_begin_frame(freerdp_peer* client)
176{
177 rdpUpdate* update = NULL;
178 SURFACE_FRAME_MARKER fm = { 0 };
179 testPeerContext* context = NULL;
180
181 WINPR_ASSERT(client);
182 WINPR_ASSERT(client->context);
183
184 update = client->context->update;
185 WINPR_ASSERT(update);
186
187 context = (testPeerContext*)client->context;
188 WINPR_ASSERT(context);
189
190 fm.frameAction = SURFACECMD_FRAMEACTION_BEGIN;
191 fm.frameId = context->frame_id;
192 WINPR_ASSERT(update->SurfaceFrameMarker);
193 update->SurfaceFrameMarker(update->context, &fm);
194}
195
196static void test_peer_end_frame(freerdp_peer* client)
197{
198 rdpUpdate* update = NULL;
199 SURFACE_FRAME_MARKER fm = { 0 };
200 testPeerContext* context = NULL;
201
202 WINPR_ASSERT(client);
203
204 context = (testPeerContext*)client->context;
205 WINPR_ASSERT(context);
206
207 update = client->context->update;
208 WINPR_ASSERT(update);
209
210 fm.frameAction = SURFACECMD_FRAMEACTION_END;
211 fm.frameId = context->frame_id;
212 WINPR_ASSERT(update->SurfaceFrameMarker);
213 update->SurfaceFrameMarker(update->context, &fm);
214 context->frame_id++;
215}
216
217static BOOL stream_surface_bits_supported(const rdpSettings* settings)
218{
219 const UINT32 supported =
220 freerdp_settings_get_uint32(settings, FreeRDP_SurfaceCommandsSupported);
221 return ((supported & SURFCMDS_STREAM_SURFACE_BITS) != 0);
222}
223
224static BOOL test_peer_draw_background(freerdp_peer* client, const RFX_RECT* rect)
225{
226 SURFACE_BITS_COMMAND cmd = { 0 };
227 BOOL ret = FALSE;
228 const UINT32 colorFormat = PIXEL_FORMAT_RGB24;
229 const size_t bpp = FreeRDPGetBytesPerPixel(colorFormat);
230
231 WINPR_ASSERT(client);
232
233 testPeerContext* context = (testPeerContext*)client->context;
234 WINPR_ASSERT(context);
235
236 rdpSettings* settings = client->context->settings;
237 WINPR_ASSERT(settings);
238
239 rdpUpdate* update = client->context->update;
240 WINPR_ASSERT(update);
241
242 const BOOL RemoteFxCodec = freerdp_settings_get_bool(settings, FreeRDP_RemoteFxCodec);
243 if (!RemoteFxCodec && !freerdp_settings_get_bool(settings, FreeRDP_NSCodec))
244 return FALSE;
245
246 WINPR_ASSERT(freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) <= UINT16_MAX);
247 WINPR_ASSERT(freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) <= UINT16_MAX);
248
249 wStream* s = test_peer_stream_init(context);
250 const size_t size = bpp * rect->width * rect->height;
251 if (size == 0)
252 return FALSE;
253
254 BYTE* rgb_data = malloc(size);
255 if (!rgb_data)
256 {
257 WLog_ERR(TAG, "Problem allocating memory");
258 return FALSE;
259 }
260
261 memset(rgb_data, 0xA0, size);
262
263 if (RemoteFxCodec && stream_surface_bits_supported(settings))
264 {
265 WLog_DBG(TAG, "Using RemoteFX codec");
266 rfx_context_set_pixel_format(context->rfx_context, colorFormat);
267
268 WINPR_ASSERT(bpp <= UINT16_MAX);
269 RFX_RECT rrect = { .x = 0, .y = 0, .width = rect->width, .height = rect->height };
270
271 if (!rfx_compose_message(context->rfx_context, s, &rrect, 1, rgb_data, rect->width,
272 rect->height, (UINT32)(bpp * rect->width)))
273 {
274 goto out;
275 }
276
277 const UINT32 RemoteFxCodecId =
278 freerdp_settings_get_uint32(settings, FreeRDP_RemoteFxCodecId);
279 WINPR_ASSERT(RemoteFxCodecId <= UINT16_MAX);
280 cmd.bmp.codecID = (UINT16)RemoteFxCodecId;
281 cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
282 }
283 else
284 {
285 WLog_DBG(TAG, "Using NSCodec");
286 nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, colorFormat);
287
288 WINPR_ASSERT(bpp <= UINT16_MAX);
289 nsc_compose_message(context->nsc_context, s, rgb_data, rect->width, rect->height,
290 (UINT32)(bpp * rect->width));
291 const UINT32 NSCodecId = freerdp_settings_get_uint32(settings, FreeRDP_NSCodecId);
292 WINPR_ASSERT(NSCodecId <= UINT16_MAX);
293 cmd.bmp.codecID = (UINT16)NSCodecId;
294 cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
295 }
296
297 cmd.destLeft = rect->x;
298 cmd.destTop = rect->y;
299 cmd.destRight = rect->x + rect->width;
300 cmd.destBottom = rect->y + rect->height;
301 cmd.bmp.bpp = 32;
302 cmd.bmp.flags = 0;
303 cmd.bmp.width = rect->width;
304 cmd.bmp.height = rect->height;
305 WINPR_ASSERT(Stream_GetPosition(s) <= UINT32_MAX);
306 cmd.bmp.bitmapDataLength = (UINT32)Stream_GetPosition(s);
307 cmd.bmp.bitmapData = Stream_Buffer(s);
308
309 update->SurfaceBits(update->context, &cmd);
310 ret = TRUE;
311out:
312 free(rgb_data);
313 return ret;
314}
315
316static int open_icon(wImage* img)
317{
318 char* paths[] = { SAMPLE_RESOURCE_ROOT, "." };
319 const char* names[] = { "test_icon.webp", "test_icon.png", "test_icon.jpg", "test_icon.bmp" };
320
321 for (size_t x = 0; x < ARRAYSIZE(paths); x++)
322 {
323 const char* path = paths[x];
324 if (!winpr_PathFileExists(path))
325 continue;
326
327 for (size_t y = 0; y < ARRAYSIZE(names); y++)
328 {
329 const char* name = names[y];
330 char* file = GetCombinedPath(path, name);
331 int rc = winpr_image_read(img, file);
332 free(file);
333 if (rc > 0)
334 return rc;
335 }
336 }
337 WLog_ERR(TAG, "Unable to open test icon");
338 return -1;
339}
340
341static BOOL test_peer_load_icon(freerdp_peer* client)
342{
343 testPeerContext* context = NULL;
344 rdpSettings* settings = NULL;
345
346 WINPR_ASSERT(client);
347
348 context = (testPeerContext*)client->context;
349 WINPR_ASSERT(context);
350
351 settings = client->context->settings;
352 WINPR_ASSERT(settings);
353
354 if (!freerdp_settings_get_bool(settings, FreeRDP_RemoteFxCodec) &&
355 !freerdp_settings_get_bool(settings, FreeRDP_NSCodec))
356 {
357 WLog_ERR(TAG, "Client doesn't support RemoteFX or NSCodec");
358 return FALSE;
359 }
360
361 int rc = open_icon(context->image);
362 if (rc <= 0)
363 goto out_fail;
364
365 /* background with same size, which will be used to erase the icon from old position */
366 if (!(context->bg_data = calloc(context->image->height, 3ULL * context->image->width)))
367 goto out_fail;
368
369 memset(context->bg_data, 0xA0, 3ULL * context->image->height * context->image->width);
370 return TRUE;
371out_fail:
372 context->bg_data = NULL;
373 return FALSE;
374}
375
376static void test_send_cursor_update(freerdp_peer* client, UINT32 x, UINT32 y)
377{
378 WINPR_ASSERT(client);
379
380 testPeerContext* context = (testPeerContext*)client->context;
381 WINPR_ASSERT(context);
382
383 rdpSettings* settings = client->context->settings;
384
385 const BOOL RemoteFxCodec = freerdp_settings_get_bool(settings, FreeRDP_RemoteFxCodec);
386
387 if (context->image->width < 1 || !context->activated)
388 return;
389
390 RFX_RECT rect = { .x = 0,
391 .y = 0,
392 .width = WINPR_ASSERTING_INT_CAST(UINT16, context->image->width),
393 .height = WINPR_ASSERTING_INT_CAST(UINT16, context->image->height) };
394
395 const UINT32 w = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
396 const UINT32 h = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
397 if (context->icon_x + context->image->width > w)
398 return;
399 if (context->icon_y + context->image->height > h)
400 return;
401 if (x + context->image->width > w)
402 return;
403 if (y + context->image->height > h)
404 return;
405
406 SURFACE_BITS_COMMAND cmd = { 0 };
407 if (RemoteFxCodec && stream_surface_bits_supported(settings))
408 {
409 const UINT32 RemoteFxCodecId =
410 freerdp_settings_get_uint32(settings, FreeRDP_RemoteFxCodecId);
411 WINPR_ASSERT(RemoteFxCodecId <= UINT16_MAX);
412 cmd.bmp.codecID = (UINT16)RemoteFxCodecId;
413 cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
414 }
415 else
416 {
417 const UINT32 NSCodecId = freerdp_settings_get_uint32(settings, FreeRDP_NSCodecId);
418 WINPR_ASSERT(NSCodecId <= UINT16_MAX);
419 cmd.bmp.codecID = (UINT16)NSCodecId;
420 cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
421 }
422
423 wStream* s = test_peer_stream_init(context);
424
425 {
426 const UINT32 colorFormat =
427 context->image->bitsPerPixel > 24 ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGR24;
428
429 if (RemoteFxCodec)
430 {
431 rfx_context_set_pixel_format(context->rfx_context, colorFormat);
432 rfx_compose_message(context->rfx_context, s, &rect, 1, context->image->data, rect.width,
433 rect.height, context->image->scanline);
434 }
435 else
436 {
437 nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, colorFormat);
438 nsc_compose_message(context->nsc_context, s, context->image->data, rect.width,
439 rect.height, context->image->scanline);
440 }
441 }
442
443 cmd.destLeft = x;
444 cmd.destTop = y;
445 cmd.destRight = x + rect.width;
446 cmd.destBottom = y + rect.height;
447 cmd.bmp.bpp = 32;
448 cmd.bmp.width = rect.width;
449 cmd.bmp.height = rect.height;
450 cmd.bmp.bitmapDataLength = (UINT32)Stream_GetPosition(s);
451 cmd.bmp.bitmapData = Stream_Buffer(s);
452
453 rdpUpdate* update = client->context->update;
454 WINPR_ASSERT(update);
455 WINPR_ASSERT(update->SurfaceBits);
456 update->SurfaceBits(update->context, &cmd);
457 context->icon_x = x;
458 context->icon_y = y;
459}
460
461static void test_peer_draw_icon(freerdp_peer* client, UINT32 x, UINT32 y)
462{
463 WINPR_ASSERT(client);
464
465 rdpSettings* settings = client->context->settings;
466 WINPR_ASSERT(settings);
467
468 if (freerdp_settings_get_bool(settings, FreeRDP_DumpRemoteFx))
469 return;
470
471 test_peer_begin_frame(client);
472
473 testPeerContext* context = (testPeerContext*)client->context;
474 if ((context->icon_x != UINT32_MAX) && (context->icon_y != UINT32_MAX))
475 {
476 RFX_RECT rect = { .x = WINPR_ASSERTING_INT_CAST(uint16_t, context->icon_x),
477 .y = WINPR_ASSERTING_INT_CAST(uint16_t, context->icon_y),
478 .width = WINPR_ASSERTING_INT_CAST(uint16_t, context->image->width),
479 .height = WINPR_ASSERTING_INT_CAST(uint16_t, context->image->height) };
480
481 test_peer_draw_background(client, &rect);
482 }
483 test_send_cursor_update(client, x, y);
484 test_peer_end_frame(client);
485}
486
487static BOOL test_sleep_tsdiff(UINT32* old_sec, UINT32* old_usec, UINT32 new_sec, UINT32 new_usec)
488{
489 INT64 sec = 0;
490 INT64 usec = 0;
491
492 WINPR_ASSERT(old_sec);
493 WINPR_ASSERT(old_usec);
494
495 if ((*old_sec == 0) && (*old_usec == 0))
496 {
497 *old_sec = new_sec;
498 *old_usec = new_usec;
499 return TRUE;
500 }
501
502 sec = new_sec - *old_sec;
503 usec = new_usec - *old_usec;
504
505 if ((sec < 0) || ((sec == 0) && (usec < 0)))
506 {
507 WLog_ERR(TAG, "Invalid time stamp detected.");
508 return FALSE;
509 }
510
511 *old_sec = new_sec;
512 *old_usec = new_usec;
513
514 while (usec < 0)
515 {
516 usec += 1000000;
517 sec--;
518 }
519
520 if (sec > 0)
521 Sleep((DWORD)sec * 1000);
522
523 if (usec > 0)
524 USleep((DWORD)usec);
525
526 return TRUE;
527}
528
529static BOOL tf_peer_dump_rfx(freerdp_peer* client)
530{
531 BOOL rc = FALSE;
532 wStream* s = NULL;
533 UINT32 prev_seconds = 0;
534 UINT32 prev_useconds = 0;
535 rdpUpdate* update = NULL;
536 rdpPcap* pcap_rfx = NULL;
537 pcap_record record = { 0 };
538
539 WINPR_ASSERT(client);
540 WINPR_ASSERT(client->context);
541
542 struct server_info* info = client->ContextExtra;
543 WINPR_ASSERT(info);
544
545 s = Stream_New(NULL, 512);
546
547 if (!s)
548 return FALSE;
549
550 update = client->context->update;
551 WINPR_ASSERT(update);
552
553 pcap_rfx = pcap_open(info->test_pcap_file, FALSE);
554 if (!pcap_rfx)
555 goto fail;
556
557 prev_seconds = prev_useconds = 0;
558
559 while (pcap_has_next_record(pcap_rfx))
560 {
561 if (!pcap_get_next_record_header(pcap_rfx, &record))
562 break;
563
564 if (!Stream_EnsureCapacity(s, record.length))
565 break;
566
567 record.data = Stream_Buffer(s);
568 pcap_get_next_record_content(pcap_rfx, &record);
569 Stream_SetPosition(s, Stream_Capacity(s));
570
571 if (info->test_dump_rfx_realtime &&
572 test_sleep_tsdiff(&prev_seconds, &prev_useconds, record.header.ts_sec,
573 record.header.ts_usec) == FALSE)
574 break;
575
576 WINPR_ASSERT(update->SurfaceCommand);
577 update->SurfaceCommand(update->context, s);
578
579 WINPR_ASSERT(client->CheckFileDescriptor);
580 if (client->CheckFileDescriptor(client) != TRUE)
581 break;
582 }
583
584 rc = TRUE;
585fail:
586 Stream_Free(s, TRUE);
587 pcap_close(pcap_rfx);
588 return rc;
589}
590
591static DWORD WINAPI tf_debug_channel_thread_func(LPVOID arg)
592{
593 void* fd = NULL;
594 void* buffer = NULL;
595 DWORD BytesReturned = 0;
596 ULONG written = 0;
597 testPeerContext* context = (testPeerContext*)arg;
598
599 WINPR_ASSERT(context);
600 if (WTSVirtualChannelQuery(context->debug_channel, WTSVirtualFileHandle, &buffer,
601 &BytesReturned) == TRUE)
602 {
603 fd = *((void**)buffer);
604 WTSFreeMemory(buffer);
605
606 if (!(context->event = CreateWaitObjectEvent(NULL, TRUE, FALSE, fd)))
607 return 0;
608 }
609
610 wStream* s = Stream_New(NULL, 4096);
611 if (!s)
612 goto fail;
613
614 if (!WTSVirtualChannelWrite(context->debug_channel, (PCHAR) "test1", 5, &written))
615 goto fail;
616
617 while (1)
618 {
619 DWORD status = 0;
620 DWORD nCount = 0;
621 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
622
623 handles[nCount++] = context->event;
624 handles[nCount++] = freerdp_abort_event(&context->_p);
625 handles[nCount++] = context->stopEvent;
626 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
627 switch (status)
628 {
629 case WAIT_OBJECT_0:
630 break;
631 default:
632 goto fail;
633 }
634
635 Stream_SetPosition(s, 0);
636
637 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s, char),
638 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
639 {
640 if (BytesReturned == 0)
641 break;
642
643 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
644 break;
645
646 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s, char),
647 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
648 {
649 /* should not happen */
650 break;
651 }
652 }
653
654 Stream_SetPosition(s, BytesReturned);
655 WLog_DBG(TAG, "got %" PRIu32 " bytes", BytesReturned);
656 }
657fail:
658 Stream_Free(s, TRUE);
659 return 0;
660}
661
662static BOOL tf_peer_post_connect(freerdp_peer* client)
663{
664 testPeerContext* context = NULL;
665 rdpSettings* settings = NULL;
666
667 WINPR_ASSERT(client);
668
669 context = (testPeerContext*)client->context;
670 WINPR_ASSERT(context);
671
672 settings = client->context->settings;
673 WINPR_ASSERT(settings);
674
681 WLog_DBG(TAG, "Client %s is activated (osMajorType %" PRIu32 " osMinorType %" PRIu32 ")",
682 client->local ? "(local)" : client->hostname,
683 freerdp_settings_get_uint32(settings, FreeRDP_OsMajorType),
684 freerdp_settings_get_uint32(settings, FreeRDP_OsMinorType));
685
686 if (freerdp_settings_get_bool(settings, FreeRDP_AutoLogonEnabled))
687 {
688 const char* Username = freerdp_settings_get_string(settings, FreeRDP_Username);
689 const char* Domain = freerdp_settings_get_string(settings, FreeRDP_Domain);
690 WLog_DBG(TAG, " and wants to login automatically as %s\\%s", Domain ? Domain : "",
691 Username);
692 /* A real server may perform OS login here if NLA is not executed previously. */
693 }
694
695 WLog_DBG(TAG, "");
696 WLog_DBG(TAG, "Client requested desktop: %" PRIu32 "x%" PRIu32 "x%" PRIu32 "",
697 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
698 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight),
699 freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth));
700#if (SAMPLE_SERVER_USE_CLIENT_RESOLUTION == 1)
701
702 if (!rfx_context_reset(context->rfx_context,
703 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
704 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)))
705 return FALSE;
706
707 WLog_DBG(TAG, "Using resolution requested by client.");
708#else
709 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) =
710 context->rfx_context->width;
711 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) =
712 context->rfx_context->height;
713 WLog_DBG(TAG, "Resizing client to %" PRIu32 "x%" PRIu32 "",
714 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
715 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
716 client->update->DesktopResize(client->update->context);
717#endif
718
719 /* A real server should tag the peer as activated here and start sending updates in main loop.
720 */
721 if (!test_peer_load_icon(client))
722 {
723 WLog_DBG(TAG, "Unable to load icon");
724 return FALSE;
725 }
726
727 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, "rdpdbg"))
728 {
729 context->debug_channel = WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, "rdpdbg");
730
731 if (context->debug_channel != NULL)
732 {
733 WLog_DBG(TAG, "Open channel rdpdbg.");
734
735 if (!(context->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
736 {
737 WLog_ERR(TAG, "Failed to create stop event");
738 return FALSE;
739 }
740
741 if (!(context->debug_channel_thread =
742 CreateThread(NULL, 0, tf_debug_channel_thread_func, (void*)context, 0, NULL)))
743 {
744 WLog_ERR(TAG, "Failed to create debug channel thread");
745 (void)CloseHandle(context->stopEvent);
746 context->stopEvent = NULL;
747 return FALSE;
748 }
749 }
750 }
751
752 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, RDPSND_CHANNEL_NAME))
753 {
754 sf_peer_rdpsnd_init(context); /* Audio Output */
755 }
756
757 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, ENCOMSP_SVC_CHANNEL_NAME))
758 {
759 sf_peer_encomsp_init(context); /* Lync Multiparty */
760 }
761
762 /* Dynamic Virtual Channels */
763 sf_peer_audin_init(context); /* Audio Input */
764
765#if defined(CHANNEL_AINPUT_SERVER)
766 sf_peer_ainput_init(context);
767#endif
768
769 /* Return FALSE here would stop the execution of the peer main loop. */
770 return TRUE;
771}
772
773static BOOL tf_peer_activate(freerdp_peer* client)
774{
775 testPeerContext* context = NULL;
776 rdpSettings* settings = NULL;
777
778 WINPR_ASSERT(client);
779
780 context = (testPeerContext*)client->context;
781 WINPR_ASSERT(context);
782
783 settings = client->context->settings;
784 WINPR_ASSERT(settings);
785
786 struct server_info* info = client->ContextExtra;
787 WINPR_ASSERT(info);
788
789 context->activated = TRUE;
790 // PACKET_COMPR_TYPE_8K;
791 // PACKET_COMPR_TYPE_64K;
792 // PACKET_COMPR_TYPE_RDP6;
793 if (!freerdp_settings_set_uint32(settings, FreeRDP_CompressionLevel, PACKET_COMPR_TYPE_RDP8))
794 return FALSE;
795
796 if (info->test_pcap_file != NULL)
797 {
798 if (!freerdp_settings_set_bool(settings, FreeRDP_DumpRemoteFx, TRUE))
799 return FALSE;
800
801 if (!tf_peer_dump_rfx(client))
802 return FALSE;
803 }
804 else
805 {
806 const RFX_RECT rect = {
807 .x = 0,
808 .y = 0,
809 .width = (UINT16)freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
810 .height = (UINT16)freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)
811 };
812 test_peer_begin_frame(client);
813 test_peer_draw_background(client, &rect);
814 test_peer_end_frame(client);
815 }
816
817 return TRUE;
818}
819
820static BOOL tf_peer_synchronize_event(rdpInput* input, UINT32 flags)
821{
822 WINPR_UNUSED(input);
823 WINPR_ASSERT(input);
824 WLog_DBG(TAG, "Client sent a synchronize event (flags:0x%" PRIX32 ")", flags);
825 return TRUE;
826}
827
828static BOOL tf_peer_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
829{
830 freerdp_peer* client = NULL;
831 rdpUpdate* update = NULL;
832 rdpContext* context = NULL;
833 testPeerContext* tcontext = NULL;
834 rdpSettings* settings = NULL;
835
836 WINPR_ASSERT(input);
837
838 context = input->context;
839 WINPR_ASSERT(context);
840
841 client = context->peer;
842 WINPR_ASSERT(client);
843
844 settings = context->settings;
845 WINPR_ASSERT(settings);
846
847 update = context->update;
848 WINPR_ASSERT(update);
849
850 tcontext = (testPeerContext*)context;
851 WINPR_ASSERT(tcontext);
852
853 WLog_DBG(TAG, "Client sent a keyboard event (flags:0x%04" PRIX16 " code:0x%04" PRIX8 ")", flags,
854 code);
855
856 if (((flags & KBD_FLAGS_RELEASE) == 0) && (code == RDP_SCANCODE_KEY_G)) /* 'g' key */
857 {
858 if (freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) != 800)
859 {
860 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, 800))
861 return FALSE;
862 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, 600))
863 return FALSE;
864 }
865 else
866 {
867 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth,
868 SAMPLE_SERVER_DEFAULT_WIDTH))
869 return FALSE;
870 if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight,
871 SAMPLE_SERVER_DEFAULT_HEIGHT))
872 return FALSE;
873 }
874
875 if (!rfx_context_reset(tcontext->rfx_context,
876 freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
877 freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)))
878 return FALSE;
879
880 WINPR_ASSERT(update->DesktopResize);
881 update->DesktopResize(update->context);
882 tcontext->activated = FALSE;
883 }
884 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_C) /* 'c' key */
885 {
886 if (tcontext->debug_channel)
887 {
888 ULONG written = 0;
889 if (!WTSVirtualChannelWrite(tcontext->debug_channel, (PCHAR) "test2", 5, &written))
890 return FALSE;
891 }
892 }
893 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_X) /* 'x' key */
894 {
895 WINPR_ASSERT(client->Close);
896 client->Close(client);
897 }
898 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_R) /* 'r' key */
899 {
900 tcontext->audin_open = !tcontext->audin_open;
901 }
902#if defined(CHANNEL_AINPUT_SERVER)
903 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_I) /* 'i' key */
904 {
905 tcontext->ainput_open = !tcontext->ainput_open;
906 }
907#endif
908 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_S) /* 's' key */
909 {
910 }
911
912 return TRUE;
913}
914
915static BOOL tf_peer_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
916{
917 WINPR_UNUSED(input);
918 WINPR_ASSERT(input);
919
920 WLog_DBG(TAG,
921 "Client sent a unicode keyboard event (flags:0x%04" PRIX16 " code:0x%04" PRIX16 ")",
922 flags, code);
923 return TRUE;
924}
925
926static BOOL tf_peer_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
927{
928 WINPR_UNUSED(flags);
929 WINPR_ASSERT(input);
930 WINPR_ASSERT(input->context);
931
932 WLog_DBG(TAG, "Client sent a mouse event (flags:0x%04" PRIX16 " pos:%" PRIu16 ",%" PRIu16 ")",
933 flags, x, y);
934 test_peer_draw_icon(input->context->peer, x + 10, y);
935 return TRUE;
936}
937
938static UINT32 add(UINT32 old, UINT32 max, INT16 diff)
939{
940 INT64 val = old;
941 val += diff;
942 if (val > max)
943 val = val % max;
944 else if (val < 0)
945 val = max - val;
946 return WINPR_ASSERTING_INT_CAST(uint32_t, val);
947}
948
949static BOOL tf_peer_rel_mouse_event(rdpInput* input, UINT16 flags, INT16 xDelta, INT16 yDelta)
950{
951 WINPR_UNUSED(flags);
952 WINPR_ASSERT(input);
953 WINPR_ASSERT(input->context);
954
955 const UINT32 w = freerdp_settings_get_uint32(input->context->settings, FreeRDP_DesktopWidth);
956 const UINT32 h = freerdp_settings_get_uint32(input->context->settings, FreeRDP_DesktopHeight);
957
958 static UINT32 xpos = 0;
959 static UINT32 ypos = 0;
960
961 xpos = add(xpos, w, xDelta);
962 ypos = add(ypos, h, yDelta);
963
964 WLog_DBG(TAG,
965 "Client sent a relative mouse event (flags:0x%04" PRIX16 " pos:%" PRId16 ",%" PRId16
966 ")",
967 flags, xDelta, yDelta);
968 test_peer_draw_icon(input->context->peer, xpos + 10, ypos);
969 return TRUE;
970}
971
972static BOOL tf_peer_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
973{
974 WINPR_UNUSED(flags);
975 WINPR_ASSERT(input);
976 WINPR_ASSERT(input->context);
977
978 WLog_DBG(TAG,
979 "Client sent an extended mouse event (flags:0x%04" PRIX16 " pos:%" PRIu16 ",%" PRIu16
980 ")",
981 flags, x, y);
982 test_peer_draw_icon(input->context->peer, x + 10, y);
983 return TRUE;
984}
985
986static BOOL tf_peer_refresh_rect(rdpContext* context, BYTE count, const RECTANGLE_16* areas)
987{
988 WINPR_UNUSED(context);
989 WINPR_ASSERT(context);
990 WINPR_ASSERT(areas || (count == 0));
991
992 WLog_DBG(TAG, "Client requested to refresh:");
993
994 for (BYTE i = 0; i < count; i++)
995 {
996 WLog_DBG(TAG, " (%" PRIu16 ", %" PRIu16 ") (%" PRIu16 ", %" PRIu16 ")", areas[i].left,
997 areas[i].top, areas[i].right, areas[i].bottom);
998 }
999
1000 return TRUE;
1001}
1002
1003static BOOL tf_peer_suppress_output(rdpContext* context, BYTE allow, const RECTANGLE_16* area)
1004{
1005 WINPR_UNUSED(context);
1006
1007 if (allow > 0)
1008 {
1009 WINPR_ASSERT(area);
1010 WLog_DBG(TAG,
1011 "Client restore output (%" PRIu16 ", %" PRIu16 ") (%" PRIu16 ", %" PRIu16 ").",
1012 area->left, area->top, area->right, area->bottom);
1013 }
1014 else
1015 {
1016 WLog_DBG(TAG, "Client minimized and suppress output.");
1017 }
1018
1019 return TRUE;
1020}
1021
1022static int hook_peer_write_pdu(rdpTransport* transport, wStream* s)
1023{
1024 UINT64 ts = 0;
1025 wStream* ls = NULL;
1026 UINT64 last_ts = 0;
1027 size_t offset = 0;
1028 UINT32 flags = 0;
1029 rdpContext* context = transport_get_context(transport);
1030
1031 WINPR_ASSERT(context);
1032 WINPR_ASSERT(s);
1033
1034 freerdp_peer* client = context->peer;
1035 WINPR_ASSERT(client);
1036
1037 testPeerContext* peerCtx = (testPeerContext*)client->context;
1038 WINPR_ASSERT(peerCtx);
1039 WINPR_ASSERT(peerCtx->io.WritePdu);
1040
1041 /* Let the client authenticate.
1042 * After that is done, we stop the normal operation and send
1043 * a previously recorded session PDU by PDU to the client.
1044 *
1045 * This is fragile and the connecting client needs to use the same
1046 * configuration as the one that recorded the session!
1047 */
1048 CONNECTION_STATE state = freerdp_get_state(context);
1049 if (state < CONNECTION_STATE_NEGO)
1050 return peerCtx->io.WritePdu(transport, s);
1051
1052 ls = Stream_New(NULL, 4096);
1053 if (!ls)
1054 goto fail;
1055
1056 while (stream_dump_get(context, &flags, ls, &offset, &ts) > 0)
1057 {
1058 int rc = 0;
1059 /* Skip messages from client. */
1060 if (flags & STREAM_MSG_SRV_TX)
1061 {
1062 if ((last_ts > 0) && (ts > last_ts))
1063 {
1064 UINT64 diff = ts - last_ts;
1065 while (diff > 0)
1066 {
1067 UINT32 d = diff > UINT32_MAX ? UINT32_MAX : (UINT32)diff;
1068 diff -= d;
1069 Sleep(d);
1070 }
1071 }
1072 last_ts = ts;
1073 rc = peerCtx->io.WritePdu(transport, ls);
1074 if (rc < 0)
1075 goto fail;
1076 }
1077 Stream_SetPosition(ls, 0);
1078 }
1079
1080fail:
1081 Stream_Free(ls, TRUE);
1082 return -1;
1083}
1084
1085static DWORD WINAPI test_peer_mainloop(LPVOID arg)
1086{
1087 BOOL rc = 0;
1088 DWORD error = CHANNEL_RC_OK;
1089 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
1090 DWORD count = 0;
1091 DWORD status = 0;
1092 testPeerContext* context = NULL;
1093 rdpSettings* settings = NULL;
1094 rdpInput* input = NULL;
1095 rdpUpdate* update = NULL;
1096 freerdp_peer* client = (freerdp_peer*)arg;
1097
1098 WINPR_ASSERT(client);
1099
1100 struct server_info* info = client->ContextExtra;
1101 WINPR_ASSERT(info);
1102
1103 if (!test_peer_init(client))
1104 {
1105 freerdp_peer_free(client);
1106 return 0;
1107 }
1108
1109 /* Initialize the real server settings here */
1110 WINPR_ASSERT(client->context);
1111 settings = client->context->settings;
1112 WINPR_ASSERT(settings);
1113 if (info->replay_dump)
1114 {
1115 if (!freerdp_settings_set_bool(settings, FreeRDP_TransportDumpReplay, TRUE) ||
1116 !freerdp_settings_set_string(settings, FreeRDP_TransportDumpFile, info->replay_dump))
1117 goto fail;
1118 }
1119
1120 {
1121 rdpPrivateKey* key = freerdp_key_new_from_file_enc(info->key, NULL);
1122 if (!key)
1123 goto fail;
1124 if (!freerdp_settings_set_pointer_len(settings, FreeRDP_RdpServerRsaKey, key, 1))
1125 goto fail;
1126 }
1127 {
1128 rdpCertificate* cert = freerdp_certificate_new_from_file(info->cert);
1129 if (!cert)
1130 goto fail;
1131 if (!freerdp_settings_set_pointer_len(settings, FreeRDP_RdpServerCertificate, cert, 1))
1132 goto fail;
1133 }
1134
1135 if (!freerdp_settings_set_bool(settings, FreeRDP_RdpSecurity, TRUE))
1136 goto fail;
1137 if (!freerdp_settings_set_bool(settings, FreeRDP_TlsSecurity, TRUE))
1138 goto fail;
1139 if (!freerdp_settings_set_bool(settings, FreeRDP_NlaSecurity, FALSE))
1140 goto fail;
1141 if (!freerdp_settings_set_uint32(settings, FreeRDP_EncryptionLevel,
1142 ENCRYPTION_LEVEL_CLIENT_COMPATIBLE))
1143 goto fail;
1144 /* ENCRYPTION_LEVEL_HIGH; */
1145 /* ENCRYPTION_LEVEL_LOW; */
1146 /* ENCRYPTION_LEVEL_FIPS; */
1147 if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteFxCodec, TRUE))
1148 goto fail;
1149 if (!freerdp_settings_set_bool(settings, FreeRDP_NSCodec, TRUE) ||
1150 !freerdp_settings_set_uint32(settings, FreeRDP_ColorDepth, 32))
1151 goto fail;
1152
1153 if (!freerdp_settings_set_bool(settings, FreeRDP_SuppressOutput, TRUE))
1154 goto fail;
1155 if (!freerdp_settings_set_bool(settings, FreeRDP_RefreshRect, TRUE))
1156 goto fail;
1157 if (!freerdp_settings_set_bool(settings, FreeRDP_HasRelativeMouseEvent, TRUE))
1158 goto fail;
1159
1160 client->PostConnect = tf_peer_post_connect;
1161 client->Activate = tf_peer_activate;
1162
1163 WINPR_ASSERT(client->context);
1164 input = client->context->input;
1165 WINPR_ASSERT(input);
1166
1167 input->SynchronizeEvent = tf_peer_synchronize_event;
1168 input->KeyboardEvent = tf_peer_keyboard_event;
1169 input->UnicodeKeyboardEvent = tf_peer_unicode_keyboard_event;
1170 input->MouseEvent = tf_peer_mouse_event;
1171 input->RelMouseEvent = tf_peer_rel_mouse_event;
1172 input->ExtendedMouseEvent = tf_peer_extended_mouse_event;
1173
1174 update = client->context->update;
1175 WINPR_ASSERT(update);
1176
1177 update->RefreshRect = tf_peer_refresh_rect;
1178 update->SuppressOutput = tf_peer_suppress_output;
1179 if (!freerdp_settings_set_uint32(settings, FreeRDP_MultifragMaxRequestSize,
1180 0xFFFFFF /* FIXME */))
1181 goto fail;
1182
1183 WINPR_ASSERT(client->Initialize);
1184 rc = client->Initialize(client);
1185 if (!rc)
1186 goto fail;
1187
1188 context = (testPeerContext*)client->context;
1189 WINPR_ASSERT(context);
1190
1191 if (info->replay_dump)
1192 {
1193 const rdpTransportIo* cb = freerdp_get_io_callbacks(client->context);
1194 rdpTransportIo replay;
1195
1196 WINPR_ASSERT(cb);
1197 replay = *cb;
1198 context->io = *cb;
1199 replay.WritePdu = hook_peer_write_pdu;
1200 freerdp_set_io_callbacks(client->context, &replay);
1201 }
1202
1203 WLog_INFO(TAG, "We've got a client %s", client->local ? "(local)" : client->hostname);
1204
1205 while (error == CHANNEL_RC_OK)
1206 {
1207 count = 0;
1208 {
1209 WINPR_ASSERT(client->GetEventHandles);
1210 DWORD tmp = client->GetEventHandles(client, &handles[count], 32 - count);
1211
1212 if (tmp == 0)
1213 {
1214 WLog_ERR(TAG, "Failed to get FreeRDP transport event handles");
1215 break;
1216 }
1217
1218 count += tmp;
1219 }
1220
1221 HANDLE channelHandle = WTSVirtualChannelManagerGetEventHandle(context->vcm);
1222 handles[count++] = channelHandle;
1223 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
1224
1225 if (status == WAIT_FAILED)
1226 {
1227 WLog_ERR(TAG, "WaitForMultipleObjects failed (errno: %d)", errno);
1228 break;
1229 }
1230
1231 WINPR_ASSERT(client->CheckFileDescriptor);
1232 if (client->CheckFileDescriptor(client) != TRUE)
1233 break;
1234
1235 if (WTSVirtualChannelManagerCheckFileDescriptor(context->vcm) != TRUE)
1236 break;
1237
1238 /* Handle dynamic virtual channel initializations */
1239 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, DRDYNVC_SVC_CHANNEL_NAME))
1240 {
1241 switch (WTSVirtualChannelManagerGetDrdynvcState(context->vcm))
1242 {
1243 case DRDYNVC_STATE_NONE:
1244 break;
1245
1246 case DRDYNVC_STATE_INITIALIZED:
1247 break;
1248
1249 case DRDYNVC_STATE_READY:
1250
1251 /* Here is the correct state to start dynamic virtual channels */
1252 if (sf_peer_audin_running(context) != context->audin_open)
1253 {
1254 if (!sf_peer_audin_running(context))
1255 sf_peer_audin_start(context);
1256 else
1257 sf_peer_audin_stop(context);
1258 }
1259
1260#if defined(CHANNEL_AINPUT_SERVER)
1261 if (sf_peer_ainput_running(context) != context->ainput_open)
1262 {
1263 if (!sf_peer_ainput_running(context))
1264 sf_peer_ainput_start(context);
1265 else
1266 sf_peer_ainput_stop(context);
1267 }
1268#endif
1269
1270 break;
1271
1272 case DRDYNVC_STATE_FAILED:
1273 default:
1274 break;
1275 }
1276 }
1277 }
1278
1279 WLog_INFO(TAG, "Client %s disconnected.", client->local ? "(local)" : client->hostname);
1280
1281 WINPR_ASSERT(client->Disconnect);
1282 client->Disconnect(client);
1283fail:
1284 freerdp_peer_context_free(client);
1285 freerdp_peer_free(client);
1286 return error;
1287}
1288
1289static BOOL test_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
1290{
1291 HANDLE hThread = NULL;
1292
1293 WINPR_UNUSED(instance);
1294
1295 WINPR_ASSERT(instance);
1296 WINPR_ASSERT(client);
1297
1298 struct server_info* info = instance->info;
1299 client->ContextExtra = info;
1300
1301 if (!(hThread = CreateThread(NULL, 0, test_peer_mainloop, (void*)client, 0, NULL)))
1302 return FALSE;
1303
1304 (void)CloseHandle(hThread);
1305 return TRUE;
1306}
1307
1308static void test_server_mainloop(freerdp_listener* instance)
1309{
1310 HANDLE handles[32] = { 0 };
1311 DWORD count = 0;
1312 DWORD status = 0;
1313
1314 WINPR_ASSERT(instance);
1315 while (1)
1316 {
1317 WINPR_ASSERT(instance->GetEventHandles);
1318 count = instance->GetEventHandles(instance, handles, 32);
1319
1320 if (0 == count)
1321 {
1322 WLog_ERR(TAG, "Failed to get FreeRDP event handles");
1323 break;
1324 }
1325
1326 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
1327
1328 if (WAIT_FAILED == status)
1329 {
1330 WLog_ERR(TAG, "select failed");
1331 break;
1332 }
1333
1334 WINPR_ASSERT(instance->CheckFileDescriptor);
1335 if (instance->CheckFileDescriptor(instance) != TRUE)
1336 {
1337 WLog_ERR(TAG, "Failed to check FreeRDP file descriptor");
1338 break;
1339 }
1340 }
1341
1342 WINPR_ASSERT(instance->Close);
1343 instance->Close(instance);
1344}
1345
1346static const struct
1347{
1348 const char spcap[7];
1349 const char sfast[7];
1350 const char sport[7];
1351 const char slocal_only[13];
1352 const char scert[7];
1353 const char skey[6];
1354} options = { { '-', '-', 'p', 'c', 'a', 'p', '=' },
1355 { '-', '-', 'f', 'a', 's', 't' },
1356 { '-', '-', 'p', 'o', 'r', 't', '=' },
1357 { '-', '-', 'l', 'o', 'c', 'a', 'l', '-', 'o', 'n', 'l', 'y' },
1358 { '-', '-', 'c', 'e', 'r', 't', '=' },
1359 { '-', '-', 'k', 'e', 'y', '=' } };
1360
1361WINPR_PRAGMA_DIAG_PUSH
1362WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
1363WINPR_ATTR_FORMAT_ARG(2, 0)
1364static void print_entry(FILE* fp, WINPR_FORMAT_ARG const char* fmt, const char* what, size_t size)
1365{
1366 char buffer[32] = { 0 };
1367 strncpy(buffer, what, MIN(size, sizeof(buffer) - 1));
1368 (void)fprintf(fp, fmt, buffer);
1369}
1370WINPR_PRAGMA_DIAG_POP
1371
1372static int usage(const char* app, const char* invalid)
1373{
1374 FILE* fp = stdout;
1375
1376 (void)fprintf(fp, "Invalid argument '%s'\n", invalid);
1377 (void)fprintf(fp, "Usage: %s <arg>[ <arg> ...]\n", app);
1378 (void)fprintf(fp, "Arguments:\n");
1379 print_entry(fp, "\t%s<pcap file>\n", options.spcap, sizeof(options.spcap));
1380 print_entry(fp, "\t%s<cert file>\n", options.scert, sizeof(options.scert));
1381 print_entry(fp, "\t%s<key file>\n", options.skey, sizeof(options.skey));
1382 print_entry(fp, "\t%s\n", options.sfast, sizeof(options.sfast));
1383 print_entry(fp, "\t%s<port>\n", options.sport, sizeof(options.sport));
1384 print_entry(fp, "\t%s\n", options.slocal_only, sizeof(options.slocal_only));
1385 return -1;
1386}
1387
1388int main(int argc, char* argv[])
1389{
1390 int rc = -1;
1391 BOOL started = FALSE;
1392 WSADATA wsaData = { 0 };
1393 freerdp_listener* instance = NULL;
1394 char* file = NULL;
1395 char name[MAX_PATH] = { 0 };
1396 long port = 3389;
1397 BOOL localOnly = FALSE;
1398 struct server_info info = { 0 };
1399 const char* app = argv[0];
1400
1401 info.test_dump_rfx_realtime = TRUE;
1402
1403 errno = 0;
1404
1405 for (int i = 1; i < argc; i++)
1406 {
1407 char* arg = argv[i];
1408
1409 if (strncmp(arg, options.sfast, sizeof(options.sfast)) == 0)
1410 info.test_dump_rfx_realtime = FALSE;
1411 else if (strncmp(arg, options.sport, sizeof(options.sport)) == 0)
1412 {
1413 const char* sport = &arg[sizeof(options.sport)];
1414 port = strtol(sport, NULL, 10);
1415
1416 if ((port < 1) || (port > UINT16_MAX) || (errno != 0))
1417 return usage(app, arg);
1418 }
1419 else if (strncmp(arg, options.slocal_only, sizeof(options.slocal_only)) == 0)
1420 localOnly = TRUE;
1421 else if (strncmp(arg, options.spcap, sizeof(options.spcap)) == 0)
1422 {
1423 info.test_pcap_file = &arg[sizeof(options.spcap)];
1424 if (!winpr_PathFileExists(info.test_pcap_file))
1425 return usage(app, arg);
1426 }
1427 else if (strncmp(arg, options.scert, sizeof(options.scert)) == 0)
1428 {
1429 info.cert = &arg[sizeof(options.scert)];
1430 if (!winpr_PathFileExists(info.cert))
1431 return usage(app, arg);
1432 }
1433 else if (strncmp(arg, options.skey, sizeof(options.skey)) == 0)
1434 {
1435 info.key = &arg[sizeof(options.skey)];
1436 if (!winpr_PathFileExists(info.key))
1437 return usage(app, arg);
1438 }
1439 else
1440 return usage(app, arg);
1441 }
1442
1443 WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi());
1444 winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
1445 instance = freerdp_listener_new();
1446
1447 if (!instance)
1448 return -1;
1449
1450 if (!info.cert)
1451 info.cert = "server.crt";
1452 if (!info.key)
1453 info.key = "server.key";
1454
1455 instance->info = (void*)&info;
1456 instance->PeerAccepted = test_peer_accepted;
1457
1458 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
1459 goto fail;
1460
1461 /* Open the server socket and start listening. */
1462 (void)sprintf_s(name, sizeof(name), "tfreerdp-server.%ld", port);
1463 file = GetKnownSubPath(KNOWN_PATH_TEMP, name);
1464
1465 if (!file)
1466 goto fail;
1467
1468 if (localOnly)
1469 {
1470 WINPR_ASSERT(instance->OpenLocal);
1471 started = instance->OpenLocal(instance, file);
1472 }
1473 else
1474 {
1475 WINPR_ASSERT(instance->Open);
1476 started = instance->Open(instance, NULL, (UINT16)port);
1477 }
1478
1479 if (started)
1480 {
1481 /* Entering the server main loop. In a real server the listener can be run in its own
1482 * thread. */
1483 test_server_mainloop(instance);
1484 }
1485
1486 rc = 0;
1487fail:
1488 free(file);
1489 freerdp_listener_free(instance);
1490 WSACleanup();
1491 return rc;
1492}
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *param)
Sets a string settings value. The param is copied.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
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.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.
Definition rfx.h:44