Sync to Wine-0_9_5:
[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
38 #include "rpc.h"
39 #include "rpcndr.h"
40 #include "excpt.h"
41
42 #include "wine/debug.h"
43 #include "wine/exception.h"
44
45 #include "rpc_server.h"
46 #include "rpc_misc.h"
47 #include "rpc_message.h"
48 #include "rpc_defs.h"
49
50 #define MAX_THREADS 128
51
52 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
53
54 typedef struct _RpcPacket
55 {
56 struct _RpcPacket* next;
57 struct _RpcConnection* conn;
58 RpcPktHdr* hdr;
59 RPC_MESSAGE* msg;
60 } RpcPacket;
61
62 typedef struct _RpcObjTypeMap
63 {
64 /* FIXME: a hash table would be better. */
65 struct _RpcObjTypeMap *next;
66 UUID Object;
67 UUID Type;
68 } RpcObjTypeMap;
69
70 static RpcObjTypeMap *RpcObjTypeMaps;
71
72 static RpcServerProtseq* protseqs;
73 static RpcServerInterface* ifs;
74
75 static CRITICAL_SECTION server_cs;
76 static CRITICAL_SECTION_DEBUG server_cs_debug =
77 {
78 0, 0, &server_cs,
79 { &server_cs_debug.ProcessLocksList, &server_cs_debug.ProcessLocksList },
80 0, 0, { (DWORD_PTR)(__FILE__ ": server_cs") }
81 };
82 static CRITICAL_SECTION server_cs = { &server_cs_debug, -1, 0, 0, 0, 0 };
83
84 static CRITICAL_SECTION listen_cs;
85 static CRITICAL_SECTION_DEBUG listen_cs_debug =
86 {
87 0, 0, &listen_cs,
88 { &listen_cs_debug.ProcessLocksList, &listen_cs_debug.ProcessLocksList },
89 0, 0, { (DWORD_PTR)(__FILE__ ": listen_cs") }
90 };
91 static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };
92
93 /* whether the server is currently listening */
94 static BOOL std_listen;
95 /* number of manual listeners (calls to RpcServerListen) */
96 static LONG manual_listen_count;
97 /* total listeners including auto listeners */
98 static LONG listen_count;
99 /* set on change of configuration (e.g. listening on new protseq) */
100 static HANDLE mgr_event;
101 /* mutex for ensuring only one thread can change state at a time */
102 static HANDLE mgr_mutex;
103 /* set when server thread has finished opening connections */
104 static HANDLE server_ready_event;
105
106 static CRITICAL_SECTION spacket_cs;
107 static CRITICAL_SECTION_DEBUG spacket_cs_debug =
108 {
109 0, 0, &spacket_cs,
110 { &spacket_cs_debug.ProcessLocksList, &spacket_cs_debug.ProcessLocksList },
111 0, 0, { (DWORD_PTR)(__FILE__ ": spacket_cs") }
112 };
113 static CRITICAL_SECTION spacket_cs = { &spacket_cs_debug, -1, 0, 0, 0, 0 };
114
115 static RpcPacket* spacket_head;
116 static RpcPacket* spacket_tail;
117 static HANDLE server_sem;
118
119 static LONG worker_count, worker_free, worker_tls;
120
121 static UUID uuid_nil;
122
123 inline static RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
124 {
125 RpcObjTypeMap *rslt = RpcObjTypeMaps;
126 RPC_STATUS dummy;
127
128 while (rslt) {
129 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
130 rslt = rslt->next;
131 }
132
133 return rslt;
134 }
135
136 inline static UUID *LookupObjType(UUID *ObjUuid)
137 {
138 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
139 if (map)
140 return &map->Type;
141 else
142 return &uuid_nil;
143 }
144
145 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
146 RPC_SYNTAX_IDENTIFIER* if_id,
147 BOOL check_object)
148 {
149 UUID* MgrType = NULL;
150 RpcServerInterface* cif = NULL;
151 RPC_STATUS status;
152
153 if (check_object)
154 MgrType = LookupObjType(object);
155 EnterCriticalSection(&server_cs);
156 cif = ifs;
157 while (cif) {
158 if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
159 (check_object == FALSE || UuidEqual(MgrType, &cif->MgrTypeUuid, &status)) &&
160 std_listen) break;
161 cif = cif->Next;
162 }
163 LeaveCriticalSection(&server_cs);
164 TRACE("returning %p for %s\n", cif, debugstr_guid(object));
165 return cif;
166 }
167
168 static void RPCRT4_push_packet(RpcPacket* packet)
169 {
170 packet->next = NULL;
171 EnterCriticalSection(&spacket_cs);
172 if (spacket_tail) {
173 spacket_tail->next = packet;
174 spacket_tail = packet;
175 } else {
176 spacket_head = packet;
177 spacket_tail = packet;
178 }
179 LeaveCriticalSection(&spacket_cs);
180 }
181
182 static RpcPacket* RPCRT4_pop_packet(void)
183 {
184 RpcPacket* packet;
185 EnterCriticalSection(&spacket_cs);
186 packet = spacket_head;
187 if (packet) {
188 spacket_head = packet->next;
189 if (!spacket_head) spacket_tail = NULL;
190 }
191 LeaveCriticalSection(&spacket_cs);
192 if (packet) packet->next = NULL;
193 return packet;
194 }
195
196 #ifndef __REACTOS__
197 typedef struct {
198 PRPC_MESSAGE msg;
199 void* buf;
200 } packet_state;
201
202 static WINE_EXCEPTION_FILTER(rpc_filter)
203 {
204 packet_state* state;
205 PRPC_MESSAGE msg;
206 state = TlsGetValue(worker_tls);
207 msg = state->msg;
208 if (msg->Buffer != state->buf) I_RpcFreeBuffer(msg);
209 msg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
210 msg->BufferLength = sizeof(DWORD);
211 I_RpcGetBuffer(msg);
212 *(DWORD*)msg->Buffer = GetExceptionCode();
213 WARN("exception caught with code 0x%08lx = %ld\n", *(DWORD*)msg->Buffer, *(DWORD*)msg->Buffer);
214 TRACE("returning failure packet\n");
215 return EXCEPTION_EXECUTE_HANDLER;
216 }
217 #endif
218
219 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr, RPC_MESSAGE* msg)
220 {
221 RpcServerInterface* sif;
222 RPC_DISPATCH_FUNCTION func;
223 #ifndef __REACTOS__
224 packet_state state;
225 #endif
226 UUID *object_uuid;
227 RpcPktHdr *response;
228 void *buf = msg->Buffer;
229 RPC_STATUS status;
230
231 #ifndef __REACTOS__
232 state.msg = msg;
233 state.buf = buf;
234 TlsSetValue(worker_tls, &state);
235 #endif
236
237 switch (hdr->common.ptype) {
238 case PKT_BIND:
239 TRACE("got bind packet\n");
240
241 /* FIXME: do more checks! */
242 if (hdr->bind.max_tsize < RPC_MIN_PACKET_SIZE ||
243 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
244 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
245 sif = NULL;
246 } else {
247 sif = RPCRT4_find_interface(NULL, &hdr->bind.abstract, FALSE);
248 }
249 if (sif == NULL) {
250 TRACE("rejecting bind request on connection %p\n", conn);
251 /* Report failure to client. */
252 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
253 RPC_VER_MAJOR, RPC_VER_MINOR);
254 } else {
255 TRACE("accepting bind request on connection %p\n", conn);
256
257 /* accept. */
258 response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
259 RPC_MAX_PACKET_SIZE,
260 RPC_MAX_PACKET_SIZE,
261 conn->Endpoint,
262 RESULT_ACCEPT, NO_REASON,
263 &sif->If->TransferSyntax);
264
265 /* save the interface for later use */
266 conn->ActiveInterface = hdr->bind.abstract;
267 conn->MaxTransmissionSize = hdr->bind.max_tsize;
268 }
269
270 if (RPCRT4_Send(conn, response, NULL, 0) != RPC_S_OK)
271 goto fail;
272
273 break;
274
275 case PKT_REQUEST:
276 TRACE("got request packet\n");
277
278 /* fail if the connection isn't bound with an interface */
279 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
280 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
281 status);
282
283 RPCRT4_Send(conn, response, NULL, 0);
284 break;
285 }
286
287 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
288 object_uuid = (UUID*)(&hdr->request + 1);
289 } else {
290 object_uuid = NULL;
291 }
292
293 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, TRUE);
294 msg->RpcInterfaceInformation = sif->If;
295 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
296 msg->ManagerEpv = sif->MgrEpv;
297 if (object_uuid != NULL) {
298 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
299 }
300
301 /* find dispatch function */
302 msg->ProcNum = hdr->request.opnum;
303 if (sif->Flags & RPC_IF_OLE) {
304 /* native ole32 always gives us a dispatch table with a single entry
305 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
306 func = *sif->If->DispatchTable->DispatchTable;
307 } else {
308 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
309 ERR("invalid procnum\n");
310 func = NULL;
311 }
312 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
313 }
314
315 /* put in the drep. FIXME: is this more universally applicable?
316 perhaps we should move this outward... */
317 msg->DataRepresentation =
318 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
319 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
320
321 /* dispatch */
322 #ifndef __REACTOS__
323 __TRY {
324 if (func) func(msg);
325 } __EXCEPT(rpc_filter) {
326 /* failure packet was created in rpc_filter */
327 } __ENDTRY
328 #else
329 if (func) func(msg);
330 #endif
331
332 /* send response packet */
333 I_RpcSend(msg);
334
335 msg->RpcInterfaceInformation = NULL;
336
337 break;
338
339 default:
340 FIXME("unhandled packet type\n");
341 break;
342 }
343
344 fail:
345 /* clean up */
346 if (msg->Buffer == buf) msg->Buffer = NULL;
347 TRACE("freeing Buffer=%p\n", buf);
348 HeapFree(GetProcessHeap(), 0, buf);
349 RPCRT4_DestroyBinding(msg->Handle);
350 msg->Handle = 0;
351 I_RpcFreeBuffer(msg);
352 msg->Buffer = NULL;
353 RPCRT4_FreeHeader(hdr);
354 #ifndef __REACTOS__
355 TlsSetValue(worker_tls, NULL);
356 #endif
357 }
358
359 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
360 {
361 DWORD obj;
362 RpcPacket* pkt;
363
364 for (;;) {
365 /* idle timeout after 5s */
366 obj = WaitForSingleObject(server_sem, 5000);
367 if (obj == WAIT_TIMEOUT) {
368 /* if another idle thread exist, self-destruct */
369 if (worker_free > 1) break;
370 continue;
371 }
372 pkt = RPCRT4_pop_packet();
373 if (!pkt) continue;
374 InterlockedDecrement(&worker_free);
375 for (;;) {
376 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg);
377 HeapFree(GetProcessHeap(), 0, pkt);
378 /* try to grab another packet here without waiting
379 * on the semaphore, in case it hits max */
380 pkt = RPCRT4_pop_packet();
381 if (!pkt) break;
382 /* decrement semaphore */
383 WaitForSingleObject(server_sem, 0);
384 }
385 InterlockedIncrement(&worker_free);
386 }
387 InterlockedDecrement(&worker_free);
388 InterlockedDecrement(&worker_count);
389 return 0;
390 }
391
392 static void RPCRT4_create_worker_if_needed(void)
393 {
394 if (!worker_free && worker_count < MAX_THREADS) {
395 HANDLE thread;
396 InterlockedIncrement(&worker_count);
397 InterlockedIncrement(&worker_free);
398 thread = CreateThread(NULL, 0, RPCRT4_worker_thread, NULL, 0, NULL);
399 if (thread) CloseHandle(thread);
400 else {
401 InterlockedDecrement(&worker_free);
402 InterlockedDecrement(&worker_count);
403 }
404 }
405 }
406
407 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
408 {
409 RpcConnection* conn = (RpcConnection*)the_arg;
410 RpcPktHdr *hdr;
411 RpcBinding *pbind;
412 RPC_MESSAGE *msg;
413 RPC_STATUS status;
414 RpcPacket *packet;
415
416 TRACE("(%p)\n", conn);
417
418 for (;;) {
419 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
420
421 /* create temporary binding for dispatch, it will be freed in
422 * RPCRT4_process_packet */
423 RPCRT4_MakeBinding(&pbind, conn);
424 msg->Handle = (RPC_BINDING_HANDLE)pbind;
425
426 status = RPCRT4_Receive(conn, &hdr, msg);
427 if (status != RPC_S_OK) {
428 WARN("receive failed with error %lx\n", status);
429 break;
430 }
431
432 #if 0
433 RPCRT4_process_packet(conn, hdr, msg);
434 #else
435 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
436 packet->conn = conn;
437 packet->hdr = hdr;
438 packet->msg = msg;
439 RPCRT4_create_worker_if_needed();
440 RPCRT4_push_packet(packet);
441 ReleaseSemaphore(server_sem, 1, NULL);
442 #endif
443 msg = NULL;
444 }
445 HeapFree(GetProcessHeap(), 0, msg);
446 RPCRT4_DestroyConnection(conn);
447 return 0;
448 }
449
450 static void RPCRT4_new_client(RpcConnection* conn)
451 {
452 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
453 if (!thread) {
454 DWORD err = GetLastError();
455 ERR("failed to create thread, error=%08lx\n", err);
456 RPCRT4_DestroyConnection(conn);
457 }
458 /* we could set conn->thread, but then we'd have to make the io_thread wait
459 * for that, otherwise the thread might finish, destroy the connection, and
460 * free the memory we'd write to before we did, causing crashes and stuff -
461 * so let's implement that later, when we really need conn->thread */
462
463 CloseHandle( thread );
464 }
465
466 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
467 {
468 HANDLE m_event = mgr_event, b_handle;
469 HANDLE *objs = NULL;
470 DWORD count, res;
471 RpcServerProtseq* cps;
472 RpcConnection* conn;
473 RpcConnection* cconn;
474 BOOL set_ready_event = FALSE;
475
476 TRACE("(the_arg == ^%p)\n", the_arg);
477
478 for (;;) {
479 EnterCriticalSection(&server_cs);
480 /* open and count connections */
481 count = 1;
482 cps = protseqs;
483 while (cps) {
484 conn = cps->conn;
485 while (conn) {
486 RPCRT4_OpenConnection(conn);
487 if (conn->ovl[0].hEvent) count++;
488 conn = conn->Next;
489 }
490 cps = cps->Next;
491 }
492 /* make array of connections */
493 if (objs)
494 objs = HeapReAlloc(GetProcessHeap(), 0, objs, count*sizeof(HANDLE));
495 else
496 objs = HeapAlloc(GetProcessHeap(), 0, count*sizeof(HANDLE));
497
498 objs[0] = m_event;
499 count = 1;
500 cps = protseqs;
501 while (cps) {
502 conn = cps->conn;
503 while (conn) {
504 if (conn->ovl[0].hEvent) objs[count++] = conn->ovl[0].hEvent;
505 conn = conn->Next;
506 }
507 cps = cps->Next;
508 }
509 LeaveCriticalSection(&server_cs);
510
511 if (set_ready_event)
512 {
513 /* signal to function that changed state that we are now sync'ed */
514 SetEvent(server_ready_event);
515 set_ready_event = FALSE;
516 }
517
518 /* start waiting */
519 res = WaitForMultipleObjects(count, objs, FALSE, INFINITE);
520 if (res == WAIT_OBJECT_0) {
521 if (!std_listen)
522 {
523 SetEvent(server_ready_event);
524 break;
525 }
526 set_ready_event = TRUE;
527 }
528 else if (res == WAIT_FAILED) {
529 ERR("wait failed\n");
530 }
531 else {
532 b_handle = objs[res - WAIT_OBJECT_0];
533 /* find which connection got a RPC */
534 EnterCriticalSection(&server_cs);
535 conn = NULL;
536 cps = protseqs;
537 while (cps) {
538 conn = cps->conn;
539 while (conn) {
540 if (conn->ovl[0].hEvent == b_handle) break;
541 conn = conn->Next;
542 }
543 if (conn) break;
544 cps = cps->Next;
545 }
546 cconn = NULL;
547 if (conn) RPCRT4_SpawnConnection(&cconn, conn);
548 LeaveCriticalSection(&server_cs);
549 if (!conn) {
550 ERR("failed to locate connection for handle %p\n", b_handle);
551 }
552 if (cconn) RPCRT4_new_client(cconn);
553 }
554 }
555 HeapFree(GetProcessHeap(), 0, objs);
556 EnterCriticalSection(&server_cs);
557 /* close connections */
558 cps = protseqs;
559 while (cps) {
560 conn = cps->conn;
561 while (conn) {
562 RPCRT4_CloseConnection(conn);
563 conn = conn->Next;
564 }
565 cps = cps->Next;
566 }
567 LeaveCriticalSection(&server_cs);
568 return 0;
569 }
570
571 /* tells the server thread that the state has changed and waits for it to
572 * make the changes */
573 static void RPCRT4_sync_with_server_thread(void)
574 {
575 /* make sure we are the only thread sync'ing the server state, otherwise
576 * there is a race with the server thread setting an older state and setting
577 * the server_ready_event when the new state hasn't yet been applied */
578 WaitForSingleObject(mgr_mutex, INFINITE);
579
580 SetEvent(mgr_event);
581 /* wait for server thread to make the requested changes before returning */
582 WaitForSingleObject(server_ready_event, INFINITE);
583
584 ReleaseMutex(mgr_mutex);
585 }
586
587 static RPC_STATUS RPCRT4_start_listen(BOOL auto_listen)
588 {
589 RPC_STATUS status = RPC_S_ALREADY_LISTENING;
590
591 TRACE("\n");
592
593 EnterCriticalSection(&listen_cs);
594 if (auto_listen || (manual_listen_count++ == 0))
595 {
596 status = RPC_S_OK;
597 if (++listen_count == 1) {
598 HANDLE server_thread;
599 /* first listener creates server thread */
600 if (!mgr_mutex) mgr_mutex = CreateMutexW(NULL, FALSE, NULL);
601 if (!mgr_event) mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
602 if (!server_ready_event) server_ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
603 if (!server_sem) server_sem = CreateSemaphoreW(NULL, 0, MAX_THREADS, NULL);
604 if (!worker_tls) worker_tls = TlsAlloc();
605 std_listen = TRUE;
606 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, NULL, 0, NULL);
607 CloseHandle(server_thread);
608 }
609 }
610 LeaveCriticalSection(&listen_cs);
611
612 return status;
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( (char*)Protseq ), MaxCalls,
742 debugstr_a( (char*)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((char*)Protseq);
748 ps->Endpoint = RPCRT4_strdupA((char*)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((char*)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 RPCRT4_start_listen(TRUE);
861
862 /* make sure server is actually listening on the interface before
863 * returning */
864 RPCRT4_sync_with_server_thread();
865 }
866
867 return RPC_S_OK;
868 }
869
870 /***********************************************************************
871 * RpcServerUnregisterIf (RPCRT4.@)
872 */
873 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
874 {
875 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, WaitForCallsToComplete == %u): stub\n",
876 IfSpec, debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
877
878 return RPC_S_OK;
879 }
880
881 /***********************************************************************
882 * RpcServerUnregisterIfEx (RPCRT4.@)
883 */
884 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
885 {
886 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
887 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
888
889 return RPC_S_OK;
890 }
891
892 /***********************************************************************
893 * RpcObjectSetType (RPCRT4.@)
894 *
895 * PARAMS
896 * ObjUuid [I] "Object" UUID
897 * TypeUuid [I] "Type" UUID
898 *
899 * RETURNS
900 * RPC_S_OK The call succeeded
901 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
902 * RPC_S_ALREADY_REGISTERED The provided object is already registered
903 *
904 * Maps "Object" UUIDs to "Type" UUID's. Passing the nil UUID as the type
905 * resets the mapping for the specified object UUID to nil (the default).
906 * The nil object is always associated with the nil type and cannot be
907 * reassigned. Servers can support multiple implementations on the same
908 * interface by registering different end-point vectors for the different
909 * types. There's no need to call this if a server only supports the nil
910 * type, as is typical.
911 */
912 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
913 {
914 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
915 RPC_STATUS dummy;
916
917 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
918 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
919 /* nil uuid cannot be remapped */
920 return RPC_S_INVALID_OBJECT;
921 }
922
923 /* find the mapping for this object if there is one ... */
924 while (map) {
925 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
926 prev = map;
927 map = map->next;
928 }
929 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
930 /* ... and drop it from the list */
931 if (map) {
932 if (prev)
933 prev->next = map->next;
934 else
935 RpcObjTypeMaps = map->next;
936 HeapFree(GetProcessHeap(), 0, map);
937 }
938 } else {
939 /* ... , fail if we found it ... */
940 if (map)
941 return RPC_S_ALREADY_REGISTERED;
942 /* ... otherwise create a new one and add it in. */
943 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
944 memcpy(&map->Object, ObjUuid, sizeof(UUID));
945 memcpy(&map->Type, TypeUuid, sizeof(UUID));
946 map->next = NULL;
947 if (prev)
948 prev->next = map; /* prev is the last map in the linklist */
949 else
950 RpcObjTypeMaps = map;
951 }
952
953 return RPC_S_OK;
954 }
955
956 /***********************************************************************
957 * RpcServerRegisterAuthInfoA (RPCRT4.@)
958 */
959 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( unsigned char *ServerPrincName, unsigned long AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
960 LPVOID Arg )
961 {
962 FIXME( "(%s,%lu,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
963
964 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
965 }
966
967 /***********************************************************************
968 * RpcServerRegisterAuthInfoW (RPCRT4.@)
969 */
970 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( LPWSTR ServerPrincName, unsigned long AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
971 LPVOID Arg )
972 {
973 FIXME( "(%s,%lu,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
974
975 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
976 }
977
978 /***********************************************************************
979 * RpcServerListen (RPCRT4.@)
980 */
981 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
982 {
983 RPC_STATUS status;
984
985 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
986
987 if (!protseqs)
988 return RPC_S_NO_PROTSEQS_REGISTERED;
989
990 status = RPCRT4_start_listen(FALSE);
991
992 if (status == RPC_S_OK)
993 RPCRT4_sync_with_server_thread();
994
995 if (DontWait || (status != RPC_S_OK)) return status;
996
997 return RpcMgmtWaitServerListen();
998 }
999
1000 /***********************************************************************
1001 * RpcMgmtServerWaitListen (RPCRT4.@)
1002 */
1003 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
1004 {
1005 TRACE("()\n");
1006
1007 EnterCriticalSection(&listen_cs);
1008
1009 if (!std_listen) {
1010 LeaveCriticalSection(&listen_cs);
1011 return RPC_S_NOT_LISTENING;
1012 }
1013
1014 LeaveCriticalSection(&listen_cs);
1015
1016 return RPC_S_OK;
1017 }
1018
1019 /***********************************************************************
1020 * RpcMgmtStopServerListening (RPCRT4.@)
1021 */
1022 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1023 {
1024 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1025
1026 if (Binding) {
1027 FIXME("client-side invocation not implemented.\n");
1028 return RPC_S_WRONG_KIND_OF_BINDING;
1029 }
1030
1031 RPCRT4_stop_listen(FALSE);
1032
1033 return RPC_S_OK;
1034 }
1035
1036 /***********************************************************************
1037 * I_RpcServerStartListening (RPCRT4.@)
1038 */
1039 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1040 {
1041 FIXME( "(%p): stub\n", hWnd );
1042
1043 return RPC_S_OK;
1044 }
1045
1046 /***********************************************************************
1047 * I_RpcServerStopListening (RPCRT4.@)
1048 */
1049 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1050 {
1051 FIXME( "(): stub\n" );
1052
1053 return RPC_S_OK;
1054 }
1055
1056 /***********************************************************************
1057 * I_RpcWindowProc (RPCRT4.@)
1058 */
1059 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1060 {
1061 FIXME( "(%p,%08x,%08x,%08lx): stub\n", hWnd, Message, wParam, lParam );
1062
1063 return 0;
1064 }