Coverage Report

Created: 2025-07-01 06:46

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