2001-01-14 17:44:38 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS WinSock 2 DLL
|
|
|
|
* FILE: misc/handle.c
|
|
|
|
* PURPOSE: Provider handle management
|
|
|
|
* PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
|
|
|
|
* REVISIONS:
|
|
|
|
* CSH 01/09-2000 Created
|
|
|
|
*/
|
|
|
|
#include <ws2_32.h>
|
|
|
|
#include <handle.h>
|
|
|
|
#include <catalog.h>
|
|
|
|
|
|
|
|
PPROVIDER_HANDLE_BLOCK ProviderHandleTable;
|
|
|
|
CRITICAL_SECTION ProviderHandleTableLock;
|
|
|
|
|
|
|
|
PPROVIDER_HANDLE
|
|
|
|
GetProviderByHandle(
|
|
|
|
PPROVIDER_HANDLE_BLOCK HandleTable,
|
|
|
|
HANDLE Handle)
|
|
|
|
/*
|
|
|
|
* FUNCTION: Get the data structure for a handle
|
|
|
|
* ARGUMENTS:
|
|
|
|
* HandleTable = Pointer to handle table
|
|
|
|
* Handle = Handle to get data structure for
|
|
|
|
* RETURNS:
|
|
|
|
* Pointer to the data structure identified by the handle on success,
|
|
|
|
* NULL on failure
|
|
|
|
*/
|
|
|
|
{
|
2001-07-04 20:40:24 +00:00
|
|
|
PPROVIDER_HANDLE_BLOCK Current;
|
|
|
|
PLIST_ENTRY CurrentEntry;
|
|
|
|
ULONG i;
|
|
|
|
|
|
|
|
WS_DbgPrint(MAX_TRACE, ("HandleTable (0x%X) Handle (0x%X).\n", HandleTable, Handle));
|
|
|
|
|
|
|
|
CurrentEntry = HandleTable->Entry.Flink;
|
|
|
|
|
|
|
|
while (CurrentEntry != &HandleTable->Entry) {
|
|
|
|
Current = CONTAINING_RECORD(CurrentEntry, PROVIDER_HANDLE_BLOCK, Entry);
|
|
|
|
|
|
|
|
for (i = 0; i < HANDLE_BLOCK_ENTRIES; i++) {
|
|
|
|
if ((Current->Handles[i].Provider != NULL) &&
|
|
|
|
(Current->Handles[i].Handle == Handle)) {
|
|
|
|
return &Current->Handles[i];
|
|
|
|
}
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
2001-07-04 20:40:24 +00:00
|
|
|
CurrentEntry = CurrentEntry->Flink;
|
|
|
|
}
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
return NULL;
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VOID
|
2001-07-04 20:40:24 +00:00
|
|
|
CloseAllHandles(
|
|
|
|
PPROVIDER_HANDLE_BLOCK HandleTable)
|
2001-01-14 17:44:38 +00:00
|
|
|
{
|
2001-07-04 20:40:24 +00:00
|
|
|
PPROVIDER_HANDLE_BLOCK Current;
|
|
|
|
PLIST_ENTRY CurrentEntry;
|
|
|
|
PCATALOG_ENTRY Provider;
|
|
|
|
ULONG i;
|
|
|
|
|
|
|
|
WS_DbgPrint(MAX_TRACE, ("HandleTable (0x%X).\n", HandleTable));
|
|
|
|
|
|
|
|
CurrentEntry = HandleTable->Entry.Flink;
|
|
|
|
|
|
|
|
while (CurrentEntry != &HandleTable->Entry) {
|
|
|
|
Current = CONTAINING_RECORD(CurrentEntry, PROVIDER_HANDLE_BLOCK, Entry);
|
|
|
|
|
|
|
|
for (i = 0; i < HANDLE_BLOCK_ENTRIES; i++) {
|
|
|
|
Provider = Current->Handles[i].Provider;
|
|
|
|
if (Provider != NULL) {
|
|
|
|
DereferenceProviderByPointer(Provider);
|
|
|
|
Current->Handles[i].Handle = (HANDLE)0;
|
|
|
|
Current->Handles[i].Provider = NULL;
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
2001-07-04 20:40:24 +00:00
|
|
|
}
|
|
|
|
CurrentEntry = CurrentEntry->Flink;
|
|
|
|
}
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VOID
|
2001-07-04 20:40:24 +00:00
|
|
|
DeleteHandleTable(
|
|
|
|
PPROVIDER_HANDLE_BLOCK HandleTable)
|
2001-01-14 17:44:38 +00:00
|
|
|
{
|
2001-07-04 20:40:24 +00:00
|
|
|
PPROVIDER_HANDLE_BLOCK Current;
|
|
|
|
PLIST_ENTRY CurrentEntry;
|
|
|
|
|
|
|
|
CloseAllHandles(HandleTable);
|
|
|
|
|
|
|
|
CurrentEntry = RemoveHeadList(&HandleTable->Entry);
|
|
|
|
|
|
|
|
while (CurrentEntry != &HandleTable->Entry) {
|
|
|
|
Current = CONTAINING_RECORD(
|
|
|
|
CurrentEntry,
|
|
|
|
PROVIDER_HANDLE_BLOCK,
|
|
|
|
Entry);
|
|
|
|
|
|
|
|
HeapFree(GlobalHeap, 0, Current);
|
|
|
|
|
|
|
|
CurrentEntry = RemoveHeadList(&HandleTable->Entry);
|
|
|
|
}
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PCATALOG_ENTRY
|
|
|
|
DeleteProviderHandle(PPROVIDER_HANDLE_BLOCK HandleTable,
|
|
|
|
HANDLE Handle)
|
|
|
|
{
|
2001-07-04 20:40:24 +00:00
|
|
|
PPROVIDER_HANDLE Entry;
|
|
|
|
PCATALOG_ENTRY Provider;
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
WS_DbgPrint(MAX_TRACE, ("HandleTable (0x%X) Handle (0x%X).\n", HandleTable, Handle));
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
Entry = GetProviderByHandle(HandleTable, Handle);
|
|
|
|
if (!Entry)
|
|
|
|
return NULL;
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
Provider = Entry->Provider;
|
|
|
|
Entry->Handle = (HANDLE)0;
|
|
|
|
Entry->Provider = NULL;
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
return Provider;
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HANDLE
|
2001-07-04 20:40:24 +00:00
|
|
|
CreateProviderHandleTable(
|
|
|
|
PPROVIDER_HANDLE_BLOCK HandleTable,
|
|
|
|
HANDLE Handle,
|
|
|
|
PCATALOG_ENTRY Provider)
|
2001-01-14 17:44:38 +00:00
|
|
|
{
|
2001-07-04 20:40:24 +00:00
|
|
|
PPROVIDER_HANDLE_BLOCK NewBlock;
|
|
|
|
PLIST_ENTRY CurrentEntry;
|
|
|
|
ULONG i;
|
|
|
|
|
|
|
|
WS_DbgPrint(MAX_TRACE, ("HandleTable (0x%X) Handle (0x%X) Provider (0x%X).\n",
|
|
|
|
HandleTable, Handle, Provider));
|
|
|
|
|
|
|
|
/* Scan through the currently allocated handle blocks looking for a free slot */
|
|
|
|
CurrentEntry = HandleTable->Entry.Flink;
|
|
|
|
while (CurrentEntry != &HandleTable->Entry) {
|
|
|
|
PPROVIDER_HANDLE_BLOCK Block = CONTAINING_RECORD(
|
|
|
|
CurrentEntry, PROVIDER_HANDLE_BLOCK, Entry);
|
|
|
|
|
|
|
|
for (i = 0; i < HANDLE_BLOCK_ENTRIES; i++) {
|
|
|
|
WS_DbgPrint(MAX_TRACE, ("Considering slot %ld containing 0x%X.\n",
|
|
|
|
i, Block->Handles[i].Provider));
|
|
|
|
if (Block->Handles[i].Provider == NULL) {
|
|
|
|
Block->Handles[i].Handle = Handle;
|
|
|
|
Block->Handles[i].Provider = Provider;
|
|
|
|
return Handle;
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
2001-06-04 11:26:13 +00:00
|
|
|
}
|
2001-07-04 20:40:24 +00:00
|
|
|
CurrentEntry = CurrentEntry->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a new handle block to the end of the list */
|
|
|
|
NewBlock = (PPROVIDER_HANDLE_BLOCK)HeapAlloc(
|
|
|
|
GlobalHeap, 0, sizeof(PROVIDER_HANDLE_BLOCK));
|
|
|
|
|
|
|
|
if (!NewBlock)
|
|
|
|
return (HANDLE)0;
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
ZeroMemory(NewBlock, sizeof(PROVIDER_HANDLE_BLOCK));
|
|
|
|
InsertTailList(&HandleTable->Entry, &NewBlock->Entry);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
NewBlock->Handles[0].Handle = Handle;
|
|
|
|
NewBlock->Handles[0].Provider = Provider;
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
return Handle;
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HANDLE
|
2001-07-04 20:40:24 +00:00
|
|
|
CreateProviderHandle(
|
|
|
|
HANDLE Handle,
|
|
|
|
PCATALOG_ENTRY Provider)
|
2001-01-14 17:44:38 +00:00
|
|
|
{
|
2001-07-04 20:40:24 +00:00
|
|
|
HANDLE h;
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
EnterCriticalSection(&ProviderHandleTableLock);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
h = CreateProviderHandleTable(ProviderHandleTable, Handle, Provider);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
LeaveCriticalSection(&ProviderHandleTableLock);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
if (h != NULL)
|
|
|
|
ReferenceProviderByPointer(Provider);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
return h;
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOL
|
2001-07-04 20:40:24 +00:00
|
|
|
ReferenceProviderByHandle(
|
|
|
|
HANDLE Handle,
|
|
|
|
PCATALOG_ENTRY* Provider)
|
2001-01-14 17:44:38 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Increments the reference count for a provider and returns a pointer to it
|
|
|
|
* ARGUMENTS:
|
|
|
|
* Handle = Handle for the provider
|
|
|
|
* Provider = Address of buffer to place pointer to provider
|
|
|
|
* RETURNS:
|
|
|
|
* TRUE if handle was valid, FALSE if not
|
|
|
|
*/
|
|
|
|
{
|
2001-07-04 20:40:24 +00:00
|
|
|
PPROVIDER_HANDLE ProviderHandle;
|
2001-01-14 17:44:38 +00:00
|
|
|
|
|
|
|
WS_DbgPrint(MAX_TRACE, ("Handle (0x%X) Provider (0x%X).\n", Handle, Provider));
|
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
EnterCriticalSection(&ProviderHandleTableLock);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
ProviderHandle = GetProviderByHandle(ProviderHandleTable, Handle);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
LeaveCriticalSection(&ProviderHandleTableLock);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
if (ProviderHandle) {
|
|
|
|
ReferenceProviderByPointer(ProviderHandle->Provider);
|
|
|
|
*Provider = ProviderHandle->Provider;
|
|
|
|
}
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
return (ProviderHandle != NULL);
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOL
|
2001-07-04 20:40:24 +00:00
|
|
|
CloseProviderHandle(
|
|
|
|
HANDLE Handle)
|
2001-01-14 17:44:38 +00:00
|
|
|
{
|
2001-07-04 20:40:24 +00:00
|
|
|
PCATALOG_ENTRY Provider;
|
|
|
|
|
|
|
|
WS_DbgPrint(MAX_TRACE, ("Handle (0x%X).\n", Handle));
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
EnterCriticalSection(&ProviderHandleTableLock);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
Provider = DeleteProviderHandle(ProviderHandleTable, Handle);
|
|
|
|
if (!Provider)
|
|
|
|
return FALSE;
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
LeaveCriticalSection(&ProviderHandleTableLock);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
DereferenceProviderByPointer(Provider);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
return TRUE;
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
InitProviderHandleTable(VOID)
|
|
|
|
{
|
2001-07-04 20:40:24 +00:00
|
|
|
ProviderHandleTable = (PPROVIDER_HANDLE_BLOCK)
|
|
|
|
HeapAlloc(GlobalHeap, 0, sizeof(PROVIDER_HANDLE_BLOCK));
|
|
|
|
if (!ProviderHandleTable)
|
|
|
|
return FALSE;
|
2001-05-01 22:44:07 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
ZeroMemory(ProviderHandleTable, sizeof(PROVIDER_HANDLE_BLOCK));
|
2001-05-01 22:44:07 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
InitializeListHead(&ProviderHandleTable->Entry);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
InitializeCriticalSection(&ProviderHandleTableLock);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
return TRUE;
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VOID
|
|
|
|
FreeProviderHandleTable(VOID)
|
|
|
|
{
|
2001-07-04 20:40:24 +00:00
|
|
|
DeleteHandleTable(ProviderHandleTable);
|
2001-01-14 17:44:38 +00:00
|
|
|
|
2001-07-04 20:40:24 +00:00
|
|
|
DeleteCriticalSection(&ProviderHandleTableLock);
|
2001-01-14 17:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|