ntdll: Implement DVD_StartSession() on Mac OS.
[wine.git] / dlls / rpcrt4 / rpc_assoc.c
blob117301f42c0c8d5a5bb62a318125cd20d9b7b75d
1 /*
2 * Associations
4 * Copyright 2007 Robert Shearman (for CodeWeavers)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <assert.h>
25 #include "rpc.h"
26 #include "rpcndr.h"
27 #include "winternl.h"
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
32 #include "rpc_binding.h"
33 #include "rpc_assoc.h"
34 #include "rpc_message.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
38 static CRITICAL_SECTION assoc_list_cs;
39 static CRITICAL_SECTION_DEBUG assoc_list_cs_debug =
41 0, 0, &assoc_list_cs,
42 { &assoc_list_cs_debug.ProcessLocksList, &assoc_list_cs_debug.ProcessLocksList },
43 0, 0, { (DWORD_PTR)(__FILE__ ": assoc_list_cs") }
45 static CRITICAL_SECTION assoc_list_cs = { &assoc_list_cs_debug, -1, 0, 0, 0, 0 };
47 static struct list client_assoc_list = LIST_INIT(client_assoc_list);
48 static struct list server_assoc_list = LIST_INIT(server_assoc_list);
50 static LONG last_assoc_group_id;
52 typedef struct _RpcContextHandle
54 struct list entry;
55 void *user_context;
56 NDR_RUNDOWN rundown_routine;
57 void *ctx_guard;
58 UUID uuid;
59 RTL_RWLOCK rw_lock;
60 unsigned int refs;
61 } RpcContextHandle;
63 static void RpcContextHandle_Destroy(RpcContextHandle *context_handle);
65 static RPC_STATUS RpcAssoc_Alloc(LPCSTR Protseq, LPCSTR NetworkAddr,
66 LPCSTR Endpoint, LPCWSTR NetworkOptions,
67 RpcAssoc **assoc_out)
69 RpcAssoc *assoc;
70 assoc = HeapAlloc(GetProcessHeap(), 0, sizeof(*assoc));
71 if (!assoc)
72 return RPC_S_OUT_OF_RESOURCES;
73 assoc->refs = 1;
74 list_init(&assoc->free_connection_pool);
75 list_init(&assoc->context_handle_list);
76 InitializeCriticalSection(&assoc->cs);
77 assoc->Protseq = RPCRT4_strdupA(Protseq);
78 assoc->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
79 assoc->Endpoint = RPCRT4_strdupA(Endpoint);
80 assoc->NetworkOptions = NetworkOptions ? RPCRT4_strdupW(NetworkOptions) : NULL;
81 assoc->assoc_group_id = 0;
82 list_init(&assoc->entry);
83 *assoc_out = assoc;
84 return RPC_S_OK;
87 RPC_STATUS RPCRT4_GetAssociation(LPCSTR Protseq, LPCSTR NetworkAddr,
88 LPCSTR Endpoint, LPCWSTR NetworkOptions,
89 RpcAssoc **assoc_out)
91 RpcAssoc *assoc;
92 RPC_STATUS status;
94 EnterCriticalSection(&assoc_list_cs);
95 LIST_FOR_EACH_ENTRY(assoc, &client_assoc_list, RpcAssoc, entry)
97 if (!strcmp(Protseq, assoc->Protseq) &&
98 !strcmp(NetworkAddr, assoc->NetworkAddr) &&
99 !strcmp(Endpoint, assoc->Endpoint) &&
100 ((!assoc->NetworkOptions && !NetworkOptions) || !strcmpW(NetworkOptions, assoc->NetworkOptions)))
102 assoc->refs++;
103 *assoc_out = assoc;
104 LeaveCriticalSection(&assoc_list_cs);
105 TRACE("using existing assoc %p\n", assoc);
106 return RPC_S_OK;
110 status = RpcAssoc_Alloc(Protseq, NetworkAddr, Endpoint, NetworkOptions, &assoc);
111 if (status != RPC_S_OK)
113 LeaveCriticalSection(&assoc_list_cs);
114 return status;
116 list_add_head(&client_assoc_list, &assoc->entry);
117 *assoc_out = assoc;
119 LeaveCriticalSection(&assoc_list_cs);
121 TRACE("new assoc %p\n", assoc);
123 return RPC_S_OK;
126 RPC_STATUS RpcServerAssoc_GetAssociation(LPCSTR Protseq, LPCSTR NetworkAddr,
127 LPCSTR Endpoint, LPCWSTR NetworkOptions,
128 ULONG assoc_gid,
129 RpcAssoc **assoc_out)
131 RpcAssoc *assoc;
132 RPC_STATUS status;
134 EnterCriticalSection(&assoc_list_cs);
135 if (assoc_gid)
137 LIST_FOR_EACH_ENTRY(assoc, &server_assoc_list, RpcAssoc, entry)
139 /* FIXME: NetworkAddr shouldn't be NULL */
140 if (assoc->assoc_group_id == assoc_gid &&
141 !strcmp(Protseq, assoc->Protseq) &&
142 (!NetworkAddr || !assoc->NetworkAddr || !strcmp(NetworkAddr, assoc->NetworkAddr)) &&
143 !strcmp(Endpoint, assoc->Endpoint) &&
144 ((!assoc->NetworkOptions == !NetworkOptions) &&
145 (!NetworkOptions || !strcmpW(NetworkOptions, assoc->NetworkOptions))))
147 assoc->refs++;
148 *assoc_out = assoc;
149 LeaveCriticalSection(&assoc_list_cs);
150 TRACE("using existing assoc %p\n", assoc);
151 return RPC_S_OK;
154 *assoc_out = NULL;
155 LeaveCriticalSection(&assoc_list_cs);
156 return RPC_S_NO_CONTEXT_AVAILABLE;
159 status = RpcAssoc_Alloc(Protseq, NetworkAddr, Endpoint, NetworkOptions, &assoc);
160 if (status != RPC_S_OK)
162 LeaveCriticalSection(&assoc_list_cs);
163 return status;
165 assoc->assoc_group_id = InterlockedIncrement(&last_assoc_group_id);
166 list_add_head(&server_assoc_list, &assoc->entry);
167 *assoc_out = assoc;
169 LeaveCriticalSection(&assoc_list_cs);
171 TRACE("new assoc %p\n", assoc);
173 return RPC_S_OK;
176 ULONG RpcAssoc_Release(RpcAssoc *assoc)
178 ULONG refs;
180 EnterCriticalSection(&assoc_list_cs);
181 refs = --assoc->refs;
182 if (!refs)
183 list_remove(&assoc->entry);
184 LeaveCriticalSection(&assoc_list_cs);
186 if (!refs)
188 RpcConnection *Connection, *cursor2;
189 RpcContextHandle *context_handle, *context_handle_cursor;
191 TRACE("destroying assoc %p\n", assoc);
193 LIST_FOR_EACH_ENTRY_SAFE(Connection, cursor2, &assoc->free_connection_pool, RpcConnection, conn_pool_entry)
195 list_remove(&Connection->conn_pool_entry);
196 RPCRT4_DestroyConnection(Connection);
199 LIST_FOR_EACH_ENTRY_SAFE(context_handle, context_handle_cursor, &assoc->context_handle_list, RpcContextHandle, entry)
200 RpcContextHandle_Destroy(context_handle);
202 HeapFree(GetProcessHeap(), 0, assoc->NetworkOptions);
203 HeapFree(GetProcessHeap(), 0, assoc->Endpoint);
204 HeapFree(GetProcessHeap(), 0, assoc->NetworkAddr);
205 HeapFree(GetProcessHeap(), 0, assoc->Protseq);
207 DeleteCriticalSection(&assoc->cs);
209 HeapFree(GetProcessHeap(), 0, assoc);
212 return refs;
215 #define ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment)-1))
217 static RPC_STATUS RpcAssoc_BindConnection(const RpcAssoc *assoc, RpcConnection *conn,
218 const RPC_SYNTAX_IDENTIFIER *InterfaceId,
219 const RPC_SYNTAX_IDENTIFIER *TransferSyntax)
221 RpcPktHdr *hdr;
222 RpcPktHdr *response_hdr;
223 RPC_MESSAGE msg;
224 RPC_STATUS status;
225 unsigned char *auth_data = NULL;
226 ULONG auth_length;
228 TRACE("sending bind request to server\n");
230 hdr = RPCRT4_BuildBindHeader(NDR_LOCAL_DATA_REPRESENTATION,
231 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE,
232 assoc->assoc_group_id,
233 InterfaceId, TransferSyntax);
235 status = RPCRT4_Send(conn, hdr, NULL, 0);
236 RPCRT4_FreeHeader(hdr);
237 if (status != RPC_S_OK)
238 return status;
240 status = RPCRT4_ReceiveWithAuth(conn, &response_hdr, &msg, &auth_data, &auth_length);
241 if (status != RPC_S_OK)
243 ERR("receive failed with error %d\n", status);
244 return status;
247 switch (response_hdr->common.ptype)
249 case PKT_BIND_ACK:
251 RpcAddressString *server_address = msg.Buffer;
252 if ((msg.BufferLength >= FIELD_OFFSET(RpcAddressString, string[0])) ||
253 (msg.BufferLength >= ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4)))
255 unsigned short remaining = msg.BufferLength -
256 ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4);
257 RpcResults *results = (RpcResults*)((ULONG_PTR)server_address +
258 ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4));
259 if ((results->num_results == 1) && (remaining >= sizeof(*results)))
261 switch (results->results[0].result)
263 case RESULT_ACCEPT:
264 /* respond to authorization request */
265 if (auth_length > sizeof(RpcAuthVerifier))
266 status = RPCRT4_AuthorizeConnection(conn,
267 auth_data + sizeof(RpcAuthVerifier),
268 auth_length);
269 if (status == RPC_S_OK)
271 conn->assoc_group_id = response_hdr->bind_ack.assoc_gid;
272 conn->MaxTransmissionSize = response_hdr->bind_ack.max_tsize;
273 conn->ActiveInterface = *InterfaceId;
275 break;
276 case RESULT_PROVIDER_REJECTION:
277 switch (results->results[0].reason)
279 case REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED:
280 ERR("syntax %s, %d.%d not supported\n",
281 debugstr_guid(&InterfaceId->SyntaxGUID),
282 InterfaceId->SyntaxVersion.MajorVersion,
283 InterfaceId->SyntaxVersion.MinorVersion);
284 status = RPC_S_UNKNOWN_IF;
285 break;
286 case REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED:
287 ERR("transfer syntax not supported\n");
288 status = RPC_S_SERVER_UNAVAILABLE;
289 break;
290 case REASON_NONE:
291 default:
292 status = RPC_S_CALL_FAILED_DNE;
294 break;
295 case RESULT_USER_REJECTION:
296 default:
297 ERR("rejection result %d\n", results->results[0].result);
298 status = RPC_S_CALL_FAILED_DNE;
301 else
303 ERR("incorrect results size\n");
304 status = RPC_S_CALL_FAILED_DNE;
307 else
309 ERR("bind ack packet too small (%d)\n", msg.BufferLength);
310 status = RPC_S_PROTOCOL_ERROR;
312 break;
314 case PKT_BIND_NACK:
315 switch (response_hdr->bind_nack.reject_reason)
317 case REJECT_LOCAL_LIMIT_EXCEEDED:
318 case REJECT_TEMPORARY_CONGESTION:
319 ERR("server too busy\n");
320 status = RPC_S_SERVER_TOO_BUSY;
321 break;
322 case REJECT_PROTOCOL_VERSION_NOT_SUPPORTED:
323 ERR("protocol version not supported\n");
324 status = RPC_S_PROTOCOL_ERROR;
325 break;
326 case REJECT_UNKNOWN_AUTHN_SERVICE:
327 ERR("unknown authentication service\n");
328 status = RPC_S_UNKNOWN_AUTHN_SERVICE;
329 break;
330 case REJECT_INVALID_CHECKSUM:
331 ERR("invalid checksum\n");
332 status = ERROR_ACCESS_DENIED;
333 break;
334 default:
335 ERR("rejected bind for reason %d\n", response_hdr->bind_nack.reject_reason);
336 status = RPC_S_CALL_FAILED_DNE;
338 break;
339 default:
340 ERR("wrong packet type received %d\n", response_hdr->common.ptype);
341 status = RPC_S_PROTOCOL_ERROR;
342 break;
345 I_RpcFree(msg.Buffer);
346 RPCRT4_FreeHeader(response_hdr);
347 HeapFree(GetProcessHeap(), 0, auth_data);
348 return status;
351 static RpcConnection *RpcAssoc_GetIdleConnection(RpcAssoc *assoc,
352 const RPC_SYNTAX_IDENTIFIER *InterfaceId,
353 const RPC_SYNTAX_IDENTIFIER *TransferSyntax, const RpcAuthInfo *AuthInfo,
354 const RpcQualityOfService *QOS)
356 RpcConnection *Connection;
357 EnterCriticalSection(&assoc->cs);
358 /* try to find a compatible connection from the connection pool */
359 LIST_FOR_EACH_ENTRY(Connection, &assoc->free_connection_pool, RpcConnection, conn_pool_entry)
361 if (!memcmp(&Connection->ActiveInterface, InterfaceId,
362 sizeof(RPC_SYNTAX_IDENTIFIER)) &&
363 RpcAuthInfo_IsEqual(Connection->AuthInfo, AuthInfo) &&
364 RpcQualityOfService_IsEqual(Connection->QOS, QOS))
366 list_remove(&Connection->conn_pool_entry);
367 LeaveCriticalSection(&assoc->cs);
368 TRACE("got connection from pool %p\n", Connection);
369 return Connection;
373 LeaveCriticalSection(&assoc->cs);
374 return NULL;
377 RPC_STATUS RpcAssoc_GetClientConnection(RpcAssoc *assoc,
378 const RPC_SYNTAX_IDENTIFIER *InterfaceId,
379 const RPC_SYNTAX_IDENTIFIER *TransferSyntax, RpcAuthInfo *AuthInfo,
380 RpcQualityOfService *QOS, RpcConnection **Connection)
382 RpcConnection *NewConnection;
383 RPC_STATUS status;
385 *Connection = RpcAssoc_GetIdleConnection(assoc, InterfaceId, TransferSyntax, AuthInfo, QOS);
386 if (*Connection)
387 return RPC_S_OK;
389 /* create a new connection */
390 status = RPCRT4_CreateConnection(&NewConnection, FALSE /* is this a server connection? */,
391 assoc->Protseq, assoc->NetworkAddr,
392 assoc->Endpoint, assoc->NetworkOptions,
393 AuthInfo, QOS);
394 if (status != RPC_S_OK)
395 return status;
397 NewConnection->assoc = assoc;
398 status = RPCRT4_OpenClientConnection(NewConnection);
399 if (status != RPC_S_OK)
401 RPCRT4_DestroyConnection(NewConnection);
402 return status;
405 status = RpcAssoc_BindConnection(assoc, NewConnection, InterfaceId, TransferSyntax);
406 if (status != RPC_S_OK)
408 RPCRT4_DestroyConnection(NewConnection);
409 return status;
412 *Connection = NewConnection;
414 return RPC_S_OK;
417 void RpcAssoc_ReleaseIdleConnection(RpcAssoc *assoc, RpcConnection *Connection)
419 assert(!Connection->server);
420 Connection->async_state = NULL;
421 EnterCriticalSection(&assoc->cs);
422 if (!assoc->assoc_group_id) assoc->assoc_group_id = Connection->assoc_group_id;
423 list_add_head(&assoc->free_connection_pool, &Connection->conn_pool_entry);
424 LeaveCriticalSection(&assoc->cs);
427 RPC_STATUS RpcServerAssoc_AllocateContextHandle(RpcAssoc *assoc, void *CtxGuard,
428 NDR_SCONTEXT *SContext)
430 RpcContextHandle *context_handle;
432 context_handle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context_handle));
433 if (!context_handle)
434 return ERROR_OUTOFMEMORY;
436 context_handle->ctx_guard = CtxGuard;
437 RtlInitializeResource(&context_handle->rw_lock);
438 context_handle->refs = 1;
440 /* lock here to mirror unmarshall, so we don't need to special-case the
441 * freeing of a non-marshalled context handle */
442 RtlAcquireResourceExclusive(&context_handle->rw_lock, TRUE);
444 EnterCriticalSection(&assoc->cs);
445 list_add_tail(&assoc->context_handle_list, &context_handle->entry);
446 LeaveCriticalSection(&assoc->cs);
448 *SContext = (NDR_SCONTEXT)context_handle;
449 return RPC_S_OK;
452 BOOL RpcContextHandle_IsGuardCorrect(NDR_SCONTEXT SContext, void *CtxGuard)
454 RpcContextHandle *context_handle = (RpcContextHandle *)SContext;
455 return context_handle->ctx_guard == CtxGuard;
458 RPC_STATUS RpcServerAssoc_FindContextHandle(RpcAssoc *assoc, const UUID *uuid,
459 void *CtxGuard, ULONG Flags, NDR_SCONTEXT *SContext)
461 RpcContextHandle *context_handle;
463 EnterCriticalSection(&assoc->cs);
464 LIST_FOR_EACH_ENTRY(context_handle, &assoc->context_handle_list, RpcContextHandle, entry)
466 if (RpcContextHandle_IsGuardCorrect((NDR_SCONTEXT)context_handle, CtxGuard) &&
467 !memcmp(&context_handle->uuid, uuid, sizeof(*uuid)))
469 *SContext = (NDR_SCONTEXT)context_handle;
470 if (context_handle->refs++)
472 LeaveCriticalSection(&assoc->cs);
473 TRACE("found %p\n", context_handle);
474 RtlAcquireResourceExclusive(&context_handle->rw_lock, TRUE);
475 return RPC_S_OK;
479 LeaveCriticalSection(&assoc->cs);
481 ERR("no context handle found for uuid %s, guard %p\n",
482 debugstr_guid(uuid), CtxGuard);
483 return ERROR_INVALID_HANDLE;
486 RPC_STATUS RpcServerAssoc_UpdateContextHandle(RpcAssoc *assoc,
487 NDR_SCONTEXT SContext,
488 void *CtxGuard,
489 NDR_RUNDOWN rundown_routine)
491 RpcContextHandle *context_handle = (RpcContextHandle *)SContext;
492 RPC_STATUS status;
494 if (!RpcContextHandle_IsGuardCorrect((NDR_SCONTEXT)context_handle, CtxGuard))
495 return ERROR_INVALID_HANDLE;
497 EnterCriticalSection(&assoc->cs);
498 if (UuidIsNil(&context_handle->uuid, &status))
500 /* add a ref for the data being valid */
501 context_handle->refs++;
502 UuidCreate(&context_handle->uuid);
503 context_handle->rundown_routine = rundown_routine;
504 TRACE("allocated uuid %s for context handle %p\n",
505 debugstr_guid(&context_handle->uuid), context_handle);
507 LeaveCriticalSection(&assoc->cs);
509 return RPC_S_OK;
512 void RpcContextHandle_GetUuid(NDR_SCONTEXT SContext, UUID *uuid)
514 RpcContextHandle *context_handle = (RpcContextHandle *)SContext;
515 *uuid = context_handle->uuid;
518 static void RpcContextHandle_Destroy(RpcContextHandle *context_handle)
520 TRACE("freeing %p\n", context_handle);
522 if (context_handle->user_context && context_handle->rundown_routine)
524 TRACE("calling rundown routine %p with user context %p\n",
525 context_handle->rundown_routine, context_handle->user_context);
526 context_handle->rundown_routine(context_handle->user_context);
529 RtlDeleteResource(&context_handle->rw_lock);
531 HeapFree(GetProcessHeap(), 0, context_handle);
534 unsigned int RpcServerAssoc_ReleaseContextHandle(RpcAssoc *assoc, NDR_SCONTEXT SContext, BOOL release_lock)
536 RpcContextHandle *context_handle = (RpcContextHandle *)SContext;
537 unsigned int refs;
539 if (release_lock)
540 RtlReleaseResource(&context_handle->rw_lock);
542 EnterCriticalSection(&assoc->cs);
543 refs = --context_handle->refs;
544 if (!refs)
545 list_remove(&context_handle->entry);
546 LeaveCriticalSection(&assoc->cs);
548 if (!refs)
549 RpcContextHandle_Destroy(context_handle);
551 return refs;