Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

430 lines
16 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. PCH_Winsock.CPP
  5. Abstract:
  6. WBEM provider class implementation for PCH_Winsock class.
  7. This class does not use any existing Win32 Class
  8. Revision History:
  9. Kalyani Narlanka (kalyanin) 04/27/99
  10. - Created
  11. Kalyani Narlanka (kalyanin) 05/10/99
  12. - Added Name, Size, Version, Description, SystemStatus, MaxUDP, MAXSockets,
  13. Change, Timestamp
  14. *******************************************************************************/
  15. // #includes
  16. #include "pchealth.h"
  17. #include "PCH_WINSOCK.h"
  18. // #defines
  19. // nMajorVersion represents the Major Version as seen in OSVERSIONINFO
  20. #define nMajorVersion 4
  21. // nMinorVersion represents the Minor Version as seen in OSVERSIONINFO
  22. #define nMinorVersion 10
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // Begin Tracing stuff
  25. //
  26. #ifdef THIS_FILE
  27. #undef THIS_FILE
  28. #endif
  29. static char __szTraceSourceFile[] = __FILE__;
  30. #define THIS_FILE __szTraceSourceFile
  31. #define TRACE_ID DCID_WINSOCK
  32. //
  33. // End Tracing stuff
  34. ///////////////////////////////////////////////////////////////////////////////
  35. CPCH_WINSOCK MyPCH_WINSOCKSet (PROVIDER_NAME_PCH_WINSOCK, PCH_NAMESPACE) ;
  36. ///////////////////////////////////////////////////////////////////////////////
  37. //....Properties of PCHWinsock Class
  38. //
  39. const static WCHAR* pTimeStamp = L"TimeStamp" ;
  40. const static WCHAR* pChange = L"Change" ;
  41. const static WCHAR* pDescription = L"Description" ;
  42. const static WCHAR* pMaxSockets = L"MaxSockets" ;
  43. const static WCHAR* pMaxUDP = L"MaxUDP" ;
  44. const static WCHAR* pName = L"Name" ;
  45. const static WCHAR* pSize = L"Size" ;
  46. const static WCHAR* pSystemStatus = L"SystemStatus" ;
  47. const static WCHAR* pVersion = L"Version" ;
  48. //*****************************************************************************
  49. //
  50. // Function Name : CPCH_WINSOCK::EnumerateInstances
  51. //
  52. // Input Parameters : pMethodContext : Pointer to the MethodContext for
  53. // communication with WinMgmt.
  54. //
  55. // lFlags : Long that contains the flags described
  56. // in IWbemServices::CreateInstanceEnumAsync
  57. // Note that the following flags are handled
  58. // by (and filtered out by) WinMgmt:
  59. // WBEM_FLAG_DEEP
  60. // WBEM_FLAG_SHALLOW
  61. // WBEM_FLAG_RETURN_IMMEDIATELY
  62. // WBEM_FLAG_FORWARD_ONLY
  63. // WBEM_FLAG_BIDIRECTIONAL
  64. // Output Parameters : None
  65. //
  66. // Returns : WBEM_S_NO_ERROR
  67. //
  68. //
  69. // Synopsis : There is a single instance of this class on the machine
  70. // and this is returned..
  71. // If there is no instances returns WBEM_S_NO_ERROR.
  72. // It is not an error to have no instances.
  73. //
  74. //*****************************************************************************
  75. HRESULT CPCH_WINSOCK::EnumerateInstances(MethodContext* pMethodContext,
  76. long lFlags)
  77. {
  78. TraceFunctEnter("CPCH_Winsock::EnumerateInstances");
  79. // Begin Declarations...................................................
  80. HRESULT hRes = WBEM_S_NO_ERROR;
  81. // PCH_WinSock Class instance
  82. // CInstance *pPCHWinsockInstance;
  83. // Strings
  84. TCHAR tchBuf[MAX_PATH];
  85. TCHAR tchTemp[MAX_PATH];
  86. TCHAR szDirectory[MAX_PATH];
  87. TCHAR tchWinsockDll[MAX_PATH];
  88. LPCTSTR lpctstrWS2_32Dll = _T("ws2_32.dll");
  89. LPCTSTR lpctstrWSock32Dll = _T("wsock32.dll");
  90. LPCWSTR lpctstrFileSize = L"FileSize";
  91. LPCTSTR lpctstrWSAStartup = _T("WSAStartup");
  92. LPCTSTR lpctstrWSACleanup = _T("WSACleanup");
  93. // WORDs
  94. WORD wVersionRequested;
  95. // WSAData
  96. WSADATA wsaData;
  97. // CComVariants
  98. CComVariant varValue;
  99. CComVariant varSnapshot = "Snapshot";
  100. // ints
  101. int nError;
  102. // HINSTANCE
  103. HINSTANCE hModule;
  104. // OSVersion
  105. OSVERSIONINFO osVersionInfo;
  106. // SystemTime
  107. SYSTEMTIME stUTCTime;
  108. // Strings
  109. CComBSTR bstrWinsockDllWithPath;
  110. BOOL fWinsockDllFound = FALSE;
  111. struct _stat filestat;
  112. ULONG uiReturn;
  113. IWbemClassObjectPtr pWinsockDllObj;
  114. LPFN_WSASTARTUP WSAStartup;
  115. LPFN_WSACLEANUP WSACleanup;
  116. BOOL fCommit = FALSE;
  117. // END Declarations
  118. // There is only one instance of PCH_Winsock class
  119. // Create a new instance of PCH_Winsock Class based on the passed-in MethodContext
  120. CInstancePtr pPCHWinsockInstance(CreateNewInstance(pMethodContext), false);
  121. // Get the date and time to update the TimeStamp Field
  122. GetSystemTime(&stUTCTime);
  123. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  124. // TIME STAMP //
  125. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  126. hRes = pPCHWinsockInstance->SetDateTime(pTimeStamp, WBEMTime(stUTCTime));
  127. if (FAILED(hRes))
  128. {
  129. // Could not Set the Time Stamp
  130. // Continue anyway
  131. ErrorTrace(TRACE_ID, "SetDateTime on Timestamp Field failed.");
  132. }
  133. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  134. // CHANGE //
  135. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  136. hRes = pPCHWinsockInstance->SetVariant(pChange, varSnapshot);
  137. if (FAILED(hRes))
  138. {
  139. // Could not Set the Change Property
  140. // Continue anyway
  141. ErrorTrace(TRACE_ID, "Set Variant on Change Field failed.");
  142. }
  143. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  144. // NAME //
  145. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  146. // Before callling GetVersionEx set dwOSVersionInfoSize to the foll.
  147. osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  148. if (GetVersionEx(&osVersionInfo) != 0)
  149. {
  150. if (osVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  151. {
  152. if ((osVersionInfo.dwMajorVersion == nMajorVersion) && (osVersionInfo.dwMinorVersion >= nMinorVersion))
  153. {
  154. _tcscpy(tchWinsockDll, lpctstrWS2_32Dll);
  155. }
  156. else if (osVersionInfo.dwMajorVersion > nMajorVersion)
  157. {
  158. _tcscpy(tchWinsockDll, lpctstrWS2_32Dll);
  159. }
  160. else
  161. {
  162. _tcscpy(tchWinsockDll, lpctstrWSock32Dll);
  163. }
  164. } //end of osVersionInfo.... if
  165. else if (osVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
  166. {
  167. _tcscpy(tchWinsockDll, lpctstrWS2_32Dll);
  168. }
  169. else
  170. {
  171. _tcscpy(tchWinsockDll, lpctstrWS2_32Dll);
  172. }
  173. } //end of if GetVersionEx
  174. else
  175. {
  176. _tcscpy(tchWinsockDll, lpctstrWS2_32Dll);
  177. }
  178. // Got the right winsock DLL Name
  179. // Load the Library
  180. varValue = tchWinsockDll;
  181. hModule = LoadLibrary(tchWinsockDll);
  182. if (hModule == NULL)
  183. {
  184. goto END;
  185. }
  186. else
  187. {
  188. fCommit = TRUE;
  189. }
  190. try
  191. {
  192. hRes = pPCHWinsockInstance->SetVariant(pName, varValue);
  193. if (FAILED(hRes))
  194. {
  195. // Could not Set the Change Property
  196. // Continue anyway
  197. ErrorTrace(TRACE_ID, "Set Variant on Name Field failed.");
  198. }
  199. }
  200. catch(...)
  201. {
  202. FreeLibrary(hModule);
  203. throw;
  204. }
  205. if ((WSAStartup = (LPFN_WSASTARTUP) GetProcAddress(hModule, lpctstrWSAStartup)) == NULL)
  206. {
  207. FreeLibrary(hModule);
  208. goto END;
  209. }
  210. if ((WSACleanup = (LPFN_WSACLEANUP) GetProcAddress(hModule, lpctstrWSACleanup)) == NULL)
  211. {
  212. FreeLibrary(hModule);
  213. goto END;
  214. }
  215. try
  216. {
  217. wVersionRequested = MAKEWORD( 2, 0 );
  218. nError = (*WSAStartup)( wVersionRequested, &wsaData );
  219. if (nError != 0)
  220. {
  221. // Cannot get any winsock values
  222. goto END;
  223. }
  224. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  225. // SIZE //
  226. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  227. fWinsockDllFound = getCompletePath(tchWinsockDll, bstrWinsockDllWithPath);
  228. if(fWinsockDllFound)
  229. {
  230. // Got the complete Path , use this to get the filesize.
  231. if(SUCCEEDED(GetCIMDataFile(bstrWinsockDllWithPath, &pWinsockDllObj)))
  232. {
  233. // From the CIM_DataFile Object get the size property
  234. CopyProperty(pWinsockDllObj, lpctstrFileSize, pPCHWinsockInstance, pSize);
  235. }
  236. }
  237. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  238. // VERSION //
  239. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  240. _stprintf(tchBuf, "%d.%d", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion));
  241. varValue = tchBuf;
  242. hRes = pPCHWinsockInstance->SetVariant(pVersion, varValue);
  243. if (FAILED(hRes))
  244. {
  245. // Could not Set the Change Property
  246. // Continue anyway
  247. ErrorTrace(TRACE_ID, "Set Variant on Version Field failed.");
  248. }
  249. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  250. // DESCRIPTION // KAYANI -9++***************************---------------------------------------------------------+++
  251. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  252. if (_tcslen(wsaData.szDescription) < sizeof(tchBuf))
  253. {
  254. _tcscpy(tchBuf, wsaData.szDescription);
  255. }
  256. else
  257. {
  258. _tcsncpy(tchBuf, wsaData.szDescription, sizeof(tchBuf)-1);
  259. tchBuf[sizeof(tchBuf)] = 0;
  260. }
  261. varValue = tchBuf;
  262. hRes = pPCHWinsockInstance->SetVariant(pDescription, varValue);
  263. if(FAILED(hRes))
  264. {
  265. // Could not Set the Change Property
  266. // Continue anyway
  267. ErrorTrace(TRACE_ID, "Set Variant on Description Field failed.");
  268. }
  269. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  270. // SYSTEMSTATUS //
  271. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  272. if (_tcslen(wsaData.szSystemStatus) < sizeof(tchBuf))
  273. _tcscpy(tchBuf, wsaData.szSystemStatus);
  274. else
  275. {
  276. _tcsncpy(tchBuf, wsaData.szSystemStatus, sizeof(tchBuf)-1);
  277. tchBuf[sizeof(tchBuf)] = 0;
  278. }
  279. varValue = tchBuf;
  280. hRes = pPCHWinsockInstance->SetVariant(pSystemStatus, varValue);
  281. if (FAILED(hRes))
  282. {
  283. // Could not Set the Change Property
  284. // Continue anyway
  285. ErrorTrace(TRACE_ID, "Set Variant on SystemStatus Field failed.");
  286. }
  287. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  288. // MAXUDP //
  289. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  290. if (LOBYTE(wsaData.wHighVersion) >= 2)
  291. {
  292. varValue = 0;
  293. }
  294. else
  295. {
  296. varValue = wsaData.iMaxUdpDg;
  297. }
  298. hRes = pPCHWinsockInstance->SetVariant(pMaxUDP, varValue);
  299. if (FAILED(hRes))
  300. {
  301. // Could not Set the Change Property
  302. // Continue anyway
  303. ErrorTrace(TRACE_ID, "Set Variant on MAXUDP Field failed.");
  304. }
  305. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  306. // MAXSOCKETS //
  307. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  308. if (LOBYTE(wsaData.wHighVersion) >= 2)
  309. {
  310. varValue = 0;
  311. }
  312. else
  313. {
  314. varValue = wsaData.iMaxSockets;
  315. }
  316. hRes = pPCHWinsockInstance->SetVariant(pMaxSockets, varValue);
  317. if (FAILED(hRes))
  318. {
  319. // Could not Set the Change Property
  320. // Continue anyway
  321. ErrorTrace(TRACE_ID, "Set Variant on MaxSockets Field failed.");
  322. }
  323. if(fCommit)
  324. {
  325. hRes = pPCHWinsockInstance->Commit();
  326. if (FAILED(hRes))
  327. {
  328. // Could not Commit
  329. // Continue anyway
  330. ErrorTrace(TRACE_ID, "Commit failed.");
  331. }
  332. }
  333. if(0 != (*WSACleanup)())
  334. {
  335. // Could not Cleanup
  336. // Continue anyway
  337. ErrorTrace(TRACE_ID, "WSACleanup failed.");
  338. }
  339. FreeLibrary(hModule);
  340. }
  341. catch(...)
  342. {
  343. if(0 != (*WSACleanup)())
  344. {
  345. // Could not Cleanup
  346. // Continue anyway
  347. ErrorTrace(TRACE_ID, "WSACleanup failed.");
  348. }
  349. FreeLibrary(hModule);
  350. throw;
  351. }
  352. END: TraceFunctLeave();
  353. return hRes ;
  354. }