Coverage Report

Created: 2026-01-09 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/location/client/location_main.c
Line
Count
Source
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
      {
190
0
        char cbuffer[64] = { 0 };
191
0
        char sbuffer[64] = { 0 };
192
0
        WLog_Print(plugin->baseDynPlugin.log, WLOG_DEBUG,
193
0
                   "Server version %s, client version %s",
194
0
                   location_version_str(callback->serverVersion, sbuffer, sizeof(sbuffer)),
195
0
                   location_version_str(callback->clientVersion, cbuffer, sizeof(cbuffer)));
196
0
      }
197
198
0
      if (!plugin->context.LocationStart)
199
0
      {
200
0
        WLog_Print(plugin->baseDynPlugin.log, WLOG_WARN,
201
0
                   "LocationStart=NULL, no location data will be sent");
202
0
        return CHANNEL_RC_OK;
203
0
      }
204
205
0
      {
206
0
        const UINT res =
207
0
            plugin->context.LocationStart(&plugin->context, callback->clientVersion, 0);
208
0
        if (res != CHANNEL_RC_OK)
209
0
          return res;
210
0
      }
211
0
      return location_send_client_ready_pdu(callback);
212
0
    default:
213
0
      WLog_WARN(TAG, "invalid pduType=%s");
214
0
      return ERROR_INVALID_DATA;
215
0
  }
216
0
}
217
218
static UINT location_send_base_location3d(IWTSVirtualChannel* channel,
219
                                          const RDPLOCATION_BASE_LOCATION3D_PDU* pdu)
220
0
{
221
0
  wStream sbuffer = { 0 };
222
0
  BYTE buffer[32] = { 0 };
223
0
  wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
224
0
  WINPR_ASSERT(s);
225
0
  WINPR_ASSERT(channel);
226
0
  WINPR_ASSERT(pdu);
227
228
0
  if (pdu->source)
229
0
    WLog_DBG(TAG,
230
0
             "latitude=%lf, longitude=%lf, altitude=%" PRId32
231
0
             ", speed=%lf, heading=%lf, haccuracy=%lf, source=%" PRIu8,
232
0
             pdu->latitude, pdu->longitude, pdu->altitude, pdu->speed, pdu->heading,
233
0
             pdu->horizontalAccuracy, *pdu->source);
234
0
  else
235
0
    WLog_DBG(TAG, "latitude=%lf, longitude=%lf, altitude=%" PRId32, pdu->latitude,
236
0
             pdu->longitude, pdu->altitude);
237
238
0
  if (!location_write_header(s, PDUTYPE_BASE_LOCATION3D, pdu->source ? 25 : 12))
239
0
    return ERROR_OUTOFMEMORY;
240
241
0
  if (!freerdp_write_four_byte_float(s, pdu->latitude) ||
242
0
      !freerdp_write_four_byte_float(s, pdu->longitude) ||
243
0
      !freerdp_write_four_byte_signed_integer(s, pdu->altitude))
244
0
    return ERROR_INTERNAL_ERROR;
245
246
0
  if (pdu->source)
247
0
  {
248
0
    if (!freerdp_write_four_byte_float(s, *pdu->speed) ||
249
0
        !freerdp_write_four_byte_float(s, *pdu->heading) ||
250
0
        !freerdp_write_four_byte_float(s, *pdu->horizontalAccuracy))
251
0
      return ERROR_INTERNAL_ERROR;
252
253
0
    Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, *pdu->source));
254
0
  }
255
256
0
  return location_channel_send(channel, s);
257
0
}
258
259
static UINT location_send_location2d_delta(IWTSVirtualChannel* channel,
260
                                           const RDPLOCATION_LOCATION2D_DELTA_PDU* pdu)
261
0
{
262
0
  wStream sbuffer = { 0 };
263
0
  BYTE buffer[32] = { 0 };
264
0
  wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
265
0
  WINPR_ASSERT(s);
266
267
0
  WINPR_ASSERT(channel);
268
0
  WINPR_ASSERT(pdu);
269
270
0
  const BOOL ext = pdu->speedDelta && pdu->headingDelta;
271
272
0
  if (ext)
273
0
    WLog_DBG(TAG, "latitude=%lf, longitude=%lf, speed=%lf, heading=%lf", pdu->latitudeDelta,
274
0
             pdu->longitudeDelta, pdu->speedDelta, pdu->headingDelta);
275
0
  else
276
0
    WLog_DBG(TAG, "latitude=%lf, longitude=%lf", pdu->latitudeDelta, pdu->longitudeDelta);
277
278
0
  if (!location_write_header(s, PDUTYPE_LOCATION2D_DELTA, ext ? 16 : 8))
279
0
    return ERROR_OUTOFMEMORY;
280
281
0
  if (!freerdp_write_four_byte_float(s, pdu->latitudeDelta) ||
282
0
      !freerdp_write_four_byte_float(s, pdu->longitudeDelta))
283
0
    return ERROR_INTERNAL_ERROR;
284
285
0
  if (ext)
286
0
  {
287
0
    if (!freerdp_write_four_byte_float(s, *pdu->speedDelta) ||
288
0
        !freerdp_write_four_byte_float(s, *pdu->headingDelta))
289
0
      return ERROR_INTERNAL_ERROR;
290
0
  }
291
292
0
  return location_channel_send(channel, s);
293
0
}
294
295
static UINT location_send_location3d_delta(IWTSVirtualChannel* channel,
296
                                           const RDPLOCATION_LOCATION3D_DELTA_PDU* pdu)
297
0
{
298
0
  wStream sbuffer = { 0 };
299
0
  BYTE buffer[32] = { 0 };
300
0
  wStream* s = Stream_StaticInit(&sbuffer, buffer, sizeof(buffer));
301
0
  WINPR_ASSERT(s);
302
303
0
  WINPR_ASSERT(channel);
304
0
  WINPR_ASSERT(pdu);
305
306
0
  const BOOL ext = pdu->speedDelta && pdu->headingDelta;
307
308
0
  if (ext)
309
0
    WLog_DBG(TAG, "latitude=%lf, longitude=%lf, altitude=%" PRId32 ", speed=%lf, heading=%lf",
310
0
             pdu->latitudeDelta, pdu->longitudeDelta, pdu->altitudeDelta, pdu->speedDelta,
311
0
             pdu->headingDelta);
312
0
  else
313
0
    WLog_DBG(TAG, "latitude=%lf, longitude=%lf, altitude=%" PRId32, pdu->latitudeDelta,
314
0
             pdu->longitudeDelta, pdu->altitudeDelta);
315
316
0
  if (!location_write_header(s, PDUTYPE_LOCATION3D_DELTA, ext ? 20 : 12))
317
0
    return ERROR_OUTOFMEMORY;
318
319
0
  if (!freerdp_write_four_byte_float(s, pdu->latitudeDelta) ||
320
0
      !freerdp_write_four_byte_float(s, pdu->longitudeDelta) ||
321
0
      !freerdp_write_four_byte_signed_integer(s, pdu->altitudeDelta))
322
0
    return ERROR_INTERNAL_ERROR;
323
324
0
  if (ext)
325
0
  {
326
0
    if (!freerdp_write_four_byte_float(s, *pdu->speedDelta) ||
327
0
        !freerdp_write_four_byte_float(s, *pdu->headingDelta))
328
0
      return ERROR_INTERNAL_ERROR;
329
0
  }
330
331
0
  return location_channel_send(channel, s);
332
0
}
333
334
static UINT location_send(LocationClientContext* context, LOCATION_PDUTYPE type, size_t count, ...)
335
0
{
336
0
  WINPR_ASSERT(context);
337
338
0
  LOCATION_PLUGIN* loc = context->handle;
339
0
  WINPR_ASSERT(loc);
340
341
0
  GENERIC_LISTENER_CALLBACK* cb = loc->baseDynPlugin.listener_callback;
342
0
  WINPR_ASSERT(cb);
343
344
0
  IWTSVirtualChannel* channel = cb->channel;
345
0
  WINPR_ASSERT(channel);
346
347
0
  const LOCATION_CALLBACK* callback =
348
0
      (const LOCATION_CALLBACK*)loc->baseDynPlugin.channel_callbacks;
349
0
  WINPR_ASSERT(callback);
350
351
0
  UINT32 res = ERROR_INTERNAL_ERROR;
352
0
  va_list ap = { 0 };
353
0
  va_start(ap, count);
354
0
  switch (type)
355
0
  {
356
0
    case PDUTYPE_BASE_LOCATION3D:
357
0
      if ((count != 3) && (count != 7))
358
0
        res = ERROR_INVALID_PARAMETER;
359
0
      else
360
0
      {
361
0
        RDPLOCATION_BASE_LOCATION3D_PDU pdu = { 0 };
362
0
        LOCATIONSOURCE source = LOCATIONSOURCE_IP;
363
0
        double speed = FP_NAN;
364
0
        double heading = FP_NAN;
365
0
        double horizontalAccuracy = FP_NAN;
366
0
        pdu.latitude = va_arg(ap, double);
367
0
        pdu.longitude = va_arg(ap, double);
368
0
        pdu.altitude = va_arg(ap, INT32);
369
0
        if ((count > 3) && (callback->clientVersion >= RDPLOCATION_PROTOCOL_VERSION_200))
370
0
        {
371
0
          speed = va_arg(ap, double);
372
0
          heading = va_arg(ap, double);
373
0
          horizontalAccuracy = va_arg(ap, double);
374
0
          source = WINPR_ASSERTING_INT_CAST(LOCATIONSOURCE, va_arg(ap, int));
375
0
          pdu.speed = &speed;
376
0
          pdu.heading = &heading;
377
0
          pdu.horizontalAccuracy = &horizontalAccuracy;
378
0
          pdu.source = &source;
379
0
        }
380
0
        res = location_send_base_location3d(channel, &pdu);
381
0
      }
382
0
      break;
383
0
    case PDUTYPE_LOCATION2D_DELTA:
384
0
      if ((count != 2) && (count != 4))
385
0
        res = ERROR_INVALID_PARAMETER;
386
0
      else
387
0
      {
388
0
        RDPLOCATION_LOCATION2D_DELTA_PDU pdu = { 0 };
389
390
0
        pdu.latitudeDelta = va_arg(ap, double);
391
0
        pdu.longitudeDelta = va_arg(ap, double);
392
393
0
        double speedDelta = FP_NAN;
394
0
        double headingDelta = FP_NAN;
395
0
        if ((count > 2) && (callback->clientVersion >= RDPLOCATION_PROTOCOL_VERSION_200))
396
0
        {
397
0
          speedDelta = va_arg(ap, double);
398
0
          headingDelta = va_arg(ap, double);
399
0
          pdu.speedDelta = &speedDelta;
400
0
          pdu.headingDelta = &headingDelta;
401
0
        }
402
0
        res = location_send_location2d_delta(channel, &pdu);
403
0
      }
404
0
      break;
405
0
    case PDUTYPE_LOCATION3D_DELTA:
406
0
      if ((count != 3) && (count != 5))
407
0
        res = ERROR_INVALID_PARAMETER;
408
0
      else
409
0
      {
410
0
        RDPLOCATION_LOCATION3D_DELTA_PDU pdu = { 0 };
411
0
        double speedDelta = FP_NAN;
412
0
        double headingDelta = FP_NAN;
413
414
0
        pdu.latitudeDelta = va_arg(ap, double);
415
0
        pdu.longitudeDelta = va_arg(ap, double);
416
0
        pdu.altitudeDelta = va_arg(ap, INT32);
417
0
        if ((count > 3) && (callback->clientVersion >= RDPLOCATION_PROTOCOL_VERSION_200))
418
0
        {
419
0
          speedDelta = va_arg(ap, double);
420
0
          headingDelta = va_arg(ap, double);
421
0
          pdu.speedDelta = &speedDelta;
422
0
          pdu.headingDelta = &headingDelta;
423
0
        }
424
0
        res = location_send_location3d_delta(channel, &pdu);
425
0
      }
426
0
      break;
427
0
    default:
428
0
      res = ERROR_INVALID_PARAMETER;
429
0
      break;
430
0
  }
431
0
  va_end(ap);
432
0
  return res;
433
0
}
434
435
/**
436
 * Function description
437
 *
438
 * @return 0 on success, otherwise a Win32 error code
439
 */
440
static UINT location_on_close(IWTSVirtualChannelCallback* pChannelCallback)
441
0
{
442
0
  UINT res = CHANNEL_RC_OK;
443
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
444
445
0
  if (callback)
446
0
  {
447
0
    LOCATION_PLUGIN* plugin = (LOCATION_PLUGIN*)callback->plugin;
448
0
    WINPR_ASSERT(plugin);
449
450
0
    res = IFCALLRESULT(CHANNEL_RC_OK, plugin->context.LocationStop, &plugin->context);
451
0
  }
452
0
  free(callback);
453
454
0
  return res;
455
0
}
456
457
static UINT location_init(GENERIC_DYNVC_PLUGIN* plugin, WINPR_ATTR_UNUSED rdpContext* context,
458
                          WINPR_ATTR_UNUSED rdpSettings* settings)
459
0
{
460
0
  LOCATION_PLUGIN* loc = (LOCATION_PLUGIN*)plugin;
461
462
0
  WINPR_ASSERT(loc);
463
464
0
  loc->context.LocationSend = location_send;
465
0
  loc->context.handle = loc;
466
0
  plugin->iface.pInterface = &loc->context;
467
0
  return CHANNEL_RC_OK;
468
0
}
469
470
static const IWTSVirtualChannelCallback location_callbacks = { location_on_data_received,
471
                                                             NULL, /* Open */
472
                                                             location_on_close, NULL };
473
474
/**
475
 * Function description
476
 *
477
 * @return 0 on success, otherwise a Win32 error code
478
 */
479
FREERDP_ENTRY_POINT(UINT VCAPITYPE location_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
480
0
{
481
0
  return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, LOCATION_DVC_CHANNEL_NAME,
482
0
                                        sizeof(LOCATION_PLUGIN), sizeof(LOCATION_CALLBACK),
483
                                        &location_callbacks, location_init, NULL);
484
0
}