1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/* BSD-2-Clause license
*
* Copyright (c) 2018-2023 NST <www.newinfosec.ru>, sss <sss at dark-alexandr dot net>.
*
*/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <ev.h>
#include <wslay/wslay.h>
#include "ws_session.h"
#include "ws_protocol.h"
#include "webrdp_module_api.h"
#include "wrdp_thpool_internals.h"
#include "globals.h"
#include "task.h"
#include "thread_impl.h"
#include "utilities.h"
#include "log.h"
void
send_text_msg(const char *msg, void *_task_info)
{
ws_send_text((uint8_t *)msg, strlen(msg), _task_info);
}
static void
send_typed_text_msg(const char *type, const char *msg, void *_task_info)
{
size_t len = strlen(msg) + strlen(type);
uint8_t *buf = malloc(len);
#ifdef DEBUG
log_msg((const uint8_t *)msg, strlen(msg), wrdp_log_level_trace, 0);
#endif
if (!buf)
{
perror("malloc");
return;
}
memset(buf, 0, len);
memcpy(buf, type, strlen(type));
memcpy(buf + strlen(type), msg, len - strlen(type));
ws_send_text(buf, len, _task_info);
free(buf);
}
void
send_text_info_msg(const char *msg, void *task_info)
{
send_typed_text_msg("I:", msg, task_info);
}
void
send_text_warning_msg(const char *msg, void *task_info)
{
send_typed_text_msg("W:", msg, task_info);
}
void
send_text_debug_msg(const char *msg, void *task_info)
{
send_typed_text_msg("D:", msg, task_info);
}
void
send_error_msg(const char *msg, void *task_info)
{
send_typed_text_msg("E:", msg, task_info);
}
void
send_ctl_msg(const char *msg, void *task_info)
{
send_typed_text_msg("0:", msg, task_info);
}
static void
send_termination_msg(void *task_info)
{
ws_send_text((uint8_t *)"T:", 2, task_info);
}
static void
send_typed_proto_msg(
uint8_t *buf, size_t size, ws_output_codes type, void *task_info)
{
uint8_t *msg = ws_pack_msg(buf, size, type);
ws_send_binary(msg, size + 4, task_info);
free(msg);
}
static void
send_begin_paint(void *task_info)
{
send_typed_proto_msg(0, 0, ws_out_beginpaint, task_info);
}
static void
send_end_paint(void *task_info)
{
send_typed_proto_msg(0, 0, ws_out_endpaint, task_info);
}
static void
send_bitmap(
const wrdp_core_display_bmp *bmp, uint8_t *bmp_data, void *task_info)
{
size_t buf_size = sizeof(wrdp_core_display_bmp) + bmp->size;
uint8_t *buf = malloc(buf_size);
if (!buf)
{
perror("malloc");
return;
}
memcpy(buf, bmp, sizeof(wrdp_core_display_bmp));
memcpy(buf + sizeof(wrdp_core_display_bmp), bmp_data, bmp->size);
send_typed_proto_msg(buf, buf_size, ws_out_bitmap, task_info);
free(buf);
}
static void
send_opaque_rect_order(
const wrdp_core_display_opaque_rect_order *oro, void *task_info)
{
send_typed_proto_msg((uint8_t *)oro,
sizeof(wrdp_core_display_opaque_rect_order), ws_out_opaquerect,
task_info);
}
static void
send_set_bounds(const wrdp_core_display_bounds *bounds, void *task_info)
{
send_typed_proto_msg((uint8_t *)bounds,
sizeof(wrdp_core_display_bounds), ws_out_setbounds, task_info);
}
static void
send_pat_blt(const wrdp_core_display_patblt_order *patblt, void *task_info)
{
send_typed_proto_msg((uint8_t *)patblt,
sizeof(wrdp_core_display_patblt_order), ws_out_patblt, task_info);
}
static void
send_multi_opaque_rect(
const wrdp_core_display_m_opaque_rect *mrect, void *task_info)
{
size_t buf_size
= (sizeof(uint32_t) * 2)
+ (sizeof(wrdp_core_display_delta_rect) * mrect->num_rect);
uint8_t *buf = malloc(buf_size);
if (!buf)
{
perror("malloc");
return;
}
memcpy(buf, (uint8_t *)&(mrect->color), 4);
memcpy(buf + 4, (uint8_t *)&(mrect->num_rect), 4);
memcpy(buf + 4 + 4, (uint8_t *)mrect->rects,
mrect->num_rect * sizeof(wrdp_core_display_delta_rect));
send_typed_proto_msg(buf, buf_size, ws_out_multi_opaquerect, task_info);
free(buf);
}
static void
send_scr_blt(const wrdp_core_display_scr_blt *scr_blt, void *task_info)
{
send_typed_proto_msg((uint8_t *)&scr_blt,
sizeof(wrdp_core_display_scr_blt), ws_out_scr_btl, task_info);
}
static void
send_ptr_new(const wrdp_core_display_cursor_info *ptr, void *task_info)
{
size_t buf_size = sizeof(wrdp_core_display_cursor) - sizeof(uint8_t *)
+ sizeof(wrdp_core_display_cursor_info)
- sizeof(uint32_t)
- sizeof(wrdp_core_display_cursor *) + ptr->data_size;
uint8_t *buf = malloc(buf_size);
if (!buf)
{
perror("malloc");
return;
}
memcpy(buf, ptr,
sizeof(wrdp_core_display_cursor_info) - sizeof(uint32_t)
- sizeof(wrdp_core_display_cursor *));
memcpy(buf + sizeof(wrdp_core_display_cursor_info) - sizeof(uint32_t)
- sizeof(wrdp_core_display_cursor *),
ptr->cur, sizeof(wrdp_core_display_cursor) - sizeof(uint8_t *));
memcpy(buf + sizeof(wrdp_core_display_cursor_info) - sizeof(uint32_t)
- sizeof(wrdp_core_display_cursor *)
+ sizeof(wrdp_core_display_cursor) - sizeof(uint8_t *),
ptr->cur->data, ptr->data_size);
send_typed_proto_msg(buf, buf_size, ws_out_ptr_new, task_info);
free(buf);
}
static void
send_ptr_free(uint32_t ptr_id, void *task_info)
{
send_typed_proto_msg(
(uint8_t *)&ptr_id, sizeof(uint32_t), ws_out_ptr_free, task_info);
}
static void
send_ptr_set(uint32_t ptr_id, void *task_info)
{
send_typed_proto_msg(
(uint8_t *)&ptr_id, sizeof(uint32_t), ws_out_ptr_set, task_info);
}
static void
send_ptr_set_null(void *task_info)
{
send_typed_proto_msg(0, 0, ws_out_ptr_set_null, task_info);
}
static void
send_ptr_set_default(void *task_info)
{
send_typed_proto_msg(0, 0, ws_out_ptr_set_default, task_info);
}
static void
clipboard_changed(uint8_t *fmts, size_t fmts_count, void *task_info)
{
send_typed_proto_msg(
fmts, fmts_count, ws_out_clpbrd_changed, task_info);
}
static void
clipboard_send_data(uint8_t *buf, size_t buf_size, void *task_info)
{
send_typed_proto_msg(buf, buf_size, ws_out_clpbrd_data, task_info);
}
static void
clipboard_request_data(uint8_t data_fmt, void *task_info)
{
send_typed_proto_msg(
&data_fmt, 1, ws_out_clpbrd_request_data, task_info);
}
static void
ft_request(const wrdp_backend_ft_file_request *req, void *task_info)
{
send_typed_proto_msg((uint8_t *)req,
sizeof(wrdp_backend_ft_file_request), ws_out_ft_request, task_info);
}
static void
ft_send_chunk(
const wrdp_backend_ft_chunk *chunk, uint8_t *data, void *task_info)
{
size_t buf_size = sizeof(wrdp_backend_ft_chunk) + chunk->size;
uint8_t *buf = malloc(buf_size);
if (!buf)
{
perror("malloc");
return;
}
memcpy(buf, chunk, sizeof(wrdp_backend_ft_chunk));
memcpy(buf + sizeof(wrdp_backend_ft_chunk), data, chunk->size);
send_typed_proto_msg(buf, buf_size, ws_out_ft_chunk, task_info);
free(buf);
}
static void
ft_finish(const wrdp_backend_ft_status *status, void *task_info)
{
send_typed_proto_msg((uint8_t *)status, sizeof(wrdp_backend_ft_status),
ws_out_ft_finish, task_info);
}
static void *
get_libev_loop(void *_task_info)
{
task_info *t = _task_info;
wrdp_thpool_task *pt = t->wrdp_thpool_task;
return pt->thread->ev_th_loop;
}
static void
task_finished(bool success, void *_task_info)
{
/* TODO: handle task failure */
destroy_task(_task_info);
}
static void
reset_idle(void *_task_info)
{
task_info *t = _task_info;
t->idle_time = 0;
}
bool
init_exports()
{
g_globals.exports = calloc(1, sizeof(wrdp_core_exports));
if (!g_globals.exports)
{
perror("calloc");
exit(EXIT_FAILURE);
}
g_globals.exports->api_core = calloc(1, sizeof(wrdp_core_cb_core));
if (!g_globals.exports->api_core)
{
perror("calloc");
exit(EXIT_FAILURE);
}
g_globals.exports->api_msgs = calloc(1, sizeof(wrdp_core_cb_msgs));
if (!g_globals.exports->api_msgs)
{
perror("calloc");
exit(EXIT_FAILURE);
}
g_globals.exports->api_paint = calloc(1, sizeof(wrdp_core_cb_paint));
if (!g_globals.exports->api_paint)
{
perror("calloc");
exit(EXIT_FAILURE);
}
g_globals.exports->api_clipboard
= calloc(1, sizeof(wrdp_core_cb_clipboard));
if (!g_globals.exports->api_clipboard)
{
perror("calloc");
exit(EXIT_FAILURE);
}
g_globals.exports->api_filetransfers
= calloc(1, sizeof(wrdp_core_cb_filetransfer));
if (!g_globals.exports->api_filetransfers)
{
perror("calloc");
exit(EXIT_FAILURE);
}
g_globals.exports->api_utils = calloc(1, sizeof(wrdp_core_cb_utils));
if (!g_globals.exports->api_utils)
{
perror("calloc");
exit(EXIT_FAILURE);
}
g_globals.exports->api_paint->send_begin_paint = send_begin_paint;
g_globals.exports->api_paint->send_bitmap = send_bitmap;
g_globals.exports->api_paint->send_end_paint = send_end_paint;
g_globals.exports->api_paint->send_multi_opaque_rect
= send_multi_opaque_rect;
g_globals.exports->api_paint->send_opaque_rect_order
= send_opaque_rect_order;
g_globals.exports->api_paint->send_pat_blt = send_pat_blt;
g_globals.exports->api_paint->send_ptr_free = send_ptr_free;
g_globals.exports->api_paint->send_ptr_new = send_ptr_new;
g_globals.exports->api_paint->send_ptr_set = send_ptr_set;
g_globals.exports->api_paint->send_ptr_set_default
= send_ptr_set_default;
g_globals.exports->api_paint->send_ptr_set_null = send_ptr_set_null;
g_globals.exports->api_paint->send_scr_blt = send_scr_blt;
g_globals.exports->api_paint->send_set_bounds = send_set_bounds;
g_globals.exports->api_msgs->send_text_msg = send_text_msg;
g_globals.exports->api_msgs->send_text_info_msg = send_text_info_msg;
g_globals.exports->api_msgs->send_text_warning_msg
= send_text_warning_msg;
g_globals.exports->api_msgs->send_text_debug_msg = send_text_debug_msg;
g_globals.exports->api_msgs->send_ctl_msg = send_ctl_msg;
g_globals.exports->api_msgs->send_error_msg = send_error_msg;
g_globals.exports->api_msgs->send_termination_msg
= send_termination_msg;
g_globals.exports->api_core->get_libev_loop = get_libev_loop;
g_globals.exports->api_core->task_finished = task_finished;
g_globals.exports->api_core->reset_idle = reset_idle;
g_globals.exports->api_clipboard->send_data = clipboard_send_data;
g_globals.exports->api_clipboard->clipboard_changed = clipboard_changed;
g_globals.exports->api_clipboard->request_data = clipboard_request_data;
g_globals.exports->api_filetransfers->ft_finish = ft_finish;
g_globals.exports->api_filetransfers->ft_request = ft_request;
g_globals.exports->api_filetransfers->ft_send_chunk = ft_send_chunk;
g_globals.exports->api_utils->hex_print = hex_print;
g_globals.exports->api_utils->log_msg = log_msg;
g_globals.exports->api_utils->log_msg_ex = log_msg_ex;
return true;
}
|