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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * WINE RPC TODO's (and a few TODONT's)
22 * - Ove's decreasingly incomplete widl is an IDL compiler for wine. For widl
23 * to be wine's only IDL compiler, a fair bit of work remains to be done.
24 * until then we have used some midl-generated stuff. (What?)
25 * widl currently doesn't generate stub/proxy files required by wine's (O)RPC
26 * capabilities -- nor does it make those lovely format strings :(
27 * The MS MIDL compiler does some really esoteric stuff. Of course Ove has
28 * started with the less esoteric stuff. There are also lots of nice
29 * comments in there if you want to flex your bison and help build this monster.
31 * - RPC has a quite featureful error handling mechanism; basically none of this is
32 * implemented right now. We also have deficiencies on the compiler side, where
33 * wine's __TRY / __EXCEPT / __FINALLY macros are not even used for RpcTryExcept & co,
34 * due to syntactic differences! (we can fix it with widl by using __TRY)
36 * - There are several different memory allocation schemes for MSRPC.
37 * I don't even understand what they all are yet, much less have them
38 * properly implemented. Surely we are supposed to be doing something with
39 * the user-provided allocation/deallocation functions, but so far,
40 * I don't think we are doing this...
42 * - MSRPC provides impersonation capabilities which currently are not possible
43 * to implement in wine. At the very least we should implement the authorization
44 * API's & gracefully ignore the irrelevant stuff (to an extent we already do).
46 * - Some transports are not yet implemented. The existing transport implementations
47 * are incomplete and may be bug-infested.
49 * - The various transports that we do support ought to be supported in a more
50 * object-oriented manner, as in DCE's RPC implementation, instead of cluttering
51 * up the code with conditionals like we do now.
53 * - Data marshalling: So far, only the beginnings of a full implementation
54 * exist in wine. NDR protocol itself is documented, but the MS API's to
55 * convert data-types in memory into NDR are not. This is challenging work,
56 * and has supposedly been "at the top of Greg's queue" for several months now.
58 * - ORPC is RPC for OLE; once we have a working RPC framework, we can
59 * use it to implement out-of-process OLE client/server communications.
60 * ATM there is maybe a disconnect between the marshalling in the OLE DLL's
61 * and the marshalling going on here [TODO: well, is there or not?]
63 * - In-source API Documentation, at least for those functions which we have
64 * implemented, but preferably for everything we can document, would be nice,
65 * since some of this stuff is quite obscure.
67 * - Name services... [TODO: what about them]
69 * - Protocol Towers: Totally unimplemented.... I think.
71 * - Context Handle Rundown: whatever that is.
73 * - Nested RPC's: Totally unimplemented.
75 * - Statistics: we are supposed to be keeping various counters. we aren't.
77 * - Async RPC: Unimplemented.
79 * - XML/http RPC: Somewhere there's an XML fiend that wants to do this! Betcha
80 * we could use these as a transport for RPC's across computers without a
81 * permissions and/or licensing crisis.
83 * - The NT "ports" API, aka LPC. Greg claims this is on his radar. Might (or
84 * might not) enable users to get some kind of meaningful result out of
85 * NT-based native rpcrt4's. Commonly-used transport for self-to-self RPC's.
87 * - ...? More stuff I haven't thought of. If you think of more RPC todo's
88 * drop me an e-mail <gmturner007@ameritech.net> or send a patch to the
89 * wine-patches mailing list.
100 #include "winerror.h"
104 #include "iphlpapi.h"
105 #include "wine/unicode.h"
110 #include "rpcproxy.h"
112 #include "rpc_binding.h"
113 #include "rpcss_np_client.h"
115 #include "wine/debug.h"
117 WINE_DEFAULT_DEBUG_CHANNEL(ole);
119 static UUID uuid_nil;
120 static HANDLE master_mutex;
122 HANDLE RPCRT4_GetMasterMutex(void)
127 static CRITICAL_SECTION uuid_cs;
128 static CRITICAL_SECTION_DEBUG critsect_debug =
131 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
132 0, 0, { 0, (DWORD)(__FILE__ ": uuid_cs") }
134 static CRITICAL_SECTION uuid_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
136 /***********************************************************************
140 * hinstDLL [I] handle to the DLL's instance
142 * lpvReserved [I] reserved, must be NULL
149 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
152 case DLL_PROCESS_ATTACH:
153 DisableThreadLibraryCalls(hinstDLL);
154 master_mutex = CreateMutexA( NULL, FALSE, RPCSS_MASTER_MUTEX_NAME);
156 ERR("Failed to create master mutex\n");
159 case DLL_PROCESS_DETACH:
160 CloseHandle(master_mutex);
168 /*************************************************************************
169 * RpcStringFreeA [RPCRT4.@]
171 * Frees a character string allocated by the RPC run-time library.
175 * S_OK if successful.
177 RPC_STATUS WINAPI RpcStringFreeA(unsigned char** String)
179 HeapFree( GetProcessHeap(), 0, *String);
184 /*************************************************************************
185 * RpcStringFreeW [RPCRT4.@]
187 * Frees a character string allocated by the RPC run-time library.
191 * S_OK if successful.
193 RPC_STATUS WINAPI RpcStringFreeW(unsigned short** String)
195 HeapFree( GetProcessHeap(), 0, *String);
200 /*************************************************************************
201 * RpcRaiseException [RPCRT4.@]
203 * Raises an exception.
205 void WINAPI RpcRaiseException(RPC_STATUS exception)
207 /* FIXME: translate exception? */
208 RaiseException(exception, 0, 0, NULL);
211 /*************************************************************************
212 * UuidCompare [RPCRT4.@]
215 * UUID *Uuid1 [I] Uuid to compare
216 * UUID *Uuid2 [I] Uuid to compare
217 * RPC_STATUS *Status [O] returns RPC_S_OK
220 * -1 if Uuid1 is less than Uuid2
221 * 0 if Uuid1 and Uuid2 are equal
222 * 1 if Uuid1 is greater than Uuid2
224 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
228 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
232 if (!Uuid1) Uuid1 = &uuid_nil;
233 if (!Uuid2) Uuid2 = &uuid_nil;
235 if (Uuid1 == Uuid2) return 0;
237 if (Uuid1->Data1 != Uuid2->Data1)
238 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
240 if (Uuid1->Data2 != Uuid2->Data2)
241 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
243 if (Uuid1->Data3 != Uuid2->Data3)
244 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
246 for (i = 0; i < 8; i++) {
247 if (Uuid1->Data4[i] < Uuid2->Data4[i])
249 if (Uuid1->Data4[i] > Uuid2->Data4[i])
256 /*************************************************************************
257 * UuidEqual [RPCRT4.@]
260 * UUID *Uuid1 [I] Uuid to compare
261 * UUID *Uuid2 [I] Uuid to compare
262 * RPC_STATUS *Status [O] returns RPC_S_OK
267 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
269 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
270 return !UuidCompare(Uuid1, Uuid2, Status);
273 /*************************************************************************
274 * UuidIsNil [RPCRT4.@]
277 * UUID *Uuid [I] Uuid to compare
278 * RPC_STATUS *Status [O] retuns RPC_S_OK
283 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
285 TRACE("(%s)\n", debugstr_guid(Uuid));
286 if (!Uuid) return TRUE;
287 return !UuidCompare(Uuid, &uuid_nil, Status);
290 /*************************************************************************
291 * UuidCreateNil [RPCRT4.@]
294 * UUID *Uuid [O] returns a nil UUID
299 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
305 /* Number of 100ns ticks per clock tick. To be safe, assume that the clock
306 resolution is at least 1000 * 100 * (1/1000000) = 1/10 of a second */
307 #define TICKS_PER_CLOCK_TICK 1000
308 #define SECSPERDAY 86400
309 #define TICKSPERSEC 10000000
310 /* UUID system time starts at October 15, 1582 */
311 #define SECS_15_OCT_1582_TO_1601 ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)
312 #define TICKS_15_OCT_1582_TO_1601 ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)
314 static void RPC_UuidGetSystemTime(ULONGLONG *time)
318 GetSystemTimeAsFileTime(&ft);
320 *time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
321 *time += TICKS_15_OCT_1582_TO_1601;
324 /* Assume that a hardware address is at least 6 bytes long */
325 #define ADDRESS_BYTES_NEEDED 6
327 static RPC_STATUS RPC_UuidGetNodeAddress(BYTE *address)
330 DWORD status = RPC_S_OK;
332 ULONG buflen = sizeof(IP_ADAPTER_INFO);
333 PIP_ADAPTER_INFO adapter = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(), 0, buflen);
335 if (GetAdaptersInfo(adapter, &buflen) == ERROR_BUFFER_OVERFLOW) {
336 HeapFree(GetProcessHeap(), 0, adapter);
337 adapter = (IP_ADAPTER_INFO *)HeapAlloc(GetProcessHeap(), 0, buflen);
340 if (GetAdaptersInfo(adapter, &buflen) == NO_ERROR) {
341 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
342 address[i] = adapter->Address[i];
345 /* We can't get a hardware address, just use random numbers.
346 Set the multicast bit to prevent conflicts with real cards. */
348 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
349 address[i] = rand() & 0xff;
353 status = RPC_S_UUID_LOCAL_ONLY;
356 HeapFree(GetProcessHeap(), 0, adapter);
360 /*************************************************************************
361 * UuidCreate [RPCRT4.@]
363 * Creates a 128bit UUID.
367 * RPC_S_OK if successful.
368 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
370 * FIXME: No compensation for changes across reloading
371 * this dll or across reboots (e.g. clock going
372 * backwards and swapped network cards). The RFC
373 * suggests using NVRAM for storing persistent
376 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
378 static int initialised, count;
381 static ULONGLONG timelast;
382 static WORD sequence;
385 static BYTE address[MAX_ADAPTER_ADDRESS_LENGTH];
387 EnterCriticalSection(&uuid_cs);
390 RPC_UuidGetSystemTime(&timelast);
391 count = TICKS_PER_CLOCK_TICK;
393 sequence = ((rand() & 0xff) << 8) + (rand() & 0xff);
396 status = RPC_UuidGetNodeAddress(address);
400 /* Generate time element of the UUID. Account for going faster
401 than our clock as well as the clock going backwards. */
403 RPC_UuidGetSystemTime(&time);
404 if (time > timelast) {
408 if (time < timelast) {
409 sequence = (sequence + 1) & 0x1fff;
413 if (count < TICKS_PER_CLOCK_TICK) {
422 /* Pack the information into the UUID structure. */
424 Uuid->Data1 = (unsigned long)(time & 0xffffffff);
425 Uuid->Data2 = (unsigned short)((time >> 32) & 0xffff);
426 Uuid->Data3 = (unsigned short)((time >> 48) & 0x0fff);
428 /* This is a version 1 UUID */
429 Uuid->Data3 |= (1 << 12);
431 Uuid->Data4[0] = sequence & 0xff;
432 Uuid->Data4[1] = (sequence & 0x3f00) >> 8;
433 Uuid->Data4[1] |= 0x80;
435 Uuid->Data4[2] = address[0];
436 Uuid->Data4[3] = address[1];
437 Uuid->Data4[4] = address[2];
438 Uuid->Data4[5] = address[3];
439 Uuid->Data4[6] = address[4];
440 Uuid->Data4[7] = address[5];
442 LeaveCriticalSection(&uuid_cs);
444 TRACE("%s\n", debugstr_guid(Uuid));
449 /*************************************************************************
450 * UuidCreateSequential [RPCRT4.@]
452 * Creates a 128bit UUID.
456 * RPC_S_OK if successful.
457 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
460 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
462 return UuidCreate(Uuid);
466 /*************************************************************************
467 * UuidHash [RPCRT4.@]
469 * Generates a hash value for a given UUID
471 * Code based on FreeDCE implementation
474 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
476 BYTE *data = (BYTE*)uuid;
477 short c0 = 0, c1 = 0, x, y;
480 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
482 TRACE("(%s)\n", debugstr_guid(uuid));
484 for (i=0; i<sizeof(UUID); i++) {
499 /*************************************************************************
500 * UuidToStringA [RPCRT4.@]
502 * Converts a UUID to a string.
504 * UUID format is 8 hex digits, followed by a hyphen then three groups of
505 * 4 hex digits each followed by a hyphen and then 12 hex digits
509 * S_OK if successful.
510 * S_OUT_OF_MEMORY if unsucessful.
512 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, unsigned char** StringUuid)
514 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
517 return RPC_S_OUT_OF_MEMORY;
519 if (!Uuid) Uuid = &uuid_nil;
521 sprintf(*StringUuid, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
522 Uuid->Data1, Uuid->Data2, Uuid->Data3,
523 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
524 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
525 Uuid->Data4[6], Uuid->Data4[7] );
530 /*************************************************************************
531 * UuidToStringW [RPCRT4.@]
533 * Converts a UUID to a string.
535 * S_OK if successful.
536 * S_OUT_OF_MEMORY if unsucessful.
538 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, unsigned short** StringUuid)
542 if (!Uuid) Uuid = &uuid_nil;
544 sprintf(buf, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
545 Uuid->Data1, Uuid->Data2, Uuid->Data3,
546 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
547 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
548 Uuid->Data4[6], Uuid->Data4[7] );
550 *StringUuid = RPCRT4_strdupAtoW(buf);
553 return RPC_S_OUT_OF_MEMORY;
558 static const BYTE hex2bin[] =
560 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
561 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
562 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
563 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
564 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
565 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
566 0,10,11,12,13,14,15 /* 0x60 */
569 /***********************************************************************
570 * UuidFromStringA (RPCRT4.@)
572 RPC_STATUS WINAPI UuidFromStringA(unsigned char* str, UUID *uuid)
574 BYTE *s = (BYTE *)str;
577 if (!s) return UuidCreateNil( uuid );
579 if (strlen(s) != 36) return RPC_S_INVALID_STRING_UUID;
581 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
582 return RPC_S_INVALID_STRING_UUID;
586 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
587 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
590 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
592 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
593 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
594 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
595 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
597 /* these are just sequential bytes */
598 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
599 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
600 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
601 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
602 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
603 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
604 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
605 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
610 /***********************************************************************
611 * UuidFromStringW (RPCRT4.@)
613 RPC_STATUS WINAPI UuidFromStringW(unsigned short* s, UUID *uuid)
617 if (!s) return UuidCreateNil( uuid );
619 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
621 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
622 return RPC_S_INVALID_STRING_UUID;
626 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
627 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
630 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
632 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
633 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
634 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
635 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
637 /* these are just sequential bytes */
638 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
639 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
640 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
641 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
642 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
643 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
644 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
645 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
649 /***********************************************************************
650 * DllRegisterServer (RPCRT4.@)
653 HRESULT WINAPI RPCRT4_DllRegisterServer( void )
655 FIXME( "(): stub\n" );
659 BOOL RPCRT4_StartRPCSS(void)
661 PROCESS_INFORMATION pi;
666 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
667 ZeroMemory(&si, sizeof(STARTUPINFOA));
668 si.cb = sizeof(STARTUPINFOA);
670 /* apparently it's not OK to use a constant string below */
671 CopyMemory(cmd, "rpcss", 6);
673 /* FIXME: will this do the right thing when run as a test? */
674 rslt = CreateProcessA(
675 NULL, /* executable */
676 cmd, /* command line */
677 NULL, /* process security attributes */
678 NULL, /* primary thread security attributes */
679 FALSE, /* inherit handles */
680 0, /* creation flags */
681 NULL, /* use parent's environment */
682 NULL, /* use parent's current directory */
683 &si, /* STARTUPINFO pointer */
684 &pi /* PROCESS_INFORMATION */
688 CloseHandle(pi.hProcess);
689 CloseHandle(pi.hThread);
695 /***********************************************************************
696 * RPCRT4_RPCSSOnDemandCall (internal)
698 * Attempts to send a message to the RPCSS process
699 * on the local machine, invoking it if necessary.
700 * For remote RPCSS calls, use.... your imagination.
703 * msg [I] pointer to the RPCSS message
704 * vardata_payload [I] pointer vardata portion of the RPCSS message
705 * reply [O] pointer to reply structure
711 BOOL RPCRT4_RPCSSOnDemandCall(PRPCSS_NP_MESSAGE msg, char *vardata_payload, PRPCSS_NP_REPLY reply)
713 HANDLE client_handle;
716 TRACE("(msg == %p, vardata_payload == %p, reply == %p)\n", msg, vardata_payload, reply);
718 client_handle = RPCRT4_RpcssNPConnect();
720 while (!client_handle) {
721 /* start the RPCSS process */
722 if (!RPCRT4_StartRPCSS()) {
723 ERR("Unable to start RPCSS process.\n");
726 /* wait for a connection (w/ periodic polling) */
727 for (i = 0; i < 60; i++) {
729 client_handle = RPCRT4_RpcssNPConnect();
730 if (client_handle) break;
732 /* we are only willing to try twice */
736 if (!client_handle) {
738 ERR("Unable to connect to RPCSS process!\n");
739 SetLastError(RPC_E_SERVER_DIED_DNE);
743 /* great, we're connected. now send the message */
744 if (!RPCRT4_SendReceiveNPMsg(client_handle, msg, vardata_payload, reply)) {
745 ERR("Something is amiss: RPC_SendReceive failed.\n");