1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * COPYRIGHT:   See COPYING in the top level directory
 * PROJECT:     ReactOS WinSock 2 API
 * FILE:        dll/win32/ws2_32_new/src/dcatitem.c
 * PURPOSE:     Transport Catalog Entry Object
 * PROGRAMMER:  Alex Ionescu (alex@relsoft.net)
 */

/* INCLUDES ******************************************************************/

#include <ws2_32.h>

/* FUNCTIONS *****************************************************************/

PTCATALOG_ENTRY
WSAAPI
WsTcEntryAllocate(VOID)
{
    PTCATALOG_ENTRY CatalogEntry;

    /* Allocate the catalog entry */
    CatalogEntry = HeapAlloc(WsSockHeap, HEAP_ZERO_MEMORY, sizeof(*CatalogEntry));
    if (CatalogEntry)
    {
        /* Set the default non-null members */
        CatalogEntry->RefCount = 1;
    }

    /* Return it */
    return CatalogEntry;
}

VOID
WSAAPI
WsTcEntryDelete(IN PTCATALOG_ENTRY CatalogEntry)
{
    /* Check if a provider is loaded */
    if (CatalogEntry->Provider)
    {
        /* Dereference it too */
        WsTpDereference(CatalogEntry->Provider);
        CatalogEntry->Provider = NULL;
    }

    /* Delete us */
    HeapFree(WsSockHeap, 0, CatalogEntry);
}

VOID
WSAAPI
WsTcEntryDereference(IN PTCATALOG_ENTRY CatalogEntry)
{
    /* Dereference and check if it's now 0 */
    if (!(InterlockedDecrement(&CatalogEntry->RefCount)))
    {
        /* We can delete the Provider now */
        WsTcEntryDelete(CatalogEntry);
    }
}

DWORD
WSAAPI
WsTcEntryInitializeFromRegistry(IN PTCATALOG_ENTRY CatalogEntry,
                                IN HKEY ParentKey,
                                IN DWORD UniqueId)
{
    CHAR CatalogEntryName[13];
    DWORD RegSize;
    DWORD RegType = REG_BINARY;
    HKEY EntryKey;
    DWORD Return;
    LPBYTE Buf;
    DWORD index;

    /* Convert to a 00000xxx string */
    sprintf(CatalogEntryName, "%0""12""lu", UniqueId);

    /* Open the Entry */
    Return = RegOpenKeyEx(ParentKey,<--- Return is assigned
                          CatalogEntryName,
                          0,
                          KEY_READ,
                          &EntryKey);

    /* Get Size of Catalog Entry Structure */
    Return = RegQueryValueEx(EntryKey,<--- Return is overwritten<--- Return is assigned
                              "PackedCatalogItem",
                              0,
                              NULL,
                              NULL,
                              &RegSize);

    if (!(Buf = HeapAlloc(WsSockHeap, HEAP_ZERO_MEMORY, RegSize)))
        return ERROR_NOT_ENOUGH_MEMORY;

    /* Read the Whole Catalog Entry Structure */
    Return = RegQueryValueEx(EntryKey,<--- Return is overwritten
                              "PackedCatalogItem",
                              0,
                              &RegType,
                              Buf,
                              &RegSize);


    memcpy(CatalogEntry->DllPath, Buf, sizeof(CatalogEntry->DllPath));
    index = sizeof(CatalogEntry->DllPath);
    if (index < RegSize)
    {
        memcpy(&CatalogEntry->ProtocolInfo, &Buf[index], sizeof(WSAPROTOCOL_INFOW));
        index += sizeof(WSAPROTOCOL_INFOW);
    }
    HeapFree(WsSockHeap, 0, Buf);

    /* Done */
    RegCloseKey(EntryKey);
    return Return;
}

VOID
WSAAPI
WsTcEntrySetProvider(IN PTCATALOG_ENTRY Entry,
                     IN PTPROVIDER Provider)
{
    /* Reference the provider */
    InterlockedIncrement(&Provider->RefCount);

    /* Set it */
    Entry->Provider = Provider;
}