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,
122 const RPC_SYNTAX_IDENTIFIER *transfer_syntax,
125 UUID* MgrType = NULL;
126 RpcServerInterface* cif;
130 MgrType = LookupObjType(object);
131 EnterCriticalSection(&server_cs);
132 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
133 if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
134 (!transfer_syntax || !memcmp(transfer_syntax, &cif->If->TransferSyntax, 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 object %s, if_id { %d.%d %s }\n", cif,
144 debugstr_guid(object), if_id->SyntaxVersion.MajorVersion,
145 if_id->SyntaxVersion.MinorVersion, debugstr_guid(&if_id->SyntaxGUID));
149 static void RPCRT4_release_server_interface(RpcServerInterface *sif)
151 if (!InterlockedDecrement(&sif->CurrentCalls) &&
153 /* sif must have been removed from server_interfaces before
154 * CallsCompletedEvent is set */
155 if (sif->CallsCompletedEvent)
156 SetEvent(sif->CallsCompletedEvent);
157 HeapFree(GetProcessHeap(), 0, sif);
161 static RPC_STATUS process_bind_packet(RpcConnection *conn, RpcPktBindHdr *hdr, RPC_MESSAGE *msg)
165 RpcContextElement *ctxt_elem;
168 for (i = 0, ctxt_elem = msg->Buffer;
169 i < hdr->num_elements;
170 i++, ctxt_elem = (RpcContextElement *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes])
172 if (((char *)ctxt_elem - (char *)msg->Buffer) > msg->BufferLength ||
173 ((char *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes] - (char *)msg->Buffer) > msg->BufferLength)
175 ERR("inconsistent data in packet - packet length %d, num elements %d\n",
176 msg->BufferLength, hdr->num_elements);
177 /* Report failure to client. */
178 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
179 RPC_VER_MAJOR, RPC_VER_MINOR,
180 REJECT_REASON_NOT_SPECIFIED);
185 if (hdr->max_tsize < RPC_MIN_PACKET_SIZE ||
186 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status) ||
187 conn->server_binding)
189 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
191 /* Report failure to client. */
192 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
193 RPC_VER_MAJOR, RPC_VER_MINOR,
194 REJECT_REASON_NOT_SPECIFIED);
198 RpcResult *results = HeapAlloc(GetProcessHeap(), 0,
199 hdr->num_elements * sizeof(*results));
202 /* Report failure to client. */
203 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
204 RPC_VER_MAJOR, RPC_VER_MINOR,
205 REJECT_LOCAL_LIMIT_EXCEEDED);
209 for (i = 0, ctxt_elem = (RpcContextElement *)msg->Buffer;
210 i < hdr->num_elements;
211 i++, ctxt_elem = (RpcContextElement *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes])
213 RpcServerInterface* sif = NULL;
216 for (j = 0; !sif && j < ctxt_elem->num_syntaxes; j++)
218 sif = RPCRT4_find_interface(NULL, &ctxt_elem->abstract_syntax,
219 &ctxt_elem->transfer_syntaxes[j], FALSE);
226 RPCRT4_release_server_interface(sif);
227 TRACE("accepting bind request on connection %p for %s\n", conn,
228 debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
229 results[i].result = RESULT_ACCEPT;
230 results[i].reason = REASON_NONE;
231 results[i].transfer_syntax = ctxt_elem->transfer_syntaxes[j];
233 /* save the interface for later use */
234 /* FIXME: save linked list */
235 conn->ActiveInterface = ctxt_elem->abstract_syntax;
237 else if ((sif = RPCRT4_find_interface(NULL, &ctxt_elem->abstract_syntax,
238 NULL, FALSE)) != NULL)
240 RPCRT4_release_server_interface(sif);
241 TRACE("not accepting bind request on connection %p for %s - no transfer syntaxes supported\n",
242 conn, debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
243 results[i].result = RESULT_PROVIDER_REJECTION;
244 results[i].reason = REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED;
245 memset(&results[i].transfer_syntax, 0, sizeof(results[i].transfer_syntax));
249 TRACE("not accepting bind request on connection %p for %s - abstract syntax not supported\n",
250 conn, debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
251 results[i].result = RESULT_PROVIDER_REJECTION;
252 results[i].reason = REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED;
253 memset(&results[i].transfer_syntax, 0, sizeof(results[i].transfer_syntax));
257 /* create temporary binding */
258 if (RPCRT4_MakeBinding(&conn->server_binding, conn) == RPC_S_OK &&
259 RpcServerAssoc_GetAssociation(rpcrt4_conn_get_name(conn),
260 conn->NetworkAddr, conn->Endpoint,
261 conn->NetworkOptions,
263 &conn->server_binding->Assoc) == RPC_S_OK)
265 response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
268 conn->server_binding->Assoc->assoc_group_id,
269 conn->Endpoint, hdr->num_elements,
272 conn->MaxTransmissionSize = hdr->max_tsize;
276 /* Report failure to client. */
277 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
278 RPC_VER_MAJOR, RPC_VER_MINOR,
279 REJECT_LOCAL_LIMIT_EXCEEDED);
281 HeapFree(GetProcessHeap(), 0, results);
286 status = RPCRT4_Send(conn, response, NULL, 0);
288 status = ERROR_OUTOFMEMORY;
289 RPCRT4_FreeHeader(response);
294 static RPC_STATUS process_request_packet(RpcConnection *conn, RpcPktRequestHdr *hdr, RPC_MESSAGE *msg)
297 RpcPktHdr *response = NULL;
298 RpcServerInterface* sif;
299 RPC_DISPATCH_FUNCTION func;
302 NDR_SCONTEXT context_handle;
303 void *buf = msg->Buffer;
305 /* fail if the connection isn't bound with an interface */
306 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
307 /* FIXME: should send BindNack instead */
308 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
311 RPCRT4_Send(conn, response, NULL, 0);
312 RPCRT4_FreeHeader(response);
316 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
317 object_uuid = (UUID*)(hdr + 1);
322 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, NULL, TRUE);
324 WARN("interface %s no longer registered, returning fault packet\n", debugstr_guid(&conn->ActiveInterface.SyntaxGUID));
325 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
328 RPCRT4_Send(conn, response, NULL, 0);
329 RPCRT4_FreeHeader(response);
332 msg->RpcInterfaceInformation = sif->If;
333 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
334 msg->ManagerEpv = sif->MgrEpv;
335 if (object_uuid != NULL) {
336 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
339 /* find dispatch function */
340 msg->ProcNum = hdr->opnum;
341 if (sif->Flags & RPC_IF_OLE) {
342 /* native ole32 always gives us a dispatch table with a single entry
343 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
344 func = *sif->If->DispatchTable->DispatchTable;
346 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
347 WARN("invalid procnum (%d/%d)\n", msg->ProcNum, sif->If->DispatchTable->DispatchTableCount);
348 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
351 RPCRT4_Send(conn, response, NULL, 0);
352 RPCRT4_FreeHeader(response);
354 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
357 /* put in the drep. FIXME: is this more universally applicable?
358 perhaps we should move this outward... */
359 msg->DataRepresentation =
360 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
361 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
366 RPCRT4_SetThreadCurrentCallHandle(msg->Handle);
370 WARN("exception caught with code 0x%08x = %d\n", GetExceptionCode(), GetExceptionCode());
372 if (GetExceptionCode() == STATUS_ACCESS_VIOLATION)
373 status = ERROR_NOACCESS;
375 status = GetExceptionCode();
376 response = RPCRT4_BuildFaultHeader(msg->DataRepresentation,
377 RPC2NCA_STATUS(status));
379 RPCRT4_SetThreadCurrentCallHandle(NULL);
381 /* release any unmarshalled context handles */
382 while ((context_handle = RPCRT4_PopThreadContextHandle()) != NULL)
383 RpcServerAssoc_ReleaseContextHandle(conn->server_binding->Assoc, context_handle, TRUE);
386 response = RPCRT4_BuildResponseHeader(msg->DataRepresentation,
389 /* send response packet */
391 status = RPCRT4_Send(conn, response, exception ? NULL : msg->Buffer,
392 exception ? 0 : msg->BufferLength);
393 RPCRT4_FreeHeader(response);
395 ERR("out of memory\n");
397 msg->RpcInterfaceInformation = NULL;
398 RPCRT4_release_server_interface(sif);
400 if (msg->Buffer == buf) buf = NULL;
401 TRACE("freeing Buffer=%p\n", buf);
407 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr, RPC_MESSAGE* msg)
411 msg->Handle = (RPC_BINDING_HANDLE)conn->server_binding;
413 switch (hdr->common.ptype) {
415 TRACE("got bind packet\n");
417 status = process_bind_packet(conn, &hdr->bind, msg);
421 TRACE("got request packet\n");
423 status = process_request_packet(conn, &hdr->request, msg);
427 FIXME("unhandled packet type %u\n", hdr->common.ptype);
432 I_RpcFree(msg->Buffer);
433 RPCRT4_FreeHeader(hdr);
434 HeapFree(GetProcessHeap(), 0, msg);
437 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
439 RpcPacket *pkt = the_arg;
440 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg);
441 HeapFree(GetProcessHeap(), 0, pkt);
445 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
447 RpcConnection* conn = the_arg;
453 TRACE("(%p)\n", conn);
456 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
458 status = RPCRT4_Receive(conn, &hdr, msg);
459 if (status != RPC_S_OK) {
460 WARN("receive failed with error %x\n", status);
461 HeapFree(GetProcessHeap(), 0, msg);
465 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
467 I_RpcFree(msg->Buffer);
468 RPCRT4_FreeHeader(hdr);
469 HeapFree(GetProcessHeap(), 0, msg);
475 if (!QueueUserWorkItem(RPCRT4_worker_thread, packet, WT_EXECUTELONGFUNCTION)) {
476 ERR("couldn't queue work item for worker thread, error was %d\n", GetLastError());
477 I_RpcFree(msg->Buffer);
478 RPCRT4_FreeHeader(hdr);
479 HeapFree(GetProcessHeap(), 0, msg);
480 HeapFree(GetProcessHeap(), 0, packet);
486 RPCRT4_DestroyConnection(conn);
490 void RPCRT4_new_client(RpcConnection* conn)
492 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
494 DWORD err = GetLastError();
495 ERR("failed to create thread, error=%08x\n", err);
496 RPCRT4_DestroyConnection(conn);
498 /* we could set conn->thread, but then we'd have to make the io_thread wait
499 * for that, otherwise the thread might finish, destroy the connection, and
500 * free the memory we'd write to before we did, causing crashes and stuff -
501 * so let's implement that later, when we really need conn->thread */
503 CloseHandle( thread );
506 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
511 RpcServerProtseq* cps = the_arg;
513 BOOL set_ready_event = FALSE;
515 TRACE("(the_arg == ^%p)\n", the_arg);
518 objs = cps->ops->get_wait_array(cps, objs, &count);
522 /* signal to function that changed state that we are now sync'ed */
523 SetEvent(cps->server_ready_event);
524 set_ready_event = FALSE;
528 res = cps->ops->wait_for_new_connection(cps, count, objs);
530 if (res == -1 || (res == 0 && !std_listen))
533 cps->ops->free_wait_array(cps, objs);
534 EnterCriticalSection(&cps->cs);
535 for (conn = cps->conn; conn; conn = conn->Next)
536 RPCRT4_CloseConnection(conn);
537 LeaveCriticalSection(&cps->cs);
539 if (res == 0 && !std_listen)
540 SetEvent(cps->server_ready_event);
544 set_ready_event = TRUE;
549 /* tells the server thread that the state has changed and waits for it to
550 * make the changes */
551 static void RPCRT4_sync_with_server_thread(RpcServerProtseq *ps)
553 /* make sure we are the only thread sync'ing the server state, otherwise
554 * there is a race with the server thread setting an older state and setting
555 * the server_ready_event when the new state hasn't yet been applied */
556 WaitForSingleObject(ps->mgr_mutex, INFINITE);
558 ps->ops->signal_state_changed(ps);
560 /* wait for server thread to make the requested changes before returning */
561 WaitForSingleObject(ps->server_ready_event, INFINITE);
563 ReleaseMutex(ps->mgr_mutex);
566 static RPC_STATUS RPCRT4_start_listen_protseq(RpcServerProtseq *ps, BOOL auto_listen)
568 RPC_STATUS status = RPC_S_OK;
569 HANDLE server_thread;
571 EnterCriticalSection(&listen_cs);
572 if (ps->is_listening) goto done;
574 if (!ps->mgr_mutex) ps->mgr_mutex = CreateMutexW(NULL, FALSE, NULL);
575 if (!ps->server_ready_event) ps->server_ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
576 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, ps, 0, NULL);
579 status = RPC_S_OUT_OF_RESOURCES;
582 ps->is_listening = TRUE;
583 CloseHandle(server_thread);
586 LeaveCriticalSection(&listen_cs);
590 static RPC_STATUS RPCRT4_start_listen(BOOL auto_listen)
592 RPC_STATUS status = RPC_S_ALREADY_LISTENING;
593 RpcServerProtseq *cps;
597 EnterCriticalSection(&listen_cs);
598 if (auto_listen || (manual_listen_count++ == 0))
601 if (++listen_count == 1)
604 LeaveCriticalSection(&listen_cs);
608 EnterCriticalSection(&server_cs);
609 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
611 status = RPCRT4_start_listen_protseq(cps, TRUE);
612 if (status != RPC_S_OK)
615 /* make sure server is actually listening on the interface before
617 RPCRT4_sync_with_server_thread(cps);
619 LeaveCriticalSection(&server_cs);
625 static void RPCRT4_stop_listen(BOOL auto_listen)
627 EnterCriticalSection(&listen_cs);
628 if (auto_listen || (--manual_listen_count == 0))
630 if (listen_count != 0 && --listen_count == 0) {
631 RpcServerProtseq *cps;
634 LeaveCriticalSection(&listen_cs);
636 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
637 RPCRT4_sync_with_server_thread(cps);
641 assert(listen_count >= 0);
643 LeaveCriticalSection(&listen_cs);
646 static BOOL RPCRT4_protseq_is_endpoint_registered(RpcServerProtseq *protseq, const char *endpoint)
649 EnterCriticalSection(&protseq->cs);
650 for (conn = protseq->conn; conn; conn = conn->Next)
652 if (!endpoint || !strcmp(endpoint, conn->Endpoint))
655 LeaveCriticalSection(&protseq->cs);
656 return (conn != NULL);
659 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps, const char *endpoint)
663 EnterCriticalSection(&ps->cs);
665 if (RPCRT4_protseq_is_endpoint_registered(ps, endpoint))
668 status = ps->ops->open_endpoint(ps, endpoint);
670 LeaveCriticalSection(&ps->cs);
672 if (status != RPC_S_OK)
677 status = RPCRT4_start_listen_protseq(ps, FALSE);
678 if (status == RPC_S_OK)
679 RPCRT4_sync_with_server_thread(ps);
685 /***********************************************************************
686 * RpcServerInqBindings (RPCRT4.@)
688 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
692 RpcServerProtseq* ps;
696 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
698 ERR("(BindingVector == NULL!!?)\n");
700 EnterCriticalSection(&server_cs);
701 /* count connections */
703 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
704 EnterCriticalSection(&ps->cs);
705 for (conn = ps->conn; conn; conn = conn->Next)
707 LeaveCriticalSection(&ps->cs);
710 /* export bindings */
711 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
712 sizeof(RPC_BINDING_VECTOR) +
713 sizeof(RPC_BINDING_HANDLE)*(count-1));
714 (*BindingVector)->Count = count;
716 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
717 EnterCriticalSection(&ps->cs);
718 for (conn = ps->conn; conn; conn = conn->Next) {
719 RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
723 LeaveCriticalSection(&ps->cs);
727 *BindingVector = NULL;
728 status = RPC_S_NO_BINDINGS;
730 LeaveCriticalSection(&server_cs);
734 /***********************************************************************
735 * RpcServerUseProtseqEpA (RPCRT4.@)
737 RPC_STATUS WINAPI RpcServerUseProtseqEpA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor )
741 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
743 /* This should provide the default behaviour */
744 policy.Length = sizeof( policy );
745 policy.EndpointFlags = 0;
748 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
751 /***********************************************************************
752 * RpcServerUseProtseqEpW (RPCRT4.@)
754 RPC_STATUS WINAPI RpcServerUseProtseqEpW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor )
758 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
760 /* This should provide the default behaviour */
761 policy.Length = sizeof( policy );
762 policy.EndpointFlags = 0;
765 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
768 /***********************************************************************
769 * alloc_serverprotoseq (internal)
771 * Must be called with server_cs held.
773 static RPC_STATUS alloc_serverprotoseq(UINT MaxCalls, const char *Protseq, RpcServerProtseq **ps)
775 const struct protseq_ops *ops = rpcrt4_get_protseq_ops(Protseq);
779 FIXME("protseq %s not supported\n", debugstr_a(Protseq));
780 return RPC_S_PROTSEQ_NOT_SUPPORTED;
785 return RPC_S_OUT_OF_RESOURCES;
786 (*ps)->MaxCalls = MaxCalls;
787 (*ps)->Protseq = RPCRT4_strdupA(Protseq);
791 InitializeCriticalSection(&(*ps)->cs);
792 (*ps)->is_listening = FALSE;
793 (*ps)->mgr_mutex = NULL;
794 (*ps)->server_ready_event = NULL;
796 list_add_head(&protseqs, &(*ps)->entry);
798 TRACE("new protseq %p created for %s\n", *ps, Protseq);
803 /* must be called with server_cs held */
804 static void destroy_serverprotoseq(RpcServerProtseq *ps)
806 RPCRT4_strfree(ps->Protseq);
807 DeleteCriticalSection(&ps->cs);
808 CloseHandle(ps->mgr_mutex);
809 CloseHandle(ps->server_ready_event);
810 list_remove(&ps->entry);
811 HeapFree(GetProcessHeap(), 0, ps);
814 /* Finds a given protseq or creates a new one if one doesn't already exist */
815 static RPC_STATUS RPCRT4_get_or_create_serverprotseq(UINT MaxCalls, const char *Protseq, RpcServerProtseq **ps)
818 RpcServerProtseq *cps;
820 EnterCriticalSection(&server_cs);
822 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
823 if (!strcmp(cps->Protseq, Protseq))
825 TRACE("found existing protseq object for %s\n", Protseq);
827 LeaveCriticalSection(&server_cs);
831 status = alloc_serverprotoseq(MaxCalls, Protseq, ps);
833 LeaveCriticalSection(&server_cs);
838 /***********************************************************************
839 * RpcServerUseProtseqEpExA (RPCRT4.@)
841 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor,
842 PRPC_POLICY lpPolicy )
844 RpcServerProtseq* ps;
847 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_a((const char *)Protseq),
848 MaxCalls, debugstr_a((const char *)Endpoint), SecurityDescriptor,
849 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
851 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
852 if (status != RPC_S_OK)
855 return RPCRT4_use_protseq(ps, (const char *)Endpoint);
858 /***********************************************************************
859 * RpcServerUseProtseqEpExW (RPCRT4.@)
861 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor,
862 PRPC_POLICY lpPolicy )
864 RpcServerProtseq* ps;
869 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_w( Protseq ), MaxCalls,
870 debugstr_w( Endpoint ), SecurityDescriptor,
871 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
873 ProtseqA = RPCRT4_strdupWtoA(Protseq);
874 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, ProtseqA, &ps);
875 RPCRT4_strfree(ProtseqA);
876 if (status != RPC_S_OK)
879 EndpointA = RPCRT4_strdupWtoA(Endpoint);
880 status = RPCRT4_use_protseq(ps, EndpointA);
881 RPCRT4_strfree(EndpointA);
885 /***********************************************************************
886 * RpcServerUseProtseqA (RPCRT4.@)
888 RPC_STATUS WINAPI RpcServerUseProtseqA(RPC_CSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
891 RpcServerProtseq* ps;
893 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq), MaxCalls, SecurityDescriptor);
895 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
896 if (status != RPC_S_OK)
899 return RPCRT4_use_protseq(ps, NULL);
902 /***********************************************************************
903 * RpcServerUseProtseqW (RPCRT4.@)
905 RPC_STATUS WINAPI RpcServerUseProtseqW(RPC_WSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
908 RpcServerProtseq* ps;
911 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
913 ProtseqA = RPCRT4_strdupWtoA(Protseq);
914 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, ProtseqA, &ps);
915 RPCRT4_strfree(ProtseqA);
916 if (status != RPC_S_OK)
919 return RPCRT4_use_protseq(ps, NULL);
922 void RPCRT4_destroy_all_protseqs(void)
924 RpcServerProtseq *cps, *cursor2;
926 if (listen_count != 0)
929 EnterCriticalSection(&server_cs);
930 LIST_FOR_EACH_ENTRY_SAFE(cps, cursor2, &protseqs, RpcServerProtseq, entry)
932 if (listen_count != 0)
933 RPCRT4_sync_with_server_thread(cps);
934 destroy_serverprotoseq(cps);
936 LeaveCriticalSection(&server_cs);
939 /***********************************************************************
940 * RpcServerRegisterIf (RPCRT4.@)
942 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
944 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
945 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
948 /***********************************************************************
949 * RpcServerRegisterIfEx (RPCRT4.@)
951 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
952 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
954 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
955 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
958 /***********************************************************************
959 * RpcServerRegisterIf2 (RPCRT4.@)
961 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
962 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
964 PRPC_SERVER_INTERFACE If = IfSpec;
965 RpcServerInterface* sif;
968 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
969 MaxRpcSize, IfCallbackFn);
970 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
971 If->InterfaceId.SyntaxVersion.MajorVersion,
972 If->InterfaceId.SyntaxVersion.MinorVersion);
973 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
974 If->TransferSyntax.SyntaxVersion.MajorVersion,
975 If->TransferSyntax.SyntaxVersion.MinorVersion);
976 TRACE(" dispatch table: %p\n", If->DispatchTable);
977 if (If->DispatchTable) {
978 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
979 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
980 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
982 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
984 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
985 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
986 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
988 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
991 sif->MgrTypeUuid = *MgrTypeUuid;
992 sif->MgrEpv = MgrEpv;
994 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
995 sif->MgrEpv = If->DefaultManagerEpv;
998 sif->MaxCalls = MaxCalls;
999 sif->MaxRpcSize = MaxRpcSize;
1000 sif->IfCallbackFn = IfCallbackFn;
1002 EnterCriticalSection(&server_cs);
1003 list_add_head(&server_interfaces, &sif->entry);
1004 LeaveCriticalSection(&server_cs);
1006 if (sif->Flags & RPC_IF_AUTOLISTEN)
1007 RPCRT4_start_listen(TRUE);
1012 /***********************************************************************
1013 * RpcServerUnregisterIf (RPCRT4.@)
1015 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
1017 PRPC_SERVER_INTERFACE If = IfSpec;
1018 HANDLE event = NULL;
1020 BOOL completed = TRUE;
1021 RpcServerInterface *cif;
1024 TRACE("(IfSpec == (RPC_IF_HANDLE)^%p (%s), MgrTypeUuid == %s, WaitForCallsToComplete == %u)\n",
1025 IfSpec, debugstr_guid(&If->InterfaceId.SyntaxGUID), debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
1027 EnterCriticalSection(&server_cs);
1028 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
1029 if ((!IfSpec || !memcmp(&If->InterfaceId, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER))) &&
1030 UuidEqual(MgrTypeUuid, &cif->MgrTypeUuid, &status)) {
1031 list_remove(&cif->entry);
1032 TRACE("unregistering cif %p\n", cif);
1033 if (cif->CurrentCalls) {
1036 if (WaitForCallsToComplete)
1037 cif->CallsCompletedEvent = event = CreateEventW(NULL, FALSE, FALSE, NULL);
1043 LeaveCriticalSection(&server_cs);
1046 ERR("not found for object %s\n", debugstr_guid(MgrTypeUuid));
1047 return RPC_S_UNKNOWN_IF;
1051 HeapFree(GetProcessHeap(), 0, cif);
1053 /* sif will be freed when the last call is completed, so be careful not to
1054 * touch that memory here as that could happen before we get here */
1055 WaitForSingleObject(event, INFINITE);
1062 /***********************************************************************
1063 * RpcServerUnregisterIfEx (RPCRT4.@)
1065 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
1067 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
1068 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
1073 /***********************************************************************
1074 * RpcObjectSetType (RPCRT4.@)
1077 * ObjUuid [I] "Object" UUID
1078 * TypeUuid [I] "Type" UUID
1081 * RPC_S_OK The call succeeded
1082 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
1083 * RPC_S_ALREADY_REGISTERED The provided object is already registered
1085 * Maps "Object" UUIDs to "Type" UUID's. Passing the nil UUID as the type
1086 * resets the mapping for the specified object UUID to nil (the default).
1087 * The nil object is always associated with the nil type and cannot be
1088 * reassigned. Servers can support multiple implementations on the same
1089 * interface by registering different end-point vectors for the different
1090 * types. There's no need to call this if a server only supports the nil
1091 * type, as is typical.
1093 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
1095 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
1098 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
1099 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
1100 /* nil uuid cannot be remapped */
1101 return RPC_S_INVALID_OBJECT;
1104 /* find the mapping for this object if there is one ... */
1106 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
1110 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
1111 /* ... and drop it from the list */
1114 prev->next = map->next;
1116 RpcObjTypeMaps = map->next;
1117 HeapFree(GetProcessHeap(), 0, map);
1120 /* ... , fail if we found it ... */
1122 return RPC_S_ALREADY_REGISTERED;
1123 /* ... otherwise create a new one and add it in. */
1124 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
1125 map->Object = *ObjUuid;
1126 map->Type = *TypeUuid;
1129 prev->next = map; /* prev is the last map in the linklist */
1131 RpcObjTypeMaps = map;
1137 /***********************************************************************
1138 * RpcServerRegisterAuthInfoA (RPCRT4.@)
1140 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1143 FIXME( "(%s,%u,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
1145 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
1148 /***********************************************************************
1149 * RpcServerRegisterAuthInfoW (RPCRT4.@)
1151 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1154 FIXME( "(%s,%u,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
1156 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
1159 /***********************************************************************
1160 * RpcServerListen (RPCRT4.@)
1162 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
1164 RPC_STATUS status = RPC_S_OK;
1166 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
1168 if (list_empty(&protseqs))
1169 return RPC_S_NO_PROTSEQS_REGISTERED;
1171 status = RPCRT4_start_listen(FALSE);
1173 if (DontWait || (status != RPC_S_OK)) return status;
1175 return RpcMgmtWaitServerListen();
1178 /***********************************************************************
1179 * RpcMgmtServerWaitListen (RPCRT4.@)
1181 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
1185 EnterCriticalSection(&listen_cs);
1188 LeaveCriticalSection(&listen_cs);
1189 return RPC_S_NOT_LISTENING;
1192 LeaveCriticalSection(&listen_cs);
1194 FIXME("not waiting for server calls to finish\n");
1199 /***********************************************************************
1200 * RpcMgmtStopServerListening (RPCRT4.@)
1202 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1204 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1207 FIXME("client-side invocation not implemented.\n");
1208 return RPC_S_WRONG_KIND_OF_BINDING;
1211 RPCRT4_stop_listen(FALSE);
1216 /***********************************************************************
1217 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
1219 RPC_STATUS WINAPI RpcMgmtEnableIdleCleanup(void)
1221 FIXME("(): stub\n");
1225 /***********************************************************************
1226 * I_RpcServerStartListening (RPCRT4.@)
1228 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1230 FIXME( "(%p): stub\n", hWnd );
1235 /***********************************************************************
1236 * I_RpcServerStopListening (RPCRT4.@)
1238 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1240 FIXME( "(): stub\n" );
1245 /***********************************************************************
1246 * I_RpcWindowProc (RPCRT4.@)
1248 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1250 FIXME( "(%p,%08x,%08x,%08x): stub\n", hWnd, Message, wParam, lParam );
1255 /***********************************************************************
1256 * RpcMgmtInqIfIds (RPCRT4.@)
1258 RPC_STATUS WINAPI RpcMgmtInqIfIds(RPC_BINDING_HANDLE Binding, RPC_IF_ID_VECTOR **IfIdVector)
1260 FIXME("(%p,%p): stub\n", Binding, IfIdVector);
1261 return RPC_S_INVALID_BINDING;
1264 /***********************************************************************
1265 * RpcMgmtInqStats (RPCRT4.@)
1267 RPC_STATUS WINAPI RpcMgmtInqStats(RPC_BINDING_HANDLE Binding, RPC_STATS_VECTOR **Statistics)
1269 RPC_STATS_VECTOR *stats;
1271 FIXME("(%p,%p)\n", Binding, Statistics);
1273 if ((stats = HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_STATS_VECTOR))))
1276 stats->Stats[0] = 0;
1277 *Statistics = stats;
1280 return RPC_S_OUT_OF_RESOURCES;
1283 /***********************************************************************
1284 * RpcMgmtStatsVectorFree (RPCRT4.@)
1286 RPC_STATUS WINAPI RpcMgmtStatsVectorFree(RPC_STATS_VECTOR **StatsVector)
1288 FIXME("(%p)\n", StatsVector);
1292 HeapFree(GetProcessHeap(), 0, *StatsVector);
1293 *StatsVector = NULL;
1298 /***********************************************************************
1299 * RpcMgmtEpEltInqBegin (RPCRT4.@)
1301 RPC_STATUS WINAPI RpcMgmtEpEltInqBegin(RPC_BINDING_HANDLE Binding, ULONG InquiryType,
1302 RPC_IF_ID *IfId, ULONG VersOption, UUID *ObjectUuid, RPC_EP_INQ_HANDLE* InquiryContext)
1304 FIXME("(%p,%u,%p,%u,%p,%p): stub\n",
1305 Binding, InquiryType, IfId, VersOption, ObjectUuid, InquiryContext);
1306 return RPC_S_INVALID_BINDING;
1309 /***********************************************************************
1310 * RpcMgmtIsServerListening (RPCRT4.@)
1312 RPC_STATUS WINAPI RpcMgmtIsServerListening(RPC_BINDING_HANDLE Binding)
1314 FIXME("(%p): stub\n", Binding);
1315 return RPC_S_INVALID_BINDING;
1318 /***********************************************************************
1319 * RpcMgmtSetServerStackSize (RPCRT4.@)
1321 RPC_STATUS WINAPI RpcMgmtSetServerStackSize(ULONG ThreadStackSize)
1323 FIXME("(0x%x): stub\n", ThreadStackSize);
1327 /***********************************************************************
1328 * I_RpcGetCurrentCallHandle (RPCRT4.@)
1330 RPC_BINDING_HANDLE WINAPI I_RpcGetCurrentCallHandle(void)
1333 return RPCRT4_GetThreadCurrentCallHandle();