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.

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