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