Coverage Report

Created: 2026-03-04 06:17

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