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.

156 lines
3.8 KiB

  1. /////////////////////////////////////////////////////////////////////
  2. // IsLocal.cpp
  3. //
  4. // Microsoft Windows
  5. // Copyright (C) Microsoft Corporation, 1997-1998
  6. //
  7. //
  8. // Determines whether a computername is the local computer
  9. //
  10. //
  11. // HISTORY
  12. // 09-Jan-1999 jonn Creation.
  13. //
  14. /////////////////////////////////////////////////////////////////////
  15. #include "stdafx.h"
  16. #include "dns.h"
  17. #include <winsock.h>
  18. #include "stdutils.h"
  19. #if _WIN32_WINNT < 0x0500
  20. //
  21. // CODEWORK This was taken from winbase.h. MFC requires _WIN32_WINNT=0x4000 whereas
  22. // winbase.h only includes this for _WIN32_WINNT=0x5000. JonN 1/14/99
  23. //
  24. extern "C" {
  25. typedef enum _COMPUTER_NAME_FORMAT {
  26. ComputerNameNetBIOS,
  27. ComputerNameDnsHostname,
  28. ComputerNameDnsDomain,
  29. ComputerNameDnsFullyQualified,
  30. ComputerNamePhysicalNetBIOS,
  31. ComputerNamePhysicalDnsHostname,
  32. ComputerNamePhysicalDnsDomain,
  33. ComputerNamePhysicalDnsFullyQualified,
  34. ComputerNameMax
  35. } COMPUTER_NAME_FORMAT ;
  36. WINBASEAPI
  37. BOOL
  38. WINAPI
  39. GetComputerNameExA (
  40. IN COMPUTER_NAME_FORMAT NameType,
  41. OUT LPSTR lpBuffer,
  42. IN OUT LPDWORD nSize
  43. );
  44. WINBASEAPI
  45. BOOL
  46. WINAPI
  47. GetComputerNameExW (
  48. IN COMPUTER_NAME_FORMAT NameType,
  49. OUT LPWSTR lpBuffer,
  50. IN OUT LPDWORD nSize
  51. );
  52. #ifdef UNICODE
  53. #define GetComputerNameEx GetComputerNameExW
  54. #else
  55. #define GetComputerNameEx GetComputerNameExA
  56. #endif // !UNICODE
  57. } // extern "C"
  58. #endif
  59. // ISSUE-2002/03/28-JonN never freed
  60. LPTSTR g_ptzComputerName = NULL;
  61. LPTSTR g_ptzDnsComputerName = NULL;
  62. /////////////////////////////////////////////////////////////////////
  63. // IsLocalComputername()
  64. //
  65. BOOL
  66. IsLocalComputername( IN LPCTSTR pszMachineName )
  67. {
  68. if ( NULL == pszMachineName || L'\0' == pszMachineName[0] )
  69. return TRUE;
  70. if ( L'\\' == pszMachineName[0] && L'\\' == pszMachineName[1] )
  71. pszMachineName += 2;
  72. // compare with the local computer name
  73. if ( NULL == g_ptzComputerName )
  74. {
  75. TCHAR achComputerName[ MAX_COMPUTERNAME_LENGTH+1 ];
  76. DWORD dwSize = sizeof(achComputerName)/sizeof(TCHAR);
  77. if ( !GetComputerName( achComputerName, &dwSize ) )
  78. {
  79. ASSERT(FALSE);
  80. }
  81. else
  82. {
  83. g_ptzComputerName = SysAllocString( achComputerName );
  84. ASSERT( NULL != g_ptzComputerName );
  85. }
  86. }
  87. if ( NULL != g_ptzComputerName && 0 == _tcsicmp( pszMachineName, g_ptzComputerName ) )
  88. {
  89. return TRUE;
  90. }
  91. // compare with the local DNS name
  92. // SKwan confirms that ComputerNameDnsFullyQualified is the right name to use
  93. // when clustering is taken into account
  94. if ( NULL == g_ptzDnsComputerName )
  95. {
  96. TCHAR achDnsComputerName[DNS_MAX_NAME_BUFFER_LENGTH];
  97. DWORD dwSize = sizeof(achDnsComputerName)/sizeof(TCHAR);
  98. if ( !GetComputerNameEx(
  99. ComputerNameDnsFullyQualified,
  100. achDnsComputerName,
  101. &dwSize ) )
  102. {
  103. ASSERT(FALSE);
  104. }
  105. else
  106. {
  107. g_ptzDnsComputerName = SysAllocString( achDnsComputerName );
  108. ASSERT( NULL != g_ptzDnsComputerName );
  109. }
  110. }
  111. if ( NULL != g_ptzDnsComputerName && 0 == _tcsicmp( pszMachineName, g_ptzDnsComputerName ) )
  112. {
  113. return TRUE;
  114. }
  115. /*
  116. // compare with alternate DNS names
  117. do {
  118. hostent* phostent = gethostbyname( NULL );
  119. if (NULL == phostent)
  120. break;
  121. USES_CONVERSION;
  122. char** ppaliases = phostent->h_aliases;
  123. for ( ; *ppaliases != NULL; ppaliases++ )
  124. {
  125. TCHAR* ptsz = A2OLE(*ppaliases);
  126. if (0 == _tcsicmp( pszMachineName, ptsz ))
  127. {
  128. return TRUE;
  129. }
  130. }
  131. // these are IP addresses, not strings
  132. // char** ppaddresses = phostent->h_addr_list;
  133. // for ( ; *ppaddresses != NULL; ppaliases++ )
  134. // {
  135. // TCHAR* ptsz = A2OLE(*ppaddresses);
  136. // if (0 == _tcsicmp( pszMachineName, ptsz ))
  137. // {
  138. // return TRUE;
  139. // }
  140. // }
  141. } while (false); // false loop
  142. */
  143. return FALSE;
  144. } // IsLocalComputername()