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.

406 lines
16 KiB

  1. /******************************************************************
  2. LsScal.cpp -- WBEM provider class implementation
  3. MODULE:
  4. DhcpProv.dll
  5. DESCRIPTION:
  6. Contains: the definition of the DHCP_Lease class,
  7. the static table of manageable objects.
  8. REVISION:
  9. 08/14/98 - created
  10. ******************************************************************/
  11. #include <stdafx.h>
  12. #include "LsFn.h" // needed for the declarations of all the functions.
  13. #include "LsScal.h" // own header
  14. #include "SrvFn.h" // for server parameters
  15. #include "ResFn.h" // for checking if a lease is a reservation or not
  16. // static table of CDHCP_Property objects containing the DHCP Lease
  17. // scalar parameters (properties) which are WBEM manageable. Each object associates
  18. // the name of the property with their SET and GET functions.
  19. // *** NOTE ***
  20. // The name of each property has to be in sync with the ones specified in the DhcpSchema.mof.
  21. // The indices specified in LsScal.h should also be in sync with the actual row from this table (they are used
  22. // in the property's action functions.
  23. static const CDHCP_Property DHCP_Lease_Property[]=
  24. {
  25. CDHCP_Property(L"Subnet", fnLsGetSubnet, NULL),
  26. CDHCP_Property(L"Address", fnLsGetAddress, NULL),
  27. CDHCP_Property(L"SubnetMask", fnLsGetMask, NULL),
  28. CDHCP_Property(L"UniqueClientIdentifier", fnLsGetHdwAddress, fnLsSetHdwAddress),
  29. CDHCP_Property(L"Name", fnLsGetName, fnLsSetName),
  30. CDHCP_Property(L"Comment", fnLsGetComment, fnLsSetComment),
  31. CDHCP_Property(L"LeaseExpiryDate", fnLsGetExpiry, NULL),
  32. CDHCP_Property(L"Type", fnLsGetType, fnLsSetType),
  33. #ifdef NT5
  34. CDHCP_Property(L"State", fnLsGetState, NULL)
  35. #endif
  36. };
  37. // the name of the WBEM class
  38. #define PROVIDER_NAME_DHCP_LEASE "DHCP_Lease"
  39. // main class instantiation.
  40. CDHCP_Lease MyDHCP_Lease_Scalars (PROVIDER_NAME_DHCP_LEASE, PROVIDER_NAMESPACE_DHCP) ;
  41. /*****************************************************************************
  42. *
  43. * FUNCTION : CDHCP_Lease::CDHCP_Lease
  44. *
  45. * DESCRIPTION : Constructor
  46. *
  47. * INPUTS : none
  48. *
  49. * RETURNS : nothing
  50. *
  51. * COMMENTS : Calls the Provider constructor.
  52. *
  53. *****************************************************************************/
  54. CDHCP_Lease::CDHCP_Lease (const CHString& strName, LPCSTR pszNameSpace ) :
  55. Provider(strName, pszNameSpace)
  56. {
  57. }
  58. /*****************************************************************************
  59. *
  60. * FUNCTION : CDHCP_Lease::~CDHCP_Lease
  61. *
  62. * DESCRIPTION : Destructor
  63. *
  64. * INPUTS : none
  65. *
  66. * RETURNS : nothing
  67. *
  68. * COMMENTS :
  69. *
  70. *****************************************************************************/
  71. CDHCP_Lease::~CDHCP_Lease ()
  72. {
  73. }
  74. /*****************************************************************************
  75. *
  76. * FUNCTION : CDHCP_Lease::EnumerateInstances
  77. *
  78. * DESCRIPTION : Returns all the instances of this class.
  79. *
  80. * INPUTS : none
  81. *
  82. * RETURNS : WBEM_S_NO_ERROR if successful
  83. *
  84. * COMMENTS : Enumerates all this instances of this class. Here we scan
  85. * all the subnets, for which we get the info on all
  86. * clients.
  87. *****************************************************************************/
  88. HRESULT CDHCP_Lease::EnumerateInstances ( MethodContext* pMethodContext, long lFlags )
  89. {
  90. CDHCP_Server_Parameters srvParams;
  91. LPDHCP_MIB_INFO pSrvMibInfo;
  92. if (srvParams.GetMibInfo(pSrvMibInfo, TRUE))
  93. {
  94. // loop through all the subnets configured on the local server
  95. for (int i = 0; i < pSrvMibInfo->Scopes; i++)
  96. {
  97. DWORD dwSubnet;
  98. DHCP_RESUME_HANDLE ResumeHandle;
  99. dwSubnet = pSrvMibInfo->ScopeInfo[i].Subnet;
  100. CDHCP_Lease_Parameters lsParams(dwSubnet, 0);
  101. ResumeHandle = 0;
  102. // for each subnet, loop through all the client buffers belonging to it
  103. do
  104. {
  105. HRESULT hRes = WBEM_S_NO_ERROR;
  106. // load the next buffer
  107. if (!lsParams.NextSubnetClients(ResumeHandle))
  108. return WBEM_E_FAILED; // will fail here
  109. // for the current buffer, loop through all the clients
  110. for (int j = 0; hRes == WBEM_S_NO_ERROR && j < lsParams.m_pClientInfoArray->NumElements; j++)
  111. {
  112. // this is finally the info of the current client
  113. #ifdef NT5
  114. LPDHCP_CLIENT_INFO_V5 pClient = lsParams.m_pClientInfoArray->Clients[j];
  115. #else if NT4
  116. LPDHCP_CLIENT_INFO_V4 pClient = lsParams.m_pClientInfoArray->Clients[j];
  117. #endif
  118. WCHAR wcsSubnet[16], wcsAddress[16]; // should be enough for holding 'xxx.yyy.zzz.uuu\0'
  119. // build the str representation of the Subnet address
  120. swprintf(wcsSubnet, L"%u.%u.%u.%u",(dwSubnet & 0xff000000) >> 24,
  121. (dwSubnet & 0x00ff0000) >> 16,
  122. (dwSubnet & 0x0000ff00) >> 8,
  123. (dwSubnet & 0x000000ff));
  124. // build the str representation of the ClientIpAddress address
  125. swprintf(wcsAddress, L"%u.%u.%u.%u",(pClient->ClientIpAddress & 0xff000000) >> 24,
  126. (pClient->ClientIpAddress & 0x00ff0000) >> 16,
  127. (pClient->ClientIpAddress & 0x0000ff00) >> 8,
  128. (pClient->ClientIpAddress & 0x000000ff));
  129. // update the second key into the lsParams
  130. lsParams.m_dwClientAddress = pClient->ClientIpAddress;
  131. // we finally have everything we need for the creating one more instance
  132. CInstance* pInstance = CreateNewInstance(pMethodContext);
  133. // initialize the instance with the key info and call LoadInstanceProperties for the rest of the info
  134. if (pInstance->SetCHString(DHCP_Lease_Property[IDX_Ls_Subnet].m_wsPropName, wcsSubnet) &&
  135. pInstance->SetCHString(DHCP_Lease_Property[IDX_Ls_Address].m_wsPropName, wcsAddress) &&
  136. LoadInstanceProperties(pInstance))
  137. {
  138. hRes = Commit(pInstance);
  139. // now everything relys on the err returned above.
  140. }
  141. }
  142. // if there was an error above, bail
  143. if (hRes != WBEM_S_NO_ERROR)
  144. return WBEM_E_FAILED;
  145. } while (ResumeHandle != 0); // bail if ResumeHandle got back to 0 (the end was reached)
  146. }
  147. }
  148. return WBEM_S_NO_ERROR;
  149. }
  150. /*****************************************************************************
  151. *
  152. * FUNCTION : CDHCP_Lease::GetObject
  153. *
  154. * DESCRIPTION : Find a single instance based on the key properties for the
  155. * class.
  156. *
  157. * INPUTS : A pointer to a CInstance object containing the key properties.
  158. *
  159. * RETURNS : WBEM_S_NO_ERROR if the instance can be found
  160. * WBEM_E_NOT_FOUND if the instance described by the key properties
  161. * could not be found
  162. * WBEM_E_FAILED if the instance could be found but another error
  163. * occurred.
  164. *
  165. * COMMENTS :
  166. *
  167. *****************************************************************************/
  168. HRESULT CDHCP_Lease::GetObject ( CInstance* pInstance, long lFlags )
  169. {
  170. return LoadInstanceProperties(pInstance)? WBEM_S_NO_ERROR : WBEM_E_NOT_FOUND;
  171. }
  172. /*****************************************************************************
  173. *
  174. * FUNCTION : CDHCP_Lease::ExecQuery
  175. *
  176. * DESCRIPTION : You are passed a method context to use in the creation of
  177. * instances that satisfy the query, and a CFrameworkQuery
  178. * which describes the query. Create and populate all
  179. * instances which satisfy the query. CIMOM will post -
  180. * filter the query for you, you may return more instances
  181. * or more properties than are requested and CIMOM
  182. * will filter out any that do not apply.
  183. *
  184. *
  185. * INPUTS :
  186. *
  187. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if not supported for this class
  188. * WBEM_E_FAILED if the query failed
  189. * WBEM_S_NO_ERROR if query was successful
  190. *
  191. * COMMENTS : TO DO: Most providers will not need to implement this method. If you don't, cimom
  192. * will call your enumerate function to get all the instances and perform the
  193. * filtering for you. Unless you expect SIGNIFICANT savings from implementing
  194. * queries, you should remove this entire method.
  195. *
  196. *****************************************************************************/
  197. HRESULT CDHCP_Lease::ExecQuery (MethodContext *pMethodContext, CFrameworkQuery& Query, long lFlags)
  198. {
  199. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  200. }
  201. /*****************************************************************************
  202. *
  203. * FUNCTION : CDHCP_Lease::PutInstance
  204. *
  205. * DESCRIPTION : PutInstance should be used in provider classes that can write
  206. * instance information back to the hardware or software.
  207. * For example: Win32_Environment will allow a PutInstance of a new
  208. * environment variable, because environment variables are "software"
  209. * related. However, a class like Win32_MotherboardDevice will not
  210. * allow editing of the bus speed. Since by default PutInstance
  211. * returns WBEM_E_PROVIDER_NOT_CAPABLE, this function is placed here as a
  212. * skeleton, but can be removed if not used.
  213. *
  214. * INPUTS :
  215. *
  216. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if PutInstance is not available
  217. * WBEM_E_FAILED if there is an error delivering the instance
  218. * WBEM_E_INVALID_PARAMETER if any of the instance properties
  219. * are incorrect.
  220. * WBEM_S_NO_ERROR if instance is properly delivered
  221. *
  222. * COMMENTS : TO DO: If you don't intend to support writing to your provider, remove this method.
  223. *
  224. *****************************************************************************/
  225. HRESULT CDHCP_Lease::PutInstance ( const CInstance &Instance, long lFlags)
  226. {
  227. CHString str;
  228. DHCP_IP_ADDRESS dwSubnet, dwAddress;
  229. DWORD errCode;
  230. // at this point, the key information should be provided by the pInstance
  231. if (!Instance.GetCHString(DHCP_Lease_Property[IDX_Ls_Subnet].m_wsPropName, str) ||
  232. !inet_wstodw(str, dwSubnet) ||
  233. !Instance.GetCHString(DHCP_Lease_Property[IDX_Ls_Address].m_wsPropName, str) ||
  234. !inet_wstodw(str, dwAddress)
  235. )
  236. return WBEM_E_FAILED;
  237. CDHCP_Lease_Parameters leaseParams(dwSubnet, dwAddress);
  238. return LoadLeaseParams(&leaseParams, (CInstance *)&Instance) &&
  239. leaseParams.CommitSet(errCode) ? WBEM_S_NO_ERROR : WBEM_E_FAILED;
  240. }
  241. /*****************************************************************************
  242. *
  243. * FUNCTION : CDHCP_Lease::DeleteInstance
  244. *
  245. * DESCRIPTION : DeleteInstance, like PutInstance, actually writes information
  246. * to the software or hardware. For most hardware devices,
  247. * DeleteInstance should not be implemented, but for software
  248. * configuration, DeleteInstance implementation is plausible.
  249. * Like PutInstance, DeleteInstance returns WBEM_E_PROVIDER_NOT_CAPABLE from
  250. * inside Provider::DeleteInstance (defined in Provider.h). So, if
  251. * you choose not to implement DeleteInstance, remove this function
  252. * definition and the declaration from DHCP_Server_Scalars.h
  253. *
  254. * INPUTS :
  255. *
  256. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if DeleteInstance is not available.
  257. * WBEM_E_FAILED if there is an error deleting the instance.
  258. * WBEM_E_INVALID_PARAMETER if any of the instance properties
  259. * are incorrect.
  260. * WBEM_S_NO_ERROR if instance is properly deleted.
  261. *
  262. * COMMENTS : TO DO: If you don't intend to support deleting instances, remove this method.
  263. *
  264. *****************************************************************************/
  265. HRESULT CDHCP_Lease::DeleteInstance ( const CInstance &Instance, long lFlags )
  266. {
  267. return WBEM_E_PROVIDER_NOT_CAPABLE;
  268. }
  269. /*****************************************************************************
  270. *
  271. * FUNCTION : CDHCP_Lease::ExecMethod
  272. *
  273. * DESCRIPTION : Override this function to provide support for methods.
  274. * A method is an entry point for the user of your provider
  275. * to request your class perform some function above and
  276. * beyond a change of state. (A change of state should be
  277. * handled by PutInstance() )
  278. *
  279. * INPUTS : A pointer to a CInstance containing the instance the method was executed against.
  280. * A string containing the method name
  281. * A pointer to the CInstance which contains the IN parameters.
  282. * A pointer to the CInstance to contain the OUT parameters.
  283. * A set of specialized method flags
  284. *
  285. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if not implemented for this class
  286. * WBEM_S_NO_ERROR if method executes successfully
  287. * WBEM_E_FAILED if error occurs executing method
  288. *
  289. * COMMENTS : TO DO: If you don't intend to support Methods, remove this method.
  290. *
  291. *****************************************************************************/
  292. HRESULT CDHCP_Lease::ExecMethod ( const CInstance& Instance,
  293. const BSTR bstrMethodName,
  294. CInstance *pInParams,
  295. CInstance *pOutParams,
  296. long lFlags)
  297. {
  298. return WBEM_E_PROVIDER_NOT_CAPABLE;
  299. }
  300. /*****************************************************************************
  301. *
  302. * FUNCTION : CDHCP_Lease::LoadInstanceProperties
  303. *
  304. * RETURNS : TRUE if the values for all the properties was loaded successfully,
  305. * FALSE otherwise.
  306. *
  307. * COMMENTS : wrapper for LoadInstanceProperties.
  308. *
  309. *****************************************************************************/
  310. BOOL CDHCP_Lease::LoadInstanceProperties(CInstance* pInstance)
  311. {
  312. DWORD dwSubnet, dwAddress;
  313. return LoadInstanceProperties(pInstance, dwSubnet, dwAddress);
  314. }
  315. /*****************************************************************************
  316. *
  317. * FUNCTION : CDHCP_Lease::LoadInstanceProperties
  318. *
  319. * RETURNS : TRUE if the values for all the properties was loaded successfully,
  320. * FALSE otherwise.
  321. *
  322. * COMMENTS : It loops through the Lease_Property table, calling the GET functions.
  323. * The pInstance parameter must have at this point all the key information
  324. * (Subnet and Address in this case)
  325. *
  326. *****************************************************************************/
  327. BOOL CDHCP_Lease::LoadInstanceProperties(CInstance* pInstance, DWORD &dwSubnet, DWORD &dwAddress)
  328. {
  329. CHString str;
  330. // at this point, the key information should be provided by the pInstance
  331. if (!pInstance->GetCHString(DHCP_Lease_Property[IDX_Ls_Subnet].m_wsPropName, str) ||
  332. !inet_wstodw(str, dwSubnet) ||
  333. !pInstance->GetCHString(DHCP_Lease_Property[IDX_Ls_Address].m_wsPropName, str) ||
  334. !inet_wstodw(str, dwAddress))
  335. return FALSE;
  336. CDHCP_Lease_Parameters leaseParams(dwSubnet, dwAddress);
  337. for (int i = 0; i < NUM_LEASE_PROPERTIES; i++)
  338. {
  339. // if there is an invisible property (does not support GET) just skip it.
  340. if (DHCP_Lease_Property[i].m_pfnActionGet == NULL)
  341. continue;
  342. // call the appropriate GET function, fail if the call fails (there should be no reason for failure)
  343. if (!(*(DHCP_Lease_Property[i].m_pfnActionGet))(&leaseParams, NULL, pInstance))
  344. return FALSE;
  345. }
  346. return TRUE;
  347. }
  348. // Loader function for the lease parameters;
  349. BOOL CDHCP_Lease::LoadLeaseParams(CDHCP_Lease_Parameters *pLeaseParams, CInstance *pInstance)
  350. {
  351. for (int i = 0; i < NUM_LEASE_PROPERTIES; i++)
  352. {
  353. // if there is an invisible property (does not support SET) just skip it.
  354. if (DHCP_Lease_Property[i].m_pfnActionSet == NULL)
  355. continue;
  356. // call the appropriate SET function, fail if the call fails (there should be no reason for failure)
  357. if (!(*(DHCP_Lease_Property[i].m_pfnActionSet))(pLeaseParams, pInstance, NULL))
  358. return FALSE;
  359. }
  360. return TRUE;
  361. }