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.

196 lines
6.2 KiB

  1. /******************************************************************
  2. LsFn.cpp -- Properties action functions (GET)
  3. MODULE:
  4. DhcpProv.dll
  5. DESCRIPTION:
  6. Contains the definition for the action functions associated to
  7. each manageable property from the class CDHCP_Range
  8. REVISION:
  9. 08/14/98 - created
  10. ******************************************************************/
  11. #include <stdafx.h>
  12. #include "RngFn.h" // own header
  13. /*****************************************************************
  14. * The definition of the class CDHCP_Range_Parameters
  15. *****************************************************************/
  16. // by default, all the data structures are NULL (and dw variables are 0'ed)
  17. // those values indicates that no data is cached from the server.
  18. void CDHCP_Range_Parameters::DeleteRangeInfoArray(LPDHCP_SUBNET_ELEMENT_INFO_ARRAY_V4& pRangeInfoArray)
  19. {
  20. if (pRangeInfoArray)
  21. {
  22. if (pRangeInfoArray->Elements)
  23. {
  24. for (int i = 0; i < pRangeInfoArray->NumElements; i++)
  25. {
  26. if (pRangeInfoArray->Elements[i].ElementType == DhcpIpRanges)
  27. DhcpRpcFreeMemory(pRangeInfoArray->Elements[i].Element.IpRange);
  28. if (pRangeInfoArray->Elements[i].ElementType == DhcpExcludedIpRanges)
  29. DhcpRpcFreeMemory(pRangeInfoArray->Elements[i].Element.ExcludeIpRange);
  30. }
  31. DhcpRpcFreeMemory(pRangeInfoArray->Elements);
  32. }
  33. DhcpRpcFreeMemory(pRangeInfoArray);
  34. pRangeInfoArray = NULL;
  35. }
  36. }
  37. CDHCP_Range_Parameters::CDHCP_Range_Parameters(
  38. DHCP_IP_ADDRESS dwSubnet,
  39. DHCP_IP_ADDRESS dwStartAddress,
  40. DHCP_IP_ADDRESS dwEndAddress)
  41. {
  42. m_dwSubnet = dwSubnet;
  43. m_dwStartAddress = dwStartAddress;
  44. m_dwEndAddress = dwEndAddress;
  45. m_pRangeInfoArray = NULL;
  46. }
  47. // the DHCP API calls are allocating memory for which the caller is responsible
  48. // to release. We are releasing this memory upon the destruction of this object's instance.
  49. CDHCP_Range_Parameters::~CDHCP_Range_Parameters()
  50. {
  51. DeleteRangeInfoArray(m_pRangeInfoArray);
  52. }
  53. // DESCRIPTION:
  54. // Fills in the internal cache with the information from the database, starting
  55. // from the given handle. If the end of the database is reached, the handle is
  56. // reset to 0. Returns TRUE on success (regardless there is more data to be read or not).
  57. DWORD CDHCP_Range_Parameters::NextSubnetRange(DHCP_RESUME_HANDLE & hResume, DHCP_SUBNET_ELEMENT_TYPE rangeType)
  58. {
  59. DWORD dwError;
  60. DWORD dwRangeRead = 0;
  61. DWORD dwRangeTotal = 0;
  62. // each time the API gets called, the previous
  63. // m_pReservationInfoArray is useless and should be freed
  64. DeleteRangeInfoArray(m_pRangeInfoArray);
  65. // calls the API.
  66. dwError = DhcpEnumSubnetElementsV4 (
  67. SERVER_IP_ADDRESS,
  68. m_dwSubnet,
  69. rangeType,
  70. &hResume,
  71. (DWORD)(-1),
  72. &m_pRangeInfoArray,
  73. &dwRangeRead,
  74. &dwRangeRead);
  75. if (dwError == ERROR_SUCCESS)
  76. hResume = 0;
  77. return dwError; // possible (normal) codes: ERROR_SUCCESS, ERROR_MORE_DATA, ERROR_NO_MORE_ITEMS
  78. }
  79. // DESCRIPTION:
  80. // Check the cache for the existance of a given subnet range, of the given range type.
  81. BOOL CDHCP_Range_Parameters::CheckExistsRangeInCache(DHCP_SUBNET_ELEMENT_TYPE rangeType)
  82. {
  83. if (m_pRangeInfoArray == NULL)
  84. return FALSE;
  85. for (int i=0; i < m_pRangeInfoArray->NumElements; i++)
  86. {
  87. LPDHCP_IP_RANGE lpRange;
  88. if (m_pRangeInfoArray->Elements[i].ElementType != rangeType)
  89. continue;
  90. if (rangeType == DhcpIpRanges)
  91. lpRange = m_pRangeInfoArray->Elements[i].Element.IpRange;
  92. else if (rangeType == DhcpExcludedIpRanges)
  93. lpRange = m_pRangeInfoArray->Elements[i].Element.ExcludeIpRange;
  94. else
  95. return FALSE;
  96. if (lpRange->StartAddress == m_dwStartAddress &&
  97. lpRange->EndAddress == m_dwEndAddress)
  98. return TRUE;
  99. }
  100. return FALSE;
  101. }
  102. // DESCRIPTION:
  103. // Check the existance of a given subnet range, first in cache, and if this fails, from the info refreshed from
  104. // the server. This method assumes m_dwSubnet, m_dwStartAddress and m_dwEndAddress are already filled in
  105. // with consistent data.
  106. BOOL CDHCP_Range_Parameters::CheckExistsRange(DHCP_SUBNET_ELEMENT_TYPE rangeType)
  107. {
  108. DHCP_RESUME_HANDLE hResume;
  109. if (CheckExistsRangeInCache(rangeType))
  110. return TRUE;
  111. hResume = 0;
  112. do
  113. {
  114. DWORD errCode;
  115. errCode = NextSubnetRange(hResume, rangeType);
  116. if (errCode != ERROR_SUCCESS &&
  117. errCode != ERROR_MORE_DATA)
  118. break;
  119. if (CheckExistsRangeInCache(rangeType))
  120. return TRUE;
  121. } while(hResume != 0);
  122. return FALSE;
  123. }
  124. BOOL CDHCP_Range_Parameters::CreateRange(DHCP_SUBNET_ELEMENT_TYPE rangeType)
  125. {
  126. DWORD errCode;
  127. DHCP_SUBNET_ELEMENT_DATA_V4 subnetElementInfo;
  128. DHCP_IP_RANGE rangeInfo;
  129. rangeInfo.StartAddress = m_dwStartAddress;
  130. rangeInfo.EndAddress = m_dwEndAddress;
  131. subnetElementInfo.ElementType = rangeType;
  132. // no matter the range type, 'Element' is a union so this pointer is set
  133. // to both IpRange and ExcludeIpRange;
  134. subnetElementInfo.Element.IpRange = &rangeInfo;
  135. errCode = DhcpAddSubnetElementV4(
  136. SERVER_IP_ADDRESS,
  137. m_dwSubnet,
  138. &subnetElementInfo);
  139. return (errCode == ERROR_SUCCESS);
  140. }
  141. BOOL CDHCP_Range_Parameters::DeleteRange(DHCP_SUBNET_ELEMENT_TYPE rangeType)
  142. {
  143. DWORD errCode;
  144. DHCP_SUBNET_ELEMENT_DATA_V4 subnetElementInfo;
  145. DHCP_IP_RANGE rangeInfo;
  146. rangeInfo.StartAddress = m_dwStartAddress;
  147. rangeInfo.EndAddress = m_dwEndAddress;
  148. subnetElementInfo.ElementType = rangeType;
  149. // no matter the range type, 'Element' is a union so this pointer is set
  150. // to both IpRange and ExcludeIpRange;
  151. subnetElementInfo.Element.IpRange = &rangeInfo;
  152. errCode = DhcpRemoveSubnetElementV4(
  153. SERVER_IP_ADDRESS,
  154. m_dwSubnet,
  155. &subnetElementInfo,
  156. DhcpFullForce);
  157. return (errCode == ERROR_SUCCESS);
  158. }