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 #define WIN32_NO_STATUS
34 #include <wine/unicode.h>
35 #include <wine/debug.h>
37 //#include "rpc_binding.h"
38 #include "rpc_assoc.h"
39 #include "rpc_message.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
43 static CRITICAL_SECTION assoc_list_cs
;
44 static CRITICAL_SECTION_DEBUG assoc_list_cs_debug
=
47 { &assoc_list_cs_debug
.ProcessLocksList
, &assoc_list_cs_debug
.ProcessLocksList
},
48 0, 0, { (DWORD_PTR
)(__FILE__
": assoc_list_cs") }
50 static CRITICAL_SECTION assoc_list_cs
= { &assoc_list_cs_debug
, -1, 0, 0, 0, 0 };
52 static struct list client_assoc_list
= LIST_INIT(client_assoc_list
);
53 static struct list server_assoc_list
= LIST_INIT(server_assoc_list
);
55 static LONG last_assoc_group_id
;
57 typedef struct _RpcContextHandle
61 NDR_RUNDOWN rundown_routine
;
68 static void RpcContextHandle_Destroy(RpcContextHandle
*context_handle
);
70 static RPC_STATUS
RpcAssoc_Alloc(LPCSTR Protseq
, LPCSTR NetworkAddr
,
71 LPCSTR Endpoint
, LPCWSTR NetworkOptions
,
75 assoc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*assoc
));
77 return RPC_S_OUT_OF_RESOURCES
;
79 list_init(&assoc
->free_connection_pool
);
80 list_init(&assoc
->context_handle_list
);
81 InitializeCriticalSection(&assoc
->cs
);
82 assoc
->Protseq
= RPCRT4_strdupA(Protseq
);
83 assoc
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
84 assoc
->Endpoint
= RPCRT4_strdupA(Endpoint
);
85 assoc
->NetworkOptions
= NetworkOptions
? RPCRT4_strdupW(NetworkOptions
) : NULL
;
86 assoc
->assoc_group_id
= 0;
87 list_init(&assoc
->entry
);
92 static BOOL
compare_networkoptions(LPCWSTR opts1
, LPCWSTR opts2
)
94 if ((opts1
== NULL
) && (opts2
== NULL
))
96 if ((opts1
== NULL
) || (opts2
== NULL
))
98 return !strcmpW(opts1
, opts2
);
101 RPC_STATUS
RPCRT4_GetAssociation(LPCSTR Protseq
, LPCSTR NetworkAddr
,
102 LPCSTR Endpoint
, LPCWSTR NetworkOptions
,
103 RpcAssoc
**assoc_out
)
108 EnterCriticalSection(&assoc_list_cs
);
109 LIST_FOR_EACH_ENTRY(assoc
, &client_assoc_list
, RpcAssoc
, entry
)
111 if (!strcmp(Protseq
, assoc
->Protseq
) &&
112 !strcmp(NetworkAddr
, assoc
->NetworkAddr
) &&
113 !strcmp(Endpoint
, assoc
->Endpoint
) &&
114 compare_networkoptions(NetworkOptions
, assoc
->NetworkOptions
))
118 LeaveCriticalSection(&assoc_list_cs
);
119 TRACE("using existing assoc %p\n", assoc
);
124 status
= RpcAssoc_Alloc(Protseq
, NetworkAddr
, Endpoint
, NetworkOptions
, &assoc
);
125 if (status
!= RPC_S_OK
)
127 LeaveCriticalSection(&assoc_list_cs
);
130 list_add_head(&client_assoc_list
, &assoc
->entry
);
133 LeaveCriticalSection(&assoc_list_cs
);
135 TRACE("new assoc %p\n", assoc
);
140 RPC_STATUS
RpcServerAssoc_GetAssociation(LPCSTR Protseq
, LPCSTR NetworkAddr
,
141 LPCSTR Endpoint
, LPCWSTR NetworkOptions
,
143 RpcAssoc
**assoc_out
)
148 EnterCriticalSection(&assoc_list_cs
);
151 LIST_FOR_EACH_ENTRY(assoc
, &server_assoc_list
, RpcAssoc
, entry
)
153 /* FIXME: NetworkAddr shouldn't be NULL */
154 if (assoc
->assoc_group_id
== assoc_gid
&&
155 !strcmp(Protseq
, assoc
->Protseq
) &&
156 (!NetworkAddr
|| !assoc
->NetworkAddr
|| !strcmp(NetworkAddr
, assoc
->NetworkAddr
)) &&
157 !strcmp(Endpoint
, assoc
->Endpoint
) &&
158 ((!assoc
->NetworkOptions
== !NetworkOptions
) &&
159 (!NetworkOptions
|| !strcmpW(NetworkOptions
, assoc
->NetworkOptions
))))
163 LeaveCriticalSection(&assoc_list_cs
);
164 TRACE("using existing assoc %p\n", assoc
);
169 LeaveCriticalSection(&assoc_list_cs
);
170 return RPC_S_NO_CONTEXT_AVAILABLE
;
173 status
= RpcAssoc_Alloc(Protseq
, NetworkAddr
, Endpoint
, NetworkOptions
, &assoc
);
174 if (status
!= RPC_S_OK
)
176 LeaveCriticalSection(&assoc_list_cs
);
179 assoc
->assoc_group_id
= InterlockedIncrement(&last_assoc_group_id
);
180 list_add_head(&server_assoc_list
, &assoc
->entry
);
183 LeaveCriticalSection(&assoc_list_cs
);
185 TRACE("new assoc %p\n", assoc
);
190 ULONG
RpcAssoc_Release(RpcAssoc
*assoc
)
194 EnterCriticalSection(&assoc_list_cs
);
195 refs
= --assoc
->refs
;
197 list_remove(&assoc
->entry
);
198 LeaveCriticalSection(&assoc_list_cs
);
202 RpcConnection
*Connection
, *cursor2
;
203 RpcContextHandle
*context_handle
, *context_handle_cursor
;
205 TRACE("destroying assoc %p\n", assoc
);
207 LIST_FOR_EACH_ENTRY_SAFE(Connection
, cursor2
, &assoc
->free_connection_pool
, RpcConnection
, conn_pool_entry
)
209 list_remove(&Connection
->conn_pool_entry
);
210 RPCRT4_DestroyConnection(Connection
);
213 LIST_FOR_EACH_ENTRY_SAFE(context_handle
, context_handle_cursor
, &assoc
->context_handle_list
, RpcContextHandle
, entry
)
214 RpcContextHandle_Destroy(context_handle
);
216 HeapFree(GetProcessHeap(), 0, assoc
->NetworkOptions
);
217 HeapFree(GetProcessHeap(), 0, assoc
->Endpoint
);
218 HeapFree(GetProcessHeap(), 0, assoc
->NetworkAddr
);
219 HeapFree(GetProcessHeap(), 0, assoc
->Protseq
);
221 DeleteCriticalSection(&assoc
->cs
);
223 HeapFree(GetProcessHeap(), 0, assoc
);
229 #define ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment)-1))
231 static RPC_STATUS
RpcAssoc_BindConnection(const RpcAssoc
*assoc
, RpcConnection
*conn
,
232 const RPC_SYNTAX_IDENTIFIER
*InterfaceId
,
233 const RPC_SYNTAX_IDENTIFIER
*TransferSyntax
)
236 RpcPktHdr
*response_hdr
;
239 unsigned char *auth_data
= NULL
;
242 TRACE("sending bind request to server\n");
244 hdr
= RPCRT4_BuildBindHeader(NDR_LOCAL_DATA_REPRESENTATION
,
245 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
,
246 assoc
->assoc_group_id
,
247 InterfaceId
, TransferSyntax
);
249 status
= RPCRT4_Send(conn
, hdr
, NULL
, 0);
250 RPCRT4_FreeHeader(hdr
);
251 if (status
!= RPC_S_OK
)
254 status
= RPCRT4_ReceiveWithAuth(conn
, &response_hdr
, &msg
, &auth_data
, &auth_length
);
255 if (status
!= RPC_S_OK
)
257 ERR("receive failed with error %d\n", status
);
261 switch (response_hdr
->common
.ptype
)
265 RpcAddressString
*server_address
= msg
.Buffer
;
266 if ((msg
.BufferLength
>= FIELD_OFFSET(RpcAddressString
, string
[0])) ||
267 (msg
.BufferLength
>= ROUND_UP(FIELD_OFFSET(RpcAddressString
, string
[server_address
->length
]), 4)))
269 unsigned short remaining
= msg
.BufferLength
-
270 ROUND_UP(FIELD_OFFSET(RpcAddressString
, string
[server_address
->length
]), 4);
271 RpcResultList
*results
= (RpcResultList
*)((ULONG_PTR
)server_address
+
272 ROUND_UP(FIELD_OFFSET(RpcAddressString
, string
[server_address
->length
]), 4));
273 if ((results
->num_results
== 1) &&
274 (remaining
>= FIELD_OFFSET(RpcResultList
, results
[results
->num_results
])))
276 switch (results
->results
[0].result
)
279 /* respond to authorization request */
280 if (auth_length
> sizeof(RpcAuthVerifier
))
281 status
= RPCRT4_ClientConnectionAuth(conn
,
282 auth_data
+ sizeof(RpcAuthVerifier
),
284 if (status
== RPC_S_OK
)
286 conn
->assoc_group_id
= response_hdr
->bind_ack
.assoc_gid
;
287 conn
->MaxTransmissionSize
= response_hdr
->bind_ack
.max_tsize
;
288 conn
->ActiveInterface
= *InterfaceId
;
291 case RESULT_PROVIDER_REJECTION
:
292 switch (results
->results
[0].reason
)
294 case REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED
:
295 ERR("syntax %s, %d.%d not supported\n",
296 debugstr_guid(&InterfaceId
->SyntaxGUID
),
297 InterfaceId
->SyntaxVersion
.MajorVersion
,
298 InterfaceId
->SyntaxVersion
.MinorVersion
);
299 status
= RPC_S_UNKNOWN_IF
;
301 case REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED
:
302 ERR("transfer syntax not supported\n");
303 status
= RPC_S_SERVER_UNAVAILABLE
;
307 status
= RPC_S_CALL_FAILED_DNE
;
310 case RESULT_USER_REJECTION
:
312 ERR("rejection result %d\n", results
->results
[0].result
);
313 status
= RPC_S_CALL_FAILED_DNE
;
318 ERR("incorrect results size\n");
319 status
= RPC_S_CALL_FAILED_DNE
;
324 ERR("bind ack packet too small (%d)\n", msg
.BufferLength
);
325 status
= RPC_S_PROTOCOL_ERROR
;
330 switch (response_hdr
->bind_nack
.reject_reason
)
332 case REJECT_LOCAL_LIMIT_EXCEEDED
:
333 case REJECT_TEMPORARY_CONGESTION
:
334 ERR("server too busy\n");
335 status
= RPC_S_SERVER_TOO_BUSY
;
337 case REJECT_PROTOCOL_VERSION_NOT_SUPPORTED
:
338 ERR("protocol version not supported\n");
339 status
= RPC_S_PROTOCOL_ERROR
;
341 case REJECT_UNKNOWN_AUTHN_SERVICE
:
342 ERR("unknown authentication service\n");
343 status
= RPC_S_UNKNOWN_AUTHN_SERVICE
;
345 case REJECT_INVALID_CHECKSUM
:
346 ERR("invalid checksum\n");
347 status
= ERROR_ACCESS_DENIED
;
350 ERR("rejected bind for reason %d\n", response_hdr
->bind_nack
.reject_reason
);
351 status
= RPC_S_CALL_FAILED_DNE
;
355 ERR("wrong packet type received %d\n", response_hdr
->common
.ptype
);
356 status
= RPC_S_PROTOCOL_ERROR
;
360 I_RpcFree(msg
.Buffer
);
361 RPCRT4_FreeHeader(response_hdr
);
362 HeapFree(GetProcessHeap(), 0, auth_data
);
366 static RpcConnection
*RpcAssoc_GetIdleConnection(RpcAssoc
*assoc
,
367 const RPC_SYNTAX_IDENTIFIER
*InterfaceId
,
368 const RPC_SYNTAX_IDENTIFIER
*TransferSyntax
, const RpcAuthInfo
*AuthInfo
,
369 const RpcQualityOfService
*QOS
)
371 RpcConnection
*Connection
;
372 EnterCriticalSection(&assoc
->cs
);
373 /* try to find a compatible connection from the connection pool */
374 LIST_FOR_EACH_ENTRY(Connection
, &assoc
->free_connection_pool
, RpcConnection
, conn_pool_entry
)
376 if (!memcmp(&Connection
->ActiveInterface
, InterfaceId
,
377 sizeof(RPC_SYNTAX_IDENTIFIER
)) &&
378 RpcAuthInfo_IsEqual(Connection
->AuthInfo
, AuthInfo
) &&
379 RpcQualityOfService_IsEqual(Connection
->QOS
, QOS
))
381 list_remove(&Connection
->conn_pool_entry
);
382 LeaveCriticalSection(&assoc
->cs
);
383 TRACE("got connection from pool %p\n", Connection
);
388 LeaveCriticalSection(&assoc
->cs
);
392 RPC_STATUS
RpcAssoc_GetClientConnection(RpcAssoc
*assoc
,
393 const RPC_SYNTAX_IDENTIFIER
*InterfaceId
,
394 const RPC_SYNTAX_IDENTIFIER
*TransferSyntax
, RpcAuthInfo
*AuthInfo
,
395 RpcQualityOfService
*QOS
, RpcConnection
**Connection
)
397 RpcConnection
*NewConnection
;
400 *Connection
= RpcAssoc_GetIdleConnection(assoc
, InterfaceId
, TransferSyntax
, AuthInfo
, QOS
);
404 /* create a new connection */
405 status
= RPCRT4_CreateConnection(&NewConnection
, FALSE
/* is this a server connection? */,
406 assoc
->Protseq
, assoc
->NetworkAddr
,
407 assoc
->Endpoint
, assoc
->NetworkOptions
,
409 if (status
!= RPC_S_OK
)
412 NewConnection
->assoc
= assoc
;
413 status
= RPCRT4_OpenClientConnection(NewConnection
);
414 if (status
!= RPC_S_OK
)
416 RPCRT4_DestroyConnection(NewConnection
);
420 status
= RpcAssoc_BindConnection(assoc
, NewConnection
, InterfaceId
, TransferSyntax
);
421 if (status
!= RPC_S_OK
)
423 RPCRT4_DestroyConnection(NewConnection
);
427 *Connection
= NewConnection
;
432 void RpcAssoc_ReleaseIdleConnection(RpcAssoc
*assoc
, RpcConnection
*Connection
)
434 assert(!Connection
->server
);
435 Connection
->async_state
= NULL
;
436 EnterCriticalSection(&assoc
->cs
);
437 if (!assoc
->assoc_group_id
) assoc
->assoc_group_id
= Connection
->assoc_group_id
;
438 list_add_head(&assoc
->free_connection_pool
, &Connection
->conn_pool_entry
);
439 LeaveCriticalSection(&assoc
->cs
);
442 RPC_STATUS
RpcServerAssoc_AllocateContextHandle(RpcAssoc
*assoc
, void *CtxGuard
,
443 NDR_SCONTEXT
*SContext
)
445 RpcContextHandle
*context_handle
;
447 context_handle
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*context_handle
));
449 return ERROR_OUTOFMEMORY
;
451 context_handle
->ctx_guard
= CtxGuard
;
452 RtlInitializeResource(&context_handle
->rw_lock
);
453 context_handle
->refs
= 1;
455 /* lock here to mirror unmarshall, so we don't need to special-case the
456 * freeing of a non-marshalled context handle */
457 RtlAcquireResourceExclusive(&context_handle
->rw_lock
, TRUE
);
459 EnterCriticalSection(&assoc
->cs
);
460 list_add_tail(&assoc
->context_handle_list
, &context_handle
->entry
);
461 LeaveCriticalSection(&assoc
->cs
);
463 *SContext
= (NDR_SCONTEXT
)context_handle
;
467 BOOL
RpcContextHandle_IsGuardCorrect(NDR_SCONTEXT SContext
, void *CtxGuard
)
469 RpcContextHandle
*context_handle
= (RpcContextHandle
*)SContext
;
470 return context_handle
->ctx_guard
== CtxGuard
;
473 RPC_STATUS
RpcServerAssoc_FindContextHandle(RpcAssoc
*assoc
, const UUID
*uuid
,
474 void *CtxGuard
, ULONG Flags
, NDR_SCONTEXT
*SContext
)
476 RpcContextHandle
*context_handle
;
478 EnterCriticalSection(&assoc
->cs
);
479 LIST_FOR_EACH_ENTRY(context_handle
, &assoc
->context_handle_list
, RpcContextHandle
, entry
)
481 if (RpcContextHandle_IsGuardCorrect((NDR_SCONTEXT
)context_handle
, CtxGuard
) &&
482 !memcmp(&context_handle
->uuid
, uuid
, sizeof(*uuid
)))
484 *SContext
= (NDR_SCONTEXT
)context_handle
;
485 if (context_handle
->refs
++)
487 LeaveCriticalSection(&assoc
->cs
);
488 TRACE("found %p\n", context_handle
);
489 RtlAcquireResourceExclusive(&context_handle
->rw_lock
, TRUE
);
494 LeaveCriticalSection(&assoc
->cs
);
496 ERR("no context handle found for uuid %s, guard %p\n",
497 debugstr_guid(uuid
), CtxGuard
);
498 return ERROR_INVALID_HANDLE
;
501 RPC_STATUS
RpcServerAssoc_UpdateContextHandle(RpcAssoc
*assoc
,
502 NDR_SCONTEXT SContext
,
504 NDR_RUNDOWN rundown_routine
)
506 RpcContextHandle
*context_handle
= (RpcContextHandle
*)SContext
;
509 if (!RpcContextHandle_IsGuardCorrect((NDR_SCONTEXT
)context_handle
, CtxGuard
))
510 return ERROR_INVALID_HANDLE
;
512 EnterCriticalSection(&assoc
->cs
);
513 if (UuidIsNil(&context_handle
->uuid
, &status
))
515 /* add a ref for the data being valid */
516 context_handle
->refs
++;
517 UuidCreate(&context_handle
->uuid
);
518 context_handle
->rundown_routine
= rundown_routine
;
519 TRACE("allocated uuid %s for context handle %p\n",
520 debugstr_guid(&context_handle
->uuid
), context_handle
);
522 LeaveCriticalSection(&assoc
->cs
);
527 void RpcContextHandle_GetUuid(NDR_SCONTEXT SContext
, UUID
*uuid
)
529 RpcContextHandle
*context_handle
= (RpcContextHandle
*)SContext
;
530 *uuid
= context_handle
->uuid
;
533 static void RpcContextHandle_Destroy(RpcContextHandle
*context_handle
)
535 TRACE("freeing %p\n", context_handle
);
537 if (context_handle
->user_context
&& context_handle
->rundown_routine
)
539 TRACE("calling rundown routine %p with user context %p\n",
540 context_handle
->rundown_routine
, context_handle
->user_context
);
541 context_handle
->rundown_routine(context_handle
->user_context
);
544 RtlDeleteResource(&context_handle
->rw_lock
);
546 HeapFree(GetProcessHeap(), 0, context_handle
);
549 unsigned int RpcServerAssoc_ReleaseContextHandle(RpcAssoc
*assoc
, NDR_SCONTEXT SContext
, BOOL release_lock
)
551 RpcContextHandle
*context_handle
= (RpcContextHandle
*)SContext
;
555 RtlReleaseResource(&context_handle
->rw_lock
);
557 EnterCriticalSection(&assoc
->cs
);
558 refs
= --context_handle
->refs
;
560 list_remove(&context_handle
->entry
);
561 LeaveCriticalSection(&assoc
->cs
);
564 RpcContextHandle_Destroy(context_handle
);