[RPCRT4]
[reactos.git] / reactos / dll / win32 / rpcrt4 / ndr_contexthandle.c
1 /*
2 * NDR data marshalling
3 *
4 * Copyright 2006 Mike McCormack (for CodeWeavers)
5 * Copyright 2006-2007 Robert Shearman (for CodeWeavers)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "precomp.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(ole);
25
26 #define NDR_CONTEXT_HANDLE_MAGIC 0x4352444e
27
28 typedef struct ndr_context_handle
29 {
30 ULONG attributes;
31 GUID uuid;
32 } ndr_context_handle;
33
34 struct context_handle_entry
35 {
36 struct list entry;
37 DWORD magic;
38 RPC_BINDING_HANDLE handle;
39 ndr_context_handle wire_data;
40 };
41
42 static struct list context_handle_list = LIST_INIT(context_handle_list);
43
44 static CRITICAL_SECTION ndr_context_cs;
45 static CRITICAL_SECTION_DEBUG ndr_context_debug =
46 {
47 0, 0, &ndr_context_cs,
48 { &ndr_context_debug.ProcessLocksList, &ndr_context_debug.ProcessLocksList },
49 0, 0, { (DWORD_PTR)(__FILE__ ": ndr_context") }
50 };
51 static CRITICAL_SECTION ndr_context_cs = { &ndr_context_debug, -1, 0, 0, 0, 0 };
52
53 static struct context_handle_entry *get_context_entry(NDR_CCONTEXT CContext)
54 {
55 struct context_handle_entry *che = CContext;
56
57 if (che->magic != NDR_CONTEXT_HANDLE_MAGIC)
58 return NULL;
59 return che;
60 }
61
62 static struct context_handle_entry *context_entry_from_guid(LPCGUID uuid)
63 {
64 struct context_handle_entry *che;
65 LIST_FOR_EACH_ENTRY(che, &context_handle_list, struct context_handle_entry, entry)
66 if (IsEqualGUID(&che->wire_data.uuid, uuid))
67 return che;
68 return NULL;
69 }
70
71 RPC_BINDING_HANDLE WINAPI NDRCContextBinding(NDR_CCONTEXT CContext)
72 {
73 struct context_handle_entry *che;
74 RPC_BINDING_HANDLE handle = NULL;
75
76 TRACE("%p\n", CContext);
77
78 EnterCriticalSection(&ndr_context_cs);
79 che = get_context_entry(CContext);
80 if (che)
81 handle = che->handle;
82 LeaveCriticalSection(&ndr_context_cs);
83
84 if (!handle)
85 {
86 ERR("invalid handle %p\n", CContext);
87 RpcRaiseException(ERROR_INVALID_HANDLE);
88 }
89 return handle;
90 }
91
92 void WINAPI NDRCContextMarshall(NDR_CCONTEXT CContext, void *pBuff)
93 {
94 struct context_handle_entry *che;
95
96 TRACE("%p %p\n", CContext, pBuff);
97
98 if (CContext)
99 {
100 EnterCriticalSection(&ndr_context_cs);
101 che = get_context_entry(CContext);
102 memcpy(pBuff, &che->wire_data, sizeof (ndr_context_handle));
103 LeaveCriticalSection(&ndr_context_cs);
104 }
105 else
106 {
107 ndr_context_handle *wire_data = pBuff;
108 wire_data->attributes = 0;
109 wire_data->uuid = GUID_NULL;
110 }
111 }
112
113 /***********************************************************************
114 * RpcSmDestroyClientContext [RPCRT4.@]
115 */
116 RPC_STATUS WINAPI RpcSmDestroyClientContext(void **ContextHandle)
117 {
118 RPC_STATUS status = RPC_X_SS_CONTEXT_MISMATCH;
119 struct context_handle_entry *che = NULL;
120
121 TRACE("(%p)\n", ContextHandle);
122
123 EnterCriticalSection(&ndr_context_cs);
124 che = get_context_entry(*ContextHandle);
125 *ContextHandle = NULL;
126 if (che)
127 {
128 status = RPC_S_OK;
129 list_remove(&che->entry);
130 }
131
132 LeaveCriticalSection(&ndr_context_cs);
133
134 if (che)
135 {
136 RpcBindingFree(&che->handle);
137 HeapFree(GetProcessHeap(), 0, che);
138 }
139
140 return status;
141 }
142
143 /***********************************************************************
144 * RpcSsDestroyClientContext [RPCRT4.@]
145 */
146 void WINAPI RpcSsDestroyClientContext(void **ContextHandle)
147 {
148 RPC_STATUS status = RpcSmDestroyClientContext(ContextHandle);
149 if (status != RPC_S_OK)
150 RpcRaiseException(status);
151 }
152
153 static UINT ndr_update_context_handle(NDR_CCONTEXT *CContext,
154 RPC_BINDING_HANDLE hBinding,
155 const ndr_context_handle *chi)
156 {
157 struct context_handle_entry *che = NULL;
158
159 /* a null UUID means we should free the context handle */
160 if (IsEqualGUID(&chi->uuid, &GUID_NULL))
161 {
162 if (*CContext)
163 {
164 che = get_context_entry(*CContext);
165 if (!che)
166 return ERROR_INVALID_HANDLE;
167 list_remove(&che->entry);
168 RpcBindingFree(&che->handle);
169 HeapFree(GetProcessHeap(), 0, che);
170 che = NULL;
171 }
172 }
173 /* if there's no existing entry matching the GUID, allocate one */
174 else if (!(che = context_entry_from_guid(&chi->uuid)))
175 {
176 che = HeapAlloc(GetProcessHeap(), 0, sizeof *che);
177 if (!che)
178 return ERROR_NOT_ENOUGH_MEMORY;
179 che->magic = NDR_CONTEXT_HANDLE_MAGIC;
180 RpcBindingCopy(hBinding, &che->handle);
181 list_add_tail(&context_handle_list, &che->entry);
182 che->wire_data = *chi;
183 }
184
185 *CContext = che;
186
187 return ERROR_SUCCESS;
188 }
189
190 /***********************************************************************
191 * NDRCContextUnmarshall [RPCRT4.@]
192 */
193 void WINAPI NDRCContextUnmarshall(NDR_CCONTEXT *CContext,
194 RPC_BINDING_HANDLE hBinding,
195 void *pBuff, ULONG DataRepresentation)
196 {
197 UINT r;
198
199 TRACE("*%p=(%p) %p %p %08x\n",
200 CContext, *CContext, hBinding, pBuff, DataRepresentation);
201
202 EnterCriticalSection(&ndr_context_cs);
203 r = ndr_update_context_handle(CContext, hBinding, pBuff);
204 LeaveCriticalSection(&ndr_context_cs);
205 if (r)
206 RpcRaiseException(r);
207 }
208
209 /***********************************************************************
210 * NDRSContextMarshall [RPCRT4.@]
211 */
212 void WINAPI NDRSContextMarshall(NDR_SCONTEXT SContext,
213 void *pBuff,
214 NDR_RUNDOWN userRunDownIn)
215 {
216 TRACE("(%p %p %p)\n", SContext, pBuff, userRunDownIn);
217 NDRSContextMarshall2(I_RpcGetCurrentCallHandle(), SContext, pBuff,
218 userRunDownIn, NULL, RPC_CONTEXT_HANDLE_DEFAULT_FLAGS);
219 }
220
221 /***********************************************************************
222 * NDRSContextMarshallEx [RPCRT4.@]
223 */
224 void WINAPI NDRSContextMarshallEx(RPC_BINDING_HANDLE hBinding,
225 NDR_SCONTEXT SContext,
226 void *pBuff,
227 NDR_RUNDOWN userRunDownIn)
228 {
229 TRACE("(%p %p %p %p)\n", hBinding, SContext, pBuff, userRunDownIn);
230 NDRSContextMarshall2(hBinding, SContext, pBuff, userRunDownIn, NULL,
231 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS);
232 }
233
234 /***********************************************************************
235 * NDRSContextMarshall2 [RPCRT4.@]
236 */
237 void WINAPI NDRSContextMarshall2(RPC_BINDING_HANDLE hBinding,
238 NDR_SCONTEXT SContext,
239 void *pBuff,
240 NDR_RUNDOWN userRunDownIn,
241 void *CtxGuard, ULONG Flags)
242 {
243 RpcBinding *binding = hBinding;
244 RPC_STATUS status;
245 ndr_context_handle *ndr = pBuff;
246
247 TRACE("(%p %p %p %p %p %u)\n",
248 hBinding, SContext, pBuff, userRunDownIn, CtxGuard, Flags);
249
250 if (!binding->server || !binding->Assoc)
251 RpcRaiseException(ERROR_INVALID_HANDLE);
252
253 if (Flags & RPC_CONTEXT_HANDLE_FLAGS)
254 FIXME("unimplemented flags: 0x%x\n", Flags & RPC_CONTEXT_HANDLE_FLAGS);
255
256 if (SContext->userContext)
257 {
258 status = RpcServerAssoc_UpdateContextHandle(binding->Assoc, SContext, CtxGuard, userRunDownIn);
259 if (status != RPC_S_OK)
260 RpcRaiseException(status);
261 ndr->attributes = 0;
262 RpcContextHandle_GetUuid(SContext, &ndr->uuid);
263
264 RPCRT4_RemoveThreadContextHandle(SContext);
265 RpcServerAssoc_ReleaseContextHandle(binding->Assoc, SContext, TRUE);
266 }
267 else
268 {
269 if (!RpcContextHandle_IsGuardCorrect(SContext, CtxGuard))
270 RpcRaiseException(ERROR_INVALID_HANDLE);
271 memset(ndr, 0, sizeof(*ndr));
272
273 RPCRT4_RemoveThreadContextHandle(SContext);
274 /* Note: release the context handle twice in this case to release
275 * one ref being kept around for the data and one ref for the
276 * unmarshall/marshall sequence */
277 if (!RpcServerAssoc_ReleaseContextHandle(binding->Assoc, SContext, TRUE))
278 return; /* this is to cope with the case of the data not being valid
279 * before and so not having a further reference */
280 RpcServerAssoc_ReleaseContextHandle(binding->Assoc, SContext, FALSE);
281 }
282 }
283
284 /***********************************************************************
285 * NDRSContextUnmarshall [RPCRT4.@]
286 */
287 NDR_SCONTEXT WINAPI NDRSContextUnmarshall(void *pBuff,
288 ULONG DataRepresentation)
289 {
290 TRACE("(%p %08x)\n", pBuff, DataRepresentation);
291 return NDRSContextUnmarshall2(I_RpcGetCurrentCallHandle(), pBuff,
292 DataRepresentation, NULL,
293 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS);
294 }
295
296 /***********************************************************************
297 * NDRSContextUnmarshallEx [RPCRT4.@]
298 */
299 NDR_SCONTEXT WINAPI NDRSContextUnmarshallEx(RPC_BINDING_HANDLE hBinding,
300 void *pBuff,
301 ULONG DataRepresentation)
302 {
303 TRACE("(%p %p %08x)\n", hBinding, pBuff, DataRepresentation);
304 return NDRSContextUnmarshall2(hBinding, pBuff, DataRepresentation, NULL,
305 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS);
306 }
307
308 /***********************************************************************
309 * NDRSContextUnmarshall2 [RPCRT4.@]
310 */
311 NDR_SCONTEXT WINAPI NDRSContextUnmarshall2(RPC_BINDING_HANDLE hBinding,
312 void *pBuff,
313 ULONG DataRepresentation,
314 void *CtxGuard, ULONG Flags)
315 {
316 RpcBinding *binding = hBinding;
317 NDR_SCONTEXT SContext;
318 RPC_STATUS status;
319 const ndr_context_handle *context_ndr = pBuff;
320
321 TRACE("(%p %p %08x %p %u)\n",
322 hBinding, pBuff, DataRepresentation, CtxGuard, Flags);
323
324 if (!binding->server || !binding->Assoc)
325 RpcRaiseException(ERROR_INVALID_HANDLE);
326
327 if (Flags & RPC_CONTEXT_HANDLE_FLAGS)
328 FIXME("unimplemented flags: 0x%x\n", Flags & RPC_CONTEXT_HANDLE_FLAGS);
329
330 if (!pBuff || (!context_ndr->attributes &&
331 UuidIsNil((UUID *)&context_ndr->uuid, &status)))
332 status = RpcServerAssoc_AllocateContextHandle(binding->Assoc, CtxGuard,
333 &SContext);
334 else
335 {
336 if (context_ndr->attributes)
337 {
338 ERR("non-null attributes 0x%x\n", context_ndr->attributes);
339 status = ERROR_INVALID_HANDLE;
340 }
341 else
342 status = RpcServerAssoc_FindContextHandle(binding->Assoc,
343 &context_ndr->uuid,
344 CtxGuard, Flags,
345 &SContext);
346 }
347
348 if (status != RPC_S_OK)
349 RpcRaiseException(status);
350
351 RPCRT4_PushThreadContextHandle(SContext);
352 return SContext;
353 }