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.

276 lines
6.5 KiB

  1. #include "precomp.h"
  2. #include "ping.h"
  3. #include "avutil.h" // for RtStrToInt
  4. const CHAR g_cszPingData[] = "NetMeetingPing";
  5. const int PING_BUFFERSIZE = 1024;
  6. const DWORD PING_TIMEOUT = 4000; // 4 seconds
  7. const DWORD PING_RETRIES = 4;
  8. const TCHAR g_cszICMPDLLName[] = _TEXT("icmp.dll");
  9. //
  10. // CPing::Ping()
  11. //
  12. // Return value:
  13. // E_FAIL: Function failed
  14. // S_FALSE: Function succeeded, ping failed
  15. // S_OK: Function succeeded, ping succeeded
  16. //
  17. HRESULT CPing::Ping(DWORD dwAddr, DWORD dwTimeout, DWORD dwRetries)
  18. {
  19. DebugEntry(CPing::Ping);
  20. HRESULT hr = E_FAIL;
  21. if (0 != dwAddr)
  22. {
  23. if (NULL == m_hICMPDLL)
  24. {
  25. m_hICMPDLL = NmLoadLibrary(g_cszICMPDLLName,TRUE);
  26. }
  27. if (NULL != m_hICMPDLL)
  28. {
  29. m_pfnCreateFile = (PFNIcmpCreateFile)
  30. ::GetProcAddress(m_hICMPDLL, "IcmpCreateFile");
  31. m_pfnCloseHandle = (PFNIcmpCloseHandle)
  32. ::GetProcAddress(m_hICMPDLL, "IcmpCloseHandle");
  33. m_pfnSendEcho = (PFNIcmpSendEcho)
  34. ::GetProcAddress(m_hICMPDLL, "IcmpSendEcho");
  35. if ((NULL != m_pfnCreateFile) &&
  36. (NULL != m_pfnCloseHandle) &&
  37. (NULL != m_pfnSendEcho))
  38. {
  39. HANDLE hPing = m_pfnCreateFile();
  40. if (NULL != hPing)
  41. {
  42. BYTE buffer[PING_BUFFERSIZE];
  43. for (DWORD dwTry = 0; dwTry < dwRetries; dwTry++)
  44. {
  45. DWORD dwStatus = m_pfnSendEcho( hPing,
  46. dwAddr,
  47. (LPVOID) g_cszPingData,
  48. (WORD) CCHMAX(g_cszPingData),
  49. NULL,
  50. buffer,
  51. sizeof(buffer),
  52. dwTimeout);
  53. if (0 != dwStatus)
  54. {
  55. if (((PICMP_ECHO_REPLY)buffer)->Status == IP_SUCCESS)
  56. {
  57. TRACE_OUT(("ping: %d.%d.%d.%d succeeded",
  58. ((LPBYTE)&dwAddr)[0],
  59. ((LPBYTE)&dwAddr)[1],
  60. ((LPBYTE)&dwAddr)[2],
  61. ((LPBYTE)&dwAddr)[3]));
  62. hr = S_OK; // function succeeded - ping succeeded
  63. }
  64. else
  65. {
  66. TRACE_OUT(("ping: %d.%d.%d.%d failed",
  67. ((LPBYTE)&dwAddr)[0],
  68. ((LPBYTE)&dwAddr)[1],
  69. ((LPBYTE)&dwAddr)[2],
  70. ((LPBYTE)&dwAddr)[3]));
  71. hr = S_FALSE; // function succeeded - ping failed
  72. }
  73. break;
  74. }
  75. else
  76. {
  77. TRACE_OUT(("ping: %d.%d.%d.%d did not respond",
  78. ((LPBYTE)&dwAddr)[0],
  79. ((LPBYTE)&dwAddr)[1],
  80. ((LPBYTE)&dwAddr)[2],
  81. ((LPBYTE)&dwAddr)[3]));
  82. }
  83. }
  84. m_pfnCloseHandle(hPing);
  85. }
  86. else
  87. {
  88. ERROR_OUT(("IcmpCreateFile() failed"));
  89. }
  90. }
  91. else
  92. {
  93. ERROR_OUT(("Could not find icmp.dll entry points"));
  94. }
  95. }
  96. else
  97. {
  98. ERROR_OUT(("Could not load icmp.dll"));
  99. }
  100. }
  101. DebugExitHRESULT(CPing::Ping, hr);
  102. return hr;
  103. }
  104. BOOL CPing::IsAutodialEnabled ( VOID )
  105. {
  106. // Figure out the os platform if not done so
  107. //
  108. if (m_dwPlatformId == PLATFORM_UNKNOWN)
  109. {
  110. OSVERSIONINFO osvi;
  111. ZeroMemory (&osvi, sizeof (osvi));
  112. osvi.dwOSVersionInfoSize = sizeof (osvi);
  113. if (GetVersionEx (&osvi))
  114. {
  115. m_dwPlatformId = osvi.dwPlatformId;
  116. }
  117. else
  118. {
  119. return FALSE;
  120. }
  121. }
  122. // Check autodial enabling for either platform
  123. //
  124. BOOL fEnabled;
  125. switch (m_dwPlatformId)
  126. {
  127. case VER_PLATFORM_WIN32_WINDOWS: // 1, Windows 95
  128. fEnabled = IsWin95AutodialEnabled ();
  129. break;
  130. case VER_PLATFORM_WIN32_NT: // 2, Windows NT
  131. fEnabled = IsWinNTAutodialEnabled ();
  132. break;
  133. case VER_PLATFORM_WIN32s: // 0, Windows 3.1
  134. default: // unknown
  135. ASSERT (FALSE);
  136. fEnabled = FALSE;
  137. break;
  138. }
  139. return fEnabled;
  140. }
  141. #define c_szWin95AutodialRegFolder TEXT ("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")
  142. #define c_szWin95AutodialRegKey TEXT ("EnableAutodial")
  143. BOOL CPing::IsWin95AutodialEnabled ( VOID )
  144. {
  145. // Always check the registry
  146. //
  147. BOOL fEnabled = FALSE;
  148. // Need to check the registry setting.
  149. // In case of error, report no autodial.
  150. //
  151. HKEY hKey;
  152. if (RegOpenKeyEx ( HKEY_CURRENT_USER,
  153. c_szWin95AutodialRegFolder,
  154. 0,
  155. KEY_READ,
  156. &hKey) == NOERROR)
  157. {
  158. TCHAR szValue[16];
  159. ZeroMemory (&szValue[0], sizeof (DWORD));
  160. ULONG cb = sizeof (szValue);
  161. DWORD dwType;
  162. if (RegQueryValueEx (hKey, c_szWin95AutodialRegKey, NULL,
  163. &dwType, (BYTE *) &szValue[0], &cb)
  164. == NOERROR)
  165. {
  166. switch (dwType)
  167. {
  168. case REG_DWORD:
  169. case REG_BINARY:
  170. fEnabled = (BOOL) *(LONG *) &szValue[0];
  171. break;
  172. #if 0 // do not need to worry about this case, IE must maintain backward compatibility
  173. case REG_SZ:
  174. fEnabled = (BOOL) RtStrToInt (&szValue[0]);
  175. break;
  176. #endif // 0
  177. default:
  178. ASSERT (FALSE);
  179. break;
  180. }
  181. }
  182. RegCloseKey (hKey);
  183. }
  184. return fEnabled;
  185. }
  186. // RAS only runs on NT 4.0 or later, as a result, WINVER must be 0x401 or larger
  187. //
  188. #if (WINVER < 0x401)
  189. #undef WINVER
  190. #define WINVER 0x401
  191. #endif
  192. #include <ras.h>
  193. // DWORD APIENTRY RasGetAutodialParamA( DWORD, LPVOID, LPDWORD ); // defined in <ras.h>
  194. // DWORD APIENTRY RasGetAutodialParamW( DWORD, LPVOID, LPDWORD ); // defined in <ras.h>
  195. typedef DWORD (APIENTRY *PFN_RasGetAutodialParam) ( DWORD, LPVOID, LPDWORD );
  196. #define c_szRasGetAutodialParam "RasGetAutodialParamW"
  197. #define c_szRasApi32Dll TEXT ("rasapi32.dll")
  198. BOOL CPing::IsWinNTAutodialEnabled ( VOID )
  199. {
  200. // Decide if we want to check autodial registry setting
  201. //
  202. BOOL fEnabled = FALSE;
  203. if (m_fWinNTAutodialEnabled == AUTODIAL_UNKNOWN)
  204. {
  205. // We do not want to have initialization error
  206. //
  207. UINT uErrMode = SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
  208. // Load the library rasapi32.dll from the system directory
  209. //
  210. HINSTANCE hRasApi32Dll = NmLoadLibrary (c_szRasApi32Dll,TRUE);
  211. if (hRasApi32Dll != NULL)
  212. {
  213. // Get the proc address for RasGetAutodialParam()
  214. //
  215. PFN_RasGetAutodialParam pfn = (PFN_RasGetAutodialParam)
  216. GetProcAddress (hRasApi32Dll, c_szRasGetAutodialParam);
  217. if (pfn != NULL)
  218. {
  219. // Query RAS if it disables autodial
  220. //
  221. DWORD dwVal, dwSize = sizeof (DWORD);
  222. DWORD dwErr = (*pfn) (RASADP_LoginSessionDisable, &dwVal, &dwSize);
  223. if (dwErr == 0)
  224. {
  225. // Set the autodial flag only when everything succeeds
  226. //
  227. fEnabled = (dwVal == 0);
  228. }
  229. }
  230. FreeLibrary (hRasApi32Dll);
  231. }
  232. // Restore error mode
  233. //
  234. SetErrorMode (uErrMode);
  235. m_fWinNTAutodialEnabled = fEnabled;
  236. }
  237. else
  238. {
  239. // Do not need to check the registry setting.
  240. // Simply use the cached one.
  241. //
  242. fEnabled = m_fWinNTAutodialEnabled;
  243. }
  244. return fEnabled;
  245. }