4 * Copyright 2006 Mike McCormack (for CodeWeavers)
5 * Copyright 2006-2007 Robert Shearman (for CodeWeavers)
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.
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.
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
24 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
26 #define NDR_CONTEXT_HANDLE_MAGIC 0x4352444e
28 typedef struct ndr_context_handle
34 struct context_handle_entry
38 RPC_BINDING_HANDLE handle
;
39 ndr_context_handle wire_data
;
42 static struct list context_handle_list
= LIST_INIT(context_handle_list
);
44 static CRITICAL_SECTION ndr_context_cs
;
45 static CRITICAL_SECTION_DEBUG ndr_context_debug
=
47 0, 0, &ndr_context_cs
,
48 { &ndr_context_debug
.ProcessLocksList
, &ndr_context_debug
.ProcessLocksList
},
49 0, 0, { (DWORD_PTR
)(__FILE__
": ndr_context") }
51 static CRITICAL_SECTION ndr_context_cs
= { &ndr_context_debug
, -1, 0, 0, 0, 0 };
53 static struct context_handle_entry
*get_context_entry(NDR_CCONTEXT CContext
)
55 struct context_handle_entry
*che
= CContext
;
57 if (che
->magic
!= NDR_CONTEXT_HANDLE_MAGIC
)
62 static struct context_handle_entry
*context_entry_from_guid(LPCGUID uuid
)
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
))
71 RPC_BINDING_HANDLE WINAPI
NDRCContextBinding(NDR_CCONTEXT CContext
)
73 struct context_handle_entry
*che
;
74 RPC_BINDING_HANDLE handle
= NULL
;
76 TRACE("%p\n", CContext
);
78 EnterCriticalSection(&ndr_context_cs
);
79 che
= get_context_entry(CContext
);
82 LeaveCriticalSection(&ndr_context_cs
);
86 ERR("invalid handle %p\n", CContext
);
87 RpcRaiseException(ERROR_INVALID_HANDLE
);
92 void WINAPI
NDRCContextMarshall(NDR_CCONTEXT CContext
, void *pBuff
)
94 struct context_handle_entry
*che
;
96 TRACE("%p %p\n", CContext
, pBuff
);
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
);
107 ndr_context_handle
*wire_data
= pBuff
;
108 wire_data
->attributes
= 0;
109 wire_data
->uuid
= GUID_NULL
;
113 /***********************************************************************
114 * RpcSmDestroyClientContext [RPCRT4.@]
116 RPC_STATUS WINAPI
RpcSmDestroyClientContext(void **ContextHandle
)
118 RPC_STATUS status
= RPC_X_SS_CONTEXT_MISMATCH
;
119 struct context_handle_entry
*che
= NULL
;
121 TRACE("(%p)\n", ContextHandle
);
123 EnterCriticalSection(&ndr_context_cs
);
124 che
= get_context_entry(*ContextHandle
);
125 *ContextHandle
= NULL
;
129 list_remove(&che
->entry
);
132 LeaveCriticalSection(&ndr_context_cs
);
136 RpcBindingFree(&che
->handle
);
137 HeapFree(GetProcessHeap(), 0, che
);
143 /***********************************************************************
144 * RpcSsDestroyClientContext [RPCRT4.@]
146 void WINAPI
RpcSsDestroyClientContext(void **ContextHandle
)
148 RPC_STATUS status
= RpcSmDestroyClientContext(ContextHandle
);
149 if (status
!= RPC_S_OK
)
150 RpcRaiseException(status
);
153 static UINT
ndr_update_context_handle(NDR_CCONTEXT
*CContext
,
154 RPC_BINDING_HANDLE hBinding
,
155 const ndr_context_handle
*chi
)
157 struct context_handle_entry
*che
= NULL
;
159 /* a null UUID means we should free the context handle */
160 if (IsEqualGUID(&chi
->uuid
, &GUID_NULL
))
164 che
= get_context_entry(*CContext
);
166 return ERROR_INVALID_HANDLE
;
167 list_remove(&che
->entry
);
168 RpcBindingFree(&che
->handle
);
169 HeapFree(GetProcessHeap(), 0, che
);
173 /* if there's no existing entry matching the GUID, allocate one */
174 else if (!(che
= context_entry_from_guid(&chi
->uuid
)))
176 che
= HeapAlloc(GetProcessHeap(), 0, sizeof *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
;
187 return ERROR_SUCCESS
;
190 /***********************************************************************
191 * NDRCContextUnmarshall [RPCRT4.@]
193 void WINAPI
NDRCContextUnmarshall(NDR_CCONTEXT
*CContext
,
194 RPC_BINDING_HANDLE hBinding
,
195 void *pBuff
, ULONG DataRepresentation
)
199 TRACE("*%p=(%p) %p %p %08x\n",
200 CContext
, *CContext
, hBinding
, pBuff
, DataRepresentation
);
202 EnterCriticalSection(&ndr_context_cs
);
203 r
= ndr_update_context_handle(CContext
, hBinding
, pBuff
);
204 LeaveCriticalSection(&ndr_context_cs
);
206 RpcRaiseException(r
);
209 /***********************************************************************
210 * NDRSContextMarshall [RPCRT4.@]
212 void WINAPI
NDRSContextMarshall(NDR_SCONTEXT SContext
,
214 NDR_RUNDOWN userRunDownIn
)
216 TRACE("(%p %p %p)\n", SContext
, pBuff
, userRunDownIn
);
217 NDRSContextMarshall2(I_RpcGetCurrentCallHandle(), SContext
, pBuff
,
218 userRunDownIn
, NULL
, RPC_CONTEXT_HANDLE_DEFAULT_FLAGS
);
221 /***********************************************************************
222 * NDRSContextMarshallEx [RPCRT4.@]
224 void WINAPI
NDRSContextMarshallEx(RPC_BINDING_HANDLE hBinding
,
225 NDR_SCONTEXT SContext
,
227 NDR_RUNDOWN userRunDownIn
)
229 TRACE("(%p %p %p %p)\n", hBinding
, SContext
, pBuff
, userRunDownIn
);
230 NDRSContextMarshall2(hBinding
, SContext
, pBuff
, userRunDownIn
, NULL
,
231 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS
);
234 /***********************************************************************
235 * NDRSContextMarshall2 [RPCRT4.@]
237 void WINAPI
NDRSContextMarshall2(RPC_BINDING_HANDLE hBinding
,
238 NDR_SCONTEXT SContext
,
240 NDR_RUNDOWN userRunDownIn
,
241 void *CtxGuard
, ULONG Flags
)
243 RpcBinding
*binding
= hBinding
;
245 ndr_context_handle
*ndr
= pBuff
;
247 TRACE("(%p %p %p %p %p %u)\n",
248 hBinding
, SContext
, pBuff
, userRunDownIn
, CtxGuard
, Flags
);
250 if (!binding
->server
|| !binding
->Assoc
)
251 RpcRaiseException(ERROR_INVALID_HANDLE
);
253 if (Flags
& RPC_CONTEXT_HANDLE_FLAGS
)
254 FIXME("unimplemented flags: 0x%x\n", Flags
& RPC_CONTEXT_HANDLE_FLAGS
);
256 if (SContext
->userContext
)
258 status
= RpcServerAssoc_UpdateContextHandle(binding
->Assoc
, SContext
, CtxGuard
, userRunDownIn
);
259 if (status
!= RPC_S_OK
)
260 RpcRaiseException(status
);
262 RpcContextHandle_GetUuid(SContext
, &ndr
->uuid
);
264 RPCRT4_RemoveThreadContextHandle(SContext
);
265 RpcServerAssoc_ReleaseContextHandle(binding
->Assoc
, SContext
, TRUE
);
269 if (!RpcContextHandle_IsGuardCorrect(SContext
, CtxGuard
))
270 RpcRaiseException(ERROR_INVALID_HANDLE
);
271 memset(ndr
, 0, sizeof(*ndr
));
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
);
284 /***********************************************************************
285 * NDRSContextUnmarshall [RPCRT4.@]
287 NDR_SCONTEXT WINAPI
NDRSContextUnmarshall(void *pBuff
,
288 ULONG DataRepresentation
)
290 TRACE("(%p %08x)\n", pBuff
, DataRepresentation
);
291 return NDRSContextUnmarshall2(I_RpcGetCurrentCallHandle(), pBuff
,
292 DataRepresentation
, NULL
,
293 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS
);
296 /***********************************************************************
297 * NDRSContextUnmarshallEx [RPCRT4.@]
299 NDR_SCONTEXT WINAPI
NDRSContextUnmarshallEx(RPC_BINDING_HANDLE hBinding
,
301 ULONG DataRepresentation
)
303 TRACE("(%p %p %08x)\n", hBinding
, pBuff
, DataRepresentation
);
304 return NDRSContextUnmarshall2(hBinding
, pBuff
, DataRepresentation
, NULL
,
305 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS
);
308 /***********************************************************************
309 * NDRSContextUnmarshall2 [RPCRT4.@]
311 NDR_SCONTEXT WINAPI
NDRSContextUnmarshall2(RPC_BINDING_HANDLE hBinding
,
313 ULONG DataRepresentation
,
314 void *CtxGuard
, ULONG Flags
)
316 RpcBinding
*binding
= hBinding
;
317 NDR_SCONTEXT SContext
;
319 const ndr_context_handle
*context_ndr
= pBuff
;
321 TRACE("(%p %p %08x %p %u)\n",
322 hBinding
, pBuff
, DataRepresentation
, CtxGuard
, Flags
);
324 if (!binding
->server
|| !binding
->Assoc
)
325 RpcRaiseException(ERROR_INVALID_HANDLE
);
327 if (Flags
& RPC_CONTEXT_HANDLE_FLAGS
)
328 FIXME("unimplemented flags: 0x%x\n", Flags
& RPC_CONTEXT_HANDLE_FLAGS
);
330 if (!pBuff
|| (!context_ndr
->attributes
&&
331 UuidIsNil((UUID
*)&context_ndr
->uuid
, &status
)))
332 status
= RpcServerAssoc_AllocateContextHandle(binding
->Assoc
, CtxGuard
,
336 if (context_ndr
->attributes
)
338 ERR("non-null attributes 0x%x\n", context_ndr
->attributes
);
339 status
= ERROR_INVALID_HANDLE
;
342 status
= RpcServerAssoc_FindContextHandle(binding
->Assoc
,
348 if (status
!= RPC_S_OK
)
349 RpcRaiseException(status
);
351 RPCRT4_PushThreadContextHandle(SContext
);