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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/port.h"
43 #include "wine/debug.h"
44 #include "wine/exception.h"
46 #include "rpc_server.h"
48 #include "rpc_message.h"
51 #define MAX_THREADS 128
53 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
55 typedef struct _RpcPacket
57 struct _RpcPacket* next;
58 struct _RpcConnection* conn;
63 typedef struct _RpcObjTypeMap
65 /* FIXME: a hash table would be better. */
66 struct _RpcObjTypeMap *next;
71 static RpcObjTypeMap *RpcObjTypeMaps;
73 static RpcServerProtseq* protseqs;
74 static RpcServerInterface* ifs;
76 static CRITICAL_SECTION server_cs;
77 static CRITICAL_SECTION_DEBUG server_cs_debug =
80 { &server_cs_debug.ProcessLocksList, &server_cs_debug.ProcessLocksList },
81 0, 0, { 0, (DWORD)(__FILE__ ": server_cs") }
83 static CRITICAL_SECTION server_cs = { &server_cs_debug, -1, 0, 0, 0, 0 };
85 static CRITICAL_SECTION listen_cs;
86 static CRITICAL_SECTION_DEBUG listen_cs_debug =
89 { &listen_cs_debug.ProcessLocksList, &listen_cs_debug.ProcessLocksList },
90 0, 0, { 0, (DWORD)(__FILE__ ": listen_cs") }
92 static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };
94 /* whether the server is currently listening */
95 static BOOL std_listen;
96 /* number of manual listeners (calls to RpcServerListen) */
97 static LONG manual_listen_count;
98 /* total listeners including auto listeners */
99 static LONG listen_count;
100 /* set on change of configuration (e.g. listening on new protseq) */
101 static HANDLE mgr_event;
102 /* mutex for ensuring only one thread can change state at a time */
103 static HANDLE mgr_mutex;
104 /* set when server thread has finished opening connections */
105 static HANDLE server_ready_event;
106 /* thread that waits for connections */
107 static HANDLE server_thread;
109 static CRITICAL_SECTION spacket_cs;
110 static CRITICAL_SECTION_DEBUG spacket_cs_debug =
113 { &spacket_cs_debug.ProcessLocksList, &spacket_cs_debug.ProcessLocksList },
114 0, 0, { 0, (DWORD)(__FILE__ ": spacket_cs") }
116 static CRITICAL_SECTION spacket_cs = { &spacket_cs_debug, -1, 0, 0, 0, 0 };
118 static RpcPacket* spacket_head;
119 static RpcPacket* spacket_tail;
120 static HANDLE server_sem;
122 static DWORD worker_count, worker_free, worker_tls;
124 static UUID uuid_nil;
126 inline static RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
128 RpcObjTypeMap *rslt = RpcObjTypeMaps;
132 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
139 inline static UUID *LookupObjType(UUID *ObjUuid)
141 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
148 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
149 RPC_SYNTAX_IDENTIFIER* if_id,
152 UUID* MgrType = NULL;
153 RpcServerInterface* cif = NULL;
157 MgrType = LookupObjType(object);
158 EnterCriticalSection(&server_cs);
161 if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
162 (check_object == FALSE || UuidEqual(MgrType, &cif->MgrTypeUuid, &status)) &&
166 LeaveCriticalSection(&server_cs);
167 TRACE("returning %p for %s\n", cif, debugstr_guid(object));
171 static void RPCRT4_push_packet(RpcPacket* packet)
174 EnterCriticalSection(&spacket_cs);
176 spacket_tail->next = packet;
177 spacket_tail = packet;
179 spacket_head = packet;
180 spacket_tail = packet;
182 LeaveCriticalSection(&spacket_cs);
185 static RpcPacket* RPCRT4_pop_packet(void)
188 EnterCriticalSection(&spacket_cs);
189 packet = spacket_head;
191 spacket_head = packet->next;
192 if (!spacket_head) spacket_tail = NULL;
194 LeaveCriticalSection(&spacket_cs);
195 if (packet) packet->next = NULL;
204 static WINE_EXCEPTION_FILTER(rpc_filter)
208 state = TlsGetValue(worker_tls);
210 if (msg->Buffer != state->buf) I_RpcFreeBuffer(msg);
211 msg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
212 msg->BufferLength = sizeof(DWORD);
214 *(DWORD*)msg->Buffer = GetExceptionCode();
215 WARN("exception caught with code 0x%08lx = %ld\n", *(DWORD*)msg->Buffer, *(DWORD*)msg->Buffer);
216 TRACE("returning failure packet\n");
217 return EXCEPTION_EXECUTE_HANDLER;
220 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr, RPC_MESSAGE* msg)
222 RpcServerInterface* sif;
223 RPC_DISPATCH_FUNCTION func;
227 void *buf = msg->Buffer;
232 TlsSetValue(worker_tls, &state);
234 switch (hdr->common.ptype) {
236 TRACE("got bind packet\n");
238 /* FIXME: do more checks! */
239 if (hdr->bind.max_tsize < RPC_MIN_PACKET_SIZE ||
240 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
241 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
244 sif = RPCRT4_find_interface(NULL, &hdr->bind.abstract, FALSE);
247 TRACE("rejecting bind request on connection %p\n", conn);
248 /* Report failure to client. */
249 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
250 RPC_VER_MAJOR, RPC_VER_MINOR);
252 TRACE("accepting bind request on connection %p\n", conn);
255 response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
259 RESULT_ACCEPT, NO_REASON,
260 &sif->If->TransferSyntax);
262 /* save the interface for later use */
263 conn->ActiveInterface = hdr->bind.abstract;
264 conn->MaxTransmissionSize = hdr->bind.max_tsize;
267 if (RPCRT4_Send(conn, response, NULL, 0) != RPC_S_OK)
273 TRACE("got request packet\n");
275 /* fail if the connection isn't bound with an interface */
276 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
277 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
280 RPCRT4_Send(conn, response, NULL, 0);
284 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
285 object_uuid = (UUID*)(&hdr->request + 1);
290 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, TRUE);
291 msg->RpcInterfaceInformation = sif->If;
292 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
293 msg->ManagerEpv = sif->MgrEpv;
294 if (object_uuid != NULL) {
295 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
298 /* find dispatch function */
299 msg->ProcNum = hdr->request.opnum;
300 if (sif->Flags & RPC_IF_OLE) {
301 /* native ole32 always gives us a dispatch table with a single entry
302 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
303 func = *sif->If->DispatchTable->DispatchTable;
305 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
306 ERR("invalid procnum\n");
309 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
312 /* put in the drep. FIXME: is this more universally applicable?
313 perhaps we should move this outward... */
314 msg->DataRepresentation =
315 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
316 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
321 } __EXCEPT(rpc_filter) {
322 /* failure packet was created in rpc_filter */
325 /* send response packet */
328 msg->RpcInterfaceInformation = NULL;
333 FIXME("unhandled packet type\n");
339 if (msg->Buffer == buf) msg->Buffer = NULL;
340 TRACE("freeing Buffer=%p\n", buf);
341 HeapFree(GetProcessHeap(), 0, buf);
342 RPCRT4_DestroyBinding(msg->Handle);
344 I_RpcFreeBuffer(msg);
346 RPCRT4_FreeHeader(hdr);
347 TlsSetValue(worker_tls, NULL);
350 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
356 /* idle timeout after 5s */
357 obj = WaitForSingleObject(server_sem, 5000);
358 if (obj == WAIT_TIMEOUT) {
359 /* if another idle thread exist, self-destruct */
360 if (worker_free > 1) break;
363 pkt = RPCRT4_pop_packet();
365 InterlockedDecrement(&worker_free);
367 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg);
368 HeapFree(GetProcessHeap(), 0, pkt);
369 /* try to grab another packet here without waiting
370 * on the semaphore, in case it hits max */
371 pkt = RPCRT4_pop_packet();
373 /* decrement semaphore */
374 WaitForSingleObject(server_sem, 0);
376 InterlockedIncrement(&worker_free);
378 InterlockedDecrement(&worker_free);
379 InterlockedDecrement(&worker_count);
383 static void RPCRT4_create_worker_if_needed(void)
385 if (!worker_free && worker_count < MAX_THREADS) {
387 InterlockedIncrement(&worker_count);
388 InterlockedIncrement(&worker_free);
389 thread = CreateThread(NULL, 0, RPCRT4_worker_thread, NULL, 0, NULL);
390 if (thread) CloseHandle(thread);
392 InterlockedDecrement(&worker_free);
393 InterlockedDecrement(&worker_count);
398 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
400 RpcConnection* conn = (RpcConnection*)the_arg;
407 TRACE("(%p)\n", conn);
410 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
412 /* create temporary binding for dispatch, it will be freed in
413 * RPCRT4_process_packet */
414 RPCRT4_MakeBinding(&pbind, conn);
415 msg->Handle = (RPC_BINDING_HANDLE)pbind;
417 status = RPCRT4_Receive(conn, &hdr, msg);
418 if (status != RPC_S_OK) {
419 WARN("receive failed with error %lx\n", status);
424 RPCRT4_process_packet(conn, hdr, msg);
426 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
430 RPCRT4_create_worker_if_needed();
431 RPCRT4_push_packet(packet);
432 ReleaseSemaphore(server_sem, 1, NULL);
436 HeapFree(GetProcessHeap(), 0, msg);
437 RPCRT4_DestroyConnection(conn);
441 static void RPCRT4_new_client(RpcConnection* conn)
443 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
445 DWORD err = GetLastError();
446 ERR("failed to create thread, error=%08lx\n", err);
447 RPCRT4_DestroyConnection(conn);
449 /* we could set conn->thread, but then we'd have to make the io_thread wait
450 * for that, otherwise the thread might finish, destroy the connection, and
451 * free the memory we'd write to before we did, causing crashes and stuff -
452 * so let's implement that later, when we really need conn->thread */
454 CloseHandle( thread );
457 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
459 HANDLE m_event = mgr_event, b_handle;
462 RpcServerProtseq* cps;
464 RpcConnection* cconn;
465 BOOL set_ready_event = FALSE;
467 TRACE("(the_arg == ^%p)\n", the_arg);
470 EnterCriticalSection(&server_cs);
471 /* open and count connections */
477 RPCRT4_OpenConnection(conn);
478 if (conn->ovl.hEvent) count++;
483 /* make array of connections */
485 objs = HeapReAlloc(GetProcessHeap(), 0, objs, count*sizeof(HANDLE));
487 objs = HeapAlloc(GetProcessHeap(), 0, count*sizeof(HANDLE));
495 if (conn->ovl.hEvent) objs[count++] = conn->ovl.hEvent;
500 LeaveCriticalSection(&server_cs);
504 /* signal to function that changed state that we are now sync'ed */
505 SetEvent(server_ready_event);
506 set_ready_event = FALSE;
510 res = WaitForMultipleObjects(count, objs, FALSE, INFINITE);
511 if (res == WAIT_OBJECT_0) {
514 SetEvent(server_ready_event);
517 set_ready_event = TRUE;
519 else if (res == WAIT_FAILED) {
520 ERR("wait failed\n");
523 b_handle = objs[res - WAIT_OBJECT_0];
524 /* find which connection got a RPC */
525 EnterCriticalSection(&server_cs);
531 if (conn->ovl.hEvent == b_handle) break;
538 if (conn) RPCRT4_SpawnConnection(&cconn, conn);
539 LeaveCriticalSection(&server_cs);
541 ERR("failed to locate connection for handle %p\n", b_handle);
543 if (cconn) RPCRT4_new_client(cconn);
546 HeapFree(GetProcessHeap(), 0, objs);
547 EnterCriticalSection(&server_cs);
548 /* close connections */
553 RPCRT4_CloseConnection(conn);
558 LeaveCriticalSection(&server_cs);
562 /* tells the server thread that the state has changed and waits for it to
563 * make the changes */
564 static void RPCRT4_sync_with_server_thread(void)
566 /* make sure we are the only thread sync'ing the server state, otherwise
567 * there is a race with the server thread setting an older state and setting
568 * the server_ready_event when the new state hasn't yet been applied */
569 WaitForSingleObject(mgr_mutex, INFINITE);
572 /* wait for server thread to make the requested changes before returning */
573 WaitForSingleObject(server_ready_event, INFINITE);
575 ReleaseMutex(mgr_mutex);
578 static void RPCRT4_start_listen(BOOL auto_listen)
582 EnterCriticalSection(&listen_cs);
583 if (auto_listen || (manual_listen_count++ == 0))
585 if (++listen_count == 1) {
586 /* first listener creates server thread */
587 if (!mgr_mutex) mgr_mutex = CreateMutexW(NULL, FALSE, NULL);
588 if (!mgr_event) mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
589 if (!server_ready_event) server_ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
590 if (!server_sem) server_sem = CreateSemaphoreW(NULL, 0, MAX_THREADS, NULL);
591 if (!worker_tls) worker_tls = TlsAlloc();
593 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, NULL, 0, NULL);
595 LeaveCriticalSection(&listen_cs);
596 RPCRT4_sync_with_server_thread();
600 LeaveCriticalSection(&listen_cs);
603 static void RPCRT4_stop_listen(BOOL auto_listen)
605 EnterCriticalSection(&listen_cs);
606 if (auto_listen || (--manual_listen_count == 0))
608 if (listen_count != 0 && --listen_count == 0) {
610 LeaveCriticalSection(&listen_cs);
611 RPCRT4_sync_with_server_thread();
614 assert(listen_count >= 0);
616 LeaveCriticalSection(&listen_cs);
619 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps)
621 RPCRT4_CreateConnection(&ps->conn, TRUE, ps->Protseq, NULL, ps->Endpoint, NULL, NULL);
623 EnterCriticalSection(&server_cs);
626 LeaveCriticalSection(&server_cs);
628 if (std_listen) RPCRT4_sync_with_server_thread();
633 /***********************************************************************
634 * RpcServerInqBindings (RPCRT4.@)
636 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
640 RpcServerProtseq* ps;
644 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
646 ERR("(BindingVector == NULL!!?)\n");
648 EnterCriticalSection(&server_cs);
649 /* count connections */
661 /* export bindings */
662 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
663 sizeof(RPC_BINDING_VECTOR) +
664 sizeof(RPC_BINDING_HANDLE)*(count-1));
665 (*BindingVector)->Count = count;
671 RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
680 *BindingVector = NULL;
681 status = RPC_S_NO_BINDINGS;
683 LeaveCriticalSection(&server_cs);
687 /***********************************************************************
688 * RpcServerUseProtseqEpA (RPCRT4.@)
690 RPC_STATUS WINAPI RpcServerUseProtseqEpA( unsigned char *Protseq, UINT MaxCalls, unsigned char *Endpoint, LPVOID SecurityDescriptor )
694 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
696 /* This should provide the default behaviour */
697 policy.Length = sizeof( policy );
698 policy.EndpointFlags = 0;
701 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
704 /***********************************************************************
705 * RpcServerUseProtseqEpW (RPCRT4.@)
707 RPC_STATUS WINAPI RpcServerUseProtseqEpW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor )
711 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
713 /* This should provide the default behaviour */
714 policy.Length = sizeof( policy );
715 policy.EndpointFlags = 0;
718 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
721 /***********************************************************************
722 * RpcServerUseProtseqEpExA (RPCRT4.@)
724 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( unsigned char *Protseq, UINT MaxCalls, unsigned char *Endpoint, LPVOID SecurityDescriptor,
725 PRPC_POLICY lpPolicy )
727 RpcServerProtseq* ps;
729 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_a( Protseq ), MaxCalls,
730 debugstr_a( Endpoint ), SecurityDescriptor,
731 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
733 ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerProtseq));
734 ps->MaxCalls = MaxCalls;
735 ps->Protseq = RPCRT4_strdupA(Protseq);
736 ps->Endpoint = RPCRT4_strdupA(Endpoint);
738 return RPCRT4_use_protseq(ps);
741 /***********************************************************************
742 * RpcServerUseProtseqEpExW (RPCRT4.@)
744 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor,
745 PRPC_POLICY lpPolicy )
747 RpcServerProtseq* ps;
749 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_w( Protseq ), MaxCalls,
750 debugstr_w( Endpoint ), SecurityDescriptor,
751 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
753 ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerProtseq));
754 ps->MaxCalls = MaxCalls;
755 ps->Protseq = RPCRT4_strdupWtoA(Protseq);
756 ps->Endpoint = RPCRT4_strdupWtoA(Endpoint);
758 return RPCRT4_use_protseq(ps);
761 /***********************************************************************
762 * RpcServerUseProtseqA (RPCRT4.@)
764 RPC_STATUS WINAPI RpcServerUseProtseqA(unsigned char *Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
766 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a(Protseq), MaxCalls, SecurityDescriptor);
767 return RpcServerUseProtseqEpA(Protseq, MaxCalls, NULL, SecurityDescriptor);
770 /***********************************************************************
771 * RpcServerUseProtseqW (RPCRT4.@)
773 RPC_STATUS WINAPI RpcServerUseProtseqW(LPWSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
775 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
776 return RpcServerUseProtseqEpW(Protseq, MaxCalls, NULL, SecurityDescriptor);
779 /***********************************************************************
780 * RpcServerRegisterIf (RPCRT4.@)
782 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
784 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
785 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
788 /***********************************************************************
789 * RpcServerRegisterIfEx (RPCRT4.@)
791 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
792 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
794 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
795 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
798 /***********************************************************************
799 * RpcServerRegisterIf2 (RPCRT4.@)
801 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
802 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
804 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
805 RpcServerInterface* sif;
808 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
809 MaxRpcSize, IfCallbackFn);
810 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
811 If->InterfaceId.SyntaxVersion.MajorVersion,
812 If->InterfaceId.SyntaxVersion.MinorVersion);
813 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
814 If->TransferSyntax.SyntaxVersion.MajorVersion,
815 If->TransferSyntax.SyntaxVersion.MinorVersion);
816 TRACE(" dispatch table: %p\n", If->DispatchTable);
817 if (If->DispatchTable) {
818 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
819 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
820 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
822 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
824 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
825 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
826 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
828 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
831 memcpy(&sif->MgrTypeUuid, MgrTypeUuid, sizeof(UUID));
832 sif->MgrEpv = MgrEpv;
834 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
835 sif->MgrEpv = If->DefaultManagerEpv;
838 sif->MaxCalls = MaxCalls;
839 sif->MaxRpcSize = MaxRpcSize;
840 sif->IfCallbackFn = IfCallbackFn;
842 EnterCriticalSection(&server_cs);
845 LeaveCriticalSection(&server_cs);
847 if (sif->Flags & RPC_IF_AUTOLISTEN) {
848 /* well, start listening, I think... */
849 RPCRT4_start_listen(TRUE);
855 /***********************************************************************
856 * RpcServerUnregisterIf (RPCRT4.@)
858 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
860 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, WaitForCallsToComplete == %u): stub\n",
861 IfSpec, debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
866 /***********************************************************************
867 * RpcServerUnregisterIfEx (RPCRT4.@)
869 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
871 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
872 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
877 /***********************************************************************
878 * RpcObjectSetType (RPCRT4.@)
881 * ObjUuid [I] "Object" UUID
882 * TypeUuid [I] "Type" UUID
885 * RPC_S_OK The call succeeded
886 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
887 * RPC_S_ALREADY_REGISTERED The provided object is already registered
889 * Maps "Object" UUIDs to "Type" UUID's. Passing the nil UUID as the type
890 * resets the mapping for the specified object UUID to nil (the default).
891 * The nil object is always associated with the nil type and cannot be
892 * reassigned. Servers can support multiple implementations on the same
893 * interface by registering different end-point vectors for the different
894 * types. There's no need to call this if a server only supports the nil
895 * type, as is typical.
897 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
899 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
902 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
903 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
904 /* nil uuid cannot be remapped */
905 return RPC_S_INVALID_OBJECT;
908 /* find the mapping for this object if there is one ... */
910 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
914 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
915 /* ... and drop it from the list */
918 prev->next = map->next;
920 RpcObjTypeMaps = map->next;
921 HeapFree(GetProcessHeap(), 0, map);
924 /* ... , fail if we found it ... */
926 return RPC_S_ALREADY_REGISTERED;
927 /* ... otherwise create a new one and add it in. */
928 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
929 memcpy(&map->Object, ObjUuid, sizeof(UUID));
930 memcpy(&map->Type, TypeUuid, sizeof(UUID));
933 prev->next = map; /* prev is the last map in the linklist */
935 RpcObjTypeMaps = map;
941 /***********************************************************************
942 * RpcServerRegisterAuthInfoA (RPCRT4.@)
944 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( unsigned char *ServerPrincName, unsigned long AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
947 FIXME( "(%s,%lu,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
949 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
952 /***********************************************************************
953 * RpcServerRegisterAuthInfoW (RPCRT4.@)
955 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( LPWSTR ServerPrincName, unsigned long AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
958 FIXME( "(%s,%lu,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
960 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
963 /***********************************************************************
964 * RpcServerListen (RPCRT4.@)
966 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
968 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
971 return RPC_S_NO_PROTSEQS_REGISTERED;
973 EnterCriticalSection(&listen_cs);
976 LeaveCriticalSection(&listen_cs);
977 return RPC_S_ALREADY_LISTENING;
980 RPCRT4_start_listen(FALSE);
982 LeaveCriticalSection(&listen_cs);
984 if (DontWait) return RPC_S_OK;
986 return RpcMgmtWaitServerListen();
989 /***********************************************************************
990 * RpcMgmtServerWaitListen (RPCRT4.@)
992 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
994 RPC_STATUS rslt = RPC_S_OK;
998 EnterCriticalSection(&listen_cs);
1001 if ( (rslt = RpcServerListen(1, 0, TRUE)) != RPC_S_OK ) {
1002 LeaveCriticalSection(&listen_cs);
1006 LeaveCriticalSection(&listen_cs);
1008 while (std_listen) {
1009 WaitForSingleObject(mgr_event, INFINITE);
1011 Sleep(100); /* don't spin violently */
1012 TRACE("spinning.\n");
1019 /***********************************************************************
1020 * RpcMgmtStopServerListening (RPCRT4.@)
1022 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1024 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1027 FIXME("client-side invocation not implemented.\n");
1028 return RPC_S_WRONG_KIND_OF_BINDING;
1031 RPCRT4_stop_listen(FALSE);
1036 /***********************************************************************
1037 * I_RpcServerStartListening (RPCRT4.@)
1039 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1041 FIXME( "(%p): stub\n", hWnd );
1046 /***********************************************************************
1047 * I_RpcServerStopListening (RPCRT4.@)
1049 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1051 FIXME( "(): stub\n" );
1056 /***********************************************************************
1057 * I_RpcWindowProc (RPCRT4.@)
1059 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1061 FIXME( "(%p,%08x,%08x,%08lx): stub\n", hWnd, Message, wParam, lParam );