Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / rpcrt4 / rpcrt4_main.c
bloba790ccbd5683cc43ff14a99aad58fb8f565aada9
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 RPCRT4_ServerFreeAllRegisteredAuthInfo();
131 break;
134 return TRUE;
137 /*************************************************************************
138 * RpcStringFreeA [RPCRT4.@]
140 * Frees a character string allocated by the RPC run-time library.
142 * RETURNS
144 * S_OK if successful.
146 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
148 HeapFree( GetProcessHeap(), 0, *String);
150 return RPC_S_OK;
153 /*************************************************************************
154 * RpcStringFreeW [RPCRT4.@]
156 * Frees a character string allocated by the RPC run-time library.
158 * RETURNS
160 * S_OK if successful.
162 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
164 HeapFree( GetProcessHeap(), 0, *String);
166 return RPC_S_OK;
169 /*************************************************************************
170 * RpcRaiseException [RPCRT4.@]
172 * Raises an exception.
174 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
176 /* shouldn't return */
177 RaiseException(exception, 0, 0, NULL);
178 ERR("handler continued execution\n");
179 ExitProcess(1);
182 /*************************************************************************
183 * UuidCompare [RPCRT4.@]
185 * PARAMS
186 * UUID *Uuid1 [I] Uuid to compare
187 * UUID *Uuid2 [I] Uuid to compare
188 * RPC_STATUS *Status [O] returns RPC_S_OK
190 * RETURNS
191 * -1 if Uuid1 is less than Uuid2
192 * 0 if Uuid1 and Uuid2 are equal
193 * 1 if Uuid1 is greater than Uuid2
195 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
197 int i;
199 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
201 *Status = RPC_S_OK;
203 if (!Uuid1) Uuid1 = &uuid_nil;
204 if (!Uuid2) Uuid2 = &uuid_nil;
206 if (Uuid1 == Uuid2) return 0;
208 if (Uuid1->Data1 != Uuid2->Data1)
209 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
211 if (Uuid1->Data2 != Uuid2->Data2)
212 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
214 if (Uuid1->Data3 != Uuid2->Data3)
215 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
217 for (i = 0; i < 8; i++) {
218 if (Uuid1->Data4[i] < Uuid2->Data4[i])
219 return -1;
220 if (Uuid1->Data4[i] > Uuid2->Data4[i])
221 return 1;
224 return 0;
227 /*************************************************************************
228 * UuidEqual [RPCRT4.@]
230 * PARAMS
231 * UUID *Uuid1 [I] Uuid to compare
232 * UUID *Uuid2 [I] Uuid to compare
233 * RPC_STATUS *Status [O] returns RPC_S_OK
235 * RETURNS
236 * TRUE/FALSE
238 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
240 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
241 return !UuidCompare(Uuid1, Uuid2, Status);
244 /*************************************************************************
245 * UuidIsNil [RPCRT4.@]
247 * PARAMS
248 * UUID *Uuid [I] Uuid to compare
249 * RPC_STATUS *Status [O] returns RPC_S_OK
251 * RETURNS
252 * TRUE/FALSE
254 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
256 TRACE("(%s)\n", debugstr_guid(Uuid));
257 if (!Uuid) return TRUE;
258 return !UuidCompare(Uuid, &uuid_nil, Status);
261 /*************************************************************************
262 * UuidCreateNil [RPCRT4.@]
264 * PARAMS
265 * UUID *Uuid [O] returns a nil UUID
267 * RETURNS
268 * RPC_S_OK
270 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
272 *Uuid = uuid_nil;
273 return RPC_S_OK;
276 /*************************************************************************
277 * UuidCreate [RPCRT4.@]
279 * Creates a 128bit UUID.
281 * RETURNS
283 * RPC_S_OK if successful.
284 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
286 * NOTES
288 * Follows RFC 4122, section 4.4 (Algorithms for Creating a UUID from
289 * Truly Random or Pseudo-Random Numbers)
291 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
293 RtlGenRandom(Uuid, sizeof(*Uuid));
294 /* Clear the version bits and set the version (4) */
295 Uuid->Data3 &= 0x0fff;
296 Uuid->Data3 |= (4 << 12);
297 /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
298 * specified in RFC 4122, section 4.4.
300 Uuid->Data4[0] &= 0x3f;
301 Uuid->Data4[0] |= 0x80;
303 TRACE("%s\n", debugstr_guid(Uuid));
305 return RPC_S_OK;
308 /*************************************************************************
309 * UuidCreateSequential [RPCRT4.@]
311 * Creates a 128bit UUID.
313 * RETURNS
315 * RPC_S_OK if successful.
316 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
319 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
321 return UuidCreate(Uuid);
325 /*************************************************************************
326 * UuidHash [RPCRT4.@]
328 * Generates a hash value for a given UUID
330 * Code based on FreeDCE implementation
333 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
335 BYTE *data = (BYTE*)uuid;
336 short c0 = 0, c1 = 0, x, y;
337 unsigned int i;
339 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
341 TRACE("(%s)\n", debugstr_guid(uuid));
343 for (i=0; i<sizeof(UUID); i++) {
344 c0 += data[i];
345 c1 += c0;
348 x = -c1 % 255;
349 if (x < 0) x += 255;
351 y = (c1 - c0) % 255;
352 if (y < 0) y += 255;
354 *Status = RPC_S_OK;
355 return y*256 + x;
358 /*************************************************************************
359 * UuidToStringA [RPCRT4.@]
361 * Converts a UUID to a string.
363 * UUID format is 8 hex digits, followed by a hyphen then three groups of
364 * 4 hex digits each followed by a hyphen and then 12 hex digits
366 * RETURNS
368 * S_OK if successful.
369 * S_OUT_OF_MEMORY if unsuccessful.
371 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
373 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
375 if(!(*StringUuid))
376 return RPC_S_OUT_OF_MEMORY;
378 if (!Uuid) Uuid = &uuid_nil;
380 sprintf( (char*)*StringUuid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
381 Uuid->Data1, Uuid->Data2, Uuid->Data3,
382 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
383 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
384 Uuid->Data4[6], Uuid->Data4[7] );
386 return RPC_S_OK;
389 /*************************************************************************
390 * UuidToStringW [RPCRT4.@]
392 * Converts a UUID to a string.
394 * S_OK if successful.
395 * S_OUT_OF_MEMORY if unsuccessful.
397 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
399 char buf[37];
401 if (!Uuid) Uuid = &uuid_nil;
403 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
404 Uuid->Data1, Uuid->Data2, Uuid->Data3,
405 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
406 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
407 Uuid->Data4[6], Uuid->Data4[7] );
409 *StringUuid = RPCRT4_strdupAtoW(buf);
411 if(!(*StringUuid))
412 return RPC_S_OUT_OF_MEMORY;
414 return RPC_S_OK;
417 static const BYTE hex2bin[] =
419 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
420 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
421 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
422 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
423 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
424 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
425 0,10,11,12,13,14,15 /* 0x60 */
428 /***********************************************************************
429 * UuidFromStringA (RPCRT4.@)
431 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
433 int i;
435 if (!s) return UuidCreateNil( uuid );
437 if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
439 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
440 return RPC_S_INVALID_STRING_UUID;
442 for (i=0; i<36; i++)
444 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
445 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
448 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
450 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
451 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
452 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
453 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
455 /* these are just sequential bytes */
456 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
457 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
458 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
459 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
460 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
461 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
462 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
463 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
464 return RPC_S_OK;
468 /***********************************************************************
469 * UuidFromStringW (RPCRT4.@)
471 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
473 int i;
475 if (!s) return UuidCreateNil( uuid );
477 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
479 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
480 return RPC_S_INVALID_STRING_UUID;
482 for (i=0; i<36; i++)
484 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
485 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
488 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
490 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
491 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
492 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
493 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
495 /* these are just sequential bytes */
496 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
497 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
498 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
499 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
500 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
501 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
502 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
503 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
504 return RPC_S_OK;
507 /***********************************************************************
508 * DllRegisterServer (RPCRT4.@)
511 HRESULT WINAPI DllRegisterServer( void )
513 FIXME( "(): stub\n" );
514 return S_OK;
517 #define MAX_RPC_ERROR_TEXT 256
519 /******************************************************************************
520 * DceErrorInqTextW (rpcrt4.@)
522 * Notes
523 * 1. On passing a NULL pointer the code does bomb out.
524 * 2. The size of the required buffer is not defined in the documentation.
525 * It appears to be 256.
526 * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
527 * of any value for which it does.
528 * 4. The MSDN documentation currently declares that the second argument is
529 * unsigned char *, even for the W version. I don't believe it.
531 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
533 DWORD count;
534 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
535 FORMAT_MESSAGE_IGNORE_INSERTS,
536 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
537 if (!count)
539 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
540 FORMAT_MESSAGE_IGNORE_INSERTS,
541 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
542 if (!count)
544 ERR ("Failed to translate error\n");
545 return RPC_S_INVALID_ARG;
548 return RPC_S_OK;
551 /******************************************************************************
552 * DceErrorInqTextA (rpcrt4.@)
554 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
556 RPC_STATUS status;
557 WCHAR bufferW [MAX_RPC_ERROR_TEXT];
558 if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
560 if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
561 NULL, NULL))
563 ERR ("Failed to translate error\n");
564 status = RPC_S_INVALID_ARG;
567 return status;
570 /******************************************************************************
571 * I_RpcAllocate (rpcrt4.@)
573 void * WINAPI I_RpcAllocate(unsigned int Size)
575 return HeapAlloc(GetProcessHeap(), 0, Size);
578 /******************************************************************************
579 * I_RpcFree (rpcrt4.@)
581 void WINAPI I_RpcFree(void *Object)
583 HeapFree(GetProcessHeap(), 0, Object);
586 /******************************************************************************
587 * I_RpcMapWin32Status (rpcrt4.@)
589 * Maps Win32 RPC error codes to NT statuses.
591 * PARAMS
592 * status [I] Win32 RPC error code.
594 * RETURNS
595 * Appropriate translation into an NT status code.
597 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
599 TRACE("(%d)\n", status);
600 switch (status)
602 case ERROR_ACCESS_DENIED: return STATUS_ACCESS_DENIED;
603 case ERROR_INVALID_HANDLE: return RPC_NT_SS_CONTEXT_MISMATCH;
604 case ERROR_OUTOFMEMORY: return STATUS_NO_MEMORY;
605 case ERROR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
606 case ERROR_INSUFFICIENT_BUFFER: return STATUS_BUFFER_TOO_SMALL;
607 case ERROR_MAX_THRDS_REACHED: return STATUS_NO_MEMORY;
608 case ERROR_NOACCESS: return STATUS_ACCESS_VIOLATION;
609 case ERROR_NOT_ENOUGH_SERVER_MEMORY: return STATUS_INSUFF_SERVER_RESOURCES;
610 case ERROR_WRONG_PASSWORD: return STATUS_WRONG_PASSWORD;
611 case ERROR_INVALID_LOGON_HOURS: return STATUS_INVALID_LOGON_HOURS;
612 case ERROR_PASSWORD_EXPIRED: return STATUS_PASSWORD_EXPIRED;
613 case ERROR_ACCOUNT_DISABLED: return STATUS_ACCOUNT_DISABLED;
614 case ERROR_INVALID_SECURITY_DESCR: return STATUS_INVALID_SECURITY_DESCR;
615 case RPC_S_INVALID_STRING_BINDING: return RPC_NT_INVALID_STRING_BINDING;
616 case RPC_S_WRONG_KIND_OF_BINDING: return RPC_NT_WRONG_KIND_OF_BINDING;
617 case RPC_S_INVALID_BINDING: return RPC_NT_INVALID_BINDING;
618 case RPC_S_PROTSEQ_NOT_SUPPORTED: return RPC_NT_PROTSEQ_NOT_SUPPORTED;
619 case RPC_S_INVALID_RPC_PROTSEQ: return RPC_NT_INVALID_RPC_PROTSEQ;
620 case RPC_S_INVALID_STRING_UUID: return RPC_NT_INVALID_STRING_UUID;
621 case RPC_S_INVALID_ENDPOINT_FORMAT: return RPC_NT_INVALID_ENDPOINT_FORMAT;
622 case RPC_S_INVALID_NET_ADDR: return RPC_NT_INVALID_NET_ADDR;
623 case RPC_S_NO_ENDPOINT_FOUND: return RPC_NT_NO_ENDPOINT_FOUND;
624 case RPC_S_INVALID_TIMEOUT: return RPC_NT_INVALID_TIMEOUT;
625 case RPC_S_OBJECT_NOT_FOUND: return RPC_NT_OBJECT_NOT_FOUND;
626 case RPC_S_ALREADY_REGISTERED: return RPC_NT_ALREADY_REGISTERED;
627 case RPC_S_TYPE_ALREADY_REGISTERED: return RPC_NT_TYPE_ALREADY_REGISTERED;
628 case RPC_S_ALREADY_LISTENING: return RPC_NT_ALREADY_LISTENING;
629 case RPC_S_NO_PROTSEQS_REGISTERED: return RPC_NT_NO_PROTSEQS_REGISTERED;
630 case RPC_S_NOT_LISTENING: return RPC_NT_NOT_LISTENING;
631 case RPC_S_UNKNOWN_MGR_TYPE: return RPC_NT_UNKNOWN_MGR_TYPE;
632 case RPC_S_UNKNOWN_IF: return RPC_NT_UNKNOWN_IF;
633 case RPC_S_NO_BINDINGS: return RPC_NT_NO_BINDINGS;
634 case RPC_S_NO_PROTSEQS: return RPC_NT_NO_PROTSEQS;
635 case RPC_S_CANT_CREATE_ENDPOINT: return RPC_NT_CANT_CREATE_ENDPOINT;
636 case RPC_S_OUT_OF_RESOURCES: return RPC_NT_OUT_OF_RESOURCES;
637 case RPC_S_SERVER_UNAVAILABLE: return RPC_NT_SERVER_UNAVAILABLE;
638 case RPC_S_SERVER_TOO_BUSY: return RPC_NT_SERVER_TOO_BUSY;
639 case RPC_S_INVALID_NETWORK_OPTIONS: return RPC_NT_INVALID_NETWORK_OPTIONS;
640 case RPC_S_NO_CALL_ACTIVE: return RPC_NT_NO_CALL_ACTIVE;
641 case RPC_S_CALL_FAILED: return RPC_NT_CALL_FAILED;
642 case RPC_S_CALL_FAILED_DNE: return RPC_NT_CALL_FAILED_DNE;
643 case RPC_S_PROTOCOL_ERROR: return RPC_NT_PROTOCOL_ERROR;
644 case RPC_S_UNSUPPORTED_TRANS_SYN: return RPC_NT_UNSUPPORTED_TRANS_SYN;
645 case RPC_S_UNSUPPORTED_TYPE: return RPC_NT_UNSUPPORTED_TYPE;
646 case RPC_S_INVALID_TAG: return RPC_NT_INVALID_TAG;
647 case RPC_S_INVALID_BOUND: return RPC_NT_INVALID_BOUND;
648 case RPC_S_NO_ENTRY_NAME: return RPC_NT_NO_ENTRY_NAME;
649 case RPC_S_INVALID_NAME_SYNTAX: return RPC_NT_INVALID_NAME_SYNTAX;
650 case RPC_S_UNSUPPORTED_NAME_SYNTAX: return RPC_NT_UNSUPPORTED_NAME_SYNTAX;
651 case RPC_S_UUID_NO_ADDRESS: return RPC_NT_UUID_NO_ADDRESS;
652 case RPC_S_DUPLICATE_ENDPOINT: return RPC_NT_DUPLICATE_ENDPOINT;
653 case RPC_S_UNKNOWN_AUTHN_TYPE: return RPC_NT_UNKNOWN_AUTHN_TYPE;
654 case RPC_S_MAX_CALLS_TOO_SMALL: return RPC_NT_MAX_CALLS_TOO_SMALL;
655 case RPC_S_STRING_TOO_LONG: return RPC_NT_STRING_TOO_LONG;
656 case RPC_S_PROTSEQ_NOT_FOUND: return RPC_NT_PROTSEQ_NOT_FOUND;
657 case RPC_S_PROCNUM_OUT_OF_RANGE: return RPC_NT_PROCNUM_OUT_OF_RANGE;
658 case RPC_S_BINDING_HAS_NO_AUTH: return RPC_NT_BINDING_HAS_NO_AUTH;
659 case RPC_S_UNKNOWN_AUTHN_SERVICE: return RPC_NT_UNKNOWN_AUTHN_SERVICE;
660 case RPC_S_UNKNOWN_AUTHN_LEVEL: return RPC_NT_UNKNOWN_AUTHN_LEVEL;
661 case RPC_S_INVALID_AUTH_IDENTITY: return RPC_NT_INVALID_AUTH_IDENTITY;
662 case RPC_S_UNKNOWN_AUTHZ_SERVICE: return RPC_NT_UNKNOWN_AUTHZ_SERVICE;
663 case EPT_S_INVALID_ENTRY: return EPT_NT_INVALID_ENTRY;
664 case EPT_S_CANT_PERFORM_OP: return EPT_NT_CANT_PERFORM_OP;
665 case EPT_S_NOT_REGISTERED: return EPT_NT_NOT_REGISTERED;
666 case EPT_S_CANT_CREATE: return EPT_NT_CANT_CREATE;
667 case RPC_S_NOTHING_TO_EXPORT: return RPC_NT_NOTHING_TO_EXPORT;
668 case RPC_S_INCOMPLETE_NAME: return RPC_NT_INCOMPLETE_NAME;
669 case RPC_S_INVALID_VERS_OPTION: return RPC_NT_INVALID_VERS_OPTION;
670 case RPC_S_NO_MORE_MEMBERS: return RPC_NT_NO_MORE_MEMBERS;
671 case RPC_S_NOT_ALL_OBJS_UNEXPORTED: return RPC_NT_NOT_ALL_OBJS_UNEXPORTED;
672 case RPC_S_INTERFACE_NOT_FOUND: return RPC_NT_INTERFACE_NOT_FOUND;
673 case RPC_S_ENTRY_ALREADY_EXISTS: return RPC_NT_ENTRY_ALREADY_EXISTS;
674 case RPC_S_ENTRY_NOT_FOUND: return RPC_NT_ENTRY_NOT_FOUND;
675 case RPC_S_NAME_SERVICE_UNAVAILABLE: return RPC_NT_NAME_SERVICE_UNAVAILABLE;
676 case RPC_S_INVALID_NAF_ID: return RPC_NT_INVALID_NAF_ID;
677 case RPC_S_CANNOT_SUPPORT: return RPC_NT_CANNOT_SUPPORT;
678 case RPC_S_NO_CONTEXT_AVAILABLE: return RPC_NT_NO_CONTEXT_AVAILABLE;
679 case RPC_S_INTERNAL_ERROR: return RPC_NT_INTERNAL_ERROR;
680 case RPC_S_ZERO_DIVIDE: return RPC_NT_ZERO_DIVIDE;
681 case RPC_S_ADDRESS_ERROR: return RPC_NT_ADDRESS_ERROR;
682 case RPC_S_FP_DIV_ZERO: return RPC_NT_FP_DIV_ZERO;
683 case RPC_S_FP_UNDERFLOW: return RPC_NT_FP_UNDERFLOW;
684 case RPC_S_FP_OVERFLOW: return RPC_NT_FP_OVERFLOW;
685 case RPC_S_CALL_IN_PROGRESS: return RPC_NT_CALL_IN_PROGRESS;
686 case RPC_S_NO_MORE_BINDINGS: return RPC_NT_NO_MORE_BINDINGS;
687 case RPC_S_CALL_CANCELLED: return RPC_NT_CALL_CANCELLED;
688 case RPC_S_INVALID_OBJECT: return RPC_NT_INVALID_OBJECT;
689 case RPC_S_INVALID_ASYNC_HANDLE: return RPC_NT_INVALID_ASYNC_HANDLE;
690 case RPC_S_INVALID_ASYNC_CALL: return RPC_NT_INVALID_ASYNC_CALL;
691 case RPC_S_GROUP_MEMBER_NOT_FOUND: return RPC_NT_GROUP_MEMBER_NOT_FOUND;
692 case RPC_X_NO_MORE_ENTRIES: return RPC_NT_NO_MORE_ENTRIES;
693 case RPC_X_SS_CHAR_TRANS_OPEN_FAIL: return RPC_NT_SS_CHAR_TRANS_OPEN_FAIL;
694 case RPC_X_SS_CHAR_TRANS_SHORT_FILE: return RPC_NT_SS_CHAR_TRANS_SHORT_FILE;
695 case RPC_X_SS_IN_NULL_CONTEXT: return RPC_NT_SS_IN_NULL_CONTEXT;
696 case RPC_X_SS_CONTEXT_DAMAGED: return RPC_NT_SS_CONTEXT_DAMAGED;
697 case RPC_X_SS_HANDLES_MISMATCH: return RPC_NT_SS_HANDLES_MISMATCH;
698 case RPC_X_SS_CANNOT_GET_CALL_HANDLE: return RPC_NT_SS_CANNOT_GET_CALL_HANDLE;
699 case RPC_X_NULL_REF_POINTER: return RPC_NT_NULL_REF_POINTER;
700 case RPC_X_ENUM_VALUE_OUT_OF_RANGE: return RPC_NT_ENUM_VALUE_OUT_OF_RANGE;
701 case RPC_X_BYTE_COUNT_TOO_SMALL: return RPC_NT_BYTE_COUNT_TOO_SMALL;
702 case RPC_X_BAD_STUB_DATA: return RPC_NT_BAD_STUB_DATA;
703 case RPC_X_PIPE_CLOSED: return RPC_NT_PIPE_CLOSED;
704 case RPC_X_PIPE_DISCIPLINE_ERROR: return RPC_NT_PIPE_DISCIPLINE_ERROR;
705 case RPC_X_PIPE_EMPTY: return RPC_NT_PIPE_EMPTY;
706 case ERROR_PASSWORD_MUST_CHANGE: return STATUS_PASSWORD_MUST_CHANGE;
707 case ERROR_ACCOUNT_LOCKED_OUT: return STATUS_ACCOUNT_LOCKED_OUT;
708 default: return status;
712 /******************************************************************************
713 * I_RpcExceptionFilter (rpcrt4.@)
715 int WINAPI I_RpcExceptionFilter(ULONG ExceptionCode)
717 TRACE("0x%x\n", ExceptionCode);
718 switch (ExceptionCode)
720 case STATUS_DATATYPE_MISALIGNMENT:
721 case STATUS_BREAKPOINT:
722 case STATUS_ACCESS_VIOLATION:
723 case STATUS_ILLEGAL_INSTRUCTION:
724 case STATUS_PRIVILEGED_INSTRUCTION:
725 case STATUS_INSTRUCTION_MISALIGNMENT:
726 case STATUS_STACK_OVERFLOW:
727 case STATUS_POSSIBLE_DEADLOCK:
728 return EXCEPTION_CONTINUE_SEARCH;
729 default:
730 return EXCEPTION_EXECUTE_HANDLER;
734 /******************************************************************************
735 * RpcErrorStartEnumeration (rpcrt4.@)
737 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
739 FIXME("(%p): stub\n", EnumHandle);
740 return RPC_S_ENTRY_NOT_FOUND;
743 /******************************************************************************
744 * RpcMgmtSetCancelTimeout (rpcrt4.@)
746 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
748 FIXME("(%d): stub\n", Timeout);
749 return RPC_S_OK;
752 static struct threaddata *get_or_create_threaddata(void)
754 struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
755 if (!tdata)
757 tdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*tdata));
758 if (!tdata) return NULL;
760 InitializeCriticalSection(&tdata->cs);
761 tdata->thread_id = GetCurrentThreadId();
763 EnterCriticalSection(&threaddata_cs);
764 list_add_tail(&threaddata_list, &tdata->entry);
765 LeaveCriticalSection(&threaddata_cs);
767 NtCurrentTeb()->ReservedForNtRpc = tdata;
768 return tdata;
770 return tdata;
773 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
775 struct threaddata *tdata = get_or_create_threaddata();
776 if (!tdata) return;
778 EnterCriticalSection(&tdata->cs);
779 tdata->connection = Connection;
780 LeaveCriticalSection(&tdata->cs);
783 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
785 struct threaddata *tdata = get_or_create_threaddata();
786 if (!tdata) return;
788 tdata->server_binding = Binding;
791 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
793 struct threaddata *tdata = get_or_create_threaddata();
794 if (!tdata) return NULL;
796 return tdata->server_binding;
799 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext)
801 struct threaddata *tdata = get_or_create_threaddata();
802 struct context_handle_list *context_handle_list;
804 if (!tdata) return;
806 context_handle_list = HeapAlloc(GetProcessHeap(), 0, sizeof(*context_handle_list));
807 if (!context_handle_list) return;
809 context_handle_list->context_handle = SContext;
810 context_handle_list->next = tdata->context_handle_list;
811 tdata->context_handle_list = context_handle_list;
814 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext)
816 struct threaddata *tdata = get_or_create_threaddata();
817 struct context_handle_list *current, *prev;
819 if (!tdata) return;
821 for (current = tdata->context_handle_list, prev = NULL; current; prev = current, current = current->next)
823 if (current->context_handle == SContext)
825 if (prev)
826 prev->next = current->next;
827 else
828 tdata->context_handle_list = current->next;
829 HeapFree(GetProcessHeap(), 0, current);
830 return;
835 NDR_SCONTEXT RPCRT4_PopThreadContextHandle(void)
837 struct threaddata *tdata = get_or_create_threaddata();
838 struct context_handle_list *context_handle_list;
839 NDR_SCONTEXT context_handle;
841 if (!tdata) return NULL;
843 context_handle_list = tdata->context_handle_list;
844 if (!context_handle_list) return NULL;
845 tdata->context_handle_list = context_handle_list->next;
847 context_handle = context_handle_list->context_handle;
848 HeapFree(GetProcessHeap(), 0, context_handle_list);
849 return context_handle;
852 static RPC_STATUS rpc_cancel_thread(DWORD target_tid)
854 struct threaddata *tdata;
856 EnterCriticalSection(&threaddata_cs);
857 LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
858 if (tdata->thread_id == target_tid)
860 EnterCriticalSection(&tdata->cs);
861 if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
862 LeaveCriticalSection(&tdata->cs);
863 break;
865 LeaveCriticalSection(&threaddata_cs);
867 return RPC_S_OK;
870 /******************************************************************************
871 * RpcCancelThread (rpcrt4.@)
873 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
875 TRACE("(%p)\n", ThreadHandle);
876 return RpcCancelThreadEx(ThreadHandle, 0);
879 /******************************************************************************
880 * RpcCancelThreadEx (rpcrt4.@)
882 RPC_STATUS RPC_ENTRY RpcCancelThreadEx(void* ThreadHandle, LONG Timeout)
884 DWORD target_tid;
886 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
888 target_tid = GetThreadId(ThreadHandle);
889 if (!target_tid)
890 return RPC_S_INVALID_ARG;
892 if (Timeout)
894 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
895 return RPC_S_OK;
897 else
898 return rpc_cancel_thread(target_tid);