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.

187 lines
5.7 KiB

  1. /******************************************************************
  2. SNetFn.cpp -- Properties action functions (GET/SET)
  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_Server
  8. REVISION:
  9. 08/03/98 - created
  10. ******************************************************************/
  11. #include <stdafx.h>
  12. #include "SScoScal.h" // needed for DHCP_SuperScope_Property[] (for retrieving the property's name, for SET's)
  13. #include "SScoFn.h" // own header
  14. static UINT g_uSortFlags;
  15. int __cdecl sortHandler(const void* elem1, const void* elem2)
  16. {
  17. LPDHCP_SUPER_SCOPE_TABLE_ENTRY entry1, entry2;
  18. entry1 = *((LPDHCP_SUPER_SCOPE_TABLE_ENTRY*)elem1);
  19. entry2 = *((LPDHCP_SUPER_SCOPE_TABLE_ENTRY*)elem2);
  20. switch (g_uSortFlags)
  21. {
  22. case ENTRIES_SORTED_ON_SUPER_SCOPE:
  23. return entry1->SuperScopeNumber - entry2->SuperScopeNumber;
  24. case ENTRIES_SORTED_ON_SUBNET:
  25. return entry1->SubnetAddress - entry2->SubnetAddress;
  26. }
  27. return 0;
  28. }
  29. // DESCRIPTION:
  30. // Frees the memory allocated for the tables
  31. void CDHCP_SuperScope_Parameters::CleanupTable()
  32. {
  33. if (m_pSuperScopeTable != NULL)
  34. {
  35. if (m_pSuperScopeTable->cEntries > 0)
  36. {
  37. do
  38. {
  39. DhcpRpcFreeMemory(m_pSuperScopeTable->pEntries[--(m_pSuperScopeTable->cEntries)].SuperScopeName);
  40. } while (m_pSuperScopeTable->cEntries > 0);
  41. DhcpRpcFreeMemory(m_pSuperScopeTable->pEntries);
  42. }
  43. DhcpRpcFreeMemory(m_pSuperScopeTable);
  44. m_pSuperScopeTable = NULL;
  45. DhcpRpcFreeMemory(m_pSortedEntries);
  46. m_pSortedEntries = NULL;
  47. }
  48. }
  49. // DESCRIPTION:
  50. // Class constructor. All internal variables are NULL, no data is cached
  51. CDHCP_SuperScope_Parameters::CDHCP_SuperScope_Parameters( const WCHAR *wcsSuperScopeName )
  52. {
  53. m_pSuperScopeTable = NULL;
  54. m_pSortedEntries = NULL;
  55. m_wcsSuperScopeName = wcsSuperScopeName != NULL ? _wcsdup(wcsSuperScopeName) : NULL;
  56. }
  57. CDHCP_SuperScope_Parameters::CDHCP_SuperScope_Parameters(const CHString & str)
  58. {
  59. m_pSuperScopeTable = NULL;
  60. m_pSortedEntries = NULL;
  61. m_wcsSuperScopeName = (WCHAR *)MIDL_user_allocate(sizeof(WCHAR)*str.GetLength() + sizeof(WCHAR));
  62. if (m_wcsSuperScopeName != NULL)
  63. {
  64. #ifdef _UNICODE
  65. wcscpy(m_wcsSuperScopeName, str);
  66. #else
  67. swprintf(m_wcsSuperScopeName, L"%S", str);
  68. #endif
  69. }
  70. }
  71. // DESCRIPTION:
  72. // Class destructor. All internal variables are released, if they are not null
  73. CDHCP_SuperScope_Parameters::~CDHCP_SuperScope_Parameters()
  74. {
  75. CleanupTable();
  76. if (m_wcsSuperScopeName != NULL)
  77. DhcpRpcFreeMemory(m_wcsSuperScopeName);
  78. }
  79. BOOL CDHCP_SuperScope_Parameters::ExistsSuperScope (BOOL fRefresh)
  80. {
  81. LPDHCP_SUPER_SCOPE_TABLE pSuperScopes;
  82. if (!GetSuperScopes(pSuperScopes, fRefresh))
  83. return FALSE;
  84. for (int i=0; i<pSuperScopes->cEntries; i++)
  85. {
  86. // cover the NULL case
  87. if (m_wcsSuperScopeName == pSuperScopes->pEntries[i].SuperScopeName)
  88. return TRUE;
  89. // make sure there is something to compare on both sides
  90. if (m_wcsSuperScopeName == NULL || pSuperScopes->pEntries[i].SuperScopeName == NULL)
  91. continue;
  92. // check if this is the same super scope name
  93. if (wcscmp(m_wcsSuperScopeName, pSuperScopes->pEntries[i].SuperScopeName) == 0)
  94. return TRUE;
  95. }
  96. return FALSE;
  97. }
  98. // DESCRIPTION:
  99. // Provides the data structure filled in through the DhcpGetSuperScopeInfoV4 API
  100. // If this data is cached and the caller is not forcing the refresh,
  101. // return the internal cache. Otherwise, the internal cache is refreshed as well.
  102. BOOL CDHCP_SuperScope_Parameters::GetSuperScopes(LPDHCP_SUPER_SCOPE_TABLE & pSuperScopes, BOOL fRefresh)
  103. {
  104. if (m_pSuperScopeTable == NULL)
  105. fRefresh = TRUE;
  106. if (fRefresh)
  107. {
  108. // just make sure everything is cleaned up before getting new info from server
  109. CleanupTable();
  110. // call the server for getting the super scope info
  111. if (DhcpGetSuperScopeInfoV4(SERVER_IP_ADDRESS, &m_pSuperScopeTable) != ERROR_SUCCESS)
  112. return FALSE;
  113. // allocate and initialize the sorted entry array of pointers
  114. m_pSortedEntries = (LPDHCP_SUPER_SCOPE_TABLE_ENTRY *)
  115. MIDL_user_allocate(m_pSuperScopeTable->cEntries * sizeof(LPDHCP_SUPER_SCOPE_TABLE_ENTRY));
  116. if (m_pSortedEntries == NULL)
  117. return FALSE;
  118. for (int i = 0; i < m_pSuperScopeTable->cEntries; i++)
  119. m_pSortedEntries[i] = &(m_pSuperScopeTable->pEntries[i]);
  120. }
  121. pSuperScopes = m_pSuperScopeTable;
  122. return TRUE;
  123. }
  124. BOOL CDHCP_SuperScope_Parameters::GetSortedScopes(UINT sortFlags, LPDHCP_SUPER_SCOPE_TABLE_ENTRY * & pSortedEntries, DWORD &dwNumEntries)
  125. {
  126. if (m_pSuperScopeTable == NULL)
  127. return FALSE;
  128. g_uSortFlags = sortFlags;
  129. qsort(m_pSortedEntries,
  130. m_pSuperScopeTable->cEntries,
  131. sizeof(LPDHCP_SUPER_SCOPE_TABLE_ENTRY),
  132. sortHandler);
  133. pSortedEntries = m_pSortedEntries;
  134. dwNumEntries = m_pSuperScopeTable->cEntries;
  135. return TRUE;
  136. }
  137. BOOL CDHCP_SuperScope_Parameters::AlterSubnetSet(DHCP_IP_ADDRESS subnetAddress)
  138. {
  139. LPDHCP_SUPER_SCOPE_TABLE pSuperScopes;
  140. return DhcpSetSuperScopeV4(SERVER_IP_ADDRESS, subnetAddress, m_wcsSuperScopeName, TRUE) == ERROR_SUCCESS;
  141. }
  142. BOOL CDHCP_SuperScope_Parameters::DeleteSuperScope()
  143. {
  144. LPDHCP_SUPER_SCOPE_TABLE pSuperScopes;
  145. DWORD retCode;
  146. return(m_wcsSuperScopeName != NULL &&
  147. DhcpDeleteSuperScopeV4(SERVER_IP_ADDRESS, m_wcsSuperScopeName) == ERROR_SUCCESS);
  148. }