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
23 #include "rpc_assoc.h"
27 #include "wine/debug.h"
28 #include "wine/list.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
32 #define NDR_CONTEXT_HANDLE_MAGIC 0x4352444e
34 typedef struct ndr_context_handle
40 struct context_handle_entry
44 RPC_BINDING_HANDLE handle
;
45 ndr_context_handle wire_data
;
48 static struct list context_handle_list
= LIST_INIT(context_handle_list
);
50 static CRITICAL_SECTION ndr_context_cs
;
51 static CRITICAL_SECTION_DEBUG ndr_context_debug
=
53 0, 0, &ndr_context_cs
,
54 { &ndr_context_debug
.ProcessLocksList
, &ndr_context_debug
.ProcessLocksList
},
55 0, 0, { (DWORD_PTR
)(__FILE__
": ndr_context") }
57 static CRITICAL_SECTION ndr_context_cs
= { &ndr_context_debug
, -1, 0, 0, 0, 0 };
59 static struct context_handle_entry
*get_context_entry(NDR_CCONTEXT CContext
)
61 struct context_handle_entry
*che
= CContext
;
63 if (che
->magic
!= NDR_CONTEXT_HANDLE_MAGIC
)
68 static struct context_handle_entry
*context_entry_from_guid(LPCGUID uuid
)
70 struct context_handle_entry
*che
;
71 LIST_FOR_EACH_ENTRY(che
, &context_handle_list
, struct context_handle_entry
, entry
)
72 if (IsEqualGUID(&che
->wire_data
.uuid
, uuid
))
77 RPC_BINDING_HANDLE WINAPI
NDRCContextBinding(NDR_CCONTEXT CContext
)
79 struct context_handle_entry
*che
;
80 RPC_BINDING_HANDLE handle
= NULL
;
82 TRACE("%p\n", CContext
);
84 EnterCriticalSection(&ndr_context_cs
);
85 che
= get_context_entry(CContext
);
88 LeaveCriticalSection(&ndr_context_cs
);
92 ERR("invalid handle %p\n", CContext
);
93 RpcRaiseException(RPC_X_SS_CONTEXT_MISMATCH
);
98 void WINAPI
NDRCContextMarshall(NDR_CCONTEXT CContext
, void *pBuff
)
100 struct context_handle_entry
*che
;
102 TRACE("%p %p\n", CContext
, pBuff
);
106 EnterCriticalSection(&ndr_context_cs
);
107 che
= get_context_entry(CContext
);
108 memcpy(pBuff
, &che
->wire_data
, sizeof (ndr_context_handle
));
109 LeaveCriticalSection(&ndr_context_cs
);
113 ndr_context_handle
*wire_data
= pBuff
;
114 wire_data
->attributes
= 0;
115 wire_data
->uuid
= GUID_NULL
;
119 /***********************************************************************
120 * RpcSmDestroyClientContext [RPCRT4.@]
122 RPC_STATUS WINAPI
RpcSmDestroyClientContext(void **ContextHandle
)
124 RPC_STATUS status
= RPC_X_SS_CONTEXT_MISMATCH
;
125 struct context_handle_entry
*che
= NULL
;
127 TRACE("(%p)\n", ContextHandle
);
129 EnterCriticalSection(&ndr_context_cs
);
130 che
= get_context_entry(*ContextHandle
);
131 *ContextHandle
= NULL
;
135 list_remove(&che
->entry
);
138 LeaveCriticalSection(&ndr_context_cs
);
142 RpcBindingFree(&che
->handle
);
143 HeapFree(GetProcessHeap(), 0, che
);
149 /***********************************************************************
150 * RpcSsDestroyClientContext [RPCRT4.@]
152 void WINAPI
RpcSsDestroyClientContext(void **ContextHandle
)
154 RPC_STATUS status
= RpcSmDestroyClientContext(ContextHandle
);
155 if (status
!= RPC_S_OK
)
156 RpcRaiseException(status
);
159 /***********************************************************************
160 * RpcSsDontSerializeContext [RPCRT4.@]
162 void WINAPI
RpcSsDontSerializeContext(void)
167 static RPC_STATUS
ndr_update_context_handle(NDR_CCONTEXT
*CContext
,
168 RPC_BINDING_HANDLE hBinding
,
169 const ndr_context_handle
*chi
)
171 struct context_handle_entry
*che
= NULL
;
173 /* a null UUID means we should free the context handle */
174 if (IsEqualGUID(&chi
->uuid
, &GUID_NULL
))
178 che
= get_context_entry(*CContext
);
180 return RPC_X_SS_CONTEXT_MISMATCH
;
181 list_remove(&che
->entry
);
182 RpcBindingFree(&che
->handle
);
183 HeapFree(GetProcessHeap(), 0, che
);
187 /* if there's no existing entry matching the GUID, allocate one */
188 else if (!(che
= context_entry_from_guid(&chi
->uuid
)))
190 che
= HeapAlloc(GetProcessHeap(), 0, sizeof *che
);
192 return RPC_X_NO_MEMORY
;
193 che
->magic
= NDR_CONTEXT_HANDLE_MAGIC
;
194 RpcBindingCopy(hBinding
, &che
->handle
);
195 list_add_tail(&context_handle_list
, &che
->entry
);
196 che
->wire_data
= *chi
;
204 /***********************************************************************
205 * NDRCContextUnmarshall [RPCRT4.@]
207 void WINAPI
NDRCContextUnmarshall(NDR_CCONTEXT
*CContext
,
208 RPC_BINDING_HANDLE hBinding
,
209 void *pBuff
, ULONG DataRepresentation
)
213 TRACE("*%p=(%p) %p %p %08x\n",
214 CContext
, *CContext
, hBinding
, pBuff
, DataRepresentation
);
216 EnterCriticalSection(&ndr_context_cs
);
217 status
= ndr_update_context_handle(CContext
, hBinding
, pBuff
);
218 LeaveCriticalSection(&ndr_context_cs
);
220 RpcRaiseException(status
);
223 /***********************************************************************
224 * NDRSContextMarshall [RPCRT4.@]
226 void WINAPI
NDRSContextMarshall(NDR_SCONTEXT SContext
,
228 NDR_RUNDOWN userRunDownIn
)
230 TRACE("(%p %p %p)\n", SContext
, pBuff
, userRunDownIn
);
231 NDRSContextMarshall2(I_RpcGetCurrentCallHandle(), SContext
, pBuff
,
232 userRunDownIn
, NULL
, RPC_CONTEXT_HANDLE_DEFAULT_FLAGS
);
235 /***********************************************************************
236 * NDRSContextMarshallEx [RPCRT4.@]
238 void WINAPI
NDRSContextMarshallEx(RPC_BINDING_HANDLE hBinding
,
239 NDR_SCONTEXT SContext
,
241 NDR_RUNDOWN userRunDownIn
)
243 TRACE("(%p %p %p %p)\n", hBinding
, SContext
, pBuff
, userRunDownIn
);
244 NDRSContextMarshall2(hBinding
, SContext
, pBuff
, userRunDownIn
, NULL
,
245 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS
);
248 /***********************************************************************
249 * NDRSContextMarshall2 [RPCRT4.@]
251 void WINAPI
NDRSContextMarshall2(RPC_BINDING_HANDLE hBinding
,
252 NDR_SCONTEXT SContext
,
254 NDR_RUNDOWN userRunDownIn
,
255 void *CtxGuard
, ULONG Flags
)
257 RpcBinding
*binding
= hBinding
;
259 ndr_context_handle
*ndr
= pBuff
;
261 TRACE("(%p %p %p %p %p %u)\n",
262 hBinding
, SContext
, pBuff
, userRunDownIn
, CtxGuard
, Flags
);
264 if (!binding
->server
|| !binding
->Assoc
)
265 RpcRaiseException(RPC_S_INVALID_BINDING
);
267 if (SContext
->userContext
)
269 status
= RpcServerAssoc_UpdateContextHandle(binding
->Assoc
, SContext
, CtxGuard
, userRunDownIn
);
270 if (status
!= RPC_S_OK
)
271 RpcRaiseException(status
);
273 RpcContextHandle_GetUuid(SContext
, &ndr
->uuid
);
275 RPCRT4_RemoveThreadContextHandle(SContext
);
276 RpcServerAssoc_ReleaseContextHandle(binding
->Assoc
, SContext
, TRUE
);
280 if (!RpcContextHandle_IsGuardCorrect(SContext
, CtxGuard
))
281 RpcRaiseException(RPC_X_SS_CONTEXT_MISMATCH
);
282 memset(ndr
, 0, sizeof(*ndr
));
284 RPCRT4_RemoveThreadContextHandle(SContext
);
285 /* Note: release the context handle twice in this case to release
286 * one ref being kept around for the data and one ref for the
287 * unmarshall/marshall sequence */
288 if (!RpcServerAssoc_ReleaseContextHandle(binding
->Assoc
, SContext
, TRUE
))
289 return; /* this is to cope with the case of the data not being valid
290 * before and so not having a further reference */
291 RpcServerAssoc_ReleaseContextHandle(binding
->Assoc
, SContext
, FALSE
);
295 /***********************************************************************
296 * NDRSContextUnmarshall [RPCRT4.@]
298 NDR_SCONTEXT WINAPI
NDRSContextUnmarshall(void *pBuff
,
299 ULONG DataRepresentation
)
301 TRACE("(%p %08x)\n", pBuff
, DataRepresentation
);
302 return NDRSContextUnmarshall2(I_RpcGetCurrentCallHandle(), pBuff
,
303 DataRepresentation
, NULL
,
304 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS
);
307 /***********************************************************************
308 * NDRSContextUnmarshallEx [RPCRT4.@]
310 NDR_SCONTEXT WINAPI
NDRSContextUnmarshallEx(RPC_BINDING_HANDLE hBinding
,
312 ULONG DataRepresentation
)
314 TRACE("(%p %p %08x)\n", hBinding
, pBuff
, DataRepresentation
);
315 return NDRSContextUnmarshall2(hBinding
, pBuff
, DataRepresentation
, NULL
,
316 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS
);
319 /***********************************************************************
320 * NDRSContextUnmarshall2 [RPCRT4.@]
322 NDR_SCONTEXT WINAPI
NDRSContextUnmarshall2(RPC_BINDING_HANDLE hBinding
,
324 ULONG DataRepresentation
,
325 void *CtxGuard
, ULONG Flags
)
327 RpcBinding
*binding
= hBinding
;
328 NDR_SCONTEXT SContext
;
330 const ndr_context_handle
*context_ndr
= pBuff
;
332 TRACE("(%p %p %08x %p %u)\n",
333 hBinding
, pBuff
, DataRepresentation
, CtxGuard
, Flags
);
335 if (!binding
->server
|| !binding
->Assoc
)
336 RpcRaiseException(RPC_S_INVALID_BINDING
);
338 if (!pBuff
|| (!context_ndr
->attributes
&&
339 UuidIsNil((UUID
*)&context_ndr
->uuid
, &status
)))
340 status
= RpcServerAssoc_AllocateContextHandle(binding
->Assoc
, CtxGuard
,
344 if (context_ndr
->attributes
)
346 ERR("non-null attributes 0x%x\n", context_ndr
->attributes
);
347 status
= RPC_X_SS_CONTEXT_MISMATCH
;
350 status
= RpcServerAssoc_FindContextHandle(binding
->Assoc
,
356 if (status
!= RPC_S_OK
)
357 RpcRaiseException(status
);
359 RPCRT4_PushThreadContextHandle(SContext
);