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.
36 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
40 static CRITICAL_SECTION uuid_cs
;
41 static CRITICAL_SECTION_DEBUG critsect_debug
=
44 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
45 0, 0, { (DWORD_PTR
)(__FILE__
": uuid_cs") }
47 static CRITICAL_SECTION uuid_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
49 static CRITICAL_SECTION threaddata_cs
;
50 static CRITICAL_SECTION_DEBUG threaddata_cs_debug
=
53 { &threaddata_cs_debug
.ProcessLocksList
, &threaddata_cs_debug
.ProcessLocksList
},
54 0, 0, { (DWORD_PTR
)(__FILE__
": threaddata_cs") }
56 static CRITICAL_SECTION threaddata_cs
= { &threaddata_cs_debug
, -1, 0, 0, 0, 0 };
58 static struct list threaddata_list
= LIST_INIT(threaddata_list
);
60 struct context_handle_list
62 struct context_handle_list
*next
;
63 NDR_SCONTEXT context_handle
;
71 RpcConnection
*connection
;
72 RpcBinding
*server_binding
;
73 struct context_handle_list
*context_handle_list
;
76 /***********************************************************************
80 * hinstDLL [I] handle to the DLL's instance
82 * lpvReserved [I] reserved, must be NULL
89 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
91 struct threaddata
*tdata
;
94 case DLL_PROCESS_ATTACH
:
97 case DLL_THREAD_DETACH
:
98 tdata
= NtCurrentTeb()->ReservedForNtRpc
;
101 EnterCriticalSection(&threaddata_cs
);
102 list_remove(&tdata
->entry
);
103 LeaveCriticalSection(&threaddata_cs
);
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
);
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
);
127 /*************************************************************************
128 * RpcStringFreeA [RPCRT4.@]
130 * Frees a character string allocated by the RPC run-time library.
134 * S_OK if successful.
136 RPC_STATUS WINAPI
RpcStringFreeA(RPC_CSTR
* String
)
138 HeapFree( GetProcessHeap(), 0, *String
);
143 /*************************************************************************
144 * RpcStringFreeW [RPCRT4.@]
146 * Frees a character string allocated by the RPC run-time library.
150 * S_OK if successful.
152 RPC_STATUS WINAPI
RpcStringFreeW(RPC_WSTR
* String
)
154 HeapFree( GetProcessHeap(), 0, *String
);
159 /*************************************************************************
160 * RpcRaiseException [RPCRT4.@]
162 * Raises an exception.
164 void DECLSPEC_NORETURN WINAPI
RpcRaiseException(RPC_STATUS exception
)
166 /* shouldn't return */
167 RaiseException(exception
, 0, 0, NULL
);
168 ERR("handler continued execution\n");
172 /*************************************************************************
173 * UuidCompare [RPCRT4.@]
176 * UUID *Uuid1 [I] Uuid to compare
177 * UUID *Uuid2 [I] Uuid to compare
178 * RPC_STATUS *Status [O] returns RPC_S_OK
181 * -1 if Uuid1 is less than Uuid2
182 * 0 if Uuid1 and Uuid2 are equal
183 * 1 if Uuid1 is greater than Uuid2
185 int WINAPI
UuidCompare(UUID
*Uuid1
, UUID
*Uuid2
, RPC_STATUS
*Status
)
189 TRACE("(%s,%s)\n", debugstr_guid(Uuid1
), debugstr_guid(Uuid2
));
193 if (!Uuid1
) Uuid1
= &uuid_nil
;
194 if (!Uuid2
) Uuid2
= &uuid_nil
;
196 if (Uuid1
== Uuid2
) return 0;
198 if (Uuid1
->Data1
!= Uuid2
->Data1
)
199 return Uuid1
->Data1
< Uuid2
->Data1
? -1 : 1;
201 if (Uuid1
->Data2
!= Uuid2
->Data2
)
202 return Uuid1
->Data2
< Uuid2
->Data2
? -1 : 1;
204 if (Uuid1
->Data3
!= Uuid2
->Data3
)
205 return Uuid1
->Data3
< Uuid2
->Data3
? -1 : 1;
207 for (i
= 0; i
< 8; i
++) {
208 if (Uuid1
->Data4
[i
] < Uuid2
->Data4
[i
])
210 if (Uuid1
->Data4
[i
] > Uuid2
->Data4
[i
])
217 /*************************************************************************
218 * UuidEqual [RPCRT4.@]
221 * UUID *Uuid1 [I] Uuid to compare
222 * UUID *Uuid2 [I] Uuid to compare
223 * RPC_STATUS *Status [O] returns RPC_S_OK
228 int WINAPI
UuidEqual(UUID
*Uuid1
, UUID
*Uuid2
, RPC_STATUS
*Status
)
230 TRACE("(%s,%s)\n", debugstr_guid(Uuid1
), debugstr_guid(Uuid2
));
231 return !UuidCompare(Uuid1
, Uuid2
, Status
);
234 /*************************************************************************
235 * UuidIsNil [RPCRT4.@]
238 * UUID *Uuid [I] Uuid to compare
239 * RPC_STATUS *Status [O] returns RPC_S_OK
244 int WINAPI
UuidIsNil(UUID
*Uuid
, RPC_STATUS
*Status
)
246 TRACE("(%s)\n", debugstr_guid(Uuid
));
247 if (!Uuid
) return TRUE
;
248 return !UuidCompare(Uuid
, &uuid_nil
, Status
);
251 /*************************************************************************
252 * UuidCreateNil [RPCRT4.@]
255 * UUID *Uuid [O] returns a nil UUID
260 RPC_STATUS WINAPI
UuidCreateNil(UUID
*Uuid
)
266 /*************************************************************************
267 * UuidCreate [RPCRT4.@]
269 * Creates a 128bit UUID.
273 * RPC_S_OK if successful.
274 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
278 * Follows RFC 4122, section 4.4 (Algorithms for Creating a UUID from
279 * Truly Random or Pseudo-Random Numbers)
281 RPC_STATUS WINAPI
UuidCreate(UUID
*Uuid
)
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.
290 Uuid
->Data4
[0] &= 0x3f;
291 Uuid
->Data4
[0] |= 0x80;
293 TRACE("%s\n", debugstr_guid(Uuid
));
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)
307 static void RPC_UuidGetSystemTime(ULONGLONG
*time
)
311 GetSystemTimeAsFileTime(&ft
);
313 *time
= ((ULONGLONG
)ft
.dwHighDateTime
<< 32) | ft
.dwLowDateTime
;
314 *time
+= TICKS_15_OCT_1582_TO_1601
;
317 /* Assume that a hardware address is at least 6 bytes long */
318 #define ADDRESS_BYTES_NEEDED 6
320 static RPC_STATUS
RPC_UuidGetNodeAddress(BYTE
*address
)
323 DWORD status
= RPC_S_OK
;
325 ULONG buflen
= sizeof(IP_ADAPTER_INFO
);
326 PIP_ADAPTER_INFO adapter
= HeapAlloc(GetProcessHeap(), 0, buflen
);
328 if (GetAdaptersInfo(adapter
, &buflen
) == ERROR_BUFFER_OVERFLOW
) {
329 HeapFree(GetProcessHeap(), 0, adapter
);
330 adapter
= HeapAlloc(GetProcessHeap(), 0, buflen
);
333 if (GetAdaptersInfo(adapter
, &buflen
) == NO_ERROR
) {
334 for (i
= 0; i
< ADDRESS_BYTES_NEEDED
; i
++) {
335 address
[i
] = adapter
->Address
[i
];
338 /* We can't get a hardware address, just use random numbers.
339 Set the multicast bit to prevent conflicts with real cards. */
341 RtlGenRandom(address
, ADDRESS_BYTES_NEEDED
);
343 status
= RPC_S_UUID_LOCAL_ONLY
;
346 HeapFree(GetProcessHeap(), 0, adapter
);
350 /*************************************************************************
351 * UuidCreateSequential [RPCRT4.@]
353 * Creates a 128bit UUID.
357 * RPC_S_OK if successful.
358 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
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
366 RPC_STATUS WINAPI
UuidCreateSequential(UUID
*Uuid
)
368 static BOOL initialised
;
372 static ULONGLONG timelast
;
373 static WORD sequence
;
376 static BYTE address
[MAX_ADAPTER_ADDRESS_LENGTH
];
378 EnterCriticalSection(&uuid_cs
);
381 RPC_UuidGetSystemTime(&timelast
);
382 count
= TICKS_PER_CLOCK_TICK
;
384 sequence
= ((rand() & 0xff) << 8) + (rand() & 0xff);
387 status
= RPC_UuidGetNodeAddress(address
);
391 /* Generate time element of the UUID. Account for going faster
392 than our clock as well as the clock going backwards. */
394 RPC_UuidGetSystemTime(&time
);
395 if (time
> timelast
) {
399 if (time
< timelast
) {
400 sequence
= (sequence
+ 1) & 0x1fff;
404 if (count
< TICKS_PER_CLOCK_TICK
) {
413 /* Pack the information into the UUID structure. */
415 Uuid
->Data1
= (ULONG
)(time
& 0xffffffff);
416 Uuid
->Data2
= (unsigned short)((time
>> 32) & 0xffff);
417 Uuid
->Data3
= (unsigned short)((time
>> 48) & 0x0fff);
419 /* This is a version 1 UUID */
420 Uuid
->Data3
|= (1 << 12);
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
);
427 LeaveCriticalSection(&uuid_cs
);
429 TRACE("%s\n", debugstr_guid(Uuid
));
435 /*************************************************************************
436 * UuidHash [RPCRT4.@]
438 * Generates a hash value for a given UUID
440 * Code based on FreeDCE implementation
443 unsigned short WINAPI
UuidHash(UUID
*uuid
, RPC_STATUS
*Status
)
445 BYTE
*data
= (BYTE
*)uuid
;
446 short c0
= 0, c1
= 0, x
, y
;
449 if (!uuid
) data
= (BYTE
*)(uuid
= &uuid_nil
);
451 TRACE("(%s)\n", debugstr_guid(uuid
));
453 for (i
=0; i
<sizeof(UUID
); i
++) {
468 /*************************************************************************
469 * UuidToStringA [RPCRT4.@]
471 * Converts a UUID to a string.
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
478 * S_OK if successful.
479 * S_OUT_OF_MEMORY if unsuccessful.
481 RPC_STATUS WINAPI
UuidToStringA(UUID
*Uuid
, RPC_CSTR
* StringUuid
)
483 *StringUuid
= HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
486 return RPC_S_OUT_OF_MEMORY
;
488 if (!Uuid
) Uuid
= &uuid_nil
;
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] );
499 /*************************************************************************
500 * UuidToStringW [RPCRT4.@]
502 * Converts a UUID to a string.
504 * S_OK if successful.
505 * S_OUT_OF_MEMORY if unsuccessful.
507 RPC_STATUS WINAPI
UuidToStringW(UUID
*Uuid
, RPC_WSTR
* StringUuid
)
511 if (!Uuid
) Uuid
= &uuid_nil
;
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] );
519 *StringUuid
= RPCRT4_strdupAtoW(buf
);
522 return RPC_S_OUT_OF_MEMORY
;
527 static const BYTE hex2bin
[] =
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 */
538 /***********************************************************************
539 * UuidFromStringA (RPCRT4.@)
541 RPC_STATUS WINAPI
UuidFromStringA(RPC_CSTR s
, UUID
*uuid
)
545 if (!s
) return UuidCreateNil( uuid
);
547 if (strlen((char*)s
) != 36) return RPC_S_INVALID_STRING_UUID
;
549 if ((s
[8]!='-') || (s
[13]!='-') || (s
[18]!='-') || (s
[23]!='-'))
550 return RPC_S_INVALID_STRING_UUID
;
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
;
558 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
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]];
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]];
578 /***********************************************************************
579 * UuidFromStringW (RPCRT4.@)
581 RPC_STATUS WINAPI
UuidFromStringW(RPC_WSTR s
, UUID
*uuid
)
585 if (!s
) return UuidCreateNil( uuid
);
587 if (strlenW(s
) != 36) return RPC_S_INVALID_STRING_UUID
;
589 if ((s
[8]!='-') || (s
[13]!='-') || (s
[18]!='-') || (s
[23]!='-'))
590 return RPC_S_INVALID_STRING_UUID
;
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
;
598 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
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]];
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]];
617 /***********************************************************************
618 * DllRegisterServer (RPCRT4.@)
621 HRESULT WINAPI
DllRegisterServer( void )
623 FIXME( "(): stub\n" );
627 #define MAX_RPC_ERROR_TEXT 256
629 /******************************************************************************
630 * DceErrorInqTextW (rpcrt4.@)
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.
641 RPC_STATUS RPC_ENTRY
DceErrorInqTextW (RPC_STATUS e
, RPC_WSTR buffer
)
644 count
= FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM
|
645 FORMAT_MESSAGE_IGNORE_INSERTS
,
646 NULL
, e
, 0, buffer
, MAX_RPC_ERROR_TEXT
, NULL
);
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
);
654 ERR ("Failed to translate error\n");
655 return RPC_S_INVALID_ARG
;
661 /******************************************************************************
662 * DceErrorInqTextA (rpcrt4.@)
664 RPC_STATUS RPC_ENTRY
DceErrorInqTextA (RPC_STATUS e
, RPC_CSTR buffer
)
667 WCHAR bufferW
[MAX_RPC_ERROR_TEXT
];
668 if ((status
= DceErrorInqTextW (e
, bufferW
)) == RPC_S_OK
)
670 if (!WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, (LPSTR
)buffer
, MAX_RPC_ERROR_TEXT
,
673 ERR ("Failed to translate error\n");
674 status
= RPC_S_INVALID_ARG
;
680 /******************************************************************************
681 * I_RpcAllocate (rpcrt4.@)
683 void * WINAPI
I_RpcAllocate(unsigned int Size
)
685 return HeapAlloc(GetProcessHeap(), 0, Size
);
688 /******************************************************************************
689 * I_RpcFree (rpcrt4.@)
691 void WINAPI
I_RpcFree(void *Object
)
693 HeapFree(GetProcessHeap(), 0, Object
);
696 /******************************************************************************
697 * I_RpcMapWin32Status (rpcrt4.@)
699 * Maps Win32 RPC error codes to NT statuses.
702 * status [I] Win32 RPC error code.
705 * Appropriate translation into an NT status code.
707 LONG WINAPI
I_RpcMapWin32Status(RPC_STATUS status
)
709 TRACE("(%d)\n", status
);
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
;
822 /******************************************************************************
823 * I_RpcExceptionFilter (rpcrt4.@)
825 int WINAPI
I_RpcExceptionFilter(ULONG ExceptionCode
)
827 TRACE("0x%x\n", ExceptionCode
);
828 switch (ExceptionCode
)
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
;
840 return EXCEPTION_EXECUTE_HANDLER
;
844 /******************************************************************************
845 * RpcErrorStartEnumeration (rpcrt4.@)
847 RPC_STATUS RPC_ENTRY
RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE
* EnumHandle
)
849 FIXME("(%p): stub\n", EnumHandle
);
850 return RPC_S_ENTRY_NOT_FOUND
;
853 /******************************************************************************
854 * RpcErrorEndEnumeration (rpcrt4.@)
856 RPC_STATUS RPC_ENTRY
RpcErrorEndEnumeration(RPC_ERROR_ENUM_HANDLE
* EnumHandle
)
858 FIXME("(%p): stub\n", EnumHandle
);
862 /******************************************************************************
863 * RpcErrorSaveErrorInfo (rpcrt4.@)
865 RPC_STATUS RPC_ENTRY
RpcErrorSaveErrorInfo(RPC_ERROR_ENUM_HANDLE
*EnumHandle
, void **ErrorBlob
, SIZE_T
*BlobSize
)
867 FIXME("(%p %p %p): stub\n", EnumHandle
, ErrorBlob
, BlobSize
);
868 return ERROR_CALL_NOT_IMPLEMENTED
;
871 /******************************************************************************
872 * RpcErrorLoadErrorInfo (rpcrt4.@)
874 RPC_STATUS RPC_ENTRY
RpcErrorLoadErrorInfo(void *ErrorBlob
, SIZE_T BlobSize
, RPC_ERROR_ENUM_HANDLE
*EnumHandle
)
876 FIXME("(%p %lu %p): stub\n", ErrorBlob
, BlobSize
, EnumHandle
);
877 return ERROR_CALL_NOT_IMPLEMENTED
;
880 /******************************************************************************
881 * RpcErrorGetNextRecord (rpcrt4.@)
883 RPC_STATUS RPC_ENTRY
RpcErrorGetNextRecord(RPC_ERROR_ENUM_HANDLE
*EnumHandle
, BOOL CopyStrings
, RPC_EXTENDED_ERROR_INFO
*ErrorInfo
)
885 FIXME("(%p %x %p): stub\n", EnumHandle
, CopyStrings
, ErrorInfo
);
886 return RPC_S_ENTRY_NOT_FOUND
;
889 /******************************************************************************
890 * RpcMgmtSetCancelTimeout (rpcrt4.@)
892 RPC_STATUS RPC_ENTRY
RpcMgmtSetCancelTimeout(LONG Timeout
)
894 FIXME("(%d): stub\n", Timeout
);
898 static struct threaddata
*get_or_create_threaddata(void)
900 struct threaddata
*tdata
= NtCurrentTeb()->ReservedForNtRpc
;
903 tdata
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*tdata
));
904 if (!tdata
) return NULL
;
906 InitializeCriticalSection(&tdata
->cs
);
907 tdata
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": threaddata.cs");
908 tdata
->thread_id
= GetCurrentThreadId();
910 EnterCriticalSection(&threaddata_cs
);
911 list_add_tail(&threaddata_list
, &tdata
->entry
);
912 LeaveCriticalSection(&threaddata_cs
);
914 NtCurrentTeb()->ReservedForNtRpc
= tdata
;
920 void RPCRT4_SetThreadCurrentConnection(RpcConnection
*Connection
)
922 struct threaddata
*tdata
= get_or_create_threaddata();
925 EnterCriticalSection(&tdata
->cs
);
926 tdata
->connection
= Connection
;
927 LeaveCriticalSection(&tdata
->cs
);
930 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding
*Binding
)
932 struct threaddata
*tdata
= get_or_create_threaddata();
935 tdata
->server_binding
= Binding
;
938 RpcBinding
*RPCRT4_GetThreadCurrentCallHandle(void)
940 struct threaddata
*tdata
= get_or_create_threaddata();
941 if (!tdata
) return NULL
;
943 return tdata
->server_binding
;
946 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext
)
948 struct threaddata
*tdata
= get_or_create_threaddata();
949 struct context_handle_list
*context_handle_list
;
953 context_handle_list
= HeapAlloc(GetProcessHeap(), 0, sizeof(*context_handle_list
));
954 if (!context_handle_list
) return;
956 context_handle_list
->context_handle
= SContext
;
957 context_handle_list
->next
= tdata
->context_handle_list
;
958 tdata
->context_handle_list
= context_handle_list
;
961 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext
)
963 struct threaddata
*tdata
= get_or_create_threaddata();
964 struct context_handle_list
*current
, *prev
;
968 for (current
= tdata
->context_handle_list
, prev
= NULL
; current
; prev
= current
, current
= current
->next
)
970 if (current
->context_handle
== SContext
)
973 prev
->next
= current
->next
;
975 tdata
->context_handle_list
= current
->next
;
976 HeapFree(GetProcessHeap(), 0, current
);
982 NDR_SCONTEXT
RPCRT4_PopThreadContextHandle(void)
984 struct threaddata
*tdata
= get_or_create_threaddata();
985 struct context_handle_list
*context_handle_list
;
986 NDR_SCONTEXT context_handle
;
988 if (!tdata
) return NULL
;
990 context_handle_list
= tdata
->context_handle_list
;
991 if (!context_handle_list
) return NULL
;
992 tdata
->context_handle_list
= context_handle_list
->next
;
994 context_handle
= context_handle_list
->context_handle
;
995 HeapFree(GetProcessHeap(), 0, context_handle_list
);
996 return context_handle
;
999 static RPC_STATUS
rpc_cancel_thread(DWORD target_tid
)
1001 struct threaddata
*tdata
;
1003 EnterCriticalSection(&threaddata_cs
);
1004 LIST_FOR_EACH_ENTRY(tdata
, &threaddata_list
, struct threaddata
, entry
)
1005 if (tdata
->thread_id
== target_tid
)
1007 EnterCriticalSection(&tdata
->cs
);
1008 if (tdata
->connection
) rpcrt4_conn_cancel_call(tdata
->connection
);
1009 LeaveCriticalSection(&tdata
->cs
);
1012 LeaveCriticalSection(&threaddata_cs
);
1017 /******************************************************************************
1018 * RpcCancelThread (rpcrt4.@)
1020 RPC_STATUS RPC_ENTRY
RpcCancelThread(void* ThreadHandle
)
1022 TRACE("(%p)\n", ThreadHandle
);
1023 return RpcCancelThreadEx(ThreadHandle
, 0);
1026 /******************************************************************************
1027 * RpcCancelThreadEx (rpcrt4.@)
1029 RPC_STATUS RPC_ENTRY
RpcCancelThreadEx(void* ThreadHandle
, LONG Timeout
)
1033 FIXME("(%p, %d)\n", ThreadHandle
, Timeout
);
1035 target_tid
= GetThreadId(ThreadHandle
);
1037 return RPC_S_INVALID_ARG
;
1041 FIXME("(%p, %d)\n", ThreadHandle
, Timeout
);
1045 return rpc_cancel_thread(target_tid
);