winex11: Remove unnecessary #include.
[wine.git] / dlls / rpcrt4 / rpcrt4_main.c
blob8e565afdfc4251fe9a227a7196f976b325fa59df
1 /*
2 * RPCRT4
4 * Copyright 2000 Huw D M Davies for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * WINE RPC TODO's (and a few TODONT's)
22 * - Statistics: we are supposed to be keeping various counters. we aren't.
24 * - Async RPC: Unimplemented.
26 * - The NT "ports" API, aka LPC. Greg claims this is on his radar. Might (or
27 * might not) enable users to get some kind of meaningful result out of
28 * NT-based native rpcrt4's. Commonly-used transport for self-to-self RPC's.
31 #include "config.h"
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "ntstatus.h"
39 #define WIN32_NO_STATUS
40 #include "windef.h"
41 #include "winerror.h"
42 #include "winbase.h"
43 #include "winuser.h"
44 #include "winnt.h"
45 #include "winternl.h"
46 #include "iptypes.h"
47 #include "iphlpapi.h"
48 #include "wine/unicode.h"
49 #include "rpc.h"
51 #include "ole2.h"
52 #include "rpcndr.h"
53 #include "rpcproxy.h"
55 #include "rpc_binding.h"
56 #include "rpcss_np_client.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
62 static UUID uuid_nil;
63 static HANDLE master_mutex;
65 HANDLE RPCRT4_GetMasterMutex(void)
67 return master_mutex;
70 static CRITICAL_SECTION uuid_cs;
71 static CRITICAL_SECTION_DEBUG critsect_debug =
73 0, 0, &uuid_cs,
74 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
75 0, 0, { (DWORD_PTR)(__FILE__ ": uuid_cs") }
77 static CRITICAL_SECTION uuid_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
79 static CRITICAL_SECTION threaddata_cs;
80 static CRITICAL_SECTION_DEBUG threaddata_cs_debug =
82 0, 0, &threaddata_cs,
83 { &threaddata_cs_debug.ProcessLocksList, &threaddata_cs_debug.ProcessLocksList },
84 0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
86 static CRITICAL_SECTION threaddata_cs = { &threaddata_cs_debug, -1, 0, 0, 0, 0 };
88 struct list threaddata_list = LIST_INIT(threaddata_list);
90 struct context_handle_list
92 struct context_handle_list *next;
93 NDR_SCONTEXT context_handle;
96 struct threaddata
98 struct list entry;
99 CRITICAL_SECTION cs;
100 DWORD thread_id;
101 RpcConnection *connection;
102 RpcBinding *server_binding;
103 struct context_handle_list *context_handle_list;
106 /***********************************************************************
107 * DllMain
109 * PARAMS
110 * hinstDLL [I] handle to the DLL's instance
111 * fdwReason [I]
112 * lpvReserved [I] reserved, must be NULL
114 * RETURNS
115 * Success: TRUE
116 * Failure: FALSE
119 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
121 struct threaddata *tdata;
123 switch (fdwReason) {
124 case DLL_PROCESS_ATTACH:
125 master_mutex = CreateMutexA( NULL, FALSE, RPCSS_MASTER_MUTEX_NAME);
126 if (!master_mutex)
127 ERR("Failed to create master mutex\n");
128 break;
130 case DLL_THREAD_DETACH:
131 tdata = NtCurrentTeb()->ReservedForNtRpc;
132 if (tdata)
134 EnterCriticalSection(&threaddata_cs);
135 list_remove(&tdata->entry);
136 LeaveCriticalSection(&threaddata_cs);
138 DeleteCriticalSection(&tdata->cs);
139 if (tdata->connection)
140 ERR("tdata->connection should be NULL but is still set to %p\n", tdata->connection);
141 if (tdata->server_binding)
142 ERR("tdata->server_binding should be NULL but is still set to %p\n", tdata->server_binding);
143 HeapFree(GetProcessHeap(), 0, tdata);
145 break;
147 case DLL_PROCESS_DETACH:
148 CloseHandle(master_mutex);
149 master_mutex = NULL;
150 break;
153 return TRUE;
156 /*************************************************************************
157 * RpcStringFreeA [RPCRT4.@]
159 * Frees a character string allocated by the RPC run-time library.
161 * RETURNS
163 * S_OK if successful.
165 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
167 HeapFree( GetProcessHeap(), 0, *String);
169 return RPC_S_OK;
172 /*************************************************************************
173 * RpcStringFreeW [RPCRT4.@]
175 * Frees a character string allocated by the RPC run-time library.
177 * RETURNS
179 * S_OK if successful.
181 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
183 HeapFree( GetProcessHeap(), 0, *String);
185 return RPC_S_OK;
188 /*************************************************************************
189 * RpcRaiseException [RPCRT4.@]
191 * Raises an exception.
193 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
195 /* shouldn't return */
196 RaiseException(exception, 0, 0, NULL);
197 ERR("handler continued execution\n");
198 ExitProcess(1);
201 /*************************************************************************
202 * UuidCompare [RPCRT4.@]
204 * PARAMS
205 * UUID *Uuid1 [I] Uuid to compare
206 * UUID *Uuid2 [I] Uuid to compare
207 * RPC_STATUS *Status [O] returns RPC_S_OK
209 * RETURNS
210 * -1 if Uuid1 is less than Uuid2
211 * 0 if Uuid1 and Uuid2 are equal
212 * 1 if Uuid1 is greater than Uuid2
214 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
216 int i;
218 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
220 *Status = RPC_S_OK;
222 if (!Uuid1) Uuid1 = &uuid_nil;
223 if (!Uuid2) Uuid2 = &uuid_nil;
225 if (Uuid1 == Uuid2) return 0;
227 if (Uuid1->Data1 != Uuid2->Data1)
228 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
230 if (Uuid1->Data2 != Uuid2->Data2)
231 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
233 if (Uuid1->Data3 != Uuid2->Data3)
234 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
236 for (i = 0; i < 8; i++) {
237 if (Uuid1->Data4[i] < Uuid2->Data4[i])
238 return -1;
239 if (Uuid1->Data4[i] > Uuid2->Data4[i])
240 return 1;
243 return 0;
246 /*************************************************************************
247 * UuidEqual [RPCRT4.@]
249 * PARAMS
250 * UUID *Uuid1 [I] Uuid to compare
251 * UUID *Uuid2 [I] Uuid to compare
252 * RPC_STATUS *Status [O] returns RPC_S_OK
254 * RETURNS
255 * TRUE/FALSE
257 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
259 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
260 return !UuidCompare(Uuid1, Uuid2, Status);
263 /*************************************************************************
264 * UuidIsNil [RPCRT4.@]
266 * PARAMS
267 * UUID *Uuid [I] Uuid to compare
268 * RPC_STATUS *Status [O] returns RPC_S_OK
270 * RETURNS
271 * TRUE/FALSE
273 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
275 TRACE("(%s)\n", debugstr_guid(Uuid));
276 if (!Uuid) return TRUE;
277 return !UuidCompare(Uuid, &uuid_nil, Status);
280 /*************************************************************************
281 * UuidCreateNil [RPCRT4.@]
283 * PARAMS
284 * UUID *Uuid [O] returns a nil UUID
286 * RETURNS
287 * RPC_S_OK
289 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
291 *Uuid = uuid_nil;
292 return RPC_S_OK;
295 /* Number of 100ns ticks per clock tick. To be safe, assume that the clock
296 resolution is at least 1000 * 100 * (1/1000000) = 1/10 of a second */
297 #define TICKS_PER_CLOCK_TICK 1000
298 #define SECSPERDAY 86400
299 #define TICKSPERSEC 10000000
300 /* UUID system time starts at October 15, 1582 */
301 #define SECS_15_OCT_1582_TO_1601 ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)
302 #define TICKS_15_OCT_1582_TO_1601 ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)
304 static void RPC_UuidGetSystemTime(ULONGLONG *time)
306 FILETIME ft;
308 GetSystemTimeAsFileTime(&ft);
310 *time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
311 *time += TICKS_15_OCT_1582_TO_1601;
314 /* Assume that a hardware address is at least 6 bytes long */
315 #define ADDRESS_BYTES_NEEDED 6
317 static RPC_STATUS RPC_UuidGetNodeAddress(BYTE *address)
319 int i;
320 DWORD status = RPC_S_OK;
322 ULONG buflen = sizeof(IP_ADAPTER_INFO);
323 PIP_ADAPTER_INFO adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
325 if (GetAdaptersInfo(adapter, &buflen) == ERROR_BUFFER_OVERFLOW) {
326 HeapFree(GetProcessHeap(), 0, adapter);
327 adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
330 if (GetAdaptersInfo(adapter, &buflen) == NO_ERROR) {
331 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
332 address[i] = adapter->Address[i];
335 /* We can't get a hardware address, just use random numbers.
336 Set the multicast bit to prevent conflicts with real cards. */
337 else {
338 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
339 address[i] = rand() & 0xff;
342 address[0] |= 0x01;
343 status = RPC_S_UUID_LOCAL_ONLY;
346 HeapFree(GetProcessHeap(), 0, adapter);
347 return status;
350 /*************************************************************************
351 * UuidCreate [RPCRT4.@]
353 * Creates a 128bit UUID.
355 * RETURNS
357 * RPC_S_OK if successful.
358 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
360 * FIXME: No compensation for changes across reloading
361 * this dll or across reboots (e.g. clock going
362 * backwards and swapped network cards). The RFC
363 * suggests using NVRAM for storing persistent
364 * values.
366 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
368 static int initialised, count;
370 ULONGLONG time;
371 static ULONGLONG timelast;
372 static WORD sequence;
374 static DWORD status;
375 static BYTE address[MAX_ADAPTER_ADDRESS_LENGTH];
377 EnterCriticalSection(&uuid_cs);
379 if (!initialised) {
380 RPC_UuidGetSystemTime(&timelast);
381 count = TICKS_PER_CLOCK_TICK;
383 sequence = ((rand() & 0xff) << 8) + (rand() & 0xff);
384 sequence &= 0x1fff;
386 status = RPC_UuidGetNodeAddress(address);
387 initialised = 1;
390 /* Generate time element of the UUID. Account for going faster
391 than our clock as well as the clock going backwards. */
392 while (1) {
393 RPC_UuidGetSystemTime(&time);
394 if (time > timelast) {
395 count = 0;
396 break;
398 if (time < timelast) {
399 sequence = (sequence + 1) & 0x1fff;
400 count = 0;
401 break;
403 if (count < TICKS_PER_CLOCK_TICK) {
404 count++;
405 break;
409 timelast = time;
410 time += count;
412 /* Pack the information into the UUID structure. */
414 Uuid->Data1 = (unsigned long)(time & 0xffffffff);
415 Uuid->Data2 = (unsigned short)((time >> 32) & 0xffff);
416 Uuid->Data3 = (unsigned short)((time >> 48) & 0x0fff);
418 /* This is a version 1 UUID */
419 Uuid->Data3 |= (1 << 12);
421 Uuid->Data4[0] = sequence & 0xff;
422 Uuid->Data4[1] = (sequence & 0x3f00) >> 8;
423 Uuid->Data4[1] |= 0x80;
425 Uuid->Data4[2] = address[0];
426 Uuid->Data4[3] = address[1];
427 Uuid->Data4[4] = address[2];
428 Uuid->Data4[5] = address[3];
429 Uuid->Data4[6] = address[4];
430 Uuid->Data4[7] = address[5];
432 LeaveCriticalSection(&uuid_cs);
434 TRACE("%s\n", debugstr_guid(Uuid));
436 return status;
439 /*************************************************************************
440 * UuidCreateSequential [RPCRT4.@]
442 * Creates a 128bit UUID.
444 * RETURNS
446 * RPC_S_OK if successful.
447 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
450 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
452 return UuidCreate(Uuid);
456 /*************************************************************************
457 * UuidHash [RPCRT4.@]
459 * Generates a hash value for a given UUID
461 * Code based on FreeDCE implementation
464 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
466 BYTE *data = (BYTE*)uuid;
467 short c0 = 0, c1 = 0, x, y;
468 unsigned int i;
470 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
472 TRACE("(%s)\n", debugstr_guid(uuid));
474 for (i=0; i<sizeof(UUID); i++) {
475 c0 += data[i];
476 c1 += c0;
479 x = -c1 % 255;
480 if (x < 0) x += 255;
482 y = (c1 - c0) % 255;
483 if (y < 0) y += 255;
485 *Status = RPC_S_OK;
486 return y*256 + x;
489 /*************************************************************************
490 * UuidToStringA [RPCRT4.@]
492 * Converts a UUID to a string.
494 * UUID format is 8 hex digits, followed by a hyphen then three groups of
495 * 4 hex digits each followed by a hyphen and then 12 hex digits
497 * RETURNS
499 * S_OK if successful.
500 * S_OUT_OF_MEMORY if unsuccessful.
502 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
504 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
506 if(!(*StringUuid))
507 return RPC_S_OUT_OF_MEMORY;
509 if (!Uuid) Uuid = &uuid_nil;
511 sprintf( (char*)*StringUuid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
512 Uuid->Data1, Uuid->Data2, Uuid->Data3,
513 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
514 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
515 Uuid->Data4[6], Uuid->Data4[7] );
517 return RPC_S_OK;
520 /*************************************************************************
521 * UuidToStringW [RPCRT4.@]
523 * Converts a UUID to a string.
525 * S_OK if successful.
526 * S_OUT_OF_MEMORY if unsuccessful.
528 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
530 char buf[37];
532 if (!Uuid) Uuid = &uuid_nil;
534 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
535 Uuid->Data1, Uuid->Data2, Uuid->Data3,
536 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
537 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
538 Uuid->Data4[6], Uuid->Data4[7] );
540 *StringUuid = RPCRT4_strdupAtoW(buf);
542 if(!(*StringUuid))
543 return RPC_S_OUT_OF_MEMORY;
545 return RPC_S_OK;
548 static const BYTE hex2bin[] =
550 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
551 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
552 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
553 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
554 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
555 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
556 0,10,11,12,13,14,15 /* 0x60 */
559 /***********************************************************************
560 * UuidFromStringA (RPCRT4.@)
562 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
564 int i;
566 if (!s) return UuidCreateNil( uuid );
568 if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
570 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
571 return RPC_S_INVALID_STRING_UUID;
573 for (i=0; i<36; i++)
575 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
576 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
579 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
581 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
582 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
583 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
584 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
586 /* these are just sequential bytes */
587 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
588 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
589 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
590 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
591 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
592 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
593 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
594 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
595 return RPC_S_OK;
599 /***********************************************************************
600 * UuidFromStringW (RPCRT4.@)
602 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
604 int i;
606 if (!s) return UuidCreateNil( uuid );
608 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
610 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
611 return RPC_S_INVALID_STRING_UUID;
613 for (i=0; i<36; i++)
615 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
616 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
619 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
621 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
622 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
623 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
624 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
626 /* these are just sequential bytes */
627 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
628 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
629 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
630 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
631 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
632 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
633 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
634 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
635 return RPC_S_OK;
638 /***********************************************************************
639 * DllRegisterServer (RPCRT4.@)
642 HRESULT WINAPI DllRegisterServer( void )
644 FIXME( "(): stub\n" );
645 return S_OK;
648 static BOOL RPCRT4_StartRPCSS(void)
650 PROCESS_INFORMATION pi;
651 STARTUPINFOA si;
652 static char cmd[6];
653 BOOL rslt;
655 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
656 ZeroMemory(&si, sizeof(STARTUPINFOA));
657 si.cb = sizeof(STARTUPINFOA);
659 /* apparently it's not OK to use a constant string below */
660 CopyMemory(cmd, "rpcss", 6);
662 /* FIXME: will this do the right thing when run as a test? */
663 rslt = CreateProcessA(
664 NULL, /* executable */
665 cmd, /* command line */
666 NULL, /* process security attributes */
667 NULL, /* primary thread security attributes */
668 FALSE, /* inherit handles */
669 0, /* creation flags */
670 NULL, /* use parent's environment */
671 NULL, /* use parent's current directory */
672 &si, /* STARTUPINFO pointer */
673 &pi /* PROCESS_INFORMATION */
676 if (rslt) {
677 CloseHandle(pi.hProcess);
678 CloseHandle(pi.hThread);
681 return rslt;
684 /***********************************************************************
685 * RPCRT4_RPCSSOnDemandCall (internal)
687 * Attempts to send a message to the RPCSS process
688 * on the local machine, invoking it if necessary.
689 * For remote RPCSS calls, use.... your imagination.
691 * PARAMS
692 * msg [I] pointer to the RPCSS message
693 * vardata_payload [I] pointer vardata portion of the RPCSS message
694 * reply [O] pointer to reply structure
696 * RETURNS
697 * TRUE if successful
698 * FALSE otherwise
700 BOOL RPCRT4_RPCSSOnDemandCall(PRPCSS_NP_MESSAGE msg, char *vardata_payload, PRPCSS_NP_REPLY reply)
702 HANDLE client_handle;
703 BOOL ret;
704 int i, j = 0;
706 TRACE("(msg == %p, vardata_payload == %p, reply == %p)\n", msg, vardata_payload, reply);
708 client_handle = RPCRT4_RpcssNPConnect();
710 while (INVALID_HANDLE_VALUE == client_handle) {
711 /* start the RPCSS process */
712 if (!RPCRT4_StartRPCSS()) {
713 ERR("Unable to start RPCSS process.\n");
714 return FALSE;
716 /* wait for a connection (w/ periodic polling) */
717 for (i = 0; i < 60; i++) {
718 Sleep(200);
719 client_handle = RPCRT4_RpcssNPConnect();
720 if (INVALID_HANDLE_VALUE != client_handle) break;
722 /* we are only willing to try twice */
723 if (j++ >= 1) break;
726 if (INVALID_HANDLE_VALUE == client_handle) {
727 /* no dice! */
728 ERR("Unable to connect to RPCSS process!\n");
729 SetLastError(RPC_E_SERVER_DIED_DNE);
730 return FALSE;
733 /* great, we're connected. now send the message */
734 ret = TRUE;
735 if (!RPCRT4_SendReceiveNPMsg(client_handle, msg, vardata_payload, reply)) {
736 ERR("Something is amiss: RPC_SendReceive failed.\n");
737 ret = FALSE;
739 CloseHandle(client_handle);
741 return ret;
744 #define MAX_RPC_ERROR_TEXT 256
746 /******************************************************************************
747 * DceErrorInqTextW (rpcrt4.@)
749 * Notes
750 * 1. On passing a NULL pointer the code does bomb out.
751 * 2. The size of the required buffer is not defined in the documentation.
752 * It appears to be 256.
753 * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
754 * of any value for which it does.
755 * 4. The MSDN documentation currently declares that the second argument is
756 * unsigned char *, even for the W version. I don't believe it.
758 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
760 DWORD count;
761 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
762 FORMAT_MESSAGE_IGNORE_INSERTS,
763 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
764 if (!count)
766 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
767 FORMAT_MESSAGE_IGNORE_INSERTS,
768 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
769 if (!count)
771 ERR ("Failed to translate error\n");
772 return RPC_S_INVALID_ARG;
775 return RPC_S_OK;
778 /******************************************************************************
779 * DceErrorInqTextA (rpcrt4.@)
781 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
783 RPC_STATUS status;
784 WCHAR bufferW [MAX_RPC_ERROR_TEXT];
785 if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
787 if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
788 NULL, NULL))
790 ERR ("Failed to translate error\n");
791 status = RPC_S_INVALID_ARG;
794 return status;
797 /******************************************************************************
798 * I_RpcAllocate (rpcrt4.@)
800 void * WINAPI I_RpcAllocate(unsigned int Size)
802 return HeapAlloc(GetProcessHeap(), 0, Size);
805 /******************************************************************************
806 * I_RpcFree (rpcrt4.@)
808 void WINAPI I_RpcFree(void *Object)
810 HeapFree(GetProcessHeap(), 0, Object);
813 /******************************************************************************
814 * I_RpcMapWin32Status (rpcrt4.@)
816 * Maps Win32 RPC error codes to NT statuses.
818 * PARAMS
819 * status [I] Win32 RPC error code.
821 * RETURNS
822 * Appropriate translation into an NT status code.
824 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
826 TRACE("(%ld)\n", status);
827 switch (status)
829 case ERROR_ACCESS_DENIED: return STATUS_ACCESS_DENIED;
830 case ERROR_INVALID_HANDLE: return RPC_NT_SS_CONTEXT_MISMATCH;
831 case ERROR_OUTOFMEMORY: return STATUS_NO_MEMORY;
832 case ERROR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
833 case ERROR_INSUFFICIENT_BUFFER: return STATUS_BUFFER_TOO_SMALL;
834 case ERROR_MAX_THRDS_REACHED: return STATUS_NO_MEMORY;
835 case ERROR_NOACCESS: return STATUS_ACCESS_VIOLATION;
836 case ERROR_NOT_ENOUGH_SERVER_MEMORY: return STATUS_INSUFF_SERVER_RESOURCES;
837 case ERROR_WRONG_PASSWORD: return STATUS_WRONG_PASSWORD;
838 case ERROR_INVALID_LOGON_HOURS: return STATUS_INVALID_LOGON_HOURS;
839 case ERROR_PASSWORD_EXPIRED: return STATUS_PASSWORD_EXPIRED;
840 case ERROR_ACCOUNT_DISABLED: return STATUS_ACCOUNT_DISABLED;
841 case ERROR_INVALID_SECURITY_DESCR: return STATUS_INVALID_SECURITY_DESCR;
842 case RPC_S_INVALID_STRING_BINDING: return RPC_NT_INVALID_STRING_BINDING;
843 case RPC_S_WRONG_KIND_OF_BINDING: return RPC_NT_WRONG_KIND_OF_BINDING;
844 case RPC_S_INVALID_BINDING: return RPC_NT_INVALID_BINDING;
845 case RPC_S_PROTSEQ_NOT_SUPPORTED: return RPC_NT_PROTSEQ_NOT_SUPPORTED;
846 case RPC_S_INVALID_RPC_PROTSEQ: return RPC_NT_INVALID_RPC_PROTSEQ;
847 case RPC_S_INVALID_STRING_UUID: return RPC_NT_INVALID_STRING_UUID;
848 case RPC_S_INVALID_ENDPOINT_FORMAT: return RPC_NT_INVALID_ENDPOINT_FORMAT;
849 case RPC_S_INVALID_NET_ADDR: return RPC_NT_INVALID_NET_ADDR;
850 case RPC_S_NO_ENDPOINT_FOUND: return RPC_NT_NO_ENDPOINT_FOUND;
851 case RPC_S_INVALID_TIMEOUT: return RPC_NT_INVALID_TIMEOUT;
852 case RPC_S_OBJECT_NOT_FOUND: return RPC_NT_OBJECT_NOT_FOUND;
853 case RPC_S_ALREADY_REGISTERED: return RPC_NT_ALREADY_REGISTERED;
854 case RPC_S_TYPE_ALREADY_REGISTERED: return RPC_NT_TYPE_ALREADY_REGISTERED;
855 case RPC_S_ALREADY_LISTENING: return RPC_NT_ALREADY_LISTENING;
856 case RPC_S_NO_PROTSEQS_REGISTERED: return RPC_NT_NO_PROTSEQS_REGISTERED;
857 case RPC_S_NOT_LISTENING: return RPC_NT_NOT_LISTENING;
858 case RPC_S_UNKNOWN_MGR_TYPE: return RPC_NT_UNKNOWN_MGR_TYPE;
859 case RPC_S_UNKNOWN_IF: return RPC_NT_UNKNOWN_IF;
860 case RPC_S_NO_BINDINGS: return RPC_NT_NO_BINDINGS;
861 case RPC_S_NO_PROTSEQS: return RPC_NT_NO_PROTSEQS;
862 case RPC_S_CANT_CREATE_ENDPOINT: return RPC_NT_CANT_CREATE_ENDPOINT;
863 case RPC_S_OUT_OF_RESOURCES: return RPC_NT_OUT_OF_RESOURCES;
864 case RPC_S_SERVER_UNAVAILABLE: return RPC_NT_SERVER_UNAVAILABLE;
865 case RPC_S_SERVER_TOO_BUSY: return RPC_NT_SERVER_TOO_BUSY;
866 case RPC_S_INVALID_NETWORK_OPTIONS: return RPC_NT_INVALID_NETWORK_OPTIONS;
867 case RPC_S_NO_CALL_ACTIVE: return RPC_NT_NO_CALL_ACTIVE;
868 case RPC_S_CALL_FAILED: return RPC_NT_CALL_FAILED;
869 case RPC_S_CALL_FAILED_DNE: return RPC_NT_CALL_FAILED_DNE;
870 case RPC_S_PROTOCOL_ERROR: return RPC_NT_PROTOCOL_ERROR;
871 case RPC_S_UNSUPPORTED_TRANS_SYN: return RPC_NT_UNSUPPORTED_TRANS_SYN;
872 case RPC_S_UNSUPPORTED_TYPE: return RPC_NT_UNSUPPORTED_TYPE;
873 case RPC_S_INVALID_TAG: return RPC_NT_INVALID_TAG;
874 case RPC_S_INVALID_BOUND: return RPC_NT_INVALID_BOUND;
875 case RPC_S_NO_ENTRY_NAME: return RPC_NT_NO_ENTRY_NAME;
876 case RPC_S_INVALID_NAME_SYNTAX: return RPC_NT_INVALID_NAME_SYNTAX;
877 case RPC_S_UNSUPPORTED_NAME_SYNTAX: return RPC_NT_UNSUPPORTED_NAME_SYNTAX;
878 case RPC_S_UUID_NO_ADDRESS: return RPC_NT_UUID_NO_ADDRESS;
879 case RPC_S_DUPLICATE_ENDPOINT: return RPC_NT_DUPLICATE_ENDPOINT;
880 case RPC_S_UNKNOWN_AUTHN_TYPE: return RPC_NT_UNKNOWN_AUTHN_TYPE;
881 case RPC_S_MAX_CALLS_TOO_SMALL: return RPC_NT_MAX_CALLS_TOO_SMALL;
882 case RPC_S_STRING_TOO_LONG: return RPC_NT_STRING_TOO_LONG;
883 case RPC_S_PROTSEQ_NOT_FOUND: return RPC_NT_PROTSEQ_NOT_FOUND;
884 case RPC_S_PROCNUM_OUT_OF_RANGE: return RPC_NT_PROCNUM_OUT_OF_RANGE;
885 case RPC_S_BINDING_HAS_NO_AUTH: return RPC_NT_BINDING_HAS_NO_AUTH;
886 case RPC_S_UNKNOWN_AUTHN_SERVICE: return RPC_NT_UNKNOWN_AUTHN_SERVICE;
887 case RPC_S_UNKNOWN_AUTHN_LEVEL: return RPC_NT_UNKNOWN_AUTHN_LEVEL;
888 case RPC_S_INVALID_AUTH_IDENTITY: return RPC_NT_INVALID_AUTH_IDENTITY;
889 case RPC_S_UNKNOWN_AUTHZ_SERVICE: return RPC_NT_UNKNOWN_AUTHZ_SERVICE;
890 case EPT_S_INVALID_ENTRY: return EPT_NT_INVALID_ENTRY;
891 case EPT_S_CANT_PERFORM_OP: return EPT_NT_CANT_PERFORM_OP;
892 case EPT_S_NOT_REGISTERED: return EPT_NT_NOT_REGISTERED;
893 case EPT_S_CANT_CREATE: return EPT_NT_CANT_CREATE;
894 case RPC_S_NOTHING_TO_EXPORT: return RPC_NT_NOTHING_TO_EXPORT;
895 case RPC_S_INCOMPLETE_NAME: return RPC_NT_INCOMPLETE_NAME;
896 case RPC_S_INVALID_VERS_OPTION: return RPC_NT_INVALID_VERS_OPTION;
897 case RPC_S_NO_MORE_MEMBERS: return RPC_NT_NO_MORE_MEMBERS;
898 case RPC_S_NOT_ALL_OBJS_UNEXPORTED: return RPC_NT_NOT_ALL_OBJS_UNEXPORTED;
899 case RPC_S_INTERFACE_NOT_FOUND: return RPC_NT_INTERFACE_NOT_FOUND;
900 case RPC_S_ENTRY_ALREADY_EXISTS: return RPC_NT_ENTRY_ALREADY_EXISTS;
901 case RPC_S_ENTRY_NOT_FOUND: return RPC_NT_ENTRY_NOT_FOUND;
902 case RPC_S_NAME_SERVICE_UNAVAILABLE: return RPC_NT_NAME_SERVICE_UNAVAILABLE;
903 case RPC_S_INVALID_NAF_ID: return RPC_NT_INVALID_NAF_ID;
904 case RPC_S_CANNOT_SUPPORT: return RPC_NT_CANNOT_SUPPORT;
905 case RPC_S_NO_CONTEXT_AVAILABLE: return RPC_NT_NO_CONTEXT_AVAILABLE;
906 case RPC_S_INTERNAL_ERROR: return RPC_NT_INTERNAL_ERROR;
907 case RPC_S_ZERO_DIVIDE: return RPC_NT_ZERO_DIVIDE;
908 case RPC_S_ADDRESS_ERROR: return RPC_NT_ADDRESS_ERROR;
909 case RPC_S_FP_DIV_ZERO: return RPC_NT_FP_DIV_ZERO;
910 case RPC_S_FP_UNDERFLOW: return RPC_NT_FP_UNDERFLOW;
911 case RPC_S_FP_OVERFLOW: return RPC_NT_FP_OVERFLOW;
912 case RPC_S_CALL_IN_PROGRESS: return RPC_NT_CALL_IN_PROGRESS;
913 case RPC_S_NO_MORE_BINDINGS: return RPC_NT_NO_MORE_BINDINGS;
914 case RPC_S_CALL_CANCELLED: return RPC_NT_CALL_CANCELLED;
915 case RPC_S_INVALID_OBJECT: return RPC_NT_INVALID_OBJECT;
916 case RPC_S_INVALID_ASYNC_HANDLE: return RPC_NT_INVALID_ASYNC_HANDLE;
917 case RPC_S_INVALID_ASYNC_CALL: return RPC_NT_INVALID_ASYNC_CALL;
918 case RPC_S_GROUP_MEMBER_NOT_FOUND: return RPC_NT_GROUP_MEMBER_NOT_FOUND;
919 case RPC_X_NO_MORE_ENTRIES: return RPC_NT_NO_MORE_ENTRIES;
920 case RPC_X_SS_CHAR_TRANS_OPEN_FAIL: return RPC_NT_SS_CHAR_TRANS_OPEN_FAIL;
921 case RPC_X_SS_CHAR_TRANS_SHORT_FILE: return RPC_NT_SS_CHAR_TRANS_SHORT_FILE;
922 case RPC_X_SS_IN_NULL_CONTEXT: return RPC_NT_SS_IN_NULL_CONTEXT;
923 case RPC_X_SS_CONTEXT_DAMAGED: return RPC_NT_SS_CONTEXT_DAMAGED;
924 case RPC_X_SS_HANDLES_MISMATCH: return RPC_NT_SS_HANDLES_MISMATCH;
925 case RPC_X_SS_CANNOT_GET_CALL_HANDLE: return RPC_NT_SS_CANNOT_GET_CALL_HANDLE;
926 case RPC_X_NULL_REF_POINTER: return RPC_NT_NULL_REF_POINTER;
927 case RPC_X_ENUM_VALUE_OUT_OF_RANGE: return RPC_NT_ENUM_VALUE_OUT_OF_RANGE;
928 case RPC_X_BYTE_COUNT_TOO_SMALL: return RPC_NT_BYTE_COUNT_TOO_SMALL;
929 case RPC_X_BAD_STUB_DATA: return RPC_NT_BAD_STUB_DATA;
930 case RPC_X_PIPE_CLOSED: return RPC_NT_PIPE_CLOSED;
931 case RPC_X_PIPE_DISCIPLINE_ERROR: return RPC_NT_PIPE_DISCIPLINE_ERROR;
932 case RPC_X_PIPE_EMPTY: return RPC_NT_PIPE_EMPTY;
933 case ERROR_PASSWORD_MUST_CHANGE: return STATUS_PASSWORD_MUST_CHANGE;
934 case ERROR_ACCOUNT_LOCKED_OUT: return STATUS_ACCOUNT_LOCKED_OUT;
935 default: return status;
939 /******************************************************************************
940 * I_RpcExceptionFilter (rpcrt4.@)
942 int WINAPI I_RpcExceptionFilter(ULONG ExceptionCode)
944 TRACE("0x%x\n", ExceptionCode);
945 switch (ExceptionCode)
947 case STATUS_DATATYPE_MISALIGNMENT:
948 case STATUS_BREAKPOINT:
949 case STATUS_ACCESS_VIOLATION:
950 case STATUS_ILLEGAL_INSTRUCTION:
951 case STATUS_PRIVILEGED_INSTRUCTION:
952 case STATUS_INSTRUCTION_MISALIGNMENT:
953 case STATUS_STACK_OVERFLOW:
954 case STATUS_POSSIBLE_DEADLOCK:
955 return EXCEPTION_CONTINUE_SEARCH;
956 default:
957 return EXCEPTION_EXECUTE_HANDLER;
961 /******************************************************************************
962 * RpcErrorStartEnumeration (rpcrt4.@)
964 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
966 FIXME("(%p): stub\n", EnumHandle);
967 return RPC_S_ENTRY_NOT_FOUND;
970 /******************************************************************************
971 * RpcMgmtSetCancelTimeout (rpcrt4.@)
973 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
975 FIXME("(%d): stub\n", Timeout);
976 return RPC_S_OK;
979 static struct threaddata *get_or_create_threaddata(void)
981 struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
982 if (!tdata)
984 tdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*tdata));
985 if (!tdata) return NULL;
987 InitializeCriticalSection(&tdata->cs);
988 tdata->thread_id = GetCurrentThreadId();
990 EnterCriticalSection(&threaddata_cs);
991 list_add_tail(&threaddata_list, &tdata->entry);
992 LeaveCriticalSection(&threaddata_cs);
994 NtCurrentTeb()->ReservedForNtRpc = tdata;
995 return tdata;
997 return tdata;
1000 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
1002 struct threaddata *tdata = get_or_create_threaddata();
1003 if (!tdata) return;
1005 EnterCriticalSection(&tdata->cs);
1006 tdata->connection = Connection;
1007 LeaveCriticalSection(&tdata->cs);
1010 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
1012 struct threaddata *tdata = get_or_create_threaddata();
1013 if (!tdata) return;
1015 tdata->server_binding = Binding;
1018 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
1020 struct threaddata *tdata = get_or_create_threaddata();
1021 if (!tdata) return NULL;
1023 return tdata->server_binding;
1026 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext)
1028 struct threaddata *tdata = get_or_create_threaddata();
1029 struct context_handle_list *context_handle_list;
1031 if (!tdata) return;
1033 context_handle_list = HeapAlloc(GetProcessHeap(), 0, sizeof(*context_handle_list));
1034 if (!context_handle_list) return;
1036 context_handle_list->context_handle = SContext;
1037 context_handle_list->next = tdata->context_handle_list;
1038 tdata->context_handle_list = context_handle_list;
1041 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext)
1043 struct threaddata *tdata = get_or_create_threaddata();
1044 struct context_handle_list *current, *prev;
1046 if (!tdata) return;
1048 for (current = tdata->context_handle_list, prev = NULL; current; prev = current, current = current->next)
1050 if (current->context_handle == SContext)
1052 if (prev)
1053 prev->next = current->next;
1054 else
1055 tdata->context_handle_list = current->next;
1056 HeapFree(GetProcessHeap(), 0, current);
1057 return;
1062 NDR_SCONTEXT RPCRT4_PopThreadContextHandle(void)
1064 struct threaddata *tdata = get_or_create_threaddata();
1065 struct context_handle_list *context_handle_list;
1066 NDR_SCONTEXT context_handle;
1068 if (!tdata) return NULL;
1070 context_handle_list = tdata->context_handle_list;
1071 if (!context_handle_list) return NULL;
1072 tdata->context_handle_list = context_handle_list->next;
1074 context_handle = context_handle_list->context_handle;
1075 HeapFree(GetProcessHeap(), 0, context_handle_list);
1076 return context_handle;
1079 /******************************************************************************
1080 * RpcCancelThread (rpcrt4.@)
1082 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
1084 DWORD target_tid;
1085 struct threaddata *tdata;
1087 TRACE("(%p)\n", ThreadHandle);
1089 target_tid = GetThreadId(ThreadHandle);
1090 if (!target_tid)
1091 return RPC_S_INVALID_ARG;
1093 EnterCriticalSection(&threaddata_cs);
1094 LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
1095 if (tdata->thread_id == target_tid)
1097 EnterCriticalSection(&tdata->cs);
1098 if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
1099 LeaveCriticalSection(&tdata->cs);
1100 break;
1102 LeaveCriticalSection(&threaddata_cs);
1104 return RPC_S_OK;
1107 /******************************************************************************
1108 * RpcCancelThreadEx (rpcrt4.@)
1110 RPC_STATUS RPC_ENTRY RpcCancelThreadEx(void* ThreadHandle, LONG Timeout)
1112 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
1113 return RPC_S_OK;