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.

359 lines
15 KiB

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