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
24 #include "wine/port.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"
46 #include "ncastatus.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
50 typedef struct _RpcPacket
52 struct _RpcConnection* conn;
57 typedef struct _RpcObjTypeMap
59 /* FIXME: a hash table would be better. */
60 struct _RpcObjTypeMap *next;
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 =
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 =
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;
98 static inline RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
100 RpcObjTypeMap *rslt = RpcObjTypeMaps;
104 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
111 static inline UUID *LookupObjType(UUID *ObjUuid)
113 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
120 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
121 const RPC_SYNTAX_IDENTIFIER* if_id,
124 UUID* MgrType = NULL;
125 RpcServerInterface* cif;
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)) &&
135 InterlockedIncrement(&cif->CurrentCalls);
139 LeaveCriticalSection(&server_cs);
140 if (&cif->entry == &server_interfaces) cif = NULL;
141 TRACE("returning %p for %s\n", cif, debugstr_guid(object));
145 static void RPCRT4_release_server_interface(RpcServerInterface *sif)
147 if (!InterlockedDecrement(&sif->CurrentCalls) &&
148 sif->CallsCompletedEvent) {
149 /* sif must have been removed from server_interfaces before
150 * CallsCompletedEvent is set */
151 SetEvent(sif->CallsCompletedEvent);
152 HeapFree(GetProcessHeap(), 0, sif);
156 static WINE_EXCEPTION_FILTER(rpc_filter)
158 WARN("exception caught with code 0x%08x = %d\n", GetExceptionCode(), GetExceptionCode());
159 TRACE("returning failure packet\n");
160 /* catch every exception */
161 return EXCEPTION_EXECUTE_HANDLER;
164 static RPC_STATUS process_bind_packet(RpcConnection *conn, RpcPktBindHdr *hdr, RPC_MESSAGE *msg)
167 RpcServerInterface* sif;
168 RpcPktHdr *response = NULL;
170 /* FIXME: do more checks! */
171 if (hdr->max_tsize < RPC_MIN_PACKET_SIZE ||
172 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status) ||
173 conn->server_binding) {
174 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
177 /* create temporary binding */
178 if (RPCRT4_MakeBinding(&conn->server_binding, conn) == RPC_S_OK &&
179 RpcServerAssoc_GetAssociation(rpcrt4_conn_get_name(conn),
180 conn->NetworkAddr, conn->Endpoint,
181 conn->NetworkOptions,
183 &conn->server_binding->Assoc) == RPC_S_OK)
184 sif = RPCRT4_find_interface(NULL, &hdr->abstract, FALSE);
189 TRACE("rejecting bind request on connection %p\n", conn);
190 /* Report failure to client. */
191 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
192 RPC_VER_MAJOR, RPC_VER_MINOR);
194 TRACE("accepting bind request on connection %p for %s\n", conn,
195 debugstr_guid(&hdr->abstract.SyntaxGUID));
198 response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
201 conn->server_binding->Assoc->assoc_group_id,
203 RESULT_ACCEPT, REASON_NONE,
204 &sif->If->TransferSyntax);
206 /* save the interface for later use */
207 conn->ActiveInterface = hdr->abstract;
208 conn->MaxTransmissionSize = hdr->max_tsize;
210 RPCRT4_release_server_interface(sif);
214 status = RPCRT4_Send(conn, response, NULL, 0);
216 status = ERROR_OUTOFMEMORY;
217 RPCRT4_FreeHeader(response);
222 static RPC_STATUS process_request_packet(RpcConnection *conn, RpcPktRequestHdr *hdr, RPC_MESSAGE *msg)
225 RpcPktHdr *response = NULL;
226 RpcServerInterface* sif;
227 RPC_DISPATCH_FUNCTION func;
230 NDR_SCONTEXT context_handle;
231 void *buf = msg->Buffer;
233 /* fail if the connection isn't bound with an interface */
234 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
235 /* FIXME: should send BindNack instead */
236 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
239 RPCRT4_Send(conn, response, NULL, 0);
240 RPCRT4_FreeHeader(response);
244 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
245 object_uuid = (UUID*)(hdr + 1);
250 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, TRUE);
252 WARN("interface %s no longer registered, returning fault packet\n", debugstr_guid(&conn->ActiveInterface.SyntaxGUID));
253 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
256 RPCRT4_Send(conn, response, NULL, 0);
257 RPCRT4_FreeHeader(response);
260 msg->RpcInterfaceInformation = sif->If;
261 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
262 msg->ManagerEpv = sif->MgrEpv;
263 if (object_uuid != NULL) {
264 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
267 /* find dispatch function */
268 msg->ProcNum = hdr->opnum;
269 if (sif->Flags & RPC_IF_OLE) {
270 /* native ole32 always gives us a dispatch table with a single entry
271 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
272 func = *sif->If->DispatchTable->DispatchTable;
274 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
275 WARN("invalid procnum (%d/%d)\n", msg->ProcNum, sif->If->DispatchTable->DispatchTableCount);
276 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
279 RPCRT4_Send(conn, response, NULL, 0);
280 RPCRT4_FreeHeader(response);
282 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
285 /* put in the drep. FIXME: is this more universally applicable?
286 perhaps we should move this outward... */
287 msg->DataRepresentation =
288 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
289 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
294 RPCRT4_SetThreadCurrentCallHandle(msg->Handle);
297 } __EXCEPT(rpc_filter) {
299 if (GetExceptionCode() == STATUS_ACCESS_VIOLATION)
300 status = ERROR_NOACCESS;
302 status = GetExceptionCode();
303 response = RPCRT4_BuildFaultHeader(msg->DataRepresentation,
304 RPC2NCA_STATUS(status));
306 RPCRT4_SetThreadCurrentCallHandle(NULL);
308 /* release any unmarshalled context handles */
309 while ((context_handle = RPCRT4_PopThreadContextHandle()) != NULL)
310 RpcServerAssoc_ReleaseContextHandle(conn->server_binding->Assoc, context_handle, TRUE);
313 response = RPCRT4_BuildResponseHeader(msg->DataRepresentation,
316 /* send response packet */
318 status = RPCRT4_Send(conn, response, exception ? NULL : msg->Buffer,
319 exception ? 0 : msg->BufferLength);
320 RPCRT4_FreeHeader(response);
322 ERR("out of memory\n");
324 msg->RpcInterfaceInformation = NULL;
325 RPCRT4_release_server_interface(sif);
327 if (msg->Buffer == buf) buf = NULL;
328 TRACE("freeing Buffer=%p\n", buf);
334 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr, RPC_MESSAGE* msg)
338 msg->Handle = (RPC_BINDING_HANDLE)conn->server_binding;
340 switch (hdr->common.ptype) {
342 TRACE("got bind packet\n");
344 status = process_bind_packet(conn, &hdr->bind, msg);
348 TRACE("got request packet\n");
350 status = process_request_packet(conn, &hdr->request, msg);
354 FIXME("unhandled packet type %u\n", hdr->common.ptype);
359 I_RpcFreeBuffer(msg);
360 RPCRT4_FreeHeader(hdr);
361 HeapFree(GetProcessHeap(), 0, msg);
364 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
366 RpcPacket *pkt = the_arg;
367 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg);
368 HeapFree(GetProcessHeap(), 0, pkt);
372 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
374 RpcConnection* conn = (RpcConnection*)the_arg;
380 TRACE("(%p)\n", conn);
383 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
385 status = RPCRT4_Receive(conn, &hdr, msg);
386 if (status != RPC_S_OK) {
387 WARN("receive failed with error %lx\n", status);
388 HeapFree(GetProcessHeap(), 0, msg);
392 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
394 I_RpcFree(msg->Buffer);
395 HeapFree(GetProcessHeap(), 0, msg);
401 if (!QueueUserWorkItem(RPCRT4_worker_thread, packet, WT_EXECUTELONGFUNCTION)) {
402 ERR("couldn't queue work item for worker thread, error was %d\n", GetLastError());
403 I_RpcFree(msg->Buffer);
404 HeapFree(GetProcessHeap(), 0, msg);
405 HeapFree(GetProcessHeap(), 0, packet);
411 RPCRT4_DestroyConnection(conn);
415 void RPCRT4_new_client(RpcConnection* conn)
417 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
419 DWORD err = GetLastError();
420 ERR("failed to create thread, error=%08x\n", err);
421 RPCRT4_DestroyConnection(conn);
423 /* we could set conn->thread, but then we'd have to make the io_thread wait
424 * for that, otherwise the thread might finish, destroy the connection, and
425 * free the memory we'd write to before we did, causing crashes and stuff -
426 * so let's implement that later, when we really need conn->thread */
428 CloseHandle( thread );
431 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
436 RpcServerProtseq* cps = the_arg;
438 BOOL set_ready_event = FALSE;
440 TRACE("(the_arg == ^%p)\n", the_arg);
443 objs = cps->ops->get_wait_array(cps, objs, &count);
447 /* signal to function that changed state that we are now sync'ed */
448 SetEvent(cps->server_ready_event);
449 set_ready_event = FALSE;
453 res = cps->ops->wait_for_new_connection(cps, count, objs);
460 SetEvent(cps->server_ready_event);
463 set_ready_event = TRUE;
466 cps->ops->free_wait_array(cps, objs);
467 EnterCriticalSection(&cps->cs);
468 /* close connections */
471 RPCRT4_CloseConnection(conn);
474 LeaveCriticalSection(&cps->cs);
478 /* tells the server thread that the state has changed and waits for it to
479 * make the changes */
480 static void RPCRT4_sync_with_server_thread(RpcServerProtseq *ps)
482 /* make sure we are the only thread sync'ing the server state, otherwise
483 * there is a race with the server thread setting an older state and setting
484 * the server_ready_event when the new state hasn't yet been applied */
485 WaitForSingleObject(ps->mgr_mutex, INFINITE);
487 ps->ops->signal_state_changed(ps);
489 /* wait for server thread to make the requested changes before returning */
490 WaitForSingleObject(ps->server_ready_event, INFINITE);
492 ReleaseMutex(ps->mgr_mutex);
495 static RPC_STATUS RPCRT4_start_listen_protseq(RpcServerProtseq *ps, BOOL auto_listen)
497 RPC_STATUS status = RPC_S_OK;
498 HANDLE server_thread;
500 EnterCriticalSection(&listen_cs);
501 if (ps->is_listening) goto done;
503 if (!ps->mgr_mutex) ps->mgr_mutex = CreateMutexW(NULL, FALSE, NULL);
504 if (!ps->server_ready_event) ps->server_ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
505 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, ps, 0, NULL);
508 status = RPC_S_OUT_OF_RESOURCES;
511 ps->is_listening = TRUE;
512 CloseHandle(server_thread);
515 LeaveCriticalSection(&listen_cs);
519 static RPC_STATUS RPCRT4_start_listen(BOOL auto_listen)
521 RPC_STATUS status = RPC_S_ALREADY_LISTENING;
522 RpcServerProtseq *cps;
526 EnterCriticalSection(&listen_cs);
527 if (auto_listen || (manual_listen_count++ == 0))
530 if (++listen_count == 1)
533 LeaveCriticalSection(&listen_cs);
537 EnterCriticalSection(&server_cs);
538 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
540 status = RPCRT4_start_listen_protseq(cps, TRUE);
541 if (status != RPC_S_OK)
544 /* make sure server is actually listening on the interface before
546 RPCRT4_sync_with_server_thread(cps);
548 LeaveCriticalSection(&server_cs);
554 static void RPCRT4_stop_listen(BOOL auto_listen)
556 EnterCriticalSection(&listen_cs);
557 if (auto_listen || (--manual_listen_count == 0))
559 if (listen_count != 0 && --listen_count == 0) {
560 RpcServerProtseq *cps;
563 LeaveCriticalSection(&listen_cs);
565 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
566 RPCRT4_sync_with_server_thread(cps);
570 assert(listen_count >= 0);
572 LeaveCriticalSection(&listen_cs);
575 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps, LPSTR endpoint)
579 status = ps->ops->open_endpoint(ps, endpoint);
580 if (status != RPC_S_OK)
585 status = RPCRT4_start_listen_protseq(ps, FALSE);
586 if (status == RPC_S_OK)
587 RPCRT4_sync_with_server_thread(ps);
593 /***********************************************************************
594 * RpcServerInqBindings (RPCRT4.@)
596 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
600 RpcServerProtseq* ps;
604 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
606 ERR("(BindingVector == NULL!!?)\n");
608 EnterCriticalSection(&server_cs);
609 /* count connections */
611 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
612 EnterCriticalSection(&ps->cs);
618 LeaveCriticalSection(&ps->cs);
621 /* export bindings */
622 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
623 sizeof(RPC_BINDING_VECTOR) +
624 sizeof(RPC_BINDING_HANDLE)*(count-1));
625 (*BindingVector)->Count = count;
627 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
628 EnterCriticalSection(&ps->cs);
631 RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
636 LeaveCriticalSection(&ps->cs);
640 *BindingVector = NULL;
641 status = RPC_S_NO_BINDINGS;
643 LeaveCriticalSection(&server_cs);
647 /***********************************************************************
648 * RpcServerUseProtseqEpA (RPCRT4.@)
650 RPC_STATUS WINAPI RpcServerUseProtseqEpA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor )
654 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
656 /* This should provide the default behaviour */
657 policy.Length = sizeof( policy );
658 policy.EndpointFlags = 0;
661 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
664 /***********************************************************************
665 * RpcServerUseProtseqEpW (RPCRT4.@)
667 RPC_STATUS WINAPI RpcServerUseProtseqEpW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor )
671 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
673 /* This should provide the default behaviour */
674 policy.Length = sizeof( policy );
675 policy.EndpointFlags = 0;
678 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
681 /***********************************************************************
682 * alloc_serverprotoseq (internal)
684 * Must be called with server_cs held.
686 static RPC_STATUS alloc_serverprotoseq(UINT MaxCalls, char *Protseq, RpcServerProtseq **ps)
688 const struct protseq_ops *ops = rpcrt4_get_protseq_ops(Protseq);
692 FIXME("protseq %s not supported\n", debugstr_a(Protseq));
693 return RPC_S_PROTSEQ_NOT_SUPPORTED;
698 return RPC_S_OUT_OF_RESOURCES;
699 (*ps)->MaxCalls = MaxCalls;
700 (*ps)->Protseq = Protseq;
704 InitializeCriticalSection(&(*ps)->cs);
705 (*ps)->is_listening = FALSE;
706 (*ps)->mgr_mutex = NULL;
707 (*ps)->server_ready_event = NULL;
709 list_add_head(&protseqs, &(*ps)->entry);
711 TRACE("new protseq %p created for %s\n", *ps, Protseq);
716 /* Finds a given protseq or creates a new one if one doesn't already exist */
717 static RPC_STATUS RPCRT4_get_or_create_serverprotseq(UINT MaxCalls, char *Protseq, RpcServerProtseq **ps)
720 RpcServerProtseq *cps;
722 EnterCriticalSection(&server_cs);
724 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
725 if (!strcmp(cps->Protseq, Protseq))
727 TRACE("found existing protseq object for %s\n", Protseq);
729 LeaveCriticalSection(&server_cs);
733 status = alloc_serverprotoseq(MaxCalls, Protseq, ps);
735 LeaveCriticalSection(&server_cs);
740 /***********************************************************************
741 * RpcServerUseProtseqEpExA (RPCRT4.@)
743 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor,
744 PRPC_POLICY lpPolicy )
746 char *szps = (char*)Protseq, *szep = (char*)Endpoint;
747 RpcServerProtseq* ps;
750 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_a(szps), MaxCalls,
751 debugstr_a(szep), SecurityDescriptor,
752 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
754 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, RPCRT4_strdupA(szps), &ps);
755 if (status != RPC_S_OK)
758 return RPCRT4_use_protseq(ps, szep);
761 /***********************************************************************
762 * RpcServerUseProtseqEpExW (RPCRT4.@)
764 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor,
765 PRPC_POLICY lpPolicy )
767 RpcServerProtseq* ps;
771 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_w( Protseq ), MaxCalls,
772 debugstr_w( Endpoint ), SecurityDescriptor,
773 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
775 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, RPCRT4_strdupWtoA(Protseq), &ps);
776 if (status != RPC_S_OK)
779 EndpointA = RPCRT4_strdupWtoA(Endpoint);
780 status = RPCRT4_use_protseq(ps, EndpointA);
781 RPCRT4_strfree(EndpointA);
785 /***********************************************************************
786 * RpcServerUseProtseqA (RPCRT4.@)
788 RPC_STATUS WINAPI RpcServerUseProtseqA(RPC_CSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
790 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq), MaxCalls, SecurityDescriptor);
791 return RpcServerUseProtseqEpA(Protseq, MaxCalls, NULL, SecurityDescriptor);
794 /***********************************************************************
795 * RpcServerUseProtseqW (RPCRT4.@)
797 RPC_STATUS WINAPI RpcServerUseProtseqW(RPC_WSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
799 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
800 return RpcServerUseProtseqEpW(Protseq, MaxCalls, NULL, SecurityDescriptor);
803 /***********************************************************************
804 * RpcServerRegisterIf (RPCRT4.@)
806 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
808 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
809 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
812 /***********************************************************************
813 * RpcServerRegisterIfEx (RPCRT4.@)
815 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
816 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
818 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
819 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
822 /***********************************************************************
823 * RpcServerRegisterIf2 (RPCRT4.@)
825 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
826 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
828 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
829 RpcServerInterface* sif;
832 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
833 MaxRpcSize, IfCallbackFn);
834 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
835 If->InterfaceId.SyntaxVersion.MajorVersion,
836 If->InterfaceId.SyntaxVersion.MinorVersion);
837 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
838 If->TransferSyntax.SyntaxVersion.MajorVersion,
839 If->TransferSyntax.SyntaxVersion.MinorVersion);
840 TRACE(" dispatch table: %p\n", If->DispatchTable);
841 if (If->DispatchTable) {
842 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
843 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
844 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
846 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
848 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
849 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
850 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
852 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
855 memcpy(&sif->MgrTypeUuid, MgrTypeUuid, sizeof(UUID));
856 sif->MgrEpv = MgrEpv;
858 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
859 sif->MgrEpv = If->DefaultManagerEpv;
862 sif->MaxCalls = MaxCalls;
863 sif->MaxRpcSize = MaxRpcSize;
864 sif->IfCallbackFn = IfCallbackFn;
866 EnterCriticalSection(&server_cs);
867 list_add_head(&server_interfaces, &sif->entry);
868 LeaveCriticalSection(&server_cs);
870 if (sif->Flags & RPC_IF_AUTOLISTEN)
871 RPCRT4_start_listen(TRUE);
876 /***********************************************************************
877 * RpcServerUnregisterIf (RPCRT4.@)
879 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
881 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
884 BOOL completed = TRUE;
885 RpcServerInterface *cif;
888 TRACE("(IfSpec == (RPC_IF_HANDLE)^%p (%s), MgrTypeUuid == %s, WaitForCallsToComplete == %u)\n",
889 IfSpec, debugstr_guid(&If->InterfaceId.SyntaxGUID), debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
891 EnterCriticalSection(&server_cs);
892 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
893 if ((!IfSpec || !memcmp(&If->InterfaceId, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER))) &&
894 UuidEqual(MgrTypeUuid, &cif->MgrTypeUuid, &status)) {
895 list_remove(&cif->entry);
896 if (cif->CurrentCalls) {
898 if (WaitForCallsToComplete)
899 cif->CallsCompletedEvent = event = CreateEventW(NULL, FALSE, FALSE, NULL);
905 LeaveCriticalSection(&server_cs);
908 ERR("not found for object %s\n", debugstr_guid(MgrTypeUuid));
909 return RPC_S_UNKNOWN_IF;
913 HeapFree(GetProcessHeap(), 0, cif);
915 /* sif will be freed when the last call is completed, so be careful not to
916 * touch that memory here as that could happen before we get here */
917 WaitForSingleObject(event, INFINITE);
924 /***********************************************************************
925 * RpcServerUnregisterIfEx (RPCRT4.@)
927 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
929 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
930 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
935 /***********************************************************************
936 * RpcObjectSetType (RPCRT4.@)
939 * ObjUuid [I] "Object" UUID
940 * TypeUuid [I] "Type" UUID
943 * RPC_S_OK The call succeeded
944 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
945 * RPC_S_ALREADY_REGISTERED The provided object is already registered
947 * Maps "Object" UUIDs to "Type" UUID's. Passing the nil UUID as the type
948 * resets the mapping for the specified object UUID to nil (the default).
949 * The nil object is always associated with the nil type and cannot be
950 * reassigned. Servers can support multiple implementations on the same
951 * interface by registering different end-point vectors for the different
952 * types. There's no need to call this if a server only supports the nil
953 * type, as is typical.
955 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
957 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
960 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
961 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
962 /* nil uuid cannot be remapped */
963 return RPC_S_INVALID_OBJECT;
966 /* find the mapping for this object if there is one ... */
968 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
972 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
973 /* ... and drop it from the list */
976 prev->next = map->next;
978 RpcObjTypeMaps = map->next;
979 HeapFree(GetProcessHeap(), 0, map);
982 /* ... , fail if we found it ... */
984 return RPC_S_ALREADY_REGISTERED;
985 /* ... otherwise create a new one and add it in. */
986 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
987 memcpy(&map->Object, ObjUuid, sizeof(UUID));
988 memcpy(&map->Type, TypeUuid, sizeof(UUID));
991 prev->next = map; /* prev is the last map in the linklist */
993 RpcObjTypeMaps = map;
999 /***********************************************************************
1000 * RpcServerRegisterAuthInfoA (RPCRT4.@)
1002 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1005 FIXME( "(%s,%u,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
1007 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
1010 /***********************************************************************
1011 * RpcServerRegisterAuthInfoW (RPCRT4.@)
1013 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1016 FIXME( "(%s,%u,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
1018 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
1021 /***********************************************************************
1022 * RpcServerListen (RPCRT4.@)
1024 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
1026 RPC_STATUS status = RPC_S_OK;
1028 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
1030 if (list_empty(&protseqs))
1031 return RPC_S_NO_PROTSEQS_REGISTERED;
1033 status = RPCRT4_start_listen(FALSE);
1035 if (DontWait || (status != RPC_S_OK)) return status;
1037 return RpcMgmtWaitServerListen();
1040 /***********************************************************************
1041 * RpcMgmtServerWaitListen (RPCRT4.@)
1043 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
1047 EnterCriticalSection(&listen_cs);
1050 LeaveCriticalSection(&listen_cs);
1051 return RPC_S_NOT_LISTENING;
1054 LeaveCriticalSection(&listen_cs);
1056 FIXME("not waiting for server calls to finish\n");
1061 /***********************************************************************
1062 * RpcMgmtStopServerListening (RPCRT4.@)
1064 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1066 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1069 FIXME("client-side invocation not implemented.\n");
1070 return RPC_S_WRONG_KIND_OF_BINDING;
1073 RPCRT4_stop_listen(FALSE);
1078 /***********************************************************************
1079 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
1081 RPC_STATUS WINAPI RpcMgmtEnableIdleCleanup(void)
1083 FIXME("(): stub\n");
1087 /***********************************************************************
1088 * I_RpcServerStartListening (RPCRT4.@)
1090 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1092 FIXME( "(%p): stub\n", hWnd );
1097 /***********************************************************************
1098 * I_RpcServerStopListening (RPCRT4.@)
1100 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1102 FIXME( "(): stub\n" );
1107 /***********************************************************************
1108 * I_RpcWindowProc (RPCRT4.@)
1110 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1112 FIXME( "(%p,%08x,%08x,%08x): stub\n", hWnd, Message, wParam, lParam );
1117 /***********************************************************************
1118 * RpcMgmtInqIfIds (RPCRT4.@)
1120 RPC_STATUS WINAPI RpcMgmtInqIfIds(RPC_BINDING_HANDLE Binding, RPC_IF_ID_VECTOR **IfIdVector)
1122 FIXME("(%p,%p): stub\n", Binding, IfIdVector);
1123 return RPC_S_INVALID_BINDING;
1126 /***********************************************************************
1127 * RpcMgmtEpEltInqBegin (RPCRT4.@)
1129 RPC_STATUS WINAPI RpcMgmtEpEltInqBegin(RPC_BINDING_HANDLE Binding, ULONG InquiryType,
1130 RPC_IF_ID *IfId, ULONG VersOption, UUID *ObjectUuid, RPC_EP_INQ_HANDLE* InquiryContext)
1132 FIXME("(%p,%u,%p,%u,%p,%p): stub\n",
1133 Binding, InquiryType, IfId, VersOption, ObjectUuid, InquiryContext);
1134 return RPC_S_INVALID_BINDING;
1137 /***********************************************************************
1138 * RpcMgmtIsServerListening (RPCRT4.@)
1140 RPC_STATUS WINAPI RpcMgmtIsServerListening(RPC_BINDING_HANDLE Binding)
1142 FIXME("(%p): stub\n", Binding);
1143 return RPC_S_INVALID_BINDING;
1146 /***********************************************************************
1147 * RpcMgmtSetServerStackSize (RPCRT4.@)
1149 RPC_STATUS WINAPI RpcMgmtSetServerStackSize(ULONG ThreadStackSize)
1151 FIXME("(0x%x): stub\n", ThreadStackSize);
1155 /***********************************************************************
1156 * I_RpcGetCurrentCallHandle (RPCRT4.@)
1158 RPC_BINDING_HANDLE WINAPI I_RpcGetCurrentCallHandle(void)
1161 return RPCRT4_GetThreadCurrentCallHandle();