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.

592 lines
15 KiB

  1. // Copyright (c) Microsoft. All rights reserved.
  2. //
  3. // This is unpublished source code of Microsoft.
  4. // The copyright notice above does not evidence any
  5. // actual or intended publication of such source code.
  6. // OneLiner : Implementation of MIPAddressAdmin
  7. // DevUnit : wlbstest
  8. // Author : Murtaza Hakim
  9. // include files
  10. #include "MIPAddressAdmin.h"
  11. #include "MWmiParameter.h"
  12. #include "Mtrace.h"
  13. #include "NICCard.h"
  14. #include <algorithm>
  15. using namespace std;
  16. // constructor for doing remotely.
  17. //
  18. MIPAddressAdmin::MIPAddressAdmin( const _bstr_t& machineIP,
  19. const _bstr_t& nicName )
  20. : _machineIP( machineIP ),
  21. _nicName( nicName ),
  22. machine( machineIP,
  23. L"root\\cimv2",
  24. NLBMGR_USERNAME,
  25. NLBMGR_PASSWORD)
  26. {
  27. }
  28. // constructor for doing things locally.
  29. //
  30. MIPAddressAdmin::MIPAddressAdmin( const _bstr_t& nicName )
  31. : _machineIP( L"Not Set" ),
  32. _nicName( nicName ),
  33. machine( L"root\\cimv2" )
  34. {
  35. }
  36. // copy constructor
  37. //
  38. MIPAddressAdmin::MIPAddressAdmin( const MIPAddressAdmin& obj )
  39. : _machineIP( obj._machineIP ),
  40. _nicName( obj._nicName ),
  41. machine( obj.machine )
  42. {
  43. }
  44. // assignment operator
  45. //
  46. MIPAddressAdmin&
  47. MIPAddressAdmin::operator=(const MIPAddressAdmin& rhs )
  48. {
  49. _machineIP = rhs._machineIP;
  50. _nicName = rhs._nicName;
  51. machine = rhs.machine;
  52. return *this;
  53. }
  54. // destructor
  55. //
  56. MIPAddressAdmin::~MIPAddressAdmin()
  57. {
  58. }
  59. MIPAddressAdmin::MIPAddressAdmin_Error
  60. MIPAddressAdmin::refreshConnection()
  61. {
  62. return status;
  63. }
  64. // addIPAddress
  65. //
  66. MIPAddressAdmin::MIPAddressAdmin_Error
  67. MIPAddressAdmin::addIPAddress(const _bstr_t& ipAddrToAdd,
  68. const _bstr_t& subnetMask)
  69. {
  70. // do basic verification.
  71. //
  72. // ensure that machine specified exists.
  73. vector<MWmiInstance> nicInstance;
  74. checkStatus( &nicInstance );
  75. // get present ip addresses.
  76. //
  77. vector<_bstr_t> ipAddressStore;
  78. vector<_bstr_t> subnetMaskStore;
  79. bool dhcpEnabled;
  80. isDHCPEnabled( dhcpEnabled );
  81. if( dhcpEnabled == false )
  82. {
  83. getIPAddresses( &ipAddressStore,
  84. &subnetMaskStore );
  85. }
  86. if( find( ipAddressStore.begin(),
  87. ipAddressStore.end(),
  88. ipAddrToAdd )
  89. != ipAddressStore.end() )
  90. {
  91. // the ip to add already exists.
  92. // thus just return at this point.
  93. return MIPAddressAdmin_SUCCESS;
  94. }
  95. ipAddressStore.push_back( ipAddrToAdd );
  96. subnetMaskStore.push_back( subnetMask );
  97. // form the safearray.
  98. //
  99. // array has one dimension.
  100. //
  101. SAFEARRAYBOUND rgsabound[1];
  102. rgsabound[0].lLbound = 0;
  103. rgsabound[0].cElements = ipAddressStore.size();
  104. SAFEARRAY* psaIPAddress = SafeArrayCreate( VT_BSTR, 1, rgsabound );
  105. SAFEARRAY* psaSubnetMask = SafeArrayCreate( VT_BSTR, 1, rgsabound );
  106. long rgIndices;
  107. HRESULT hr;
  108. for( int i = 0; i < ipAddressStore.size(); ++i )
  109. {
  110. rgIndices = i;
  111. hr = SafeArrayPutElement( psaIPAddress,
  112. &rgIndices,
  113. ( wchar_t *) ipAddressStore[i]
  114. );
  115. hr = SafeArrayPutElement( psaSubnetMask,
  116. &rgIndices,
  117. ( wchar_t *) subnetMaskStore[i]
  118. );
  119. }
  120. // run enable static method.
  121. VARIANT ipsV;
  122. VARIANT ipaV;
  123. VariantInit( &ipsV );
  124. VariantInit( &ipaV );
  125. ipaV.parray = psaIPAddress;
  126. ipaV.vt = VT_ARRAY | VT_BSTR;
  127. ipsV.parray = psaSubnetMask;
  128. ipsV.vt = VT_ARRAY | VT_BSTR;
  129. vector<MWmiParameter *> inputParameters;
  130. MWmiParameter ipa(L"IPAddress");
  131. ipa.setValue( ipaV );
  132. inputParameters.push_back( &ipa );
  133. MWmiParameter ips(L"SubnetMask");
  134. ips.setValue( ipsV );
  135. inputParameters.push_back( &ips );
  136. // set output parameters
  137. vector<MWmiParameter *> outputParameters;
  138. MWmiParameter returnValue(L"ReturnValue");
  139. outputParameters.push_back( &returnValue );
  140. nicInstance[0].runMethod(L"EnableStatic",
  141. inputParameters,
  142. outputParameters );
  143. VariantClear( &ipsV );
  144. VariantClear( &ipaV );
  145. // SafeArrayDestroy( psaIPAddress );
  146. // SafeArrayDestroy( psaSubnetMask );
  147. if( long( returnValue.getValue()) == 0 )
  148. {
  149. return MIPAddressAdmin_SUCCESS;
  150. }
  151. else
  152. {
  153. cout << "enablestatic has return " << long( returnValue.getValue() ) << endl;
  154. #if DBG
  155. WCHAR buf[256];
  156. wsprintf(buf, L"EnableStatic failed with error 0x%08lx\n", long( returnValue.getValue() ) );
  157. OutputDebugString(buf);
  158. #endif // DBG
  159. return COM_FAILURE;
  160. }
  161. }
  162. // deleteIPAddress
  163. //
  164. MIPAddressAdmin::MIPAddressAdmin_Error
  165. MIPAddressAdmin::deleteIPAddress(const _bstr_t& ipAddrToDelete )
  166. {
  167. // do basic verification.
  168. //
  169. // ensure that machine specified exists with nic.
  170. vector<MWmiInstance> nicInstance;
  171. checkStatus( &nicInstance );
  172. // check if dhcp is enabled,
  173. // if enabled we cannot delete anything
  174. bool dhcpEnabled;
  175. isDHCPEnabled( dhcpEnabled );
  176. if( dhcpEnabled == true )
  177. {
  178. return NOT_SUPPORTED;
  179. }
  180. // get present ip addresses.
  181. //
  182. vector<_bstr_t> ipAddressStore;
  183. vector<_bstr_t> subnetMaskStore;
  184. getIPAddresses( &ipAddressStore,
  185. &subnetMaskStore );
  186. // check if ip address to delete exists.
  187. //
  188. vector<_bstr_t>::iterator posnToDelete;
  189. posnToDelete = find( ipAddressStore.begin(),
  190. ipAddressStore.end(),
  191. ipAddrToDelete );
  192. if( posnToDelete
  193. == ipAddressStore.end() )
  194. {
  195. // the ip to delete does not exist.
  196. return NO_SUCH_IP;
  197. }
  198. // remove this ip.
  199. vector<_bstr_t> ipAddressNewStore;
  200. vector<_bstr_t> subnetMaskNewStore;
  201. bool ipAddrToDeleteFound = false;
  202. for( int i = 0; i < ipAddressStore.size(); ++i )
  203. {
  204. if( ipAddressStore[i] == ipAddrToDelete )
  205. {
  206. ipAddrToDeleteFound = true;
  207. }
  208. else
  209. {
  210. ipAddressNewStore.push_back( ipAddressStore[i] );
  211. subnetMaskNewStore.push_back( subnetMaskStore[i] );
  212. }
  213. }
  214. if( ipAddrToDeleteFound == false )
  215. {
  216. // ip to delete does not exist on
  217. // this nic.
  218. return NO_SUCH_IP;
  219. }
  220. if( ipAddressNewStore.size() == 0 )
  221. {
  222. // the ip to remove is the last ip which exists.
  223. // thus need to switch to dhcp.
  224. //
  225. return enableDHCP();
  226. }
  227. // form the safearray.
  228. // array has one dimension.
  229. //
  230. SAFEARRAYBOUND rgsabound[1];
  231. rgsabound[0].lLbound = 0;
  232. rgsabound[0].cElements = ipAddressNewStore.size();
  233. SAFEARRAY* psaIPAddress = SafeArrayCreate( VT_BSTR, 1, rgsabound );
  234. SAFEARRAY* psaSubnetMask = SafeArrayCreate( VT_BSTR, 1, rgsabound );
  235. long rgIndices[1];
  236. HRESULT hr;
  237. for( int i = 0; i < ipAddressNewStore.size(); ++i )
  238. {
  239. rgIndices[0] = i;
  240. hr = SafeArrayPutElement( psaIPAddress,
  241. rgIndices,
  242. ( wchar_t *) ipAddressNewStore[i]
  243. );
  244. hr = SafeArrayPutElement( psaSubnetMask,
  245. rgIndices,
  246. ( wchar_t *) subnetMaskNewStore[i]
  247. );
  248. }
  249. // run enable static method.
  250. VARIANT ipsV;
  251. VARIANT ipaV;
  252. ipsV.parray = psaSubnetMask;
  253. ipsV.vt = VT_ARRAY | VT_BSTR;
  254. ipaV.parray = psaIPAddress;
  255. ipaV.vt = VT_ARRAY | VT_BSTR;
  256. vector<MWmiParameter *> inputParameters;
  257. MWmiParameter ipa(L"IPAddress");
  258. ipa.setValue( ipaV );
  259. inputParameters.push_back( &ipa );
  260. MWmiParameter ips(L"SubnetMask");
  261. ips.setValue( ipsV );
  262. inputParameters.push_back( &ips );
  263. // set output parameters
  264. vector<MWmiParameter *> outputParameters;
  265. MWmiParameter returnValue(L"ReturnValue");
  266. outputParameters.push_back( &returnValue );
  267. nicInstance[0].runMethod(L"EnableStatic",
  268. inputParameters,
  269. outputParameters );
  270. VariantClear( &ipsV );
  271. VariantClear( &ipaV );
  272. // SafeArrayDestroy( psaIPAddress );
  273. // SafeArrayDestroy( psaSubnetMask );
  274. if( long( returnValue.getValue()) == 0 )
  275. {
  276. return MIPAddressAdmin_SUCCESS;
  277. }
  278. else
  279. {
  280. cout << "enablestatic has return " << long( returnValue.getValue() ) << endl;
  281. return COM_FAILURE;
  282. }
  283. return MIPAddressAdmin_SUCCESS;
  284. }
  285. // getIPAddresses
  286. //
  287. MIPAddressAdmin::MIPAddressAdmin_Error
  288. MIPAddressAdmin::getIPAddresses( vector<_bstr_t>* ipAddress,
  289. vector<_bstr_t>* subnetMask )
  290. {
  291. // do basic verification.
  292. //
  293. // ensure that machine specified exists.
  294. vector<MWmiInstance> nicInstance;
  295. checkStatus( &nicInstance );
  296. // get the present values for ip address and subnet
  297. //
  298. vector<MWmiParameter* > parameterStore;
  299. MWmiParameter ipsPresent(L"IPSubnet");
  300. parameterStore.push_back( &ipsPresent );
  301. MWmiParameter ipaPresent(L"IPAddress");
  302. parameterStore.push_back( &ipaPresent );
  303. nicInstance[0].getParameters( parameterStore );
  304. _variant_t ipsT = ipsPresent.getValue();
  305. VARIANT ipsV = ipsT.Detach();
  306. _variant_t ipaT = ipaPresent.getValue();
  307. VARIANT ipaV = ipaT.Detach();
  308. if ( (ipsV.vt != (VT_ARRAY | VT_BSTR))
  309. || (ipaV.vt != (VT_ARRAY | VT_BSTR)))
  310. {
  311. // Ugh, let's pretend we didn't get anything.
  312. goto end;
  313. }
  314. LONG count = ipsV.parray->rgsabound[0].cElements;
  315. HRESULT hr;
  316. BSTR* pbstr;
  317. BSTR* pbstr1;
  318. if( SUCCEEDED( SafeArrayAccessData( ipaV.parray, ( void **) &pbstr)))
  319. {
  320. hr = SafeArrayAccessData( ipsV.parray, (void **) &pbstr1 );
  321. for( LONG x = 0; x < count; x++ )
  322. {
  323. ipAddress->push_back( _bstr_t( pbstr[x] ) );
  324. subnetMask->push_back( _bstr_t( pbstr1[x] ) );
  325. }
  326. hr = SafeArrayUnaccessData( ipsV.parray );
  327. hr = SafeArrayUnaccessData( ipaV.parray );
  328. }
  329. end:
  330. VariantClear( &ipsV );
  331. VariantClear( &ipaV );
  332. return MIPAddressAdmin_SUCCESS;
  333. }
  334. // checkStatus
  335. MIPAddressAdmin::MIPAddressAdmin_Error
  336. MIPAddressAdmin::checkStatus( vector<MWmiInstance>* nicInstance )
  337. {
  338. _bstr_t temp;
  339. if( _machineIP == _bstr_t( L"Not Set") )
  340. {
  341. NICCard::NICCard_Error errN;
  342. vector< NICCard::Info > nicList;
  343. errN = NICCard::getNics( &nicList );
  344. if( errN != NICCard::NICCard_SUCCESS )
  345. {
  346. throw _com_error( WBEM_E_NOT_FOUND );
  347. }
  348. bool foundNic = false;
  349. _bstr_t myguid;
  350. for( int i = 0; i < nicList.size(); ++i )
  351. {
  352. if( _bstr_t( nicList[i].fullName.c_str() ) == _nicName )
  353. {
  354. // found nic
  355. foundNic = true;
  356. myguid = _bstr_t( nicList[i].guid.c_str() );
  357. break;
  358. }
  359. }
  360. if( foundNic == false )
  361. {
  362. throw _com_error( WBEM_E_NOT_FOUND );
  363. }
  364. vector< MWmiInstance > instanceStore;
  365. machine.getInstances( L"Win32_NetworkAdapterConfiguration",
  366. &instanceStore );
  367. // set parameters to get.
  368. vector<MWmiParameter* > parameterStore;
  369. MWmiParameter SettingID(L"SettingID");
  370. parameterStore.push_back( &SettingID );
  371. for( int i = 0; i < instanceStore.size(); ++i )
  372. {
  373. instanceStore[i].getParameters( parameterStore );
  374. temp = SettingID.getValue();
  375. if( myguid == temp )
  376. {
  377. nicInstance->push_back( instanceStore[i] );
  378. return MIPAddressAdmin_SUCCESS;
  379. }
  380. }
  381. throw _com_error( WBEM_E_NOT_FOUND );
  382. }
  383. else
  384. {
  385. //
  386. // ensure that the nic specified exists.
  387. //
  388. MWmiObject machineNlbsNic( _machineIP,
  389. L"root\\microsoftnlb",
  390. NLBMGR_USERNAME,
  391. NLBMGR_PASSWORD);
  392. vector< MWmiInstance > instanceStoreNlbsNic;
  393. _bstr_t relPath =
  394. L"NlbsNic.FullName=\"" + _nicName + "\"";
  395. machineNlbsNic.getSpecificInstance( L"NlbsNic",
  396. relPath,
  397. &instanceStoreNlbsNic );
  398. // set parameters to get.
  399. vector<MWmiParameter* > parameterStoreNlbsNic;
  400. MWmiParameter AdapterGuid(L"AdapterGuid");
  401. parameterStoreNlbsNic.push_back( &AdapterGuid );
  402. instanceStoreNlbsNic[0].getParameters( parameterStoreNlbsNic );
  403. vector< MWmiInstance > instanceStore;
  404. machine.getInstances( L"Win32_NetworkAdapterConfiguration",
  405. &instanceStore );
  406. // set parameters to get.
  407. vector<MWmiParameter* > parameterStore;
  408. MWmiParameter SettingID(L"SettingID");
  409. parameterStore.push_back( &SettingID );
  410. for( int i = 0; i < instanceStore.size(); ++i )
  411. {
  412. instanceStore[i].getParameters( parameterStore );
  413. temp = SettingID.getValue();
  414. if( _bstr_t( AdapterGuid.getValue() ) == temp )
  415. {
  416. nicInstance->push_back( instanceStore[i] );
  417. return MIPAddressAdmin_SUCCESS;
  418. }
  419. }
  420. throw _com_error( WBEM_E_NOT_FOUND );
  421. }
  422. }
  423. // isDHCPEnabled
  424. //
  425. MIPAddressAdmin::MIPAddressAdmin_Error
  426. MIPAddressAdmin::isDHCPEnabled( bool& dhcpEnabled )
  427. {
  428. // do basic verification.
  429. //
  430. // ensure that machine specified exists.
  431. vector<MWmiInstance> nicInstance;
  432. checkStatus( &nicInstance );
  433. // get the present value for DHCPEnabled
  434. //
  435. vector<MWmiParameter* > parameterStore;
  436. MWmiParameter DHCPEnabled( L"DHCPEnabled" );
  437. parameterStore.push_back( &DHCPEnabled );
  438. nicInstance[0].getParameters( parameterStore );
  439. dhcpEnabled = DHCPEnabled.getValue();
  440. return MIPAddressAdmin_SUCCESS;
  441. }
  442. // enableDHCP
  443. //
  444. MIPAddressAdmin::MIPAddressAdmin_Error
  445. MIPAddressAdmin::enableDHCP()
  446. {
  447. // do basic verification.
  448. //
  449. // ensure that machine specified exists.
  450. vector<MWmiInstance> nicInstance;
  451. checkStatus( &nicInstance );
  452. bool dhcpEnabled;
  453. isDHCPEnabled( dhcpEnabled );
  454. if( dhcpEnabled == true )
  455. {
  456. // dhcp is already enabled.
  457. return MIPAddressAdmin_SUCCESS;
  458. }
  459. // set input parameters.
  460. // no input parameters.
  461. vector<MWmiParameter *> inputParameters;
  462. // set output parameters
  463. vector<MWmiParameter *> outputParameters;
  464. MWmiParameter returnValue(L"ReturnValue");
  465. outputParameters.push_back( &returnValue );
  466. nicInstance[0].runMethod(L"EnableDHCP",
  467. inputParameters,
  468. outputParameters );
  469. if( long ( returnValue.getValue() ) == 0 )
  470. {
  471. return MIPAddressAdmin_SUCCESS;
  472. }
  473. else
  474. {
  475. cout << "enablestatic has return " << long( returnValue.getValue() ) << endl;
  476. return COM_FAILURE;
  477. }
  478. }