/src/FreeRDP/channels/rdpdr/client/irp.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Device Redirection Virtual Channel |
4 | | * |
5 | | * Copyright 2010-2011 Vic Lee |
6 | | * Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
7 | | * Copyright 2015 Thincast Technologies GmbH |
8 | | * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> |
9 | | * |
10 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
11 | | * you may not use this file except in compliance with the License. |
12 | | * You may obtain a copy of the License at |
13 | | * |
14 | | * http://www.apache.org/licenses/LICENSE-2.0 |
15 | | * |
16 | | * Unless required by applicable law or agreed to in writing, software |
17 | | * distributed under the License is distributed on an "AS IS" BASIS, |
18 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
19 | | * See the License for the specific language governing permissions and |
20 | | * limitations under the License. |
21 | | */ |
22 | | |
23 | | #include <freerdp/config.h> |
24 | | |
25 | | #include <stdio.h> |
26 | | #include <stdlib.h> |
27 | | #include <string.h> |
28 | | |
29 | | #include <winpr/crt.h> |
30 | | #include <winpr/stream.h> |
31 | | |
32 | | #include <freerdp/utils/rdpdr_utils.h> |
33 | | |
34 | | #include "rdpdr_main.h" |
35 | | #include "devman.h" |
36 | | #include "irp.h" |
37 | | |
38 | | /** |
39 | | * Function description |
40 | | * |
41 | | * @return 0 on success, otherwise a Win32 error code |
42 | | */ |
43 | | static UINT irp_free(IRP* irp) |
44 | 0 | { |
45 | 0 | if (!irp) |
46 | 0 | return CHANNEL_RC_OK; |
47 | | |
48 | 0 | if (irp->input) |
49 | 0 | Stream_Release(irp->input); |
50 | 0 | if (irp->output) |
51 | 0 | Stream_Release(irp->output); |
52 | |
|
53 | 0 | winpr_aligned_free(irp); |
54 | 0 | return CHANNEL_RC_OK; |
55 | 0 | } |
56 | | |
57 | | /** |
58 | | * Function description |
59 | | * |
60 | | * @return 0 on success, otherwise a Win32 error code |
61 | | */ |
62 | | static UINT irp_complete(IRP* irp) |
63 | 0 | { |
64 | 0 | WINPR_ASSERT(irp); |
65 | 0 | WINPR_ASSERT(irp->output); |
66 | 0 | WINPR_ASSERT(irp->devman); |
67 | |
|
68 | 0 | rdpdrPlugin* rdpdr = (rdpdrPlugin*)irp->devman->plugin; |
69 | 0 | WINPR_ASSERT(rdpdr); |
70 | |
|
71 | 0 | if (rdpdr->clearing) |
72 | 0 | return irp->Discard(irp); |
73 | | |
74 | 0 | UINT error = ERROR_INVALID_DATA; |
75 | |
|
76 | 0 | const size_t pos = Stream_GetPosition(irp->output); |
77 | 0 | if (!Stream_SetPosition(irp->output, RDPDR_DEVICE_IO_RESPONSE_LENGTH - 4)) |
78 | 0 | goto fail; |
79 | 0 | Stream_Write_INT32(irp->output, irp->IoStatus); /* IoStatus (4 bytes) */ |
80 | 0 | if (!Stream_SetPosition(irp->output, pos)) |
81 | 0 | goto fail; |
82 | | |
83 | 0 | error = rdpdr_send(rdpdr, irp->output); |
84 | 0 | irp->output = nullptr; |
85 | 0 | fail: |
86 | 0 | irp_free(irp); |
87 | 0 | return error; |
88 | 0 | } |
89 | | |
90 | | IRP* irp_new(DEVMAN* devman, wStreamPool* pool, wStream* s, wLog* log, UINT* error) |
91 | 0 | { |
92 | 0 | IRP* irp = nullptr; |
93 | 0 | DEVICE* device = nullptr; |
94 | 0 | UINT32 DeviceId = 0; |
95 | |
|
96 | 0 | WINPR_ASSERT(devman); |
97 | 0 | WINPR_ASSERT(pool); |
98 | 0 | WINPR_ASSERT(s); |
99 | |
|
100 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 20)) |
101 | 0 | { |
102 | 0 | if (error) |
103 | 0 | *error = ERROR_INVALID_DATA; |
104 | 0 | return nullptr; |
105 | 0 | } |
106 | | |
107 | 0 | Stream_Read_UINT32(s, DeviceId); /* DeviceId (4 bytes) */ |
108 | 0 | device = devman_get_device_by_id(devman, DeviceId); |
109 | |
|
110 | 0 | if (!device) |
111 | 0 | { |
112 | 0 | if (error) |
113 | 0 | *error = ERROR_DEV_NOT_EXIST; |
114 | |
|
115 | 0 | return nullptr; |
116 | 0 | } |
117 | | |
118 | 0 | irp = (IRP*)winpr_aligned_calloc(1, sizeof(IRP), MEMORY_ALLOCATION_ALIGNMENT); |
119 | |
|
120 | 0 | if (!irp) |
121 | 0 | { |
122 | 0 | WLog_Print(log, WLOG_ERROR, "_aligned_malloc failed!"); |
123 | 0 | if (error) |
124 | 0 | *error = CHANNEL_RC_NO_MEMORY; |
125 | 0 | return nullptr; |
126 | 0 | } |
127 | | |
128 | 0 | Stream_Read_UINT32(s, irp->FileId); /* FileId (4 bytes) */ |
129 | 0 | Stream_Read_UINT32(s, irp->CompletionId); /* CompletionId (4 bytes) */ |
130 | 0 | Stream_Read_UINT32(s, irp->MajorFunction); /* MajorFunction (4 bytes) */ |
131 | 0 | Stream_Read_UINT32(s, irp->MinorFunction); /* MinorFunction (4 bytes) */ |
132 | |
|
133 | 0 | Stream_AddRef(s); |
134 | 0 | irp->input = s; |
135 | 0 | irp->device = device; |
136 | 0 | irp->devman = devman; |
137 | |
|
138 | 0 | irp->output = StreamPool_Take(pool, 256); |
139 | 0 | if (!irp->output) |
140 | 0 | { |
141 | 0 | WLog_Print(log, WLOG_ERROR, "Stream_New failed!"); |
142 | 0 | irp_free(irp); |
143 | 0 | if (error) |
144 | 0 | *error = CHANNEL_RC_NO_MEMORY; |
145 | 0 | return nullptr; |
146 | 0 | } |
147 | | |
148 | 0 | if (!rdpdr_write_iocompletion_header(irp->output, DeviceId, irp->CompletionId, 0)) |
149 | 0 | { |
150 | 0 | irp_free(irp); |
151 | 0 | if (error) |
152 | 0 | *error = CHANNEL_RC_NO_MEMORY; |
153 | 0 | return nullptr; |
154 | 0 | } |
155 | | |
156 | 0 | irp->Complete = irp_complete; |
157 | 0 | irp->Discard = irp_free; |
158 | |
|
159 | 0 | irp->thread = nullptr; |
160 | 0 | irp->cancelled = FALSE; |
161 | |
|
162 | 0 | if (error) |
163 | 0 | *error = CHANNEL_RC_OK; |
164 | |
|
165 | 0 | return irp; |
166 | 0 | } |