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.

428 lines
11 KiB

  1. #include "WLBS_Provider.h"
  2. #include "WLBS_NodeSetPortRule.h"
  3. #include "ClusterWrapper.h"
  4. #include "ControlWrapper.h"
  5. #include "utils.h"
  6. ////////////////////////////////////////////////////////////////////////////////
  7. //
  8. // CWLBS_NodeSetPortRule::CWLBS_NodeSetPortRule
  9. //
  10. // Purpose: Constructor
  11. //
  12. ////////////////////////////////////////////////////////////////////////////////
  13. CWLBS_NodeSetPortRule::CWLBS_NodeSetPortRule(CWbemServices* a_pNameSpace,
  14. IWbemObjectSink* a_pResponseHandler)
  15. : CWlbs_Root( a_pNameSpace, a_pResponseHandler )
  16. {
  17. }
  18. ////////////////////////////////////////////////////////////////////////////////
  19. //
  20. // CWLBS_NodeSetPortRule::Create
  21. //
  22. // Purpose: This instantiates this class and is invoked from an array of
  23. // function pointers.
  24. //
  25. ////////////////////////////////////////////////////////////////////////////////
  26. CWlbs_Root* CWLBS_NodeSetPortRule::Create
  27. (
  28. CWbemServices* a_pNameSpace,
  29. IWbemObjectSink* a_pResponseHandler
  30. )
  31. {
  32. CWlbs_Root* pRoot = new CWLBS_NodeSetPortRule( a_pNameSpace, a_pResponseHandler );
  33. if( !pRoot )
  34. throw _com_error( WBEM_E_OUT_OF_MEMORY );
  35. return pRoot;
  36. }
  37. ////////////////////////////////////////////////////////////////////////////////
  38. //
  39. // CWLBS_NodeSetPortRule::GetInstance
  40. //
  41. // Purpose:
  42. //
  43. ////////////////////////////////////////////////////////////////////////////////
  44. HRESULT CWLBS_NodeSetPortRule::GetInstance
  45. (
  46. const ParsedObjectPath* /* a_pParsedPath */,
  47. long /* a_lFlags */,
  48. IWbemContext* /* a_pIContex */
  49. )
  50. {
  51. IWbemClassObject* pWlbsInstance = NULL;
  52. HRESULT hRes = 0;
  53. try {
  54. //TODO: remove
  55. throw _com_error( WBEM_E_NOT_SUPPORTED );
  56. }
  57. catch(CErrorWlbsControl Err) {
  58. IWbemClassObject* pWbemExtStat = NULL;
  59. CreateExtendedStatus( m_pNameSpace,
  60. &pWbemExtStat,
  61. Err.Error(),
  62. (PWCHAR)(Err.Description()) );
  63. m_pResponseHandler->SetStatus(0, WBEM_E_FAILED, NULL, pWbemExtStat);
  64. if( pWbemExtStat )
  65. pWbemExtStat->Release();
  66. if( pWlbsInstance )
  67. pWlbsInstance->Release();
  68. //do not return WBEM_E_FAILED, this causes a race condition
  69. hRes = WBEM_S_NO_ERROR;
  70. }
  71. catch(_com_error HResErr ) {
  72. m_pResponseHandler->SetStatus(0, HResErr.Error(), NULL, NULL);
  73. if( pWlbsInstance )
  74. pWlbsInstance->Release();
  75. hRes = HResErr.Error();
  76. }
  77. catch(...) {
  78. if( pWlbsInstance )
  79. pWlbsInstance->Release();
  80. throw;
  81. }
  82. return hRes;
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////
  85. //
  86. // CWLBS_NodeSetPortRule::EnumInstances
  87. //
  88. // Purpose: Queries WLBS for desired node instances then constructs an
  89. // an associator for each node found.
  90. //
  91. ////////////////////////////////////////////////////////////////////////////////
  92. HRESULT CWLBS_NodeSetPortRule::EnumInstances
  93. (
  94. BSTR /* a_bstrClass */,
  95. long /* a_lFlags */,
  96. IWbemContext* /* a_pIContex */
  97. )
  98. {
  99. IWbemClassObject** ppWlbsInstance = NULL;
  100. PWLBS_PORT_RULE pPortRules = NULL;
  101. DWORD dwNumRules = 0;
  102. HRESULT hRes = 0;
  103. try {
  104. DWORD dwNumClusters = 0;
  105. CWlbsClusterWrapper** ppCluster = NULL;
  106. g_pWlbsControl->EnumClusters(ppCluster, &dwNumClusters);
  107. if (dwNumClusters == 0)
  108. {
  109. throw _com_error( WBEM_E_NOT_FOUND );
  110. }
  111. //declare an IWbemClassObject smart pointer
  112. IWbemClassObjectPtr pWlbsClass;
  113. //get the MOF class object
  114. hRes = m_pNameSpace->GetObject(
  115. _bstr_t( MOF_NODESETTINGPORTRULE::szName ),
  116. 0,
  117. NULL,
  118. &pWlbsClass,
  119. NULL );
  120. if( FAILED( hRes ) ) {
  121. throw _com_error( hRes );
  122. }
  123. for (DWORD iCluster=0; iCluster < dwNumClusters; iCluster++)
  124. {
  125. // The "NodeSettingPortRule" class associates an instance of "NodeSetting" class
  126. // with an instance of "PortRule" class. The PortRule class does NOT contain
  127. // VIP as a property, so we do not want to return any instance of the "NodeSettingPortRule"
  128. // class that associates a port rule that is specific to a vip (other than the "all vip").
  129. // The "EffectiveVersion" registry value is checked for a value of CVY_VERSION_FULL to
  130. // see of there is any port rule that is specific to a vip
  131. CNodeConfiguration NodeConfig;
  132. ppCluster[iCluster]->GetNodeConfig(NodeConfig);
  133. if(NodeConfig.dwEffectiveVersion == CVY_VERSION_FULL)
  134. continue;
  135. //call the API query function to find all the port rules
  136. ppCluster[iCluster]->EnumPortRules( &pPortRules, &dwNumRules, 0 );
  137. if( dwNumRules == 0 )
  138. continue;
  139. //spawn an instance of the nodesetting portrule associator
  140. //for each portrule found
  141. ppWlbsInstance = new IWbemClassObject *[dwNumRules];
  142. if( !ppWlbsInstance )
  143. throw _com_error( WBEM_E_OUT_OF_MEMORY );
  144. //initialize array
  145. ZeroMemory( ppWlbsInstance, dwNumRules * sizeof(IWbemClassObject *) );
  146. for(DWORD i = 0; i < dwNumRules; i ++ ) {
  147. hRes = pWlbsClass->SpawnInstance( 0, &ppWlbsInstance[i] );
  148. if( FAILED( hRes ) )
  149. throw _com_error( hRes );
  150. FillWbemInstance(ppCluster[iCluster], *(ppWlbsInstance + i), pPortRules + i );
  151. }
  152. //send the results back to WinMgMt
  153. hRes = m_pResponseHandler->Indicate( dwNumRules, ppWlbsInstance );
  154. if( FAILED( hRes ) ) {
  155. throw _com_error( hRes );
  156. }
  157. if( ppWlbsInstance ) {
  158. for( i = 0; i < dwNumRules; i++ ) {
  159. if( ppWlbsInstance[i] ) {
  160. ppWlbsInstance[i]->Release();
  161. }
  162. }
  163. delete [] ppWlbsInstance;
  164. }
  165. if( pPortRules )
  166. delete [] pPortRules;
  167. }
  168. m_pResponseHandler->SetStatus( 0, WBEM_S_NO_ERROR, NULL, NULL );
  169. hRes = WBEM_S_NO_ERROR;
  170. }
  171. catch(CErrorWlbsControl Err) {
  172. IWbemClassObject* pWbemExtStat = NULL;
  173. CreateExtendedStatus( m_pNameSpace,
  174. &pWbemExtStat,
  175. Err.Error(),
  176. (PWCHAR)(Err.Description()) );
  177. m_pResponseHandler->SetStatus(0, WBEM_E_FAILED, NULL, pWbemExtStat);
  178. if( pWbemExtStat )
  179. pWbemExtStat->Release();
  180. if( ppWlbsInstance ) {
  181. for(DWORD i = 0; i < dwNumRules; i++ ) {
  182. if( ppWlbsInstance[i] ) {
  183. ppWlbsInstance[i]->Release();
  184. }
  185. }
  186. delete [] ppWlbsInstance;
  187. }
  188. if( pPortRules )
  189. delete [] pPortRules;
  190. //do not return WBEM_E_FAILED, this causes a race condition
  191. hRes = WBEM_S_NO_ERROR;
  192. }
  193. catch(_com_error HResErr ) {
  194. m_pResponseHandler->SetStatus(0, HResErr.Error(), NULL, NULL);
  195. if( ppWlbsInstance ) {
  196. for(DWORD i = 0; i < dwNumRules; i++ ) {
  197. if( ppWlbsInstance[i] ) {
  198. ppWlbsInstance[i]->Release();
  199. }
  200. }
  201. delete [] ppWlbsInstance;
  202. }
  203. if( pPortRules )
  204. delete [] pPortRules;
  205. hRes = HResErr.Error();
  206. }
  207. catch(...) {
  208. if( ppWlbsInstance ) {
  209. for(DWORD i = 0; i < dwNumRules; i++ ) {
  210. if( ppWlbsInstance[i] ) {
  211. ppWlbsInstance[i]->Release();
  212. }
  213. }
  214. delete [] ppWlbsInstance;
  215. }
  216. if( pPortRules )
  217. delete [] pPortRules;
  218. throw;
  219. }
  220. return hRes;
  221. }
  222. ////////////////////////////////////////////////////////////////////////////////
  223. //
  224. // CWLBS_NodeSetPortRule::FindInstance
  225. //
  226. // Purpose: Returns the requested associator.
  227. //
  228. ////////////////////////////////////////////////////////////////////////////////
  229. void CWLBS_NodeSetPortRule::FindInstance
  230. (
  231. IWbemClassObject** /* a_ppWbemInstance */,
  232. const ParsedObjectPath* /* a_pParsedPath */
  233. )
  234. {
  235. }
  236. ////////////////////////////////////////////////////////////////////////////////
  237. //
  238. // CWLBS_NodeSetPortRule::FillWbemInstance
  239. //
  240. // Purpose: This constructs the ParticipatingNode wbem associator.
  241. //
  242. ////////////////////////////////////////////////////////////////////////////////
  243. void CWLBS_NodeSetPortRule::FillWbemInstance
  244. (
  245. CWlbsClusterWrapper* pCluster,
  246. IWbemClassObject* a_pWbemInstance,
  247. PWLBS_PORT_RULE a_pPortRule
  248. )
  249. {
  250. namespace NSPR = MOF_NODESETTINGPORTRULE;
  251. ASSERT( a_pWbemInstance );
  252. ASSERT( a_pPortRule );
  253. ParsedObjectPath NodeSetPath;
  254. ParsedObjectPath PRPath;
  255. LPWSTR szNodeSetPath = NULL;
  256. LPWSTR szPRPath = NULL;
  257. try {
  258. //set the names of the classes
  259. if( !NodeSetPath.SetClassName( MOF_NODESETTING::szName ) )
  260. throw _com_error( WBEM_E_FAILED );
  261. //determine the type of port rule to create
  262. switch( a_pPortRule->mode ) {
  263. case WLBS_SINGLE:
  264. if( !PRPath.SetClassName( MOF_PRFAIL::szName ) )
  265. throw _com_error( WBEM_E_FAILED );
  266. break;
  267. case WLBS_MULTI:
  268. if( !PRPath.SetClassName( MOF_PRLOADBAL::szName ) )
  269. throw _com_error( WBEM_E_FAILED );
  270. break;
  271. case WLBS_NEVER:
  272. if( !PRPath.SetClassName( MOF_PRDIS::szName ) )
  273. throw _com_error( WBEM_E_FAILED );
  274. break;
  275. default:
  276. throw _com_error( WBEM_E_FAILED );
  277. }
  278. wstring wstrHostName;
  279. ConstructHostName( wstrHostName, pCluster->GetClusterIpOrIndex(g_pWlbsControl),
  280. pCluster->GetHostID() );
  281. //set the keys for the node setting
  282. if( !NodeSetPath.AddKeyRef( MOF_NODESETTING::pProperties[MOF_NODESETTING::NAME],
  283. &_variant_t(wstrHostName.c_str()) ) )
  284. throw _com_error( WBEM_E_FAILED );
  285. //set the keys for the port rule
  286. if( !PRPath.AddKeyRef( MOF_PORTRULE::pProperties[MOF_PORTRULE::NAME],
  287. &_variant_t(wstrHostName.c_str())) )
  288. throw _com_error( WBEM_E_FAILED );
  289. //start port key
  290. if( !PRPath.AddKeyRef( MOF_PORTRULE::pProperties[MOF_PORTRULE::STPORT],
  291. &_variant_t((long)a_pPortRule->start_port)) )
  292. throw _com_error( WBEM_E_FAILED );
  293. //convert parsed object paths to strings
  294. if (CObjectPathParser::Unparse(&NodeSetPath, &szNodeSetPath) != CObjectPathParser::NoError)
  295. throw _com_error( WBEM_E_FAILED );
  296. if (CObjectPathParser::Unparse(&PRPath, &szPRPath) != CObjectPathParser::NoError)
  297. throw _com_error( WBEM_E_FAILED );
  298. //Node setting reference
  299. HRESULT hRes = a_pWbemInstance->Put
  300. (
  301. _bstr_t( NSPR::pProperties[NSPR::NODESET] ),
  302. 0,
  303. &_variant_t(szNodeSetPath),
  304. NULL
  305. );
  306. if( FAILED( hRes ) )
  307. throw _com_error( hRes );
  308. //Port rule reference
  309. hRes = a_pWbemInstance->Put
  310. (
  311. _bstr_t( NSPR::pProperties[NSPR::PORTRULE] ),
  312. 0,
  313. &_variant_t(szPRPath),
  314. NULL
  315. );
  316. if( FAILED( hRes ) )
  317. throw _com_error( hRes );
  318. //free resources
  319. NodeSetPath.ClearKeys();
  320. PRPath.ClearKeys();
  321. if( szNodeSetPath )
  322. delete (szNodeSetPath);
  323. if( szPRPath )
  324. delete (szPRPath);
  325. } catch (...) {
  326. NodeSetPath.ClearKeys();
  327. PRPath.ClearKeys();
  328. if( szNodeSetPath )
  329. delete (szNodeSetPath);
  330. if( szPRPath )
  331. delete (szPRPath);
  332. throw;
  333. }
  334. }