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 WS_DbgPrint(MAX_TRACE
, ("OFFSET2 (0x%X)\n",
215 FIELD_OFFSET(WSPPROC_TABLE
, lpWSPSocket
)));
217 assert(Provider
->ProcTable
.lpWSPSocket
);
220 Status
= ERROR_BAD_PROVIDER
;
222 Status
= ERROR_DLL_NOT_FOUND
;
226 WS_DbgPrint(MAX_TRACE
, ("Status (%d).\n", Status
));
233 PCATALOG_ENTRY Provider
)
235 INT Status
= NO_ERROR
;
237 WS_DbgPrint(MAX_TRACE
, ("Unloading provider at (0x%X)\n", Provider
));
239 if (Provider
->hModule
) {
240 WS_DbgPrint(MAX_TRACE
, ("Calling WSPCleanup at (0x%X).\n",
241 Provider
->ProcTable
.lpWSPCleanup
));
242 Provider
->ProcTable
.lpWSPCleanup(&Status
);
244 if (!FreeLibrary(Provider
->hModule
)) {
245 WS_DbgPrint(MIN_TRACE
, ("Could not free library.\n"));
246 Status
= GetLastError();
249 Provider
->hModule
= (HMODULE
)INVALID_HANDLE_VALUE
;
252 WS_DbgPrint(MAX_TRACE
, ("Status (%d).\n", Status
));
258 VOID
CreateCatalog(VOID
)
260 PCATALOG_ENTRY Provider
;
262 InitializeCriticalSection(&CatalogLock
);
264 InitializeListHead(&CatalogListHead
);
266 /* FIXME: Read service provider catalog from registry */
268 Provider
= CreateCatalogEntry(L
"msafd.dll");
270 WS_DbgPrint(MIN_TRACE
, ("Could not create catalog entry.\n"));
274 /* Assume one Service Provider with id 1 */
275 Provider
->ProtocolInfo
.dwCatalogEntryId
= 1;
277 Provider
->Mapping
= HeapAlloc(GlobalHeap
, 0, sizeof(WINSOCK_MAPPING
) + 3 * sizeof(DWORD
));
278 if (!Provider
->Mapping
) {
279 WS_DbgPrint(MIN_TRACE
, ("Insufficient memory.\n"));
283 Provider
->Mapping
->Rows
= 1;
284 Provider
->Mapping
->Columns
= 3;
285 Provider
->Mapping
->Mapping
[0].AddressFamily
= AF_INET
;
286 Provider
->Mapping
->Mapping
[0].SocketType
= SOCK_RAW
;
287 Provider
->Mapping
->Mapping
[0].Protocol
= 0;
292 VOID
DestroyCatalog(VOID
)
294 PLIST_ENTRY CurrentEntry
;
295 PLIST_ENTRY NextEntry
;
296 PCATALOG_ENTRY Provider
;
298 CurrentEntry
= CatalogListHead
.Flink
;
299 while (CurrentEntry
!= &CatalogListHead
) {
300 NextEntry
= CurrentEntry
->Flink
;
301 Provider
= CONTAINING_RECORD(CurrentEntry
,
304 DestroyCatalogEntry(Provider
);
305 CurrentEntry
= NextEntry
;
307 //DeleteCriticalSection(&CatalogLock);