/src/FreeRDP/channels/rdpdr/client/irp.c
Line  | Count  | Source (jump to first uncovered line)  | 
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  |   size_t pos = 0;  | 
65  | 0  |   rdpdrPlugin* rdpdr = NULL;  | 
66  | 0  |   UINT error = 0;  | 
67  |  | 
  | 
68  | 0  |   WINPR_ASSERT(irp);  | 
69  | 0  |   WINPR_ASSERT(irp->output);  | 
70  | 0  |   WINPR_ASSERT(irp->devman);  | 
71  |  |  | 
72  | 0  |   rdpdr = (rdpdrPlugin*)irp->devman->plugin;  | 
73  | 0  |   WINPR_ASSERT(rdpdr);  | 
74  |  |  | 
75  | 0  |   pos = Stream_GetPosition(irp->output);  | 
76  | 0  |   Stream_SetPosition(irp->output, RDPDR_DEVICE_IO_RESPONSE_LENGTH - 4);  | 
77  | 0  |   Stream_Write_UINT32(irp->output, irp->IoStatus); /* IoStatus (4 bytes) */  | 
78  | 0  |   Stream_SetPosition(irp->output, pos);  | 
79  |  | 
  | 
80  | 0  |   error = rdpdr_send(rdpdr, irp->output);  | 
81  | 0  |   irp->output = NULL;  | 
82  |  | 
  | 
83  | 0  |   irp_free(irp);  | 
84  | 0  |   return error;  | 
85  | 0  | }  | 
86  |  |  | 
87  |  | IRP* irp_new(DEVMAN* devman, wStreamPool* pool, wStream* s, wLog* log, UINT* error)  | 
88  | 0  | { | 
89  | 0  |   IRP* irp = NULL;  | 
90  | 0  |   DEVICE* device = NULL;  | 
91  | 0  |   UINT32 DeviceId = 0;  | 
92  |  | 
  | 
93  | 0  |   WINPR_ASSERT(devman);  | 
94  | 0  |   WINPR_ASSERT(pool);  | 
95  | 0  |   WINPR_ASSERT(s);  | 
96  |  |  | 
97  | 0  |   if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 20))  | 
98  | 0  |   { | 
99  | 0  |     if (error)  | 
100  | 0  |       *error = ERROR_INVALID_DATA;  | 
101  | 0  |     return NULL;  | 
102  | 0  |   }  | 
103  |  |  | 
104  | 0  |   Stream_Read_UINT32(s, DeviceId); /* DeviceId (4 bytes) */  | 
105  | 0  |   device = devman_get_device_by_id(devman, DeviceId);  | 
106  |  | 
  | 
107  | 0  |   if (!device)  | 
108  | 0  |   { | 
109  | 0  |     if (error)  | 
110  | 0  |       *error = ERROR_DEV_NOT_EXIST;  | 
111  |  | 
  | 
112  | 0  |     return NULL;  | 
113  | 0  |   }  | 
114  |  |  | 
115  | 0  |   irp = (IRP*)winpr_aligned_malloc(sizeof(IRP), MEMORY_ALLOCATION_ALIGNMENT);  | 
116  |  | 
  | 
117  | 0  |   if (!irp)  | 
118  | 0  |   { | 
119  | 0  |     WLog_Print(log, WLOG_ERROR, "_aligned_malloc failed!");  | 
120  | 0  |     if (error)  | 
121  | 0  |       *error = CHANNEL_RC_NO_MEMORY;  | 
122  | 0  |     return NULL;  | 
123  | 0  |   }  | 
124  |  |  | 
125  | 0  |   ZeroMemory(irp, sizeof(IRP));  | 
126  |  | 
  | 
127  | 0  |   Stream_Read_UINT32(s, irp->FileId);        /* FileId (4 bytes) */  | 
128  | 0  |   Stream_Read_UINT32(s, irp->CompletionId);  /* CompletionId (4 bytes) */  | 
129  | 0  |   Stream_Read_UINT32(s, irp->MajorFunction); /* MajorFunction (4 bytes) */  | 
130  | 0  |   Stream_Read_UINT32(s, irp->MinorFunction); /* MinorFunction (4 bytes) */  | 
131  |  | 
  | 
132  | 0  |   Stream_AddRef(s);  | 
133  | 0  |   irp->input = s;  | 
134  | 0  |   irp->device = device;  | 
135  | 0  |   irp->devman = devman;  | 
136  |  | 
  | 
137  | 0  |   irp->output = StreamPool_Take(pool, 256);  | 
138  | 0  |   if (!irp->output)  | 
139  | 0  |   { | 
140  | 0  |     WLog_Print(log, WLOG_ERROR, "Stream_New failed!");  | 
141  | 0  |     irp_free(irp);  | 
142  | 0  |     if (error)  | 
143  | 0  |       *error = CHANNEL_RC_NO_MEMORY;  | 
144  | 0  |     return NULL;  | 
145  | 0  |   }  | 
146  |  |  | 
147  | 0  |   if (!rdpdr_write_iocompletion_header(irp->output, DeviceId, irp->CompletionId, 0))  | 
148  | 0  |   { | 
149  | 0  |     irp_free(irp);  | 
150  | 0  |     if (error)  | 
151  | 0  |       *error = CHANNEL_RC_NO_MEMORY;  | 
152  | 0  |     return NULL;  | 
153  | 0  |   }  | 
154  |  |  | 
155  | 0  |   irp->Complete = irp_complete;  | 
156  | 0  |   irp->Discard = irp_free;  | 
157  |  | 
  | 
158  | 0  |   irp->thread = NULL;  | 
159  | 0  |   irp->cancelled = FALSE;  | 
160  |  | 
  | 
161  | 0  |   if (error)  | 
162  | 0  |     *error = CHANNEL_RC_OK;  | 
163  |  | 
  | 
164  | 0  |   return irp;  | 
165  | 0  | }  |