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.

601 lines
16 KiB

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