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.

332 lines
13 KiB

  1. /******************************************************************
  2. SNetScal.cpp -- WBEM provider class implementation
  3. MODULE:
  4. DhcpProv.dll
  5. DESCRIPTION:
  6. Contains: the definition of the DHCP_Subnet class,
  7. the static table of manageable objects.
  8. REVISION:
  9. 08/03/98 - created
  10. ******************************************************************/
  11. #include <stdafx.h>
  12. #include "props.h"
  13. #include "snetfn.h"
  14. #include "lsfn.h"
  15. #include "srvfn.h"
  16. #include "anetlea.h"
  17. // the name of the WBEM class
  18. #define PROVIDER_NAME_DHCP_ASSOCIATIONSUBNETLEASE "DHCP_SubnetLease"
  19. // the properties' names
  20. #define PROP_NAME_SUBNET L"Subnet"
  21. #define PROP_NAME_LEASE L"Lease"
  22. // main class instantiation.
  23. CDHCP_AssociationSubnetToLease MyDHCP_Association_Subnet_Lease (PROVIDER_NAME_DHCP_ASSOCIATIONSUBNETLEASE, PROVIDER_NAMESPACE_DHCP) ;
  24. /*****************************************************************************
  25. *
  26. * FUNCTION : CDHCP_AssociationSubnetToLease::CDHCP_AssociationSubnetToLease
  27. *
  28. * DESCRIPTION : Constructor
  29. *
  30. * INPUTS : none
  31. *
  32. * RETURNS : nothing
  33. *
  34. * COMMENTS : Calls the Provider constructor.
  35. *
  36. *****************************************************************************/
  37. CDHCP_AssociationSubnetToLease::CDHCP_AssociationSubnetToLease (const CHString& strName, LPCSTR pszNameSpace ) :
  38. Provider(strName, pszNameSpace)
  39. {
  40. }
  41. /*****************************************************************************
  42. *
  43. * FUNCTION : CDHCP_AssociationSubnetToLease::~CDHCP_AssociationSubnetToLease
  44. *
  45. * DESCRIPTION : Destructor
  46. *
  47. * INPUTS : none
  48. *
  49. * RETURNS : nothing
  50. *
  51. * COMMENTS :
  52. *
  53. *****************************************************************************/
  54. CDHCP_AssociationSubnetToLease::~CDHCP_AssociationSubnetToLease ()
  55. {
  56. }
  57. /*****************************************************************************
  58. *
  59. * FUNCTION : CDHCP_AssociationSubnetToLease::EnumerateInstances
  60. *
  61. * DESCRIPTION : Returns all the instances of this class.
  62. *
  63. * INPUTS : none
  64. *
  65. * RETURNS : WBEM_S_NO_ERROR if successful
  66. *
  67. * COMMENTS : Enumerates all this instances of this class. As there is only one
  68. * DHCP Server per system, there is only one instance for this class
  69. *
  70. *****************************************************************************/
  71. HRESULT CDHCP_AssociationSubnetToLease::EnumerateInstances ( MethodContext* pMethodContext, long lFlags )
  72. {
  73. HRESULT hRes = WBEM_S_NO_ERROR;
  74. CDHCP_Server_Parameters srvParams;
  75. LPDHCP_MIB_INFO pSrvMibInfo;
  76. if (srvParams.GetMibInfo(pSrvMibInfo, TRUE))
  77. {
  78. // loop through all the subnets configured on the local server
  79. for (int i = 0; i < pSrvMibInfo->Scopes; i++)
  80. {
  81. DWORD dwSubnet;
  82. DHCP_RESUME_HANDLE ResumeHandle;
  83. dwSubnet = pSrvMibInfo->ScopeInfo[i].Subnet;
  84. CDHCP_Lease_Parameters lsParams(dwSubnet, 0);
  85. ResumeHandle = 0;
  86. // for each subnet, loop through all the client buffers belonging to it
  87. do
  88. {
  89. // load the next buffer
  90. if (!lsParams.NextSubnetClients(ResumeHandle))
  91. return WBEM_E_FAILED; // will fail here
  92. // for the current buffer, loop through all the clients
  93. for (int j = 0; hRes == WBEM_S_NO_ERROR && j < lsParams.m_pClientInfoArray->NumElements; j++)
  94. {
  95. // this is finally the info of the current client
  96. LPCLIENT_INFO pClient = lsParams.m_pClientInfoArray->Clients[j];
  97. WCHAR wcsSubnet[256], wcsAddress[256]; // should be enough for holding 'xxx.yyy.zzz.uuu\0'
  98. // build the str representation of the Subnet address
  99. swprintf(wcsSubnet, L"DHCP_Subnet.Address=\"%u.%u.%u.%u\"",(dwSubnet & 0xff000000) >> 24,
  100. (dwSubnet & 0x00ff0000) >> 16,
  101. (dwSubnet & 0x0000ff00) >> 8,
  102. (dwSubnet & 0x000000ff));
  103. // build the str representation of the ClientIpAddress address
  104. swprintf(wcsAddress, L"DHCP_Lease.Subnet=\"%u.%u.%u.%u\",Address=\"%u.%u.%u.%u\"",
  105. (dwSubnet & 0xff000000) >> 24,
  106. (dwSubnet & 0x00ff0000) >> 16,
  107. (dwSubnet & 0x0000ff00) >> 8,
  108. (dwSubnet & 0x000000ff) ,
  109. (pClient->ClientIpAddress & 0xff000000) >> 24,
  110. (pClient->ClientIpAddress & 0x00ff0000) >> 16,
  111. (pClient->ClientIpAddress & 0x0000ff00) >> 8,
  112. (pClient->ClientIpAddress & 0x000000ff));
  113. // we finally have everything we need for the creating one more instance
  114. CInstance* pInstance = CreateNewInstance(pMethodContext);
  115. // initialize the instance with the key info and call LoadInstanceProperties for the rest of the info
  116. if (pInstance->SetCHString(PROP_NAME_SUBNET, wcsSubnet) &&
  117. pInstance->SetCHString(PROP_NAME_LEASE, wcsAddress))
  118. {
  119. hRes = Commit(pInstance);
  120. // now everything relys on the err returned above.
  121. }
  122. }
  123. // if there was an error above, bail
  124. if (hRes != WBEM_S_NO_ERROR)
  125. return WBEM_E_FAILED;
  126. } while (ResumeHandle != 0); // bail if ResumeHandle got back to 0 (the end was reached)
  127. }
  128. }
  129. return hRes ;
  130. }
  131. /*****************************************************************************
  132. *
  133. * FUNCTION : CDHCP_AssociationSubnetToLease::GetObject
  134. *
  135. * DESCRIPTION : Find a single instance based on the key properties for the
  136. * class.
  137. *
  138. * INPUTS : A pointer to a CInstance object containing the key properties.
  139. *
  140. * RETURNS : WBEM_S_NO_ERROR if the instance can be found
  141. * WBEM_E_NOT_FOUND if the instance described by the key properties
  142. * could not be found
  143. * WBEM_E_FAILED if the instance could be found but another error
  144. * occurred.
  145. *
  146. * COMMENTS :
  147. *
  148. *****************************************************************************/
  149. HRESULT CDHCP_AssociationSubnetToLease::GetObject ( CInstance* pInstance, long lFlags )
  150. {
  151. return LoadInstanceProperties(pInstance)? WBEM_S_NO_ERROR : WBEM_E_NOT_FOUND;
  152. }
  153. /*****************************************************************************
  154. *
  155. * FUNCTION : CDHCP_AssociationSubnetToLease::ExecQuery
  156. *
  157. * DESCRIPTION : You are passed a method context to use in the creation of
  158. * instances that satisfy the query, and a CFrameworkQuery
  159. * which describes the query. Create and populate all
  160. * instances which satisfy the query. CIMOM will post -
  161. * filter the query for you, you may return more instances
  162. * or more properties than are requested and CIMOM
  163. * will filter out any that do not apply.
  164. *
  165. *
  166. * INPUTS :
  167. *
  168. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if not supported for this class
  169. * WBEM_E_FAILED if the query failed
  170. * WBEM_S_NO_ERROR if query was successful
  171. *
  172. * COMMENTS : TO DO: Most providers will not need to implement this method. If you don't, cimom
  173. * will call your enumerate function to get all the instances and perform the
  174. * filtering for you. Unless you expect SIGNIFICANT savings from implementing
  175. * queries, you should remove this entire method.
  176. *
  177. *****************************************************************************/
  178. HRESULT CDHCP_AssociationSubnetToLease::ExecQuery (MethodContext *pMethodContext, CFrameworkQuery& Query, long lFlags)
  179. {
  180. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  181. }
  182. /*****************************************************************************
  183. *
  184. * FUNCTION : CDHCP_AssociationSubnetToLease::PutInstance
  185. *
  186. * DESCRIPTION : PutInstance should be used in provider classes that can write
  187. * instance information back to the hardware or software.
  188. * For example: Win32_Environment will allow a PutInstance of a new
  189. * environment variable, because environment variables are "software"
  190. * related. However, a class like Win32_MotherboardDevice will not
  191. * allow editing of the bus speed. Since by default PutInstance
  192. * returns WBEM_E_PROVIDER_NOT_CAPABLE, this function is placed here as a
  193. * skeleton, but can be removed if not used.
  194. *
  195. * INPUTS :
  196. *
  197. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if PutInstance is not available
  198. * WBEM_E_FAILED if there is an error delivering the instance
  199. * WBEM_E_INVALID_PARAMETER if any of the instance properties
  200. * are incorrect.
  201. * WBEM_S_NO_ERROR if instance is properly delivered
  202. *
  203. * COMMENTS : TO DO: If you don't intend to support writing to your provider, remove this method.
  204. *
  205. *****************************************************************************/
  206. HRESULT CDHCP_AssociationSubnetToLease::PutInstance ( const CInstance &Instance, long lFlags)
  207. {
  208. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  209. }
  210. /*****************************************************************************
  211. *
  212. * FUNCTION : CDHCP_AssociationSubnetToLease::DeleteInstance
  213. *
  214. * DESCRIPTION : DeleteInstance, like PutInstance, actually writes information
  215. * to the software or hardware. For most hardware devices,
  216. * DeleteInstance should not be implemented, but for software
  217. * configuration, DeleteInstance implementation is plausible.
  218. * Like PutInstance, DeleteInstance returns WBEM_E_PROVIDER_NOT_CAPABLE from
  219. * inside Provider::DeleteInstance (defined in Provider.h). So, if
  220. * you choose not to implement DeleteInstance, remove this function
  221. * definition and the declaration from DHCP_Server_Scalars.h
  222. *
  223. * INPUTS :
  224. *
  225. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if DeleteInstance is not available.
  226. * WBEM_E_FAILED if there is an error deleting the instance.
  227. * WBEM_E_INVALID_PARAMETER if any of the instance properties
  228. * are incorrect.
  229. * WBEM_S_NO_ERROR if instance is properly deleted.
  230. *
  231. * COMMENTS : TO DO: If you don't intend to support deleting instances, remove this method.
  232. *
  233. *****************************************************************************/
  234. HRESULT CDHCP_AssociationSubnetToLease::DeleteInstance ( const CInstance &Instance, long lFlags )
  235. {
  236. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  237. }
  238. /*****************************************************************************
  239. *
  240. * FUNCTION : CDHCP_AssociationSubnetToLease::ExecMethod
  241. *
  242. * DESCRIPTION : Override this function to provide support for methods.
  243. * A method is an entry point for the user of your provider
  244. * to request your class perform some function above and
  245. * beyond a change of state. (A change of state should be
  246. * handled by PutInstance() )
  247. *
  248. * INPUTS : A pointer to a CInstance containing the instance the method was executed against.
  249. * A string containing the method name
  250. * A pointer to the CInstance which contains the IN parameters.
  251. * A pointer to the CInstance to contain the OUT parameters.
  252. * A set of specialized method flags
  253. *
  254. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if not implemented for this class
  255. * WBEM_S_NO_ERROR if method executes successfully
  256. * WBEM_E_FAILED if error occurs executing method
  257. *
  258. * COMMENTS : TO DO: If you don't intend to support Methods, remove this method.
  259. *
  260. *****************************************************************************/
  261. HRESULT CDHCP_AssociationSubnetToLease::ExecMethod ( const CInstance& Instance,
  262. const BSTR bstrMethodName,
  263. CInstance *pInParams,
  264. CInstance *pOutParams,
  265. long lFlags)
  266. {
  267. return WBEM_E_PROVIDER_NOT_CAPABLE;
  268. }
  269. /*****************************************************************************
  270. *
  271. * FUNCTION : CDHCP_AssociationSubnetToLease::LoadInstanceProperties
  272. *
  273. * RETURNS : TRUE if the values for all the properties was loaded successfully,
  274. * FALSE otherwise.
  275. *
  276. * COMMENTS : It loops through the Server_Property table, calling the GET functions.
  277. *
  278. *****************************************************************************/
  279. BOOL CDHCP_AssociationSubnetToLease::LoadInstanceProperties(CInstance* pInstance)
  280. {
  281. CHString str;
  282. char *pAddress;
  283. DHCP_IP_ADDRESS dwSubnet, dwAddress;
  284. // at this point, the key information should be provided by the pInstance
  285. if (!pInstance->GetCHString (PROP_NAME_SUBNET, str ) ||
  286. !inet_wstodw(str, dwSubnet) ||
  287. !pInstance->GetCHString ( PROP_NAME_LEASE, str) ||
  288. !(pAddress = strchr ( str.GetBuffer(0), ',' )) ||
  289. !inet_wstodw(pAddress, dwAddress))
  290. return FALSE;
  291. CDHCP_Subnet_Parameters subnetParams(dwSubnet) ;
  292. CDHCP_Lease_Parameters leaseParams(dwSubnet, dwAddress);
  293. LPDHCP_SUBNET_INFO pSubnetInfo;
  294. LPCLIENT_INFO pClientInfo;
  295. return subnetParams.GetSubnetInfo(pSubnetInfo, TRUE) && leaseParams.GetClientInfo(pClientInfo, TRUE);
  296. }