msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / rpcrt4 / rpcrt4_main.c
blob7380b7a151f24e7c758d44d1ad321133af412101
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"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
60 static UUID uuid_nil;
62 static CRITICAL_SECTION threaddata_cs;
63 static CRITICAL_SECTION_DEBUG threaddata_cs_debug =
65 0, 0, &threaddata_cs,
66 { &threaddata_cs_debug.ProcessLocksList, &threaddata_cs_debug.ProcessLocksList },
67 0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
69 static CRITICAL_SECTION threaddata_cs = { &threaddata_cs_debug, -1, 0, 0, 0, 0 };
71 static struct list threaddata_list = LIST_INIT(threaddata_list);
73 struct context_handle_list
75 struct context_handle_list *next;
76 NDR_SCONTEXT context_handle;
79 struct threaddata
81 struct list entry;
82 CRITICAL_SECTION cs;
83 DWORD thread_id;
84 RpcConnection *connection;
85 RpcBinding *server_binding;
86 struct context_handle_list *context_handle_list;
89 /***********************************************************************
90 * DllMain
92 * PARAMS
93 * hinstDLL [I] handle to the DLL's instance
94 * fdwReason [I]
95 * lpvReserved [I] reserved, must be NULL
97 * RETURNS
98 * Success: TRUE
99 * Failure: FALSE
102 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
104 struct threaddata *tdata;
106 switch (fdwReason) {
107 case DLL_PROCESS_ATTACH:
108 break;
110 case DLL_THREAD_DETACH:
111 tdata = NtCurrentTeb()->ReservedForNtRpc;
112 if (tdata)
114 EnterCriticalSection(&threaddata_cs);
115 list_remove(&tdata->entry);
116 LeaveCriticalSection(&threaddata_cs);
118 DeleteCriticalSection(&tdata->cs);
119 if (tdata->connection)
120 ERR("tdata->connection should be NULL but is still set to %p\n", tdata->connection);
121 if (tdata->server_binding)
122 ERR("tdata->server_binding should be NULL but is still set to %p\n", tdata->server_binding);
123 HeapFree(GetProcessHeap(), 0, tdata);
125 break;
127 case DLL_PROCESS_DETACH:
128 break;
131 return TRUE;
134 /*************************************************************************
135 * RpcStringFreeA [RPCRT4.@]
137 * Frees a character string allocated by the RPC run-time library.
139 * RETURNS
141 * S_OK if successful.
143 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
145 HeapFree( GetProcessHeap(), 0, *String);
147 return RPC_S_OK;
150 /*************************************************************************
151 * RpcStringFreeW [RPCRT4.@]
153 * Frees a character string allocated by the RPC run-time library.
155 * RETURNS
157 * S_OK if successful.
159 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
161 HeapFree( GetProcessHeap(), 0, *String);
163 return RPC_S_OK;
166 /*************************************************************************
167 * RpcRaiseException [RPCRT4.@]
169 * Raises an exception.
171 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
173 /* shouldn't return */
174 RaiseException(exception, 0, 0, NULL);
175 ERR("handler continued execution\n");
176 ExitProcess(1);
179 /*************************************************************************
180 * UuidCompare [RPCRT4.@]
182 * PARAMS
183 * UUID *Uuid1 [I] Uuid to compare
184 * UUID *Uuid2 [I] Uuid to compare
185 * RPC_STATUS *Status [O] returns RPC_S_OK
187 * RETURNS
188 * -1 if Uuid1 is less than Uuid2
189 * 0 if Uuid1 and Uuid2 are equal
190 * 1 if Uuid1 is greater than Uuid2
192 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
194 int i;
196 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
198 *Status = RPC_S_OK;
200 if (!Uuid1) Uuid1 = &uuid_nil;
201 if (!Uuid2) Uuid2 = &uuid_nil;
203 if (Uuid1 == Uuid2) return 0;
205 if (Uuid1->Data1 != Uuid2->Data1)
206 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
208 if (Uuid1->Data2 != Uuid2->Data2)
209 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
211 if (Uuid1->Data3 != Uuid2->Data3)
212 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
214 for (i = 0; i < 8; i++) {
215 if (Uuid1->Data4[i] < Uuid2->Data4[i])
216 return -1;
217 if (Uuid1->Data4[i] > Uuid2->Data4[i])
218 return 1;
221 return 0;
224 /*************************************************************************
225 * UuidEqual [RPCRT4.@]
227 * PARAMS
228 * UUID *Uuid1 [I] Uuid to compare
229 * UUID *Uuid2 [I] Uuid to compare
230 * RPC_STATUS *Status [O] returns RPC_S_OK
232 * RETURNS
233 * TRUE/FALSE
235 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
237 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
238 return !UuidCompare(Uuid1, Uuid2, Status);
241 /*************************************************************************
242 * UuidIsNil [RPCRT4.@]
244 * PARAMS
245 * UUID *Uuid [I] Uuid to compare
246 * RPC_STATUS *Status [O] returns RPC_S_OK
248 * RETURNS
249 * TRUE/FALSE
251 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
253 TRACE("(%s)\n", debugstr_guid(Uuid));
254 if (!Uuid) return TRUE;
255 return !UuidCompare(Uuid, &uuid_nil, Status);
258 /*************************************************************************
259 * UuidCreateNil [RPCRT4.@]
261 * PARAMS
262 * UUID *Uuid [O] returns a nil UUID
264 * RETURNS
265 * RPC_S_OK
267 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
269 *Uuid = uuid_nil;
270 return RPC_S_OK;
273 /*************************************************************************
274 * UuidCreate [RPCRT4.@]
276 * Creates a 128bit UUID.
278 * RETURNS
280 * RPC_S_OK if successful.
281 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
283 * NOTES
285 * Follows RFC 4122, section 4.4 (Algorithms for Creating a UUID from
286 * Truly Random or Pseudo-Random Numbers)
288 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
290 RtlGenRandom(Uuid, sizeof(*Uuid));
291 /* Clear the version bits and set the version (4) */
292 Uuid->Data3 &= 0x0fff;
293 Uuid->Data3 |= (4 << 12);
294 /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
295 * specified in RFC 4122, section 4.4.
297 Uuid->Data4[0] &= 0x3f;
298 Uuid->Data4[0] |= 0x80;
300 TRACE("%s\n", debugstr_guid(Uuid));
302 return RPC_S_OK;
305 /*************************************************************************
306 * UuidCreateSequential [RPCRT4.@]
308 * Creates a 128bit UUID.
310 * RETURNS
312 * RPC_S_OK if successful.
313 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
316 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
318 return UuidCreate(Uuid);
322 /*************************************************************************
323 * UuidHash [RPCRT4.@]
325 * Generates a hash value for a given UUID
327 * Code based on FreeDCE implementation
330 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
332 BYTE *data = (BYTE*)uuid;
333 short c0 = 0, c1 = 0, x, y;
334 unsigned int i;
336 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
338 TRACE("(%s)\n", debugstr_guid(uuid));
340 for (i=0; i<sizeof(UUID); i++) {
341 c0 += data[i];
342 c1 += c0;
345 x = -c1 % 255;
346 if (x < 0) x += 255;
348 y = (c1 - c0) % 255;
349 if (y < 0) y += 255;
351 *Status = RPC_S_OK;
352 return y*256 + x;
355 /*************************************************************************
356 * UuidToStringA [RPCRT4.@]
358 * Converts a UUID to a string.
360 * UUID format is 8 hex digits, followed by a hyphen then three groups of
361 * 4 hex digits each followed by a hyphen and then 12 hex digits
363 * RETURNS
365 * S_OK if successful.
366 * S_OUT_OF_MEMORY if unsuccessful.
368 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
370 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
372 if(!(*StringUuid))
373 return RPC_S_OUT_OF_MEMORY;
375 if (!Uuid) Uuid = &uuid_nil;
377 sprintf( (char*)*StringUuid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
378 Uuid->Data1, Uuid->Data2, Uuid->Data3,
379 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
380 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
381 Uuid->Data4[6], Uuid->Data4[7] );
383 return RPC_S_OK;
386 /*************************************************************************
387 * UuidToStringW [RPCRT4.@]
389 * Converts a UUID to a string.
391 * S_OK if successful.
392 * S_OUT_OF_MEMORY if unsuccessful.
394 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
396 char buf[37];
398 if (!Uuid) Uuid = &uuid_nil;
400 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
401 Uuid->Data1, Uuid->Data2, Uuid->Data3,
402 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
403 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
404 Uuid->Data4[6], Uuid->Data4[7] );
406 *StringUuid = RPCRT4_strdupAtoW(buf);
408 if(!(*StringUuid))
409 return RPC_S_OUT_OF_MEMORY;
411 return RPC_S_OK;
414 static const BYTE hex2bin[] =
416 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
417 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
418 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
419 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
420 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
421 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
422 0,10,11,12,13,14,15 /* 0x60 */
425 /***********************************************************************
426 * UuidFromStringA (RPCRT4.@)
428 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
430 int i;
432 if (!s) return UuidCreateNil( uuid );
434 if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
436 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
437 return RPC_S_INVALID_STRING_UUID;
439 for (i=0; i<36; i++)
441 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
442 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
445 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
447 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
448 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
449 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
450 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
452 /* these are just sequential bytes */
453 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
454 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
455 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
456 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
457 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
458 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
459 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
460 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
461 return RPC_S_OK;
465 /***********************************************************************
466 * UuidFromStringW (RPCRT4.@)
468 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
470 int i;
472 if (!s) return UuidCreateNil( uuid );
474 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
476 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
477 return RPC_S_INVALID_STRING_UUID;
479 for (i=0; i<36; i++)
481 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
482 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
485 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
487 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
488 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
489 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
490 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
492 /* these are just sequential bytes */
493 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
494 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
495 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
496 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
497 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
498 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
499 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
500 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
501 return RPC_S_OK;
504 /***********************************************************************
505 * DllRegisterServer (RPCRT4.@)
508 HRESULT WINAPI DllRegisterServer( void )
510 FIXME( "(): stub\n" );
511 return S_OK;
514 #define MAX_RPC_ERROR_TEXT 256
516 /******************************************************************************
517 * DceErrorInqTextW (rpcrt4.@)
519 * Notes
520 * 1. On passing a NULL pointer the code does bomb out.
521 * 2. The size of the required buffer is not defined in the documentation.
522 * It appears to be 256.
523 * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
524 * of any value for which it does.
525 * 4. The MSDN documentation currently declares that the second argument is
526 * unsigned char *, even for the W version. I don't believe it.
528 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
530 DWORD count;
531 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
532 FORMAT_MESSAGE_IGNORE_INSERTS,
533 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
534 if (!count)
536 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
537 FORMAT_MESSAGE_IGNORE_INSERTS,
538 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
539 if (!count)
541 ERR ("Failed to translate error\n");
542 return RPC_S_INVALID_ARG;
545 return RPC_S_OK;
548 /******************************************************************************
549 * DceErrorInqTextA (rpcrt4.@)
551 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
553 RPC_STATUS status;
554 WCHAR bufferW [MAX_RPC_ERROR_TEXT];
555 if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
557 if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
558 NULL, NULL))
560 ERR ("Failed to translate error\n");
561 status = RPC_S_INVALID_ARG;
564 return status;
567 /******************************************************************************
568 * I_RpcAllocate (rpcrt4.@)
570 void * WINAPI I_RpcAllocate(unsigned int Size)
572 return HeapAlloc(GetProcessHeap(), 0, Size);
575 /******************************************************************************
576 * I_RpcFree (rpcrt4.@)
578 void WINAPI I_RpcFree(void *Object)
580 HeapFree(GetProcessHeap(), 0, Object);
583 /******************************************************************************
584 * I_RpcMapWin32Status (rpcrt4.@)
586 * Maps Win32 RPC error codes to NT statuses.
588 * PARAMS
589 * status [I] Win32 RPC error code.
591 * RETURNS
592 * Appropriate translation into an NT status code.
594 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
596 TRACE("(%d)\n", status);
597 switch (status)
599 case ERROR_ACCESS_DENIED: return STATUS_ACCESS_DENIED;
600 case ERROR_INVALID_HANDLE: return RPC_NT_SS_CONTEXT_MISMATCH;
601 case ERROR_OUTOFMEMORY: return STATUS_NO_MEMORY;
602 case ERROR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
603 case ERROR_INSUFFICIENT_BUFFER: return STATUS_BUFFER_TOO_SMALL;
604 case ERROR_MAX_THRDS_REACHED: return STATUS_NO_MEMORY;
605 case ERROR_NOACCESS: return STATUS_ACCESS_VIOLATION;
606 case ERROR_NOT_ENOUGH_SERVER_MEMORY: return STATUS_INSUFF_SERVER_RESOURCES;
607 case ERROR_WRONG_PASSWORD: return STATUS_WRONG_PASSWORD;
608 case ERROR_INVALID_LOGON_HOURS: return STATUS_INVALID_LOGON_HOURS;
609 case ERROR_PASSWORD_EXPIRED: return STATUS_PASSWORD_EXPIRED;
610 case ERROR_ACCOUNT_DISABLED: return STATUS_ACCOUNT_DISABLED;
611 case ERROR_INVALID_SECURITY_DESCR: return STATUS_INVALID_SECURITY_DESCR;
612 case RPC_S_INVALID_STRING_BINDING: return RPC_NT_INVALID_STRING_BINDING;
613 case RPC_S_WRONG_KIND_OF_BINDING: return RPC_NT_WRONG_KIND_OF_BINDING;
614 case RPC_S_INVALID_BINDING: return RPC_NT_INVALID_BINDING;
615 case RPC_S_PROTSEQ_NOT_SUPPORTED: return RPC_NT_PROTSEQ_NOT_SUPPORTED;
616 case RPC_S_INVALID_RPC_PROTSEQ: return RPC_NT_INVALID_RPC_PROTSEQ;
617 case RPC_S_INVALID_STRING_UUID: return RPC_NT_INVALID_STRING_UUID;
618 case RPC_S_INVALID_ENDPOINT_FORMAT: return RPC_NT_INVALID_ENDPOINT_FORMAT;
619 case RPC_S_INVALID_NET_ADDR: return RPC_NT_INVALID_NET_ADDR;
620 case RPC_S_NO_ENDPOINT_FOUND: return RPC_NT_NO_ENDPOINT_FOUND;
621 case RPC_S_INVALID_TIMEOUT: return RPC_NT_INVALID_TIMEOUT;
622 case RPC_S_OBJECT_NOT_FOUND: return RPC_NT_OBJECT_NOT_FOUND;
623 case RPC_S_ALREADY_REGISTERED: return RPC_NT_ALREADY_REGISTERED;
624 case RPC_S_TYPE_ALREADY_REGISTERED: return RPC_NT_TYPE_ALREADY_REGISTERED;
625 case RPC_S_ALREADY_LISTENING: return RPC_NT_ALREADY_LISTENING;
626 case RPC_S_NO_PROTSEQS_REGISTERED: return RPC_NT_NO_PROTSEQS_REGISTERED;
627 case RPC_S_NOT_LISTENING: return RPC_NT_NOT_LISTENING;
628 case RPC_S_UNKNOWN_MGR_TYPE: return RPC_NT_UNKNOWN_MGR_TYPE;
629 case RPC_S_UNKNOWN_IF: return RPC_NT_UNKNOWN_IF;
630 case RPC_S_NO_BINDINGS: return RPC_NT_NO_BINDINGS;
631 case RPC_S_NO_PROTSEQS: return RPC_NT_NO_PROTSEQS;
632 case RPC_S_CANT_CREATE_ENDPOINT: return RPC_NT_CANT_CREATE_ENDPOINT;
633 case RPC_S_OUT_OF_RESOURCES: return RPC_NT_OUT_OF_RESOURCES;
634 case RPC_S_SERVER_UNAVAILABLE: return RPC_NT_SERVER_UNAVAILABLE;
635 case RPC_S_SERVER_TOO_BUSY: return RPC_NT_SERVER_TOO_BUSY;
636 case RPC_S_INVALID_NETWORK_OPTIONS: return RPC_NT_INVALID_NETWORK_OPTIONS;
637 case RPC_S_NO_CALL_ACTIVE: return RPC_NT_NO_CALL_ACTIVE;
638 case RPC_S_CALL_FAILED: return RPC_NT_CALL_FAILED;
639 case RPC_S_CALL_FAILED_DNE: return RPC_NT_CALL_FAILED_DNE;
640 case RPC_S_PROTOCOL_ERROR: return RPC_NT_PROTOCOL_ERROR;
641 case RPC_S_UNSUPPORTED_TRANS_SYN: return RPC_NT_UNSUPPORTED_TRANS_SYN;
642 case RPC_S_UNSUPPORTED_TYPE: return RPC_NT_UNSUPPORTED_TYPE;
643 case RPC_S_INVALID_TAG: return RPC_NT_INVALID_TAG;
644 case RPC_S_INVALID_BOUND: return RPC_NT_INVALID_BOUND;
645 case RPC_S_NO_ENTRY_NAME: return RPC_NT_NO_ENTRY_NAME;
646 case RPC_S_INVALID_NAME_SYNTAX: return RPC_NT_INVALID_NAME_SYNTAX;
647 case RPC_S_UNSUPPORTED_NAME_SYNTAX: return RPC_NT_UNSUPPORTED_NAME_SYNTAX;
648 case RPC_S_UUID_NO_ADDRESS: return RPC_NT_UUID_NO_ADDRESS;
649 case RPC_S_DUPLICATE_ENDPOINT: return RPC_NT_DUPLICATE_ENDPOINT;
650 case RPC_S_UNKNOWN_AUTHN_TYPE: return RPC_NT_UNKNOWN_AUTHN_TYPE;
651 case RPC_S_MAX_CALLS_TOO_SMALL: return RPC_NT_MAX_CALLS_TOO_SMALL;
652 case RPC_S_STRING_TOO_LONG: return RPC_NT_STRING_TOO_LONG;
653 case RPC_S_PROTSEQ_NOT_FOUND: return RPC_NT_PROTSEQ_NOT_FOUND;
654 case RPC_S_PROCNUM_OUT_OF_RANGE: return RPC_NT_PROCNUM_OUT_OF_RANGE;
655 case RPC_S_BINDING_HAS_NO_AUTH: return RPC_NT_BINDING_HAS_NO_AUTH;
656 case RPC_S_UNKNOWN_AUTHN_SERVICE: return RPC_NT_UNKNOWN_AUTHN_SERVICE;
657 case RPC_S_UNKNOWN_AUTHN_LEVEL: return RPC_NT_UNKNOWN_AUTHN_LEVEL;
658 case RPC_S_INVALID_AUTH_IDENTITY: return RPC_NT_INVALID_AUTH_IDENTITY;
659 case RPC_S_UNKNOWN_AUTHZ_SERVICE: return RPC_NT_UNKNOWN_AUTHZ_SERVICE;
660 case EPT_S_INVALID_ENTRY: return EPT_NT_INVALID_ENTRY;
661 case EPT_S_CANT_PERFORM_OP: return EPT_NT_CANT_PERFORM_OP;
662 case EPT_S_NOT_REGISTERED: return EPT_NT_NOT_REGISTERED;
663 case EPT_S_CANT_CREATE: return EPT_NT_CANT_CREATE;
664 case RPC_S_NOTHING_TO_EXPORT: return RPC_NT_NOTHING_TO_EXPORT;
665 case RPC_S_INCOMPLETE_NAME: return RPC_NT_INCOMPLETE_NAME;
666 case RPC_S_INVALID_VERS_OPTION: return RPC_NT_INVALID_VERS_OPTION;
667 case RPC_S_NO_MORE_MEMBERS: return RPC_NT_NO_MORE_MEMBERS;
668 case RPC_S_NOT_ALL_OBJS_UNEXPORTED: return RPC_NT_NOT_ALL_OBJS_UNEXPORTED;
669 case RPC_S_INTERFACE_NOT_FOUND: return RPC_NT_INTERFACE_NOT_FOUND;
670 case RPC_S_ENTRY_ALREADY_EXISTS: return RPC_NT_ENTRY_ALREADY_EXISTS;
671 case RPC_S_ENTRY_NOT_FOUND: return RPC_NT_ENTRY_NOT_FOUND;
672 case RPC_S_NAME_SERVICE_UNAVAILABLE: return RPC_NT_NAME_SERVICE_UNAVAILABLE;
673 case RPC_S_INVALID_NAF_ID: return RPC_NT_INVALID_NAF_ID;
674 case RPC_S_CANNOT_SUPPORT: return RPC_NT_CANNOT_SUPPORT;
675 case RPC_S_NO_CONTEXT_AVAILABLE: return RPC_NT_NO_CONTEXT_AVAILABLE;
676 case RPC_S_INTERNAL_ERROR: return RPC_NT_INTERNAL_ERROR;
677 case RPC_S_ZERO_DIVIDE: return RPC_NT_ZERO_DIVIDE;
678 case RPC_S_ADDRESS_ERROR: return RPC_NT_ADDRESS_ERROR;
679 case RPC_S_FP_DIV_ZERO: return RPC_NT_FP_DIV_ZERO;
680 case RPC_S_FP_UNDERFLOW: return RPC_NT_FP_UNDERFLOW;
681 case RPC_S_FP_OVERFLOW: return RPC_NT_FP_OVERFLOW;
682 case RPC_S_CALL_IN_PROGRESS: return RPC_NT_CALL_IN_PROGRESS;
683 case RPC_S_NO_MORE_BINDINGS: return RPC_NT_NO_MORE_BINDINGS;
684 case RPC_S_CALL_CANCELLED: return RPC_NT_CALL_CANCELLED;
685 case RPC_S_INVALID_OBJECT: return RPC_NT_INVALID_OBJECT;
686 case RPC_S_INVALID_ASYNC_HANDLE: return RPC_NT_INVALID_ASYNC_HANDLE;
687 case RPC_S_INVALID_ASYNC_CALL: return RPC_NT_INVALID_ASYNC_CALL;
688 case RPC_S_GROUP_MEMBER_NOT_FOUND: return RPC_NT_GROUP_MEMBER_NOT_FOUND;
689 case RPC_X_NO_MORE_ENTRIES: return RPC_NT_NO_MORE_ENTRIES;
690 case RPC_X_SS_CHAR_TRANS_OPEN_FAIL: return RPC_NT_SS_CHAR_TRANS_OPEN_FAIL;
691 case RPC_X_SS_CHAR_TRANS_SHORT_FILE: return RPC_NT_SS_CHAR_TRANS_SHORT_FILE;
692 case RPC_X_SS_IN_NULL_CONTEXT: return RPC_NT_SS_IN_NULL_CONTEXT;
693 case RPC_X_SS_CONTEXT_DAMAGED: return RPC_NT_SS_CONTEXT_DAMAGED;
694 case RPC_X_SS_HANDLES_MISMATCH: return RPC_NT_SS_HANDLES_MISMATCH;
695 case RPC_X_SS_CANNOT_GET_CALL_HANDLE: return RPC_NT_SS_CANNOT_GET_CALL_HANDLE;
696 case RPC_X_NULL_REF_POINTER: return RPC_NT_NULL_REF_POINTER;
697 case RPC_X_ENUM_VALUE_OUT_OF_RANGE: return RPC_NT_ENUM_VALUE_OUT_OF_RANGE;
698 case RPC_X_BYTE_COUNT_TOO_SMALL: return RPC_NT_BYTE_COUNT_TOO_SMALL;
699 case RPC_X_BAD_STUB_DATA: return RPC_NT_BAD_STUB_DATA;
700 case RPC_X_PIPE_CLOSED: return RPC_NT_PIPE_CLOSED;
701 case RPC_X_PIPE_DISCIPLINE_ERROR: return RPC_NT_PIPE_DISCIPLINE_ERROR;
702 case RPC_X_PIPE_EMPTY: return RPC_NT_PIPE_EMPTY;
703 case ERROR_PASSWORD_MUST_CHANGE: return STATUS_PASSWORD_MUST_CHANGE;
704 case ERROR_ACCOUNT_LOCKED_OUT: return STATUS_ACCOUNT_LOCKED_OUT;
705 default: return status;
709 /******************************************************************************
710 * I_RpcExceptionFilter (rpcrt4.@)
712 int WINAPI I_RpcExceptionFilter(ULONG ExceptionCode)
714 TRACE("0x%x\n", ExceptionCode);
715 switch (ExceptionCode)
717 case STATUS_DATATYPE_MISALIGNMENT:
718 case STATUS_BREAKPOINT:
719 case STATUS_ACCESS_VIOLATION:
720 case STATUS_ILLEGAL_INSTRUCTION:
721 case STATUS_PRIVILEGED_INSTRUCTION:
722 case STATUS_INSTRUCTION_MISALIGNMENT:
723 case STATUS_STACK_OVERFLOW:
724 case STATUS_POSSIBLE_DEADLOCK:
725 return EXCEPTION_CONTINUE_SEARCH;
726 default:
727 return EXCEPTION_EXECUTE_HANDLER;
731 /******************************************************************************
732 * RpcErrorStartEnumeration (rpcrt4.@)
734 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
736 FIXME("(%p): stub\n", EnumHandle);
737 return RPC_S_ENTRY_NOT_FOUND;
740 /******************************************************************************
741 * RpcMgmtSetCancelTimeout (rpcrt4.@)
743 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
745 FIXME("(%d): stub\n", Timeout);
746 return RPC_S_OK;
749 static struct threaddata *get_or_create_threaddata(void)
751 struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
752 if (!tdata)
754 tdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*tdata));
755 if (!tdata) return NULL;
757 InitializeCriticalSection(&tdata->cs);
758 tdata->thread_id = GetCurrentThreadId();
760 EnterCriticalSection(&threaddata_cs);
761 list_add_tail(&threaddata_list, &tdata->entry);
762 LeaveCriticalSection(&threaddata_cs);
764 NtCurrentTeb()->ReservedForNtRpc = tdata;
765 return tdata;
767 return tdata;
770 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
772 struct threaddata *tdata = get_or_create_threaddata();
773 if (!tdata) return;
775 EnterCriticalSection(&tdata->cs);
776 tdata->connection = Connection;
777 LeaveCriticalSection(&tdata->cs);
780 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
782 struct threaddata *tdata = get_or_create_threaddata();
783 if (!tdata) return;
785 tdata->server_binding = Binding;
788 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
790 struct threaddata *tdata = get_or_create_threaddata();
791 if (!tdata) return NULL;
793 return tdata->server_binding;
796 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext)
798 struct threaddata *tdata = get_or_create_threaddata();
799 struct context_handle_list *context_handle_list;
801 if (!tdata) return;
803 context_handle_list = HeapAlloc(GetProcessHeap(), 0, sizeof(*context_handle_list));
804 if (!context_handle_list) return;
806 context_handle_list->context_handle = SContext;
807 context_handle_list->next = tdata->context_handle_list;
808 tdata->context_handle_list = context_handle_list;
811 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext)
813 struct threaddata *tdata = get_or_create_threaddata();
814 struct context_handle_list *current, *prev;
816 if (!tdata) return;
818 for (current = tdata->context_handle_list, prev = NULL; current; prev = current, current = current->next)
820 if (current->context_handle == SContext)
822 if (prev)
823 prev->next = current->next;
824 else
825 tdata->context_handle_list = current->next;
826 HeapFree(GetProcessHeap(), 0, current);
827 return;
832 NDR_SCONTEXT RPCRT4_PopThreadContextHandle(void)
834 struct threaddata *tdata = get_or_create_threaddata();
835 struct context_handle_list *context_handle_list;
836 NDR_SCONTEXT context_handle;
838 if (!tdata) return NULL;
840 context_handle_list = tdata->context_handle_list;
841 if (!context_handle_list) return NULL;
842 tdata->context_handle_list = context_handle_list->next;
844 context_handle = context_handle_list->context_handle;
845 HeapFree(GetProcessHeap(), 0, context_handle_list);
846 return context_handle;
849 static RPC_STATUS rpc_cancel_thread(DWORD target_tid)
851 struct threaddata *tdata;
853 EnterCriticalSection(&threaddata_cs);
854 LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
855 if (tdata->thread_id == target_tid)
857 EnterCriticalSection(&tdata->cs);
858 if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
859 LeaveCriticalSection(&tdata->cs);
860 break;
862 LeaveCriticalSection(&threaddata_cs);
864 return RPC_S_OK;
867 /******************************************************************************
868 * RpcCancelThread (rpcrt4.@)
870 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
872 TRACE("(%p)\n", ThreadHandle);
873 return RpcCancelThreadEx(ThreadHandle, 0);
876 /******************************************************************************
877 * RpcCancelThreadEx (rpcrt4.@)
879 RPC_STATUS RPC_ENTRY RpcCancelThreadEx(void* ThreadHandle, LONG Timeout)
881 DWORD target_tid;
883 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
885 target_tid = GetThreadId(ThreadHandle);
886 if (!target_tid)
887 return RPC_S_INVALID_ARG;
889 if (Timeout)
891 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
892 return RPC_S_OK;
894 else
895 return rpc_cancel_thread(target_tid);