xref: /reactos/dll/win32/ws2_32/src/dcatitem.c (revision 40462c92)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS WinSock 2 API
4  * FILE:        dll/win32/ws2_32_new/src/dcatitem.c
5  * PURPOSE:     Transport Catalog Entry Object
6  * PROGRAMMER:  Alex Ionescu (alex@relsoft.net)
7  */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include <ws2_32.h>
12 
13 /* FUNCTIONS *****************************************************************/
14 
15 PTCATALOG_ENTRY
16 WSAAPI
17 WsTcEntryAllocate(VOID)
18 {
19     PTCATALOG_ENTRY CatalogEntry;
20 
21     /* Allocate the catalog entry */
22     CatalogEntry = HeapAlloc(WsSockHeap, HEAP_ZERO_MEMORY, sizeof(*CatalogEntry));
23     if (CatalogEntry)
24     {
25         /* Set the default non-null members */
26         CatalogEntry->RefCount = 1;
27     }
28 
29     /* Return it */
30     return CatalogEntry;
31 }
32 
33 VOID
34 WSAAPI
35 WsTcEntryDelete(IN PTCATALOG_ENTRY CatalogEntry)
36 {
37     /* Check if a provider is loaded */
38     if (CatalogEntry->Provider)
39     {
40         /* Dereference it too */
41         WsTpDereference(CatalogEntry->Provider);
42         CatalogEntry->Provider = NULL;
43     }
44 
45     /* Delete us */
46     HeapFree(WsSockHeap, 0, CatalogEntry);
47 }
48 
49 VOID
50 WSAAPI
51 WsTcEntryDereference(IN PTCATALOG_ENTRY CatalogEntry)
52 {
53     /* Dereference and check if it's now 0 */
54     if (!(InterlockedDecrement(&CatalogEntry->RefCount)))
55     {
56         /* We can delete the Provider now */
57         WsTcEntryDelete(CatalogEntry);
58     }
59 }
60 
61 DWORD
62 WSAAPI
63 WsTcEntryInitializeFromRegistry(IN PTCATALOG_ENTRY CatalogEntry,
64                                 IN HKEY ParentKey,
65                                 IN DWORD UniqueId)
66 {
67     CHAR CatalogEntryName[13];
68     DWORD RegSize;
69     DWORD RegType = REG_BINARY;
70     HKEY EntryKey;
71     DWORD Return;
72     LPBYTE Buf;
73     DWORD index;
74 
75     /* Convert to a 00000xxx string */
76     sprintf(CatalogEntryName, "%0""12""lu", UniqueId);
77 
78     /* Open the Entry */
79     Return = RegOpenKeyEx(ParentKey,
80                           CatalogEntryName,
81                           0,
82                           KEY_READ,
83                           &EntryKey);
84 
85     /* Get Size of Catalog Entry Structure */
86     Return = RegQueryValueEx(EntryKey,
87                               "PackedCatalogItem",
88                               0,
89                               NULL,
90                               NULL,
91                               &RegSize);
92 
93     if (!(Buf = HeapAlloc(WsSockHeap, HEAP_ZERO_MEMORY, RegSize)))
94         return ERROR_NOT_ENOUGH_MEMORY;
95 
96     /* Read the Whole Catalog Entry Structure */
97     Return = RegQueryValueEx(EntryKey,
98                               "PackedCatalogItem",
99                               0,
100                               &RegType,
101                               Buf,
102                               &RegSize);
103 
104 
105     memcpy(CatalogEntry->DllPath, Buf, sizeof(CatalogEntry->DllPath));
106     index = sizeof(CatalogEntry->DllPath);
107     if (index < RegSize)
108     {
109         memcpy(&CatalogEntry->ProtocolInfo, &Buf[index], sizeof(WSAPROTOCOL_INFOW));
110         index += sizeof(WSAPROTOCOL_INFOW);
111     }
112     HeapFree(WsSockHeap, 0, Buf);
113 
114     /* Done */
115     RegCloseKey(EntryKey);
116     return Return;
117 }
118 
119 VOID
120 WSAAPI
121 WsTcEntrySetProvider(IN PTCATALOG_ENTRY Entry,
122                      IN PTPROVIDER Provider)
123 {
124     /* Reference the provider */
125     InterlockedIncrement(&Provider->RefCount);
126 
127     /* Set it */
128     Entry->Provider = Provider;
129 }
130