Leaked source code of windows server 2003
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.

241 lines
5.9 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2001, Microsoft Corporation
  4. //
  5. // File: DfsServerSiteInfo.hxx
  6. //
  7. // Contents: the Dfs Site Information class.
  8. //
  9. // Classes: DfsServerSiteInfo
  10. //
  11. // History: Jan. 8 2001, Author: udayh
  12. //
  13. //-----------------------------------------------------------------------------
  14. #ifndef __DFS_SERVER_SITE_INFO__
  15. #define __DFS_SERVER_SITE_INFO__
  16. #include "DfsGeneric.hxx"
  17. #include "dsgetdc.h"
  18. #include "lm.h"
  19. #include "dfsinit.hxx"
  20. #include <winsock2.h>
  21. #include <DfsSite.hxx>
  22. extern
  23. DFSSTATUS
  24. DfsGetSiteNameFromIpAddress(char * IpData,
  25. ULONG IpLength,
  26. USHORT IpFamily,
  27. LPWSTR **SiteNames);
  28. //
  29. // This class is the hashentry for the ServerSiteSupport
  30. // hash table. It maps a ServerName to a DfsSite.
  31. //
  32. class DfsServerSiteInfo: public DfsGeneric
  33. {
  34. private:
  35. ULONG _FirstAccessTime;
  36. UNICODE_STRING _ServerName;
  37. DfsSite *_ServerSite;
  38. public:
  39. VOID
  40. DfsGetSiteForServer(
  41. PUNICODE_STRING pServerName,
  42. DfsSite **ppServerSite)
  43. {
  44. DFSSTATUS Status = ERROR_SUCCESS;
  45. NTSTATUS NtStatus = STATUS_SUCCESS;
  46. struct hostent *hp = NULL;
  47. ANSI_STRING DestinationString;
  48. DestinationString.Buffer = NULL;
  49. NtStatus = RtlUnicodeStringToAnsiString(&DestinationString,
  50. pServerName,
  51. TRUE);
  52. if (NtStatus != STATUS_SUCCESS)
  53. {
  54. *ppServerSite = DfsGetDefaultSite();
  55. return;
  56. }
  57. //
  58. // This can be a bogus host name. So beware.
  59. //
  60. hp = gethostbyname (DestinationString.Buffer);
  61. if (hp != NULL)
  62. {
  63. //
  64. // Get a corresponding DfsSite for this IP.
  65. // This call will always succeed, because it'll
  66. // return the DefaultSite in case of any error.
  67. //
  68. DfsIpToDfsSite(hp->h_addr_list[0],
  69. 4,
  70. AF_INET,
  71. ppServerSite);
  72. }
  73. else
  74. {
  75. *ppServerSite = DfsGetDefaultSite();
  76. }
  77. if(DestinationString.Buffer != NULL)
  78. {
  79. RtlFreeAnsiString(&DestinationString);
  80. }
  81. return;
  82. }
  83. //
  84. // Initialize the server name and its site.
  85. //
  86. DFSSTATUS
  87. DfsInitializeServerSiteInfo(PUNICODE_STRING pServerName)
  88. {
  89. DFSSTATUS Status = ERROR_SUCCESS;
  90. Status = DfsCreateUnicodeString( &_ServerName,
  91. pServerName);
  92. if (Status == ERROR_SUCCESS)
  93. {
  94. //
  95. // Find the DfsSite for this server.
  96. // This DfsSite will already be referenced and
  97. // the call is guaranteed to succeed.
  98. //
  99. DfsGetSiteForServer(pServerName,
  100. &_ServerSite);
  101. }
  102. return Status;
  103. }
  104. //
  105. // Creates and initializes a ServerSiteInfo instance.
  106. //
  107. static DfsServerSiteInfo *
  108. DfsCreateServerSiteInfo(DFSSTATUS * pStatus,
  109. PUNICODE_STRING pServerName)
  110. {
  111. DFSSTATUS Status = ERROR_SUCCESS;
  112. DfsServerSiteInfo * pNewSiteSupport = NULL;
  113. pNewSiteSupport = new DfsServerSiteInfo();
  114. if(pNewSiteSupport == NULL)
  115. {
  116. Status = ERROR_NOT_ENOUGH_MEMORY;
  117. }
  118. else
  119. {
  120. //
  121. // First find out the site for this server. NOT_ENOUGH_MEMORY
  122. // is the only possible failure here.
  123. //
  124. Status = pNewSiteSupport->DfsInitializeServerSiteInfo(pServerName);
  125. if(Status != ERROR_SUCCESS)
  126. {
  127. pNewSiteSupport->ReleaseReference();
  128. pNewSiteSupport = NULL;
  129. }
  130. }
  131. *pStatus = Status;
  132. return pNewSiteSupport;
  133. }
  134. ~DfsServerSiteInfo()
  135. {
  136. DfsFreeUnicodeString(&_ServerName);
  137. if (_ServerSite != NULL)
  138. {
  139. _ServerSite->ReleaseReference();
  140. _ServerSite = NULL;
  141. }
  142. }
  143. PUNICODE_STRING
  144. GetServerName()
  145. {
  146. return &_ServerName;
  147. }
  148. LPWSTR
  149. GetServerNameString()
  150. {
  151. return _ServerName.Buffer;
  152. }
  153. PUNICODE_STRING
  154. GetSiteName()
  155. {
  156. ASSERT(_ServerSite);
  157. return _ServerSite->SiteName();
  158. }
  159. LPWSTR
  160. GetSiteNameString()
  161. {
  162. ASSERT(_ServerSite);
  163. return _ServerSite->SiteName()->Buffer;
  164. }
  165. DfsSite *
  166. GetSite()
  167. {
  168. return _ServerSite;
  169. }
  170. BOOLEAN
  171. IsTimeToRefresh(void)
  172. {
  173. DWORD TimeNow = 0;
  174. TimeNow = GetTickCount();
  175. if ((TimeNow > _FirstAccessTime) &&
  176. (TimeNow - _FirstAccessTime) > DfsServerGlobalData.SiteSupportRefreshInterval)
  177. {
  178. return TRUE;
  179. }
  180. if ((TimeNow < _FirstAccessTime) &&
  181. ((TimeNow - 0) + (0xFFFFFFFF - _FirstAccessTime) > DfsServerGlobalData.SiteSupportRefreshInterval))
  182. {
  183. return TRUE;
  184. }
  185. return FALSE;
  186. }
  187. DFSSTATUS
  188. Refresh(void)
  189. {
  190. DFSSTATUS Status = STATUS_UNSUCCESSFUL;
  191. if(IsTimeToRefresh())
  192. {
  193. Status = ERROR_SUCCESS;
  194. }
  195. return Status;
  196. }
  197. private:
  198. DfsServerSiteInfo(void) : DfsGeneric(DFS_OBJECT_TYPE_SERVER_SITE_INFO)
  199. {
  200. _FirstAccessTime = GetTickCount();
  201. RtlInitUnicodeString( &_ServerName, NULL );
  202. _ServerSite = NULL;
  203. }
  204. };
  205. #endif __DFS_SERVER_SITE_INFO__