2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 DLL
5 * PURPOSE: Service Provider Catalog
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * CSH 01/09-2000 Created
14 LIST_ENTRY CatalogListHead;
15 CRITICAL_SECTION CatalogLock;
17 VOID ReferenceProviderByPointer(
18 PCATALOG_ENTRY Provider)
20 WS_DbgPrint(MAX_TRACE, ("Provider (0x%X).\n", Provider));
22 //EnterCriticalSection(&Provider->Lock);
23 Provider->ReferenceCount++;
24 //LeaveCriticalSection(&Provider->Lock);
28 VOID DereferenceProviderByPointer(
29 PCATALOG_ENTRY Provider)
31 WS_DbgPrint(MAX_TRACE, ("Provider (0x%X).\n", Provider));
34 if (Provider->ReferenceCount <= 0) {
35 WS_DbgPrint(MIN_TRACE, ("Provider at 0x%X has invalid reference count (%ld).\n",
36 Provider, Provider->ReferenceCount));
40 //EnterCriticalSection(&Provider->Lock);
41 Provider->ReferenceCount--;
42 //LeaveCriticalSection(&Provider->Lock);
44 if (Provider->ReferenceCount == 0) {
45 WS_DbgPrint(MAX_TRACE, ("Provider at 0x%X has reference count 0 (unloading).\n",
48 DestroyCatalogEntry(Provider);
53 PCATALOG_ENTRY CreateCatalogEntry(
56 PCATALOG_ENTRY Provider;
58 WS_DbgPrint(MAX_TRACE, ("LibraryName (%S).\n", LibraryName));
60 Provider = HeapAlloc(GlobalHeap, 0, sizeof(CATALOG_ENTRY));
65 ZeroMemory(Provider, sizeof(CATALOG_ENTRY));
67 if (!RtlCreateUnicodeString(&Provider->LibraryName, LibraryName)) {
68 RtlFreeHeap(GlobalHeap, 0, Provider);
72 Provider->ReferenceCount = 1;
74 InitializeCriticalSection(&Provider->Lock);
75 Provider->hModule = (HMODULE)INVALID_HANDLE_VALUE;
77 Provider->Mapping = NULL;
79 //EnterCriticalSection(&CatalogLock);
81 InsertTailList(&CatalogListHead, &Provider->ListEntry);
83 //LeaveCriticalSection(&CatalogLock);
89 INT DestroyCatalogEntry(
90 PCATALOG_ENTRY Provider)
94 WS_DbgPrint(MAX_TRACE, ("Provider (0x%X).\n", Provider));
96 //EnterCriticalSection(&CatalogLock);
97 RemoveEntryList(&Provider->ListEntry);
98 //LeaveCriticalSection(&CatalogLock);
100 HeapFree(GlobalHeap, 0, Provider->Mapping);
102 if (Provider->hModule) {
103 Status = UnloadProvider(Provider);
108 //DeleteCriticalSection(&Provider->Lock);
110 HeapFree(GlobalHeap, 0, Provider);
116 PCATALOG_ENTRY LocateProvider(
117 LPWSAPROTOCOL_INFOW lpProtocolInfo)
119 PLIST_ENTRY CurrentEntry;
120 PCATALOG_ENTRY Provider;
123 WS_DbgPrint(MAX_TRACE, ("lpProtocolInfo (0x%X).\n", lpProtocolInfo));
125 //EnterCriticalSection(&CatalogLock);
127 CurrentEntry = CatalogListHead.Flink;
128 while (CurrentEntry != &CatalogListHead) {
129 Provider = CONTAINING_RECORD(CurrentEntry,
133 for (i = 0; i < Provider->Mapping->Rows; i++) {
134 if ((lpProtocolInfo->iAddressFamily == Provider->Mapping->Mapping[i].AddressFamily) &&
135 (lpProtocolInfo->iSocketType == Provider->Mapping->Mapping[i].SocketType) &&
136 ((lpProtocolInfo->iProtocol == Provider->Mapping->Mapping[i].Protocol) ||
137 (lpProtocolInfo->iSocketType == SOCK_RAW))) {
138 //LeaveCriticalSection(&CatalogLock);
139 WS_DbgPrint(MID_TRACE, ("Returning provider at (0x%X).\n", Provider));
144 CurrentEntry = CurrentEntry->Flink;
147 //LeaveCriticalSection(&CatalogLock);
153 PCATALOG_ENTRY LocateProviderById(
154 DWORD CatalogEntryId)
156 PLIST_ENTRY CurrentEntry;
157 PCATALOG_ENTRY Provider;
160 WS_DbgPrint(MAX_TRACE, ("CatalogEntryId (%d).\n", CatalogEntryId));
162 //EnterCriticalSection(&CatalogLock);
163 CurrentEntry = CatalogListHead.Flink;
164 while (CurrentEntry != &CatalogListHead) {
165 Provider = CONTAINING_RECORD(CurrentEntry,
169 if (Provider->ProtocolInfo.dwCatalogEntryId == CatalogEntryId) {
170 //LeaveCriticalSection(&CatalogLock);
171 WS_DbgPrint(MID_TRACE, ("Returning provider at (0x%X) Name (%wZ).\n",
172 Provider, &Provider->LibraryName));
176 CurrentEntry = CurrentEntry->Flink;
178 //LeaveCriticalSection(&CatalogLock);
180 WS_DbgPrint(MID_TRACE, ("Provider was not found.\n"));
187 PCATALOG_ENTRY Provider,
188 LPWSAPROTOCOL_INFOW lpProtocolInfo)
192 WS_DbgPrint(MAX_TRACE, ("Loading provider at (0x%X) Name (%wZ).\n",
193 Provider, &Provider->LibraryName));
195 if (Provider->hModule == INVALID_HANDLE_VALUE) {
196 /* DLL is not loaded so load it now */
197 Provider->hModule = LoadLibrary(Provider->LibraryName.Buffer);
198 if (Provider->hModule != INVALID_HANDLE_VALUE) {
199 Provider->WSPStartup = (LPWSPSTARTUP)GetProcAddress(
202 if (Provider->WSPStartup) {
203 WS_DbgPrint(MAX_TRACE, ("Calling WSPStartup at (0x%X).\n",
204 Provider->WSPStartup));
205 Status = Provider->WSPStartup(
210 &Provider->ProcTable);
212 /* FIXME: Validate the procedure table */
214 Status = ERROR_BAD_PROVIDER;
216 Status = ERROR_DLL_NOT_FOUND;
220 WS_DbgPrint(MAX_TRACE, ("Status (%d).\n", Status));
227 PCATALOG_ENTRY Provider)
229 INT Status = NO_ERROR;
231 WS_DbgPrint(MAX_TRACE, ("Unloading provider at (0x%X)\n", Provider));
233 if (Provider->hModule) {
234 WS_DbgPrint(MAX_TRACE, ("Calling WSPCleanup at (0x%X).\n",
235 Provider->ProcTable.lpWSPCleanup));
236 Provider->ProcTable.lpWSPCleanup(&Status);
238 WS_DbgPrint(MAX_TRACE, ("Calling FreeLibrary(0x%X).\n", Provider->hModule));
239 if (!FreeLibrary(Provider->hModule)) {
240 WS_DbgPrint(MIN_TRACE, ("Could not free library at (0x%X).\n", Provider->hModule));
241 Status = GetLastError();
244 Provider->hModule = (HMODULE)INVALID_HANDLE_VALUE;
247 WS_DbgPrint(MAX_TRACE, ("Status (%d).\n", Status));
253 VOID CreateCatalog(VOID)
255 PCATALOG_ENTRY Provider;
257 InitializeCriticalSection(&CatalogLock);
259 InitializeListHead(&CatalogListHead);
261 /* FIXME: Read service provider catalog from registry */
263 Provider = CreateCatalogEntry(L"msafd.dll");
265 WS_DbgPrint(MIN_TRACE, ("Could not create catalog entry.\n"));
269 /* Assume one Service Provider with id 1 */
270 Provider->ProtocolInfo.dwCatalogEntryId = 1;
272 Provider->Mapping = HeapAlloc(GlobalHeap,
274 3 * sizeof(WINSOCK_MAPPING) + 3 * sizeof(DWORD));
275 if (!Provider->Mapping)
278 Provider->Mapping->Rows = 3;
279 Provider->Mapping->Columns = 3;
281 Provider->Mapping->Mapping[0].AddressFamily = AF_INET;
282 Provider->Mapping->Mapping[0].SocketType = SOCK_STREAM;
283 Provider->Mapping->Mapping[0].Protocol = IPPROTO_TCP;
285 Provider->Mapping->Mapping[1].AddressFamily = AF_INET;
286 Provider->Mapping->Mapping[1].SocketType = SOCK_DGRAM;
287 Provider->Mapping->Mapping[1].Protocol = IPPROTO_UDP;
289 Provider->Mapping->Mapping[2].AddressFamily = AF_INET;
290 Provider->Mapping->Mapping[2].SocketType = SOCK_RAW;
291 Provider->Mapping->Mapping[2].Protocol = 0;
296 VOID DestroyCatalog(VOID)
298 PLIST_ENTRY CurrentEntry;
299 PLIST_ENTRY NextEntry;
300 PCATALOG_ENTRY Provider;
302 CurrentEntry = CatalogListHead.Flink;
303 while (CurrentEntry != &CatalogListHead) {
304 NextEntry = CurrentEntry->Flink;
305 Provider = CONTAINING_RECORD(CurrentEntry,
308 DestroyCatalogEntry(Provider);
309 CurrentEntry = NextEntry;
311 //DeleteCriticalSection(&CatalogLock);