1 |
#include <unistd.h> |
2 |
#include <sys/types.h> |
3 |
#include <sys/time.h> |
4 |
#include <time.h> |
5 |
#include "rdesktop.h" |
6 |
|
7 |
#define IRP_MJ_CREATE 0x00 |
8 |
#define IRP_MJ_CLOSE 0x02 |
9 |
#define IRP_MJ_READ 0x03 |
10 |
#define IRP_MJ_WRITE 0x04 |
11 |
#define IRP_MJ_DEVICE_CONTROL 0x0e |
12 |
|
13 |
#define IRP_MJ_CREATE 0x00 |
14 |
#define IRP_MJ_CLOSE 0x02 |
15 |
#define IRP_MJ_READ 0x03 |
16 |
#define IRP_MJ_WRITE 0x04 |
17 |
#define IRP_MJ_QUERY_INFORMATION 0x05 |
18 |
#define IRP_MJ_SET_INFORMATION 0x06 |
19 |
#define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a |
20 |
#define IRP_MJ_DIRECTORY_CONTROL 0x0c |
21 |
#define IRP_MJ_DEVICE_CONTROL 0x0e |
22 |
|
23 |
#define IRP_MN_QUERY_DIRECTORY 0x01 |
24 |
#define IRP_MN_NOTIFY_CHANGE_DIRECTORY 0x02 |
25 |
|
26 |
extern char hostname[16]; |
27 |
extern DEVICE_FNS serial_fns; |
28 |
extern DEVICE_FNS printer_fns; |
29 |
extern DEVICE_FNS parallel_fns; |
30 |
extern DEVICE_FNS disk_fns; |
31 |
|
32 |
static VCHANNEL *rdpdr_channel; |
33 |
|
34 |
/* If select() times out, the request for the device with handle g_min_timeout_fd is aborted */ |
35 |
HANDLE g_min_timeout_fd; |
36 |
uint32 g_num_devices; |
37 |
|
38 |
/* Table with information about rdpdr devices */ |
39 |
RDPDR_DEVICE g_rdpdr_device[RDPDR_MAX_DEVICES]; |
40 |
|
41 |
/* Used to store incoming io request, until they are ready to be completed */ |
42 |
/* using a linked list ensures that they are processed in the right order, */ |
43 |
/* if multiple ios are being done on the same fd */ |
44 |
struct async_iorequest |
45 |
{ |
46 |
uint32 fd, major, minor, offset, device, id, length, partial_len; |
47 |
long timeout, /* Total timeout */ |
48 |
itv_timeout; /* Interval timeout (between serial characters) */ |
49 |
uint8 *buffer; |
50 |
DEVICE_FNS *fns; |
51 |
|
52 |
struct async_iorequest *next; /* next element in list */ |
53 |
}; |
54 |
|
55 |
struct async_iorequest *g_iorequest; |
56 |
|
57 |
/* Return device_id for a given handle */ |
58 |
int |
59 |
get_device_index(HANDLE handle) |
60 |
{ |
61 |
int i; |
62 |
for (i = 0; i < RDPDR_MAX_DEVICES; i++) |
63 |
{ |
64 |
if (g_rdpdr_device[i].handle == handle) |
65 |
return i; |
66 |
} |
67 |
return -1; |
68 |
} |
69 |
|
70 |
/* Converts a windows path to a unix path */ |
71 |
void |
72 |
convert_to_unix_filename(char *filename) |
73 |
{ |
74 |
char *p; |
75 |
|
76 |
while ((p = strchr(filename, '\\'))) |
77 |
{ |
78 |
*p = '/'; |
79 |
} |
80 |
} |
81 |
|
82 |
/* Add a new io request to the table containing pending io requests so it won't block rdesktop */ |
83 |
BOOL |
84 |
add_async_iorequest(uint32 device, uint32 file, uint32 id, uint32 major, uint32 length, |
85 |
DEVICE_FNS * fns, uint32 total_timeout, uint32 interval_timeout, uint8 * buffer) |
86 |
{ |
87 |
struct async_iorequest *iorq; |
88 |
|
89 |
if (g_iorequest == NULL) |
90 |
{ |
91 |
g_iorequest = (struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); |
92 |
if (!g_iorequest) |
93 |
return False; |
94 |
g_iorequest->fd = 0; |
95 |
g_iorequest->next = NULL; |
96 |
} |
97 |
|
98 |
iorq = g_iorequest; |
99 |
|
100 |
while (iorq->fd != 0) |
101 |
{ |
102 |
// create new element if needed |
103 |
if (iorq->next == NULL) |
104 |
{ |
105 |
iorq->next = |
106 |
(struct async_iorequest *) xmalloc(sizeof(struct async_iorequest)); |
107 |
if (!iorq->next) |
108 |
return False; |
109 |
iorq->next->fd = 0; |
110 |
iorq->next->next = NULL; |
111 |
} |
112 |
iorq = iorq->next; |
113 |
} |
114 |
iorq->device = device; |
115 |
iorq->fd = file; |
116 |
iorq->id = id; |
117 |
iorq->major = major; |
118 |
iorq->length = length; |
119 |
iorq->partial_len = 0; |
120 |
iorq->fns = fns; |
121 |
iorq->timeout = total_timeout; |
122 |
iorq->itv_timeout = interval_timeout; |
123 |
iorq->buffer = buffer; |
124 |
return True; |
125 |
} |
126 |
|
127 |
void |
128 |
rdpdr_send_connect(void) |
129 |
{ |
130 |
uint8 magic[4] = "rDCC"; |
131 |
STREAM s; |
132 |
|
133 |
s = channel_init(rdpdr_channel, 12); |
134 |
out_uint8a(s, magic, 4); |
135 |
out_uint16_le(s, 1); /* unknown */ |
136 |
out_uint16_le(s, 5); |
137 |
out_uint32_be(s, 0x815ed39d); /* IP address (use 127.0.0.1) 0x815ed39d */ |
138 |
s_mark_end(s); |
139 |
channel_send(s, rdpdr_channel); |
140 |
} |
141 |
|
142 |
|
143 |
void |
144 |
rdpdr_send_name(void) |
145 |
{ |
146 |
uint8 magic[4] = "rDNC"; |
147 |
uint32 hostlen = (strlen(hostname) + 1) * 2; |
148 |
STREAM s; |
149 |
|
150 |
s = channel_init(rdpdr_channel, 16 + hostlen); |
151 |
out_uint8a(s, magic, 4); |
152 |
out_uint16_le(s, 0x63); /* unknown */ |
153 |
out_uint16_le(s, 0x72); |
154 |
out_uint32(s, 0); |
155 |
out_uint32_le(s, hostlen); |
156 |
rdp_out_unistr(s, hostname, hostlen - 2); |
157 |
s_mark_end(s); |
158 |
channel_send(s, rdpdr_channel); |
159 |
} |
160 |
|
161 |
/* Returns the size of the payload of the announce packet */ |
162 |
int |
163 |
announcedata_size() |
164 |
{ |
165 |
int size, i; |
166 |
PRINTER *printerinfo; |
167 |
|
168 |
size = 8; //static announce size |
169 |
size += g_num_devices * 0x14; |
170 |
|
171 |
for (i = 0; i < g_num_devices; i++) |
172 |
{ |
173 |
if (g_rdpdr_device[i].device_type == DEVICE_TYPE_PRINTER) |
174 |
{ |
175 |
printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data; |
176 |
printerinfo->bloblen = |
177 |
printercache_load_blob(printerinfo->printer, &(printerinfo->blob)); |
178 |
|
179 |
size += 0x18; |
180 |
size += 2 * strlen(printerinfo->driver) + 2; |
181 |
size += 2 * strlen(printerinfo->printer) + 2; |
182 |
size += printerinfo->bloblen; |
183 |
} |
184 |
} |
185 |
|
186 |
return size; |
187 |
} |
188 |
|
189 |
void |
190 |
rdpdr_send_available(void) |
191 |
{ |
192 |
|
193 |
uint8 magic[4] = "rDAD"; |
194 |
uint32 driverlen, printerlen, bloblen; |
195 |
int i; |
196 |
STREAM s; |
197 |
PRINTER *printerinfo; |
198 |
|
199 |
s = channel_init(rdpdr_channel, announcedata_size()); |
200 |
out_uint8a(s, magic, 4); |
201 |
out_uint32_le(s, g_num_devices); |
202 |
|
203 |
for (i = 0; i < g_num_devices; i++) |
204 |
{ |
205 |
out_uint32_le(s, g_rdpdr_device[i].device_type); |
206 |
out_uint32_le(s, i); /* RDP Device ID */ |
207 |
out_uint8p(s, g_rdpdr_device[i].name, 8); |
208 |
|
209 |
switch (g_rdpdr_device[i].device_type) |
210 |
{ |
211 |
case DEVICE_TYPE_PRINTER: |
212 |
printerinfo = (PRINTER *) g_rdpdr_device[i].pdevice_data; |
213 |
|
214 |
driverlen = 2 * strlen(printerinfo->driver) + 2; |
215 |
printerlen = 2 * strlen(printerinfo->printer) + 2; |
216 |
bloblen = printerinfo->bloblen; |
217 |
|
218 |
out_uint32_le(s, 24 + driverlen + printerlen + bloblen); /* length of extra info */ |
219 |
out_uint32_le(s, printerinfo->default_printer ? 2 : 0); |
220 |
out_uint8s(s, 8); /* unknown */ |
221 |
out_uint32_le(s, driverlen); |
222 |
out_uint32_le(s, printerlen); |
223 |
out_uint32_le(s, bloblen); |
224 |
rdp_out_unistr(s, printerinfo->driver, driverlen - 2); |
225 |
rdp_out_unistr(s, printerinfo->printer, printerlen - 2); |
226 |
out_uint8a(s, printerinfo->blob, bloblen); |
227 |
|
228 |
if (printerinfo->blob) |
229 |
xfree(printerinfo->blob); /* Blob is sent twice if reconnecting */ |
230 |
break; |
231 |
default: |
232 |
out_uint32(s, 0); |
233 |
} |
234 |
} |
235 |
#if 0 |
236 |
out_uint32_le(s, 0x20); /* Device type 0x20 - smart card */ |
237 |
out_uint32_le(s, 0); |
238 |
out_uint8p(s, "SCARD", 5); |
239 |
out_uint8s(s, 3); |
240 |
out_uint32(s, 0); |
241 |
#endif |
242 |
|
243 |
s_mark_end(s); |
244 |
channel_send(s, rdpdr_channel); |
245 |
} |
246 |
|
247 |
void |
248 |
rdpdr_send_completion(uint32 device, uint32 id, uint32 status, uint32 result, uint8 * buffer, |
249 |
uint32 length) |
250 |
{ |
251 |
uint8 magic[4] = "rDCI"; |
252 |
STREAM s; |
253 |
|
254 |
s = channel_init(rdpdr_channel, 20 + length); |
255 |
out_uint8a(s, magic, 4); |
256 |
out_uint32_le(s, device); |
257 |
out_uint32_le(s, id); |
258 |
out_uint32_le(s, status); |
259 |
out_uint32_le(s, result); |
260 |
out_uint8p(s, buffer, length); |
261 |
s_mark_end(s); |
262 |
/* JIF |
263 |
hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); */ |
264 |
channel_send(s, rdpdr_channel); |
265 |
} |
266 |
|
267 |
static void |
268 |
rdpdr_process_irp(STREAM s) |
269 |
{ |
270 |
uint32 result = 0, |
271 |
length = 0, |
272 |
desired_access = 0, |
273 |
request, |
274 |
file, |
275 |
info_level, |
276 |
buffer_len, |
277 |
id, |
278 |
major, |
279 |
minor, |
280 |
device, |
281 |
offset, |
282 |
bytes_in, |
283 |
bytes_out, |
284 |
error_mode, |
285 |
share_mode, disposition, total_timeout, interval_timeout, flags_and_attributes = 0; |
286 |
|
287 |
char filename[256]; |
288 |
uint8 *buffer, *pst_buf; |
289 |
struct stream out; |
290 |
DEVICE_FNS *fns; |
291 |
BOOL rw_blocking = True; |
292 |
NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST; |
293 |
|
294 |
in_uint32_le(s, device); |
295 |
in_uint32_le(s, file); |
296 |
in_uint32_le(s, id); |
297 |
in_uint32_le(s, major); |
298 |
in_uint32_le(s, minor); |
299 |
|
300 |
buffer_len = 0; |
301 |
buffer = (uint8 *) xmalloc(1024); |
302 |
buffer[0] = 0; |
303 |
|
304 |
switch (g_rdpdr_device[device].device_type) |
305 |
{ |
306 |
case DEVICE_TYPE_SERIAL: |
307 |
|
308 |
fns = &serial_fns; |
309 |
rw_blocking = False; |
310 |
break; |
311 |
|
312 |
case DEVICE_TYPE_PARALLEL: |
313 |
|
314 |
fns = ¶llel_fns; |
315 |
rw_blocking = False; |
316 |
break; |
317 |
|
318 |
case DEVICE_TYPE_PRINTER: |
319 |
|
320 |
fns = &printer_fns; |
321 |
break; |
322 |
|
323 |
case DEVICE_TYPE_DISK: |
324 |
|
325 |
fns = &disk_fns; |
326 |
rw_blocking = False; |
327 |
break; |
328 |
|
329 |
case DEVICE_TYPE_SCARD: |
330 |
default: |
331 |
|
332 |
error("IRP for bad device %ld\n", device); |
333 |
return; |
334 |
} |
335 |
|
336 |
switch (major) |
337 |
{ |
338 |
case IRP_MJ_CREATE: |
339 |
|
340 |
in_uint32_be(s, desired_access); |
341 |
in_uint8s(s, 0x08); // unknown |
342 |
in_uint32_le(s, error_mode); |
343 |
in_uint32_le(s, share_mode); |
344 |
in_uint32_le(s, disposition); |
345 |
in_uint32_le(s, flags_and_attributes); |
346 |
in_uint32_le(s, length); |
347 |
|
348 |
if (length && (length / 2) < 256) |
349 |
{ |
350 |
rdp_in_unistr(s, filename, length); |
351 |
convert_to_unix_filename(filename); |
352 |
} |
353 |
else |
354 |
{ |
355 |
filename[0] = 0; |
356 |
} |
357 |
|
358 |
if (!fns->create) |
359 |
{ |
360 |
status = STATUS_NOT_SUPPORTED; |
361 |
break; |
362 |
} |
363 |
|
364 |
status = fns->create(device, desired_access, share_mode, disposition, |
365 |
flags_and_attributes, filename, &result); |
366 |
buffer_len = 1; |
367 |
break; |
368 |
|
369 |
case IRP_MJ_CLOSE: |
370 |
if (!fns->close) |
371 |
{ |
372 |
status = STATUS_NOT_SUPPORTED; |
373 |
break; |
374 |
} |
375 |
|
376 |
status = fns->close(file); |
377 |
break; |
378 |
|
379 |
case IRP_MJ_READ: |
380 |
|
381 |
if (!fns->read) |
382 |
{ |
383 |
status = STATUS_NOT_SUPPORTED; |
384 |
break; |
385 |
} |
386 |
|
387 |
in_uint32_le(s, length); |
388 |
in_uint32_le(s, offset); |
389 |
#if WITH_DEBUG_RDP5 |
390 |
DEBUG(("RDPDR IRP Read (length: %d, offset: %d)\n", length, offset)); |
391 |
#endif |
392 |
if (rw_blocking) // Complete read immediately |
393 |
{ |
394 |
buffer = (uint8 *) xrealloc((void *) buffer, length); |
395 |
if (!buffer) |
396 |
{ |
397 |
status = STATUS_CANCELLED; |
398 |
break; |
399 |
} |
400 |
status = fns->read(file, buffer, length, offset, &result); |
401 |
buffer_len = result; |
402 |
break; |
403 |
} |
404 |
|
405 |
// Add request to table |
406 |
pst_buf = (uint8 *) xmalloc(length); |
407 |
if (!pst_buf) |
408 |
{ |
409 |
status = STATUS_CANCELLED; |
410 |
break; |
411 |
} |
412 |
serial_get_timeout(file, length, &total_timeout, &interval_timeout); |
413 |
if (add_async_iorequest |
414 |
(device, file, id, major, length, fns, total_timeout, interval_timeout, |
415 |
pst_buf)) |
416 |
{ |
417 |
status = STATUS_PENDING; |
418 |
break; |
419 |
} |
420 |
|
421 |
status = STATUS_CANCELLED; |
422 |
break; |
423 |
case IRP_MJ_WRITE: |
424 |
|
425 |
buffer_len = 1; |
426 |
|
427 |
if (!fns->write) |
428 |
{ |
429 |
status = STATUS_NOT_SUPPORTED; |
430 |
break; |
431 |
} |
432 |
|
433 |
in_uint32_le(s, length); |
434 |
in_uint32_le(s, offset); |
435 |
in_uint8s(s, 0x18); |
436 |
#if WITH_DEBUG_RDP5 |
437 |
DEBUG(("RDPDR IRP Write (length: %d)\n", result)); |
438 |
#endif |
439 |
if (rw_blocking) // Complete immediately |
440 |
{ |
441 |
status = fns->write(file, s->p, length, offset, &result); |
442 |
break; |
443 |
} |
444 |
|
445 |
// Add to table |
446 |
pst_buf = (uint8 *) xmalloc(length); |
447 |
if (!pst_buf) |
448 |
{ |
449 |
status = STATUS_CANCELLED; |
450 |
break; |
451 |
} |
452 |
|
453 |
in_uint8a(s, pst_buf, length); |
454 |
|
455 |
if (add_async_iorequest |
456 |
(device, file, id, major, length, fns, 0, 0, pst_buf)) |
457 |
{ |
458 |
status = STATUS_PENDING; |
459 |
break; |
460 |
} |
461 |
|
462 |
status = STATUS_CANCELLED; |
463 |
break; |
464 |
|
465 |
case IRP_MJ_QUERY_INFORMATION: |
466 |
|
467 |
if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK) |
468 |
{ |
469 |
status = STATUS_INVALID_HANDLE; |
470 |
break; |
471 |
} |
472 |
in_uint32_le(s, info_level); |
473 |
|
474 |
out.data = out.p = buffer; |
475 |
out.size = sizeof(buffer); |
476 |
status = disk_query_information(file, info_level, &out); |
477 |
result = buffer_len = out.p - out.data; |
478 |
|
479 |
break; |
480 |
|
481 |
case IRP_MJ_SET_INFORMATION: |
482 |
|
483 |
if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK) |
484 |
{ |
485 |
status = STATUS_INVALID_HANDLE; |
486 |
break; |
487 |
} |
488 |
|
489 |
in_uint32_le(s, info_level); |
490 |
|
491 |
out.data = out.p = buffer; |
492 |
out.size = sizeof(buffer); |
493 |
status = disk_set_information(file, info_level, s, &out); |
494 |
result = buffer_len = out.p - out.data; |
495 |
break; |
496 |
|
497 |
case IRP_MJ_QUERY_VOLUME_INFORMATION: |
498 |
|
499 |
if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK) |
500 |
{ |
501 |
status = STATUS_INVALID_HANDLE; |
502 |
break; |
503 |
} |
504 |
|
505 |
in_uint32_le(s, info_level); |
506 |
|
507 |
out.data = out.p = buffer; |
508 |
out.size = sizeof(buffer); |
509 |
status = disk_query_volume_information(file, info_level, &out); |
510 |
result = buffer_len = out.p - out.data; |
511 |
break; |
512 |
|
513 |
case IRP_MJ_DIRECTORY_CONTROL: |
514 |
|
515 |
if (g_rdpdr_device[device].device_type != DEVICE_TYPE_DISK) |
516 |
{ |
517 |
status = STATUS_INVALID_HANDLE; |
518 |
break; |
519 |
} |
520 |
|
521 |
switch (minor) |
522 |
{ |
523 |
case IRP_MN_QUERY_DIRECTORY: |
524 |
|
525 |
in_uint32_le(s, info_level); |
526 |
in_uint8s(s, 1); |
527 |
in_uint32_le(s, length); |
528 |
in_uint8s(s, 0x17); |
529 |
if (length && length < 2 * 255) |
530 |
{ |
531 |
rdp_in_unistr(s, filename, length); |
532 |
convert_to_unix_filename(filename); |
533 |
} |
534 |
else |
535 |
{ |
536 |
filename[0] = 0; |
537 |
} |
538 |
out.data = out.p = buffer; |
539 |
out.size = sizeof(buffer); |
540 |
status = disk_query_directory(file, info_level, filename, |
541 |
&out); |
542 |
result = buffer_len = out.p - out.data; |
543 |
if (!buffer_len) |
544 |
buffer_len++; |
545 |
break; |
546 |
|
547 |
case IRP_MN_NOTIFY_CHANGE_DIRECTORY: |
548 |
|
549 |
/* JIF |
550 |
unimpl("IRP major=0x%x minor=0x%x: IRP_MN_NOTIFY_CHANGE_DIRECTORY\n", major, minor); */ |
551 |
status = STATUS_PENDING; // Don't send completion packet |
552 |
break; |
553 |
|
554 |
default: |
555 |
|
556 |
status = STATUS_INVALID_PARAMETER; |
557 |
/* JIF |
558 |
unimpl("IRP major=0x%x minor=0x%x\n", major, minor); */ |
559 |
} |
560 |
break; |
561 |
|
562 |
case IRP_MJ_DEVICE_CONTROL: |
563 |
|
564 |
if (!fns->device_control) |
565 |
{ |
566 |
status = STATUS_NOT_SUPPORTED; |
567 |
break; |
568 |
} |
569 |
|
570 |
in_uint32_le(s, bytes_out); |
571 |
in_uint32_le(s, bytes_in); |
572 |
in_uint32_le(s, request); |
573 |
in_uint8s(s, 0x14); |
574 |
|
575 |
buffer = (uint8 *) xrealloc((void *) buffer, bytes_out + 0x14); |
576 |
if (!buffer) |
577 |
{ |
578 |
status = STATUS_CANCELLED; |
579 |
break; |
580 |
} |
581 |
|
582 |
out.data = out.p = buffer; |
583 |
out.size = sizeof(buffer); |
584 |
status = fns->device_control(file, request, s, &out); |
585 |
result = buffer_len = out.p - out.data; |
586 |
break; |
587 |
|
588 |
default: |
589 |
unimpl("IRP major=0x%x minor=0x%x\n", major, minor); |
590 |
break; |
591 |
} |
592 |
|
593 |
if (status != STATUS_PENDING) |
594 |
{ |
595 |
rdpdr_send_completion(device, id, status, result, buffer, buffer_len); |
596 |
} |
597 |
if (buffer) |
598 |
xfree(buffer); |
599 |
buffer = NULL; |
600 |
} |
601 |
|
602 |
void |
603 |
rdpdr_send_clientcapabilty(void) |
604 |
{ |
605 |
uint8 magic[4] = "rDPC"; |
606 |
STREAM s; |
607 |
|
608 |
s = channel_init(rdpdr_channel, 0x50); |
609 |
out_uint8a(s, magic, 4); |
610 |
out_uint32_le(s, 5); /* count */ |
611 |
out_uint16_le(s, 1); /* first */ |
612 |
out_uint16_le(s, 0x28); /* length */ |
613 |
out_uint32_le(s, 1); |
614 |
out_uint32_le(s, 2); |
615 |
out_uint16_le(s, 2); |
616 |
out_uint16_le(s, 5); |
617 |
out_uint16_le(s, 1); |
618 |
out_uint16_le(s, 5); |
619 |
out_uint16_le(s, 0xFFFF); |
620 |
out_uint16_le(s, 0); |
621 |
out_uint32_le(s, 0); |
622 |
out_uint32_le(s, 3); |
623 |
out_uint32_le(s, 0); |
624 |
out_uint32_le(s, 0); |
625 |
out_uint16_le(s, 2); /* second */ |
626 |
out_uint16_le(s, 8); /* length */ |
627 |
out_uint32_le(s, 1); |
628 |
out_uint16_le(s, 3); /* third */ |
629 |
out_uint16_le(s, 8); /* length */ |
630 |
out_uint32_le(s, 1); |
631 |
out_uint16_le(s, 4); /* fourth */ |
632 |
out_uint16_le(s, 8); /* length */ |
633 |
out_uint32_le(s, 1); |
634 |
out_uint16_le(s, 5); /* fifth */ |
635 |
out_uint16_le(s, 8); /* length */ |
636 |
out_uint32_le(s, 1); |
637 |
|
638 |
s_mark_end(s); |
639 |
channel_send(s, rdpdr_channel); |
640 |
} |
641 |
|
642 |
static void |
643 |
rdpdr_process(STREAM s) |
644 |
{ |
645 |
uint32 handle; |
646 |
uint8 *magic; |
647 |
|
648 |
#if WITH_DEBUG_RDP5 |
649 |
printf("--- rdpdr_process ---\n"); |
650 |
hexdump(s->p, s->end - s->p); |
651 |
#endif |
652 |
in_uint8p(s, magic, 4); |
653 |
|
654 |
if ((magic[0] == 'r') && (magic[1] == 'D')) |
655 |
{ |
656 |
if ((magic[2] == 'R') && (magic[3] == 'I')) |
657 |
{ |
658 |
rdpdr_process_irp(s); |
659 |
return; |
660 |
} |
661 |
if ((magic[2] == 'n') && (magic[3] == 'I')) |
662 |
{ |
663 |
rdpdr_send_connect(); |
664 |
rdpdr_send_name(); |
665 |
return; |
666 |
} |
667 |
if ((magic[2] == 'C') && (magic[3] == 'C')) |
668 |
{ |
669 |
/* connect from server */ |
670 |
rdpdr_send_clientcapabilty(); |
671 |
rdpdr_send_available(); |
672 |
return; |
673 |
} |
674 |
if ((magic[2] == 'r') && (magic[3] == 'd')) |
675 |
{ |
676 |
/* connect to a specific resource */ |
677 |
in_uint32(s, handle); |
678 |
#if WITH_DEBUG_RDP5 |
679 |
DEBUG(("RDPDR: Server connected to resource %d\n", handle)); |
680 |
#endif |
681 |
return; |
682 |
} |
683 |
if ((magic[2] == 'P') && (magic[3] == 'S')) |
684 |
{ |
685 |
/* server capability */ |
686 |
return; |
687 |
} |
688 |
} |
689 |
if ((magic[0] == 'R') && (magic[1] == 'P')) |
690 |
{ |
691 |
if ((magic[2] == 'C') && (magic[3] == 'P')) |
692 |
{ |
693 |
printercache_process(s); |
694 |
return; |
695 |
} |
696 |
} |
697 |
unimpl("RDPDR packet type %c%c%c%c\n", magic[0], magic[1], magic[2], magic[3]); |
698 |
} |
699 |
|
700 |
BOOL |
701 |
rdpdr_init() |
702 |
{ |
703 |
if (g_num_devices > 0) |
704 |
{ |
705 |
rdpdr_channel = |
706 |
channel_register("rdpdr", |
707 |
CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_COMPRESS_RDP, |
708 |
rdpdr_process); |
709 |
} |
710 |
|
711 |
return (rdpdr_channel != NULL); |
712 |
} |
713 |
|
714 |
/* Add file descriptors of pending io request to select() */ |
715 |
void |
716 |
rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, BOOL * timeout) |
717 |
{ |
718 |
uint32 select_timeout = 0; // Timeout value to be used for select() (in millisecons). |
719 |
struct async_iorequest *iorq; |
720 |
|
721 |
iorq = g_iorequest; |
722 |
while (iorq != NULL) |
723 |
{ |
724 |
if (iorq->fd != 0) |
725 |
{ |
726 |
switch (iorq->major) |
727 |
{ |
728 |
case IRP_MJ_READ: |
729 |
|
730 |
FD_SET(iorq->fd, rfds); |
731 |
|
732 |
// Check if io request timeout is smaller than current (but not 0). |
733 |
if (iorq->timeout |
734 |
&& (select_timeout == 0 |
735 |
|| iorq->timeout < select_timeout)) |
736 |
{ |
737 |
// Set new timeout |
738 |
select_timeout = iorq->timeout; |
739 |
g_min_timeout_fd = iorq->fd; /* Remember fd */ |
740 |
tv->tv_sec = select_timeout / 1000; |
741 |
tv->tv_usec = (select_timeout % 1000) * 1000; |
742 |
*timeout = True; |
743 |
} |
744 |
break; |
745 |
|
746 |
case IRP_MJ_WRITE: |
747 |
FD_SET(iorq->fd, wfds); |
748 |
break; |
749 |
|
750 |
} |
751 |
*n = MAX(*n, iorq->fd); |
752 |
} |
753 |
|
754 |
iorq = iorq->next; |
755 |
} |
756 |
} |
757 |
|
758 |
|
759 |
/* Check if select() returned with one of the rdpdr file descriptors, and complete io if it did */ |
760 |
void |
761 |
rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out) |
762 |
{ |
763 |
NTSTATUS status; |
764 |
uint32 result = 0; |
765 |
DEVICE_FNS *fns; |
766 |
struct async_iorequest *iorq; |
767 |
struct async_iorequest *prev; |
768 |
uint32 req_size = 0; |
769 |
|
770 |
if (timed_out) |
771 |
{ |
772 |
rdpdr_abort_io(g_min_timeout_fd, 0, STATUS_TIMEOUT); |
773 |
return; |
774 |
} |
775 |
|
776 |
iorq = g_iorequest; |
777 |
prev = NULL; |
778 |
while (iorq != NULL) |
779 |
{ |
780 |
if (iorq->fd != 0) |
781 |
{ |
782 |
switch (iorq->major) |
783 |
{ |
784 |
case IRP_MJ_READ: |
785 |
if (FD_ISSET(iorq->fd, rfds)) |
786 |
{ |
787 |
/* Read the data */ |
788 |
fns = iorq->fns; |
789 |
|
790 |
req_size = |
791 |
(iorq->length - iorq->partial_len) > |
792 |
8192 ? 8192 : (iorq->length - |
793 |
iorq->partial_len); |
794 |
/* never read larger chunks than 8k - chances are that it will block */ |
795 |
status = fns->read(iorq->fd, |
796 |
iorq->buffer + iorq->partial_len, |
797 |
req_size, 0, &result); |
798 |
iorq->partial_len += result; |
799 |
|
800 |
#if WITH_DEBUG_RDP5 |
801 |
DEBUG(("RDPDR: %d bytes of data read\n", result)); |
802 |
#endif |
803 |
/* only delete link if all data has been transfered */ |
804 |
/* or if result was 0 and status success - EOF */ |
805 |
if ((iorq->partial_len == iorq->length) || |
806 |
(result == 0)) |
807 |
{ |
808 |
#if WITH_DEBUG_RDP5 |
809 |
DEBUG(("RDPDR: AIO total %u bytes read of %u\n", iorq->partial_len, iorq->length)); |
810 |
#endif |
811 |
/* send the data */ |
812 |
status = STATUS_SUCCESS; |
813 |
rdpdr_send_completion(iorq->device, |
814 |
iorq->id, status, |
815 |
iorq->partial_len, |
816 |
iorq->buffer, |
817 |
iorq->partial_len); |
818 |
xfree(iorq->buffer); |
819 |
iorq->fd = 0; |
820 |
if (prev != NULL) |
821 |
{ |
822 |
prev->next = iorq->next; |
823 |
xfree(iorq); |
824 |
} |
825 |
else |
826 |
{ |
827 |
// Even if NULL |
828 |
g_iorequest = iorq->next; |
829 |
xfree(iorq); |
830 |
} |
831 |
} |
832 |
} |
833 |
break; |
834 |
case IRP_MJ_WRITE: |
835 |
if (FD_ISSET(iorq->fd, wfds)) |
836 |
{ |
837 |
/* Write data. */ |
838 |
fns = iorq->fns; |
839 |
|
840 |
req_size = |
841 |
(iorq->length - iorq->partial_len) > |
842 |
8192 ? 8192 : (iorq->length - |
843 |
iorq->partial_len); |
844 |
|
845 |
/* never write larger chunks than 8k - chances are that it will block */ |
846 |
status = fns->write(iorq->fd, |
847 |
iorq->buffer + |
848 |
iorq->partial_len, req_size, 0, |
849 |
&result); |
850 |
iorq->partial_len += result; |
851 |
#if WITH_DEBUG_RDP5 |
852 |
DEBUG(("RDPDR: %d bytes of data written\n", |
853 |
result)); |
854 |
#endif |
855 |
/* only delete link if all data has been transfered */ |
856 |
/* or we couldn't write */ |
857 |
if ((iorq->partial_len == iorq->length) |
858 |
|| (result == 0)) |
859 |
{ |
860 |
#if WITH_DEBUG_RDP5 |
861 |
DEBUG(("RDPDR: AIO total %u bytes written of %u\n", iorq->partial_len, iorq->length)); |
862 |
#endif |
863 |
/* send a status success */ |
864 |
status = STATUS_SUCCESS; |
865 |
rdpdr_send_completion(iorq->device, |
866 |
iorq->id, status, |
867 |
iorq->partial_len, |
868 |
(uint8 *) "", 1); |
869 |
|
870 |
xfree(iorq->buffer); |
871 |
iorq->fd = 0; |
872 |
if (prev != NULL) |
873 |
{ |
874 |
prev->next = iorq->next; |
875 |
xfree(iorq); |
876 |
} |
877 |
else |
878 |
{ |
879 |
// Even if NULL |
880 |
g_iorequest = iorq->next; |
881 |
xfree(iorq); |
882 |
} |
883 |
} |
884 |
} |
885 |
break; |
886 |
} |
887 |
|
888 |
} |
889 |
prev = iorq; |
890 |
iorq = iorq->next; |
891 |
} |
892 |
|
893 |
} |
894 |
|
895 |
/* Abort a pending io request for a given handle and major */ |
896 |
BOOL |
897 |
rdpdr_abort_io(uint32 fd, uint32 major, NTSTATUS status) |
898 |
{ |
899 |
uint32 result; |
900 |
struct async_iorequest *iorq; |
901 |
struct async_iorequest *prev; |
902 |
|
903 |
iorq = g_iorequest; |
904 |
prev = NULL; |
905 |
while (iorq != NULL) |
906 |
{ |
907 |
// Only remove from table when major is not set, or when correct major is supplied. |
908 |
// Abort read should not abort a write io request. |
909 |
if ((iorq->fd == fd) && (major == 0 || iorq->major == major)) |
910 |
{ |
911 |
result = 0; |
912 |
rdpdr_send_completion(iorq->device, iorq->id, status, result, (uint8 *) "", |
913 |
1); |
914 |
xfree(iorq->buffer); |
915 |
iorq->fd = 0; |
916 |
if (prev != NULL) |
917 |
{ |
918 |
prev->next = iorq->next; |
919 |
xfree(iorq); |
920 |
} |
921 |
else |
922 |
{ |
923 |
// Even if NULL |
924 |
g_iorequest = iorq->next; |
925 |
xfree(iorq); |
926 |
} |
927 |
return True; |
928 |
} |
929 |
|
930 |
prev = iorq; |
931 |
iorq = iorq->next; |
932 |
} |
933 |
|
934 |
return False; |
935 |
} |