FreeRDP
camera_device_enumerator_main.c
1 
20 #include <freerdp/config.h>
21 
22 #include <freerdp/freerdp.h>
23 #include <freerdp/channels/log.h>
24 #include <freerdp/server/rdpecam-enumerator.h>
25 
26 #define TAG CHANNELS_TAG("rdpecam-enumerator.server")
27 
28 typedef enum
29 {
30  ENUMERATOR_INITIAL,
31  ENUMERATOR_OPENED,
32 } eEnumeratorChannelState;
33 
34 typedef struct
35 {
36  CamDevEnumServerContext context;
37 
38  HANDLE stopEvent;
39 
40  HANDLE thread;
41  void* enumerator_channel;
42 
43  DWORD SessionId;
44 
45  BOOL isOpened;
46  BOOL externalThread;
47 
48  /* Channel state */
49  eEnumeratorChannelState state;
50 
51  wStream* buffer;
52 } enumerator_server;
53 
54 static UINT enumerator_server_initialize(CamDevEnumServerContext* context, BOOL externalThread)
55 {
56  UINT error = CHANNEL_RC_OK;
57  enumerator_server* enumerator = (enumerator_server*)context;
58 
59  WINPR_ASSERT(enumerator);
60 
61  if (enumerator->isOpened)
62  {
63  WLog_WARN(TAG, "Application error: Camera Device Enumerator channel already initialized, "
64  "calling in this state is not possible!");
65  return ERROR_INVALID_STATE;
66  }
67 
68  enumerator->externalThread = externalThread;
69 
70  return error;
71 }
72 
73 static UINT enumerator_server_open_channel(enumerator_server* enumerator)
74 {
75  CamDevEnumServerContext* context = &enumerator->context;
76  DWORD Error = ERROR_SUCCESS;
77  HANDLE hEvent = NULL;
78  DWORD BytesReturned = 0;
79  PULONG pSessionId = NULL;
80  UINT32 channelId = 0;
81  BOOL status = TRUE;
82 
83  WINPR_ASSERT(enumerator);
84 
85  if (WTSQuerySessionInformationA(enumerator->context.vcm, WTS_CURRENT_SESSION, WTSSessionId,
86  (LPSTR*)&pSessionId, &BytesReturned) == FALSE)
87  {
88  WLog_ERR(TAG, "WTSQuerySessionInformationA failed!");
89  return ERROR_INTERNAL_ERROR;
90  }
91 
92  enumerator->SessionId = (DWORD)*pSessionId;
93  WTSFreeMemory(pSessionId);
94  hEvent = WTSVirtualChannelManagerGetEventHandle(enumerator->context.vcm);
95 
96  if (WaitForSingleObject(hEvent, 1000) == WAIT_FAILED)
97  {
98  Error = GetLastError();
99  WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", Error);
100  return Error;
101  }
102 
103  enumerator->enumerator_channel = WTSVirtualChannelOpenEx(
104  enumerator->SessionId, RDPECAM_CONTROL_DVC_CHANNEL_NAME, WTS_CHANNEL_OPTION_DYNAMIC);
105  if (!enumerator->enumerator_channel)
106  {
107  Error = GetLastError();
108  WLog_ERR(TAG, "WTSVirtualChannelOpenEx failed with error %" PRIu32 "!", Error);
109  return Error;
110  }
111 
112  channelId = WTSChannelGetIdByHandle(enumerator->enumerator_channel);
113 
114  IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
115  if (!status)
116  {
117  WLog_ERR(TAG, "context->ChannelIdAssigned failed!");
118  return ERROR_INTERNAL_ERROR;
119  }
120 
121  return Error;
122 }
123 
124 static UINT enumerator_server_handle_select_version_request(CamDevEnumServerContext* context,
125  wStream* s,
126  const CAM_SHARED_MSG_HEADER* header)
127 {
128  CAM_SELECT_VERSION_REQUEST pdu = { 0 };
129  UINT error = CHANNEL_RC_OK;
130 
131  WINPR_ASSERT(context);
132  WINPR_ASSERT(header);
133 
134  pdu.Header = *header;
135 
136  IFCALLRET(context->SelectVersionRequest, error, context, &pdu);
137  if (error)
138  WLog_ERR(TAG, "context->SelectVersionRequest failed with error %" PRIu32 "", error);
139 
140  return error;
141 }
142 
143 static UINT enumerator_server_recv_device_added_notification(CamDevEnumServerContext* context,
144  wStream* s,
145  const CAM_SHARED_MSG_HEADER* header)
146 {
148  UINT error = CHANNEL_RC_OK;
149  size_t remaining_length = 0;
150  WCHAR* channel_name_start = 0;
151 
152  WINPR_ASSERT(context);
153  WINPR_ASSERT(header);
154 
155  pdu.Header = *header;
156 
157  /*
158  * RequiredLength 4:
159  *
160  * Nullterminator DeviceName (2),
161  * VirtualChannelName (>= 1),
162  * Nullterminator VirtualChannelName (1)
163  */
164  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
165  return ERROR_NO_DATA;
166 
167  pdu.DeviceName = Stream_Pointer(s);
168 
169  remaining_length = Stream_GetRemainingLength(s);
170  channel_name_start = Stream_Pointer(s);
171 
172  /* Search for null terminator of DeviceName */
173  size_t i = 0;
174  for (; i < remaining_length; i += sizeof(WCHAR), ++channel_name_start)
175  {
176  if (*channel_name_start == L'\0')
177  break;
178  }
179 
180  if (*channel_name_start != L'\0')
181  {
182  WLog_ERR(TAG, "enumerator_server_recv_device_added_notification: Invalid DeviceName!");
183  return ERROR_INVALID_DATA;
184  }
185 
186  pdu.VirtualChannelName = (char*)++channel_name_start;
187  ++i;
188 
189  if (i >= remaining_length || *pdu.VirtualChannelName == '\0')
190  {
191  WLog_ERR(TAG,
192  "enumerator_server_recv_device_added_notification: Invalid VirtualChannelName!");
193  return ERROR_INVALID_DATA;
194  }
195 
196  char* tmp = pdu.VirtualChannelName;
197  for (; i < remaining_length; ++i, ++tmp)
198  {
199  if (*tmp == '\0')
200  break;
201  }
202 
203  if (*tmp != '\0')
204  {
205  WLog_ERR(TAG,
206  "enumerator_server_recv_device_added_notification: Invalid VirtualChannelName!");
207  return ERROR_INVALID_DATA;
208  }
209 
210  IFCALLRET(context->DeviceAddedNotification, error, context, &pdu);
211  if (error)
212  WLog_ERR(TAG, "context->DeviceAddedNotification failed with error %" PRIu32 "", error);
213 
214  return error;
215 }
216 
217 static UINT enumerator_server_recv_device_removed_notification(CamDevEnumServerContext* context,
218  wStream* s,
219  const CAM_SHARED_MSG_HEADER* header)
220 {
222  UINT error = CHANNEL_RC_OK;
223  size_t remaining_length = 0;
224 
225  WINPR_ASSERT(context);
226  WINPR_ASSERT(header);
227 
228  pdu.Header = *header;
229 
230  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
231  return ERROR_NO_DATA;
232 
233  pdu.VirtualChannelName = Stream_Pointer(s);
234 
235  remaining_length = Stream_GetRemainingLength(s);
236  char* tmp = pdu.VirtualChannelName + 1;
237 
238  for (size_t i = 1; i < remaining_length; ++i, ++tmp)
239  {
240  if (*tmp == '\0')
241  break;
242  }
243 
244  if (*tmp != '\0')
245  {
246  WLog_ERR(TAG,
247  "enumerator_server_recv_device_removed_notification: Invalid VirtualChannelName!");
248  return ERROR_INVALID_DATA;
249  }
250 
251  IFCALLRET(context->DeviceRemovedNotification, error, context, &pdu);
252  if (error)
253  WLog_ERR(TAG, "context->DeviceRemovedNotification failed with error %" PRIu32 "", error);
254 
255  return error;
256 }
257 
258 static UINT enumerator_process_message(enumerator_server* enumerator)
259 {
260  BOOL rc = 0;
261  UINT error = ERROR_INTERNAL_ERROR;
262  ULONG BytesReturned = 0;
263  CAM_SHARED_MSG_HEADER header = { 0 };
264  wStream* s = NULL;
265 
266  WINPR_ASSERT(enumerator);
267  WINPR_ASSERT(enumerator->enumerator_channel);
268 
269  s = enumerator->buffer;
270  WINPR_ASSERT(s);
271 
272  Stream_SetPosition(s, 0);
273  rc = WTSVirtualChannelRead(enumerator->enumerator_channel, 0, NULL, 0, &BytesReturned);
274  if (!rc)
275  goto out;
276 
277  if (BytesReturned < 1)
278  {
279  error = CHANNEL_RC_OK;
280  goto out;
281  }
282 
283  if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
284  {
285  WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
286  error = CHANNEL_RC_NO_MEMORY;
287  goto out;
288  }
289 
290  if (WTSVirtualChannelRead(enumerator->enumerator_channel, 0, Stream_BufferAs(s, char),
291  (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
292  {
293  WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
294  goto out;
295  }
296 
297  Stream_SetLength(s, BytesReturned);
298  if (!Stream_CheckAndLogRequiredLength(TAG, s, CAM_HEADER_SIZE))
299  return ERROR_NO_DATA;
300 
301  Stream_Read_UINT8(s, header.Version);
302  Stream_Read_UINT8(s, header.MessageId);
303 
304  switch (header.MessageId)
305  {
306  case CAM_MSG_ID_SelectVersionRequest:
307  error =
308  enumerator_server_handle_select_version_request(&enumerator->context, s, &header);
309  break;
310  case CAM_MSG_ID_DeviceAddedNotification:
311  error =
312  enumerator_server_recv_device_added_notification(&enumerator->context, s, &header);
313  break;
314  case CAM_MSG_ID_DeviceRemovedNotification:
315  error = enumerator_server_recv_device_removed_notification(&enumerator->context, s,
316  &header);
317  break;
318  default:
319  WLog_ERR(TAG, "enumerator_process_message: unknown or invalid MessageId %" PRIu8 "",
320  header.MessageId);
321  break;
322  }
323 
324 out:
325  if (error)
326  WLog_ERR(TAG, "Response failed with error %" PRIu32 "!", error);
327 
328  return error;
329 }
330 
331 static UINT enumerator_server_context_poll_int(CamDevEnumServerContext* context)
332 {
333  enumerator_server* enumerator = (enumerator_server*)context;
334  UINT error = ERROR_INTERNAL_ERROR;
335 
336  WINPR_ASSERT(enumerator);
337 
338  switch (enumerator->state)
339  {
340  case ENUMERATOR_INITIAL:
341  error = enumerator_server_open_channel(enumerator);
342  if (error)
343  WLog_ERR(TAG, "enumerator_server_open_channel failed with error %" PRIu32 "!",
344  error);
345  else
346  enumerator->state = ENUMERATOR_OPENED;
347  break;
348  case ENUMERATOR_OPENED:
349  error = enumerator_process_message(enumerator);
350  break;
351  default:
352  break;
353  }
354 
355  return error;
356 }
357 
358 static HANDLE enumerator_server_get_channel_handle(enumerator_server* enumerator)
359 {
360  void* buffer = NULL;
361  DWORD BytesReturned = 0;
362  HANDLE ChannelEvent = NULL;
363 
364  WINPR_ASSERT(enumerator);
365 
366  if (WTSVirtualChannelQuery(enumerator->enumerator_channel, WTSVirtualEventHandle, &buffer,
367  &BytesReturned) == TRUE)
368  {
369  if (BytesReturned == sizeof(HANDLE))
370  CopyMemory(&ChannelEvent, buffer, sizeof(HANDLE));
371 
372  WTSFreeMemory(buffer);
373  }
374 
375  return ChannelEvent;
376 }
377 
378 static DWORD WINAPI enumerator_server_thread_func(LPVOID arg)
379 {
380  DWORD nCount = 0;
381  HANDLE events[2] = { 0 };
382  enumerator_server* enumerator = (enumerator_server*)arg;
383  UINT error = CHANNEL_RC_OK;
384  DWORD status = 0;
385 
386  WINPR_ASSERT(enumerator);
387 
388  nCount = 0;
389  events[nCount++] = enumerator->stopEvent;
390 
391  while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
392  {
393  switch (enumerator->state)
394  {
395  case ENUMERATOR_INITIAL:
396  error = enumerator_server_context_poll_int(&enumerator->context);
397  if (error == CHANNEL_RC_OK)
398  {
399  events[1] = enumerator_server_get_channel_handle(enumerator);
400  nCount = 2;
401  }
402  break;
403  case ENUMERATOR_OPENED:
404  status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
405  switch (status)
406  {
407  case WAIT_OBJECT_0:
408  break;
409  case WAIT_OBJECT_0 + 1:
410  case WAIT_TIMEOUT:
411  error = enumerator_server_context_poll_int(&enumerator->context);
412  break;
413 
414  case WAIT_FAILED:
415  default:
416  error = ERROR_INTERNAL_ERROR;
417  break;
418  }
419  break;
420  default:
421  break;
422  }
423  }
424 
425  (void)WTSVirtualChannelClose(enumerator->enumerator_channel);
426  enumerator->enumerator_channel = NULL;
427 
428  if (error && enumerator->context.rdpcontext)
429  setChannelError(enumerator->context.rdpcontext, error,
430  "enumerator_server_thread_func reported an error");
431 
432  ExitThread(error);
433  return error;
434 }
435 
436 static UINT enumerator_server_open(CamDevEnumServerContext* context)
437 {
438  enumerator_server* enumerator = (enumerator_server*)context;
439 
440  WINPR_ASSERT(enumerator);
441 
442  if (!enumerator->externalThread && (enumerator->thread == NULL))
443  {
444  enumerator->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
445  if (!enumerator->stopEvent)
446  {
447  WLog_ERR(TAG, "CreateEvent failed!");
448  return ERROR_INTERNAL_ERROR;
449  }
450 
451  enumerator->thread =
452  CreateThread(NULL, 0, enumerator_server_thread_func, enumerator, 0, NULL);
453  if (!enumerator->thread)
454  {
455  WLog_ERR(TAG, "CreateThread failed!");
456  (void)CloseHandle(enumerator->stopEvent);
457  enumerator->stopEvent = NULL;
458  return ERROR_INTERNAL_ERROR;
459  }
460  }
461  enumerator->isOpened = TRUE;
462 
463  return CHANNEL_RC_OK;
464 }
465 
466 static UINT enumerator_server_close(CamDevEnumServerContext* context)
467 {
468  UINT error = CHANNEL_RC_OK;
469  enumerator_server* enumerator = (enumerator_server*)context;
470 
471  WINPR_ASSERT(enumerator);
472 
473  if (!enumerator->externalThread && enumerator->thread)
474  {
475  (void)SetEvent(enumerator->stopEvent);
476 
477  if (WaitForSingleObject(enumerator->thread, INFINITE) == WAIT_FAILED)
478  {
479  error = GetLastError();
480  WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
481  return error;
482  }
483 
484  (void)CloseHandle(enumerator->thread);
485  (void)CloseHandle(enumerator->stopEvent);
486  enumerator->thread = NULL;
487  enumerator->stopEvent = NULL;
488  }
489  if (enumerator->externalThread)
490  {
491  if (enumerator->state != ENUMERATOR_INITIAL)
492  {
493  (void)WTSVirtualChannelClose(enumerator->enumerator_channel);
494  enumerator->enumerator_channel = NULL;
495  enumerator->state = ENUMERATOR_INITIAL;
496  }
497  }
498  enumerator->isOpened = FALSE;
499 
500  return error;
501 }
502 
503 static UINT enumerator_server_context_poll(CamDevEnumServerContext* context)
504 {
505  enumerator_server* enumerator = (enumerator_server*)context;
506 
507  WINPR_ASSERT(enumerator);
508 
509  if (!enumerator->externalThread)
510  return ERROR_INTERNAL_ERROR;
511 
512  return enumerator_server_context_poll_int(context);
513 }
514 
515 static BOOL enumerator_server_context_handle(CamDevEnumServerContext* context, HANDLE* handle)
516 {
517  enumerator_server* enumerator = (enumerator_server*)context;
518 
519  WINPR_ASSERT(enumerator);
520  WINPR_ASSERT(handle);
521 
522  if (!enumerator->externalThread)
523  return FALSE;
524  if (enumerator->state == ENUMERATOR_INITIAL)
525  return FALSE;
526 
527  *handle = enumerator_server_get_channel_handle(enumerator);
528 
529  return TRUE;
530 }
531 
532 static UINT enumerator_server_packet_send(CamDevEnumServerContext* context, wStream* s)
533 {
534  enumerator_server* enumerator = (enumerator_server*)context;
535  UINT error = CHANNEL_RC_OK;
536  ULONG written = 0;
537 
538  const size_t len = Stream_GetPosition(s);
539  WINPR_ASSERT(len <= UINT32_MAX);
540  if (!WTSVirtualChannelWrite(enumerator->enumerator_channel, Stream_BufferAs(s, char),
541  (UINT32)len, &written))
542  {
543  WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
544  error = ERROR_INTERNAL_ERROR;
545  goto out;
546  }
547 
548  if (written < Stream_GetPosition(s))
549  {
550  WLog_WARN(TAG, "Unexpected bytes written: %" PRIu32 "/%" PRIuz "", written,
551  Stream_GetPosition(s));
552  }
553 
554 out:
555  Stream_Free(s, TRUE);
556  return error;
557 }
558 
559 static UINT enumerator_send_select_version_response_pdu(
560  CamDevEnumServerContext* context, const CAM_SELECT_VERSION_RESPONSE* selectVersionResponse)
561 {
562  wStream* s = NULL;
563 
564  s = Stream_New(NULL, CAM_HEADER_SIZE);
565  if (!s)
566  {
567  WLog_ERR(TAG, "Stream_New failed!");
568  return ERROR_NOT_ENOUGH_MEMORY;
569  }
570 
571  Stream_Write_UINT8(s, selectVersionResponse->Header.Version);
572  Stream_Write_UINT8(s, selectVersionResponse->Header.MessageId);
573 
574  return enumerator_server_packet_send(context, s);
575 }
576 
577 CamDevEnumServerContext* cam_dev_enum_server_context_new(HANDLE vcm)
578 {
579  enumerator_server* enumerator = (enumerator_server*)calloc(1, sizeof(enumerator_server));
580 
581  if (!enumerator)
582  return NULL;
583 
584  enumerator->context.vcm = vcm;
585  enumerator->context.Initialize = enumerator_server_initialize;
586  enumerator->context.Open = enumerator_server_open;
587  enumerator->context.Close = enumerator_server_close;
588  enumerator->context.Poll = enumerator_server_context_poll;
589  enumerator->context.ChannelHandle = enumerator_server_context_handle;
590 
591  enumerator->context.SelectVersionResponse = enumerator_send_select_version_response_pdu;
592 
593  enumerator->buffer = Stream_New(NULL, 4096);
594  if (!enumerator->buffer)
595  goto fail;
596 
597  return &enumerator->context;
598 fail:
599  WINPR_PRAGMA_DIAG_PUSH
600  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
601  cam_dev_enum_server_context_free(&enumerator->context);
602  WINPR_PRAGMA_DIAG_POP
603  return NULL;
604 }
605 
606 void cam_dev_enum_server_context_free(CamDevEnumServerContext* context)
607 {
608  enumerator_server* enumerator = (enumerator_server*)context;
609 
610  if (enumerator)
611  {
612  enumerator_server_close(context);
613  Stream_Free(enumerator->buffer, TRUE);
614  }
615 
616  free(enumerator);
617 }