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
));
62 WS_DbgPrint(MIN_TRACE
, ("Insufficient memory.\n"));
66 ZeroMemory(Provider
, sizeof(CATALOG_ENTRY
));
68 Provider
->ReferenceCount
= 1;
70 InitializeCriticalSection(&Provider
->Lock
);
71 Provider
->hModule
= (HMODULE
)INVALID_HANDLE_VALUE
;
73 Provider
->Mapping
= NULL
;
75 //EnterCriticalSection(&CatalogLock);
77 InsertTailList(&CatalogListHead
, &Provider
->ListEntry
);
79 //LeaveCriticalSection(&CatalogLock);
85 INT
DestroyCatalogEntry(
86 PCATALOG_ENTRY Provider
)
90 WS_DbgPrint(MAX_TRACE
, ("Provider (0x%X).\n", Provider
));
92 //EnterCriticalSection(&CatalogLock);
93 RemoveEntryList(&Provider
->ListEntry
);
94 //LeaveCriticalSection(&CatalogLock);
96 HeapFree(GlobalHeap
, 0, Provider
->Mapping
);
98 if (Provider
->hModule
) {
99 Status
= UnloadProvider(Provider
);
104 //DeleteCriticalSection(&Provider->Lock);
106 HeapFree(GlobalHeap
, 0, Provider
);
112 PCATALOG_ENTRY
LocateProvider(
113 LPWSAPROTOCOL_INFOW lpProtocolInfo
)
115 PLIST_ENTRY CurrentEntry
;
116 PCATALOG_ENTRY Provider
;
119 WS_DbgPrint(MAX_TRACE
, ("lpProtocolInfo (0x%X).\n", lpProtocolInfo
));
121 //EnterCriticalSection(&CatalogLock);
123 CurrentEntry
= CatalogListHead
.Flink
;
124 while (CurrentEntry
!= &CatalogListHead
) {
125 Provider
= CONTAINING_RECORD(CurrentEntry
,
129 for (i
= 0; i
< Provider
->Mapping
->Rows
; i
++) {
130 if ((lpProtocolInfo
->iAddressFamily
== Provider
->Mapping
->Mapping
[i
].AddressFamily
) &&
131 (lpProtocolInfo
->iSocketType
== Provider
->Mapping
->Mapping
[i
].SocketType
) &&
132 ((lpProtocolInfo
->iProtocol
== Provider
->Mapping
->Mapping
[i
].Protocol
) ||
133 (lpProtocolInfo
->iSocketType
== SOCK_RAW
))) {
134 //LeaveCriticalSection(&CatalogLock);
135 WS_DbgPrint(MID_TRACE
, ("Returning provider at (0x%X).\n", Provider
));
140 CurrentEntry
= CurrentEntry
->Flink
;
143 LeaveCriticalSection(&CatalogLock
);
149 PCATALOG_ENTRY
LocateProviderById(
150 DWORD CatalogEntryId
)
152 PLIST_ENTRY CurrentEntry
;
153 PCATALOG_ENTRY Provider
;
156 WS_DbgPrint(MAX_TRACE
, ("CatalogEntryId (%d).\n", CatalogEntryId
));
158 //EnterCriticalSection(&CatalogLock);
159 CurrentEntry
= CatalogListHead
.Flink
;
160 while (CurrentEntry
!= &CatalogListHead
) {
161 Provider
= CONTAINING_RECORD(CurrentEntry
,
165 if (Provider
->ProtocolInfo
.dwCatalogEntryId
== CatalogEntryId
) {
166 //LeaveCriticalSection(&CatalogLock);
167 WS_DbgPrint(MID_TRACE
, ("Returning provider at (0x%X) Name (%s).\n",
168 Provider
, Provider
->LibraryName
));
172 CurrentEntry
= CurrentEntry
->Flink
;
174 //LeaveCriticalSection(&CatalogLock);
176 WS_DbgPrint(MID_TRACE
, ("Provider was not found.\n"));
183 PCATALOG_ENTRY Provider
,
184 LPWSAPROTOCOL_INFOW lpProtocolInfo
)
188 WS_DbgPrint(MAX_TRACE
, ("Loading provider at (0x%X) Name (%S).\n",
189 Provider
, Provider
->LibraryName
));
191 if (!Provider
->hModule
) {
192 /* DLL is not loaded so load it now */
193 Provider
->hModule
= LoadLibrary(Provider
->LibraryName
);
195 if (Provider
->hModule
) {
196 Provider
->WSPStartup
= (LPWSPSTARTUP
)GetProcAddress(Provider
->hModule
,
198 if (Provider
->WSPStartup
) {
199 WS_DbgPrint(MAX_TRACE
, ("Calling WSPStartup at (0x%X).\n", Provider
->WSPStartup
));
200 Status
= Provider
->WSPStartup(MAKEWORD(2, 2),
204 &Provider
->ProcTable
);
206 Status
= ERROR_BAD_PROVIDER
;
208 Status
= ERROR_DLL_NOT_FOUND
;
212 WS_DbgPrint(MAX_TRACE
, ("Status (%d).\n", Status
));
219 PCATALOG_ENTRY Provider
)
221 INT Status
= NO_ERROR
;
223 WS_DbgPrint(MAX_TRACE
, ("Unloading provider at (0x%X)\n", Provider
));
225 if (Provider
->hModule
) {
226 WS_DbgPrint(MAX_TRACE
, ("Calling WSPCleanup at (0x%X).\n",
227 Provider
->ProcTable
.lpWSPCleanup
));
228 Provider
->ProcTable
.lpWSPCleanup(&Status
);
230 if (!FreeLibrary(Provider
->hModule
)) {
231 WS_DbgPrint(MIN_TRACE
, ("Could not free library.\n"));
232 Status
= GetLastError();
235 Provider
->hModule
= (HMODULE
)INVALID_HANDLE_VALUE
;
238 WS_DbgPrint(MAX_TRACE
, ("Status (%d).\n", Status
));
244 VOID
CreateCatalog(VOID
)
246 PCATALOG_ENTRY Provider
;
248 InitializeCriticalSection(&CatalogLock
);
250 InitializeListHead(&CatalogListHead
);
252 /* FIXME: Read service provider catalog from registry */
254 Provider
= CreateCatalogEntry(L
"msafd.dll");
256 WS_DbgPrint(MIN_TRACE
, ("Could not create catalog entry.\n"));
260 /* Assume one Service Provider with id 1 */
261 Provider
->ProtocolInfo
.dwCatalogEntryId
= 1;
263 Provider
->Mapping
= HeapAlloc(GlobalHeap
, 0, sizeof(WINSOCK_MAPPING
) + 3 * sizeof(DWORD
));
264 if (!Provider
->Mapping
) {
265 WS_DbgPrint(MIN_TRACE
, ("Insufficient memory.\n"));
269 Provider
->Mapping
->Rows
= 1;
270 Provider
->Mapping
->Columns
= 3;
271 Provider
->Mapping
->Mapping
[0].AddressFamily
= AF_INET
;
272 Provider
->Mapping
->Mapping
[0].SocketType
= SOCK_RAW
;
273 Provider
->Mapping
->Mapping
[0].Protocol
= 0;
278 VOID
DestroyCatalog(VOID
)
280 PLIST_ENTRY CurrentEntry
;
281 PLIST_ENTRY NextEntry
;
282 PCATALOG_ENTRY Provider
;
284 CurrentEntry
= CatalogListHead
.Flink
;
285 while (CurrentEntry
!= &CatalogListHead
) {
286 NextEntry
= CurrentEntry
->Flink
;
287 Provider
= CONTAINING_RECORD(CurrentEntry
,
290 DestroyCatalogEntry(Provider
);
291 CurrentEntry
= NextEntry
;
293 //DeleteCriticalSection(&CatalogLock);