Coverage Report

Created: 2024-05-20 06:11

/src/FreeRDP/channels/location/client/location_main.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Location Virtual Channel Extension
4
 *
5
 * Copyright 2024 Armin Novak <anovak@thincast.com>
6
 * Copyright 2024 Thincast Technologies GmbH
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
#include <freerdp/config.h>
22
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <float.h>
26
#include <math.h>
27
28
#include <winpr/crt.h>
29
#include <winpr/assert.h>
30
#include <winpr/stream.h>
31
32
#include <freerdp/client/channels.h>
33
#include <freerdp/channels/log.h>
34
#include <freerdp/channels/location.h>
35
#include <freerdp/client/location.h>
36
#include <freerdp/utils/encoded_types.h>
37
38
0
#define TAG CHANNELS_TAG("location.client")
39
40
/* implement [MS-RDPEL]
41
 * https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpel/4397a0af-c821-4b75-9068-476fb579c327
42
 */
43
typedef struct
44
{
45
  GENERIC_DYNVC_PLUGIN baseDynPlugin;
46
  LocationClientContext context;
47
} LOCATION_PLUGIN;
48
49
typedef struct
50
{
51
  GENERIC_CHANNEL_CALLBACK baseCb;
52
  UINT32 serverVersion;
53
  UINT32 clientVersion;
54
  UINT32 serverFlags;
55
  UINT32 clientFlags;
56
} LOCATION_CALLBACK;
57
58
static BOOL location_read_header(wLog* log, wStream* s, UINT16* ppduType, UINT32* ppduLength)
59
0
{
60
0
  WINPR_ASSERT(log);
61
0
  WINPR_ASSERT(s);
62
0
  WINPR_ASSERT(ppduType);
63
0
  WINPR_ASSERT(ppduLength);
64
65
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 6))
66
0
    return FALSE;
67
0
  Stream_Read_UINT16(s, *ppduType);
68
0
  Stream_Read_UINT32(s, *ppduLength);
69
0
  if (*ppduLength < 6)
70
0
  {
71
0
    WLog_Print(log, WLOG_ERROR,
72
0
               "RDPLOCATION_HEADER::pduLengh=%" PRIu16 " < sizeof(RDPLOCATION_HEADER)[6]",
73
0
               *ppduLength);
74
0
    return FALSE;
75
0
  }
76
0
  return Stream_CheckAndLogRequiredLengthWLog(log, s, *ppduLength - 6ull);
77
0
}
78
79
static BOOL location_write_header(wStream* s, UINT16 pduType, UINT32 pduLength)
80
0
{
81
0
  if (!Stream_EnsureRemainingCapacity(s, 6))
82
0
    return FALSE;
83
0
  Stream_Write_UINT16(s, pduType);
84
0
  Stream_Write_UINT32(s, pduLength + 6);
85
0
  return Stream_EnsureRemainingCapacity(s, pduLength);
86
0
}
87
88
static BOOL location_read_server_ready_pdu(LOCATION_CALLBACK* callback, wStream* s, UINT16 pduSize)
89
0
{
90
0
  if (pduSize < 6 + 4)
91
0
    return FALSE; // Short message
92
93
0
  Stream_Read_UINT32(s, callback->serverVersion);
94
0
  if (pduSize >= 6 + 4 + 4)
95
0
    Stream_Read_UINT32(s, callback->serverFlags);
96
0
  return TRUE;
97
0
}
98
99
static UINT location_channel_send(IWTSVirtualChannel* channel, wStream* s)
100
0
{
101
0
  const size_t len = Stream_GetPosition(s);
102
0
  Stream_SetPosition(s, 2);
103
0
  Stream_Write_UINT32(s, len);
104
105
0
  WINPR_ASSERT(channel);
106
0
  WINPR_ASSERT(channel->Write);
107
0
  return channel->Write(channel, len, Stream_Buffer(s), NULL);
108
0
}
109
110
static UINT location_send_client_ready_pdu(const LOCATION_CALLBACK* callback)
111
0
{
112
0
  wStream sbuffer = { 0 };
113
0
  BYTE buffer[32] = { 0 };
114
0
  wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
115
0
  WINPR_ASSERT(s);
116
117
0
  if (!location_write_header(s, PDUTYPE_CLIENT_READY, 8))
118
0
    return ERROR_OUTOFMEMORY;
119
120
0
  Stream_Write_UINT32(s, callback->clientVersion);
121
0
  Stream_Write_UINT32(s, callback->clientFlags);
122
0
  return location_channel_send(callback->baseCb.channel, s);
123
0
}
124
125
static const char* location_version_str(UINT32 version, char* buffer, size_t size)
126
0
{
127
0
  const char* str = NULL;
128
0
  switch (version)
129
0
  {
130
0
    case RDPLOCATION_PROTOCOL_VERSION_100:
131
0
      str = "RDPLOCATION_PROTOCOL_VERSION_100";
132
0
      break;
133
0
    case RDPLOCATION_PROTOCOL_VERSION_200:
134
0
      str = "RDPLOCATION_PROTOCOL_VERSION_200";
135
0
      break;
136
0
    default:
137
0
      str = "RDPLOCATION_PROTOCOL_VERSION_UNKNOWN";
138
0
      break;
139
0
  }
140
141
0
  _snprintf(buffer, size, "%s [0x%08" PRIx32 "]", str, version);
142
0
  return buffer;
143
0
}
144
145
/**
146
 * Function description
147
 *
148
 * @return 0 on success, otherwise a Win32 error code
149
 */
150
static UINT location_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
151
0
{
152
0
  LOCATION_CALLBACK* callback = (LOCATION_CALLBACK*)pChannelCallback;
153
154
0
  WINPR_ASSERT(callback);
155
156
0
  LOCATION_PLUGIN* plugin = (LOCATION_PLUGIN*)callback->baseCb.plugin;
157
0
  WINPR_ASSERT(plugin);
158
159
0
  UINT16 pduType = 0;
160
0
  UINT32 pduLength = 0;
161
0
  if (!location_read_header(plugin->baseDynPlugin.log, data, &pduType, &pduLength))
162
0
    return ERROR_INVALID_DATA;
163
164
0
  switch (pduType)
165
0
  {
166
0
    case PDUTYPE_SERVER_READY:
167
0
      if (!location_read_server_ready_pdu(callback, data, pduLength))
168
0
        return ERROR_INVALID_DATA;
169
170
0
      switch (callback->serverVersion)
171
0
      {
172
0
        case RDPLOCATION_PROTOCOL_VERSION_200:
173
0
          callback->clientVersion = RDPLOCATION_PROTOCOL_VERSION_200;
174
0
          break;
175
0
        case RDPLOCATION_PROTOCOL_VERSION_100:
176
0
          callback->clientVersion = RDPLOCATION_PROTOCOL_VERSION_100;
177
0
          break;
178
0
        default:
179
0
          callback->clientVersion = RDPLOCATION_PROTOCOL_VERSION_100;
180
0
          if (callback->serverVersion > RDPLOCATION_PROTOCOL_VERSION_200)
181
0
            callback->clientVersion = RDPLOCATION_PROTOCOL_VERSION_200;
182
0
          break;
183
0
      }
184
185
0
      char cbuffer[64] = { 0 };
186
0
      char sbuffer[64] = { 0 };
187
0
      WLog_Print(plugin->baseDynPlugin.log, WLOG_DEBUG,
188
0
                 "Server version %s, client version %s",
189
0
                 location_version_str(callback->serverVersion, sbuffer, sizeof(sbuffer)),
190
0
                 location_version_str(callback->clientVersion, cbuffer, sizeof(cbuffer)));
191
192
0
      if (!plugin->context.LocationStart)
193
0
      {
194
0
        WLog_Print(plugin->baseDynPlugin.log, WLOG_WARN,
195
0
                   "LocationStart=NULL, no location data will be sent");
196
0
        return CHANNEL_RC_OK;
197
0
      }
198
0
      const UINT res =
199
0
          plugin->context.LocationStart(&plugin->context, callback->clientVersion, 0);
200
0
      if (res != CHANNEL_RC_OK)
201
0
        return res;
202
0
      return location_send_client_ready_pdu(callback);
203
0
    default:
204
0
      WLog_WARN(TAG, "invalid pduType=%s");
205
0
      return ERROR_INVALID_DATA;
206
0
  }
207
0
}
208
209
static UINT location_send_base_location3d(IWTSVirtualChannel* channel,
210
                                          const RDPLOCATION_BASE_LOCATION3D_PDU* pdu)
211
0
{
212
0
  wStream sbuffer = { 0 };
213
0
  BYTE buffer[32] = { 0 };
214
0
  wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
215
0
  WINPR_ASSERT(s);
216
0
  WINPR_ASSERT(channel);
217
0
  WINPR_ASSERT(pdu);
218
219
0
  if (pdu->source)
220
0
    WLog_DBG(TAG,
221
0
             "latitude=%lf, logitude=%lf, altitude=%" PRId32
222
0
             ", speed=%lf, heading=%lf, haccuracy=%lf, source=%" PRIu8,
223
0
             pdu->latitude, pdu->longitude, pdu->altitude, pdu->speed, pdu->heading,
224
0
             pdu->horizontalAccuracy, *pdu->source);
225
0
  else
226
0
    WLog_DBG(TAG, "latitude=%lf, logitude=%lf, altitude=%" PRId32, pdu->latitude,
227
0
             pdu->longitude, pdu->altitude);
228
229
0
  if (!location_write_header(s, PDUTYPE_BASE_LOCATION3D, pdu->source ? 25 : 12))
230
0
    return ERROR_OUTOFMEMORY;
231
232
0
  if (!freerdp_write_four_byte_float(s, pdu->latitude) ||
233
0
      !freerdp_write_four_byte_float(s, pdu->longitude) ||
234
0
      !freerdp_write_four_byte_signed_integer(s, pdu->altitude))
235
0
    return ERROR_INTERNAL_ERROR;
236
237
0
  if (pdu->source)
238
0
  {
239
0
    if (!freerdp_write_four_byte_float(s, *pdu->speed) ||
240
0
        !freerdp_write_four_byte_float(s, *pdu->heading) ||
241
0
        !freerdp_write_four_byte_float(s, *pdu->horizontalAccuracy))
242
0
      return ERROR_INTERNAL_ERROR;
243
244
0
    Stream_Write_UINT8(s, *pdu->source);
245
0
  }
246
247
0
  return location_channel_send(channel, s);
248
0
}
249
250
static UINT location_send_location2d_delta(IWTSVirtualChannel* channel,
251
                                           const RDPLOCATION_LOCATION2D_DELTA_PDU* pdu)
252
0
{
253
0
  wStream sbuffer = { 0 };
254
0
  BYTE buffer[32] = { 0 };
255
0
  wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
256
0
  WINPR_ASSERT(s);
257
258
0
  WINPR_ASSERT(channel);
259
0
  WINPR_ASSERT(pdu);
260
261
0
  const BOOL ext = pdu->speedDelta && pdu->headingDelta;
262
263
0
  if (ext)
264
0
    WLog_DBG(TAG, "latitude=%lf, logitude=%lf, speed=%lf, heading=%lf", pdu->latitudeDelta,
265
0
             pdu->longitudeDelta, pdu->speedDelta, pdu->headingDelta);
266
0
  else
267
0
    WLog_DBG(TAG, "latitude=%lf, logitude=%lf", pdu->latitudeDelta, pdu->longitudeDelta);
268
269
0
  if (!location_write_header(s, PDUTYPE_LOCATION2D_DELTA, ext ? 16 : 8))
270
0
    return ERROR_OUTOFMEMORY;
271
272
0
  if (!freerdp_write_four_byte_float(s, pdu->latitudeDelta) ||
273
0
      !freerdp_write_four_byte_float(s, pdu->longitudeDelta))
274
0
    return ERROR_INTERNAL_ERROR;
275
276
0
  if (ext)
277
0
  {
278
0
    if (!freerdp_write_four_byte_float(s, *pdu->speedDelta) ||
279
0
        !freerdp_write_four_byte_float(s, *pdu->headingDelta))
280
0
      return ERROR_INTERNAL_ERROR;
281
0
  }
282
283
0
  return location_channel_send(channel, s);
284
0
}
285
286
static UINT location_send_location3d_delta(IWTSVirtualChannel* channel,
287
                                           const RDPLOCATION_LOCATION3D_DELTA_PDU* pdu)
288
0
{
289
0
  wStream sbuffer = { 0 };
290
0
  BYTE buffer[32] = { 0 };
291
0
  wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
292
0
  WINPR_ASSERT(s);
293
294
0
  WINPR_ASSERT(channel);
295
0
  WINPR_ASSERT(pdu);
296
297
0
  const BOOL ext = pdu->speedDelta && pdu->headingDelta;
298
299
0
  if (ext)
300
0
    WLog_DBG(TAG, "latitude=%lf, logitude=%lf, altitude=%" PRId32 ", speed=%lf, heading=%lf",
301
0
             pdu->latitudeDelta, pdu->longitudeDelta, pdu->altitudeDelta, pdu->speedDelta,
302
0
             pdu->headingDelta);
303
0
  else
304
0
    WLog_DBG(TAG, "latitude=%lf, logitude=%lf, altitude=%" PRId32, pdu->latitudeDelta,
305
0
             pdu->longitudeDelta, pdu->altitudeDelta);
306
307
0
  if (!location_write_header(s, PDUTYPE_LOCATION3D_DELTA, ext ? 20 : 12))
308
0
    return ERROR_OUTOFMEMORY;
309
310
0
  if (!freerdp_write_four_byte_float(s, pdu->latitudeDelta) ||
311
0
      !freerdp_write_four_byte_float(s, pdu->longitudeDelta) ||
312
0
      !freerdp_write_four_byte_signed_integer(s, pdu->altitudeDelta))
313
0
    return ERROR_INTERNAL_ERROR;
314
315
0
  if (ext)
316
0
  {
317
0
    if (!freerdp_write_four_byte_float(s, *pdu->speedDelta) ||
318
0
        !freerdp_write_four_byte_float(s, *pdu->headingDelta))
319
0
      return ERROR_INTERNAL_ERROR;
320
0
  }
321
322
0
  return location_channel_send(channel, s);
323
0
}
324
325
static UINT location_send(LocationClientContext* context, LOCATION_PDUTYPE type, size_t count, ...)
326
0
{
327
0
  WINPR_ASSERT(context);
328
329
0
  LOCATION_PLUGIN* loc = context->handle;
330
0
  WINPR_ASSERT(loc);
331
332
0
  GENERIC_LISTENER_CALLBACK* cb = loc->baseDynPlugin.listener_callback;
333
0
  WINPR_ASSERT(cb);
334
335
0
  IWTSVirtualChannel* channel = cb->channel;
336
0
  WINPR_ASSERT(channel);
337
338
0
  LOCATION_CALLBACK* callback = (LOCATION_CALLBACK*)loc->baseDynPlugin.channel_callbacks;
339
0
  WINPR_ASSERT(callback);
340
341
0
  UINT32 res = ERROR_INTERNAL_ERROR;
342
0
  va_list ap;
343
0
  va_start(ap, count);
344
0
  switch (type)
345
0
  {
346
0
    case PDUTYPE_BASE_LOCATION3D:
347
0
      if ((count != 3) && (count != 7))
348
0
        res = ERROR_INVALID_PARAMETER;
349
0
      else
350
0
      {
351
0
        RDPLOCATION_BASE_LOCATION3D_PDU pdu = { 0 };
352
0
        LOCATIONSOURCE source = LOCATIONSOURCE_IP;
353
0
        double speed = FP_NAN;
354
0
        double heading = FP_NAN;
355
0
        double horizontalAccuracy = FP_NAN;
356
0
        pdu.latitude = va_arg(ap, double);
357
0
        pdu.longitude = va_arg(ap, double);
358
0
        pdu.altitude = va_arg(ap, INT32);
359
0
        if ((count > 3) && (callback->clientVersion >= RDPLOCATION_PROTOCOL_VERSION_200))
360
0
        {
361
0
          speed = va_arg(ap, double);
362
0
          heading = va_arg(ap, double);
363
0
          horizontalAccuracy = va_arg(ap, double);
364
0
          source = va_arg(ap, int);
365
0
          pdu.speed = &speed;
366
0
          pdu.heading = &heading;
367
0
          pdu.horizontalAccuracy = &horizontalAccuracy;
368
0
          pdu.source = &source;
369
0
        }
370
0
        res = location_send_base_location3d(channel, &pdu);
371
0
      }
372
0
      break;
373
0
    case PDUTYPE_LOCATION2D_DELTA:
374
0
      if ((count != 2) && (count != 4))
375
0
        res = ERROR_INVALID_PARAMETER;
376
0
      else
377
0
      {
378
0
        RDPLOCATION_LOCATION2D_DELTA_PDU pdu = { 0 };
379
380
0
        pdu.latitudeDelta = va_arg(ap, double);
381
0
        pdu.longitudeDelta = va_arg(ap, double);
382
383
0
        double speedDelta = FP_NAN;
384
0
        double headingDelta = FP_NAN;
385
0
        if ((count > 2) && (callback->clientVersion >= RDPLOCATION_PROTOCOL_VERSION_200))
386
0
        {
387
0
          speedDelta = va_arg(ap, double);
388
0
          headingDelta = va_arg(ap, double);
389
0
          pdu.speedDelta = &speedDelta;
390
0
          pdu.headingDelta = &headingDelta;
391
0
        }
392
0
        res = location_send_location2d_delta(channel, &pdu);
393
0
      }
394
0
      break;
395
0
    case PDUTYPE_LOCATION3D_DELTA:
396
0
      if ((count != 3) && (count != 5))
397
0
        res = ERROR_INVALID_PARAMETER;
398
0
      else
399
0
      {
400
0
        RDPLOCATION_LOCATION3D_DELTA_PDU pdu = { 0 };
401
0
        double speedDelta = FP_NAN;
402
0
        double headingDelta = FP_NAN;
403
404
0
        pdu.latitudeDelta = va_arg(ap, double);
405
0
        pdu.longitudeDelta = va_arg(ap, double);
406
0
        pdu.altitudeDelta = va_arg(ap, INT32);
407
0
        if ((count > 3) && (callback->clientVersion >= RDPLOCATION_PROTOCOL_VERSION_200))
408
0
        {
409
0
          speedDelta = va_arg(ap, double);
410
0
          headingDelta = va_arg(ap, double);
411
0
          pdu.speedDelta = &speedDelta;
412
0
          pdu.headingDelta = &headingDelta;
413
0
        }
414
0
        res = location_send_location3d_delta(channel, &pdu);
415
0
      }
416
0
      break;
417
0
    default:
418
0
      res = ERROR_INVALID_PARAMETER;
419
0
      break;
420
0
  }
421
0
  va_end(ap);
422
0
  return res;
423
0
}
424
425
/**
426
 * Function description
427
 *
428
 * @return 0 on success, otherwise a Win32 error code
429
 */
430
static UINT location_on_close(IWTSVirtualChannelCallback* pChannelCallback)
431
0
{
432
0
  UINT res = CHANNEL_RC_OK;
433
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
434
435
0
  if (callback)
436
0
  {
437
0
    LOCATION_PLUGIN* plugin = (LOCATION_PLUGIN*)callback->plugin;
438
0
    WINPR_ASSERT(plugin);
439
440
0
    res = IFCALLRESULT(CHANNEL_RC_OK, plugin->context.LocationStop, &plugin->context);
441
0
  }
442
0
  free(callback);
443
444
0
  return res;
445
0
}
446
447
static UINT location_init(GENERIC_DYNVC_PLUGIN* plugin, rdpContext* context, rdpSettings* settings)
448
0
{
449
0
  LOCATION_PLUGIN* loc = (LOCATION_PLUGIN*)plugin;
450
451
0
  WINPR_ASSERT(loc);
452
453
0
  loc->context.LocationSend = location_send;
454
0
  loc->context.handle = loc;
455
0
  plugin->iface.pInterface = &loc->context;
456
0
  return CHANNEL_RC_OK;
457
0
}
458
459
static const IWTSVirtualChannelCallback location_callbacks = { location_on_data_received,
460
                                                             NULL, /* Open */
461
                                                             location_on_close, NULL };
462
463
/**
464
 * Function description
465
 *
466
 * @return 0 on success, otherwise a Win32 error code
467
 */
468
FREERDP_ENTRY_POINT(UINT location_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
469
0
{
470
0
  return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, LOCATION_DVC_CHANNEL_NAME,
471
0
                                        sizeof(LOCATION_PLUGIN), sizeof(LOCATION_CALLBACK),
472
0
                                        &location_callbacks, location_init, NULL);
473
0
}