Porting fixes.
[wine/wine-kai.git] / dlls / rpcrt4 / rpc_server.c
blobd18d0914fc54757af929dec793f58d032380f204
1 /*
2 * RPC server API
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * TODO:
21 * - a whole lot
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
35 #include "winreg.h"
36 #include "ntstatus.h"
38 #include "rpc.h"
39 #include "excpt.h"
41 #include "wine/debug.h"
42 #include "wine/exception.h"
44 #include "rpc_server.h"
45 #include "rpc_misc.h"
46 #include "rpc_defs.h"
48 #define MAX_THREADS 128
50 WINE_DEFAULT_DEBUG_CHANNEL(ole);
52 typedef struct _RpcPacket
54 struct _RpcPacket* next;
55 struct _RpcConnection* conn;
56 RpcPktHdr hdr;
57 void* buf;
58 } RpcPacket;
60 typedef struct _RpcObjTypeMap
62 /* FIXME: a hash table would be better. */
63 struct _RpcObjTypeMap *next;
64 UUID Object;
65 UUID Type;
66 } RpcObjTypeMap;
68 static RpcObjTypeMap *RpcObjTypeMaps;
70 static RpcServerProtseq* protseqs;
71 static RpcServerInterface* ifs;
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, { 0, (DWORD)(__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, { 0, (DWORD)(__FILE__ ": listen_cs") }
89 static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };
91 static BOOL std_listen;
92 static LONG listen_count = -1;
93 static HANDLE mgr_event, server_thread;
95 static CRITICAL_SECTION spacket_cs;
96 static CRITICAL_SECTION_DEBUG spacket_cs_debug =
98 0, 0, &spacket_cs,
99 { &spacket_cs_debug.ProcessLocksList, &spacket_cs_debug.ProcessLocksList },
100 0, 0, { 0, (DWORD)(__FILE__ ": spacket_cs") }
102 static CRITICAL_SECTION spacket_cs = { &spacket_cs_debug, -1, 0, 0, 0, 0 };
104 static RpcPacket* spacket_head;
105 static RpcPacket* spacket_tail;
106 static HANDLE server_sem;
108 static DWORD worker_count, worker_free, worker_tls;
110 static UUID uuid_nil;
112 inline static RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
114 RpcObjTypeMap *rslt = RpcObjTypeMaps;
115 RPC_STATUS dummy;
117 while (rslt) {
118 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
119 rslt = rslt->next;
122 return rslt;
125 inline static UUID *LookupObjType(UUID *ObjUuid)
127 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
128 if (map)
129 return &map->Type;
130 else
131 return &uuid_nil;
134 static RpcServerInterface* RPCRT4_find_interface(UUID* object, UUID* if_id)
136 UUID* MgrType = NULL;
137 RpcServerInterface* cif = NULL;
138 RPC_STATUS status;
140 MgrType = LookupObjType(object);
141 EnterCriticalSection(&server_cs);
142 cif = ifs;
143 while (cif) {
144 if (UuidEqual(if_id, &cif->If->InterfaceId.SyntaxGUID, &status) &&
145 UuidEqual(MgrType, &cif->MgrTypeUuid, &status) &&
146 (std_listen || (cif->Flags & RPC_IF_AUTOLISTEN))) break;
147 cif = cif->Next;
149 LeaveCriticalSection(&server_cs);
150 return cif;
153 static void RPCRT4_push_packet(RpcPacket* packet)
155 packet->next = NULL;
156 EnterCriticalSection(&spacket_cs);
157 if (spacket_tail) {
158 spacket_tail->next = packet;
159 spacket_tail = packet;
160 } else {
161 spacket_head = packet;
162 spacket_tail = packet;
164 LeaveCriticalSection(&spacket_cs);
167 static RpcPacket* RPCRT4_pop_packet(void)
169 RpcPacket* packet;
170 EnterCriticalSection(&spacket_cs);
171 packet = spacket_head;
172 if (packet) {
173 spacket_head = packet->next;
174 if (!spacket_head) spacket_tail = NULL;
176 LeaveCriticalSection(&spacket_cs);
177 if (packet) packet->next = NULL;
178 return packet;
181 static WINE_EXCEPTION_FILTER(rpc_filter)
183 PRPC_MESSAGE msg;
184 msg = TlsGetValue(worker_tls);
185 I_RpcFreeBuffer(msg);
186 msg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
187 msg->BufferLength = sizeof(DWORD);
188 I_RpcGetBuffer(msg);
189 *(DWORD*)msg->Buffer = GetExceptionCode();
190 return EXCEPTION_EXECUTE_HANDLER;
193 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr, void* buf)
195 RpcBinding* pbind;
196 RPC_MESSAGE msg;
197 RpcServerInterface* sif;
198 RPC_DISPATCH_FUNCTION func;
200 TlsSetValue(worker_tls, &msg);
201 memset(&msg, 0, sizeof(msg));
202 msg.BufferLength = hdr->len;
203 msg.Buffer = buf;
204 sif = RPCRT4_find_interface(&hdr->object, &hdr->if_id);
205 if (sif) {
206 TRACE("packet received for interface %s\n", debugstr_guid(&hdr->if_id));
207 msg.RpcInterfaceInformation = sif->If;
208 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
209 msg.ManagerEpv = sif->MgrEpv;
210 /* create temporary binding for dispatch */
211 RPCRT4_MakeBinding(&pbind, conn);
212 RPCRT4_SetBindingObject(pbind, &hdr->object);
213 msg.Handle = (RPC_BINDING_HANDLE)pbind;
214 /* process packet */
215 switch (hdr->ptype) {
216 case PKT_REQUEST:
217 /* find dispatch function */
218 msg.ProcNum = hdr->opnum;
219 if (sif->Flags & RPC_IF_OLE) {
220 /* native ole32 always gives us a dispatch table with a single entry
221 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
222 func = *sif->If->DispatchTable->DispatchTable;
223 } else {
224 if (msg.ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
225 ERR("invalid procnum\n");
226 func = NULL;
228 func = sif->If->DispatchTable->DispatchTable[msg.ProcNum];
231 /* put in the drep. FIXME: is this more universally applicable?
232 perhaps we should move this outward... */
233 msg.DataRepresentation =
234 MAKELONG( MAKEWORD(hdr->drep[0], hdr->drep[1]),
235 MAKEWORD(hdr->drep[2], 0));
237 /* dispatch */
238 __TRY {
239 if (func) func(&msg);
240 } __EXCEPT(rpc_filter) {
241 /* failure packet was created in rpc_filter */
242 TRACE("exception caught, returning failure packet\n");
243 } __ENDTRY
245 /* send response packet */
246 I_RpcSend(&msg);
247 break;
248 default:
249 ERR("unknown packet type\n");
250 break;
253 RPCRT4_DestroyBinding(pbind);
254 msg.Handle = 0;
255 msg.RpcInterfaceInformation = NULL;
257 else {
258 ERR("got RPC packet to unregistered interface %s\n", debugstr_guid(&hdr->if_id));
261 /* clean up */
262 if (msg.Buffer == buf) msg.Buffer = NULL;
263 TRACE("freeing Buffer=%p\n", buf);
264 HeapFree(GetProcessHeap(), 0, buf);
265 I_RpcFreeBuffer(&msg);
266 msg.Buffer = NULL;
267 TlsSetValue(worker_tls, NULL);
270 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
272 DWORD obj;
273 RpcPacket* pkt;
275 for (;;) {
276 /* idle timeout after 5s */
277 obj = WaitForSingleObject(server_sem, 5000);
278 if (obj == WAIT_TIMEOUT) {
279 /* if another idle thread exist, self-destruct */
280 if (worker_free > 1) break;
281 continue;
283 pkt = RPCRT4_pop_packet();
284 if (!pkt) continue;
285 InterlockedDecrement(&worker_free);
286 for (;;) {
287 RPCRT4_process_packet(pkt->conn, &pkt->hdr, pkt->buf);
288 HeapFree(GetProcessHeap(), 0, pkt);
289 /* try to grab another packet here without waiting
290 * on the semaphore, in case it hits max */
291 pkt = RPCRT4_pop_packet();
292 if (!pkt) break;
293 /* decrement semaphore */
294 WaitForSingleObject(server_sem, 0);
296 InterlockedIncrement(&worker_free);
298 InterlockedDecrement(&worker_free);
299 InterlockedDecrement(&worker_count);
300 return 0;
303 static void RPCRT4_create_worker_if_needed(void)
305 if (!worker_free && worker_count < MAX_THREADS) {
306 HANDLE thread;
307 InterlockedIncrement(&worker_count);
308 InterlockedIncrement(&worker_free);
309 thread = CreateThread(NULL, 0, RPCRT4_worker_thread, NULL, 0, NULL);
310 if (thread) CloseHandle(thread);
311 else {
312 InterlockedDecrement(&worker_free);
313 InterlockedDecrement(&worker_count);
318 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
320 RpcConnection* conn = (RpcConnection*)the_arg;
321 RpcPktHdr hdr;
322 DWORD dwRead;
323 void* buf = NULL;
324 RpcPacket* packet;
326 TRACE("(%p)\n", conn);
328 for (;;) {
329 /* read packet header */
330 #ifdef OVERLAPPED_WORKS
331 if (!ReadFile(conn->conn, &hdr, sizeof(hdr), &dwRead, &conn->ovl)) {
332 DWORD err = GetLastError();
333 if (err != ERROR_IO_PENDING) {
334 TRACE("connection lost, error=%08lx\n", err);
335 break;
337 if (!GetOverlappedResult(conn->conn, &conn->ovl, &dwRead, TRUE)) break;
339 #else
340 if (!ReadFile(conn->conn, &hdr, sizeof(hdr), &dwRead, NULL)) {
341 TRACE("connection lost, error=%08lx\n", GetLastError());
342 break;
344 #endif
345 if (dwRead != sizeof(hdr)) {
346 if (dwRead) TRACE("protocol error: <hdrsz == %d, dwRead == %lu>\n", sizeof(hdr), dwRead);
347 break;
350 /* read packet body */
351 buf = HeapAlloc(GetProcessHeap(), 0, hdr.len);
352 TRACE("receiving payload=%d\n", hdr.len);
353 if (!hdr.len) dwRead = 0; else
354 #ifdef OVERLAPPED_WORKS
355 if (!ReadFile(conn->conn, buf, hdr.len, &dwRead, &conn->ovl)) {
356 DWORD err = GetLastError();
357 if (err != ERROR_IO_PENDING) {
358 TRACE("connection lost, error=%08lx\n", err);
359 break;
361 if (!GetOverlappedResult(conn->conn, &conn->ovl, &dwRead, TRUE)) break;
363 #else
364 if (!ReadFile(conn->conn, buf, hdr.len, &dwRead, NULL)) {
365 TRACE("connection lost, error=%08lx\n", GetLastError());
366 break;
368 #endif
369 if (dwRead != hdr.len) {
370 TRACE("protocol error: <bodylen == %d, dwRead == %lu>\n", hdr.len, dwRead);
371 break;
374 #if 0
375 RPCRT4_process_packet(conn, &hdr, buf);
376 #else
377 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
378 packet->conn = conn;
379 packet->hdr = hdr;
380 packet->buf = buf;
381 RPCRT4_create_worker_if_needed();
382 RPCRT4_push_packet(packet);
383 ReleaseSemaphore(server_sem, 1, NULL);
384 #endif
385 buf = NULL;
387 if (buf) HeapFree(GetProcessHeap(), 0, buf);
388 RPCRT4_DestroyConnection(conn);
389 return 0;
392 static void RPCRT4_new_client(RpcConnection* conn)
394 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
395 if (!thread) {
396 DWORD err = GetLastError();
397 ERR("failed to create thread, error=%08lx\n", err);
398 RPCRT4_DestroyConnection(conn);
400 /* we could set conn->thread, but then we'd have to make the io_thread wait
401 * for that, otherwise the thread might finish, destroy the connection, and
402 * free the memory we'd write to before we did, causing crashes and stuff -
403 * so let's implement that later, when we really need conn->thread */
405 CloseHandle( thread );
408 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
410 HANDLE m_event = mgr_event, b_handle;
411 HANDLE *objs = NULL;
412 DWORD count, res;
413 RpcServerProtseq* cps;
414 RpcConnection* conn;
415 RpcConnection* cconn;
417 TRACE("(the_arg == ^%p)\n", the_arg);
419 for (;;) {
420 EnterCriticalSection(&server_cs);
421 /* open and count connections */
422 count = 1;
423 cps = protseqs;
424 while (cps) {
425 conn = cps->conn;
426 while (conn) {
427 RPCRT4_OpenConnection(conn);
428 if (conn->ovl.hEvent) count++;
429 conn = conn->Next;
431 cps = cps->Next;
433 /* make array of connections */
434 if (objs)
435 objs = HeapReAlloc(GetProcessHeap(), 0, objs, count*sizeof(HANDLE));
436 else
437 objs = HeapAlloc(GetProcessHeap(), 0, count*sizeof(HANDLE));
439 objs[0] = m_event;
440 count = 1;
441 cps = protseqs;
442 while (cps) {
443 conn = cps->conn;
444 while (conn) {
445 if (conn->ovl.hEvent) objs[count++] = conn->ovl.hEvent;
446 conn = conn->Next;
448 cps = cps->Next;
450 LeaveCriticalSection(&server_cs);
452 /* start waiting */
453 res = WaitForMultipleObjects(count, objs, FALSE, INFINITE);
454 if (res == WAIT_OBJECT_0) {
455 ResetEvent(m_event);
456 if (!std_listen) break;
458 else if (res == WAIT_FAILED) {
459 ERR("wait failed\n");
461 else {
462 b_handle = objs[res - WAIT_OBJECT_0];
463 /* find which connection got a RPC */
464 EnterCriticalSection(&server_cs);
465 conn = NULL;
466 cps = protseqs;
467 while (cps) {
468 conn = cps->conn;
469 while (conn) {
470 if (conn->ovl.hEvent == b_handle) break;
471 conn = conn->Next;
473 if (conn) break;
474 cps = cps->Next;
476 cconn = NULL;
477 if (conn) RPCRT4_SpawnConnection(&cconn, conn);
478 LeaveCriticalSection(&server_cs);
479 if (!conn) {
480 ERR("failed to locate connection for handle %p\n", b_handle);
482 if (cconn) RPCRT4_new_client(cconn);
485 HeapFree(GetProcessHeap(), 0, objs);
486 EnterCriticalSection(&server_cs);
487 /* close connections */
488 cps = protseqs;
489 while (cps) {
490 conn = cps->conn;
491 while (conn) {
492 RPCRT4_CloseConnection(conn);
493 conn = conn->Next;
495 cps = cps->Next;
497 LeaveCriticalSection(&server_cs);
498 return 0;
501 static void RPCRT4_start_listen(void)
503 TRACE("\n");
505 EnterCriticalSection(&listen_cs);
506 if (! ++listen_count) {
507 if (!mgr_event) mgr_event = CreateEventA(NULL, TRUE, FALSE, NULL);
508 if (!server_sem) server_sem = CreateSemaphoreA(NULL, 0, MAX_THREADS, NULL);
509 if (!worker_tls) worker_tls = TlsAlloc();
510 std_listen = TRUE;
511 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, NULL, 0, NULL);
512 LeaveCriticalSection(&listen_cs);
513 } else {
514 LeaveCriticalSection(&listen_cs);
515 SetEvent(mgr_event);
519 static void RPCRT4_stop_listen(void)
521 EnterCriticalSection(&listen_cs);
522 if (listen_count == -1)
523 LeaveCriticalSection(&listen_cs);
524 else if (--listen_count == -1) {
525 std_listen = FALSE;
526 LeaveCriticalSection(&listen_cs);
527 SetEvent(mgr_event);
528 } else
529 LeaveCriticalSection(&listen_cs);
530 assert(listen_count > -2);
533 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps)
535 RPCRT4_CreateConnection(&ps->conn, TRUE, ps->Protseq, NULL, ps->Endpoint, NULL, NULL);
537 EnterCriticalSection(&server_cs);
538 ps->Next = protseqs;
539 protseqs = ps;
540 LeaveCriticalSection(&server_cs);
542 if (std_listen) SetEvent(mgr_event);
544 return RPC_S_OK;
547 /***********************************************************************
548 * RpcServerInqBindings (RPCRT4.@)
550 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
552 RPC_STATUS status;
553 DWORD count;
554 RpcServerProtseq* ps;
555 RpcConnection* conn;
557 if (BindingVector)
558 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
559 else
560 ERR("(BindingVector == NULL!!?)\n");
562 EnterCriticalSection(&server_cs);
563 /* count connections */
564 count = 0;
565 ps = protseqs;
566 while (ps) {
567 conn = ps->conn;
568 while (conn) {
569 count++;
570 conn = conn->Next;
572 ps = ps->Next;
574 if (count) {
575 /* export bindings */
576 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
577 sizeof(RPC_BINDING_VECTOR) +
578 sizeof(RPC_BINDING_HANDLE)*(count-1));
579 (*BindingVector)->Count = count;
580 count = 0;
581 ps = protseqs;
582 while (ps) {
583 conn = ps->conn;
584 while (conn) {
585 RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
586 conn);
587 count++;
588 conn = conn->Next;
590 ps = ps->Next;
592 status = RPC_S_OK;
593 } else {
594 *BindingVector = NULL;
595 status = RPC_S_NO_BINDINGS;
597 LeaveCriticalSection(&server_cs);
598 return status;
601 /***********************************************************************
602 * RpcServerUseProtseqEpA (RPCRT4.@)
604 RPC_STATUS WINAPI RpcServerUseProtseqEpA( LPSTR Protseq, UINT MaxCalls, LPSTR Endpoint, LPVOID SecurityDescriptor )
606 RPC_POLICY policy;
608 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
610 /* This should provide the default behaviour */
611 policy.Length = sizeof( policy );
612 policy.EndpointFlags = 0;
613 policy.NICFlags = 0;
615 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
618 /***********************************************************************
619 * RpcServerUseProtseqEpW (RPCRT4.@)
621 RPC_STATUS WINAPI RpcServerUseProtseqEpW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor )
623 RPC_POLICY policy;
625 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
627 /* This should provide the default behaviour */
628 policy.Length = sizeof( policy );
629 policy.EndpointFlags = 0;
630 policy.NICFlags = 0;
632 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
635 /***********************************************************************
636 * RpcServerUseProtseqEpExA (RPCRT4.@)
638 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( LPSTR Protseq, UINT MaxCalls, LPSTR Endpoint, LPVOID SecurityDescriptor,
639 PRPC_POLICY lpPolicy )
641 RpcServerProtseq* ps;
643 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_a( Protseq ), MaxCalls,
644 debugstr_a( Endpoint ), SecurityDescriptor,
645 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
647 ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerProtseq));
648 ps->MaxCalls = MaxCalls;
649 ps->Protseq = RPCRT4_strdupA(Protseq);
650 ps->Endpoint = RPCRT4_strdupA(Endpoint);
652 return RPCRT4_use_protseq(ps);
655 /***********************************************************************
656 * RpcServerUseProtseqEpExW (RPCRT4.@)
658 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor,
659 PRPC_POLICY lpPolicy )
661 RpcServerProtseq* ps;
663 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_w( Protseq ), MaxCalls,
664 debugstr_w( Endpoint ), SecurityDescriptor,
665 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
667 ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerProtseq));
668 ps->MaxCalls = MaxCalls;
669 ps->Protseq = RPCRT4_strdupWtoA(Protseq);
670 ps->Endpoint = RPCRT4_strdupWtoA(Endpoint);
672 return RPCRT4_use_protseq(ps);
675 /***********************************************************************
676 * RpcServerUseProtseqA (RPCRT4.@)
678 RPC_STATUS WINAPI RpcServerUseProtseqA(LPSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
680 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)", debugstr_a(Protseq), MaxCalls, SecurityDescriptor);
681 return RpcServerUseProtseqEpA(Protseq, MaxCalls, NULL, SecurityDescriptor);
684 /***********************************************************************
685 * RpcServerUseProtseqW (RPCRT4.@)
687 RPC_STATUS WINAPI RpcServerUseProtseqW(LPWSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
689 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
690 return RpcServerUseProtseqEpW(Protseq, MaxCalls, NULL, SecurityDescriptor);
693 /***********************************************************************
694 * RpcServerRegisterIf (RPCRT4.@)
696 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
698 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
699 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
702 /***********************************************************************
703 * RpcServerRegisterIfEx (RPCRT4.@)
705 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
706 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
708 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
709 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
712 /***********************************************************************
713 * RpcServerRegisterIf2 (RPCRT4.@)
715 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
716 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
718 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
719 RpcServerInterface* sif;
720 int i;
722 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
723 MaxRpcSize, IfCallbackFn);
724 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
725 If->InterfaceId.SyntaxVersion.MajorVersion,
726 If->InterfaceId.SyntaxVersion.MinorVersion);
727 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
728 If->TransferSyntax.SyntaxVersion.MajorVersion,
729 If->TransferSyntax.SyntaxVersion.MinorVersion);
730 TRACE(" dispatch table: %p\n", If->DispatchTable);
731 if (If->DispatchTable) {
732 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
733 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
734 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
736 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
738 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
739 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
740 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
741 TRACE(" flags: %08x\n", If->Flags);
743 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
744 sif->If = If;
745 if (MgrTypeUuid) {
746 memcpy(&sif->MgrTypeUuid, MgrTypeUuid, sizeof(UUID));
747 sif->MgrEpv = MgrEpv;
748 } else {
749 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
750 sif->MgrEpv = If->DefaultManagerEpv;
752 sif->Flags = Flags;
753 sif->MaxCalls = MaxCalls;
754 sif->MaxRpcSize = MaxRpcSize;
755 sif->IfCallbackFn = IfCallbackFn;
757 EnterCriticalSection(&server_cs);
758 sif->Next = ifs;
759 ifs = sif;
760 LeaveCriticalSection(&server_cs);
762 if (sif->Flags & RPC_IF_AUTOLISTEN) {
763 /* well, start listening, I think... */
764 RPCRT4_start_listen();
767 return RPC_S_OK;
770 /***********************************************************************
771 * RpcServerUnregisterIf (RPCRT4.@)
773 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
775 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, WaitForCallsToComplete == %u): stub\n",
776 IfSpec, debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
778 return RPC_S_OK;
781 /***********************************************************************
782 * RpcServerUnregisterIfEx (RPCRT4.@)
784 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
786 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
787 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
789 return RPC_S_OK;
792 /***********************************************************************
793 * RpcObjectSetType (RPCRT4.@)
795 * PARAMS
796 * ObjUuid [I] "Object" UUID
797 * TypeUuid [I] "Type" UUID
799 * RETURNS
800 * RPC_S_OK The call succeeded
801 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
802 * RPC_S_ALREADY_REGISTERED The provided object is already registered
804 * Maps "Object" UUIDs to "Type" UUID's. Passing the nil UUID as the type
805 * resets the mapping for the specified object UUID to nil (the default).
806 * The nil object is always associated with the nil type and cannot be
807 * reassigned. Servers can support multiple implementations on the same
808 * interface by registering different end-point vectors for the different
809 * types. There's no need to call this if a server only supports the nil
810 * type, as is typical.
812 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
814 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
815 RPC_STATUS dummy;
817 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
818 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
819 /* nil uuid cannot be remapped */
820 return RPC_S_INVALID_OBJECT;
823 /* find the mapping for this object if there is one ... */
824 while (map) {
825 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
826 prev = map;
827 map = map->next;
829 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
830 /* ... and drop it from the list */
831 if (map) {
832 if (prev)
833 prev->next = map->next;
834 else
835 RpcObjTypeMaps = map->next;
836 HeapFree(GetProcessHeap(), 0, map);
838 } else {
839 /* ... , fail if we found it ... */
840 if (map)
841 return RPC_S_ALREADY_REGISTERED;
842 /* ... otherwise create a new one and add it in. */
843 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
844 memcpy(&map->Object, ObjUuid, sizeof(UUID));
845 memcpy(&map->Type, TypeUuid, sizeof(UUID));
846 map->next = NULL;
847 if (prev)
848 prev->next = map; /* prev is the last map in the linklist */
849 else
850 RpcObjTypeMaps = map;
853 return RPC_S_OK;
856 /***********************************************************************
857 * RpcServerRegisterAuthInfoA (RPCRT4.@)
859 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( LPSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
860 LPVOID Arg )
862 FIXME( "(%s,%lu,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
864 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
867 /***********************************************************************
868 * RpcServerRegisterAuthInfoW (RPCRT4.@)
870 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( LPWSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
871 LPVOID Arg )
873 FIXME( "(%s,%lu,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
875 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
878 /***********************************************************************
879 * RpcServerListen (RPCRT4.@)
881 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
883 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
885 if (!protseqs)
886 return RPC_S_NO_PROTSEQS_REGISTERED;
888 EnterCriticalSection(&listen_cs);
890 if (std_listen) {
891 LeaveCriticalSection(&listen_cs);
892 return RPC_S_ALREADY_LISTENING;
895 RPCRT4_start_listen();
897 LeaveCriticalSection(&listen_cs);
899 if (DontWait) return RPC_S_OK;
901 return RpcMgmtWaitServerListen();
904 /***********************************************************************
905 * RpcMgmtServerWaitListen (RPCRT4.@)
907 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
909 RPC_STATUS rslt = RPC_S_OK;
911 TRACE("\n");
913 EnterCriticalSection(&listen_cs);
915 if (!std_listen)
916 if ( (rslt = RpcServerListen(1, 0, TRUE)) != RPC_S_OK ) {
917 LeaveCriticalSection(&listen_cs);
918 return rslt;
921 LeaveCriticalSection(&listen_cs);
923 while (std_listen) {
924 WaitForSingleObject(mgr_event, INFINITE);
925 if (!std_listen) {
926 Sleep(100); /* don't spin violently */
927 TRACE("spinning.\n");
931 return rslt;
934 /***********************************************************************
935 * RpcMgmtStopServerListening (RPCRT4.@)
937 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
939 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
941 if (Binding) {
942 FIXME("client-side invocation not implemented.\n");
943 return RPC_S_WRONG_KIND_OF_BINDING;
946 /* hmm... */
947 EnterCriticalSection(&listen_cs);
948 while (std_listen)
949 RPCRT4_stop_listen();
950 LeaveCriticalSection(&listen_cs);
952 return RPC_S_OK;
955 /***********************************************************************
956 * I_RpcServerStartListening (RPCRT4.@)
958 RPC_STATUS WINAPI I_RpcServerStartListening( void* hWnd )
960 FIXME( "(%p): stub\n", hWnd );
962 return RPC_S_OK;
965 /***********************************************************************
966 * I_RpcServerStopListening (RPCRT4.@)
968 RPC_STATUS WINAPI I_RpcServerStopListening( void )
970 FIXME( "(): stub\n" );
972 return RPC_S_OK;
975 /***********************************************************************
976 * I_RpcWindowProc (RPCRT4.@)
978 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
980 FIXME( "(%p,%08x,%08x,%08lx): stub\n", hWnd, Message, wParam, lParam );
982 return 0;