Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygencatalog.c
Go to the documentation of this file.
00001 /* 00002 * COPYRIGHT: See COPYING in the top level directory 00003 * PROJECT: ReactOS WinSock 2 DLL 00004 * FILE: misc/catalog.c 00005 * PURPOSE: Service Provider Catalog 00006 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net) 00007 * REVISIONS: 00008 * CSH 01/09-2000 Created 00009 */ 00010 00011 #include "ws2_32.h" 00012 00013 LIST_ENTRY CatalogListHead; 00014 CRITICAL_SECTION CatalogLock; 00015 00016 VOID 00017 ReferenceProviderByPointer(PCATALOG_ENTRY Provider) 00018 { 00019 WS_DbgPrint(MAX_TRACE, ("Provider (0x%X).\n", Provider)); 00020 00021 //EnterCriticalSection(&Provider->Lock); 00022 Provider->ReferenceCount++; 00023 //LeaveCriticalSection(&Provider->Lock); 00024 00025 WS_DbgPrint(MAX_TRACE, ("Leaving\n")); 00026 } 00027 00028 00029 VOID 00030 DereferenceProviderByPointer(PCATALOG_ENTRY Provider) 00031 { 00032 WS_DbgPrint(MAX_TRACE, ("Provider (0x%X).\n", Provider)); 00033 00034 #if DBG 00035 if (Provider->ReferenceCount <= 0) 00036 { 00037 WS_DbgPrint(MIN_TRACE, ("Provider at 0x%X has invalid reference count (%ld).\n", 00038 Provider, Provider->ReferenceCount)); 00039 } 00040 #endif 00041 00042 //EnterCriticalSection(&Provider->Lock); 00043 Provider->ReferenceCount--; 00044 //LeaveCriticalSection(&Provider->Lock); 00045 00046 if (Provider->ReferenceCount == 0) 00047 { 00048 WS_DbgPrint(MAX_TRACE, ("Provider at 0x%X has reference count 0 (unloading).\n", 00049 Provider)); 00050 00051 DestroyCatalogEntry(Provider); 00052 } 00053 } 00054 00055 00056 PCATALOG_ENTRY 00057 CreateCatalogEntry(LPWSTR LibraryName) 00058 { 00059 PCATALOG_ENTRY Provider; 00060 00061 WS_DbgPrint(MAX_TRACE, ("LibraryName (%S).\n", LibraryName)); 00062 00063 Provider = HeapAlloc(GlobalHeap, 0, sizeof(CATALOG_ENTRY)); 00064 if (!Provider) 00065 return NULL; 00066 00067 ZeroMemory(Provider, sizeof(CATALOG_ENTRY)); 00068 00069 if (!RtlCreateUnicodeString(&Provider->LibraryName, LibraryName)) 00070 { 00071 RtlFreeHeap(GlobalHeap, 0, Provider); 00072 return NULL; 00073 } 00074 00075 Provider->ReferenceCount = 1; 00076 00077 InitializeCriticalSection(&Provider->Lock); 00078 Provider->hModule = NULL; 00079 00080 Provider->Mapping = NULL; 00081 00082 //EnterCriticalSection(&CatalogLock); 00083 00084 InsertTailList(&CatalogListHead, &Provider->ListEntry); 00085 00086 //LeaveCriticalSection(&CatalogLock); 00087 00088 return Provider; 00089 } 00090 00091 00092 INT 00093 DestroyCatalogEntry(PCATALOG_ENTRY Provider) 00094 { 00095 INT Status; 00096 00097 WS_DbgPrint(MAX_TRACE, ("Provider (0x%X).\n", Provider)); 00098 00099 //EnterCriticalSection(&CatalogLock); 00100 RemoveEntryList(&Provider->ListEntry); 00101 //LeaveCriticalSection(&CatalogLock); 00102 00103 HeapFree(GlobalHeap, 0, Provider->Mapping); 00104 00105 if (NULL != Provider->hModule) 00106 { 00107 Status = UnloadProvider(Provider); 00108 } 00109 else 00110 { 00111 Status = NO_ERROR; 00112 } 00113 00114 //DeleteCriticalSection(&Provider->Lock); 00115 00116 HeapFree(GlobalHeap, 0, Provider); 00117 00118 return Status; 00119 } 00120 00121 00122 PCATALOG_ENTRY 00123 LocateProvider(LPWSAPROTOCOL_INFOW lpProtocolInfo) 00124 { 00125 PLIST_ENTRY CurrentEntry; 00126 PCATALOG_ENTRY Provider; 00127 UINT i; 00128 00129 WS_DbgPrint(MAX_TRACE, ("lpProtocolInfo (0x%X).\n", lpProtocolInfo)); 00130 00131 //EnterCriticalSection(&CatalogLock); 00132 00133 CurrentEntry = CatalogListHead.Flink; 00134 while (CurrentEntry != &CatalogListHead) 00135 { 00136 Provider = CONTAINING_RECORD(CurrentEntry, 00137 CATALOG_ENTRY, 00138 ListEntry); 00139 00140 for (i = 0; i < Provider->Mapping->Rows; i++) 00141 { 00142 if ((lpProtocolInfo->iAddressFamily == (INT) Provider->Mapping->Mapping[i].AddressFamily) && 00143 (lpProtocolInfo->iSocketType == (INT) Provider->Mapping->Mapping[i].SocketType) && 00144 ((lpProtocolInfo->iProtocol == (INT) Provider->Mapping->Mapping[i].Protocol) || 00145 (lpProtocolInfo->iSocketType == SOCK_RAW))) 00146 { 00147 //LeaveCriticalSection(&CatalogLock); 00148 lpProtocolInfo->dwCatalogEntryId = Provider->ProtocolInfo.dwCatalogEntryId; 00149 WS_DbgPrint(MID_TRACE, ("Returning provider at (0x%X).\n", Provider)); 00150 return Provider; 00151 } 00152 } 00153 00154 CurrentEntry = CurrentEntry->Flink; 00155 } 00156 00157 //LeaveCriticalSection(&CatalogLock); 00158 00159 return NULL; 00160 } 00161 00162 00163 PCATALOG_ENTRY 00164 LocateProviderById(DWORD CatalogEntryId) 00165 { 00166 PLIST_ENTRY CurrentEntry; 00167 PCATALOG_ENTRY Provider; 00168 00169 WS_DbgPrint(MAX_TRACE, ("CatalogEntryId (%d).\n", CatalogEntryId)); 00170 00171 //EnterCriticalSection(&CatalogLock); 00172 CurrentEntry = CatalogListHead.Flink; 00173 while (CurrentEntry != &CatalogListHead) 00174 { 00175 Provider = CONTAINING_RECORD(CurrentEntry, 00176 CATALOG_ENTRY, 00177 ListEntry); 00178 00179 if (Provider->ProtocolInfo.dwCatalogEntryId == CatalogEntryId) 00180 { 00181 //LeaveCriticalSection(&CatalogLock); 00182 WS_DbgPrint(MID_TRACE, ("Returning provider at (0x%X) Name (%wZ).\n", 00183 Provider, &Provider->LibraryName)); 00184 return Provider; 00185 } 00186 00187 CurrentEntry = CurrentEntry->Flink; 00188 } 00189 //LeaveCriticalSection(&CatalogLock); 00190 00191 WS_DbgPrint(MID_TRACE, ("Provider was not found.\n")); 00192 00193 return NULL; 00194 } 00195 00196 00197 INT 00198 LoadProvider(PCATALOG_ENTRY Provider, 00199 LPWSAPROTOCOL_INFOW lpProtocolInfo) 00200 { 00201 INT Status; 00202 00203 WS_DbgPrint(MID_TRACE, ("Loading provider at (0x%X) Name (%wZ).\n", 00204 Provider, &Provider->LibraryName)); 00205 00206 if (NULL == Provider->hModule) 00207 { 00208 /* DLL is not loaded so load it now 00209 * UNICODE_STRING objects are not null-terminated, but LoadLibraryW 00210 * expects a null-terminated string 00211 */ 00212 Provider->LibraryName.Buffer[Provider->LibraryName.Length / sizeof(WCHAR)] = L'\0'; 00213 Provider->hModule = LoadLibraryW(Provider->LibraryName.Buffer); 00214 if (NULL != Provider->hModule) 00215 { 00216 Provider->WSPStartup = (LPWSPSTARTUP)GetProcAddress(Provider->hModule, 00217 "WSPStartup"); 00218 if (Provider->WSPStartup) 00219 { 00220 WS_DbgPrint(MAX_TRACE, ("Calling WSPStartup at (0x%X).\n", 00221 Provider->WSPStartup)); 00222 Status = Provider->WSPStartup(MAKEWORD(2, 2), 00223 &Provider->WSPData, 00224 lpProtocolInfo, 00225 UpcallTable, 00226 &Provider->ProcTable); 00227 00228 /* FIXME: Validate the procedure table */ 00229 } 00230 else 00231 Status = ERROR_BAD_PROVIDER; 00232 } 00233 else 00234 Status = ERROR_DLL_NOT_FOUND; 00235 } 00236 else 00237 Status = NO_ERROR; 00238 00239 WS_DbgPrint(MID_TRACE, ("Status (%d).\n", Status)); 00240 00241 return Status; 00242 } 00243 00244 00245 INT 00246 UnloadProvider(PCATALOG_ENTRY Provider) 00247 { 00248 INT Status = NO_ERROR; 00249 00250 WS_DbgPrint(MAX_TRACE, ("Unloading provider at (0x%X)\n", Provider)); 00251 00252 if (NULL != Provider->hModule) 00253 { 00254 WS_DbgPrint(MAX_TRACE, ("Calling WSPCleanup at (0x%X).\n", 00255 Provider->ProcTable.lpWSPCleanup)); 00256 Provider->ProcTable.lpWSPCleanup(&Status); 00257 00258 WS_DbgPrint(MAX_TRACE, ("Calling FreeLibrary(0x%X).\n", Provider->hModule)); 00259 if (!FreeLibrary(Provider->hModule)) 00260 { 00261 WS_DbgPrint(MIN_TRACE, ("Could not free library at (0x%X).\n", Provider->hModule)); 00262 Status = GetLastError(); 00263 } 00264 00265 Provider->hModule = NULL; 00266 } 00267 00268 WS_DbgPrint(MAX_TRACE, ("Status (%d).\n", Status)); 00269 00270 return Status; 00271 } 00272 00273 00274 VOID 00275 CreateCatalog(VOID) 00276 { 00277 PCATALOG_ENTRY Provider; 00278 00279 InitializeCriticalSection(&CatalogLock); 00280 00281 InitializeListHead(&CatalogListHead); 00282 00283 /* FIXME: Read service provider catalog from registry 00284 00285 Catalog info is saved somewhere under 00286 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WinSock2 00287 */ 00288 00289 #if 1 00290 Provider = CreateCatalogEntry(L"msafd.dll"); 00291 if (!Provider) 00292 { 00293 WS_DbgPrint(MIN_TRACE, ("Could not create catalog entry.\n")); 00294 return; 00295 } 00296 00297 /* Assume one Service Provider with id 1 */ 00298 Provider->ProtocolInfo.dwCatalogEntryId = 1; 00299 00300 Provider->Mapping = HeapAlloc(GlobalHeap, 00301 0, 00302 6 * sizeof(WINSOCK_MAPPING) + 3 * sizeof(DWORD)); 00303 if (!Provider->Mapping) 00304 return; 00305 00306 Provider->Mapping->Rows = 6; 00307 Provider->Mapping->Columns = 3; 00308 00309 Provider->Mapping->Mapping[0].AddressFamily = AF_INET; 00310 Provider->Mapping->Mapping[0].SocketType = SOCK_STREAM; 00311 Provider->Mapping->Mapping[0].Protocol = 0; 00312 00313 Provider->Mapping->Mapping[1].AddressFamily = AF_INET; 00314 Provider->Mapping->Mapping[1].SocketType = SOCK_STREAM; 00315 Provider->Mapping->Mapping[1].Protocol = IPPROTO_TCP; 00316 00317 Provider->Mapping->Mapping[2].AddressFamily = AF_INET; 00318 Provider->Mapping->Mapping[2].SocketType = SOCK_DGRAM; 00319 Provider->Mapping->Mapping[2].Protocol = 0; 00320 00321 Provider->Mapping->Mapping[3].AddressFamily = AF_INET; 00322 Provider->Mapping->Mapping[3].SocketType = SOCK_DGRAM; 00323 Provider->Mapping->Mapping[3].Protocol = IPPROTO_UDP; 00324 00325 Provider->Mapping->Mapping[4].AddressFamily = AF_INET; 00326 Provider->Mapping->Mapping[4].SocketType = SOCK_RAW; 00327 Provider->Mapping->Mapping[4].Protocol = IPPROTO_ICMP; 00328 00329 Provider->Mapping->Mapping[5].AddressFamily = AF_INET; 00330 Provider->Mapping->Mapping[5].SocketType = SOCK_RAW; 00331 Provider->Mapping->Mapping[5].Protocol = 0; 00332 #endif 00333 } 00334 00335 00336 VOID DestroyCatalog(VOID) 00337 { 00338 PLIST_ENTRY CurrentEntry; 00339 PLIST_ENTRY NextEntry; 00340 PCATALOG_ENTRY Provider; 00341 00342 CurrentEntry = CatalogListHead.Flink; 00343 while (CurrentEntry != &CatalogListHead) 00344 { 00345 NextEntry = CurrentEntry->Flink; 00346 Provider = CONTAINING_RECORD(CurrentEntry, 00347 CATALOG_ENTRY, 00348 ListEntry); 00349 DestroyCatalogEntry(Provider); 00350 CurrentEntry = NextEntry; 00351 } 00352 //DeleteCriticalSection(&CatalogLock); 00353 } 00354 00355 /* EOF */ Generated on Sat May 26 2012 04:25:37 for ReactOS by
1.7.6.1
|