xref: /reactos/dll/win32/ws2_32/src/nscatent.c (revision 682f85ad)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS WinSock 2 API
4  * FILE:        dll/win32/ws2_32/src/nscatent.c
5  * PURPOSE:     Namespace 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 PNSCATALOG_ENTRY
16 WSAAPI
WsNcEntryAllocate(VOID)17 WsNcEntryAllocate(VOID)
18 {
19     PNSCATALOG_ENTRY CatalogEntry;
20 
21     /* Allocate the catalog */
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         CatalogEntry->Enabled = TRUE;
28         CatalogEntry->AddressFamily = -1;
29     }
30 
31     /* Return it */
32     return CatalogEntry;
33 }
34 
35 VOID
36 WSAAPI
WsNcEntryDelete(IN PNSCATALOG_ENTRY CatalogEntry)37 WsNcEntryDelete(IN PNSCATALOG_ENTRY CatalogEntry)
38 {
39     /* Check if a provider is loaded */
40     if (CatalogEntry->Provider)
41     {
42         /* Dereference it too */
43         WsNpDereference(CatalogEntry->Provider);
44         CatalogEntry->Provider = NULL;
45     }
46 
47     /* Delete us */
48     HeapFree(WsSockHeap, 0, CatalogEntry);
49 }
50 
51 VOID
52 WSAAPI
WsNcEntryDereference(IN PNSCATALOG_ENTRY CatalogEntry)53 WsNcEntryDereference(IN PNSCATALOG_ENTRY CatalogEntry)
54 {
55     /* Dereference and check if it's now 0 */
56     if (!(InterlockedDecrement(&CatalogEntry->RefCount)))
57     {
58         /* We can delete the Provider now */
59         WsNcEntryDelete(CatalogEntry);
60     }
61 }
62 
63 INT
64 WSAAPI
WsNcEntryInitializeFromRegistry(IN PNSCATALOG_ENTRY CatalogEntry,IN HKEY ParentKey,IN ULONG UniqueId)65 WsNcEntryInitializeFromRegistry(IN PNSCATALOG_ENTRY CatalogEntry,
66                                 IN HKEY ParentKey,
67                                 IN ULONG UniqueId)
68 {
69     INT ErrorCode;
70     CHAR CatalogEntryName[13];
71     HKEY EntryKey;
72     ULONG RegType = REG_SZ;
73     ULONG RegSize = MAX_PATH;
74     ULONG RegValue;
75 
76     /* Convert to a 00000xxx string */
77     sprintf(CatalogEntryName, "%0""12""i", (int)UniqueId);
78 
79     /* Open the Entry */
80     ErrorCode = RegOpenKeyEx(ParentKey,
81                              CatalogEntryName,
82                              0,
83                              KEY_READ,
84                              &EntryKey);
85     if (ErrorCode != ERROR_SUCCESS) return ErrorCode;
86     /* Read the Library Path */
87     ErrorCode = RegQueryValueExW(EntryKey,
88                                  L"LibraryPath",
89                                  0,
90                                  &RegType,
91                                  (LPBYTE)&CatalogEntry->DllPath,
92                                  &RegSize);
93     if (ErrorCode != ERROR_SUCCESS) goto out;
94     /* Query Display String Size*/
95     ErrorCode = RegQueryValueExW(EntryKey,
96                                  L"DisplayString",
97                                  0,
98                                  NULL,
99                                  NULL,
100                                  &RegSize);
101     if (ErrorCode != ERROR_SUCCESS) goto out;
102     /* Allocate it */
103     CatalogEntry->ProviderName = (LPWSTR)HeapAlloc(WsSockHeap, 0, RegSize);
104 
105     /* Read it */
106     ErrorCode = RegQueryValueExW(EntryKey,
107                                  L"DisplayString",
108                                  0,
109                                  &RegType,
110                                  (LPBYTE)CatalogEntry->ProviderName,
111                                  &RegSize);
112     if (ErrorCode != ERROR_SUCCESS) goto out;
113     /* Read the Provider Id */
114     RegType = REG_BINARY;
115     RegSize = sizeof(GUID);
116     ErrorCode = RegQueryValueEx(EntryKey,
117                                 "ProviderId",
118                                 0,
119                                 &RegType,
120                                 (LPBYTE)&CatalogEntry->ProviderId,
121                                 &RegSize);
122     if (ErrorCode != ERROR_SUCCESS) goto out;
123     /* Read the Address Family */
124     RegType = REG_DWORD;
125     RegSize = sizeof(DWORD);
126     ErrorCode = RegQueryValueEx(EntryKey,
127                                 "AddressFamily",
128                                 0,
129                                 &RegType,
130                                 (LPBYTE)&CatalogEntry->AddressFamily,
131                                 &RegSize);
132     if (ErrorCode != ERROR_SUCCESS) goto out;
133     /* Read the Namespace Id */
134     ErrorCode = RegQueryValueEx(EntryKey,
135                                 "SupportedNamespace",
136                                 0,
137                                 &RegType,
138                                 (LPBYTE)&CatalogEntry->NamespaceId,
139                                 &RegSize);
140     if (ErrorCode != ERROR_SUCCESS) goto out;
141     /* Read the Enabled Flag */
142     ErrorCode = RegQueryValueEx(EntryKey,
143                                 "Enabled",
144                                 0,
145                                 &RegType,
146                                 (LPBYTE)&RegValue,
147                                 &RegSize);
148     if (ErrorCode != ERROR_SUCCESS) goto out;
149     CatalogEntry->Enabled = RegValue != 0;
150 
151     /* Read the Version */
152     ErrorCode = RegQueryValueEx(EntryKey,
153                                 "Version",
154                                 0,
155                                 &RegType,
156                                 (LPBYTE)&CatalogEntry->Version,
157                                 &RegSize);
158     if (ErrorCode != ERROR_SUCCESS) goto out;
159     /* Read the Support Service Class Info Flag */
160     ErrorCode = RegQueryValueEx(EntryKey,
161                                 "StoresServiceClassInfo",
162                                 0,
163                                 &RegType,
164                                 (LPBYTE)&RegValue,
165                                 &RegSize);
166     CatalogEntry->StoresServiceClassInfo = RegValue != 0;
167 out:
168     /* Done */
169     RegCloseKey(EntryKey);
170     return ErrorCode;
171 }
172 
173 VOID
174 WSAAPI
WsNcEntrySetProvider(IN PNSCATALOG_ENTRY Entry,IN PNS_PROVIDER Provider)175 WsNcEntrySetProvider(IN PNSCATALOG_ENTRY Entry,
176                      IN PNS_PROVIDER Provider)
177 {
178     /* Reference the provider */
179     InterlockedIncrement(&Provider->RefCount);
180 
181     /* Set it */
182     Entry->Provider = Provider;
183 }
184