Only stop writing a volume label if we found a non writable fat
[wine.git] / dlls / rpcrt4 / rpc_server.c
blob7bbdd8258fc1e17751f968b75f27ce2d31585e38
1 /*
2 * RPC server API
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * TODO:
22 * - a whole lot
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "winreg.h"
37 #include "ntstatus.h"
39 #include "rpc.h"
40 #include "rpcndr.h"
41 #include "excpt.h"
43 #include "wine/debug.h"
44 #include "wine/exception.h"
46 #include "rpc_server.h"
47 #include "rpc_misc.h"
48 #include "rpc_message.h"
49 #include "rpc_defs.h"
51 #define MAX_THREADS 128
53 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 typedef struct _RpcPacket
57 struct _RpcPacket* next;
58 struct _RpcConnection* conn;
59 RpcPktHdr* hdr;
60 RPC_MESSAGE* msg;
61 } RpcPacket;
63 typedef struct _RpcObjTypeMap
65 /* FIXME: a hash table would be better. */
66 struct _RpcObjTypeMap *next;
67 UUID Object;
68 UUID Type;
69 } RpcObjTypeMap;
71 static RpcObjTypeMap *RpcObjTypeMaps;
73 static RpcServerProtseq* protseqs;
74 static RpcServerInterface* ifs;
76 static CRITICAL_SECTION server_cs;
77 static CRITICAL_SECTION_DEBUG server_cs_debug =
79 0, 0, &server_cs,
80 { &server_cs_debug.ProcessLocksList, &server_cs_debug.ProcessLocksList },
81 0, 0, { 0, (DWORD)(__FILE__ ": server_cs") }
83 static CRITICAL_SECTION server_cs = { &server_cs_debug, -1, 0, 0, 0, 0 };
85 static CRITICAL_SECTION listen_cs;
86 static CRITICAL_SECTION_DEBUG listen_cs_debug =
88 0, 0, &listen_cs,
89 { &listen_cs_debug.ProcessLocksList, &listen_cs_debug.ProcessLocksList },
90 0, 0, { 0, (DWORD)(__FILE__ ": listen_cs") }
92 static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };
94 static BOOL std_listen;
95 static LONG listen_count = -1;
96 /* set on change of configuration (e.g. listening on new protseq) */
97 static HANDLE mgr_event;
98 /* mutex for ensuring only one thread can change state at a time */
99 static HANDLE mgr_mutex;
100 /* set when server thread has finished opening connections */
101 static HANDLE server_ready_event;
102 /* thread that waits for connections */
103 static HANDLE server_thread;
105 static CRITICAL_SECTION spacket_cs;
106 static CRITICAL_SECTION_DEBUG spacket_cs_debug =
108 0, 0, &spacket_cs,
109 { &spacket_cs_debug.ProcessLocksList, &spacket_cs_debug.ProcessLocksList },
110 0, 0, { 0, (DWORD)(__FILE__ ": spacket_cs") }
112 static CRITICAL_SECTION spacket_cs = { &spacket_cs_debug, -1, 0, 0, 0, 0 };
114 static RpcPacket* spacket_head;
115 static RpcPacket* spacket_tail;
116 static HANDLE server_sem;
118 static DWORD worker_count, worker_free, worker_tls;
120 static UUID uuid_nil;
122 inline static RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
124 RpcObjTypeMap *rslt = RpcObjTypeMaps;
125 RPC_STATUS dummy;
127 while (rslt) {
128 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
129 rslt = rslt->next;
132 return rslt;
135 inline static UUID *LookupObjType(UUID *ObjUuid)
137 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
138 if (map)
139 return &map->Type;
140 else
141 return &uuid_nil;
144 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
145 RPC_SYNTAX_IDENTIFIER* if_id,
146 BOOL check_object)
148 UUID* MgrType = NULL;
149 RpcServerInterface* cif = NULL;
150 RPC_STATUS status;
152 if (check_object)
153 MgrType = LookupObjType(object);
154 EnterCriticalSection(&server_cs);
155 cif = ifs;
156 while (cif) {
157 if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
158 (check_object == FALSE || UuidEqual(MgrType, &cif->MgrTypeUuid, &status)) &&
159 (std_listen || (cif->Flags & RPC_IF_AUTOLISTEN))) break;
160 cif = cif->Next;
162 LeaveCriticalSection(&server_cs);
163 TRACE("returning %p for %s\n", cif, debugstr_guid(object));
164 return cif;
167 static void RPCRT4_push_packet(RpcPacket* packet)
169 packet->next = NULL;
170 EnterCriticalSection(&spacket_cs);
171 if (spacket_tail) {
172 spacket_tail->next = packet;
173 spacket_tail = packet;
174 } else {
175 spacket_head = packet;
176 spacket_tail = packet;
178 LeaveCriticalSection(&spacket_cs);
181 static RpcPacket* RPCRT4_pop_packet(void)
183 RpcPacket* packet;
184 EnterCriticalSection(&spacket_cs);
185 packet = spacket_head;
186 if (packet) {
187 spacket_head = packet->next;
188 if (!spacket_head) spacket_tail = NULL;
190 LeaveCriticalSection(&spacket_cs);
191 if (packet) packet->next = NULL;
192 return packet;
195 typedef struct {
196 PRPC_MESSAGE msg;
197 void* buf;
198 } packet_state;
200 static WINE_EXCEPTION_FILTER(rpc_filter)
202 packet_state* state;
203 PRPC_MESSAGE msg;
204 state = TlsGetValue(worker_tls);
205 msg = state->msg;
206 if (msg->Buffer != state->buf) I_RpcFreeBuffer(msg);
207 msg->RpcFlags |= WINE_RPCFLAG_EXCEPTION;
208 msg->BufferLength = sizeof(DWORD);
209 I_RpcGetBuffer(msg);
210 *(DWORD*)msg->Buffer = GetExceptionCode();
211 WARN("exception caught with code 0x%08lx = %ld\n", *(DWORD*)msg->Buffer, *(DWORD*)msg->Buffer);
212 TRACE("returning failure packet\n");
213 return EXCEPTION_EXECUTE_HANDLER;
216 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr, RPC_MESSAGE* msg)
218 RpcServerInterface* sif;
219 RPC_DISPATCH_FUNCTION func;
220 packet_state state;
221 UUID *object_uuid;
222 RpcPktHdr *response;
223 void *buf = msg->Buffer;
224 RPC_STATUS status;
226 state.msg = msg;
227 state.buf = buf;
228 TlsSetValue(worker_tls, &state);
230 switch (hdr->common.ptype) {
231 case PKT_BIND:
232 TRACE("got bind packet\n");
234 /* FIXME: do more checks! */
235 if (hdr->bind.max_tsize < RPC_MIN_PACKET_SIZE ||
236 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
237 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
238 sif = NULL;
239 } else {
240 sif = RPCRT4_find_interface(NULL, &hdr->bind.abstract, FALSE);
242 if (sif == NULL) {
243 TRACE("rejecting bind request on connection %p\n", conn);
244 /* Report failure to client. */
245 response = RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
246 RPC_VER_MAJOR, RPC_VER_MINOR);
247 } else {
248 TRACE("accepting bind request on connection %p\n", conn);
250 /* accept. */
251 response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
252 RPC_MAX_PACKET_SIZE,
253 RPC_MAX_PACKET_SIZE,
254 conn->Endpoint,
255 RESULT_ACCEPT, NO_REASON,
256 &sif->If->TransferSyntax);
258 /* save the interface for later use */
259 conn->ActiveInterface = hdr->bind.abstract;
260 conn->MaxTransmissionSize = hdr->bind.max_tsize;
263 if (RPCRT4_Send(conn, response, NULL, 0) != RPC_S_OK)
264 goto fail;
266 break;
268 case PKT_REQUEST:
269 TRACE("got request packet\n");
271 /* fail if the connection isn't bound with an interface */
272 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
273 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
274 status);
276 RPCRT4_Send(conn, response, NULL, 0);
277 break;
280 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
281 object_uuid = (UUID*)(&hdr->request + 1);
282 } else {
283 object_uuid = NULL;
286 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, TRUE);
287 msg->RpcInterfaceInformation = sif->If;
288 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
289 msg->ManagerEpv = sif->MgrEpv;
290 if (object_uuid != NULL) {
291 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
294 /* find dispatch function */
295 msg->ProcNum = hdr->request.opnum;
296 if (sif->Flags & RPC_IF_OLE) {
297 /* native ole32 always gives us a dispatch table with a single entry
298 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
299 func = *sif->If->DispatchTable->DispatchTable;
300 } else {
301 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
302 ERR("invalid procnum\n");
303 func = NULL;
305 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
308 /* put in the drep. FIXME: is this more universally applicable?
309 perhaps we should move this outward... */
310 msg->DataRepresentation =
311 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
312 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
314 /* dispatch */
315 __TRY {
316 if (func) func(msg);
317 } __EXCEPT(rpc_filter) {
318 /* failure packet was created in rpc_filter */
319 } __ENDTRY
321 /* send response packet */
322 I_RpcSend(msg);
324 msg->RpcInterfaceInformation = NULL;
326 break;
328 default:
329 FIXME("unhandled packet type\n");
330 break;
333 fail:
334 /* clean up */
335 if (msg->Buffer == buf) msg->Buffer = NULL;
336 TRACE("freeing Buffer=%p\n", buf);
337 HeapFree(GetProcessHeap(), 0, buf);
338 RPCRT4_DestroyBinding(msg->Handle);
339 msg->Handle = 0;
340 I_RpcFreeBuffer(msg);
341 msg->Buffer = NULL;
342 RPCRT4_FreeHeader(hdr);
343 TlsSetValue(worker_tls, NULL);
346 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
348 DWORD obj;
349 RpcPacket* pkt;
351 for (;;) {
352 /* idle timeout after 5s */
353 obj = WaitForSingleObject(server_sem, 5000);
354 if (obj == WAIT_TIMEOUT) {
355 /* if another idle thread exist, self-destruct */
356 if (worker_free > 1) break;
357 continue;
359 pkt = RPCRT4_pop_packet();
360 if (!pkt) continue;
361 InterlockedDecrement(&worker_free);
362 for (;;) {
363 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg);
364 HeapFree(GetProcessHeap(), 0, pkt);
365 /* try to grab another packet here without waiting
366 * on the semaphore, in case it hits max */
367 pkt = RPCRT4_pop_packet();
368 if (!pkt) break;
369 /* decrement semaphore */
370 WaitForSingleObject(server_sem, 0);
372 InterlockedIncrement(&worker_free);
374 InterlockedDecrement(&worker_free);
375 InterlockedDecrement(&worker_count);
376 return 0;
379 static void RPCRT4_create_worker_if_needed(void)
381 if (!worker_free && worker_count < MAX_THREADS) {
382 HANDLE thread;
383 InterlockedIncrement(&worker_count);
384 InterlockedIncrement(&worker_free);
385 thread = CreateThread(NULL, 0, RPCRT4_worker_thread, NULL, 0, NULL);
386 if (thread) CloseHandle(thread);
387 else {
388 InterlockedDecrement(&worker_free);
389 InterlockedDecrement(&worker_count);
394 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
396 RpcConnection* conn = (RpcConnection*)the_arg;
397 RpcPktHdr *hdr;
398 RpcBinding *pbind;
399 RPC_MESSAGE *msg;
400 RPC_STATUS status;
401 RpcPacket *packet;
403 TRACE("(%p)\n", conn);
405 for (;;) {
406 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
408 /* create temporary binding for dispatch, it will be freed in
409 * RPCRT4_process_packet */
410 RPCRT4_MakeBinding(&pbind, conn);
411 msg->Handle = (RPC_BINDING_HANDLE)pbind;
413 status = RPCRT4_Receive(conn, &hdr, msg);
414 if (status != RPC_S_OK) {
415 WARN("receive failed with error %lx\n", status);
416 break;
419 #if 0
420 RPCRT4_process_packet(conn, hdr, msg);
421 #else
422 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
423 packet->conn = conn;
424 packet->hdr = hdr;
425 packet->msg = msg;
426 RPCRT4_create_worker_if_needed();
427 RPCRT4_push_packet(packet);
428 ReleaseSemaphore(server_sem, 1, NULL);
429 #endif
430 msg = NULL;
432 HeapFree(GetProcessHeap(), 0, msg);
433 RPCRT4_DestroyConnection(conn);
434 return 0;
437 static void RPCRT4_new_client(RpcConnection* conn)
439 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
440 if (!thread) {
441 DWORD err = GetLastError();
442 ERR("failed to create thread, error=%08lx\n", err);
443 RPCRT4_DestroyConnection(conn);
445 /* we could set conn->thread, but then we'd have to make the io_thread wait
446 * for that, otherwise the thread might finish, destroy the connection, and
447 * free the memory we'd write to before we did, causing crashes and stuff -
448 * so let's implement that later, when we really need conn->thread */
450 CloseHandle( thread );
453 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
455 HANDLE m_event = mgr_event, b_handle;
456 HANDLE *objs = NULL;
457 DWORD count, res;
458 RpcServerProtseq* cps;
459 RpcConnection* conn;
460 RpcConnection* cconn;
461 BOOL set_ready_event = FALSE;
463 TRACE("(the_arg == ^%p)\n", the_arg);
465 for (;;) {
466 EnterCriticalSection(&server_cs);
467 /* open and count connections */
468 count = 1;
469 cps = protseqs;
470 while (cps) {
471 conn = cps->conn;
472 while (conn) {
473 RPCRT4_OpenConnection(conn);
474 if (conn->ovl.hEvent) count++;
475 conn = conn->Next;
477 cps = cps->Next;
479 /* make array of connections */
480 if (objs)
481 objs = HeapReAlloc(GetProcessHeap(), 0, objs, count*sizeof(HANDLE));
482 else
483 objs = HeapAlloc(GetProcessHeap(), 0, count*sizeof(HANDLE));
485 objs[0] = m_event;
486 count = 1;
487 cps = protseqs;
488 while (cps) {
489 conn = cps->conn;
490 while (conn) {
491 if (conn->ovl.hEvent) objs[count++] = conn->ovl.hEvent;
492 conn = conn->Next;
494 cps = cps->Next;
496 LeaveCriticalSection(&server_cs);
498 if (set_ready_event)
500 /* signal to function that changed state that we are now sync'ed */
501 SetEvent(server_ready_event);
502 set_ready_event = FALSE;
505 /* start waiting */
506 res = WaitForMultipleObjects(count, objs, FALSE, INFINITE);
507 if (res == WAIT_OBJECT_0) {
508 if (!std_listen)
510 SetEvent(server_ready_event);
511 break;
513 set_ready_event = TRUE;
515 else if (res == WAIT_FAILED) {
516 ERR("wait failed\n");
518 else {
519 b_handle = objs[res - WAIT_OBJECT_0];
520 /* find which connection got a RPC */
521 EnterCriticalSection(&server_cs);
522 conn = NULL;
523 cps = protseqs;
524 while (cps) {
525 conn = cps->conn;
526 while (conn) {
527 if (conn->ovl.hEvent == b_handle) break;
528 conn = conn->Next;
530 if (conn) break;
531 cps = cps->Next;
533 cconn = NULL;
534 if (conn) RPCRT4_SpawnConnection(&cconn, conn);
535 LeaveCriticalSection(&server_cs);
536 if (!conn) {
537 ERR("failed to locate connection for handle %p\n", b_handle);
539 if (cconn) RPCRT4_new_client(cconn);
542 HeapFree(GetProcessHeap(), 0, objs);
543 EnterCriticalSection(&server_cs);
544 /* close connections */
545 cps = protseqs;
546 while (cps) {
547 conn = cps->conn;
548 while (conn) {
549 RPCRT4_CloseConnection(conn);
550 conn = conn->Next;
552 cps = cps->Next;
554 LeaveCriticalSection(&server_cs);
555 return 0;
558 /* tells the server thread that the state has changed and waits for it to
559 * make the changes */
560 static void RPCRT4_sync_with_server_thread(void)
562 /* make sure we are the only thread sync'ing the server state, otherwise
563 * there is a race with the server thread setting an older state and setting
564 * the server_ready_event when the new state hasn't yet been applied */
565 WaitForSingleObject(mgr_mutex, INFINITE);
567 SetEvent(mgr_event);
568 /* wait for server thread to make the requested changes before returning */
569 WaitForSingleObject(server_ready_event, INFINITE);
571 ReleaseMutex(mgr_mutex);
574 static void RPCRT4_start_listen(void)
576 TRACE("\n");
578 EnterCriticalSection(&listen_cs);
579 if (! ++listen_count) {
580 if (!mgr_mutex) mgr_mutex = CreateMutexW(NULL, FALSE, NULL);
581 if (!mgr_event) mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
582 if (!server_ready_event) server_ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
583 if (!server_sem) server_sem = CreateSemaphoreW(NULL, 0, MAX_THREADS, NULL);
584 if (!worker_tls) worker_tls = TlsAlloc();
585 std_listen = TRUE;
586 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, NULL, 0, NULL);
587 LeaveCriticalSection(&listen_cs);
588 } else {
589 LeaveCriticalSection(&listen_cs);
590 RPCRT4_sync_with_server_thread();
594 static void RPCRT4_stop_listen(void)
596 EnterCriticalSection(&listen_cs);
597 if (listen_count == -1)
598 LeaveCriticalSection(&listen_cs);
599 else if (--listen_count == -1) {
600 std_listen = FALSE;
601 LeaveCriticalSection(&listen_cs);
602 RPCRT4_sync_with_server_thread();
603 } else
604 LeaveCriticalSection(&listen_cs);
605 assert(listen_count > -2);
608 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps)
610 RPCRT4_CreateConnection(&ps->conn, TRUE, ps->Protseq, NULL, ps->Endpoint, NULL, NULL);
612 EnterCriticalSection(&server_cs);
613 ps->Next = protseqs;
614 protseqs = ps;
615 LeaveCriticalSection(&server_cs);
617 if (std_listen) RPCRT4_sync_with_server_thread();
619 return RPC_S_OK;
622 /***********************************************************************
623 * RpcServerInqBindings (RPCRT4.@)
625 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
627 RPC_STATUS status;
628 DWORD count;
629 RpcServerProtseq* ps;
630 RpcConnection* conn;
632 if (BindingVector)
633 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
634 else
635 ERR("(BindingVector == NULL!!?)\n");
637 EnterCriticalSection(&server_cs);
638 /* count connections */
639 count = 0;
640 ps = protseqs;
641 while (ps) {
642 conn = ps->conn;
643 while (conn) {
644 count++;
645 conn = conn->Next;
647 ps = ps->Next;
649 if (count) {
650 /* export bindings */
651 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
652 sizeof(RPC_BINDING_VECTOR) +
653 sizeof(RPC_BINDING_HANDLE)*(count-1));
654 (*BindingVector)->Count = count;
655 count = 0;
656 ps = protseqs;
657 while (ps) {
658 conn = ps->conn;
659 while (conn) {
660 RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
661 conn);
662 count++;
663 conn = conn->Next;
665 ps = ps->Next;
667 status = RPC_S_OK;
668 } else {
669 *BindingVector = NULL;
670 status = RPC_S_NO_BINDINGS;
672 LeaveCriticalSection(&server_cs);
673 return status;
676 /***********************************************************************
677 * RpcServerUseProtseqEpA (RPCRT4.@)
679 RPC_STATUS WINAPI RpcServerUseProtseqEpA( unsigned char *Protseq, UINT MaxCalls, unsigned char *Endpoint, LPVOID SecurityDescriptor )
681 RPC_POLICY policy;
683 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
685 /* This should provide the default behaviour */
686 policy.Length = sizeof( policy );
687 policy.EndpointFlags = 0;
688 policy.NICFlags = 0;
690 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
693 /***********************************************************************
694 * RpcServerUseProtseqEpW (RPCRT4.@)
696 RPC_STATUS WINAPI RpcServerUseProtseqEpW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor )
698 RPC_POLICY policy;
700 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
702 /* This should provide the default behaviour */
703 policy.Length = sizeof( policy );
704 policy.EndpointFlags = 0;
705 policy.NICFlags = 0;
707 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
710 /***********************************************************************
711 * RpcServerUseProtseqEpExA (RPCRT4.@)
713 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( unsigned char *Protseq, UINT MaxCalls, unsigned char *Endpoint, LPVOID SecurityDescriptor,
714 PRPC_POLICY lpPolicy )
716 RpcServerProtseq* ps;
718 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_a( Protseq ), MaxCalls,
719 debugstr_a( Endpoint ), SecurityDescriptor,
720 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
722 ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerProtseq));
723 ps->MaxCalls = MaxCalls;
724 ps->Protseq = RPCRT4_strdupA(Protseq);
725 ps->Endpoint = RPCRT4_strdupA(Endpoint);
727 return RPCRT4_use_protseq(ps);
730 /***********************************************************************
731 * RpcServerUseProtseqEpExW (RPCRT4.@)
733 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( LPWSTR Protseq, UINT MaxCalls, LPWSTR Endpoint, LPVOID SecurityDescriptor,
734 PRPC_POLICY lpPolicy )
736 RpcServerProtseq* ps;
738 TRACE("(%s,%u,%s,%p,{%u,%lu,%lu})\n", debugstr_w( Protseq ), MaxCalls,
739 debugstr_w( Endpoint ), SecurityDescriptor,
740 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
742 ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerProtseq));
743 ps->MaxCalls = MaxCalls;
744 ps->Protseq = RPCRT4_strdupWtoA(Protseq);
745 ps->Endpoint = RPCRT4_strdupWtoA(Endpoint);
747 return RPCRT4_use_protseq(ps);
750 /***********************************************************************
751 * RpcServerUseProtseqA (RPCRT4.@)
753 RPC_STATUS WINAPI RpcServerUseProtseqA(unsigned char *Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
755 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a(Protseq), MaxCalls, SecurityDescriptor);
756 return RpcServerUseProtseqEpA(Protseq, MaxCalls, NULL, SecurityDescriptor);
759 /***********************************************************************
760 * RpcServerUseProtseqW (RPCRT4.@)
762 RPC_STATUS WINAPI RpcServerUseProtseqW(LPWSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
764 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
765 return RpcServerUseProtseqEpW(Protseq, MaxCalls, NULL, SecurityDescriptor);
768 /***********************************************************************
769 * RpcServerRegisterIf (RPCRT4.@)
771 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
773 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
774 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
777 /***********************************************************************
778 * RpcServerRegisterIfEx (RPCRT4.@)
780 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
781 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
783 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
784 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
787 /***********************************************************************
788 * RpcServerRegisterIf2 (RPCRT4.@)
790 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
791 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
793 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
794 RpcServerInterface* sif;
795 unsigned int i;
797 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
798 MaxRpcSize, IfCallbackFn);
799 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
800 If->InterfaceId.SyntaxVersion.MajorVersion,
801 If->InterfaceId.SyntaxVersion.MinorVersion);
802 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
803 If->TransferSyntax.SyntaxVersion.MajorVersion,
804 If->TransferSyntax.SyntaxVersion.MinorVersion);
805 TRACE(" dispatch table: %p\n", If->DispatchTable);
806 if (If->DispatchTable) {
807 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
808 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
809 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
811 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
813 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
814 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
815 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
817 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
818 sif->If = If;
819 if (MgrTypeUuid) {
820 memcpy(&sif->MgrTypeUuid, MgrTypeUuid, sizeof(UUID));
821 sif->MgrEpv = MgrEpv;
822 } else {
823 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
824 sif->MgrEpv = If->DefaultManagerEpv;
826 sif->Flags = Flags;
827 sif->MaxCalls = MaxCalls;
828 sif->MaxRpcSize = MaxRpcSize;
829 sif->IfCallbackFn = IfCallbackFn;
831 EnterCriticalSection(&server_cs);
832 sif->Next = ifs;
833 ifs = sif;
834 LeaveCriticalSection(&server_cs);
836 if (sif->Flags & RPC_IF_AUTOLISTEN) {
837 /* well, start listening, I think... */
838 RPCRT4_start_listen();
841 return RPC_S_OK;
844 /***********************************************************************
845 * RpcServerUnregisterIf (RPCRT4.@)
847 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
849 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, WaitForCallsToComplete == %u): stub\n",
850 IfSpec, debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
852 return RPC_S_OK;
855 /***********************************************************************
856 * RpcServerUnregisterIfEx (RPCRT4.@)
858 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
860 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
861 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
863 return RPC_S_OK;
866 /***********************************************************************
867 * RpcObjectSetType (RPCRT4.@)
869 * PARAMS
870 * ObjUuid [I] "Object" UUID
871 * TypeUuid [I] "Type" UUID
873 * RETURNS
874 * RPC_S_OK The call succeeded
875 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
876 * RPC_S_ALREADY_REGISTERED The provided object is already registered
878 * Maps "Object" UUIDs to "Type" UUID's. Passing the nil UUID as the type
879 * resets the mapping for the specified object UUID to nil (the default).
880 * The nil object is always associated with the nil type and cannot be
881 * reassigned. Servers can support multiple implementations on the same
882 * interface by registering different end-point vectors for the different
883 * types. There's no need to call this if a server only supports the nil
884 * type, as is typical.
886 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
888 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
889 RPC_STATUS dummy;
891 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
892 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
893 /* nil uuid cannot be remapped */
894 return RPC_S_INVALID_OBJECT;
897 /* find the mapping for this object if there is one ... */
898 while (map) {
899 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
900 prev = map;
901 map = map->next;
903 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
904 /* ... and drop it from the list */
905 if (map) {
906 if (prev)
907 prev->next = map->next;
908 else
909 RpcObjTypeMaps = map->next;
910 HeapFree(GetProcessHeap(), 0, map);
912 } else {
913 /* ... , fail if we found it ... */
914 if (map)
915 return RPC_S_ALREADY_REGISTERED;
916 /* ... otherwise create a new one and add it in. */
917 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
918 memcpy(&map->Object, ObjUuid, sizeof(UUID));
919 memcpy(&map->Type, TypeUuid, sizeof(UUID));
920 map->next = NULL;
921 if (prev)
922 prev->next = map; /* prev is the last map in the linklist */
923 else
924 RpcObjTypeMaps = map;
927 return RPC_S_OK;
930 /***********************************************************************
931 * RpcServerRegisterAuthInfoA (RPCRT4.@)
933 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( unsigned char *ServerPrincName, unsigned long AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
934 LPVOID Arg )
936 FIXME( "(%s,%lu,%p,%p): stub\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg );
938 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
941 /***********************************************************************
942 * RpcServerRegisterAuthInfoW (RPCRT4.@)
944 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( LPWSTR ServerPrincName, unsigned long AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
945 LPVOID Arg )
947 FIXME( "(%s,%lu,%p,%p): stub\n", debugstr_w( ServerPrincName ), AuthnSvc, GetKeyFn, Arg );
949 return RPC_S_UNKNOWN_AUTHN_SERVICE; /* We don't know any authentication services */
952 /***********************************************************************
953 * RpcServerListen (RPCRT4.@)
955 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
957 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
959 if (!protseqs)
960 return RPC_S_NO_PROTSEQS_REGISTERED;
962 EnterCriticalSection(&listen_cs);
964 if (std_listen) {
965 LeaveCriticalSection(&listen_cs);
966 return RPC_S_ALREADY_LISTENING;
969 RPCRT4_start_listen();
971 LeaveCriticalSection(&listen_cs);
973 if (DontWait) return RPC_S_OK;
975 return RpcMgmtWaitServerListen();
978 /***********************************************************************
979 * RpcMgmtServerWaitListen (RPCRT4.@)
981 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
983 RPC_STATUS rslt = RPC_S_OK;
985 TRACE("\n");
987 EnterCriticalSection(&listen_cs);
989 if (!std_listen)
990 if ( (rslt = RpcServerListen(1, 0, TRUE)) != RPC_S_OK ) {
991 LeaveCriticalSection(&listen_cs);
992 return rslt;
995 LeaveCriticalSection(&listen_cs);
997 while (std_listen) {
998 WaitForSingleObject(mgr_event, INFINITE);
999 if (!std_listen) {
1000 Sleep(100); /* don't spin violently */
1001 TRACE("spinning.\n");
1005 return rslt;
1008 /***********************************************************************
1009 * RpcMgmtStopServerListening (RPCRT4.@)
1011 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1013 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1015 if (Binding) {
1016 FIXME("client-side invocation not implemented.\n");
1017 return RPC_S_WRONG_KIND_OF_BINDING;
1020 /* hmm... */
1021 EnterCriticalSection(&listen_cs);
1022 while (std_listen)
1023 RPCRT4_stop_listen();
1024 LeaveCriticalSection(&listen_cs);
1026 return RPC_S_OK;
1029 /***********************************************************************
1030 * I_RpcServerStartListening (RPCRT4.@)
1032 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1034 FIXME( "(%p): stub\n", hWnd );
1036 return RPC_S_OK;
1039 /***********************************************************************
1040 * I_RpcServerStopListening (RPCRT4.@)
1042 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1044 FIXME( "(): stub\n" );
1046 return RPC_S_OK;
1049 /***********************************************************************
1050 * I_RpcWindowProc (RPCRT4.@)
1052 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1054 FIXME( "(%p,%08x,%08x,%08lx): stub\n", hWnd, Message, wParam, lParam );
1056 return 0;