/src/FreeRDP/libfreerdp/core/streamdump.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * |
4 | | * RDP session stream dump interface |
5 | | * |
6 | | * Copyright 2022 Armin Novak |
7 | | * Copyright 2022 Thincast Technologies GmbH |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <time.h> |
23 | | |
24 | | #include <winpr/sysinfo.h> |
25 | | #include <winpr/path.h> |
26 | | #include <winpr/string.h> |
27 | | |
28 | | #include <freerdp/freerdp.h> |
29 | | #include <freerdp/streamdump.h> |
30 | | #include <freerdp/transport_io.h> |
31 | | |
32 | | #include "streamdump.h" |
33 | | |
34 | 15.4k | #define TAG FREERDP_TAG("streamdump") |
35 | | |
36 | | struct stream_dump_context |
37 | | { |
38 | | rdpTransportIo io; |
39 | | size_t writeDumpOffset; |
40 | | size_t readDumpOffset; |
41 | | size_t replayOffset; |
42 | | UINT64 replayTime; |
43 | | CONNECTION_STATE state; |
44 | | BOOL isServer; |
45 | | BOOL nodelay; |
46 | | wLog* log; |
47 | | }; |
48 | | |
49 | | static UINT32 crc32b(const BYTE* data, size_t length) |
50 | 0 | { |
51 | 0 | UINT32 crc = 0xFFFFFFFF; |
52 | |
|
53 | 0 | for (size_t x = 0; x < length; x++) |
54 | 0 | { |
55 | 0 | const UINT32 d = data[x] & 0xFF; |
56 | 0 | crc = crc ^ d; |
57 | 0 | for (int j = 7; j >= 0; j--) |
58 | 0 | { |
59 | 0 | UINT32 mask = ~(crc & 1); |
60 | 0 | crc = (crc >> 1) ^ (0xEDB88320 & mask); |
61 | 0 | } |
62 | 0 | } |
63 | 0 | return ~crc; |
64 | 0 | } |
65 | | |
66 | | #if !defined(BUILD_TESTING_INTERNAL) |
67 | | static |
68 | | #endif |
69 | | BOOL stream_dump_read_line(FILE* fp, wStream* s, UINT64* pts, size_t* pOffset, UINT32* flags) |
70 | 0 | { |
71 | 0 | BOOL rc = FALSE; |
72 | 0 | UINT64 ts = 0; |
73 | 0 | UINT64 size = 0; |
74 | 0 | size_t r = 0; |
75 | 0 | UINT32 crc32 = 0; |
76 | 0 | BYTE received = 0; |
77 | |
|
78 | 0 | if (!fp || !s || !flags) |
79 | 0 | return FALSE; |
80 | | |
81 | 0 | if (pOffset) |
82 | 0 | { |
83 | 0 | if (_fseeki64(fp, WINPR_ASSERTING_INT_CAST(int64_t, *pOffset), SEEK_SET) < 0) |
84 | 0 | goto fail; |
85 | 0 | } |
86 | | |
87 | 0 | r = fread(&ts, 1, sizeof(ts), fp); |
88 | 0 | if (r != sizeof(ts)) |
89 | 0 | goto fail; |
90 | 0 | r = fread(&received, 1, sizeof(received), fp); |
91 | 0 | if (r != sizeof(received)) |
92 | 0 | goto fail; |
93 | 0 | r = fread(&crc32, 1, sizeof(crc32), fp); |
94 | 0 | if (r != sizeof(crc32)) |
95 | 0 | goto fail; |
96 | 0 | r = fread(&size, 1, sizeof(size), fp); |
97 | 0 | if (r != sizeof(size)) |
98 | 0 | goto fail; |
99 | 0 | if (received) |
100 | 0 | *flags = STREAM_MSG_SRV_RX; |
101 | 0 | else |
102 | 0 | *flags = STREAM_MSG_SRV_TX; |
103 | |
|
104 | 0 | { |
105 | 0 | const size_t usize = WINPR_ASSERTING_INT_CAST(size_t, size); |
106 | 0 | if (!Stream_EnsureRemainingCapacity(s, usize)) |
107 | 0 | goto fail; |
108 | 0 | r = fread(Stream_Pointer(s), 1, usize, fp); |
109 | 0 | if (r != size) |
110 | 0 | goto fail; |
111 | 0 | if (crc32 != crc32b(Stream_ConstPointer(s), usize)) |
112 | 0 | goto fail; |
113 | 0 | Stream_Seek(s, usize); |
114 | 0 | } |
115 | | |
116 | 0 | if (pOffset) |
117 | 0 | { |
118 | 0 | INT64 tmp = _ftelli64(fp); |
119 | 0 | if (tmp < 0) |
120 | 0 | goto fail; |
121 | 0 | *pOffset = (size_t)tmp; |
122 | 0 | } |
123 | | |
124 | 0 | if (pts) |
125 | 0 | *pts = ts; |
126 | 0 | rc = TRUE; |
127 | |
|
128 | 0 | fail: |
129 | 0 | Stream_SealLength(s); |
130 | 0 | return rc; |
131 | 0 | } |
132 | | |
133 | | #if !defined(BUILD_TESTING_INTERNAL) |
134 | | static |
135 | | #endif |
136 | | BOOL stream_dump_write_line(FILE* fp, UINT32 flags, wStream* s) |
137 | 0 | { |
138 | 0 | BOOL rc = FALSE; |
139 | 0 | const UINT64 t = GetTickCount64(); |
140 | 0 | const BYTE* data = Stream_Buffer(s); |
141 | 0 | const size_t usize = Stream_Length(s); |
142 | 0 | const uint64_t size = (uint64_t)usize; |
143 | |
|
144 | 0 | if (!fp || !s) |
145 | 0 | return FALSE; |
146 | | |
147 | 0 | { |
148 | 0 | const UINT32 crc32 = crc32b(data, usize); |
149 | 0 | const BYTE received = flags & STREAM_MSG_SRV_RX; |
150 | 0 | size_t r = fwrite(&t, 1, sizeof(t), fp); |
151 | 0 | if (r != sizeof(t)) |
152 | 0 | goto fail; |
153 | 0 | r = fwrite(&received, 1, sizeof(received), fp); |
154 | 0 | if (r != sizeof(received)) |
155 | 0 | goto fail; |
156 | 0 | r = fwrite(&crc32, 1, sizeof(crc32), fp); |
157 | 0 | if (r != sizeof(crc32)) |
158 | 0 | goto fail; |
159 | 0 | r = fwrite(&size, 1, sizeof(size), fp); |
160 | 0 | if (r != sizeof(size)) |
161 | 0 | goto fail; |
162 | 0 | r = fwrite(data, 1, usize, fp); |
163 | 0 | if (r != usize) |
164 | 0 | goto fail; |
165 | 0 | } |
166 | | |
167 | 0 | rc = TRUE; |
168 | 0 | fail: |
169 | 0 | return rc; |
170 | 0 | } |
171 | | |
172 | | static FILE* stream_dump_get_file(const rdpSettings* settings, const char* mode) |
173 | 0 | { |
174 | 0 | const char* cfolder = nullptr; |
175 | 0 | char* file = nullptr; |
176 | 0 | FILE* fp = nullptr; |
177 | |
|
178 | 0 | if (!settings || !mode) |
179 | 0 | return nullptr; |
180 | | |
181 | 0 | cfolder = freerdp_settings_get_string(settings, FreeRDP_TransportDumpFile); |
182 | 0 | if (!cfolder) |
183 | 0 | file = GetKnownSubPath(KNOWN_PATH_TEMP, "freerdp-transport-dump"); |
184 | 0 | else |
185 | 0 | file = _strdup(cfolder); |
186 | |
|
187 | 0 | if (!file) |
188 | 0 | goto fail; |
189 | | |
190 | 0 | fp = winpr_fopen(file, mode); |
191 | 0 | fail: |
192 | 0 | free(file); |
193 | 0 | return fp; |
194 | 0 | } |
195 | | |
196 | | SSIZE_T stream_dump_append(const rdpContext* context, UINT32 flags, wStream* s, size_t* offset) |
197 | 0 | { |
198 | 0 | SSIZE_T rc = -1; |
199 | 0 | FILE* fp = nullptr; |
200 | 0 | const UINT32 mask = STREAM_MSG_SRV_RX | STREAM_MSG_SRV_TX; |
201 | 0 | CONNECTION_STATE state = freerdp_get_state(context); |
202 | 0 | int r = 0; |
203 | |
|
204 | 0 | if (!context || !s || !offset) |
205 | 0 | return -1; |
206 | | |
207 | 0 | if ((flags & STREAM_MSG_SRV_RX) && (flags & STREAM_MSG_SRV_TX)) |
208 | 0 | return -1; |
209 | | |
210 | 0 | if ((flags & mask) == 0) |
211 | 0 | return -1; |
212 | | |
213 | 0 | if (state < context->dump->state) |
214 | 0 | return 0; |
215 | | |
216 | 0 | fp = stream_dump_get_file(context->settings, "ab"); |
217 | 0 | if (!fp) |
218 | 0 | return -1; |
219 | | |
220 | 0 | r = _fseeki64(fp, WINPR_ASSERTING_INT_CAST(int64_t, *offset), SEEK_SET); |
221 | 0 | if (r < 0) |
222 | 0 | goto fail; |
223 | | |
224 | 0 | if (!stream_dump_write_line(fp, flags, s)) |
225 | 0 | goto fail; |
226 | 0 | { |
227 | 0 | const int64_t rt = _ftelli64(fp); |
228 | 0 | if (rt < 0) |
229 | 0 | { |
230 | 0 | rc = -1; |
231 | 0 | goto fail; |
232 | 0 | } |
233 | 0 | rc = WINPR_ASSERTING_INT_CAST(SSIZE_T, rt); |
234 | 0 | } |
235 | 0 | *offset = (size_t)rc; |
236 | |
|
237 | 0 | fail: |
238 | 0 | if (fp) |
239 | 0 | (void)fclose(fp); |
240 | 0 | return rc; |
241 | 0 | } |
242 | | |
243 | | SSIZE_T stream_dump_get(const rdpContext* context, UINT32* flags, wStream* s, size_t* offset, |
244 | | UINT64* pts) |
245 | 0 | { |
246 | 0 | SSIZE_T rc = -1; |
247 | 0 | FILE* fp = nullptr; |
248 | 0 | int r = 0; |
249 | |
|
250 | 0 | if (!context || !s || !offset) |
251 | 0 | return -1; |
252 | 0 | fp = stream_dump_get_file(context->settings, "rb"); |
253 | 0 | if (!fp) |
254 | 0 | return -1; |
255 | 0 | r = _fseeki64(fp, WINPR_ASSERTING_INT_CAST(int64_t, *offset), SEEK_SET); |
256 | 0 | if (r < 0) |
257 | 0 | goto fail; |
258 | | |
259 | 0 | if (!stream_dump_read_line(fp, s, pts, offset, flags)) |
260 | 0 | goto fail; |
261 | | |
262 | 0 | { |
263 | 0 | const int64_t rt = _ftelli64(fp); |
264 | 0 | if (rt < 0) |
265 | 0 | goto fail; |
266 | 0 | rc = WINPR_ASSERTING_INT_CAST(SSIZE_T, rt); |
267 | 0 | } |
268 | | |
269 | 0 | fail: |
270 | 0 | if (fp) |
271 | 0 | (void)fclose(fp); |
272 | 0 | return rc; |
273 | 0 | } |
274 | | |
275 | | static int stream_dump_transport_write(rdpTransport* transport, wStream* s) |
276 | 0 | { |
277 | 0 | SSIZE_T r = 0; |
278 | 0 | rdpContext* ctx = transport_get_context(transport); |
279 | |
|
280 | 0 | WINPR_ASSERT(ctx); |
281 | 0 | WINPR_ASSERT(ctx->dump); |
282 | 0 | WINPR_ASSERT(s); |
283 | |
|
284 | 0 | r = stream_dump_append(ctx, ctx->dump->isServer ? STREAM_MSG_SRV_TX : STREAM_MSG_SRV_RX, s, |
285 | 0 | &ctx->dump->writeDumpOffset); |
286 | 0 | if (r < 0) |
287 | 0 | return -1; |
288 | | |
289 | 0 | WINPR_ASSERT(ctx->dump->io.WritePdu); |
290 | 0 | return ctx->dump->io.WritePdu(transport, s); |
291 | 0 | } |
292 | | |
293 | | static int stream_dump_transport_read(rdpTransport* transport, wStream* s) |
294 | 0 | { |
295 | 0 | int rc = 0; |
296 | 0 | rdpContext* ctx = transport_get_context(transport); |
297 | |
|
298 | 0 | WINPR_ASSERT(ctx); |
299 | 0 | WINPR_ASSERT(ctx->dump); |
300 | 0 | WINPR_ASSERT(s); |
301 | |
|
302 | 0 | WINPR_ASSERT(ctx->dump->io.ReadPdu); |
303 | 0 | rc = ctx->dump->io.ReadPdu(transport, s); |
304 | 0 | if (rc > 0) |
305 | 0 | { |
306 | 0 | SSIZE_T r = |
307 | 0 | stream_dump_append(ctx, ctx->dump->isServer ? STREAM_MSG_SRV_RX : STREAM_MSG_SRV_TX, s, |
308 | 0 | &ctx->dump->readDumpOffset); |
309 | 0 | if (r < 0) |
310 | 0 | return -1; |
311 | 0 | } |
312 | 0 | return rc; |
313 | 0 | } |
314 | | |
315 | | static BOOL stream_dump_register_write_handlers(rdpContext* context) |
316 | 0 | { |
317 | 0 | rdpTransportIo dump = WINPR_C_ARRAY_INIT; |
318 | 0 | const rdpTransportIo* dfl = freerdp_get_io_callbacks(context); |
319 | |
|
320 | 0 | if (!freerdp_settings_get_bool(context->settings, FreeRDP_TransportDump)) |
321 | 0 | return TRUE; |
322 | | |
323 | 0 | WINPR_ASSERT(dfl); |
324 | 0 | dump = *dfl; |
325 | | |
326 | | /* Remember original callbacks for later */ |
327 | 0 | WINPR_ASSERT(context->dump); |
328 | 0 | context->dump->io.ReadPdu = dfl->ReadPdu; |
329 | 0 | context->dump->io.WritePdu = dfl->WritePdu; |
330 | | |
331 | | /* Set our dump wrappers */ |
332 | 0 | dump.WritePdu = stream_dump_transport_write; |
333 | 0 | dump.ReadPdu = stream_dump_transport_read; |
334 | 0 | return freerdp_set_io_callbacks(context, &dump); |
335 | 0 | } |
336 | | |
337 | | static int stream_dump_replay_transport_write(rdpTransport* transport, wStream* s) |
338 | 0 | { |
339 | 0 | rdpContext* ctx = transport_get_context(transport); |
340 | 0 | size_t size = 0; |
341 | |
|
342 | 0 | WINPR_ASSERT(ctx); |
343 | 0 | WINPR_ASSERT(s); |
344 | |
|
345 | 0 | size = Stream_Length(s); |
346 | 0 | WLog_Print(ctx->dump->log, WLOG_TRACE, "replay write %" PRIuz, size); |
347 | | // TODO: Compare with write file |
348 | |
|
349 | 0 | return 1; |
350 | 0 | } |
351 | | |
352 | | static int stream_dump_replay_transport_read(rdpTransport* transport, wStream* s) |
353 | 0 | { |
354 | 0 | rdpContext* ctx = transport_get_context(transport); |
355 | |
|
356 | 0 | size_t size = 0; |
357 | 0 | UINT64 slp = 0; |
358 | 0 | UINT64 ts = 0; |
359 | 0 | UINT32 flags = 0; |
360 | |
|
361 | 0 | WINPR_ASSERT(ctx); |
362 | 0 | WINPR_ASSERT(ctx->dump); |
363 | 0 | WINPR_ASSERT(s); |
364 | |
|
365 | 0 | const size_t start = Stream_GetPosition(s); |
366 | 0 | do |
367 | 0 | { |
368 | 0 | if (!Stream_SetPosition(s, start)) |
369 | 0 | return -1; |
370 | 0 | if (stream_dump_get(ctx, &flags, s, &ctx->dump->replayOffset, &ts) < 0) |
371 | 0 | return -1; |
372 | 0 | } while (flags & STREAM_MSG_SRV_RX); |
373 | | |
374 | 0 | if (!ctx->dump->nodelay) |
375 | 0 | { |
376 | 0 | if ((ctx->dump->replayTime > 0) && (ts > ctx->dump->replayTime)) |
377 | 0 | slp = ts - ctx->dump->replayTime; |
378 | 0 | } |
379 | 0 | ctx->dump->replayTime = ts; |
380 | |
|
381 | 0 | size = Stream_Length(s); |
382 | 0 | Stream_ResetPosition(s); |
383 | 0 | WLog_Print(ctx->dump->log, WLOG_TRACE, "replay read %" PRIuz, size); |
384 | |
|
385 | 0 | if (slp > 0) |
386 | 0 | { |
387 | 0 | uint64_t duration = slp; |
388 | 0 | do |
389 | 0 | { |
390 | 0 | const DWORD actual = (DWORD)MIN(duration, UINT32_MAX); |
391 | 0 | Sleep(actual); |
392 | 0 | duration -= actual; |
393 | 0 | } while (duration > 0); |
394 | 0 | } |
395 | |
|
396 | 0 | return 1; |
397 | 0 | } |
398 | | |
399 | | static int stream_dump_replay_transport_tcp_connect(WINPR_ATTR_UNUSED rdpContext* context, |
400 | | WINPR_ATTR_UNUSED rdpSettings* settings, |
401 | | WINPR_ATTR_UNUSED const char* hostname, |
402 | | WINPR_ATTR_UNUSED int port, |
403 | | WINPR_ATTR_UNUSED DWORD timeout) |
404 | 0 | { |
405 | 0 | WINPR_ASSERT(context); |
406 | 0 | WINPR_ASSERT(settings); |
407 | 0 | WINPR_ASSERT(hostname); |
408 | |
|
409 | 0 | return 42; |
410 | 0 | } |
411 | | |
412 | | static rdpTransportLayer* stream_dump_replay_transport_connect_layer( |
413 | | WINPR_ATTR_UNUSED rdpTransport* transport, WINPR_ATTR_UNUSED const char* hostname, |
414 | | WINPR_ATTR_UNUSED int port, WINPR_ATTR_UNUSED DWORD timeout) |
415 | 0 | { |
416 | 0 | WINPR_ASSERT(transport); |
417 | 0 | WINPR_ASSERT(hostname); |
418 | |
|
419 | 0 | return nullptr; |
420 | 0 | } |
421 | | |
422 | | static BOOL stream_dump_replay_transport_tls_connect(WINPR_ATTR_UNUSED rdpTransport* transport) |
423 | 0 | { |
424 | 0 | WINPR_ASSERT(transport); |
425 | 0 | return TRUE; |
426 | 0 | } |
427 | | |
428 | | static BOOL stream_dump_replay_transport_accept(WINPR_ATTR_UNUSED rdpTransport* transport) |
429 | 0 | { |
430 | 0 | WINPR_ASSERT(transport); |
431 | 0 | return TRUE; |
432 | 0 | } |
433 | | |
434 | | static BOOL stream_dump_register_read_handlers(rdpContext* context) |
435 | 0 | { |
436 | 0 | const rdpTransportIo* dfl = freerdp_get_io_callbacks(context); |
437 | |
|
438 | 0 | if (!freerdp_settings_get_bool(context->settings, FreeRDP_TransportDumpReplay)) |
439 | 0 | return TRUE; |
440 | | |
441 | 0 | WINPR_ASSERT(dfl); |
442 | 0 | rdpTransportIo dump = *dfl; |
443 | | |
444 | | /* Remember original callbacks for later */ |
445 | 0 | WINPR_ASSERT(context->dump); |
446 | 0 | context->dump->nodelay = |
447 | 0 | freerdp_settings_get_bool(context->settings, FreeRDP_TransportDumpReplayNodelay); |
448 | 0 | context->dump->io.ReadPdu = dfl->ReadPdu; |
449 | 0 | context->dump->io.WritePdu = dfl->WritePdu; |
450 | | |
451 | | /* Set our dump wrappers */ |
452 | 0 | dump.WritePdu = stream_dump_transport_write; |
453 | 0 | dump.ReadPdu = stream_dump_transport_read; |
454 | | |
455 | | /* Set our dump wrappers */ |
456 | 0 | dump.WritePdu = stream_dump_replay_transport_write; |
457 | 0 | dump.ReadPdu = stream_dump_replay_transport_read; |
458 | 0 | dump.TCPConnect = stream_dump_replay_transport_tcp_connect; |
459 | 0 | dump.TLSAccept = stream_dump_replay_transport_accept; |
460 | 0 | dump.TLSConnect = stream_dump_replay_transport_tls_connect; |
461 | 0 | dump.ConnectLayer = stream_dump_replay_transport_connect_layer; |
462 | 0 | if (!freerdp_set_io_callbacks(context, &dump)) |
463 | 0 | return FALSE; |
464 | 0 | return freerdp_io_callback_set_event(context, TRUE); |
465 | 0 | } |
466 | | |
467 | | BOOL stream_dump_register_handlers(rdpContext* context, CONNECTION_STATE state, BOOL isServer) |
468 | 0 | { |
469 | 0 | WINPR_ASSERT(context); |
470 | 0 | WINPR_ASSERT(context->dump); |
471 | 0 | context->dump->state = state; |
472 | 0 | context->dump->isServer = isServer; |
473 | 0 | if (!stream_dump_register_write_handlers(context)) |
474 | 0 | return FALSE; |
475 | 0 | return stream_dump_register_read_handlers(context); |
476 | 0 | } |
477 | | |
478 | | void stream_dump_free(rdpStreamDumpContext* dump) |
479 | 15.4k | { |
480 | 15.4k | free(dump); |
481 | 15.4k | } |
482 | | |
483 | | rdpStreamDumpContext* stream_dump_new(void) |
484 | 15.4k | { |
485 | 15.4k | rdpStreamDumpContext* dump = calloc(1, sizeof(rdpStreamDumpContext)); |
486 | 15.4k | if (!dump) |
487 | 0 | return nullptr; |
488 | 15.4k | dump->log = WLog_Get(TAG); |
489 | | |
490 | 15.4k | return dump; |
491 | 15.4k | } |