1 /*
2 * PROJECT: Filesystem Filter Manager library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/fltlib/message.c
5 * PURPOSE: Handles messaging to and from the filter manager
6 * PROGRAMMERS: Ged Murphy (ged.murphy@reactos.org)
7 */
8
9 #define WIN32_NO_STATUS
10 #include <windef.h>
11 #include <winbase.h>
12
13 #define NTOS_MODE_USER
14 #include <ndk/iofuncs.h>
15 #include <ndk/obfuncs.h>
16 #include <ndk/rtlfuncs.h>
17 #include <fltuser.h>
18 #include <fltmgr_shared.h>
19
20 #include "fltlib.h"
21
22 _Must_inspect_result_
23 HRESULT
24 WINAPI
FilterConnectCommunicationPort(_In_ LPCWSTR lpPortName,_In_ DWORD dwOptions,_In_reads_bytes_opt_ (wSizeOfContext)LPCVOID lpContext,_In_ WORD wSizeOfContext,_In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,_Outptr_ HANDLE * hPort)25 FilterConnectCommunicationPort(_In_ LPCWSTR lpPortName,
26 _In_ DWORD dwOptions,
27 _In_reads_bytes_opt_(wSizeOfContext) LPCVOID lpContext,
28 _In_ WORD wSizeOfContext,
29 _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
30 _Outptr_ HANDLE *hPort)
31 {
32 OBJECT_ATTRIBUTES ObjectAttributes;
33 IO_STATUS_BLOCK IoStatusBlock;
34 PFILTER_PORT_DATA PortData;
35 UNICODE_STRING DeviceName;
36 UNICODE_STRING PortName;
37 HANDLE FileHandle;
38 SIZE_T PortNameSize;
39 SIZE_T BufferSize;
40 PCHAR Ptr;
41 NTSTATUS Status;
42 HRESULT hr;
43
44 *hPort = INVALID_HANDLE_VALUE;
45
46 /* Sanity check */
47 if (lpContext && wSizeOfContext == 0)
48 {
49 return E_INVALIDARG;
50 }
51
52 /* Get the length of the port name */
53 PortNameSize = wcslen(lpPortName) * sizeof(WCHAR);
54
55 /* Calculate and allocate the size of the required buffer */
56 BufferSize = sizeof(FILTER_PORT_DATA) + PortNameSize + wSizeOfContext;
57 PortData = RtlAllocateHeap(GetProcessHeap(), 0, BufferSize);
58 if (PortData == NULL) return E_OUTOFMEMORY;
59
60 /* Clear out the buffer and find the end of the fixed struct */
61 RtlZeroMemory(PortData, BufferSize);
62 Ptr = (PCHAR)(PortData + 1);
63
64 PortData->Size = BufferSize;
65 PortData->Options = dwOptions;
66
67 /* Setup the port name */
68 RtlInitUnicodeString(&PortName, lpPortName);
69 PortData->PortName.Buffer = (PWCH)Ptr;
70 PortData->PortName.MaximumLength = PortNameSize;
71 RtlCopyUnicodeString(&PortData->PortName, &PortName);
72 Ptr += PortData->PortName.Length;
73
74 /* Check if we were given a context */
75 if (lpContext)
76 {
77 /* Add that into the buffer too */
78 PortData->Context = Ptr;
79 RtlCopyMemory(PortData->Context, lpContext, wSizeOfContext);
80 }
81
82 /* Initialize the object attributes */
83 RtlInitUnicodeString(&DeviceName, L"\\Global??\\FltMgrMsg");
84 InitializeObjectAttributes(&ObjectAttributes,
85 &DeviceName,
86 OBJ_EXCLUSIVE | OBJ_CASE_INSENSITIVE,
87 NULL,
88 NULL);
89
90 /* Check if we were passed any security attributes */
91 if (lpSecurityAttributes)
92 {
93 /* Add these manually and update the flags if we were asked to make it inheritable */
94 ObjectAttributes.SecurityDescriptor = lpSecurityAttributes->lpSecurityDescriptor;
95 if (lpSecurityAttributes->bInheritHandle)
96 {
97 ObjectAttributes.Attributes |= OBJ_INHERIT;
98 }
99 }
100
101 /* Now get a handle to the device */
102 Status = NtCreateFile(&FileHandle,
103 SYNCHRONIZE | FILE_READ_DATA | FILE_WRITE_DATA,
104 &ObjectAttributes,
105 &IoStatusBlock,
106 0,
107 0,
108 0,
109 FILE_OPEN_IF,
110 0,
111 PortData,
112 BufferSize);
113 if (NT_SUCCESS(Status))
114 {
115 *hPort = FileHandle;
116 hr = S_OK;
117 }
118 else
119 {
120 hr = NtStatusToHResult(Status);
121 }
122
123 /* Cleanup and return */
124 RtlFreeHeap(GetProcessHeap(), 0, PortData);
125 return hr;
126 }
127
128 _Must_inspect_result_
129 HRESULT
130 WINAPI
FilterSendMessage(_In_ HANDLE hPort,_In_reads_bytes_ (dwInBufferSize)LPVOID lpInBuffer,_In_ DWORD dwInBufferSize,_Out_writes_bytes_to_opt_ (dwOutBufferSize,* lpBytesReturned)LPVOID lpOutBuffer,_In_ DWORD dwOutBufferSize,_Out_ LPDWORD lpBytesReturned)131 FilterSendMessage(_In_ HANDLE hPort,
132 _In_reads_bytes_(dwInBufferSize) LPVOID lpInBuffer,
133 _In_ DWORD dwInBufferSize,
134 _Out_writes_bytes_to_opt_(dwOutBufferSize, *lpBytesReturned) LPVOID lpOutBuffer,
135 _In_ DWORD dwOutBufferSize,
136 _Out_ LPDWORD lpBytesReturned)
137 {
138 UNREFERENCED_PARAMETER(hPort);
139 UNREFERENCED_PARAMETER(lpInBuffer);
140 UNREFERENCED_PARAMETER(dwInBufferSize);
141 UNREFERENCED_PARAMETER(lpOutBuffer);
142 UNREFERENCED_PARAMETER(dwOutBufferSize);
143 UNREFERENCED_PARAMETER(lpBytesReturned);
144 return E_NOTIMPL;
145 }
146
147 _Must_inspect_result_
148 HRESULT
149 WINAPI
FilterGetMessage(_In_ HANDLE hPort,_Out_writes_bytes_ (dwMessageBufferSize)PFILTER_MESSAGE_HEADER lpMessageBuffer,_In_ DWORD dwMessageBufferSize,_Inout_opt_ LPOVERLAPPED lpOverlapped)150 FilterGetMessage(_In_ HANDLE hPort,
151 _Out_writes_bytes_(dwMessageBufferSize) PFILTER_MESSAGE_HEADER lpMessageBuffer,
152 _In_ DWORD dwMessageBufferSize,
153 _Inout_opt_ LPOVERLAPPED lpOverlapped)
154 {
155 UNREFERENCED_PARAMETER(hPort);
156 UNREFERENCED_PARAMETER(lpMessageBuffer);
157 UNREFERENCED_PARAMETER(dwMessageBufferSize);
158 UNREFERENCED_PARAMETER(lpOverlapped);
159 return E_NOTIMPL;
160 }
161
162 _Must_inspect_result_
163 HRESULT
164 WINAPI
FilterReplyMessage(_In_ HANDLE hPort,_In_reads_bytes_ (dwReplyBufferSize)PFILTER_REPLY_HEADER lpReplyBuffer,_In_ DWORD dwReplyBufferSize)165 FilterReplyMessage(_In_ HANDLE hPort,
166 _In_reads_bytes_(dwReplyBufferSize) PFILTER_REPLY_HEADER lpReplyBuffer,
167 _In_ DWORD dwReplyBufferSize)
168 {
169 UNREFERENCED_PARAMETER(hPort);
170 UNREFERENCED_PARAMETER(lpReplyBuffer);
171 UNREFERENCED_PARAMETER(dwReplyBufferSize);
172 return E_NOTIMPL;
173 }
174