Merge 14981:15268 from trunk
[reactos.git] / reactos / lib / rpcrt4 / rpc_server.c
1 /*
2 * RPC server API
3 *
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 *
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.
11 *
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * TODO:
22 * - a whole lot
23 */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "winreg.h"
37 #include "ntstatus.h"
38
39 #include "rpc.h"
40 #include "rpcndr.h"
41 #include "excpt.h"
42
43 #include "wine/debug.h"
44 #include "wine/exception.h"
45
46 #include "rpc_server.h"
47 #include "rpc_misc.h"
48 #include "rpc_message.h"
49 #include "rpc_defs.h"
50
51 #define MAX_THREADS 128
52
53 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
54
55 typedef struct _RpcPacket
56 {
57 struct _RpcPacket* next;
58 struct _RpcConnection* conn;
59 RpcPktHdr* hdr;
60 RPC_MESSAGE* msg;
61 } RpcPacket;
62
63 typedef struct _RpcObjTypeMap
64 {
65 /* FIXME: a hash table would be better. */
66 struct _RpcObjTypeMap *next;
67 UUID Object;
68 UUID Type;
69 } RpcObjTypeMap;
70
71 static RpcObjTypeMap *RpcObjTypeMaps;
72
73 static RpcServerProtseq* protseqs;
74 static RpcServerInterface* ifs;
75
76 static CRITICAL_SECTION server_cs;
77 static CRITICAL_SECTION_DEBUG server_cs_debug =
78 {
79 0, 0, &server_cs,
80 { &server_cs_debug.ProcessLocksList, &server_cs_debug.ProcessLocksList },
81 0, 0, { 0, (DWORD)(__FILE__ ": server_cs") }
82 };
83 static CRITICAL_SECTION server_cs = { &server_cs_debug, -1, 0, 0, 0, 0 };
84
85 static CRITICAL_SECTION listen_cs;
86 static CRITICAL_SECTION_DEBUG listen_cs_debug =
87 {
88 0, 0, &listen_cs,
89 { &listen_cs_debug.ProcessLocksList, &listen_cs_debug.ProcessLocksList },
90 0, 0, { 0, (DWORD)(__FILE__ ": listen_cs") }
91 };
92 static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };
93
94 /* whether the server is currently listening */
95 static BOOL std_listen;
96 /* number of manual listeners (calls to RpcServerListen) */
97 static LONG manual_listen_count;
98 /* total listeners including auto listeners */
99 static LONG listen_count;
100 /* set on change of configuration (e.g. listening on new protseq) */
101 static HANDLE mgr_event;
102 /* mutex for ensuring only one thread can change state at a time */
103 static HANDLE mgr_mutex;
104 /* set when server thread has finished opening connections */
105 static HANDLE server_ready_event;
106 /* thread that waits for connections */
107 static HANDLE server_thread;
108
109 static CRITICAL_SECTION spacket_cs;
110 static CRITICAL_SECTION_DEBUG spacket_cs_debug =
111 {
112 0, 0, &spacket_cs,
113 { &spacket_cs_debug.ProcessLocksList, &spacket_cs_debug.ProcessLocksList },
114 0, 0, { 0, (DWORD)(__FILE__ ": spacket_cs") }
115 };
116 static CRITICAL_SECTION spacket_cs = { &spacket_cs_debug, -1, 0, 0, 0, 0 };
117
118 static RpcPacket* spacket_head;
119 static RpcPacket* spacket_tail;
120 static HANDLE server_sem;
121
122 static DWORD worker_count, worker_free, worker_tls;
123
124 static UUID uuid_nil;
125
126 inline static RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
127 {
128 RpcObjTypeMap *rslt = RpcObjTypeMaps;
129 RPC_STATUS dummy;
130
131 while (rslt) {
132 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
133 rslt = rslt->next;
134 }
135
136 return rslt;
137 }
138
139 inline static UUID *LookupObjType(UUID *ObjUuid)
140 {
141 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
142 if (map)
143 return &map->Type;
144 else
145 return &uuid_nil;
146 }
147
148 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
149 RPC_SYNTAX_IDENTIFIER* if_id,
150 BOOL check_object)
151 {
152 UUID* MgrType = NULL;
153 RpcServerInterface* cif = NULL;
154 RPC_STATUS status;
155
156 if (check_object)
157 MgrType = LookupObjType(object);
158 EnterCriticalSection(&server_cs);
159 cif = ifs;
160 while (cif) {
161 if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
162 (check_object == FALSE || UuidEqual(MgrType, &cif->MgrTypeUuid, &status)) &&
163 std_listen) break;
164 cif = cif->Next;
165 }
166 LeaveCriticalSection(&server_cs);
167 TRACE("returning %p for %s\n", cif, debugstr_guid(object));
168 return cif;
169 }
170
171 static void RPCRT4_push_packet(RpcPacket* packet)
172 {
173 packet->next = NULL;
174 EnterCriticalSection(&spacket_cs);
175 if (spacket_tail) {
176 spacket_tail->next = packet;
177 spacket_tail = packet;
178 } else {
179 spacket_head = packet;
180 spacket_tail = packet;
181 }
182 LeaveCriticalSection(&spacket_cs);
183 }
184
185 static RpcPacket* RPCRT4_pop_packet(void)
186 {
187 RpcPacket* packet;
188 EnterCriticalSection(&spacket_cs);
189 packet = spacket_head;
190 if (packet) {
191 spacket_head = packet->next;
192 if (!spacket_head) spacket_tail = NULL;
193 }
194 LeaveCriticalSection(&spacket_cs);
195 if (packet) packet->next = NULL;
196 return packet;
197 }
198
199 #ifndef __REACTOS__
200 typedef struct {
201 PRPC_MESSAGE msg;
202 void* buf;
203 } packet_state;
204
205 static WINE_EXCEPTION_FILTER(rpc_filter)
206 {
207 packet_state* state;
208 PRPC_MESSAGE msg;
209 state = TlsGetValue(worker_tls);
210 msg = state->msg;
211 if (msg->Buffer != state->buf) I_RpcFreeBuffer(msg);
212 msg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
213 msg->BufferLength = sizeof(DWORD);
214 I_RpcGetBuffer(msg);
215 *(DWORD*)msg->Buffer = GetExceptionCode();
216 WARN("exception caught with code 0x%08lx = %ld\n", *(DWORD*)msg->Buffer, *(DWORD*)msg->Buffer);
217 TRACE("returning failure packet\n");
218 return EXCEPTION_EXECUTE_HANDLER;
219 }
220 #endif
221
222 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr, RPC_MESSAGE* msg)
223 {
224 RpcServerInterface* sif;
225 RPC_DISPATCH_FUNCTION func;
226 #ifndef __REACTOS__
227 packet_state state;
228 #endif
229 UUID *object_uuid;
230 RpcPktHdr *response;
231 void *buf = msg->Buffer;
232 RPC_STATUS status;
233
234 #ifndef __REACTOS__
235 state.msg = msg;
236 state.buf = buf;
237 TlsSetValue(worker_tls, &state);
238 #endif
239
240 switch (hdr->common.ptype) {
241 case PKT_BIND:
242 TRACE("got bind packet\n");
243
244 /* FIXME: do more checks! */
245 if (hdr->bind.max_tsize < RPC_MIN_PACKET_SIZE ||
246 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
247 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
248 sif = NULL;
249 } else {
250 sif = RPCRT4_find_interface(NULL, &hdr->bind.abstract, FALSE);
251 }
252 if (sif == NULL) {
253 TRACE("rejecting bind request on connection %p\n", conn);
254 /* Report failure to client. */
255 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
256 RPC_VER_MAJOR, RPC_VER_MINOR);
257 } else {
258 TRACE("accepting bind request on connection %p\n", conn);
259
260 /* accept. */
261 response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
262 RPC_MAX_PACKET_SIZE,
263 RPC_MAX_PACKET_SIZE,
264 conn->Endpoint,
265 RESULT_ACCEPT, NO_REASON,
266 &sif->If->TransferSyntax);
267
268 /* save the interface for later use */
269 conn->ActiveInterface = hdr->bind.abstract;
270 conn->MaxTransmissionSize = hdr->bind.max_tsize;
271 }
272
273 if (RPCRT4_Send(conn, response, NULL, 0) != RPC_S_OK)
274 goto fail;
275
276 break;
277
278 case PKT_REQUEST:
279 TRACE("got request packet\n");
280
281 /* fail if the connection isn't bound with an interface */
282 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
283 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
284 status);
285
286 RPCRT4_Send(conn, response, NULL, 0);
287 break;
288 }
289
290 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
291 object_uuid = (UUID*)(&hdr->request + 1);
292 } else {
293 object_uuid = NULL;
294 }
295
296 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, TRUE);
297 msg->RpcInterfaceInformation = sif->If;
298 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
299 msg->ManagerEpv = sif->MgrEpv;
300 if (object_uuid != NULL) {
301 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
302 }
303
304 /* find dispatch function */
305 msg->ProcNum = hdr->request.opnum;
306 if (sif->Flags & RPC_IF_OLE) {
307 /* native ole32 always gives us a dispatch table with a single entry
308 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
309 func = *sif->If->DispatchTable->DispatchTable;
310 } else {
311 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
312 ERR("invalid procnum\n");
313 func = NULL;
314 }
315 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
316 }
317
318 /* put in the drep. FIXME: is this more universally applicable?
319 perhaps we should move this outward... */
320 msg->DataRepresentation =
321 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
322 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
323
324 /* dispatch */
325 #ifndef __REACTOS__
326 __TRY {
327 if (func) func(msg);
328 } __EXCEPT(rpc_filter) {
329 /* failure packet was created in rpc_filter */
330 } __ENDTRY
331 #else
332 if (func) func(msg);
333 #endif
334
335 /* send response packet */
336 I_RpcSend(msg);
337
338 msg->RpcInterfaceInformation = NULL;
339
340 break;
341
342 default:
343 FIXME("unhandled packet type\n");
344 break;
345 }
346
347 fail:
348 /* clean up */
349 if (msg->Buffer == buf) msg->Buffer = NULL;
350 TRACE("freeing Buffer=%p\n", buf);
351 HeapFree(GetProcessHeap(), 0, buf);
352 RPCRT4_DestroyBinding(msg->Handle);
353 msg->Handle = 0;
354 I_RpcFreeBuffer(msg);
355 msg->Buffer = NULL;
356 RPCRT4_FreeHeader(hdr);
357 #ifndef __REACTOS__
358 TlsSetValue(worker_tls, NULL);
359 #endif
360 }
361
362 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
363 {
364 DWORD obj;
365 RpcPacket* pkt;
366
367 for (;;) {
368 /* idle timeout after 5s */
369 obj = WaitForSingleObject(server_sem, 5000);
370 if (obj == WAIT_TIMEOUT) {
371 /* if another idle thread exist, self-destruct */
372 if (worker_free > 1) break;
373 continue;
374 }
375 pkt = RPCRT4_pop_packet();
376 if (!pkt) continue;
377 InterlockedDecrement(&worker_free);
378 for (;;) {
379 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg);
380 HeapFree(GetProcessHeap(), 0, pkt);
381 /* try to grab another packet here without waiting
382 * on the semaphore, in case it hits max */
383 pkt = RPCRT4_pop_packet();
384 if (!pkt) break;
385 /* decrement semaphore */
386 WaitForSingleObject(server_sem, 0);
387 }
388 InterlockedIncrement(&worker_free);
389 }
390 InterlockedDecrement(&worker_free);
391 InterlockedDecrement(&worker_count);
392 return 0;
393 }
394
395 static void RPCRT4_create_worker_if_needed(void)
396 {
397 if (!worker_free && worker_count < MAX_THREADS) {
398 HANDLE thread;
399 InterlockedIncrement(&worker_count);
400 InterlockedIncrement(&worker_free);
401 thread = CreateThread(NULL, 0, RPCRT4_worker_thread, NULL, 0, NULL);
402 if (thread) CloseHandle(thread);
403 else {
404 InterlockedDecrement(&worker_free);
405 InterlockedDecrement(&worker_count);
406 }
407 }
408 }
409
410 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
411 {
412 RpcConnection* conn = (RpcConnection*)the_arg;
413 RpcPktHdr *hdr;
414 RpcBinding *pbind;
415 RPC_MESSAGE *msg;
416 RPC_STATUS status;
417 RpcPacket *packet;
418
419 TRACE("(%p)\n", conn);
420
421 for (;;) {
422 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
423
424 /* create temporary binding for dispatch, it will be freed in
425 * RPCRT4_process_packet */
426 RPCRT4_MakeBinding(&pbind, conn);
427 msg->Handle = (RPC_BINDING_HANDLE)pbind;
428
429 status = RPCRT4_Receive(conn, &hdr, msg);
430 if (status != RPC_S_OK) {
431 WARN("receive failed with error %lx\n", status);
432 break;
433 }
434
435 #if 0
436 RPCRT4_process_packet(conn, hdr, msg);
437 #else
438 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
439 packet->conn = conn;
440 packet->hdr = hdr;
441 packet->msg = msg;
442 RPCRT4_create_worker_if_needed();
443 RPCRT4_push_packet(packet);
444 ReleaseSemaphore(server_sem, 1, NULL);
445 #endif
446 msg = NULL;
447 }
448 HeapFree(GetProcessHeap(), 0, msg);
449 RPCRT4_DestroyConnection(conn);
450 return 0;
451 }
452
453 static void RPCRT4_new_client(RpcConnection* conn)
454 {
455 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
456 if (!thread) {
457 DWORD err = GetLastError();
458 ERR("failed to create thread, error=%08lx\n", err);
459 RPCRT4_DestroyConnection(conn);
460 }
461 /* we could set conn->thread, but then we'd have to make the io_thread wait
462 * for that, otherwise the thread might finish, destroy the connection, and
463 * free the memory we'd write to before we did, causing crashes and stuff -
464 * so let's implement that later, when we really need conn->thread */
465
466 CloseHandle( thread );
467 }
468
469 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
470 {
471 HANDLE m_event = mgr_event, b_handle;
472 HANDLE *objs = NULL;
473 DWORD count, res;
474 RpcServerProtseq* cps;
475 RpcConnection* conn;
476 RpcConnection* cconn;
477 BOOL set_ready_event = FALSE;
478
479 TRACE("(the_arg == ^%p)\n", the_arg);
480
481 for (;;) {
482 EnterCriticalSection(&server_cs);
483 /* open and count connections */
484 count = 1;
485 cps = protseqs;
486 while (cps) {
487 conn = cps->conn;
488 while (conn) {
489 RPCRT4_OpenConnection(conn);
490 if (conn->ovl[0].hEvent) count++;
491 conn = conn->Next;
492 }
493 cps = cps->Next;
494 }
495 /* make array of connections */
496 if (objs)
497 objs = HeapReAlloc(GetProcessHeap(), 0, objs, count*sizeof(HANDLE));
498 else
499 objs = HeapAlloc(GetProcessHeap(), 0, count*sizeof(HANDLE));
500
501 objs[0] = m_event;
502 count = 1;
503 cps = protseqs;
504 while (cps) {
505 conn = cps->conn;
506 while (conn) {
507 if (conn->ovl[0].hEvent) objs[count++] = conn->ovl[0].hEvent;
508 conn = conn->Next;
509 }
510 cps = cps->Next;
511 }
512 LeaveCriticalSection(&server_cs);
513
514 if (set_ready_event)
515 {
516 /* signal to function that changed state that we are now sync'ed */
517 SetEvent(server_ready_event);
518 set_ready_event = FALSE;
519 }
520
521 /* start waiting */
522 res = WaitForMultipleObjects(count, objs, FALSE, INFINITE);
523 if (res == WAIT_OBJECT_0) {
524 if (!std_listen)
525 {
526 SetEvent(server_ready_event);
527 break;
528 }
529 set_ready_event = TRUE;
530 }
531 else if (res == WAIT_FAILED) {
532 ERR("wait failed\n");
533 }
534 else {
535 b_handle = objs[res - WAIT_OBJECT_0];
536 /* find which connection got a RPC */
537 EnterCriticalSection(&server_cs);
538 conn = NULL;
539 cps = protseqs;
540 while (cps) {
541 conn = cps->conn;
542 while (conn) {
543 if (conn->ovl[0].hEvent == b_handle) break;
544 conn = conn->Next;
545 }
546 if (conn) break;
547 cps = cps->Next;
548 }
549 cconn = NULL;
550 if (conn) RPCRT4_SpawnConnection(&cconn, conn);
551 LeaveCriticalSection(&server_cs);
552 if (!conn) {
553 ERR("failed to locate connection for handle %p\n", b_handle);
554 }
555 if (cconn) RPCRT4_new_client(cconn);
556 }
557 }
558 HeapFree(GetProcessHeap(), 0, objs);
559 EnterCriticalSection(&server_cs);
560 /* close connections */
561 cps = protseqs;
562 while (cps) {
563 conn = cps->conn;
564 while (conn) {
565 RPCRT4_CloseConnection(conn);
566 conn = conn->Next;
567 }
568 cps = cps->Next;
569 }
570 LeaveCriticalSection(&server_cs);
571 return 0;
572 }
573
574 /* tells the server thread that the state has changed and waits for it to
575 * make the changes */
576 static void RPCRT4_sync_with_server_thread(void)
577 {
578 /* make sure we are the only thread sync'ing the server state, otherwise
579 * there is a race with the server thread setting an older state and setting
580 * the server_ready_event when the new state hasn't yet been applied */
581 WaitForSingleObject(mgr_mutex, INFINITE);
582
583 SetEvent(mgr_event);
584 /* wait for server thread to make the requested changes before returning */
585 WaitForSingleObject(server_ready_event, INFINITE);
586
587 ReleaseMutex(mgr_mutex);
588 }
589
590 static void RPCRT4_start_listen(BOOL auto_listen)
591 {
592 TRACE("\n");
593
594 EnterCriticalSection(&listen_cs);
595 if (auto_listen || (manual_listen_count++ == 0))
596 {
597 if (++listen_count == 1) {
598 /* first listener creates server thread */
599 if (!mgr_mutex) mgr_mutex = CreateMutexW(NULL, FALSE, NULL);
600 if (!mgr_event) mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
601 if (!server_ready_event) server_ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
602 if (!server_sem) server_sem = CreateSemaphoreW(NULL, 0, MAX_THREADS, NULL);
603 if (!worker_tls) worker_tls = TlsAlloc();
604 std_listen = TRUE;
605 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, NULL, 0, NULL);
606 } else {
607 LeaveCriticalSection(&listen_cs);
608 RPCRT4_sync_with_server_thread();
609 return;
610 }
611 }
612 LeaveCriticalSection(&listen_cs);
613 }
614
615 static void RPCRT4_stop_listen(BOOL auto_listen)
616 {
617 EnterCriticalSection(&listen_cs);
618 if (auto_listen || (--manual_listen_count == 0))
619 {
620 if (listen_count != 0 && --listen_count == 0) {
621 std_listen = FALSE;
622 LeaveCriticalSection(&listen_cs);
623 RPCRT4_sync_with_server_thread();
624 return;
625 }
626 assert(listen_count >= 0);
627 }
628 LeaveCriticalSection(&listen_cs);
629 }
630
631 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps)
632 {
633 RPCRT4_CreateConnection(&ps->conn, TRUE, ps->Protseq, NULL, ps->Endpoint, NULL, NULL);
634
635 EnterCriticalSection(&server_cs);
636 ps->Next = protseqs;
637 protseqs = ps;
638 LeaveCriticalSection(&server_cs);
639
640 if (std_listen) RPCRT4_sync_with_server_thread();
641
642 return RPC_S_OK;
643 }
644
645 /***********************************************************************
646 * RpcServerInqBindings (RPCRT4.@)
647 */
648 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
649 {
650 RPC_STATUS status;
651 DWORD count;
652 RpcServerProtseq* ps;
653 RpcConnection* conn;
654
655 if (BindingVector)
656 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
657 else
658 ERR("(BindingVector == NULL!!?)\n");
659
660 EnterCriticalSection(&server_cs);
661 /* count connections */
662 count = 0;
663 ps = protseqs;
664 while (ps) {
665 conn = ps->conn;
666 while (conn) {
667 count++;
668 conn = conn->Next;
669 }
670 ps = ps->Next;
671 }
672 if (count) {
673 /* export bindings */
674 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
675 sizeof(RPC_BINDING_VECTOR) +
676 sizeof(RPC_BINDING_HANDLE)*(count-1));
677 (*BindingVector)->Count = count;
678 count = 0;
679 ps = protseqs;
680 while (ps) {
681 conn = ps->conn;
682 while (conn) {
683 RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
684 conn);
685 count++;
686 conn = conn->Next;
687 }
688 ps = ps->Next;
689 }
690 status = RPC_S_OK;
691 } else {
692 *BindingVector = NULL;
693 status = RPC_S_NO_BINDINGS;
694 }
695 LeaveCriticalSection(&server_cs);
696 return status;
697 }
698
699 /***********************************************************************
700 * RpcServerUseProtseqEpA (RPCRT4.@)
701 */
702 RPC_STATUS WINAPI RpcServerUseProtseqEpA( unsigned char *Protseq, UINT MaxCalls, unsigned char *Endpoint, LPVOID SecurityDescriptor )
703 {
704 RPC_POLICY policy;
705
706 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
707
708 /* This should provide the default behaviour */
709 policy.Length = sizeof( policy );
710 policy.EndpointFlags = 0;
711 policy.NICFlags = 0;
712
713 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
714 }
715
716 /***********************************************************************
717 * RpcServerUseProtseqEpW (RPCRT4.@)
718 */
719 RPC_STATUS WINAPI RpcServerUseProtseqEpW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor )
720 {
721 RPC_POLICY policy;
722
723 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
724
725 /* This should provide the default behaviour */
726 policy.Length = sizeof( policy );
727 policy.EndpointFlags = 0;
728 policy.NICFlags = 0;
729
730 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
731 }
732
733 /***********************************************************************
734 * RpcServerUseProtseqEpExA (RPCRT4.@)
735 */
736 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( unsigned char *Protseq, UINT MaxCalls, unsigned char *Endpoint, LPVOID SecurityDescriptor,
737 PRPC_POLICY lpPolicy )
738 {
739 RpcServerProtseq* ps;
740
741 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_a( Protseq ), MaxCalls,
742 debugstr_a( Endpoint ), SecurityDescriptor,
743 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
744
745 ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerProtseq));
746 ps->MaxCalls = MaxCalls;
747 ps->Protseq = RPCRT4_strdupA(Protseq);
748 ps->Endpoint = RPCRT4_strdupA(Endpoint);
749
750 return RPCRT4_use_protseq(ps);
751 }
752
753 /***********************************************************************
754 * RpcServerUseProtseqEpExW (RPCRT4.@)
755 */
756 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor,
757 PRPC_POLICY lpPolicy )
758 {
759 RpcServerProtseq* ps;
760
761 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_w( Protseq ), MaxCalls,
762 debugstr_w( Endpoint ), SecurityDescriptor,
763 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
764
765 ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerProtseq));
766 ps->MaxCalls = MaxCalls;
767 ps->Protseq = RPCRT4_strdupWtoA(Protseq);
768 ps->Endpoint = RPCRT4_strdupWtoA(Endpoint);
769
770 return RPCRT4_use_protseq(ps);
771 }
772
773 /***********************************************************************
774 * RpcServerUseProtseqA (RPCRT4.@)
775 */
776 RPC_STATUS WINAPI RpcServerUseProtseqA(unsigned char *Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
777 {
778 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a(Protseq), MaxCalls, SecurityDescriptor);
779 return RpcServerUseProtseqEpA(Protseq, MaxCalls, NULL, SecurityDescriptor);
780 }
781
782 /***********************************************************************
783 * RpcServerUseProtseqW (RPCRT4.@)
784 */
785 RPC_STATUS WINAPI RpcServerUseProtseqW(LPWSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
786 {
787 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
788 return RpcServerUseProtseqEpW(Protseq, MaxCalls, NULL, SecurityDescriptor);
789 }
790
791 /***********************************************************************
792 * RpcServerRegisterIf (RPCRT4.@)
793 */
794 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
795 {
796 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
797 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
798 }
799
800 /***********************************************************************
801 * RpcServerRegisterIfEx (RPCRT4.@)
802 */
803 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
804 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
805 {
806 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
807 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
808 }
809
810 /***********************************************************************
811 * RpcServerRegisterIf2 (RPCRT4.@)
812 */
813 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
814 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
815 {
816 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
817 RpcServerInterface* sif;
818 unsigned int i;
819
820 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
821 MaxRpcSize, IfCallbackFn);
822 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
823 If->InterfaceId.SyntaxVersion.MajorVersion,
824 If->InterfaceId.SyntaxVersion.MinorVersion);
825 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
826 If->TransferSyntax.SyntaxVersion.MajorVersion,
827 If->TransferSyntax.SyntaxVersion.MinorVersion);
828 TRACE(" dispatch table: %p\n", If->DispatchTable);
829 if (If->DispatchTable) {
830 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
831 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
832 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
833 }
834 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
835 }
836 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
837 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
838 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
839
840 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
841 sif->If = If;
842 if (MgrTypeUuid) {
843 memcpy(&sif->MgrTypeUuid, MgrTypeUuid, sizeof(UUID));
844 sif->MgrEpv = MgrEpv;
845 } else {
846 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
847 sif->MgrEpv = If->DefaultManagerEpv;
848 }
849 sif->Flags = Flags;
850 sif->MaxCalls = MaxCalls;
851 sif->MaxRpcSize = MaxRpcSize;
852 sif->IfCallbackFn = IfCallbackFn;
853
854 EnterCriticalSection(&server_cs);
855 sif->Next = ifs;
856 ifs = sif;
857 LeaveCriticalSection(&server_cs);
858
859 if (sif->Flags & RPC_IF_AUTOLISTEN) {
860 /* well, start listening, I think... */
861 RPCRT4_start_listen(TRUE);
862 }
863
864 return RPC_S_OK;
865 }
866
867 /***********************************************************************
868 * RpcServerUnregisterIf (RPCRT4.@)
869 */
870 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
871 {
872 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, WaitForCallsToComplete == %u): stub\n",
873 IfSpec, debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
874
875 return RPC_S_OK;
876 }
877
878 /***********************************************************************
879 * RpcServerUnregisterIfEx (RPCRT4.@)
880 */
881 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
882 {
883 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
884 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
885
886 return RPC_S_OK;
887 }
888
889 /***********************************************************************
890 * RpcObjectSetType (RPCRT4.@)
891 *
892 * PARAMS
893 * ObjUuid [I] "Object" UUID
894 * TypeUuid [I] "Type" UUID
895 *
896 * RETURNS
897 * RPC_S_OK The call succeeded
898 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
899 * RPC_S_ALREADY_REGISTERED The provided object is already registered
900 *
901 * Maps "Object" UUIDs to "Type" UUID's. Passing the nil UUID as the type
902 * resets the mapping for the specified object UUID to nil (the default).
903 * The nil object is always associated with the nil type and cannot be
904 * reassigned. Servers can support multiple implementations on the same
905 * interface by registering different end-point vectors for the different
906 * types. There's no need to call this if a server only supports the nil
907 * type, as is typical.
908 */
909 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
910 {
911 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
912 RPC_STATUS dummy;
913
914 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
915 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
916 /* nil uuid cannot be remapped */
917 return RPC_S_INVALID_OBJECT;
918 }
919
920 /* find the mapping for this object if there is one ... */
921 while (map) {
922 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
923 prev = map;
924 map = map->next;
925 }
926 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
927 /* ... and drop it from the list */
928 if (map) {
929 if (prev)
930 prev->next = map->next;
931 else
932 RpcObjTypeMaps = map->next;
933 HeapFree(GetProcessHeap(), 0, map);
934 }
935 } else {
936 /* ... , fail if we found it ... */
937 if (map)
938 return RPC_S_ALREADY_REGISTERED;
939 /* ... otherwise create a new one and add it in. */
940 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
941 memcpy(&map->Object, ObjUuid, sizeof(UUID));
942 memcpy(&map->Type, TypeUuid, sizeof(UUID));
943 map->next = NULL;
944 if (prev)
945 prev->next = map; /* prev is the last map in the linklist */
946 else
947 RpcObjTypeMaps = map;
948 }
949
950 return RPC_S_OK;
951 }
952
953 /***********************************************************************
954 * RpcServerRegisterAuthInfoA (RPCRT4.@)
955 */
956 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( unsigned char *ServerPrincName, unsigned long AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
957 LPVOID Arg )
958 {
959 FIXME( "(%s,%lu,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
960
961 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
962 }
963
964 /***********************************************************************
965 * RpcServerRegisterAuthInfoW (RPCRT4.@)
966 */
967 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( LPWSTR ServerPrincName, unsigned long AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
968 LPVOID Arg )
969 {
970 FIXME( "(%s,%lu,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
971
972 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
973 }
974
975 /***********************************************************************
976 * RpcServerListen (RPCRT4.@)
977 */
978 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
979 {
980 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
981
982 if (!protseqs)
983 return RPC_S_NO_PROTSEQS_REGISTERED;
984
985 EnterCriticalSection(&listen_cs);
986
987 if (std_listen) {
988 LeaveCriticalSection(&listen_cs);
989 return RPC_S_ALREADY_LISTENING;
990 }
991
992 RPCRT4_start_listen(FALSE);
993
994 LeaveCriticalSection(&listen_cs);
995
996 if (DontWait) return RPC_S_OK;
997
998 return RpcMgmtWaitServerListen();
999 }
1000
1001 /***********************************************************************
1002 * RpcMgmtServerWaitListen (RPCRT4.@)
1003 */
1004 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
1005 {
1006 RPC_STATUS rslt = RPC_S_OK;
1007
1008 TRACE("\n");
1009
1010 EnterCriticalSection(&listen_cs);
1011
1012 if (!std_listen)
1013 if ( (rslt = RpcServerListen(1, 0, TRUE)) != RPC_S_OK ) {
1014 LeaveCriticalSection(&listen_cs);
1015 return rslt;
1016 }
1017
1018 LeaveCriticalSection(&listen_cs);
1019
1020 while (std_listen) {
1021 WaitForSingleObject(mgr_event, INFINITE);
1022 if (!std_listen) {
1023 Sleep(100); /* don't spin violently */
1024 TRACE("spinning.\n");
1025 }
1026 }
1027
1028 return rslt;
1029 }
1030
1031 /***********************************************************************
1032 * RpcMgmtStopServerListening (RPCRT4.@)
1033 */
1034 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1035 {
1036 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1037
1038 if (Binding) {
1039 FIXME("client-side invocation not implemented.\n");
1040 return RPC_S_WRONG_KIND_OF_BINDING;
1041 }
1042
1043 RPCRT4_stop_listen(FALSE);
1044
1045 return RPC_S_OK;
1046 }
1047
1048 /***********************************************************************
1049 * I_RpcServerStartListening (RPCRT4.@)
1050 */
1051 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1052 {
1053 FIXME( "(%p): stub\n", hWnd );
1054
1055 return RPC_S_OK;
1056 }
1057
1058 /***********************************************************************
1059 * I_RpcServerStopListening (RPCRT4.@)
1060 */
1061 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1062 {
1063 FIXME( "(): stub\n" );
1064
1065 return RPC_S_OK;
1066 }
1067
1068 /***********************************************************************
1069 * I_RpcWindowProc (RPCRT4.@)
1070 */
1071 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1072 {
1073 FIXME( "(%p,%08x,%08x,%08lx): stub\n", hWnd, Message, wParam, lParam );
1074
1075 return 0;
1076 }