rpcrt4: Convert the server interfaces list into a standard Wine list.
[wine.git] / dlls / rpcrt4 / rpc_server.c
blobacf0f2e7be8b4f5befe66da861121b3c191f753e
1 /*
2 * RPC server API
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * TODO:
22 * - a whole lot
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "winreg.h"
38 #include "rpc.h"
39 #include "rpcndr.h"
40 #include "excpt.h"
42 #include "wine/debug.h"
43 #include "wine/exception.h"
45 #include "rpc_server.h"
46 #include "rpc_misc.h"
47 #include "rpc_message.h"
48 #include "rpc_defs.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
52 typedef struct _RpcPacket
54 struct _RpcConnection* conn;
55 RpcPktHdr* hdr;
56 RPC_MESSAGE* msg;
57 } RpcPacket;
59 typedef struct _RpcObjTypeMap
61 /* FIXME: a hash table would be better. */
62 struct _RpcObjTypeMap *next;
63 UUID Object;
64 UUID Type;
65 } RpcObjTypeMap;
67 static RpcObjTypeMap *RpcObjTypeMaps;
69 /* list of type RpcServerProtseq */
70 static struct list protseqs = LIST_INIT(protseqs);
71 static struct list server_interfaces = LIST_INIT(server_interfaces);
73 static CRITICAL_SECTION server_cs;
74 static CRITICAL_SECTION_DEBUG server_cs_debug =
76 0, 0, &server_cs,
77 { &server_cs_debug.ProcessLocksList, &server_cs_debug.ProcessLocksList },
78 0, 0, { (DWORD_PTR)(__FILE__ ": server_cs") }
80 static CRITICAL_SECTION server_cs = { &server_cs_debug, -1, 0, 0, 0, 0 };
82 static CRITICAL_SECTION listen_cs;
83 static CRITICAL_SECTION_DEBUG listen_cs_debug =
85 0, 0, &listen_cs,
86 { &listen_cs_debug.ProcessLocksList, &listen_cs_debug.ProcessLocksList },
87 0, 0, { (DWORD_PTR)(__FILE__ ": listen_cs") }
89 static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };
91 /* whether the server is currently listening */
92 static BOOL std_listen;
93 /* number of manual listeners (calls to RpcServerListen) */
94 static LONG manual_listen_count;
95 /* total listeners including auto listeners */
96 static LONG listen_count;
98 static UUID uuid_nil;
100 inline static RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
102 RpcObjTypeMap *rslt = RpcObjTypeMaps;
103 RPC_STATUS dummy;
105 while (rslt) {
106 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
107 rslt = rslt->next;
110 return rslt;
113 inline static UUID *LookupObjType(UUID *ObjUuid)
115 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
116 if (map)
117 return &map->Type;
118 else
119 return &uuid_nil;
122 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
123 RPC_SYNTAX_IDENTIFIER* if_id,
124 BOOL check_object)
126 UUID* MgrType = NULL;
127 RpcServerInterface* cif;
128 RPC_STATUS status;
130 if (check_object)
131 MgrType = LookupObjType(object);
132 EnterCriticalSection(&server_cs);
133 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
134 if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
135 (check_object == FALSE || UuidEqual(MgrType, &cif->MgrTypeUuid, &status)) &&
136 std_listen) break;
138 LeaveCriticalSection(&server_cs);
139 if (&cif->entry == &server_interfaces) cif = NULL;
140 TRACE("returning %p for %s\n", cif, debugstr_guid(object));
141 return cif;
144 static WINE_EXCEPTION_FILTER(rpc_filter)
146 WARN("exception caught with code 0x%08x = %d\n", GetExceptionCode(), GetExceptionCode());
147 TRACE("returning failure packet\n");
148 /* catch every exception */
149 return EXCEPTION_EXECUTE_HANDLER;
152 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr, RPC_MESSAGE* msg)
154 RpcServerInterface* sif;
155 RPC_DISPATCH_FUNCTION func;
156 UUID *object_uuid;
157 RpcPktHdr *response;
158 void *buf = msg->Buffer;
159 RPC_STATUS status;
161 switch (hdr->common.ptype) {
162 case PKT_BIND:
163 TRACE("got bind packet\n");
165 /* FIXME: do more checks! */
166 if (hdr->bind.max_tsize < RPC_MIN_PACKET_SIZE ||
167 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
168 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
169 sif = NULL;
170 } else {
171 sif = RPCRT4_find_interface(NULL, &hdr->bind.abstract, FALSE);
173 if (sif == NULL) {
174 TRACE("rejecting bind request on connection %p\n", conn);
175 /* Report failure to client. */
176 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
177 RPC_VER_MAJOR, RPC_VER_MINOR);
178 } else {
179 TRACE("accepting bind request on connection %p for %s\n", conn,
180 debugstr_guid(&hdr->bind.abstract.SyntaxGUID));
182 /* accept. */
183 response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
184 RPC_MAX_PACKET_SIZE,
185 RPC_MAX_PACKET_SIZE,
186 conn->Endpoint,
187 RESULT_ACCEPT, NO_REASON,
188 &sif->If->TransferSyntax);
190 /* save the interface for later use */
191 conn->ActiveInterface = hdr->bind.abstract;
192 conn->MaxTransmissionSize = hdr->bind.max_tsize;
195 status = RPCRT4_Send(conn, response, NULL, 0);
196 RPCRT4_FreeHeader(response);
197 if (status != RPC_S_OK)
198 goto fail;
200 break;
202 case PKT_REQUEST:
203 TRACE("got request packet\n");
205 /* fail if the connection isn't bound with an interface */
206 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
207 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
208 status);
210 RPCRT4_Send(conn, response, NULL, 0);
211 RPCRT4_FreeHeader(response);
212 break;
215 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
216 object_uuid = (UUID*)(&hdr->request + 1);
217 } else {
218 object_uuid = NULL;
221 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, TRUE);
222 msg->RpcInterfaceInformation = sif->If;
223 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
224 msg->ManagerEpv = sif->MgrEpv;
225 if (object_uuid != NULL) {
226 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
229 /* find dispatch function */
230 msg->ProcNum = hdr->request.opnum;
231 if (sif->Flags & RPC_IF_OLE) {
232 /* native ole32 always gives us a dispatch table with a single entry
233 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
234 func = *sif->If->DispatchTable->DispatchTable;
235 } else {
236 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
237 ERR("invalid procnum\n");
238 func = NULL;
240 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
243 /* put in the drep. FIXME: is this more universally applicable?
244 perhaps we should move this outward... */
245 msg->DataRepresentation =
246 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
247 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
249 /* dispatch */
250 __TRY {
251 if (func) func(msg);
252 } __EXCEPT(rpc_filter) {
253 if (msg->Buffer != buf) I_RpcFreeBuffer(msg);
254 /* this will cause a failure packet to be sent in I_RpcSend */
255 msg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
256 msg->BufferLength = sizeof(DWORD);
257 I_RpcGetBuffer(msg);
258 *(DWORD*)msg->Buffer = GetExceptionCode();
259 } __ENDTRY
261 /* send response packet */
262 I_RpcSend(msg);
264 msg->RpcInterfaceInformation = NULL;
266 break;
268 default:
269 FIXME("unhandled packet type\n");
270 break;
273 fail:
274 /* clean up */
275 if (msg->Buffer == buf) msg->Buffer = NULL;
276 TRACE("freeing Buffer=%p\n", buf);
277 HeapFree(GetProcessHeap(), 0, buf);
278 RPCRT4_DestroyBinding(msg->Handle);
279 msg->Handle = 0;
280 I_RpcFreeBuffer(msg);
281 msg->Buffer = NULL;
282 RPCRT4_FreeHeader(hdr);
283 HeapFree(GetProcessHeap(), 0, msg);
286 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
288 RpcPacket *pkt = the_arg;
289 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg);
290 HeapFree(GetProcessHeap(), 0, pkt);
291 return 0;
294 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
296 RpcConnection* conn = (RpcConnection*)the_arg;
297 RpcPktHdr *hdr;
298 RpcBinding *pbind;
299 RPC_MESSAGE *msg;
300 RPC_STATUS status;
301 RpcPacket *packet;
303 TRACE("(%p)\n", conn);
305 for (;;) {
306 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
308 /* create temporary binding for dispatch, it will be freed in
309 * RPCRT4_process_packet */
310 RPCRT4_MakeBinding(&pbind, conn);
311 msg->Handle = (RPC_BINDING_HANDLE)pbind;
313 status = RPCRT4_Receive(conn, &hdr, msg);
314 if (status != RPC_S_OK) {
315 WARN("receive failed with error %lx\n", status);
316 HeapFree(GetProcessHeap(), 0, msg);
317 break;
320 #if 0
321 RPCRT4_process_packet(conn, hdr, msg);
322 #else
323 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
324 packet->conn = conn;
325 packet->hdr = hdr;
326 packet->msg = msg;
327 QueueUserWorkItem(RPCRT4_worker_thread, packet, WT_EXECUTEDEFAULT);
328 #endif
329 msg = NULL;
331 RPCRT4_DestroyConnection(conn);
332 return 0;
335 void RPCRT4_new_client(RpcConnection* conn)
337 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
338 if (!thread) {
339 DWORD err = GetLastError();
340 ERR("failed to create thread, error=%08x\n", err);
341 RPCRT4_DestroyConnection(conn);
343 /* we could set conn->thread, but then we'd have to make the io_thread wait
344 * for that, otherwise the thread might finish, destroy the connection, and
345 * free the memory we'd write to before we did, causing crashes and stuff -
346 * so let's implement that later, when we really need conn->thread */
348 CloseHandle( thread );
351 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
353 int res;
354 unsigned int count;
355 void *objs = NULL;
356 RpcServerProtseq* cps = the_arg;
357 RpcConnection* conn;
358 BOOL set_ready_event = FALSE;
360 TRACE("(the_arg == ^%p)\n", the_arg);
362 for (;;) {
363 objs = cps->ops->get_wait_array(cps, objs, &count);
365 if (set_ready_event)
367 /* signal to function that changed state that we are now sync'ed */
368 SetEvent(cps->server_ready_event);
369 set_ready_event = FALSE;
372 /* start waiting */
373 res = cps->ops->wait_for_new_connection(cps, count, objs);
374 if (res == -1)
375 break;
376 else if (res == 0)
378 if (!std_listen)
380 SetEvent(cps->server_ready_event);
381 break;
383 set_ready_event = TRUE;
386 cps->ops->free_wait_array(cps, objs);
387 EnterCriticalSection(&cps->cs);
388 /* close connections */
389 conn = cps->conn;
390 while (conn) {
391 RPCRT4_CloseConnection(conn);
392 conn = conn->Next;
394 LeaveCriticalSection(&cps->cs);
395 return 0;
398 /* tells the server thread that the state has changed and waits for it to
399 * make the changes */
400 static void RPCRT4_sync_with_server_thread(RpcServerProtseq *ps)
402 /* make sure we are the only thread sync'ing the server state, otherwise
403 * there is a race with the server thread setting an older state and setting
404 * the server_ready_event when the new state hasn't yet been applied */
405 WaitForSingleObject(ps->mgr_mutex, INFINITE);
407 ps->ops->signal_state_changed(ps);
409 /* wait for server thread to make the requested changes before returning */
410 WaitForSingleObject(ps->server_ready_event, INFINITE);
412 ReleaseMutex(ps->mgr_mutex);
415 static RPC_STATUS RPCRT4_start_listen_protseq(RpcServerProtseq *ps, BOOL auto_listen)
417 RPC_STATUS status = RPC_S_OK;
418 HANDLE server_thread;
420 EnterCriticalSection(&listen_cs);
421 if (ps->is_listening) goto done;
423 if (!ps->mgr_mutex) ps->mgr_mutex = CreateMutexW(NULL, FALSE, NULL);
424 if (!ps->server_ready_event) ps->server_ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
425 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, ps, 0, NULL);
426 if (!server_thread)
428 status = RPC_S_OUT_OF_RESOURCES;
429 goto done;
431 ps->is_listening = TRUE;
432 CloseHandle(server_thread);
434 done:
435 LeaveCriticalSection(&listen_cs);
436 return status;
439 static RPC_STATUS RPCRT4_start_listen(BOOL auto_listen)
441 RPC_STATUS status = RPC_S_ALREADY_LISTENING;
442 RpcServerProtseq *cps;
444 TRACE("\n");
446 EnterCriticalSection(&listen_cs);
447 if (auto_listen || (manual_listen_count++ == 0))
449 status = RPC_S_OK;
450 if (++listen_count == 1)
451 std_listen = TRUE;
453 LeaveCriticalSection(&listen_cs);
455 if (std_listen)
457 EnterCriticalSection(&server_cs);
458 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
460 status = RPCRT4_start_listen_protseq(cps, TRUE);
461 if (status != RPC_S_OK)
462 break;
464 /* make sure server is actually listening on the interface before
465 * returning */
466 RPCRT4_sync_with_server_thread(cps);
468 LeaveCriticalSection(&server_cs);
471 return status;
474 static void RPCRT4_stop_listen(BOOL auto_listen)
476 EnterCriticalSection(&listen_cs);
477 if (auto_listen || (--manual_listen_count == 0))
479 if (listen_count != 0 && --listen_count == 0) {
480 RpcServerProtseq *cps;
482 std_listen = FALSE;
483 LeaveCriticalSection(&listen_cs);
485 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
486 RPCRT4_sync_with_server_thread(cps);
488 return;
490 assert(listen_count >= 0);
492 LeaveCriticalSection(&listen_cs);
495 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps, LPSTR endpoint)
497 RPC_STATUS status;
499 status = ps->ops->open_endpoint(ps, endpoint);
500 if (status != RPC_S_OK)
501 return status;
503 if (std_listen)
505 status = RPCRT4_start_listen_protseq(ps, FALSE);
506 if (status == RPC_S_OK)
507 RPCRT4_sync_with_server_thread(ps);
510 return status;
513 /***********************************************************************
514 * RpcServerInqBindings (RPCRT4.@)
516 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
518 RPC_STATUS status;
519 DWORD count;
520 RpcServerProtseq* ps;
521 RpcConnection* conn;
523 if (BindingVector)
524 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
525 else
526 ERR("(BindingVector == NULL!!?)\n");
528 EnterCriticalSection(&server_cs);
529 /* count connections */
530 count = 0;
531 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
532 EnterCriticalSection(&ps->cs);
533 conn = ps->conn;
534 while (conn) {
535 count++;
536 conn = conn->Next;
538 LeaveCriticalSection(&ps->cs);
540 if (count) {
541 /* export bindings */
542 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
543 sizeof(RPC_BINDING_VECTOR) +
544 sizeof(RPC_BINDING_HANDLE)*(count-1));
545 (*BindingVector)->Count = count;
546 count = 0;
547 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
548 EnterCriticalSection(&ps->cs);
549 conn = ps->conn;
550 while (conn) {
551 RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
552 conn);
553 count++;
554 conn = conn->Next;
556 LeaveCriticalSection(&ps->cs);
558 status = RPC_S_OK;
559 } else {
560 *BindingVector = NULL;
561 status = RPC_S_NO_BINDINGS;
563 LeaveCriticalSection(&server_cs);
564 return status;
567 /***********************************************************************
568 * RpcServerUseProtseqEpA (RPCRT4.@)
570 RPC_STATUS WINAPI RpcServerUseProtseqEpA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor )
572 RPC_POLICY policy;
574 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
576 /* This should provide the default behaviour */
577 policy.Length = sizeof( policy );
578 policy.EndpointFlags = 0;
579 policy.NICFlags = 0;
581 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
584 /***********************************************************************
585 * RpcServerUseProtseqEpW (RPCRT4.@)
587 RPC_STATUS WINAPI RpcServerUseProtseqEpW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor )
589 RPC_POLICY policy;
591 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
593 /* This should provide the default behaviour */
594 policy.Length = sizeof( policy );
595 policy.EndpointFlags = 0;
596 policy.NICFlags = 0;
598 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
601 /***********************************************************************
602 * alloc_serverprotoseq (internal)
604 * Must be called with server_cs held.
606 static RPC_STATUS alloc_serverprotoseq(UINT MaxCalls, char *Protseq, RpcServerProtseq **ps)
608 const struct protseq_ops *ops = rpcrt4_get_protseq_ops(Protseq);
610 if (!ops)
612 FIXME("protseq %s not supported\n", debugstr_a(Protseq));
613 return RPC_S_PROTSEQ_NOT_SUPPORTED;
616 *ps = ops->alloc();
617 if (!*ps)
618 return RPC_S_OUT_OF_RESOURCES;
619 (*ps)->MaxCalls = MaxCalls;
620 (*ps)->Protseq = Protseq;
621 (*ps)->ops = ops;
622 (*ps)->MaxCalls = 0;
623 (*ps)->conn = NULL;
624 InitializeCriticalSection(&(*ps)->cs);
625 (*ps)->is_listening = FALSE;
626 (*ps)->mgr_mutex = NULL;
627 (*ps)->server_ready_event = NULL;
629 list_add_head(&protseqs, &(*ps)->entry);
631 TRACE("new protseq %p created for %s\n", *ps, Protseq);
633 return RPC_S_OK;
636 /* Finds a given protseq or creates a new one if one doesn't already exist */
637 static RPC_STATUS RPCRT4_get_or_create_serverprotseq(UINT MaxCalls, char *Protseq, RpcServerProtseq **ps)
639 RPC_STATUS status;
640 RpcServerProtseq *cps;
642 EnterCriticalSection(&server_cs);
644 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
645 if (!strcmp(cps->Protseq, Protseq))
647 TRACE("found existing protseq object for %s\n", Protseq);
648 *ps = cps;
649 LeaveCriticalSection(&server_cs);
650 return S_OK;
653 status = alloc_serverprotoseq(MaxCalls, Protseq, ps);
655 LeaveCriticalSection(&server_cs);
657 return status;
660 /***********************************************************************
661 * RpcServerUseProtseqEpExA (RPCRT4.@)
663 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor,
664 PRPC_POLICY lpPolicy )
666 char *szps = (char*)Protseq, *szep = (char*)Endpoint;
667 RpcServerProtseq* ps;
668 RPC_STATUS status;
670 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_a(szps), MaxCalls,
671 debugstr_a(szep), SecurityDescriptor,
672 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
674 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, RPCRT4_strdupA(szps), &ps);
675 if (status != RPC_S_OK)
676 return status;
678 return RPCRT4_use_protseq(ps, szep);
681 /***********************************************************************
682 * RpcServerUseProtseqEpExW (RPCRT4.@)
684 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor,
685 PRPC_POLICY lpPolicy )
687 RpcServerProtseq* ps;
688 RPC_STATUS status;
689 LPSTR EndpointA;
691 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_w( Protseq ), MaxCalls,
692 debugstr_w( Endpoint ), SecurityDescriptor,
693 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
695 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, RPCRT4_strdupWtoA(Protseq), &ps);
696 if (status != RPC_S_OK)
697 return status;
699 EndpointA = RPCRT4_strdupWtoA(Endpoint);
700 status = RPCRT4_use_protseq(ps, EndpointA);
701 RPCRT4_strfree(EndpointA);
702 return status;
705 /***********************************************************************
706 * RpcServerUseProtseqA (RPCRT4.@)
708 RPC_STATUS WINAPI RpcServerUseProtseqA(RPC_CSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
710 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq), MaxCalls, SecurityDescriptor);
711 return RpcServerUseProtseqEpA(Protseq, MaxCalls, NULL, SecurityDescriptor);
714 /***********************************************************************
715 * RpcServerUseProtseqW (RPCRT4.@)
717 RPC_STATUS WINAPI RpcServerUseProtseqW(RPC_WSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
719 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
720 return RpcServerUseProtseqEpW(Protseq, MaxCalls, NULL, SecurityDescriptor);
723 /***********************************************************************
724 * RpcServerRegisterIf (RPCRT4.@)
726 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
728 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
729 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
732 /***********************************************************************
733 * RpcServerRegisterIfEx (RPCRT4.@)
735 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
736 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
738 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
739 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
742 /***********************************************************************
743 * RpcServerRegisterIf2 (RPCRT4.@)
745 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
746 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
748 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
749 RpcServerInterface* sif;
750 unsigned int i;
752 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
753 MaxRpcSize, IfCallbackFn);
754 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
755 If->InterfaceId.SyntaxVersion.MajorVersion,
756 If->InterfaceId.SyntaxVersion.MinorVersion);
757 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
758 If->TransferSyntax.SyntaxVersion.MajorVersion,
759 If->TransferSyntax.SyntaxVersion.MinorVersion);
760 TRACE(" dispatch table: %p\n", If->DispatchTable);
761 if (If->DispatchTable) {
762 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
763 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
764 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
766 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
768 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
769 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
770 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
772 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
773 sif->If = If;
774 if (MgrTypeUuid) {
775 memcpy(&sif->MgrTypeUuid, MgrTypeUuid, sizeof(UUID));
776 sif->MgrEpv = MgrEpv;
777 } else {
778 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
779 sif->MgrEpv = If->DefaultManagerEpv;
781 sif->Flags = Flags;
782 sif->MaxCalls = MaxCalls;
783 sif->MaxRpcSize = MaxRpcSize;
784 sif->IfCallbackFn = IfCallbackFn;
786 EnterCriticalSection(&server_cs);
787 list_add_head(&server_interfaces, &sif->entry);
788 LeaveCriticalSection(&server_cs);
790 if (sif->Flags & RPC_IF_AUTOLISTEN)
791 RPCRT4_start_listen(TRUE);
793 return RPC_S_OK;
796 /***********************************************************************
797 * RpcServerUnregisterIf (RPCRT4.@)
799 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
801 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, WaitForCallsToComplete == %u): stub\n",
802 IfSpec, debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
804 return RPC_S_OK;
807 /***********************************************************************
808 * RpcServerUnregisterIfEx (RPCRT4.@)
810 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
812 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
813 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
815 return RPC_S_OK;
818 /***********************************************************************
819 * RpcObjectSetType (RPCRT4.@)
821 * PARAMS
822 * ObjUuid [I] "Object" UUID
823 * TypeUuid [I] "Type" UUID
825 * RETURNS
826 * RPC_S_OK The call succeeded
827 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
828 * RPC_S_ALREADY_REGISTERED The provided object is already registered
830 * Maps "Object" UUIDs to "Type" UUID's. Passing the nil UUID as the type
831 * resets the mapping for the specified object UUID to nil (the default).
832 * The nil object is always associated with the nil type and cannot be
833 * reassigned. Servers can support multiple implementations on the same
834 * interface by registering different end-point vectors for the different
835 * types. There's no need to call this if a server only supports the nil
836 * type, as is typical.
838 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
840 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
841 RPC_STATUS dummy;
843 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
844 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
845 /* nil uuid cannot be remapped */
846 return RPC_S_INVALID_OBJECT;
849 /* find the mapping for this object if there is one ... */
850 while (map) {
851 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
852 prev = map;
853 map = map->next;
855 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
856 /* ... and drop it from the list */
857 if (map) {
858 if (prev)
859 prev->next = map->next;
860 else
861 RpcObjTypeMaps = map->next;
862 HeapFree(GetProcessHeap(), 0, map);
864 } else {
865 /* ... , fail if we found it ... */
866 if (map)
867 return RPC_S_ALREADY_REGISTERED;
868 /* ... otherwise create a new one and add it in. */
869 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
870 memcpy(&map->Object, ObjUuid, sizeof(UUID));
871 memcpy(&map->Type, TypeUuid, sizeof(UUID));
872 map->next = NULL;
873 if (prev)
874 prev->next = map; /* prev is the last map in the linklist */
875 else
876 RpcObjTypeMaps = map;
879 return RPC_S_OK;
882 /***********************************************************************
883 * RpcServerRegisterAuthInfoA (RPCRT4.@)
885 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName, unsigned long AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
886 LPVOID Arg )
888 FIXME( "(%s,%lu,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
890 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
893 /***********************************************************************
894 * RpcServerRegisterAuthInfoW (RPCRT4.@)
896 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName, unsigned long AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
897 LPVOID Arg )
899 FIXME( "(%s,%lu,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
901 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
904 /***********************************************************************
905 * RpcServerListen (RPCRT4.@)
907 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
909 RPC_STATUS status = RPC_S_OK;
911 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
913 if (list_empty(&protseqs))
914 return RPC_S_NO_PROTSEQS_REGISTERED;
916 status = RPCRT4_start_listen(FALSE);
918 if (DontWait || (status != RPC_S_OK)) return status;
920 return RpcMgmtWaitServerListen();
923 /***********************************************************************
924 * RpcMgmtServerWaitListen (RPCRT4.@)
926 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
928 TRACE("()\n");
930 EnterCriticalSection(&listen_cs);
932 if (!std_listen) {
933 LeaveCriticalSection(&listen_cs);
934 return RPC_S_NOT_LISTENING;
937 LeaveCriticalSection(&listen_cs);
939 FIXME("not waiting for server calls to finish\n");
941 return RPC_S_OK;
944 /***********************************************************************
945 * RpcMgmtStopServerListening (RPCRT4.@)
947 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
949 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
951 if (Binding) {
952 FIXME("client-side invocation not implemented.\n");
953 return RPC_S_WRONG_KIND_OF_BINDING;
956 RPCRT4_stop_listen(FALSE);
958 return RPC_S_OK;
961 /***********************************************************************
962 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
964 RPC_STATUS WINAPI RpcMgmtEnableIdleCleanup(void)
966 FIXME("(): stub\n");
967 return RPC_S_OK;
970 /***********************************************************************
971 * I_RpcServerStartListening (RPCRT4.@)
973 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
975 FIXME( "(%p): stub\n", hWnd );
977 return RPC_S_OK;
980 /***********************************************************************
981 * I_RpcServerStopListening (RPCRT4.@)
983 RPC_STATUS WINAPI I_RpcServerStopListening( void )
985 FIXME( "(): stub\n" );
987 return RPC_S_OK;
990 /***********************************************************************
991 * I_RpcWindowProc (RPCRT4.@)
993 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
995 FIXME( "(%p,%08x,%08x,%08x): stub\n", hWnd, Message, wParam, lParam );
997 return 0;
1000 /***********************************************************************
1001 * RpcMgmtInqIfIds (RPCRT4.@)
1003 RPC_STATUS WINAPI RpcMgmtInqIfIds(RPC_BINDING_HANDLE Binding, RPC_IF_ID_VECTOR **IfIdVector)
1005 FIXME("(%p,%p): stub\n", Binding, IfIdVector);
1006 return RPC_S_INVALID_BINDING;
1009 /***********************************************************************
1010 * RpcMgmtEpEltInqBegin (RPCRT4.@)
1012 RPC_STATUS WINAPI RpcMgmtEpEltInqBegin(RPC_BINDING_HANDLE Binding, unsigned long InquiryType,
1013 RPC_IF_ID *IfId, unsigned long VersOption, UUID *ObjectUuid, RPC_EP_INQ_HANDLE* InquiryContext)
1015 FIXME("(%p,%lu,%p,%lu,%p,%p): stub\n",
1016 Binding, InquiryType, IfId, VersOption, ObjectUuid, InquiryContext);
1017 return RPC_S_INVALID_BINDING;
1020 /***********************************************************************
1021 * RpcMgmtIsServerListening (RPCRT4.@)
1023 RPC_STATUS WINAPI RpcMgmtIsServerListening(RPC_BINDING_HANDLE Binding)
1025 FIXME("(%p): stub\n", Binding);
1026 return RPC_S_INVALID_BINDING;
1029 /***********************************************************************
1030 * RpcMgmtSetServerStackSize (RPCRT4.@)
1032 RPC_STATUS WINAPI RpcMgmtSetServerStackSize(unsigned long ThreadStackSize)
1034 FIXME("(0x%lx): stub\n", ThreadStackSize);
1035 return RPC_S_OK;