4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
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
26 #include "wine/port.h"
42 #include "wine/debug.h"
43 #include "wine/exception.h"
45 #include "rpc_server.h"
47 #include "rpc_message.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
52 typedef struct _RpcPacket
54 struct _RpcConnection* conn;
59 typedef struct _RpcObjTypeMap
61 /* FIXME: a hash table would be better. */
62 struct _RpcObjTypeMap *next;
67 static RpcObjTypeMap *RpcObjTypeMaps;
69 /* list of type RpcServerProtseq */
70 static struct list protseqs = LIST_INIT(protseqs);
71 static struct list server_interfaces = LIST_INIT(server_interfaces);
73 static CRITICAL_SECTION server_cs;
74 static CRITICAL_SECTION_DEBUG server_cs_debug =
77 { &server_cs_debug.ProcessLocksList, &server_cs_debug.ProcessLocksList },
78 0, 0, { (DWORD_PTR)(__FILE__ ": server_cs") }
80 static CRITICAL_SECTION server_cs = { &server_cs_debug, -1, 0, 0, 0, 0 };
82 static CRITICAL_SECTION listen_cs;
83 static CRITICAL_SECTION_DEBUG listen_cs_debug =
86 { &listen_cs_debug.ProcessLocksList, &listen_cs_debug.ProcessLocksList },
87 0, 0, { (DWORD_PTR)(__FILE__ ": listen_cs") }
89 static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };
91 /* whether the server is currently listening */
92 static BOOL std_listen;
93 /* number of manual listeners (calls to RpcServerListen) */
94 static LONG manual_listen_count;
95 /* total listeners including auto listeners */
96 static LONG listen_count;
100 inline static RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
102 RpcObjTypeMap *rslt = RpcObjTypeMaps;
106 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
113 inline static UUID *LookupObjType(UUID *ObjUuid)
115 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
122 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
123 RPC_SYNTAX_IDENTIFIER* if_id,
126 UUID* MgrType = NULL;
127 RpcServerInterface* cif;
131 MgrType = LookupObjType(object);
132 EnterCriticalSection(&server_cs);
133 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
134 if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
135 (check_object == FALSE || UuidEqual(MgrType, &cif->MgrTypeUuid, &status)) &&
137 InterlockedIncrement(&cif->CurrentCalls);
141 LeaveCriticalSection(&server_cs);
142 if (&cif->entry == &server_interfaces) cif = NULL;
143 TRACE("returning %p for %s\n", cif, debugstr_guid(object));
147 static void RPCRT4_release_server_interface(RpcServerInterface *sif)
149 if (!InterlockedDecrement(&sif->CurrentCalls) &&
150 sif->CallsCompletedEvent) {
151 /* sif must have been removed from server_interfaces before
152 * CallsCompletedEvent is set */
153 SetEvent(sif->CallsCompletedEvent);
154 HeapFree(GetProcessHeap(), 0, sif);
158 static WINE_EXCEPTION_FILTER(rpc_filter)
160 WARN("exception caught with code 0x%08x = %d\n", GetExceptionCode(), GetExceptionCode());
161 TRACE("returning failure packet\n");
162 /* catch every exception */
163 return EXCEPTION_EXECUTE_HANDLER;
166 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr, RPC_MESSAGE* msg)
168 RpcServerInterface* sif;
169 RPC_DISPATCH_FUNCTION func;
172 void *buf = msg->Buffer;
175 switch (hdr->common.ptype) {
177 TRACE("got bind packet\n");
179 /* FIXME: do more checks! */
180 if (hdr->bind.max_tsize < RPC_MIN_PACKET_SIZE ||
181 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
182 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
185 sif = RPCRT4_find_interface(NULL, &hdr->bind.abstract, FALSE);
188 TRACE("rejecting bind request on connection %p\n", conn);
189 /* Report failure to client. */
190 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
191 RPC_VER_MAJOR, RPC_VER_MINOR);
193 TRACE("accepting bind request on connection %p for %s\n", conn,
194 debugstr_guid(&hdr->bind.abstract.SyntaxGUID));
197 response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
201 RESULT_ACCEPT, NO_REASON,
202 &sif->If->TransferSyntax);
204 /* save the interface for later use */
205 conn->ActiveInterface = hdr->bind.abstract;
206 conn->MaxTransmissionSize = hdr->bind.max_tsize;
208 RPCRT4_release_server_interface(sif);
211 status = RPCRT4_Send(conn, response, NULL, 0);
212 RPCRT4_FreeHeader(response);
213 if (status != RPC_S_OK)
219 TRACE("got request packet\n");
221 /* fail if the connection isn't bound with an interface */
222 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
223 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
226 RPCRT4_Send(conn, response, NULL, 0);
227 RPCRT4_FreeHeader(response);
231 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
232 object_uuid = (UUID*)(&hdr->request + 1);
237 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, TRUE);
239 /* FIXME: send fault packet? */
242 msg->RpcInterfaceInformation = sif->If;
243 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
244 msg->ManagerEpv = sif->MgrEpv;
245 if (object_uuid != NULL) {
246 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
249 /* find dispatch function */
250 msg->ProcNum = hdr->request.opnum;
251 if (sif->Flags & RPC_IF_OLE) {
252 /* native ole32 always gives us a dispatch table with a single entry
253 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
254 func = *sif->If->DispatchTable->DispatchTable;
256 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
257 ERR("invalid procnum\n");
260 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
263 /* put in the drep. FIXME: is this more universally applicable?
264 perhaps we should move this outward... */
265 msg->DataRepresentation =
266 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
267 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
272 } __EXCEPT(rpc_filter) {
273 if (msg->Buffer != buf) I_RpcFreeBuffer(msg);
274 /* this will cause a failure packet to be sent in I_RpcSend */
275 msg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
276 msg->BufferLength = sizeof(DWORD);
278 *(DWORD*)msg->Buffer = GetExceptionCode();
281 /* send response packet */
284 msg->RpcInterfaceInformation = NULL;
285 RPCRT4_release_server_interface(sif);
290 FIXME("unhandled packet type %u\n", hdr->common.ptype);
296 if (msg->Buffer == buf) msg->Buffer = NULL;
297 TRACE("freeing Buffer=%p\n", buf);
298 HeapFree(GetProcessHeap(), 0, buf);
299 RPCRT4_DestroyBinding(msg->Handle);
301 I_RpcFreeBuffer(msg);
303 RPCRT4_FreeHeader(hdr);
304 HeapFree(GetProcessHeap(), 0, msg);
307 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
309 RpcPacket *pkt = the_arg;
310 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg);
311 HeapFree(GetProcessHeap(), 0, pkt);
315 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
317 RpcConnection* conn = (RpcConnection*)the_arg;
324 TRACE("(%p)\n", conn);
327 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
329 /* create temporary binding for dispatch, it will be freed in
330 * RPCRT4_process_packet */
331 RPCRT4_MakeBinding(&pbind, conn);
332 msg->Handle = (RPC_BINDING_HANDLE)pbind;
334 status = RPCRT4_Receive(conn, &hdr, msg);
335 if (status != RPC_S_OK) {
336 WARN("receive failed with error %lx\n", status);
337 HeapFree(GetProcessHeap(), 0, msg);
342 RPCRT4_process_packet(conn, hdr, msg);
344 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
348 QueueUserWorkItem(RPCRT4_worker_thread, packet, WT_EXECUTELONGFUNCTION);
352 RPCRT4_DestroyConnection(conn);
356 void RPCRT4_new_client(RpcConnection* conn)
358 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
360 DWORD err = GetLastError();
361 ERR("failed to create thread, error=%08x\n", err);
362 RPCRT4_DestroyConnection(conn);
364 /* we could set conn->thread, but then we'd have to make the io_thread wait
365 * for that, otherwise the thread might finish, destroy the connection, and
366 * free the memory we'd write to before we did, causing crashes and stuff -
367 * so let's implement that later, when we really need conn->thread */
369 CloseHandle( thread );
372 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
377 RpcServerProtseq* cps = the_arg;
379 BOOL set_ready_event = FALSE;
381 TRACE("(the_arg == ^%p)\n", the_arg);
384 objs = cps->ops->get_wait_array(cps, objs, &count);
388 /* signal to function that changed state that we are now sync'ed */
389 SetEvent(cps->server_ready_event);
390 set_ready_event = FALSE;
394 res = cps->ops->wait_for_new_connection(cps, count, objs);
401 SetEvent(cps->server_ready_event);
404 set_ready_event = TRUE;
407 cps->ops->free_wait_array(cps, objs);
408 EnterCriticalSection(&cps->cs);
409 /* close connections */
412 RPCRT4_CloseConnection(conn);
415 LeaveCriticalSection(&cps->cs);
419 /* tells the server thread that the state has changed and waits for it to
420 * make the changes */
421 static void RPCRT4_sync_with_server_thread(RpcServerProtseq *ps)
423 /* make sure we are the only thread sync'ing the server state, otherwise
424 * there is a race with the server thread setting an older state and setting
425 * the server_ready_event when the new state hasn't yet been applied */
426 WaitForSingleObject(ps->mgr_mutex, INFINITE);
428 ps->ops->signal_state_changed(ps);
430 /* wait for server thread to make the requested changes before returning */
431 WaitForSingleObject(ps->server_ready_event, INFINITE);
433 ReleaseMutex(ps->mgr_mutex);
436 static RPC_STATUS RPCRT4_start_listen_protseq(RpcServerProtseq *ps, BOOL auto_listen)
438 RPC_STATUS status = RPC_S_OK;
439 HANDLE server_thread;
441 EnterCriticalSection(&listen_cs);
442 if (ps->is_listening) goto done;
444 if (!ps->mgr_mutex) ps->mgr_mutex = CreateMutexW(NULL, FALSE, NULL);
445 if (!ps->server_ready_event) ps->server_ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
446 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, ps, 0, NULL);
449 status = RPC_S_OUT_OF_RESOURCES;
452 ps->is_listening = TRUE;
453 CloseHandle(server_thread);
456 LeaveCriticalSection(&listen_cs);
460 static RPC_STATUS RPCRT4_start_listen(BOOL auto_listen)
462 RPC_STATUS status = RPC_S_ALREADY_LISTENING;
463 RpcServerProtseq *cps;
467 EnterCriticalSection(&listen_cs);
468 if (auto_listen || (manual_listen_count++ == 0))
471 if (++listen_count == 1)
474 LeaveCriticalSection(&listen_cs);
478 EnterCriticalSection(&server_cs);
479 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
481 status = RPCRT4_start_listen_protseq(cps, TRUE);
482 if (status != RPC_S_OK)
485 /* make sure server is actually listening on the interface before
487 RPCRT4_sync_with_server_thread(cps);
489 LeaveCriticalSection(&server_cs);
495 static void RPCRT4_stop_listen(BOOL auto_listen)
497 EnterCriticalSection(&listen_cs);
498 if (auto_listen || (--manual_listen_count == 0))
500 if (listen_count != 0 && --listen_count == 0) {
501 RpcServerProtseq *cps;
504 LeaveCriticalSection(&listen_cs);
506 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
507 RPCRT4_sync_with_server_thread(cps);
511 assert(listen_count >= 0);
513 LeaveCriticalSection(&listen_cs);
516 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps, LPSTR endpoint)
520 status = ps->ops->open_endpoint(ps, endpoint);
521 if (status != RPC_S_OK)
526 status = RPCRT4_start_listen_protseq(ps, FALSE);
527 if (status == RPC_S_OK)
528 RPCRT4_sync_with_server_thread(ps);
534 /***********************************************************************
535 * RpcServerInqBindings (RPCRT4.@)
537 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
541 RpcServerProtseq* ps;
545 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
547 ERR("(BindingVector == NULL!!?)\n");
549 EnterCriticalSection(&server_cs);
550 /* count connections */
552 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
553 EnterCriticalSection(&ps->cs);
559 LeaveCriticalSection(&ps->cs);
562 /* export bindings */
563 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
564 sizeof(RPC_BINDING_VECTOR) +
565 sizeof(RPC_BINDING_HANDLE)*(count-1));
566 (*BindingVector)->Count = count;
568 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
569 EnterCriticalSection(&ps->cs);
572 RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
577 LeaveCriticalSection(&ps->cs);
581 *BindingVector = NULL;
582 status = RPC_S_NO_BINDINGS;
584 LeaveCriticalSection(&server_cs);
588 /***********************************************************************
589 * RpcServerUseProtseqEpA (RPCRT4.@)
591 RPC_STATUS WINAPI RpcServerUseProtseqEpA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor )
595 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
597 /* This should provide the default behaviour */
598 policy.Length = sizeof( policy );
599 policy.EndpointFlags = 0;
602 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
605 /***********************************************************************
606 * RpcServerUseProtseqEpW (RPCRT4.@)
608 RPC_STATUS WINAPI RpcServerUseProtseqEpW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor )
612 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
614 /* This should provide the default behaviour */
615 policy.Length = sizeof( policy );
616 policy.EndpointFlags = 0;
619 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
622 /***********************************************************************
623 * alloc_serverprotoseq (internal)
625 * Must be called with server_cs held.
627 static RPC_STATUS alloc_serverprotoseq(UINT MaxCalls, char *Protseq, RpcServerProtseq **ps)
629 const struct protseq_ops *ops = rpcrt4_get_protseq_ops(Protseq);
633 FIXME("protseq %s not supported\n", debugstr_a(Protseq));
634 return RPC_S_PROTSEQ_NOT_SUPPORTED;
639 return RPC_S_OUT_OF_RESOURCES;
640 (*ps)->MaxCalls = MaxCalls;
641 (*ps)->Protseq = Protseq;
645 InitializeCriticalSection(&(*ps)->cs);
646 (*ps)->is_listening = FALSE;
647 (*ps)->mgr_mutex = NULL;
648 (*ps)->server_ready_event = NULL;
650 list_add_head(&protseqs, &(*ps)->entry);
652 TRACE("new protseq %p created for %s\n", *ps, Protseq);
657 /* Finds a given protseq or creates a new one if one doesn't already exist */
658 static RPC_STATUS RPCRT4_get_or_create_serverprotseq(UINT MaxCalls, char *Protseq, RpcServerProtseq **ps)
661 RpcServerProtseq *cps;
663 EnterCriticalSection(&server_cs);
665 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
666 if (!strcmp(cps->Protseq, Protseq))
668 TRACE("found existing protseq object for %s\n", Protseq);
670 LeaveCriticalSection(&server_cs);
674 status = alloc_serverprotoseq(MaxCalls, Protseq, ps);
676 LeaveCriticalSection(&server_cs);
681 /***********************************************************************
682 * RpcServerUseProtseqEpExA (RPCRT4.@)
684 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor,
685 PRPC_POLICY lpPolicy )
687 char *szps = (char*)Protseq, *szep = (char*)Endpoint;
688 RpcServerProtseq* ps;
691 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_a(szps), MaxCalls,
692 debugstr_a(szep), SecurityDescriptor,
693 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
695 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, RPCRT4_strdupA(szps), &ps);
696 if (status != RPC_S_OK)
699 return RPCRT4_use_protseq(ps, szep);
702 /***********************************************************************
703 * RpcServerUseProtseqEpExW (RPCRT4.@)
705 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor,
706 PRPC_POLICY lpPolicy )
708 RpcServerProtseq* ps;
712 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_w( Protseq ), MaxCalls,
713 debugstr_w( Endpoint ), SecurityDescriptor,
714 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
716 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, RPCRT4_strdupWtoA(Protseq), &ps);
717 if (status != RPC_S_OK)
720 EndpointA = RPCRT4_strdupWtoA(Endpoint);
721 status = RPCRT4_use_protseq(ps, EndpointA);
722 RPCRT4_strfree(EndpointA);
726 /***********************************************************************
727 * RpcServerUseProtseqA (RPCRT4.@)
729 RPC_STATUS WINAPI RpcServerUseProtseqA(RPC_CSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
731 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq), MaxCalls, SecurityDescriptor);
732 return RpcServerUseProtseqEpA(Protseq, MaxCalls, NULL, SecurityDescriptor);
735 /***********************************************************************
736 * RpcServerUseProtseqW (RPCRT4.@)
738 RPC_STATUS WINAPI RpcServerUseProtseqW(RPC_WSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
740 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
741 return RpcServerUseProtseqEpW(Protseq, MaxCalls, NULL, SecurityDescriptor);
744 /***********************************************************************
745 * RpcServerRegisterIf (RPCRT4.@)
747 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
749 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
750 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
753 /***********************************************************************
754 * RpcServerRegisterIfEx (RPCRT4.@)
756 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
757 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
759 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
760 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
763 /***********************************************************************
764 * RpcServerRegisterIf2 (RPCRT4.@)
766 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
767 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
769 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
770 RpcServerInterface* sif;
773 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
774 MaxRpcSize, IfCallbackFn);
775 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
776 If->InterfaceId.SyntaxVersion.MajorVersion,
777 If->InterfaceId.SyntaxVersion.MinorVersion);
778 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
779 If->TransferSyntax.SyntaxVersion.MajorVersion,
780 If->TransferSyntax.SyntaxVersion.MinorVersion);
781 TRACE(" dispatch table: %p\n", If->DispatchTable);
782 if (If->DispatchTable) {
783 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
784 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
785 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
787 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
789 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
790 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
791 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
793 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
796 memcpy(&sif->MgrTypeUuid, MgrTypeUuid, sizeof(UUID));
797 sif->MgrEpv = MgrEpv;
799 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
800 sif->MgrEpv = If->DefaultManagerEpv;
803 sif->MaxCalls = MaxCalls;
804 sif->MaxRpcSize = MaxRpcSize;
805 sif->IfCallbackFn = IfCallbackFn;
807 EnterCriticalSection(&server_cs);
808 list_add_head(&server_interfaces, &sif->entry);
809 LeaveCriticalSection(&server_cs);
811 if (sif->Flags & RPC_IF_AUTOLISTEN)
812 RPCRT4_start_listen(TRUE);
817 /***********************************************************************
818 * RpcServerUnregisterIf (RPCRT4.@)
820 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
822 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
825 BOOL completed = TRUE;
826 RpcServerInterface *cif;
829 TRACE("(IfSpec == (RPC_IF_HANDLE)^%p (%s), MgrTypeUuid == %s, WaitForCallsToComplete == %u)\n",
830 IfSpec, debugstr_guid(&If->InterfaceId.SyntaxGUID), debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
832 EnterCriticalSection(&server_cs);
833 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
834 if ((!IfSpec || !memcmp(&If->InterfaceId, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER))) &&
835 UuidEqual(MgrTypeUuid, &cif->MgrTypeUuid, &status)) {
836 list_remove(&cif->entry);
837 if (cif->CurrentCalls) {
839 if (WaitForCallsToComplete)
840 cif->CallsCompletedEvent = event = CreateEventW(NULL, FALSE, FALSE, NULL);
846 LeaveCriticalSection(&server_cs);
849 ERR("not found for object %s\n", debugstr_guid(MgrTypeUuid));
850 return RPC_S_UNKNOWN_IF;
854 HeapFree(GetProcessHeap(), 0, cif);
856 /* sif will be freed when the last call is completed, so be careful not to
857 * touch that memory here as that could happen before we get here */
858 WaitForSingleObject(event, INFINITE);
865 /***********************************************************************
866 * RpcServerUnregisterIfEx (RPCRT4.@)
868 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
870 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
871 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
876 /***********************************************************************
877 * RpcObjectSetType (RPCRT4.@)
880 * ObjUuid [I] "Object" UUID
881 * TypeUuid [I] "Type" UUID
884 * RPC_S_OK The call succeeded
885 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
886 * RPC_S_ALREADY_REGISTERED The provided object is already registered
888 * Maps "Object" UUIDs to "Type" UUID's. Passing the nil UUID as the type
889 * resets the mapping for the specified object UUID to nil (the default).
890 * The nil object is always associated with the nil type and cannot be
891 * reassigned. Servers can support multiple implementations on the same
892 * interface by registering different end-point vectors for the different
893 * types. There's no need to call this if a server only supports the nil
894 * type, as is typical.
896 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
898 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
901 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
902 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
903 /* nil uuid cannot be remapped */
904 return RPC_S_INVALID_OBJECT;
907 /* find the mapping for this object if there is one ... */
909 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
913 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
914 /* ... and drop it from the list */
917 prev->next = map->next;
919 RpcObjTypeMaps = map->next;
920 HeapFree(GetProcessHeap(), 0, map);
923 /* ... , fail if we found it ... */
925 return RPC_S_ALREADY_REGISTERED;
926 /* ... otherwise create a new one and add it in. */
927 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
928 memcpy(&map->Object, ObjUuid, sizeof(UUID));
929 memcpy(&map->Type, TypeUuid, sizeof(UUID));
932 prev->next = map; /* prev is the last map in the linklist */
934 RpcObjTypeMaps = map;
940 /***********************************************************************
941 * RpcServerRegisterAuthInfoA (RPCRT4.@)
943 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
946 FIXME( "(%s,%u,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
948 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
951 /***********************************************************************
952 * RpcServerRegisterAuthInfoW (RPCRT4.@)
954 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
957 FIXME( "(%s,%u,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
959 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
962 /***********************************************************************
963 * RpcServerListen (RPCRT4.@)
965 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
967 RPC_STATUS status = RPC_S_OK;
969 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
971 if (list_empty(&protseqs))
972 return RPC_S_NO_PROTSEQS_REGISTERED;
974 status = RPCRT4_start_listen(FALSE);
976 if (DontWait || (status != RPC_S_OK)) return status;
978 return RpcMgmtWaitServerListen();
981 /***********************************************************************
982 * RpcMgmtServerWaitListen (RPCRT4.@)
984 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
988 EnterCriticalSection(&listen_cs);
991 LeaveCriticalSection(&listen_cs);
992 return RPC_S_NOT_LISTENING;
995 LeaveCriticalSection(&listen_cs);
997 FIXME("not waiting for server calls to finish\n");
1002 /***********************************************************************
1003 * RpcMgmtStopServerListening (RPCRT4.@)
1005 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1007 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1010 FIXME("client-side invocation not implemented.\n");
1011 return RPC_S_WRONG_KIND_OF_BINDING;
1014 RPCRT4_stop_listen(FALSE);
1019 /***********************************************************************
1020 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
1022 RPC_STATUS WINAPI RpcMgmtEnableIdleCleanup(void)
1024 FIXME("(): stub\n");
1028 /***********************************************************************
1029 * I_RpcServerStartListening (RPCRT4.@)
1031 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1033 FIXME( "(%p): stub\n", hWnd );
1038 /***********************************************************************
1039 * I_RpcServerStopListening (RPCRT4.@)
1041 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1043 FIXME( "(): stub\n" );
1048 /***********************************************************************
1049 * I_RpcWindowProc (RPCRT4.@)
1051 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1053 FIXME( "(%p,%08x,%08x,%08x): stub\n", hWnd, Message, wParam, lParam );
1058 /***********************************************************************
1059 * RpcMgmtInqIfIds (RPCRT4.@)
1061 RPC_STATUS WINAPI RpcMgmtInqIfIds(RPC_BINDING_HANDLE Binding, RPC_IF_ID_VECTOR **IfIdVector)
1063 FIXME("(%p,%p): stub\n", Binding, IfIdVector);
1064 return RPC_S_INVALID_BINDING;
1067 /***********************************************************************
1068 * RpcMgmtEpEltInqBegin (RPCRT4.@)
1070 RPC_STATUS WINAPI RpcMgmtEpEltInqBegin(RPC_BINDING_HANDLE Binding, ULONG InquiryType,
1071 RPC_IF_ID *IfId, ULONG VersOption, UUID *ObjectUuid, RPC_EP_INQ_HANDLE* InquiryContext)
1073 FIXME("(%p,%u,%p,%u,%p,%p): stub\n",
1074 Binding, InquiryType, IfId, VersOption, ObjectUuid, InquiryContext);
1075 return RPC_S_INVALID_BINDING;
1078 /***********************************************************************
1079 * RpcMgmtIsServerListening (RPCRT4.@)
1081 RPC_STATUS WINAPI RpcMgmtIsServerListening(RPC_BINDING_HANDLE Binding)
1083 FIXME("(%p): stub\n", Binding);
1084 return RPC_S_INVALID_BINDING;
1087 /***********************************************************************
1088 * RpcMgmtSetServerStackSize (RPCRT4.@)
1090 RPC_STATUS WINAPI RpcMgmtSetServerStackSize(ULONG ThreadStackSize)
1092 FIXME("(0x%x): stub\n", ThreadStackSize);