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