comctl32/tests: Move v6 test initialisation routines into separate file.
[wine/multimedia.git] / dlls / rpcrt4 / rpc_server.c
blob621496590f7820fe075c799ce48427976ae28c23
1 /*
2 * RPC server API
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 * Copyright 2006-2008 Robert Shearman (for CodeWeavers)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
35 #include "rpc.h"
36 #include "rpcndr.h"
37 #include "excpt.h"
39 #include "wine/debug.h"
40 #include "wine/exception.h"
42 #include "rpc_server.h"
43 #include "rpc_assoc.h"
44 #include "rpc_message.h"
45 #include "rpc_defs.h"
46 #include "ncastatus.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
50 typedef struct _RpcPacket
52 struct _RpcConnection* conn;
53 RpcPktHdr* hdr;
54 RPC_MESSAGE* msg;
55 } RpcPacket;
57 typedef struct _RpcObjTypeMap
59 /* FIXME: a hash table would be better. */
60 struct _RpcObjTypeMap *next;
61 UUID Object;
62 UUID Type;
63 } RpcObjTypeMap;
65 static RpcObjTypeMap *RpcObjTypeMaps;
67 /* list of type RpcServerProtseq */
68 static struct list protseqs = LIST_INIT(protseqs);
69 static struct list server_interfaces = LIST_INIT(server_interfaces);
71 static CRITICAL_SECTION server_cs;
72 static CRITICAL_SECTION_DEBUG server_cs_debug =
74 0, 0, &server_cs,
75 { &server_cs_debug.ProcessLocksList, &server_cs_debug.ProcessLocksList },
76 0, 0, { (DWORD_PTR)(__FILE__ ": server_cs") }
78 static CRITICAL_SECTION server_cs = { &server_cs_debug, -1, 0, 0, 0, 0 };
80 static CRITICAL_SECTION listen_cs;
81 static CRITICAL_SECTION_DEBUG listen_cs_debug =
83 0, 0, &listen_cs,
84 { &listen_cs_debug.ProcessLocksList, &listen_cs_debug.ProcessLocksList },
85 0, 0, { (DWORD_PTR)(__FILE__ ": listen_cs") }
87 static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };
89 /* whether the server is currently listening */
90 static BOOL std_listen;
91 /* number of manual listeners (calls to RpcServerListen) */
92 static LONG manual_listen_count;
93 /* total listeners including auto listeners */
94 static LONG listen_count;
96 static UUID uuid_nil;
98 static inline RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
100 RpcObjTypeMap *rslt = RpcObjTypeMaps;
101 RPC_STATUS dummy;
103 while (rslt) {
104 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
105 rslt = rslt->next;
108 return rslt;
111 static inline UUID *LookupObjType(UUID *ObjUuid)
113 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
114 if (map)
115 return &map->Type;
116 else
117 return &uuid_nil;
120 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
121 const RPC_SYNTAX_IDENTIFIER* if_id,
122 BOOL check_object)
124 UUID* MgrType = NULL;
125 RpcServerInterface* cif;
126 RPC_STATUS status;
128 if (check_object)
129 MgrType = LookupObjType(object);
130 EnterCriticalSection(&server_cs);
131 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
132 if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
133 (check_object == FALSE || UuidEqual(MgrType, &cif->MgrTypeUuid, &status)) &&
134 std_listen) {
135 InterlockedIncrement(&cif->CurrentCalls);
136 break;
139 LeaveCriticalSection(&server_cs);
140 if (&cif->entry == &server_interfaces) cif = NULL;
141 TRACE("returning %p for object %s, if_id { %d.%d %s }\n", cif,
142 debugstr_guid(object), if_id->SyntaxVersion.MajorVersion,
143 if_id->SyntaxVersion.MinorVersion, debugstr_guid(&if_id->SyntaxGUID));
144 return cif;
147 static void RPCRT4_release_server_interface(RpcServerInterface *sif)
149 if (!InterlockedDecrement(&sif->CurrentCalls) &&
150 sif->Delete) {
151 /* sif must have been removed from server_interfaces before
152 * CallsCompletedEvent is set */
153 if (sif->CallsCompletedEvent)
154 SetEvent(sif->CallsCompletedEvent);
155 HeapFree(GetProcessHeap(), 0, sif);
159 static RPC_STATUS process_bind_packet(RpcConnection *conn, RpcPktBindHdr *hdr, RPC_MESSAGE *msg)
161 RPC_STATUS status;
162 RpcServerInterface* sif;
163 RpcPktHdr *response = NULL;
165 /* FIXME: do more checks! */
166 if (hdr->max_tsize < RPC_MIN_PACKET_SIZE ||
167 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status) ||
168 conn->server_binding) {
169 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
170 sif = NULL;
171 } else {
172 /* create temporary binding */
173 if (RPCRT4_MakeBinding(&conn->server_binding, conn) == RPC_S_OK &&
174 RpcServerAssoc_GetAssociation(rpcrt4_conn_get_name(conn),
175 conn->NetworkAddr, conn->Endpoint,
176 conn->NetworkOptions,
177 hdr->assoc_gid,
178 &conn->server_binding->Assoc) == RPC_S_OK)
179 sif = RPCRT4_find_interface(NULL, &hdr->abstract, FALSE);
180 else
181 sif = NULL;
183 if (sif == NULL) {
184 TRACE("rejecting bind request on connection %p\n", conn);
185 /* Report failure to client. */
186 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
187 RPC_VER_MAJOR, RPC_VER_MINOR);
188 } else {
189 TRACE("accepting bind request on connection %p for %s\n", conn,
190 debugstr_guid(&hdr->abstract.SyntaxGUID));
192 /* accept. */
193 response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
194 RPC_MAX_PACKET_SIZE,
195 RPC_MAX_PACKET_SIZE,
196 conn->server_binding->Assoc->assoc_group_id,
197 conn->Endpoint,
198 RESULT_ACCEPT, REASON_NONE,
199 &sif->If->TransferSyntax);
201 /* save the interface for later use */
202 conn->ActiveInterface = hdr->abstract;
203 conn->MaxTransmissionSize = hdr->max_tsize;
205 RPCRT4_release_server_interface(sif);
208 if (response)
209 status = RPCRT4_Send(conn, response, NULL, 0);
210 else
211 status = ERROR_OUTOFMEMORY;
212 RPCRT4_FreeHeader(response);
214 return status;
217 static RPC_STATUS process_request_packet(RpcConnection *conn, RpcPktRequestHdr *hdr, RPC_MESSAGE *msg)
219 RPC_STATUS status;
220 RpcPktHdr *response = NULL;
221 RpcServerInterface* sif;
222 RPC_DISPATCH_FUNCTION func;
223 BOOL exception;
224 UUID *object_uuid;
225 NDR_SCONTEXT context_handle;
226 void *buf = msg->Buffer;
228 /* fail if the connection isn't bound with an interface */
229 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
230 /* FIXME: should send BindNack instead */
231 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
232 status);
234 RPCRT4_Send(conn, response, NULL, 0);
235 RPCRT4_FreeHeader(response);
236 return RPC_S_OK;
239 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
240 object_uuid = (UUID*)(hdr + 1);
241 } else {
242 object_uuid = NULL;
245 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, TRUE);
246 if (!sif) {
247 WARN("interface %s no longer registered, returning fault packet\n", debugstr_guid(&conn->ActiveInterface.SyntaxGUID));
248 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
249 NCA_S_UNK_IF);
251 RPCRT4_Send(conn, response, NULL, 0);
252 RPCRT4_FreeHeader(response);
253 return RPC_S_OK;
255 msg->RpcInterfaceInformation = sif->If;
256 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
257 msg->ManagerEpv = sif->MgrEpv;
258 if (object_uuid != NULL) {
259 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
262 /* find dispatch function */
263 msg->ProcNum = hdr->opnum;
264 if (sif->Flags & RPC_IF_OLE) {
265 /* native ole32 always gives us a dispatch table with a single entry
266 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
267 func = *sif->If->DispatchTable->DispatchTable;
268 } else {
269 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
270 WARN("invalid procnum (%d/%d)\n", msg->ProcNum, sif->If->DispatchTable->DispatchTableCount);
271 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
272 NCA_S_OP_RNG_ERROR);
274 RPCRT4_Send(conn, response, NULL, 0);
275 RPCRT4_FreeHeader(response);
277 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
280 /* put in the drep. FIXME: is this more universally applicable?
281 perhaps we should move this outward... */
282 msg->DataRepresentation =
283 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
284 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
286 exception = FALSE;
288 /* dispatch */
289 RPCRT4_SetThreadCurrentCallHandle(msg->Handle);
290 __TRY {
291 if (func) func(msg);
292 } __EXCEPT_ALL {
293 WARN("exception caught with code 0x%08x = %d\n", GetExceptionCode(), GetExceptionCode());
294 exception = TRUE;
295 if (GetExceptionCode() == STATUS_ACCESS_VIOLATION)
296 status = ERROR_NOACCESS;
297 else
298 status = GetExceptionCode();
299 response = RPCRT4_BuildFaultHeader(msg->DataRepresentation,
300 RPC2NCA_STATUS(status));
301 } __ENDTRY
302 RPCRT4_SetThreadCurrentCallHandle(NULL);
304 /* release any unmarshalled context handles */
305 while ((context_handle = RPCRT4_PopThreadContextHandle()) != NULL)
306 RpcServerAssoc_ReleaseContextHandle(conn->server_binding->Assoc, context_handle, TRUE);
308 if (!exception)
309 response = RPCRT4_BuildResponseHeader(msg->DataRepresentation,
310 msg->BufferLength);
312 /* send response packet */
313 if (response) {
314 status = RPCRT4_Send(conn, response, exception ? NULL : msg->Buffer,
315 exception ? 0 : msg->BufferLength);
316 RPCRT4_FreeHeader(response);
317 } else
318 ERR("out of memory\n");
320 msg->RpcInterfaceInformation = NULL;
321 RPCRT4_release_server_interface(sif);
323 if (msg->Buffer == buf) buf = NULL;
324 TRACE("freeing Buffer=%p\n", buf);
325 I_RpcFree(buf);
327 return status;
330 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr, RPC_MESSAGE* msg)
332 RPC_STATUS status;
334 msg->Handle = (RPC_BINDING_HANDLE)conn->server_binding;
336 switch (hdr->common.ptype) {
337 case PKT_BIND:
338 TRACE("got bind packet\n");
340 status = process_bind_packet(conn, &hdr->bind, msg);
341 break;
343 case PKT_REQUEST:
344 TRACE("got request packet\n");
346 status = process_request_packet(conn, &hdr->request, msg);
347 break;
349 default:
350 FIXME("unhandled packet type %u\n", hdr->common.ptype);
351 break;
354 /* clean up */
355 I_RpcFree(msg->Buffer);
356 RPCRT4_FreeHeader(hdr);
357 HeapFree(GetProcessHeap(), 0, msg);
360 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
362 RpcPacket *pkt = the_arg;
363 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg);
364 HeapFree(GetProcessHeap(), 0, pkt);
365 return 0;
368 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
370 RpcConnection* conn = the_arg;
371 RpcPktHdr *hdr;
372 RPC_MESSAGE *msg;
373 RPC_STATUS status;
374 RpcPacket *packet;
376 TRACE("(%p)\n", conn);
378 for (;;) {
379 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
381 status = RPCRT4_Receive(conn, &hdr, msg);
382 if (status != RPC_S_OK) {
383 WARN("receive failed with error %x\n", status);
384 HeapFree(GetProcessHeap(), 0, msg);
385 break;
388 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
389 if (!packet) {
390 I_RpcFree(msg->Buffer);
391 RPCRT4_FreeHeader(hdr);
392 HeapFree(GetProcessHeap(), 0, msg);
393 break;
395 packet->conn = conn;
396 packet->hdr = hdr;
397 packet->msg = msg;
398 if (!QueueUserWorkItem(RPCRT4_worker_thread, packet, WT_EXECUTELONGFUNCTION)) {
399 ERR("couldn't queue work item for worker thread, error was %d\n", GetLastError());
400 I_RpcFree(msg->Buffer);
401 RPCRT4_FreeHeader(hdr);
402 HeapFree(GetProcessHeap(), 0, msg);
403 HeapFree(GetProcessHeap(), 0, packet);
404 break;
407 msg = NULL;
409 RPCRT4_DestroyConnection(conn);
410 return 0;
413 void RPCRT4_new_client(RpcConnection* conn)
415 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
416 if (!thread) {
417 DWORD err = GetLastError();
418 ERR("failed to create thread, error=%08x\n", err);
419 RPCRT4_DestroyConnection(conn);
421 /* we could set conn->thread, but then we'd have to make the io_thread wait
422 * for that, otherwise the thread might finish, destroy the connection, and
423 * free the memory we'd write to before we did, causing crashes and stuff -
424 * so let's implement that later, when we really need conn->thread */
426 CloseHandle( thread );
429 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
431 int res;
432 unsigned int count;
433 void *objs = NULL;
434 RpcServerProtseq* cps = the_arg;
435 RpcConnection* conn;
436 BOOL set_ready_event = FALSE;
438 TRACE("(the_arg == ^%p)\n", the_arg);
440 for (;;) {
441 objs = cps->ops->get_wait_array(cps, objs, &count);
443 if (set_ready_event)
445 /* signal to function that changed state that we are now sync'ed */
446 SetEvent(cps->server_ready_event);
447 set_ready_event = FALSE;
450 /* start waiting */
451 res = cps->ops->wait_for_new_connection(cps, count, objs);
453 if (res == -1 || (res == 0 && !std_listen))
455 /* cleanup */
456 cps->ops->free_wait_array(cps, objs);
457 EnterCriticalSection(&cps->cs);
458 for (conn = cps->conn; conn; conn = conn->Next)
459 RPCRT4_CloseConnection(conn);
460 LeaveCriticalSection(&cps->cs);
462 if (res == 0 && !std_listen)
463 SetEvent(cps->server_ready_event);
464 break;
466 else if (res == 0)
467 set_ready_event = TRUE;
469 return 0;
472 /* tells the server thread that the state has changed and waits for it to
473 * make the changes */
474 static void RPCRT4_sync_with_server_thread(RpcServerProtseq *ps)
476 /* make sure we are the only thread sync'ing the server state, otherwise
477 * there is a race with the server thread setting an older state and setting
478 * the server_ready_event when the new state hasn't yet been applied */
479 WaitForSingleObject(ps->mgr_mutex, INFINITE);
481 ps->ops->signal_state_changed(ps);
483 /* wait for server thread to make the requested changes before returning */
484 WaitForSingleObject(ps->server_ready_event, INFINITE);
486 ReleaseMutex(ps->mgr_mutex);
489 static RPC_STATUS RPCRT4_start_listen_protseq(RpcServerProtseq *ps, BOOL auto_listen)
491 RPC_STATUS status = RPC_S_OK;
492 HANDLE server_thread;
494 EnterCriticalSection(&listen_cs);
495 if (ps->is_listening) goto done;
497 if (!ps->mgr_mutex) ps->mgr_mutex = CreateMutexW(NULL, FALSE, NULL);
498 if (!ps->server_ready_event) ps->server_ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
499 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, ps, 0, NULL);
500 if (!server_thread)
502 status = RPC_S_OUT_OF_RESOURCES;
503 goto done;
505 ps->is_listening = TRUE;
506 CloseHandle(server_thread);
508 done:
509 LeaveCriticalSection(&listen_cs);
510 return status;
513 static RPC_STATUS RPCRT4_start_listen(BOOL auto_listen)
515 RPC_STATUS status = RPC_S_ALREADY_LISTENING;
516 RpcServerProtseq *cps;
518 TRACE("\n");
520 EnterCriticalSection(&listen_cs);
521 if (auto_listen || (manual_listen_count++ == 0))
523 status = RPC_S_OK;
524 if (++listen_count == 1)
525 std_listen = TRUE;
527 LeaveCriticalSection(&listen_cs);
529 if (std_listen)
531 EnterCriticalSection(&server_cs);
532 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
534 status = RPCRT4_start_listen_protseq(cps, TRUE);
535 if (status != RPC_S_OK)
536 break;
538 /* make sure server is actually listening on the interface before
539 * returning */
540 RPCRT4_sync_with_server_thread(cps);
542 LeaveCriticalSection(&server_cs);
545 return status;
548 static void RPCRT4_stop_listen(BOOL auto_listen)
550 EnterCriticalSection(&listen_cs);
551 if (auto_listen || (--manual_listen_count == 0))
553 if (listen_count != 0 && --listen_count == 0) {
554 RpcServerProtseq *cps;
556 std_listen = FALSE;
557 LeaveCriticalSection(&listen_cs);
559 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
560 RPCRT4_sync_with_server_thread(cps);
562 return;
564 assert(listen_count >= 0);
566 LeaveCriticalSection(&listen_cs);
569 static BOOL RPCRT4_protseq_is_endpoint_registered(RpcServerProtseq *protseq, const char *endpoint)
571 RpcConnection *conn;
572 EnterCriticalSection(&protseq->cs);
573 for (conn = protseq->conn; conn; conn = conn->Next)
575 if (!endpoint || !strcmp(endpoint, conn->Endpoint))
576 break;
578 LeaveCriticalSection(&protseq->cs);
579 return (conn != NULL);
582 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps, const char *endpoint)
584 RPC_STATUS status;
586 EnterCriticalSection(&ps->cs);
588 if (RPCRT4_protseq_is_endpoint_registered(ps, endpoint))
589 status = RPC_S_OK;
590 else
591 status = ps->ops->open_endpoint(ps, endpoint);
593 LeaveCriticalSection(&ps->cs);
595 if (status != RPC_S_OK)
596 return status;
598 if (std_listen)
600 status = RPCRT4_start_listen_protseq(ps, FALSE);
601 if (status == RPC_S_OK)
602 RPCRT4_sync_with_server_thread(ps);
605 return status;
608 /***********************************************************************
609 * RpcServerInqBindings (RPCRT4.@)
611 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
613 RPC_STATUS status;
614 DWORD count;
615 RpcServerProtseq* ps;
616 RpcConnection* conn;
618 if (BindingVector)
619 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
620 else
621 ERR("(BindingVector == NULL!!?)\n");
623 EnterCriticalSection(&server_cs);
624 /* count connections */
625 count = 0;
626 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
627 EnterCriticalSection(&ps->cs);
628 for (conn = ps->conn; conn; conn = conn->Next)
629 count++;
630 LeaveCriticalSection(&ps->cs);
632 if (count) {
633 /* export bindings */
634 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
635 sizeof(RPC_BINDING_VECTOR) +
636 sizeof(RPC_BINDING_HANDLE)*(count-1));
637 (*BindingVector)->Count = count;
638 count = 0;
639 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
640 EnterCriticalSection(&ps->cs);
641 for (conn = ps->conn; conn; conn = conn->Next) {
642 RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
643 conn);
644 count++;
646 LeaveCriticalSection(&ps->cs);
648 status = RPC_S_OK;
649 } else {
650 *BindingVector = NULL;
651 status = RPC_S_NO_BINDINGS;
653 LeaveCriticalSection(&server_cs);
654 return status;
657 /***********************************************************************
658 * RpcServerUseProtseqEpA (RPCRT4.@)
660 RPC_STATUS WINAPI RpcServerUseProtseqEpA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor )
662 RPC_POLICY policy;
664 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
666 /* This should provide the default behaviour */
667 policy.Length = sizeof( policy );
668 policy.EndpointFlags = 0;
669 policy.NICFlags = 0;
671 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
674 /***********************************************************************
675 * RpcServerUseProtseqEpW (RPCRT4.@)
677 RPC_STATUS WINAPI RpcServerUseProtseqEpW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor )
679 RPC_POLICY policy;
681 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
683 /* This should provide the default behaviour */
684 policy.Length = sizeof( policy );
685 policy.EndpointFlags = 0;
686 policy.NICFlags = 0;
688 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
691 /***********************************************************************
692 * alloc_serverprotoseq (internal)
694 * Must be called with server_cs held.
696 static RPC_STATUS alloc_serverprotoseq(UINT MaxCalls, const char *Protseq, RpcServerProtseq **ps)
698 const struct protseq_ops *ops = rpcrt4_get_protseq_ops(Protseq);
700 if (!ops)
702 FIXME("protseq %s not supported\n", debugstr_a(Protseq));
703 return RPC_S_PROTSEQ_NOT_SUPPORTED;
706 *ps = ops->alloc();
707 if (!*ps)
708 return RPC_S_OUT_OF_RESOURCES;
709 (*ps)->MaxCalls = MaxCalls;
710 (*ps)->Protseq = RPCRT4_strdupA(Protseq);
711 (*ps)->ops = ops;
712 (*ps)->MaxCalls = 0;
713 (*ps)->conn = NULL;
714 InitializeCriticalSection(&(*ps)->cs);
715 (*ps)->is_listening = FALSE;
716 (*ps)->mgr_mutex = NULL;
717 (*ps)->server_ready_event = NULL;
719 list_add_head(&protseqs, &(*ps)->entry);
721 TRACE("new protseq %p created for %s\n", *ps, Protseq);
723 return RPC_S_OK;
726 /* must be called with server_cs held */
727 static void destroy_serverprotoseq(RpcServerProtseq *ps)
729 RPCRT4_strfree(ps->Protseq);
730 DeleteCriticalSection(&ps->cs);
731 CloseHandle(ps->mgr_mutex);
732 CloseHandle(ps->server_ready_event);
733 list_remove(&ps->entry);
734 HeapFree(GetProcessHeap(), 0, ps);
737 /* Finds a given protseq or creates a new one if one doesn't already exist */
738 static RPC_STATUS RPCRT4_get_or_create_serverprotseq(UINT MaxCalls, const char *Protseq, RpcServerProtseq **ps)
740 RPC_STATUS status;
741 RpcServerProtseq *cps;
743 EnterCriticalSection(&server_cs);
745 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
746 if (!strcmp(cps->Protseq, Protseq))
748 TRACE("found existing protseq object for %s\n", Protseq);
749 *ps = cps;
750 LeaveCriticalSection(&server_cs);
751 return S_OK;
754 status = alloc_serverprotoseq(MaxCalls, Protseq, ps);
756 LeaveCriticalSection(&server_cs);
758 return status;
761 /***********************************************************************
762 * RpcServerUseProtseqEpExA (RPCRT4.@)
764 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor,
765 PRPC_POLICY lpPolicy )
767 RpcServerProtseq* ps;
768 RPC_STATUS status;
770 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_a((const char *)Protseq),
771 MaxCalls, debugstr_a((const char *)Endpoint), SecurityDescriptor,
772 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
774 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
775 if (status != RPC_S_OK)
776 return status;
778 return RPCRT4_use_protseq(ps, (const char *)Endpoint);
781 /***********************************************************************
782 * RpcServerUseProtseqEpExW (RPCRT4.@)
784 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor,
785 PRPC_POLICY lpPolicy )
787 RpcServerProtseq* ps;
788 RPC_STATUS status;
789 LPSTR ProtseqA;
790 LPSTR EndpointA;
792 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_w( Protseq ), MaxCalls,
793 debugstr_w( Endpoint ), SecurityDescriptor,
794 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
796 ProtseqA = RPCRT4_strdupWtoA(Protseq);
797 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, ProtseqA, &ps);
798 RPCRT4_strfree(ProtseqA);
799 if (status != RPC_S_OK)
800 return status;
802 EndpointA = RPCRT4_strdupWtoA(Endpoint);
803 status = RPCRT4_use_protseq(ps, EndpointA);
804 RPCRT4_strfree(EndpointA);
805 return status;
808 /***********************************************************************
809 * RpcServerUseProtseqA (RPCRT4.@)
811 RPC_STATUS WINAPI RpcServerUseProtseqA(RPC_CSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
813 RPC_STATUS status;
814 RpcServerProtseq* ps;
816 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq), MaxCalls, SecurityDescriptor);
818 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
819 if (status != RPC_S_OK)
820 return status;
822 return RPCRT4_use_protseq(ps, NULL);
825 /***********************************************************************
826 * RpcServerUseProtseqW (RPCRT4.@)
828 RPC_STATUS WINAPI RpcServerUseProtseqW(RPC_WSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
830 RPC_STATUS status;
831 RpcServerProtseq* ps;
832 LPSTR ProtseqA;
834 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
836 ProtseqA = RPCRT4_strdupWtoA(Protseq);
837 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, ProtseqA, &ps);
838 RPCRT4_strfree(ProtseqA);
839 if (status != RPC_S_OK)
840 return status;
842 return RPCRT4_use_protseq(ps, NULL);
845 void RPCRT4_destroy_all_protseqs(void)
847 RpcServerProtseq *cps, *cursor2;
849 if (listen_count != 0)
850 std_listen = FALSE;
852 EnterCriticalSection(&server_cs);
853 LIST_FOR_EACH_ENTRY_SAFE(cps, cursor2, &protseqs, RpcServerProtseq, entry)
855 if (listen_count != 0)
856 RPCRT4_sync_with_server_thread(cps);
857 destroy_serverprotoseq(cps);
859 LeaveCriticalSection(&server_cs);
862 /***********************************************************************
863 * RpcServerRegisterIf (RPCRT4.@)
865 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
867 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
868 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
871 /***********************************************************************
872 * RpcServerRegisterIfEx (RPCRT4.@)
874 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
875 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
877 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
878 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
881 /***********************************************************************
882 * RpcServerRegisterIf2 (RPCRT4.@)
884 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
885 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
887 PRPC_SERVER_INTERFACE If = IfSpec;
888 RpcServerInterface* sif;
889 unsigned int i;
891 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
892 MaxRpcSize, IfCallbackFn);
893 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
894 If->InterfaceId.SyntaxVersion.MajorVersion,
895 If->InterfaceId.SyntaxVersion.MinorVersion);
896 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
897 If->TransferSyntax.SyntaxVersion.MajorVersion,
898 If->TransferSyntax.SyntaxVersion.MinorVersion);
899 TRACE(" dispatch table: %p\n", If->DispatchTable);
900 if (If->DispatchTable) {
901 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
902 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
903 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
905 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
907 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
908 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
909 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
911 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
912 sif->If = If;
913 if (MgrTypeUuid) {
914 sif->MgrTypeUuid = *MgrTypeUuid;
915 sif->MgrEpv = MgrEpv;
916 } else {
917 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
918 sif->MgrEpv = If->DefaultManagerEpv;
920 sif->Flags = Flags;
921 sif->MaxCalls = MaxCalls;
922 sif->MaxRpcSize = MaxRpcSize;
923 sif->IfCallbackFn = IfCallbackFn;
925 EnterCriticalSection(&server_cs);
926 list_add_head(&server_interfaces, &sif->entry);
927 LeaveCriticalSection(&server_cs);
929 if (sif->Flags & RPC_IF_AUTOLISTEN)
930 RPCRT4_start_listen(TRUE);
932 return RPC_S_OK;
935 /***********************************************************************
936 * RpcServerUnregisterIf (RPCRT4.@)
938 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
940 PRPC_SERVER_INTERFACE If = IfSpec;
941 HANDLE event = NULL;
942 BOOL found = FALSE;
943 BOOL completed = TRUE;
944 RpcServerInterface *cif;
945 RPC_STATUS status;
947 TRACE("(IfSpec == (RPC_IF_HANDLE)^%p (%s), MgrTypeUuid == %s, WaitForCallsToComplete == %u)\n",
948 IfSpec, debugstr_guid(&If->InterfaceId.SyntaxGUID), debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
950 EnterCriticalSection(&server_cs);
951 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
952 if ((!IfSpec || !memcmp(&If->InterfaceId, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER))) &&
953 UuidEqual(MgrTypeUuid, &cif->MgrTypeUuid, &status)) {
954 list_remove(&cif->entry);
955 TRACE("unregistering cif %p\n", cif);
956 if (cif->CurrentCalls) {
957 completed = FALSE;
958 cif->Delete = TRUE;
959 if (WaitForCallsToComplete)
960 cif->CallsCompletedEvent = event = CreateEventW(NULL, FALSE, FALSE, NULL);
962 found = TRUE;
963 break;
966 LeaveCriticalSection(&server_cs);
968 if (!found) {
969 ERR("not found for object %s\n", debugstr_guid(MgrTypeUuid));
970 return RPC_S_UNKNOWN_IF;
973 if (completed)
974 HeapFree(GetProcessHeap(), 0, cif);
975 else if (event) {
976 /* sif will be freed when the last call is completed, so be careful not to
977 * touch that memory here as that could happen before we get here */
978 WaitForSingleObject(event, INFINITE);
979 CloseHandle(event);
982 return RPC_S_OK;
985 /***********************************************************************
986 * RpcServerUnregisterIfEx (RPCRT4.@)
988 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
990 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
991 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
993 return RPC_S_OK;
996 /***********************************************************************
997 * RpcObjectSetType (RPCRT4.@)
999 * PARAMS
1000 * ObjUuid [I] "Object" UUID
1001 * TypeUuid [I] "Type" UUID
1003 * RETURNS
1004 * RPC_S_OK The call succeeded
1005 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
1006 * RPC_S_ALREADY_REGISTERED The provided object is already registered
1008 * Maps "Object" UUIDs to "Type" UUID's. Passing the nil UUID as the type
1009 * resets the mapping for the specified object UUID to nil (the default).
1010 * The nil object is always associated with the nil type and cannot be
1011 * reassigned. Servers can support multiple implementations on the same
1012 * interface by registering different end-point vectors for the different
1013 * types. There's no need to call this if a server only supports the nil
1014 * type, as is typical.
1016 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
1018 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
1019 RPC_STATUS dummy;
1021 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
1022 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
1023 /* nil uuid cannot be remapped */
1024 return RPC_S_INVALID_OBJECT;
1027 /* find the mapping for this object if there is one ... */
1028 while (map) {
1029 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
1030 prev = map;
1031 map = map->next;
1033 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
1034 /* ... and drop it from the list */
1035 if (map) {
1036 if (prev)
1037 prev->next = map->next;
1038 else
1039 RpcObjTypeMaps = map->next;
1040 HeapFree(GetProcessHeap(), 0, map);
1042 } else {
1043 /* ... , fail if we found it ... */
1044 if (map)
1045 return RPC_S_ALREADY_REGISTERED;
1046 /* ... otherwise create a new one and add it in. */
1047 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
1048 map->Object = *ObjUuid;
1049 map->Type = *TypeUuid;
1050 map->next = NULL;
1051 if (prev)
1052 prev->next = map; /* prev is the last map in the linklist */
1053 else
1054 RpcObjTypeMaps = map;
1057 return RPC_S_OK;
1060 /***********************************************************************
1061 * RpcServerRegisterAuthInfoA (RPCRT4.@)
1063 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1064 LPVOID Arg )
1066 FIXME( "(%s,%u,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
1068 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
1071 /***********************************************************************
1072 * RpcServerRegisterAuthInfoW (RPCRT4.@)
1074 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1075 LPVOID Arg )
1077 FIXME( "(%s,%u,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
1079 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
1082 /***********************************************************************
1083 * RpcServerListen (RPCRT4.@)
1085 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
1087 RPC_STATUS status = RPC_S_OK;
1089 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
1091 if (list_empty(&protseqs))
1092 return RPC_S_NO_PROTSEQS_REGISTERED;
1094 status = RPCRT4_start_listen(FALSE);
1096 if (DontWait || (status != RPC_S_OK)) return status;
1098 return RpcMgmtWaitServerListen();
1101 /***********************************************************************
1102 * RpcMgmtServerWaitListen (RPCRT4.@)
1104 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
1106 TRACE("()\n");
1108 EnterCriticalSection(&listen_cs);
1110 if (!std_listen) {
1111 LeaveCriticalSection(&listen_cs);
1112 return RPC_S_NOT_LISTENING;
1115 LeaveCriticalSection(&listen_cs);
1117 FIXME("not waiting for server calls to finish\n");
1119 return RPC_S_OK;
1122 /***********************************************************************
1123 * RpcMgmtStopServerListening (RPCRT4.@)
1125 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1127 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1129 if (Binding) {
1130 FIXME("client-side invocation not implemented.\n");
1131 return RPC_S_WRONG_KIND_OF_BINDING;
1134 RPCRT4_stop_listen(FALSE);
1136 return RPC_S_OK;
1139 /***********************************************************************
1140 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
1142 RPC_STATUS WINAPI RpcMgmtEnableIdleCleanup(void)
1144 FIXME("(): stub\n");
1145 return RPC_S_OK;
1148 /***********************************************************************
1149 * I_RpcServerStartListening (RPCRT4.@)
1151 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1153 FIXME( "(%p): stub\n", hWnd );
1155 return RPC_S_OK;
1158 /***********************************************************************
1159 * I_RpcServerStopListening (RPCRT4.@)
1161 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1163 FIXME( "(): stub\n" );
1165 return RPC_S_OK;
1168 /***********************************************************************
1169 * I_RpcWindowProc (RPCRT4.@)
1171 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1173 FIXME( "(%p,%08x,%08x,%08x): stub\n", hWnd, Message, wParam, lParam );
1175 return 0;
1178 /***********************************************************************
1179 * RpcMgmtInqIfIds (RPCRT4.@)
1181 RPC_STATUS WINAPI RpcMgmtInqIfIds(RPC_BINDING_HANDLE Binding, RPC_IF_ID_VECTOR **IfIdVector)
1183 FIXME("(%p,%p): stub\n", Binding, IfIdVector);
1184 return RPC_S_INVALID_BINDING;
1187 /***********************************************************************
1188 * RpcMgmtInqStats (RPCRT4.@)
1190 RPC_STATUS WINAPI RpcMgmtInqStats(RPC_BINDING_HANDLE Binding, RPC_STATS_VECTOR **Statistics)
1192 RPC_STATS_VECTOR *stats;
1194 FIXME("(%p,%p)\n", Binding, Statistics);
1196 if ((stats = HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_STATS_VECTOR))))
1198 stats->Count = 1;
1199 stats->Stats[0] = 0;
1200 *Statistics = stats;
1201 return RPC_S_OK;
1203 return RPC_S_OUT_OF_RESOURCES;
1206 /***********************************************************************
1207 * RpcMgmtStatsVectorFree (RPCRT4.@)
1209 RPC_STATUS WINAPI RpcMgmtStatsVectorFree(RPC_STATS_VECTOR **StatsVector)
1211 FIXME("(%p)\n", StatsVector);
1213 if (StatsVector)
1215 HeapFree(GetProcessHeap(), 0, *StatsVector);
1216 *StatsVector = NULL;
1218 return RPC_S_OK;
1221 /***********************************************************************
1222 * RpcMgmtEpEltInqBegin (RPCRT4.@)
1224 RPC_STATUS WINAPI RpcMgmtEpEltInqBegin(RPC_BINDING_HANDLE Binding, ULONG InquiryType,
1225 RPC_IF_ID *IfId, ULONG VersOption, UUID *ObjectUuid, RPC_EP_INQ_HANDLE* InquiryContext)
1227 FIXME("(%p,%u,%p,%u,%p,%p): stub\n",
1228 Binding, InquiryType, IfId, VersOption, ObjectUuid, InquiryContext);
1229 return RPC_S_INVALID_BINDING;
1232 /***********************************************************************
1233 * RpcMgmtIsServerListening (RPCRT4.@)
1235 RPC_STATUS WINAPI RpcMgmtIsServerListening(RPC_BINDING_HANDLE Binding)
1237 FIXME("(%p): stub\n", Binding);
1238 return RPC_S_INVALID_BINDING;
1241 /***********************************************************************
1242 * RpcMgmtSetServerStackSize (RPCRT4.@)
1244 RPC_STATUS WINAPI RpcMgmtSetServerStackSize(ULONG ThreadStackSize)
1246 FIXME("(0x%x): stub\n", ThreadStackSize);
1247 return RPC_S_OK;
1250 /***********************************************************************
1251 * I_RpcGetCurrentCallHandle (RPCRT4.@)
1253 RPC_BINDING_HANDLE WINAPI I_RpcGetCurrentCallHandle(void)
1255 TRACE("\n");
1256 return RPCRT4_GetThreadCurrentCallHandle();