Source code of Windows XP (NT5)
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.

527 lines
13 KiB

  1. #include "WLBS_Provider.h"
  2. #include "WLBS_Root.h"
  3. #include "utils.h"
  4. #include "controlwrapper.h"
  5. #include "param.h"
  6. ////////////////////////////////////////////////////////////////////////////////
  7. //
  8. // CWlbs_Root::CWlbs_Root
  9. //
  10. // Purpose: Constructor
  11. //
  12. ////////////////////////////////////////////////////////////////////////////////
  13. CWlbs_Root::CWlbs_Root(CWbemServices* a_pNameSpace,
  14. IWbemObjectSink* a_pResponseHandler)
  15. : m_pNameSpace(NULL), m_pResponseHandler(NULL)
  16. {
  17. //m_pNameSpace and m_pResponseHandler are initialized to NULL
  18. //by CWlbs_Root
  19. if(!a_pNameSpace || !a_pResponseHandler)
  20. throw _com_error( WBEM_E_INVALID_PARAMETER );
  21. m_pNameSpace = a_pNameSpace;
  22. m_pResponseHandler = a_pResponseHandler;
  23. m_pResponseHandler->AddRef();
  24. }
  25. ////////////////////////////////////////////////////////////////////////////////
  26. //
  27. // CWlbs_Root::~CWlbs_Root
  28. //
  29. // Purpose: Destructor
  30. //
  31. ////////////////////////////////////////////////////////////////////////////////
  32. CWlbs_Root::~CWlbs_Root()
  33. {
  34. if( m_pNameSpace )
  35. m_pNameSpace = NULL;
  36. if( m_pResponseHandler ) {
  37. m_pResponseHandler->Release();
  38. m_pResponseHandler = NULL;
  39. }
  40. }
  41. ////////////////////////////////////////////////////////////////////////////////
  42. //
  43. // CWlbs_Root::SpawnInstance
  44. //
  45. // Purpose: This obtains an instance of a WBEM class.
  46. //
  47. ////////////////////////////////////////////////////////////////////////////////
  48. void CWlbs_Root::SpawnInstance
  49. (
  50. LPCWSTR a_szClassName,
  51. IWbemClassObject** a_ppWbemInstance
  52. )
  53. {
  54. HRESULT hResult;
  55. IWbemClassObjectPtr pWlbsClass;
  56. //get the MOF class object
  57. hResult = m_pNameSpace->GetObject(
  58. _bstr_t( a_szClassName ),
  59. 0,
  60. NULL,
  61. &pWlbsClass,
  62. NULL);
  63. if( FAILED(hResult) ) {
  64. throw _com_error(hResult);
  65. }
  66. //spawn an instance
  67. hResult = pWlbsClass->SpawnInstance( 0, a_ppWbemInstance );
  68. if( FAILED( hResult ) )
  69. throw _com_error( hResult );
  70. }
  71. ////////////////////////////////////////////////////////////////////////////////
  72. //
  73. // CWlbs_Root::GetMethodOutputInstance
  74. //
  75. // Purpose: This obtains an IWbemClassObject that is used to store the
  76. // output parameters for a method call. The caller is responsible for
  77. // releasing the output object.
  78. //
  79. ////////////////////////////////////////////////////////////////////////////////
  80. void CWlbs_Root::GetMethodOutputInstance
  81. (
  82. LPCWSTR a_szMethodClass,
  83. const BSTR a_strMethodName,
  84. IWbemClassObject** a_ppOutputInstance
  85. )
  86. {
  87. IWbemClassObjectPtr pMethClass;
  88. IWbemClassObjectPtr pOutputClass;
  89. IWbemClassObjectPtr pOutParams;
  90. BSTR strMethodClass = NULL;
  91. HRESULT hResult;
  92. ASSERT( a_szMethodClass );
  93. try {
  94. strMethodClass = SysAllocString( a_szMethodClass );
  95. if( !strMethodClass )
  96. throw _com_error( WBEM_E_OUT_OF_MEMORY );
  97. hResult = m_pNameSpace->GetObject
  98. ( strMethodClass,
  99. 0,
  100. NULL,
  101. &pMethClass,
  102. NULL
  103. );
  104. if( FAILED( hResult ) )
  105. throw _com_error( hResult );
  106. hResult = pMethClass->GetMethod( a_strMethodName, 0, NULL, &pOutputClass );
  107. if( FAILED( hResult ) )
  108. throw _com_error( hResult );
  109. hResult = pOutputClass->SpawnInstance( 0, a_ppOutputInstance );
  110. if( FAILED( hResult ) ) {
  111. throw _com_error(hResult);
  112. }
  113. if( strMethodClass ) {
  114. SysFreeString( strMethodClass );
  115. strMethodClass = NULL;
  116. }
  117. }
  118. catch(...) {
  119. if( strMethodClass ) {
  120. SysFreeString( strMethodClass );
  121. strMethodClass = NULL;
  122. }
  123. throw;
  124. }
  125. }
  126. ////////////////////////////////////////////////////////////////////////////////
  127. //
  128. // CWlbs_Root::UpdateConfigProp
  129. //
  130. // Purpose: This extracts the propery name and value from the WBEM object and if
  131. // the property is not set to VT_NULL then the configuration is updated
  132. // else the original configuration, the Src parameter, is used.
  133. //
  134. ////////////////////////////////////////////////////////////////////////////////
  135. void CWlbs_Root::UpdateConfigProp
  136. (
  137. wstring& a_szDest,
  138. const wstring& a_szSrc,
  139. LPCWSTR a_szPropName,
  140. IWbemClassObject* a_pNodeInstance
  141. )
  142. {
  143. HRESULT hRes = NULL;
  144. VARIANT vNewValue;
  145. try {
  146. VariantInit( &vNewValue );
  147. hRes = a_pNodeInstance->Get( _bstr_t( a_szPropName ),
  148. 0,
  149. &vNewValue,
  150. NULL,
  151. NULL );
  152. if( FAILED( hRes ) )
  153. throw _com_error( hRes );
  154. if( vNewValue.vt != VT_NULL )
  155. a_szDest.assign( vNewValue.bstrVal ); //update to new value
  156. else
  157. a_szDest = a_szSrc; //keep original value
  158. // CLD: Need to check return code for error
  159. if (S_OK != VariantClear( &vNewValue ))
  160. throw _com_error( WBEM_E_FAILED );
  161. }
  162. catch(...) {
  163. // CLD: Need to check return code for error
  164. // No throw here since we are already throwing an exception.
  165. VariantClear( &vNewValue );
  166. throw;
  167. }
  168. }
  169. ////////////////////////////////////////////////////////////////////////////////
  170. //
  171. // CWlbs_Root::UpdateConfigProp
  172. //
  173. // Purpose: This extracts the propery name and value from the WBEM object and if
  174. // the property is not set to VT_NULL then the configuration is updated
  175. // else the original configuration, the Src parameter, is used.
  176. //
  177. ////////////////////////////////////////////////////////////////////////////////
  178. void CWlbs_Root::UpdateConfigProp
  179. (
  180. bool& a_bDest,
  181. bool a_bSrc,
  182. LPCWSTR a_szPropName,
  183. IWbemClassObject* a_pNodeInstance
  184. )
  185. {
  186. BSTR strPropName = NULL;
  187. HRESULT hRes = NULL;
  188. VARIANT vNewValue;
  189. try {
  190. VariantInit( &vNewValue );
  191. hRes = a_pNodeInstance->Get
  192. (
  193. _bstr_t( a_szPropName ),
  194. 0,
  195. &vNewValue,
  196. NULL,
  197. NULL
  198. );
  199. if( FAILED( hRes ) )
  200. throw _com_error( hRes );
  201. if( vNewValue.vt != VT_NULL )
  202. a_bDest = (vNewValue.boolVal != 0); //update to new value
  203. else
  204. a_bDest = a_bSrc; //keep original value
  205. // CLD: Need to check return code for error
  206. if (S_OK != VariantClear( &vNewValue ))
  207. throw _com_error( WBEM_E_FAILED );
  208. SysFreeString( strPropName );
  209. }
  210. catch(...) {
  211. // CLD: Need to check return code for error
  212. // No throw here since we are already throwing an exception.
  213. VariantClear( &vNewValue );
  214. throw;
  215. }
  216. }
  217. ////////////////////////////////////////////////////////////////////////////////
  218. //
  219. // CWlbs_Root::UpdateConfigProp
  220. //
  221. // Purpose: This extracts the propery name and value from the WBEM object and if
  222. // the property is not set to VT_NULL then the configuration is updated
  223. // else the original configuration, the Src parameter, is used.
  224. //
  225. ////////////////////////////////////////////////////////////////////////////////
  226. void CWlbs_Root::UpdateConfigProp
  227. (
  228. DWORD& a_dwDest,
  229. DWORD a_dwSrc,
  230. LPCWSTR a_szPropName,
  231. IWbemClassObject* a_pNodeInstance
  232. )
  233. {
  234. HRESULT hRes = NULL;
  235. VARIANT vNewValue;
  236. try {
  237. VariantInit( &vNewValue );
  238. hRes = a_pNodeInstance->Get( _bstr_t( a_szPropName ),
  239. 0,
  240. &vNewValue,
  241. NULL,
  242. NULL );
  243. if( FAILED( hRes ) )
  244. throw _com_error( hRes );
  245. if( vNewValue.vt != VT_NULL )
  246. a_dwDest = vNewValue.lVal;
  247. else
  248. a_dwDest = a_dwSrc;
  249. // CLD: Need to check return code for error
  250. if (S_OK != VariantClear( &vNewValue ))
  251. throw _com_error( WBEM_E_FAILED );
  252. }
  253. catch(...) {
  254. // CLD: Need to check return code for error
  255. // No throw here since we are already throwing an exception.
  256. VariantClear( &vNewValue );
  257. throw;
  258. }
  259. }
  260. ////////////////////////////////////////////////////////////////////////////////
  261. //
  262. // CWlbs_Root::CreateExtendedStatus
  263. //
  264. // Purpose: Spawn and fill a Wbem extended status object with error
  265. // information.
  266. //
  267. ////////////////////////////////////////////////////////////////////////////////
  268. void CWlbs_Root::CreateExtendedStatus
  269. (
  270. CWbemServices* a_pNameSpace,
  271. IWbemClassObject** a_ppWbemInstance,
  272. DWORD a_dwErrorCode,
  273. LPCWSTR /*a_szDescription*/
  274. )
  275. {
  276. HRESULT hResult;
  277. IWbemClassObjectPtr pWlbsExtendedObject;
  278. try {
  279. ASSERT(a_ppWbemInstance);
  280. ASSERT(a_pNameSpace );
  281. //this is the only routine that references
  282. //the MicrosoftNLB_ExtendedStatus object
  283. //if other routines start to use the class,
  284. //or if it adds additional properties, then
  285. //is should be added to the MOF data files
  286. //along with the other classes
  287. //get the MOF class object
  288. hResult = a_pNameSpace->GetObject(
  289. _bstr_t( L"MicrosoftNLB_ExtendedStatus" ),
  290. 0,
  291. NULL,
  292. &pWlbsExtendedObject,
  293. NULL);
  294. if( FAILED(hResult) ) {
  295. throw _com_error(hResult);
  296. }
  297. //spawn an instance
  298. hResult = pWlbsExtendedObject->SpawnInstance( 0, a_ppWbemInstance );
  299. if( FAILED( hResult ) )
  300. throw _com_error( hResult );
  301. //this is omitted in release due to internationalization concerns
  302. /*
  303. #if 0
  304. //fill instance with data
  305. //add description
  306. hResult = (*a_ppWbemInstance)->Put
  307. (
  308. _bstr_t( L"Description" ) ,
  309. 0 ,
  310. &( _variant_t( a_szDescription ) ) ,
  311. NULL
  312. );
  313. #endif
  314. */
  315. if( FAILED( hResult ) )
  316. throw _com_error( hResult );
  317. //add status code
  318. hResult = (*a_ppWbemInstance)->Put
  319. (
  320. _bstr_t( L"StatusCode" ) ,
  321. 0 ,
  322. &( _variant_t( (long)a_dwErrorCode ) ) ,
  323. NULL
  324. );
  325. if( FAILED( hResult ) )
  326. throw _com_error( hResult );
  327. }
  328. catch(...) {
  329. if( *a_ppWbemInstance )
  330. (*a_ppWbemInstance)->Release();
  331. throw;
  332. }
  333. }
  334. //+----------------------------------------------------------------------------
  335. //
  336. // Function: CWlbs_Root::ExtractHostID
  337. //
  338. // Description: Extract the Host ID from name "clusterIp:HostId"
  339. //
  340. // Arguments: const wstring& a_wstrName -
  341. //
  342. //
  343. // Returns: DWORD - Host ID, or -1 if failed
  344. //
  345. // History: fengsun Created Header 7/13/00
  346. //
  347. //+----------------------------------------------------------------------------
  348. DWORD CWlbs_Root::ExtractHostID(const wstring& a_wstrName)
  349. {
  350. long nColPos;
  351. nColPos = a_wstrName.find( L":" );
  352. if (nColPos == wstring::npos)
  353. {
  354. //
  355. // Not found
  356. //
  357. TRACE_ERROR1("CWlbs_Root::ExtractHostID invalid name %ws", a_wstrName.c_str());
  358. return (DWORD)-1;
  359. }
  360. wstring wstrHostID = a_wstrName.substr( nColPos+1, a_wstrName.size()-1 );
  361. return _wtol( wstrHostID.c_str() );
  362. }
  363. //+----------------------------------------------------------------------------
  364. //
  365. // Function: CWlbs_Root::ExtractClusterIP
  366. //
  367. // Description: Extract the cluster IP address from name "clusterIp:HostId"
  368. //
  369. // Arguments: const wstring& a_wstrName -
  370. //
  371. //
  372. // Returns: DWORD - Cluster IP, or INADDR_NONE (-1) if falied
  373. //
  374. // History: fengsun Created Header 7/13/00
  375. //
  376. //+----------------------------------------------------------------------------
  377. DWORD CWlbs_Root::ExtractClusterIP(const wstring& a_wstrName)
  378. {
  379. long nColPos;
  380. nColPos = a_wstrName.find( L":" );
  381. if (nColPos == wstring::npos)
  382. {
  383. //
  384. // Not found
  385. //
  386. TRACE_ERROR1("CWlbs_Root::ExtractClusterIP invalid name %ws", a_wstrName.c_str());
  387. return (DWORD)-1;
  388. }
  389. wstring wstrClusterIP = a_wstrName.substr( 0, nColPos );
  390. return WlbsResolve( wstrClusterIP.c_str() );
  391. }
  392. ////////////////////////////////////////////////////////////////////////////////
  393. //
  394. // CWlbs_Root::ConstructHostName
  395. //
  396. // Purpose:
  397. //
  398. ////////////////////////////////////////////////////////////////////////////////
  399. void CWlbs_Root::ConstructHostName
  400. (
  401. wstring& a_wstrHostName,
  402. DWORD a_dwClusIP,
  403. DWORD a_dwHostID
  404. )
  405. {
  406. WCHAR wszHostID[40];
  407. AddressToString( a_dwClusIP, a_wstrHostName );
  408. a_wstrHostName += L':';
  409. a_wstrHostName += _ltow( a_dwHostID, wszHostID, 10 );
  410. }
  411. CWlbsClusterWrapper* GetClusterFromHostName(CWlbsControlWrapper* pControl,
  412. wstring wstrHostName)
  413. {
  414. DWORD dwClusterIpOrIndex = CWlbs_Root::ExtractClusterIP(wstrHostName);
  415. return pControl->GetClusterFromIpOrIndex(dwClusterIpOrIndex);
  416. }