FreeRDP
Loading...
Searching...
No Matches
irp.c
1
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
43static UINT irp_free(IRP* irp)
44{
45 if (!irp)
46 return CHANNEL_RC_OK;
47
48 if (irp->input)
49 Stream_Release(irp->input);
50 if (irp->output)
51 Stream_Release(irp->output);
52
53 winpr_aligned_free(irp);
54 return CHANNEL_RC_OK;
55}
56
62static UINT irp_complete(IRP* irp)
63{
64 WINPR_ASSERT(irp);
65 WINPR_ASSERT(irp->output);
66 WINPR_ASSERT(irp->devman);
67
68 rdpdrPlugin* rdpdr = (rdpdrPlugin*)irp->devman->plugin;
69 WINPR_ASSERT(rdpdr);
70
71 if (rdpdr->clearing)
72 return irp->Discard(irp);
73
74 UINT error = ERROR_INVALID_DATA;
75
76 const size_t pos = Stream_GetPosition(irp->output);
77 if (!Stream_SetPosition(irp->output, RDPDR_DEVICE_IO_RESPONSE_LENGTH - 4))
78 goto fail;
79 Stream_Write_INT32(irp->output, irp->IoStatus); /* IoStatus (4 bytes) */
80 if (!Stream_SetPosition(irp->output, pos))
81 goto fail;
82
83 error = rdpdr_send(rdpdr, irp->output);
84 irp->output = nullptr;
85fail:
86 irp_free(irp);
87 return error;
88}
89
90IRP* irp_new(DEVMAN* devman, wStreamPool* pool, wStream* s, wLog* log, UINT* error)
91{
92 IRP* irp = nullptr;
93 DEVICE* device = nullptr;
94 UINT32 DeviceId = 0;
95
96 WINPR_ASSERT(devman);
97 WINPR_ASSERT(pool);
98 WINPR_ASSERT(s);
99
100 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 20))
101 {
102 if (error)
103 *error = ERROR_INVALID_DATA;
104 return nullptr;
105 }
106
107 Stream_Read_UINT32(s, DeviceId); /* DeviceId (4 bytes) */
108 device = devman_get_device_by_id(devman, DeviceId);
109
110 if (!device)
111 {
112 if (error)
113 *error = ERROR_DEV_NOT_EXIST;
114
115 return nullptr;
116 }
117
118 irp = (IRP*)winpr_aligned_calloc(1, sizeof(IRP), MEMORY_ALLOCATION_ALIGNMENT);
119
120 if (!irp)
121 {
122 WLog_Print(log, WLOG_ERROR, "_aligned_malloc failed!");
123 if (error)
124 *error = CHANNEL_RC_NO_MEMORY;
125 return nullptr;
126 }
127
128 Stream_Read_UINT32(s, irp->FileId); /* FileId (4 bytes) */
129 Stream_Read_UINT32(s, irp->CompletionId); /* CompletionId (4 bytes) */
130 Stream_Read_UINT32(s, irp->MajorFunction); /* MajorFunction (4 bytes) */
131 Stream_Read_UINT32(s, irp->MinorFunction); /* MinorFunction (4 bytes) */
132
133 Stream_AddRef(s);
134 irp->input = s;
135 irp->device = device;
136 irp->devman = devman;
137
138 irp->output = StreamPool_Take(pool, 256);
139 if (!irp->output)
140 {
141 WLog_Print(log, WLOG_ERROR, "Stream_New failed!");
142 irp_free(irp);
143 if (error)
144 *error = CHANNEL_RC_NO_MEMORY;
145 return nullptr;
146 }
147
148 if (!rdpdr_write_iocompletion_header(irp->output, DeviceId, irp->CompletionId, 0))
149 {
150 irp_free(irp);
151 if (error)
152 *error = CHANNEL_RC_NO_MEMORY;
153 return nullptr;
154 }
155
156 irp->Complete = irp_complete;
157 irp->Discard = irp_free;
158
159 irp->thread = nullptr;
160 irp->cancelled = FALSE;
161
162 if (error)
163 *error = CHANNEL_RC_OK;
164
165 return irp;
166}