ntdll: Implement DVD_StartSession() on Mac OS.
[wine.git] / dlls / rpcrt4 / rpcrt4_main.c
blob67059e77a6fff74f3aa5f53fda22f8f76f302ca7
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 "ntsecapi.h"
47 #include "wine/unicode.h"
48 #include "rpc.h"
50 #include "ole2.h"
51 #include "rpcndr.h"
52 #include "rpcproxy.h"
54 #include "rpc_binding.h"
55 #include "rpc_server.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
61 static UUID uuid_nil;
63 static CRITICAL_SECTION threaddata_cs;
64 static CRITICAL_SECTION_DEBUG threaddata_cs_debug =
66 0, 0, &threaddata_cs,
67 { &threaddata_cs_debug.ProcessLocksList, &threaddata_cs_debug.ProcessLocksList },
68 0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
70 static CRITICAL_SECTION threaddata_cs = { &threaddata_cs_debug, -1, 0, 0, 0, 0 };
72 static struct list threaddata_list = LIST_INIT(threaddata_list);
74 struct context_handle_list
76 struct context_handle_list *next;
77 NDR_SCONTEXT context_handle;
80 struct threaddata
82 struct list entry;
83 CRITICAL_SECTION cs;
84 DWORD thread_id;
85 RpcConnection *connection;
86 RpcBinding *server_binding;
87 struct context_handle_list *context_handle_list;
90 /***********************************************************************
91 * DllMain
93 * PARAMS
94 * hinstDLL [I] handle to the DLL's instance
95 * fdwReason [I]
96 * lpvReserved [I] reserved, must be NULL
98 * RETURNS
99 * Success: TRUE
100 * Failure: FALSE
103 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
105 struct threaddata *tdata;
107 switch (fdwReason) {
108 case DLL_PROCESS_ATTACH:
109 break;
111 case DLL_THREAD_DETACH:
112 tdata = NtCurrentTeb()->ReservedForNtRpc;
113 if (tdata)
115 EnterCriticalSection(&threaddata_cs);
116 list_remove(&tdata->entry);
117 LeaveCriticalSection(&threaddata_cs);
119 DeleteCriticalSection(&tdata->cs);
120 if (tdata->connection)
121 ERR("tdata->connection should be NULL but is still set to %p\n", tdata->connection);
122 if (tdata->server_binding)
123 ERR("tdata->server_binding should be NULL but is still set to %p\n", tdata->server_binding);
124 HeapFree(GetProcessHeap(), 0, tdata);
126 break;
128 case DLL_PROCESS_DETACH:
129 RPCRT4_destroy_all_protseqs();
130 break;
133 return TRUE;
136 /*************************************************************************
137 * RpcStringFreeA [RPCRT4.@]
139 * Frees a character string allocated by the RPC run-time library.
141 * RETURNS
143 * S_OK if successful.
145 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
147 HeapFree( GetProcessHeap(), 0, *String);
149 return RPC_S_OK;
152 /*************************************************************************
153 * RpcStringFreeW [RPCRT4.@]
155 * Frees a character string allocated by the RPC run-time library.
157 * RETURNS
159 * S_OK if successful.
161 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
163 HeapFree( GetProcessHeap(), 0, *String);
165 return RPC_S_OK;
168 /*************************************************************************
169 * RpcRaiseException [RPCRT4.@]
171 * Raises an exception.
173 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
175 /* shouldn't return */
176 RaiseException(exception, 0, 0, NULL);
177 ERR("handler continued execution\n");
178 ExitProcess(1);
181 /*************************************************************************
182 * UuidCompare [RPCRT4.@]
184 * PARAMS
185 * UUID *Uuid1 [I] Uuid to compare
186 * UUID *Uuid2 [I] Uuid to compare
187 * RPC_STATUS *Status [O] returns RPC_S_OK
189 * RETURNS
190 * -1 if Uuid1 is less than Uuid2
191 * 0 if Uuid1 and Uuid2 are equal
192 * 1 if Uuid1 is greater than Uuid2
194 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
196 int i;
198 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
200 *Status = RPC_S_OK;
202 if (!Uuid1) Uuid1 = &uuid_nil;
203 if (!Uuid2) Uuid2 = &uuid_nil;
205 if (Uuid1 == Uuid2) return 0;
207 if (Uuid1->Data1 != Uuid2->Data1)
208 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
210 if (Uuid1->Data2 != Uuid2->Data2)
211 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
213 if (Uuid1->Data3 != Uuid2->Data3)
214 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
216 for (i = 0; i < 8; i++) {
217 if (Uuid1->Data4[i] < Uuid2->Data4[i])
218 return -1;
219 if (Uuid1->Data4[i] > Uuid2->Data4[i])
220 return 1;
223 return 0;
226 /*************************************************************************
227 * UuidEqual [RPCRT4.@]
229 * PARAMS
230 * UUID *Uuid1 [I] Uuid to compare
231 * UUID *Uuid2 [I] Uuid to compare
232 * RPC_STATUS *Status [O] returns RPC_S_OK
234 * RETURNS
235 * TRUE/FALSE
237 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
239 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
240 return !UuidCompare(Uuid1, Uuid2, Status);
243 /*************************************************************************
244 * UuidIsNil [RPCRT4.@]
246 * PARAMS
247 * UUID *Uuid [I] Uuid to compare
248 * RPC_STATUS *Status [O] returns RPC_S_OK
250 * RETURNS
251 * TRUE/FALSE
253 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
255 TRACE("(%s)\n", debugstr_guid(Uuid));
256 if (!Uuid) return TRUE;
257 return !UuidCompare(Uuid, &uuid_nil, Status);
260 /*************************************************************************
261 * UuidCreateNil [RPCRT4.@]
263 * PARAMS
264 * UUID *Uuid [O] returns a nil UUID
266 * RETURNS
267 * RPC_S_OK
269 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
271 *Uuid = uuid_nil;
272 return RPC_S_OK;
275 /*************************************************************************
276 * UuidCreate [RPCRT4.@]
278 * Creates a 128bit UUID.
280 * RETURNS
282 * RPC_S_OK if successful.
283 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
285 * NOTES
287 * Follows RFC 4122, section 4.4 (Algorithms for Creating a UUID from
288 * Truly Random or Pseudo-Random Numbers)
290 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
292 RtlGenRandom(Uuid, sizeof(*Uuid));
293 /* Clear the version bits and set the version (4) */
294 Uuid->Data3 &= 0x0fff;
295 Uuid->Data3 |= (4 << 12);
296 /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
297 * specified in RFC 4122, section 4.4.
299 Uuid->Data4[0] &= 0x3f;
300 Uuid->Data4[0] |= 0x80;
302 TRACE("%s\n", debugstr_guid(Uuid));
304 return RPC_S_OK;
307 /*************************************************************************
308 * UuidCreateSequential [RPCRT4.@]
310 * Creates a 128bit UUID.
312 * RETURNS
314 * RPC_S_OK if successful.
315 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
318 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
320 return UuidCreate(Uuid);
324 /*************************************************************************
325 * UuidHash [RPCRT4.@]
327 * Generates a hash value for a given UUID
329 * Code based on FreeDCE implementation
332 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
334 BYTE *data = (BYTE*)uuid;
335 short c0 = 0, c1 = 0, x, y;
336 unsigned int i;
338 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
340 TRACE("(%s)\n", debugstr_guid(uuid));
342 for (i=0; i<sizeof(UUID); i++) {
343 c0 += data[i];
344 c1 += c0;
347 x = -c1 % 255;
348 if (x < 0) x += 255;
350 y = (c1 - c0) % 255;
351 if (y < 0) y += 255;
353 *Status = RPC_S_OK;
354 return y*256 + x;
357 /*************************************************************************
358 * UuidToStringA [RPCRT4.@]
360 * Converts a UUID to a string.
362 * UUID format is 8 hex digits, followed by a hyphen then three groups of
363 * 4 hex digits each followed by a hyphen and then 12 hex digits
365 * RETURNS
367 * S_OK if successful.
368 * S_OUT_OF_MEMORY if unsuccessful.
370 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
372 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
374 if(!(*StringUuid))
375 return RPC_S_OUT_OF_MEMORY;
377 if (!Uuid) Uuid = &uuid_nil;
379 sprintf( (char*)*StringUuid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
380 Uuid->Data1, Uuid->Data2, Uuid->Data3,
381 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
382 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
383 Uuid->Data4[6], Uuid->Data4[7] );
385 return RPC_S_OK;
388 /*************************************************************************
389 * UuidToStringW [RPCRT4.@]
391 * Converts a UUID to a string.
393 * S_OK if successful.
394 * S_OUT_OF_MEMORY if unsuccessful.
396 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
398 char buf[37];
400 if (!Uuid) Uuid = &uuid_nil;
402 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
403 Uuid->Data1, Uuid->Data2, Uuid->Data3,
404 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
405 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
406 Uuid->Data4[6], Uuid->Data4[7] );
408 *StringUuid = RPCRT4_strdupAtoW(buf);
410 if(!(*StringUuid))
411 return RPC_S_OUT_OF_MEMORY;
413 return RPC_S_OK;
416 static const BYTE hex2bin[] =
418 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
419 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
420 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
421 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
422 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
423 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
424 0,10,11,12,13,14,15 /* 0x60 */
427 /***********************************************************************
428 * UuidFromStringA (RPCRT4.@)
430 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
432 int i;
434 if (!s) return UuidCreateNil( uuid );
436 if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
438 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
439 return RPC_S_INVALID_STRING_UUID;
441 for (i=0; i<36; i++)
443 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
444 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
447 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
449 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
450 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
451 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
452 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
454 /* these are just sequential bytes */
455 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
456 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
457 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
458 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
459 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
460 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
461 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
462 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
463 return RPC_S_OK;
467 /***********************************************************************
468 * UuidFromStringW (RPCRT4.@)
470 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
472 int i;
474 if (!s) return UuidCreateNil( uuid );
476 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
478 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
479 return RPC_S_INVALID_STRING_UUID;
481 for (i=0; i<36; i++)
483 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
484 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
487 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
489 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
490 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
491 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
492 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
494 /* these are just sequential bytes */
495 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
496 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
497 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
498 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
499 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
500 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
501 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
502 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
503 return RPC_S_OK;
506 /***********************************************************************
507 * DllRegisterServer (RPCRT4.@)
510 HRESULT WINAPI DllRegisterServer( void )
512 FIXME( "(): stub\n" );
513 return S_OK;
516 #define MAX_RPC_ERROR_TEXT 256
518 /******************************************************************************
519 * DceErrorInqTextW (rpcrt4.@)
521 * Notes
522 * 1. On passing a NULL pointer the code does bomb out.
523 * 2. The size of the required buffer is not defined in the documentation.
524 * It appears to be 256.
525 * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
526 * of any value for which it does.
527 * 4. The MSDN documentation currently declares that the second argument is
528 * unsigned char *, even for the W version. I don't believe it.
530 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
532 DWORD count;
533 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
534 FORMAT_MESSAGE_IGNORE_INSERTS,
535 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
536 if (!count)
538 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
539 FORMAT_MESSAGE_IGNORE_INSERTS,
540 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
541 if (!count)
543 ERR ("Failed to translate error\n");
544 return RPC_S_INVALID_ARG;
547 return RPC_S_OK;
550 /******************************************************************************
551 * DceErrorInqTextA (rpcrt4.@)
553 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
555 RPC_STATUS status;
556 WCHAR bufferW [MAX_RPC_ERROR_TEXT];
557 if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
559 if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
560 NULL, NULL))
562 ERR ("Failed to translate error\n");
563 status = RPC_S_INVALID_ARG;
566 return status;
569 /******************************************************************************
570 * I_RpcAllocate (rpcrt4.@)
572 void * WINAPI I_RpcAllocate(unsigned int Size)
574 return HeapAlloc(GetProcessHeap(), 0, Size);
577 /******************************************************************************
578 * I_RpcFree (rpcrt4.@)
580 void WINAPI I_RpcFree(void *Object)
582 HeapFree(GetProcessHeap(), 0, Object);
585 /******************************************************************************
586 * I_RpcMapWin32Status (rpcrt4.@)
588 * Maps Win32 RPC error codes to NT statuses.
590 * PARAMS
591 * status [I] Win32 RPC error code.
593 * RETURNS
594 * Appropriate translation into an NT status code.
596 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
598 TRACE("(%d)\n", status);
599 switch (status)
601 case ERROR_ACCESS_DENIED: return STATUS_ACCESS_DENIED;
602 case ERROR_INVALID_HANDLE: return RPC_NT_SS_CONTEXT_MISMATCH;
603 case ERROR_OUTOFMEMORY: return STATUS_NO_MEMORY;
604 case ERROR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
605 case ERROR_INSUFFICIENT_BUFFER: return STATUS_BUFFER_TOO_SMALL;
606 case ERROR_MAX_THRDS_REACHED: return STATUS_NO_MEMORY;
607 case ERROR_NOACCESS: return STATUS_ACCESS_VIOLATION;
608 case ERROR_NOT_ENOUGH_SERVER_MEMORY: return STATUS_INSUFF_SERVER_RESOURCES;
609 case ERROR_WRONG_PASSWORD: return STATUS_WRONG_PASSWORD;
610 case ERROR_INVALID_LOGON_HOURS: return STATUS_INVALID_LOGON_HOURS;
611 case ERROR_PASSWORD_EXPIRED: return STATUS_PASSWORD_EXPIRED;
612 case ERROR_ACCOUNT_DISABLED: return STATUS_ACCOUNT_DISABLED;
613 case ERROR_INVALID_SECURITY_DESCR: return STATUS_INVALID_SECURITY_DESCR;
614 case RPC_S_INVALID_STRING_BINDING: return RPC_NT_INVALID_STRING_BINDING;
615 case RPC_S_WRONG_KIND_OF_BINDING: return RPC_NT_WRONG_KIND_OF_BINDING;
616 case RPC_S_INVALID_BINDING: return RPC_NT_INVALID_BINDING;
617 case RPC_S_PROTSEQ_NOT_SUPPORTED: return RPC_NT_PROTSEQ_NOT_SUPPORTED;
618 case RPC_S_INVALID_RPC_PROTSEQ: return RPC_NT_INVALID_RPC_PROTSEQ;
619 case RPC_S_INVALID_STRING_UUID: return RPC_NT_INVALID_STRING_UUID;
620 case RPC_S_INVALID_ENDPOINT_FORMAT: return RPC_NT_INVALID_ENDPOINT_FORMAT;
621 case RPC_S_INVALID_NET_ADDR: return RPC_NT_INVALID_NET_ADDR;
622 case RPC_S_NO_ENDPOINT_FOUND: return RPC_NT_NO_ENDPOINT_FOUND;
623 case RPC_S_INVALID_TIMEOUT: return RPC_NT_INVALID_TIMEOUT;
624 case RPC_S_OBJECT_NOT_FOUND: return RPC_NT_OBJECT_NOT_FOUND;
625 case RPC_S_ALREADY_REGISTERED: return RPC_NT_ALREADY_REGISTERED;
626 case RPC_S_TYPE_ALREADY_REGISTERED: return RPC_NT_TYPE_ALREADY_REGISTERED;
627 case RPC_S_ALREADY_LISTENING: return RPC_NT_ALREADY_LISTENING;
628 case RPC_S_NO_PROTSEQS_REGISTERED: return RPC_NT_NO_PROTSEQS_REGISTERED;
629 case RPC_S_NOT_LISTENING: return RPC_NT_NOT_LISTENING;
630 case RPC_S_UNKNOWN_MGR_TYPE: return RPC_NT_UNKNOWN_MGR_TYPE;
631 case RPC_S_UNKNOWN_IF: return RPC_NT_UNKNOWN_IF;
632 case RPC_S_NO_BINDINGS: return RPC_NT_NO_BINDINGS;
633 case RPC_S_NO_PROTSEQS: return RPC_NT_NO_PROTSEQS;
634 case RPC_S_CANT_CREATE_ENDPOINT: return RPC_NT_CANT_CREATE_ENDPOINT;
635 case RPC_S_OUT_OF_RESOURCES: return RPC_NT_OUT_OF_RESOURCES;
636 case RPC_S_SERVER_UNAVAILABLE: return RPC_NT_SERVER_UNAVAILABLE;
637 case RPC_S_SERVER_TOO_BUSY: return RPC_NT_SERVER_TOO_BUSY;
638 case RPC_S_INVALID_NETWORK_OPTIONS: return RPC_NT_INVALID_NETWORK_OPTIONS;
639 case RPC_S_NO_CALL_ACTIVE: return RPC_NT_NO_CALL_ACTIVE;
640 case RPC_S_CALL_FAILED: return RPC_NT_CALL_FAILED;
641 case RPC_S_CALL_FAILED_DNE: return RPC_NT_CALL_FAILED_DNE;
642 case RPC_S_PROTOCOL_ERROR: return RPC_NT_PROTOCOL_ERROR;
643 case RPC_S_UNSUPPORTED_TRANS_SYN: return RPC_NT_UNSUPPORTED_TRANS_SYN;
644 case RPC_S_UNSUPPORTED_TYPE: return RPC_NT_UNSUPPORTED_TYPE;
645 case RPC_S_INVALID_TAG: return RPC_NT_INVALID_TAG;
646 case RPC_S_INVALID_BOUND: return RPC_NT_INVALID_BOUND;
647 case RPC_S_NO_ENTRY_NAME: return RPC_NT_NO_ENTRY_NAME;
648 case RPC_S_INVALID_NAME_SYNTAX: return RPC_NT_INVALID_NAME_SYNTAX;
649 case RPC_S_UNSUPPORTED_NAME_SYNTAX: return RPC_NT_UNSUPPORTED_NAME_SYNTAX;
650 case RPC_S_UUID_NO_ADDRESS: return RPC_NT_UUID_NO_ADDRESS;
651 case RPC_S_DUPLICATE_ENDPOINT: return RPC_NT_DUPLICATE_ENDPOINT;
652 case RPC_S_UNKNOWN_AUTHN_TYPE: return RPC_NT_UNKNOWN_AUTHN_TYPE;
653 case RPC_S_MAX_CALLS_TOO_SMALL: return RPC_NT_MAX_CALLS_TOO_SMALL;
654 case RPC_S_STRING_TOO_LONG: return RPC_NT_STRING_TOO_LONG;
655 case RPC_S_PROTSEQ_NOT_FOUND: return RPC_NT_PROTSEQ_NOT_FOUND;
656 case RPC_S_PROCNUM_OUT_OF_RANGE: return RPC_NT_PROCNUM_OUT_OF_RANGE;
657 case RPC_S_BINDING_HAS_NO_AUTH: return RPC_NT_BINDING_HAS_NO_AUTH;
658 case RPC_S_UNKNOWN_AUTHN_SERVICE: return RPC_NT_UNKNOWN_AUTHN_SERVICE;
659 case RPC_S_UNKNOWN_AUTHN_LEVEL: return RPC_NT_UNKNOWN_AUTHN_LEVEL;
660 case RPC_S_INVALID_AUTH_IDENTITY: return RPC_NT_INVALID_AUTH_IDENTITY;
661 case RPC_S_UNKNOWN_AUTHZ_SERVICE: return RPC_NT_UNKNOWN_AUTHZ_SERVICE;
662 case EPT_S_INVALID_ENTRY: return EPT_NT_INVALID_ENTRY;
663 case EPT_S_CANT_PERFORM_OP: return EPT_NT_CANT_PERFORM_OP;
664 case EPT_S_NOT_REGISTERED: return EPT_NT_NOT_REGISTERED;
665 case EPT_S_CANT_CREATE: return EPT_NT_CANT_CREATE;
666 case RPC_S_NOTHING_TO_EXPORT: return RPC_NT_NOTHING_TO_EXPORT;
667 case RPC_S_INCOMPLETE_NAME: return RPC_NT_INCOMPLETE_NAME;
668 case RPC_S_INVALID_VERS_OPTION: return RPC_NT_INVALID_VERS_OPTION;
669 case RPC_S_NO_MORE_MEMBERS: return RPC_NT_NO_MORE_MEMBERS;
670 case RPC_S_NOT_ALL_OBJS_UNEXPORTED: return RPC_NT_NOT_ALL_OBJS_UNEXPORTED;
671 case RPC_S_INTERFACE_NOT_FOUND: return RPC_NT_INTERFACE_NOT_FOUND;
672 case RPC_S_ENTRY_ALREADY_EXISTS: return RPC_NT_ENTRY_ALREADY_EXISTS;
673 case RPC_S_ENTRY_NOT_FOUND: return RPC_NT_ENTRY_NOT_FOUND;
674 case RPC_S_NAME_SERVICE_UNAVAILABLE: return RPC_NT_NAME_SERVICE_UNAVAILABLE;
675 case RPC_S_INVALID_NAF_ID: return RPC_NT_INVALID_NAF_ID;
676 case RPC_S_CANNOT_SUPPORT: return RPC_NT_CANNOT_SUPPORT;
677 case RPC_S_NO_CONTEXT_AVAILABLE: return RPC_NT_NO_CONTEXT_AVAILABLE;
678 case RPC_S_INTERNAL_ERROR: return RPC_NT_INTERNAL_ERROR;
679 case RPC_S_ZERO_DIVIDE: return RPC_NT_ZERO_DIVIDE;
680 case RPC_S_ADDRESS_ERROR: return RPC_NT_ADDRESS_ERROR;
681 case RPC_S_FP_DIV_ZERO: return RPC_NT_FP_DIV_ZERO;
682 case RPC_S_FP_UNDERFLOW: return RPC_NT_FP_UNDERFLOW;
683 case RPC_S_FP_OVERFLOW: return RPC_NT_FP_OVERFLOW;
684 case RPC_S_CALL_IN_PROGRESS: return RPC_NT_CALL_IN_PROGRESS;
685 case RPC_S_NO_MORE_BINDINGS: return RPC_NT_NO_MORE_BINDINGS;
686 case RPC_S_CALL_CANCELLED: return RPC_NT_CALL_CANCELLED;
687 case RPC_S_INVALID_OBJECT: return RPC_NT_INVALID_OBJECT;
688 case RPC_S_INVALID_ASYNC_HANDLE: return RPC_NT_INVALID_ASYNC_HANDLE;
689 case RPC_S_INVALID_ASYNC_CALL: return RPC_NT_INVALID_ASYNC_CALL;
690 case RPC_S_GROUP_MEMBER_NOT_FOUND: return RPC_NT_GROUP_MEMBER_NOT_FOUND;
691 case RPC_X_NO_MORE_ENTRIES: return RPC_NT_NO_MORE_ENTRIES;
692 case RPC_X_SS_CHAR_TRANS_OPEN_FAIL: return RPC_NT_SS_CHAR_TRANS_OPEN_FAIL;
693 case RPC_X_SS_CHAR_TRANS_SHORT_FILE: return RPC_NT_SS_CHAR_TRANS_SHORT_FILE;
694 case RPC_X_SS_IN_NULL_CONTEXT: return RPC_NT_SS_IN_NULL_CONTEXT;
695 case RPC_X_SS_CONTEXT_DAMAGED: return RPC_NT_SS_CONTEXT_DAMAGED;
696 case RPC_X_SS_HANDLES_MISMATCH: return RPC_NT_SS_HANDLES_MISMATCH;
697 case RPC_X_SS_CANNOT_GET_CALL_HANDLE: return RPC_NT_SS_CANNOT_GET_CALL_HANDLE;
698 case RPC_X_NULL_REF_POINTER: return RPC_NT_NULL_REF_POINTER;
699 case RPC_X_ENUM_VALUE_OUT_OF_RANGE: return RPC_NT_ENUM_VALUE_OUT_OF_RANGE;
700 case RPC_X_BYTE_COUNT_TOO_SMALL: return RPC_NT_BYTE_COUNT_TOO_SMALL;
701 case RPC_X_BAD_STUB_DATA: return RPC_NT_BAD_STUB_DATA;
702 case RPC_X_PIPE_CLOSED: return RPC_NT_PIPE_CLOSED;
703 case RPC_X_PIPE_DISCIPLINE_ERROR: return RPC_NT_PIPE_DISCIPLINE_ERROR;
704 case RPC_X_PIPE_EMPTY: return RPC_NT_PIPE_EMPTY;
705 case ERROR_PASSWORD_MUST_CHANGE: return STATUS_PASSWORD_MUST_CHANGE;
706 case ERROR_ACCOUNT_LOCKED_OUT: return STATUS_ACCOUNT_LOCKED_OUT;
707 default: return status;
711 /******************************************************************************
712 * I_RpcExceptionFilter (rpcrt4.@)
714 int WINAPI I_RpcExceptionFilter(ULONG ExceptionCode)
716 TRACE("0x%x\n", ExceptionCode);
717 switch (ExceptionCode)
719 case STATUS_DATATYPE_MISALIGNMENT:
720 case STATUS_BREAKPOINT:
721 case STATUS_ACCESS_VIOLATION:
722 case STATUS_ILLEGAL_INSTRUCTION:
723 case STATUS_PRIVILEGED_INSTRUCTION:
724 case STATUS_INSTRUCTION_MISALIGNMENT:
725 case STATUS_STACK_OVERFLOW:
726 case STATUS_POSSIBLE_DEADLOCK:
727 return EXCEPTION_CONTINUE_SEARCH;
728 default:
729 return EXCEPTION_EXECUTE_HANDLER;
733 /******************************************************************************
734 * RpcErrorStartEnumeration (rpcrt4.@)
736 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
738 FIXME("(%p): stub\n", EnumHandle);
739 return RPC_S_ENTRY_NOT_FOUND;
742 /******************************************************************************
743 * RpcMgmtSetCancelTimeout (rpcrt4.@)
745 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
747 FIXME("(%d): stub\n", Timeout);
748 return RPC_S_OK;
751 static struct threaddata *get_or_create_threaddata(void)
753 struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
754 if (!tdata)
756 tdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*tdata));
757 if (!tdata) return NULL;
759 InitializeCriticalSection(&tdata->cs);
760 tdata->thread_id = GetCurrentThreadId();
762 EnterCriticalSection(&threaddata_cs);
763 list_add_tail(&threaddata_list, &tdata->entry);
764 LeaveCriticalSection(&threaddata_cs);
766 NtCurrentTeb()->ReservedForNtRpc = tdata;
767 return tdata;
769 return tdata;
772 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
774 struct threaddata *tdata = get_or_create_threaddata();
775 if (!tdata) return;
777 EnterCriticalSection(&tdata->cs);
778 tdata->connection = Connection;
779 LeaveCriticalSection(&tdata->cs);
782 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
784 struct threaddata *tdata = get_or_create_threaddata();
785 if (!tdata) return;
787 tdata->server_binding = Binding;
790 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
792 struct threaddata *tdata = get_or_create_threaddata();
793 if (!tdata) return NULL;
795 return tdata->server_binding;
798 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext)
800 struct threaddata *tdata = get_or_create_threaddata();
801 struct context_handle_list *context_handle_list;
803 if (!tdata) return;
805 context_handle_list = HeapAlloc(GetProcessHeap(), 0, sizeof(*context_handle_list));
806 if (!context_handle_list) return;
808 context_handle_list->context_handle = SContext;
809 context_handle_list->next = tdata->context_handle_list;
810 tdata->context_handle_list = context_handle_list;
813 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext)
815 struct threaddata *tdata = get_or_create_threaddata();
816 struct context_handle_list *current, *prev;
818 if (!tdata) return;
820 for (current = tdata->context_handle_list, prev = NULL; current; prev = current, current = current->next)
822 if (current->context_handle == SContext)
824 if (prev)
825 prev->next = current->next;
826 else
827 tdata->context_handle_list = current->next;
828 HeapFree(GetProcessHeap(), 0, current);
829 return;
834 NDR_SCONTEXT RPCRT4_PopThreadContextHandle(void)
836 struct threaddata *tdata = get_or_create_threaddata();
837 struct context_handle_list *context_handle_list;
838 NDR_SCONTEXT context_handle;
840 if (!tdata) return NULL;
842 context_handle_list = tdata->context_handle_list;
843 if (!context_handle_list) return NULL;
844 tdata->context_handle_list = context_handle_list->next;
846 context_handle = context_handle_list->context_handle;
847 HeapFree(GetProcessHeap(), 0, context_handle_list);
848 return context_handle;
851 static RPC_STATUS rpc_cancel_thread(DWORD target_tid)
853 struct threaddata *tdata;
855 EnterCriticalSection(&threaddata_cs);
856 LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
857 if (tdata->thread_id == target_tid)
859 EnterCriticalSection(&tdata->cs);
860 if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
861 LeaveCriticalSection(&tdata->cs);
862 break;
864 LeaveCriticalSection(&threaddata_cs);
866 return RPC_S_OK;
869 /******************************************************************************
870 * RpcCancelThread (rpcrt4.@)
872 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
874 TRACE("(%p)\n", ThreadHandle);
875 return RpcCancelThreadEx(ThreadHandle, 0);
878 /******************************************************************************
879 * RpcCancelThreadEx (rpcrt4.@)
881 RPC_STATUS RPC_ENTRY RpcCancelThreadEx(void* ThreadHandle, LONG Timeout)
883 DWORD target_tid;
885 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
887 target_tid = GetThreadId(ThreadHandle);
888 if (!target_tid)
889 return RPC_S_INVALID_ARG;
891 if (Timeout)
893 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
894 return RPC_S_OK;
896 else
897 return rpc_cancel_thread(target_tid);