Copy rpoolmgr.h from trunk
[reactos.git] / reactos / lib / rpcrt4 / rpcrt4_main.c
1 /*
2 * RPCRT4
3 *
4 * Copyright 2000 Huw D M Davies for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 *
20 * WINE RPC TODO's (and a few TODONT's)
21 *
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.
30 *
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)
35 *
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...
41 *
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).
45 *
46 * - Some transports are not yet implemented. The existing transport implementations
47 * are incomplete and may be bug-infested.
48 *
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.
52 *
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.
57 *
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?]
62 *
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.
66 *
67 * - Name services... [TODO: what about them]
68 *
69 * - Protocol Towers: Totally unimplemented.... I think.
70 *
71 * - Context Handle Rundown: whatever that is.
72 *
73 * - Nested RPC's: Totally unimplemented.
74 *
75 * - Statistics: we are supposed to be keeping various counters. we aren't.
76 *
77 * - Async RPC: Unimplemented.
78 *
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.
82 *
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.
86 *
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.
90 */
91
92 #include "config.h"
93
94 #include <stdarg.h>
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <string.h>
98
99 #include "windef.h"
100 #include "winerror.h"
101 #include "winbase.h"
102 #include "winuser.h"
103 #include "iptypes.h"
104 #include "iphlpapi.h"
105 #include "wine/unicode.h"
106 #include "rpc.h"
107
108 #include "ole2.h"
109 #include "rpcndr.h"
110 #include "rpcproxy.h"
111
112 #include "rpc_binding.h"
113 #include "rpcss_np_client.h"
114
115 #include "wine/debug.h"
116
117 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
118
119 static UUID uuid_nil;
120 static HANDLE master_mutex;
121
122 HANDLE RPCRT4_GetMasterMutex(void)
123 {
124 return master_mutex;
125 }
126
127 static CRITICAL_SECTION uuid_cs;
128 static CRITICAL_SECTION_DEBUG critsect_debug =
129 {
130 0, 0, &uuid_cs,
131 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
132 0, 0, { 0, (DWORD)(__FILE__ ": uuid_cs") }
133 };
134 static CRITICAL_SECTION uuid_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
135
136 /***********************************************************************
137 * DllMain
138 *
139 * PARAMS
140 * hinstDLL [I] handle to the DLL's instance
141 * fdwReason [I]
142 * lpvReserved [I] reserved, must be NULL
143 *
144 * RETURNS
145 * Success: TRUE
146 * Failure: FALSE
147 */
148
149 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
150 {
151 switch (fdwReason) {
152 case DLL_PROCESS_ATTACH:
153 DisableThreadLibraryCalls(hinstDLL);
154 master_mutex = CreateMutexA( NULL, FALSE, RPCSS_MASTER_MUTEX_NAME);
155 break;
156
157 case DLL_PROCESS_DETACH:
158 CloseHandle(master_mutex);
159 master_mutex = NULL;
160 break;
161 }
162
163 return TRUE;
164 }
165
166 /*************************************************************************
167 * RpcStringFreeA [RPCRT4.@]
168 *
169 * Frees a character string allocated by the RPC run-time library.
170 *
171 * RETURNS
172 *
173 * S_OK if successful.
174 */
175 RPC_STATUS WINAPI RpcStringFreeA(unsigned char** String)
176 {
177 HeapFree( GetProcessHeap(), 0, *String);
178
179 return RPC_S_OK;
180 }
181
182 /*************************************************************************
183 * RpcStringFreeW [RPCRT4.@]
184 *
185 * Frees a character string allocated by the RPC run-time library.
186 *
187 * RETURNS
188 *
189 * S_OK if successful.
190 */
191 RPC_STATUS WINAPI RpcStringFreeW(unsigned short** String)
192 {
193 HeapFree( GetProcessHeap(), 0, *String);
194
195 return RPC_S_OK;
196 }
197
198 /*************************************************************************
199 * RpcRaiseException [RPCRT4.@]
200 *
201 * Raises an exception.
202 */
203 void WINAPI RpcRaiseException(RPC_STATUS exception)
204 {
205 /* FIXME: translate exception? */
206 RaiseException(exception, 0, 0, NULL);
207 }
208
209 /*************************************************************************
210 * UuidCompare [RPCRT4.@]
211 *
212 * PARAMS
213 * UUID *Uuid1 [I] Uuid to compare
214 * UUID *Uuid2 [I] Uuid to compare
215 * RPC_STATUS *Status [O] returns RPC_S_OK
216 *
217 * RETURNS
218 * -1 if Uuid1 is less than Uuid2
219 * 0 if Uuid1 and Uuid2 are equal
220 * 1 if Uuid1 is greater than Uuid2
221 */
222 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
223 {
224 int i;
225
226 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
227
228 *Status = RPC_S_OK;
229
230 if (!Uuid1) Uuid1 = &uuid_nil;
231 if (!Uuid2) Uuid2 = &uuid_nil;
232
233 if (Uuid1 == Uuid2) return 0;
234
235 if (Uuid1->Data1 != Uuid2->Data1)
236 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
237
238 if (Uuid1->Data2 != Uuid2->Data2)
239 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
240
241 if (Uuid1->Data3 != Uuid2->Data3)
242 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
243
244 for (i = 0; i < 8; i++) {
245 if (Uuid1->Data4[i] < Uuid2->Data4[i])
246 return -1;
247 if (Uuid1->Data4[i] > Uuid2->Data4[i])
248 return 1;
249 }
250
251 return 0;
252 }
253
254 /*************************************************************************
255 * UuidEqual [RPCRT4.@]
256 *
257 * PARAMS
258 * UUID *Uuid1 [I] Uuid to compare
259 * UUID *Uuid2 [I] Uuid to compare
260 * RPC_STATUS *Status [O] returns RPC_S_OK
261 *
262 * RETURNS
263 * TRUE/FALSE
264 */
265 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
266 {
267 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
268 return !UuidCompare(Uuid1, Uuid2, Status);
269 }
270
271 /*************************************************************************
272 * UuidIsNil [RPCRT4.@]
273 *
274 * PARAMS
275 * UUID *Uuid [I] Uuid to compare
276 * RPC_STATUS *Status [O] retuns RPC_S_OK
277 *
278 * RETURNS
279 * TRUE/FALSE
280 */
281 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
282 {
283 TRACE("(%s)\n", debugstr_guid(Uuid));
284 if (!Uuid) return TRUE;
285 return !UuidCompare(Uuid, &uuid_nil, Status);
286 }
287
288 /*************************************************************************
289 * UuidCreateNil [RPCRT4.@]
290 *
291 * PARAMS
292 * UUID *Uuid [O] returns a nil UUID
293 *
294 * RETURNS
295 * RPC_S_OK
296 */
297 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
298 {
299 *Uuid = uuid_nil;
300 return RPC_S_OK;
301 }
302
303 /* Number of 100ns ticks per clock tick. To be safe, assume that the clock
304 resolution is at least 1000 * 100 * (1/1000000) = 1/10 of a second */
305 #define TICKS_PER_CLOCK_TICK 1000
306 #define SECSPERDAY 86400
307 #define TICKSPERSEC 10000000
308 /* UUID system time starts at October 15, 1582 */
309 #define SECS_15_OCT_1582_TO_1601 ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)
310 #define TICKS_15_OCT_1582_TO_1601 ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)
311
312 static void RPC_UuidGetSystemTime(ULONGLONG *time)
313 {
314 FILETIME ft;
315
316 GetSystemTimeAsFileTime(&ft);
317
318 *time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
319 *time += TICKS_15_OCT_1582_TO_1601;
320 }
321
322 /* Assume that a hardware address is at least 6 bytes long */
323 #define ADDRESS_BYTES_NEEDED 6
324
325 static RPC_STATUS RPC_UuidGetNodeAddress(BYTE *address)
326 {
327 int i;
328 DWORD status = RPC_S_OK;
329
330 ULONG buflen = sizeof(IP_ADAPTER_INFO);
331 PIP_ADAPTER_INFO adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
332
333 if (GetAdaptersInfo(adapter, &buflen) == ERROR_BUFFER_OVERFLOW) {
334 HeapFree(GetProcessHeap(), 0, adapter);
335 adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
336 }
337
338 if (GetAdaptersInfo(adapter, &buflen) == NO_ERROR) {
339 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
340 address[i] = adapter->Address[i];
341 }
342 }
343 /* We can't get a hardware address, just use random numbers.
344 Set the multicast bit to prevent conflicts with real cards. */
345 else {
346 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
347 address[i] = rand() & 0xff;
348 }
349
350 address[0] |= 0x01;
351 status = RPC_S_UUID_LOCAL_ONLY;
352 }
353
354 HeapFree(GetProcessHeap(), 0, adapter);
355 return status;
356 }
357
358 /*************************************************************************
359 * UuidCreate [RPCRT4.@]
360 *
361 * Creates a 128bit UUID.
362 *
363 * RETURNS
364 *
365 * RPC_S_OK if successful.
366 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
367 *
368 * FIXME: No compensation for changes across reloading
369 * this dll or across reboots (e.g. clock going
370 * backwards and swapped network cards). The RFC
371 * suggests using NVRAM for storing persistent
372 * values.
373 */
374 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
375 {
376 static int initialised, count;
377
378 ULONGLONG time;
379 static ULONGLONG timelast;
380 static WORD sequence;
381
382 static DWORD status;
383 static BYTE address[MAX_ADAPTER_ADDRESS_LENGTH];
384
385 EnterCriticalSection(&uuid_cs);
386
387 if (!initialised) {
388 RPC_UuidGetSystemTime(&timelast);
389 count = TICKS_PER_CLOCK_TICK;
390
391 sequence = ((rand() & 0xff) << 8) + (rand() & 0xff);
392 sequence &= 0x1fff;
393
394 status = RPC_UuidGetNodeAddress(address);
395 initialised = 1;
396 }
397
398 /* Generate time element of the UUID. Account for going faster
399 than our clock as well as the clock going backwards. */
400 while (1) {
401 RPC_UuidGetSystemTime(&time);
402 if (time > timelast) {
403 count = 0;
404 break;
405 }
406 if (time < timelast) {
407 sequence = (sequence + 1) & 0x1fff;
408 count = 0;
409 break;
410 }
411 if (count < TICKS_PER_CLOCK_TICK) {
412 count++;
413 break;
414 }
415 }
416
417 timelast = time;
418 time += count;
419
420 /* Pack the information into the UUID structure. */
421
422 Uuid->Data1 = (unsigned long)(time & 0xffffffff);
423 Uuid->Data2 = (unsigned short)((time >> 32) & 0xffff);
424 Uuid->Data3 = (unsigned short)((time >> 48) & 0x0fff);
425
426 /* This is a version 1 UUID */
427 Uuid->Data3 |= (1 << 12);
428
429 Uuid->Data4[0] = sequence & 0xff;
430 Uuid->Data4[1] = (sequence & 0x3f00) >> 8;
431 Uuid->Data4[1] |= 0x80;
432
433 Uuid->Data4[2] = address[0];
434 Uuid->Data4[3] = address[1];
435 Uuid->Data4[4] = address[2];
436 Uuid->Data4[5] = address[3];
437 Uuid->Data4[6] = address[4];
438 Uuid->Data4[7] = address[5];
439
440 LeaveCriticalSection(&uuid_cs);
441
442 TRACE("%s\n", debugstr_guid(Uuid));
443
444 return status;
445 }
446
447 /*************************************************************************
448 * UuidCreateSequential [RPCRT4.@]
449 *
450 * Creates a 128bit UUID.
451 *
452 * RETURNS
453 *
454 * RPC_S_OK if successful.
455 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
456 *
457 */
458 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
459 {
460 return UuidCreate(Uuid);
461 }
462
463
464 /*************************************************************************
465 * UuidHash [RPCRT4.@]
466 *
467 * Generates a hash value for a given UUID
468 *
469 * Code based on FreeDCE implementation
470 *
471 */
472 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
473 {
474 BYTE *data = (BYTE*)uuid;
475 short c0 = 0, c1 = 0, x, y;
476 unsigned int i;
477
478 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
479
480 TRACE("(%s)\n", debugstr_guid(uuid));
481
482 for (i=0; i<sizeof(UUID); i++) {
483 c0 += data[i];
484 c1 += c0;
485 }
486
487 x = -c1 % 255;
488 if (x < 0) x += 255;
489
490 y = (c1 - c0) % 255;
491 if (y < 0) y += 255;
492
493 *Status = RPC_S_OK;
494 return y*256 + x;
495 }
496
497 /*************************************************************************
498 * UuidToStringA [RPCRT4.@]
499 *
500 * Converts a UUID to a string.
501 *
502 * UUID format is 8 hex digits, followed by a hyphen then three groups of
503 * 4 hex digits each followed by a hyphen and then 12 hex digits
504 *
505 * RETURNS
506 *
507 * S_OK if successful.
508 * S_OUT_OF_MEMORY if unsucessful.
509 */
510 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, unsigned char** StringUuid)
511 {
512 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
513
514 if(!(*StringUuid))
515 return RPC_S_OUT_OF_MEMORY;
516
517 if (!Uuid) Uuid = &uuid_nil;
518
519 sprintf(*StringUuid, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
520 Uuid->Data1, Uuid->Data2, Uuid->Data3,
521 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
522 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
523 Uuid->Data4[6], Uuid->Data4[7] );
524
525 return RPC_S_OK;
526 }
527
528 /*************************************************************************
529 * UuidToStringW [RPCRT4.@]
530 *
531 * Converts a UUID to a string.
532 *
533 * S_OK if successful.
534 * S_OUT_OF_MEMORY if unsucessful.
535 */
536 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, unsigned short** StringUuid)
537 {
538 char buf[37];
539
540 if (!Uuid) Uuid = &uuid_nil;
541
542 sprintf(buf, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
543 Uuid->Data1, Uuid->Data2, Uuid->Data3,
544 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
545 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
546 Uuid->Data4[6], Uuid->Data4[7] );
547
548 *StringUuid = RPCRT4_strdupAtoW(buf);
549
550 if(!(*StringUuid))
551 return RPC_S_OUT_OF_MEMORY;
552
553 return RPC_S_OK;
554 }
555
556 static const BYTE hex2bin[] =
557 {
558 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
559 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
560 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
561 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
562 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
563 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
564 0,10,11,12,13,14,15 /* 0x60 */
565 };
566
567 /***********************************************************************
568 * UuidFromStringA (RPCRT4.@)
569 */
570 RPC_STATUS WINAPI UuidFromStringA(unsigned char* str, UUID *uuid)
571 {
572 BYTE *s = (BYTE *)str;
573 int i;
574
575 if (!s) return UuidCreateNil( uuid );
576
577 if (strlen(s) != 36) return RPC_S_INVALID_STRING_UUID;
578
579 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
580 return RPC_S_INVALID_STRING_UUID;
581
582 for (i=0; i<36; i++)
583 {
584 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
585 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
586 }
587
588 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
589
590 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
591 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
592 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
593 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
594
595 /* these are just sequential bytes */
596 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
597 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
598 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
599 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
600 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
601 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
602 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
603 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
604 return RPC_S_OK;
605 }
606
607
608 /***********************************************************************
609 * UuidFromStringW (RPCRT4.@)
610 */
611 RPC_STATUS WINAPI UuidFromStringW(unsigned short* s, UUID *uuid)
612 {
613 int i;
614
615 if (!s) return UuidCreateNil( uuid );
616
617 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
618
619 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
620 return RPC_S_INVALID_STRING_UUID;
621
622 for (i=0; i<36; i++)
623 {
624 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
625 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
626 }
627
628 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
629
630 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
631 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
632 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
633 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
634
635 /* these are just sequential bytes */
636 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
637 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
638 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
639 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
640 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
641 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
642 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
643 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
644 return RPC_S_OK;
645 }
646
647 /***********************************************************************
648 * DllRegisterServer (RPCRT4.@)
649 */
650
651 HRESULT WINAPI RPCRT4_DllRegisterServer( void )
652 {
653 FIXME( "(): stub\n" );
654 return S_OK;
655 }
656
657 BOOL RPCRT4_StartRPCSS(void)
658 {
659 PROCESS_INFORMATION pi;
660 STARTUPINFOA si;
661 static char cmd[6];
662 BOOL rslt;
663
664 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
665 ZeroMemory(&si, sizeof(STARTUPINFOA));
666 si.cb = sizeof(STARTUPINFOA);
667
668 /* apparently it's not OK to use a constant string below */
669 CopyMemory(cmd, "rpcss", 6);
670
671 /* FIXME: will this do the right thing when run as a test? */
672 rslt = CreateProcessA(
673 NULL, /* executable */
674 cmd, /* command line */
675 NULL, /* process security attributes */
676 NULL, /* primary thread security attributes */
677 FALSE, /* inherit handles */
678 0, /* creation flags */
679 NULL, /* use parent's environment */
680 NULL, /* use parent's current directory */
681 &si, /* STARTUPINFO pointer */
682 &pi /* PROCESS_INFORMATION */
683 );
684
685 if (rslt) {
686 CloseHandle(pi.hProcess);
687 CloseHandle(pi.hThread);
688 }
689
690 return rslt;
691 }
692
693 /***********************************************************************
694 * RPCRT4_RPCSSOnDemandCall (internal)
695 *
696 * Attempts to send a message to the RPCSS process
697 * on the local machine, invoking it if necessary.
698 * For remote RPCSS calls, use.... your imagination.
699 *
700 * PARAMS
701 * msg [I] pointer to the RPCSS message
702 * vardata_payload [I] pointer vardata portion of the RPCSS message
703 * reply [O] pointer to reply structure
704 *
705 * RETURNS
706 * TRUE if successful
707 * FALSE otherwise
708 */
709 BOOL RPCRT4_RPCSSOnDemandCall(PRPCSS_NP_MESSAGE msg, char *vardata_payload, PRPCSS_NP_REPLY reply)
710 {
711 HANDLE client_handle;
712 int i, j = 0;
713
714 TRACE("(msg == %p, vardata_payload == %p, reply == %p)\n", msg, vardata_payload, reply);
715
716 client_handle = RPCRT4_RpcssNPConnect();
717
718 while (!client_handle) {
719 /* start the RPCSS process */
720 if (!RPCRT4_StartRPCSS()) {
721 ERR("Unable to start RPCSS process.\n");
722 return FALSE;
723 }
724 /* wait for a connection (w/ periodic polling) */
725 for (i = 0; i < 60; i++) {
726 Sleep(200);
727 client_handle = RPCRT4_RpcssNPConnect();
728 if (client_handle) break;
729 }
730 /* we are only willing to try twice */
731 if (j++ >= 1) break;
732 }
733
734 if (!client_handle) {
735 /* no dice! */
736 ERR("Unable to connect to RPCSS process!\n");
737 SetLastError(RPC_E_SERVER_DIED_DNE);
738 return FALSE;
739 }
740
741 /* great, we're connected. now send the message */
742 if (!RPCRT4_SendReceiveNPMsg(client_handle, msg, vardata_payload, reply)) {
743 ERR("Something is amiss: RPC_SendReceive failed.\n");
744 return FALSE;
745 }
746
747 return TRUE;
748 }
749
750 /* DceErrorInqText
751 *
752 * Notes
753 * 1. On passing a NULL pointer the code does bomb out.
754 * 2. The size of the required buffer is not defined in the documentation.
755 * It appears to be 256.
756 * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
757 * of any value for which it does.
758 * 4. The MSDN documentation currently declares that the second argument is
759 * unsigned char *, even for the W version. I don't believe it.
760 */
761
762 #define MAX_RPC_ERROR_TEXT 256
763
764 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, unsigned short *buffer)
765 {
766 DWORD count;
767 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
768 FORMAT_MESSAGE_IGNORE_INSERTS,
769 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
770 if (!count)
771 {
772 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
773 FORMAT_MESSAGE_IGNORE_INSERTS,
774 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
775 if (!count)
776 {
777 ERR ("Failed to translate error");
778 return RPC_S_INVALID_ARG;
779 }
780 }
781 return RPC_S_OK;
782 }
783
784 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, unsigned char *buffer)
785 {
786 RPC_STATUS status;
787 WCHAR bufferW [MAX_RPC_ERROR_TEXT];
788 if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
789 {
790 if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_RPC_ERROR_TEXT,
791 NULL, NULL))
792 {
793 ERR ("Failed to translate error");
794 status = RPC_S_INVALID_ARG;
795 }
796 }
797 return status;
798 }