[RPCRT4]
[reactos.git] / reactos / dll / win32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * WINE RPC TODO's (and a few TODONT's)
21 *
22 * - Statistics: we are supposed to be keeping various counters. we aren't.
23 *
24 * - Async RPC: Unimplemented.
25 *
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.
29 */
30
31 #include "precomp.h"
32
33 #include <ntsecapi.h>
34 #include <iphlpapi.h>
35
36 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
37
38 static UUID uuid_nil;
39
40 static CRITICAL_SECTION uuid_cs;
41 static CRITICAL_SECTION_DEBUG critsect_debug =
42 {
43 0, 0, &uuid_cs,
44 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
45 0, 0, { (DWORD_PTR)(__FILE__ ": uuid_cs") }
46 };
47 static CRITICAL_SECTION uuid_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
48
49 static CRITICAL_SECTION threaddata_cs;
50 static CRITICAL_SECTION_DEBUG threaddata_cs_debug =
51 {
52 0, 0, &threaddata_cs,
53 { &threaddata_cs_debug.ProcessLocksList, &threaddata_cs_debug.ProcessLocksList },
54 0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
55 };
56 static CRITICAL_SECTION threaddata_cs = { &threaddata_cs_debug, -1, 0, 0, 0, 0 };
57
58 static struct list threaddata_list = LIST_INIT(threaddata_list);
59
60 struct context_handle_list
61 {
62 struct context_handle_list *next;
63 NDR_SCONTEXT context_handle;
64 };
65
66 struct threaddata
67 {
68 struct list entry;
69 CRITICAL_SECTION cs;
70 DWORD thread_id;
71 RpcConnection *connection;
72 RpcBinding *server_binding;
73 struct context_handle_list *context_handle_list;
74 };
75
76 /***********************************************************************
77 * DllMain
78 *
79 * PARAMS
80 * hinstDLL [I] handle to the DLL's instance
81 * fdwReason [I]
82 * lpvReserved [I] reserved, must be NULL
83 *
84 * RETURNS
85 * Success: TRUE
86 * Failure: FALSE
87 */
88
89 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
90 {
91 struct threaddata *tdata;
92
93 switch (fdwReason) {
94 case DLL_PROCESS_ATTACH:
95 break;
96
97 case DLL_THREAD_DETACH:
98 tdata = NtCurrentTeb()->ReservedForNtRpc;
99 if (tdata)
100 {
101 EnterCriticalSection(&threaddata_cs);
102 list_remove(&tdata->entry);
103 LeaveCriticalSection(&threaddata_cs);
104
105 tdata->cs.DebugInfo->Spare[0] = 0;
106 DeleteCriticalSection(&tdata->cs);
107 if (tdata->connection)
108 ERR("tdata->connection should be NULL but is still set to %p\n", tdata->connection);
109 if (tdata->server_binding)
110 ERR("tdata->server_binding should be NULL but is still set to %p\n", tdata->server_binding);
111 HeapFree(GetProcessHeap(), 0, tdata);
112 }
113 break;
114
115 case DLL_PROCESS_DETACH:
116 if (lpvReserved) break; /* do nothing if process is shutting down */
117 RPCRT4_destroy_all_protseqs();
118 RPCRT4_ServerFreeAllRegisteredAuthInfo();
119 DeleteCriticalSection(&uuid_cs);
120 DeleteCriticalSection(&threaddata_cs);
121 break;
122 }
123
124 return TRUE;
125 }
126
127 /*************************************************************************
128 * RpcStringFreeA [RPCRT4.@]
129 *
130 * Frees a character string allocated by the RPC run-time library.
131 *
132 * RETURNS
133 *
134 * S_OK if successful.
135 */
136 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
137 {
138 HeapFree( GetProcessHeap(), 0, *String);
139
140 return RPC_S_OK;
141 }
142
143 /*************************************************************************
144 * RpcStringFreeW [RPCRT4.@]
145 *
146 * Frees a character string allocated by the RPC run-time library.
147 *
148 * RETURNS
149 *
150 * S_OK if successful.
151 */
152 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
153 {
154 HeapFree( GetProcessHeap(), 0, *String);
155
156 return RPC_S_OK;
157 }
158
159 /*************************************************************************
160 * RpcRaiseException [RPCRT4.@]
161 *
162 * Raises an exception.
163 */
164 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
165 {
166 /* shouldn't return */
167 RaiseException(exception, 0, 0, NULL);
168 ERR("handler continued execution\n");
169 ExitProcess(1);
170 }
171
172 /*************************************************************************
173 * UuidCompare [RPCRT4.@]
174 *
175 * PARAMS
176 * UUID *Uuid1 [I] Uuid to compare
177 * UUID *Uuid2 [I] Uuid to compare
178 * RPC_STATUS *Status [O] returns RPC_S_OK
179 *
180 * RETURNS
181 * -1 if Uuid1 is less than Uuid2
182 * 0 if Uuid1 and Uuid2 are equal
183 * 1 if Uuid1 is greater than Uuid2
184 */
185 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
186 {
187 int i;
188
189 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
190
191 *Status = RPC_S_OK;
192
193 if (!Uuid1) Uuid1 = &uuid_nil;
194 if (!Uuid2) Uuid2 = &uuid_nil;
195
196 if (Uuid1 == Uuid2) return 0;
197
198 if (Uuid1->Data1 != Uuid2->Data1)
199 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
200
201 if (Uuid1->Data2 != Uuid2->Data2)
202 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
203
204 if (Uuid1->Data3 != Uuid2->Data3)
205 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
206
207 for (i = 0; i < 8; i++) {
208 if (Uuid1->Data4[i] < Uuid2->Data4[i])
209 return -1;
210 if (Uuid1->Data4[i] > Uuid2->Data4[i])
211 return 1;
212 }
213
214 return 0;
215 }
216
217 /*************************************************************************
218 * UuidEqual [RPCRT4.@]
219 *
220 * PARAMS
221 * UUID *Uuid1 [I] Uuid to compare
222 * UUID *Uuid2 [I] Uuid to compare
223 * RPC_STATUS *Status [O] returns RPC_S_OK
224 *
225 * RETURNS
226 * TRUE/FALSE
227 */
228 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
229 {
230 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
231 return !UuidCompare(Uuid1, Uuid2, Status);
232 }
233
234 /*************************************************************************
235 * UuidIsNil [RPCRT4.@]
236 *
237 * PARAMS
238 * UUID *Uuid [I] Uuid to compare
239 * RPC_STATUS *Status [O] returns RPC_S_OK
240 *
241 * RETURNS
242 * TRUE/FALSE
243 */
244 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
245 {
246 TRACE("(%s)\n", debugstr_guid(Uuid));
247 if (!Uuid) return TRUE;
248 return !UuidCompare(Uuid, &uuid_nil, Status);
249 }
250
251 /*************************************************************************
252 * UuidCreateNil [RPCRT4.@]
253 *
254 * PARAMS
255 * UUID *Uuid [O] returns a nil UUID
256 *
257 * RETURNS
258 * RPC_S_OK
259 */
260 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
261 {
262 *Uuid = uuid_nil;
263 return RPC_S_OK;
264 }
265
266 /*************************************************************************
267 * UuidCreate [RPCRT4.@]
268 *
269 * Creates a 128bit UUID.
270 *
271 * RETURNS
272 *
273 * RPC_S_OK if successful.
274 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
275 *
276 * NOTES
277 *
278 * Follows RFC 4122, section 4.4 (Algorithms for Creating a UUID from
279 * Truly Random or Pseudo-Random Numbers)
280 */
281 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
282 {
283 RtlGenRandom(Uuid, sizeof(*Uuid));
284 /* Clear the version bits and set the version (4) */
285 Uuid->Data3 &= 0x0fff;
286 Uuid->Data3 |= (4 << 12);
287 /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
288 * specified in RFC 4122, section 4.4.
289 */
290 Uuid->Data4[0] &= 0x3f;
291 Uuid->Data4[0] |= 0x80;
292
293 TRACE("%s\n", debugstr_guid(Uuid));
294
295 return RPC_S_OK;
296 }
297
298 /* Number of 100ns ticks per clock tick. To be safe, assume that the clock
299 resolution is at least 1000 * 100 * (1/1000000) = 1/10 of a second */
300 #define TICKS_PER_CLOCK_TICK 1000
301 #define SECSPERDAY 86400
302 #define TICKSPERSEC 10000000
303 /* UUID system time starts at October 15, 1582 */
304 #define SECS_15_OCT_1582_TO_1601 ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)
305 #define TICKS_15_OCT_1582_TO_1601 ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)
306
307 static void RPC_UuidGetSystemTime(ULONGLONG *time)
308 {
309 FILETIME ft;
310
311 GetSystemTimeAsFileTime(&ft);
312
313 *time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
314 *time += TICKS_15_OCT_1582_TO_1601;
315 }
316
317 /* Assume that a hardware address is at least 6 bytes long */
318 #define ADDRESS_BYTES_NEEDED 6
319
320 static RPC_STATUS RPC_UuidGetNodeAddress(BYTE *address)
321 {
322 int i;
323 DWORD status = RPC_S_OK;
324
325 ULONG buflen = sizeof(IP_ADAPTER_INFO);
326 PIP_ADAPTER_INFO adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
327
328 if (GetAdaptersInfo(adapter, &buflen) == ERROR_BUFFER_OVERFLOW) {
329 HeapFree(GetProcessHeap(), 0, adapter);
330 adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
331 }
332
333 if (GetAdaptersInfo(adapter, &buflen) == NO_ERROR) {
334 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
335 address[i] = adapter->Address[i];
336 }
337 }
338 /* We can't get a hardware address, just use random numbers.
339 Set the multicast bit to prevent conflicts with real cards. */
340 else {
341 RtlGenRandom(address, ADDRESS_BYTES_NEEDED);
342 address[0] |= 0x01;
343 status = RPC_S_UUID_LOCAL_ONLY;
344 }
345
346 HeapFree(GetProcessHeap(), 0, adapter);
347 return status;
348 }
349
350 /*************************************************************************
351 * UuidCreateSequential [RPCRT4.@]
352 *
353 * Creates a 128bit UUID.
354 *
355 * RETURNS
356 *
357 * RPC_S_OK if successful.
358 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
359 *
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.
365 */
366 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
367 {
368 static BOOL initialised;
369 static int count;
370
371 ULONGLONG time;
372 static ULONGLONG timelast;
373 static WORD sequence;
374
375 static DWORD status;
376 static BYTE address[MAX_ADAPTER_ADDRESS_LENGTH];
377
378 EnterCriticalSection(&uuid_cs);
379
380 if (!initialised) {
381 RPC_UuidGetSystemTime(&timelast);
382 count = TICKS_PER_CLOCK_TICK;
383
384 sequence = ((rand() & 0xff) << 8) + (rand() & 0xff);
385 sequence &= 0x1fff;
386
387 status = RPC_UuidGetNodeAddress(address);
388 initialised = TRUE;
389 }
390
391 /* Generate time element of the UUID. Account for going faster
392 than our clock as well as the clock going backwards. */
393 while (1) {
394 RPC_UuidGetSystemTime(&time);
395 if (time > timelast) {
396 count = 0;
397 break;
398 }
399 if (time < timelast) {
400 sequence = (sequence + 1) & 0x1fff;
401 count = 0;
402 break;
403 }
404 if (count < TICKS_PER_CLOCK_TICK) {
405 count++;
406 break;
407 }
408 }
409
410 timelast = time;
411 time += count;
412
413 /* Pack the information into the UUID structure. */
414
415 Uuid->Data1 = (ULONG)(time & 0xffffffff);
416 Uuid->Data2 = (unsigned short)((time >> 32) & 0xffff);
417 Uuid->Data3 = (unsigned short)((time >> 48) & 0x0fff);
418
419 /* This is a version 1 UUID */
420 Uuid->Data3 |= (1 << 12);
421
422 Uuid->Data4[0] = sequence & 0xff;
423 Uuid->Data4[1] = (sequence & 0x3f00) >> 8;
424 Uuid->Data4[1] |= 0x80;
425 memcpy(&Uuid->Data4[2], address, ADDRESS_BYTES_NEEDED);
426
427 LeaveCriticalSection(&uuid_cs);
428
429 TRACE("%s\n", debugstr_guid(Uuid));
430
431 return status;
432 }
433
434
435 /*************************************************************************
436 * UuidHash [RPCRT4.@]
437 *
438 * Generates a hash value for a given UUID
439 *
440 * Code based on FreeDCE implementation
441 *
442 */
443 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
444 {
445 BYTE *data = (BYTE*)uuid;
446 short c0 = 0, c1 = 0, x, y;
447 unsigned int i;
448
449 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
450
451 TRACE("(%s)\n", debugstr_guid(uuid));
452
453 for (i=0; i<sizeof(UUID); i++) {
454 c0 += data[i];
455 c1 += c0;
456 }
457
458 x = -c1 % 255;
459 if (x < 0) x += 255;
460
461 y = (c1 - c0) % 255;
462 if (y < 0) y += 255;
463
464 *Status = RPC_S_OK;
465 return y*256 + x;
466 }
467
468 /*************************************************************************
469 * UuidToStringA [RPCRT4.@]
470 *
471 * Converts a UUID to a string.
472 *
473 * UUID format is 8 hex digits, followed by a hyphen then three groups of
474 * 4 hex digits each followed by a hyphen and then 12 hex digits
475 *
476 * RETURNS
477 *
478 * S_OK if successful.
479 * S_OUT_OF_MEMORY if unsuccessful.
480 */
481 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
482 {
483 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
484
485 if(!(*StringUuid))
486 return RPC_S_OUT_OF_MEMORY;
487
488 if (!Uuid) Uuid = &uuid_nil;
489
490 sprintf( (char*)*StringUuid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
491 Uuid->Data1, Uuid->Data2, Uuid->Data3,
492 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
493 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
494 Uuid->Data4[6], Uuid->Data4[7] );
495
496 return RPC_S_OK;
497 }
498
499 /*************************************************************************
500 * UuidToStringW [RPCRT4.@]
501 *
502 * Converts a UUID to a string.
503 *
504 * S_OK if successful.
505 * S_OUT_OF_MEMORY if unsuccessful.
506 */
507 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
508 {
509 char buf[37];
510
511 if (!Uuid) Uuid = &uuid_nil;
512
513 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
514 Uuid->Data1, Uuid->Data2, Uuid->Data3,
515 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
516 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
517 Uuid->Data4[6], Uuid->Data4[7] );
518
519 *StringUuid = RPCRT4_strdupAtoW(buf);
520
521 if(!(*StringUuid))
522 return RPC_S_OUT_OF_MEMORY;
523
524 return RPC_S_OK;
525 }
526
527 static const BYTE hex2bin[] =
528 {
529 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
530 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
531 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
532 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
533 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
534 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
535 0,10,11,12,13,14,15 /* 0x60 */
536 };
537
538 /***********************************************************************
539 * UuidFromStringA (RPCRT4.@)
540 */
541 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
542 {
543 int i;
544
545 if (!s) return UuidCreateNil( uuid );
546
547 if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
548
549 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
550 return RPC_S_INVALID_STRING_UUID;
551
552 for (i=0; i<36; i++)
553 {
554 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
555 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
556 }
557
558 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
559
560 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
561 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
562 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
563 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
564
565 /* these are just sequential bytes */
566 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
567 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
568 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
569 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
570 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
571 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
572 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
573 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
574 return RPC_S_OK;
575 }
576
577
578 /***********************************************************************
579 * UuidFromStringW (RPCRT4.@)
580 */
581 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
582 {
583 int i;
584
585 if (!s) return UuidCreateNil( uuid );
586
587 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
588
589 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
590 return RPC_S_INVALID_STRING_UUID;
591
592 for (i=0; i<36; i++)
593 {
594 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
595 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
596 }
597
598 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
599
600 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
601 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
602 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
603 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
604
605 /* these are just sequential bytes */
606 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
607 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
608 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
609 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
610 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
611 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
612 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
613 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
614 return RPC_S_OK;
615 }
616
617 /***********************************************************************
618 * DllRegisterServer (RPCRT4.@)
619 */
620
621 HRESULT WINAPI DllRegisterServer( void )
622 {
623 FIXME( "(): stub\n" );
624 return S_OK;
625 }
626
627 #define MAX_RPC_ERROR_TEXT 256
628
629 /******************************************************************************
630 * DceErrorInqTextW (rpcrt4.@)
631 *
632 * Notes
633 * 1. On passing a NULL pointer the code does bomb out.
634 * 2. The size of the required buffer is not defined in the documentation.
635 * It appears to be 256.
636 * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
637 * of any value for which it does.
638 * 4. The MSDN documentation currently declares that the second argument is
639 * unsigned char *, even for the W version. I don't believe it.
640 */
641 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
642 {
643 DWORD count;
644 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
645 FORMAT_MESSAGE_IGNORE_INSERTS,
646 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
647 if (!count)
648 {
649 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
650 FORMAT_MESSAGE_IGNORE_INSERTS,
651 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
652 if (!count)
653 {
654 ERR ("Failed to translate error\n");
655 return RPC_S_INVALID_ARG;
656 }
657 }
658 return RPC_S_OK;
659 }
660
661 /******************************************************************************
662 * DceErrorInqTextA (rpcrt4.@)
663 */
664 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
665 {
666 RPC_STATUS status;
667 WCHAR bufferW [MAX_RPC_ERROR_TEXT];
668 if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
669 {
670 if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
671 NULL, NULL))
672 {
673 ERR ("Failed to translate error\n");
674 status = RPC_S_INVALID_ARG;
675 }
676 }
677 return status;
678 }
679
680 /******************************************************************************
681 * I_RpcAllocate (rpcrt4.@)
682 */
683 void * WINAPI I_RpcAllocate(unsigned int Size)
684 {
685 return HeapAlloc(GetProcessHeap(), 0, Size);
686 }
687
688 /******************************************************************************
689 * I_RpcFree (rpcrt4.@)
690 */
691 void WINAPI I_RpcFree(void *Object)
692 {
693 HeapFree(GetProcessHeap(), 0, Object);
694 }
695
696 /******************************************************************************
697 * I_RpcMapWin32Status (rpcrt4.@)
698 *
699 * Maps Win32 RPC error codes to NT statuses.
700 *
701 * PARAMS
702 * status [I] Win32 RPC error code.
703 *
704 * RETURNS
705 * Appropriate translation into an NT status code.
706 */
707 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
708 {
709 TRACE("(%d)\n", status);
710 switch (status)
711 {
712 case ERROR_ACCESS_DENIED: return STATUS_ACCESS_DENIED;
713 case ERROR_INVALID_HANDLE: return RPC_NT_SS_CONTEXT_MISMATCH;
714 case ERROR_OUTOFMEMORY: return STATUS_NO_MEMORY;
715 case ERROR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
716 case ERROR_INSUFFICIENT_BUFFER: return STATUS_BUFFER_TOO_SMALL;
717 case ERROR_MAX_THRDS_REACHED: return STATUS_NO_MEMORY;
718 case ERROR_NOACCESS: return STATUS_ACCESS_VIOLATION;
719 case ERROR_NOT_ENOUGH_SERVER_MEMORY: return STATUS_INSUFF_SERVER_RESOURCES;
720 case ERROR_WRONG_PASSWORD: return STATUS_WRONG_PASSWORD;
721 case ERROR_INVALID_LOGON_HOURS: return STATUS_INVALID_LOGON_HOURS;
722 case ERROR_PASSWORD_EXPIRED: return STATUS_PASSWORD_EXPIRED;
723 case ERROR_ACCOUNT_DISABLED: return STATUS_ACCOUNT_DISABLED;
724 case ERROR_INVALID_SECURITY_DESCR: return STATUS_INVALID_SECURITY_DESCR;
725 case RPC_S_INVALID_STRING_BINDING: return RPC_NT_INVALID_STRING_BINDING;
726 case RPC_S_WRONG_KIND_OF_BINDING: return RPC_NT_WRONG_KIND_OF_BINDING;
727 case RPC_S_INVALID_BINDING: return RPC_NT_INVALID_BINDING;
728 case RPC_S_PROTSEQ_NOT_SUPPORTED: return RPC_NT_PROTSEQ_NOT_SUPPORTED;
729 case RPC_S_INVALID_RPC_PROTSEQ: return RPC_NT_INVALID_RPC_PROTSEQ;
730 case RPC_S_INVALID_STRING_UUID: return RPC_NT_INVALID_STRING_UUID;
731 case RPC_S_INVALID_ENDPOINT_FORMAT: return RPC_NT_INVALID_ENDPOINT_FORMAT;
732 case RPC_S_INVALID_NET_ADDR: return RPC_NT_INVALID_NET_ADDR;
733 case RPC_S_NO_ENDPOINT_FOUND: return RPC_NT_NO_ENDPOINT_FOUND;
734 case RPC_S_INVALID_TIMEOUT: return RPC_NT_INVALID_TIMEOUT;
735 case RPC_S_OBJECT_NOT_FOUND: return RPC_NT_OBJECT_NOT_FOUND;
736 case RPC_S_ALREADY_REGISTERED: return RPC_NT_ALREADY_REGISTERED;
737 case RPC_S_TYPE_ALREADY_REGISTERED: return RPC_NT_TYPE_ALREADY_REGISTERED;
738 case RPC_S_ALREADY_LISTENING: return RPC_NT_ALREADY_LISTENING;
739 case RPC_S_NO_PROTSEQS_REGISTERED: return RPC_NT_NO_PROTSEQS_REGISTERED;
740 case RPC_S_NOT_LISTENING: return RPC_NT_NOT_LISTENING;
741 case RPC_S_UNKNOWN_MGR_TYPE: return RPC_NT_UNKNOWN_MGR_TYPE;
742 case RPC_S_UNKNOWN_IF: return RPC_NT_UNKNOWN_IF;
743 case RPC_S_NO_BINDINGS: return RPC_NT_NO_BINDINGS;
744 case RPC_S_NO_PROTSEQS: return RPC_NT_NO_PROTSEQS;
745 case RPC_S_CANT_CREATE_ENDPOINT: return RPC_NT_CANT_CREATE_ENDPOINT;
746 case RPC_S_OUT_OF_RESOURCES: return RPC_NT_OUT_OF_RESOURCES;
747 case RPC_S_SERVER_UNAVAILABLE: return RPC_NT_SERVER_UNAVAILABLE;
748 case RPC_S_SERVER_TOO_BUSY: return RPC_NT_SERVER_TOO_BUSY;
749 case RPC_S_INVALID_NETWORK_OPTIONS: return RPC_NT_INVALID_NETWORK_OPTIONS;
750 case RPC_S_NO_CALL_ACTIVE: return RPC_NT_NO_CALL_ACTIVE;
751 case RPC_S_CALL_FAILED: return RPC_NT_CALL_FAILED;
752 case RPC_S_CALL_FAILED_DNE: return RPC_NT_CALL_FAILED_DNE;
753 case RPC_S_PROTOCOL_ERROR: return RPC_NT_PROTOCOL_ERROR;
754 case RPC_S_UNSUPPORTED_TRANS_SYN: return RPC_NT_UNSUPPORTED_TRANS_SYN;
755 case RPC_S_UNSUPPORTED_TYPE: return RPC_NT_UNSUPPORTED_TYPE;
756 case RPC_S_INVALID_TAG: return RPC_NT_INVALID_TAG;
757 case RPC_S_INVALID_BOUND: return RPC_NT_INVALID_BOUND;
758 case RPC_S_NO_ENTRY_NAME: return RPC_NT_NO_ENTRY_NAME;
759 case RPC_S_INVALID_NAME_SYNTAX: return RPC_NT_INVALID_NAME_SYNTAX;
760 case RPC_S_UNSUPPORTED_NAME_SYNTAX: return RPC_NT_UNSUPPORTED_NAME_SYNTAX;
761 case RPC_S_UUID_NO_ADDRESS: return RPC_NT_UUID_NO_ADDRESS;
762 case RPC_S_DUPLICATE_ENDPOINT: return RPC_NT_DUPLICATE_ENDPOINT;
763 case RPC_S_UNKNOWN_AUTHN_TYPE: return RPC_NT_UNKNOWN_AUTHN_TYPE;
764 case RPC_S_MAX_CALLS_TOO_SMALL: return RPC_NT_MAX_CALLS_TOO_SMALL;
765 case RPC_S_STRING_TOO_LONG: return RPC_NT_STRING_TOO_LONG;
766 case RPC_S_PROTSEQ_NOT_FOUND: return RPC_NT_PROTSEQ_NOT_FOUND;
767 case RPC_S_PROCNUM_OUT_OF_RANGE: return RPC_NT_PROCNUM_OUT_OF_RANGE;
768 case RPC_S_BINDING_HAS_NO_AUTH: return RPC_NT_BINDING_HAS_NO_AUTH;
769 case RPC_S_UNKNOWN_AUTHN_SERVICE: return RPC_NT_UNKNOWN_AUTHN_SERVICE;
770 case RPC_S_UNKNOWN_AUTHN_LEVEL: return RPC_NT_UNKNOWN_AUTHN_LEVEL;
771 case RPC_S_INVALID_AUTH_IDENTITY: return RPC_NT_INVALID_AUTH_IDENTITY;
772 case RPC_S_UNKNOWN_AUTHZ_SERVICE: return RPC_NT_UNKNOWN_AUTHZ_SERVICE;
773 case EPT_S_INVALID_ENTRY: return EPT_NT_INVALID_ENTRY;
774 case EPT_S_CANT_PERFORM_OP: return EPT_NT_CANT_PERFORM_OP;
775 case EPT_S_NOT_REGISTERED: return EPT_NT_NOT_REGISTERED;
776 case EPT_S_CANT_CREATE: return EPT_NT_CANT_CREATE;
777 case RPC_S_NOTHING_TO_EXPORT: return RPC_NT_NOTHING_TO_EXPORT;
778 case RPC_S_INCOMPLETE_NAME: return RPC_NT_INCOMPLETE_NAME;
779 case RPC_S_INVALID_VERS_OPTION: return RPC_NT_INVALID_VERS_OPTION;
780 case RPC_S_NO_MORE_MEMBERS: return RPC_NT_NO_MORE_MEMBERS;
781 case RPC_S_NOT_ALL_OBJS_UNEXPORTED: return RPC_NT_NOT_ALL_OBJS_UNEXPORTED;
782 case RPC_S_INTERFACE_NOT_FOUND: return RPC_NT_INTERFACE_NOT_FOUND;
783 case RPC_S_ENTRY_ALREADY_EXISTS: return RPC_NT_ENTRY_ALREADY_EXISTS;
784 case RPC_S_ENTRY_NOT_FOUND: return RPC_NT_ENTRY_NOT_FOUND;
785 case RPC_S_NAME_SERVICE_UNAVAILABLE: return RPC_NT_NAME_SERVICE_UNAVAILABLE;
786 case RPC_S_INVALID_NAF_ID: return RPC_NT_INVALID_NAF_ID;
787 case RPC_S_CANNOT_SUPPORT: return RPC_NT_CANNOT_SUPPORT;
788 case RPC_S_NO_CONTEXT_AVAILABLE: return RPC_NT_NO_CONTEXT_AVAILABLE;
789 case RPC_S_INTERNAL_ERROR: return RPC_NT_INTERNAL_ERROR;
790 case RPC_S_ZERO_DIVIDE: return RPC_NT_ZERO_DIVIDE;
791 case RPC_S_ADDRESS_ERROR: return RPC_NT_ADDRESS_ERROR;
792 case RPC_S_FP_DIV_ZERO: return RPC_NT_FP_DIV_ZERO;
793 case RPC_S_FP_UNDERFLOW: return RPC_NT_FP_UNDERFLOW;
794 case RPC_S_FP_OVERFLOW: return RPC_NT_FP_OVERFLOW;
795 case RPC_S_CALL_IN_PROGRESS: return RPC_NT_CALL_IN_PROGRESS;
796 case RPC_S_NO_MORE_BINDINGS: return RPC_NT_NO_MORE_BINDINGS;
797 case RPC_S_CALL_CANCELLED: return RPC_NT_CALL_CANCELLED;
798 case RPC_S_INVALID_OBJECT: return RPC_NT_INVALID_OBJECT;
799 case RPC_S_INVALID_ASYNC_HANDLE: return RPC_NT_INVALID_ASYNC_HANDLE;
800 case RPC_S_INVALID_ASYNC_CALL: return RPC_NT_INVALID_ASYNC_CALL;
801 case RPC_S_GROUP_MEMBER_NOT_FOUND: return RPC_NT_GROUP_MEMBER_NOT_FOUND;
802 case RPC_X_NO_MORE_ENTRIES: return RPC_NT_NO_MORE_ENTRIES;
803 case RPC_X_SS_CHAR_TRANS_OPEN_FAIL: return RPC_NT_SS_CHAR_TRANS_OPEN_FAIL;
804 case RPC_X_SS_CHAR_TRANS_SHORT_FILE: return RPC_NT_SS_CHAR_TRANS_SHORT_FILE;
805 case RPC_X_SS_IN_NULL_CONTEXT: return RPC_NT_SS_IN_NULL_CONTEXT;
806 case RPC_X_SS_CONTEXT_DAMAGED: return RPC_NT_SS_CONTEXT_DAMAGED;
807 case RPC_X_SS_HANDLES_MISMATCH: return RPC_NT_SS_HANDLES_MISMATCH;
808 case RPC_X_SS_CANNOT_GET_CALL_HANDLE: return RPC_NT_SS_CANNOT_GET_CALL_HANDLE;
809 case RPC_X_NULL_REF_POINTER: return RPC_NT_NULL_REF_POINTER;
810 case RPC_X_ENUM_VALUE_OUT_OF_RANGE: return RPC_NT_ENUM_VALUE_OUT_OF_RANGE;
811 case RPC_X_BYTE_COUNT_TOO_SMALL: return RPC_NT_BYTE_COUNT_TOO_SMALL;
812 case RPC_X_BAD_STUB_DATA: return RPC_NT_BAD_STUB_DATA;
813 case RPC_X_PIPE_CLOSED: return RPC_NT_PIPE_CLOSED;
814 case RPC_X_PIPE_DISCIPLINE_ERROR: return RPC_NT_PIPE_DISCIPLINE_ERROR;
815 case RPC_X_PIPE_EMPTY: return RPC_NT_PIPE_EMPTY;
816 case ERROR_PASSWORD_MUST_CHANGE: return STATUS_PASSWORD_MUST_CHANGE;
817 case ERROR_ACCOUNT_LOCKED_OUT: return STATUS_ACCOUNT_LOCKED_OUT;
818 default: return status;
819 }
820 }
821
822 /******************************************************************************
823 * I_RpcExceptionFilter (rpcrt4.@)
824 */
825 int WINAPI I_RpcExceptionFilter(ULONG ExceptionCode)
826 {
827 TRACE("0x%x\n", ExceptionCode);
828 switch (ExceptionCode)
829 {
830 case STATUS_DATATYPE_MISALIGNMENT:
831 case STATUS_BREAKPOINT:
832 case STATUS_ACCESS_VIOLATION:
833 case STATUS_ILLEGAL_INSTRUCTION:
834 case STATUS_PRIVILEGED_INSTRUCTION:
835 case STATUS_INSTRUCTION_MISALIGNMENT:
836 case STATUS_STACK_OVERFLOW:
837 case STATUS_POSSIBLE_DEADLOCK:
838 return EXCEPTION_CONTINUE_SEARCH;
839 default:
840 return EXCEPTION_EXECUTE_HANDLER;
841 }
842 }
843
844 /******************************************************************************
845 * RpcErrorStartEnumeration (rpcrt4.@)
846 */
847 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
848 {
849 FIXME("(%p): stub\n", EnumHandle);
850 return RPC_S_ENTRY_NOT_FOUND;
851 }
852
853 /******************************************************************************
854 * RpcErrorEndEnumeration (rpcrt4.@)
855 */
856 RPC_STATUS RPC_ENTRY RpcErrorEndEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
857 {
858 FIXME("(%p): stub\n", EnumHandle);
859 return RPC_S_OK;
860 }
861
862 /******************************************************************************
863 * RpcErrorSaveErrorInfo (rpcrt4.@)
864 */
865 RPC_STATUS RPC_ENTRY RpcErrorSaveErrorInfo(RPC_ERROR_ENUM_HANDLE *EnumHandle, void **ErrorBlob, SIZE_T *BlobSize)
866 {
867 FIXME("(%p %p %p): stub\n", EnumHandle, ErrorBlob, BlobSize);
868 return ERROR_CALL_NOT_IMPLEMENTED;
869 }
870
871 /******************************************************************************
872 * RpcErrorLoadErrorInfo (rpcrt4.@)
873 */
874 RPC_STATUS RPC_ENTRY RpcErrorLoadErrorInfo(void *ErrorBlob, SIZE_T BlobSize, RPC_ERROR_ENUM_HANDLE *EnumHandle)
875 {
876 FIXME("(%p %lu %p): stub\n", ErrorBlob, BlobSize, EnumHandle);
877 return ERROR_CALL_NOT_IMPLEMENTED;
878 }
879
880 /******************************************************************************
881 * RpcErrorGetNextRecord (rpcrt4.@)
882 */
883 RPC_STATUS RPC_ENTRY RpcErrorGetNextRecord(RPC_ERROR_ENUM_HANDLE *EnumHandle, BOOL CopyStrings, RPC_EXTENDED_ERROR_INFO *ErrorInfo)
884 {
885 FIXME("(%p %x %p): stub\n", EnumHandle, CopyStrings, ErrorInfo);
886 return RPC_S_ENTRY_NOT_FOUND;
887 }
888
889 /******************************************************************************
890 * RpcMgmtSetCancelTimeout (rpcrt4.@)
891 */
892 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
893 {
894 FIXME("(%d): stub\n", Timeout);
895 return RPC_S_OK;
896 }
897
898 static struct threaddata *get_or_create_threaddata(void)
899 {
900 struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
901 if (!tdata)
902 {
903 tdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*tdata));
904 if (!tdata) return NULL;
905
906 InitializeCriticalSection(&tdata->cs);
907 tdata->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": threaddata.cs");
908 tdata->thread_id = GetCurrentThreadId();
909
910 EnterCriticalSection(&threaddata_cs);
911 list_add_tail(&threaddata_list, &tdata->entry);
912 LeaveCriticalSection(&threaddata_cs);
913
914 NtCurrentTeb()->ReservedForNtRpc = tdata;
915 return tdata;
916 }
917 return tdata;
918 }
919
920 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
921 {
922 struct threaddata *tdata = get_or_create_threaddata();
923 if (!tdata) return;
924
925 EnterCriticalSection(&tdata->cs);
926 tdata->connection = Connection;
927 LeaveCriticalSection(&tdata->cs);
928 }
929
930 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
931 {
932 struct threaddata *tdata = get_or_create_threaddata();
933 if (!tdata) return;
934
935 tdata->server_binding = Binding;
936 }
937
938 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
939 {
940 struct threaddata *tdata = get_or_create_threaddata();
941 if (!tdata) return NULL;
942
943 return tdata->server_binding;
944 }
945
946 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext)
947 {
948 struct threaddata *tdata = get_or_create_threaddata();
949 struct context_handle_list *context_handle_list;
950
951 if (!tdata) return;
952
953 context_handle_list = HeapAlloc(GetProcessHeap(), 0, sizeof(*context_handle_list));
954 if (!context_handle_list) return;
955
956 context_handle_list->context_handle = SContext;
957 context_handle_list->next = tdata->context_handle_list;
958 tdata->context_handle_list = context_handle_list;
959 }
960
961 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext)
962 {
963 struct threaddata *tdata = get_or_create_threaddata();
964 struct context_handle_list *current, *prev;
965
966 if (!tdata) return;
967
968 for (current = tdata->context_handle_list, prev = NULL; current; prev = current, current = current->next)
969 {
970 if (current->context_handle == SContext)
971 {
972 if (prev)
973 prev->next = current->next;
974 else
975 tdata->context_handle_list = current->next;
976 HeapFree(GetProcessHeap(), 0, current);
977 return;
978 }
979 }
980 }
981
982 NDR_SCONTEXT RPCRT4_PopThreadContextHandle(void)
983 {
984 struct threaddata *tdata = get_or_create_threaddata();
985 struct context_handle_list *context_handle_list;
986 NDR_SCONTEXT context_handle;
987
988 if (!tdata) return NULL;
989
990 context_handle_list = tdata->context_handle_list;
991 if (!context_handle_list) return NULL;
992 tdata->context_handle_list = context_handle_list->next;
993
994 context_handle = context_handle_list->context_handle;
995 HeapFree(GetProcessHeap(), 0, context_handle_list);
996 return context_handle;
997 }
998
999 static RPC_STATUS rpc_cancel_thread(DWORD target_tid)
1000 {
1001 struct threaddata *tdata;
1002
1003 EnterCriticalSection(&threaddata_cs);
1004 LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
1005 if (tdata->thread_id == target_tid)
1006 {
1007 EnterCriticalSection(&tdata->cs);
1008 if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
1009 LeaveCriticalSection(&tdata->cs);
1010 break;
1011 }
1012 LeaveCriticalSection(&threaddata_cs);
1013
1014 return RPC_S_OK;
1015 }
1016
1017 /******************************************************************************
1018 * RpcCancelThread (rpcrt4.@)
1019 */
1020 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
1021 {
1022 TRACE("(%p)\n", ThreadHandle);
1023 return RpcCancelThreadEx(ThreadHandle, 0);
1024 }
1025
1026 /******************************************************************************
1027 * RpcCancelThreadEx (rpcrt4.@)
1028 */
1029 RPC_STATUS RPC_ENTRY RpcCancelThreadEx(void* ThreadHandle, LONG Timeout)
1030 {
1031 DWORD target_tid;
1032
1033 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
1034
1035 target_tid = GetThreadId(ThreadHandle);
1036 if (!target_tid)
1037 return RPC_S_INVALID_ARG;
1038
1039 if (Timeout)
1040 {
1041 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
1042 return RPC_S_OK;
1043 }
1044 else
1045 return rpc_cancel_thread(target_tid);
1046 }