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.

124 lines
2.9 KiB

  1. // Ping.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ConnMgr.h"
  5. #include "Ping.h"
  6. extern "C"
  7. {
  8. #include <ipexport.h>
  9. #include <icmpapi.h>
  10. }
  11. #define PING_TIMEOUT 100
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CPing
  19. CPing::CPing()
  20. {
  21. }
  22. CPing::~CPing()
  23. {
  24. }
  25. /////////////////////////////////////////////////////////////////////////////
  26. // Operations
  27. /////////////////////////////////////////////////////////////////////////////
  28. unsigned long CPing::ResolveIP(const CString& sIP)
  29. {
  30. //Task 1: Given IP Address i.e. "111.111.111.111",
  31. // Return Network byte ordered address (ulIP)
  32. unsigned long ulIP;
  33. USES_CONVERSION;
  34. char* pszIP = T2A(sIP);
  35. ulIP =(IPAddr)inet_addr(pszIP);
  36. return ulIP;
  37. }
  38. unsigned long CPing::ResolveName(const CString& sHostName)
  39. {
  40. //Task 1: Resolve HostName (through DNS or WINS, whichever appropriate)
  41. //Task 2: Return network byte ordered address (ulIP)
  42. unsigned long ulIP;
  43. hostent* phostent;
  44. USES_CONVERSION;
  45. char* pszName = T2A(sHostName);
  46. phostent = gethostbyname(pszName);
  47. if (phostent == NULL)
  48. {
  49. for( int i = 0; pszName[i] != NULL; i++ )
  50. {
  51. // note that according to RFC 952, the only valid characters are the numbers 0-9,
  52. // the letters A-Z, the period and the minues sign. All other characters are
  53. // converted to a minus sign if they are invalid. Technically, the underscore and the
  54. // exclamation point are invalid as well. However, since NT
  55. if( (int)pszName[i] <= 0 )
  56. {
  57. pszName[i] = '-';
  58. }
  59. }
  60. phostent = gethostbyname(pszName);
  61. if (phostent == NULL)
  62. return 0;
  63. }
  64. ulIP = *(DWORD*)(*phostent->h_addr_list);
  65. return ulIP;
  66. }
  67. BOOL CPing::Ping(unsigned long ulIP, int iPingTimeout)
  68. {
  69. //Task 1: Open ICMP Handle
  70. //Task 2: Create Structure to receive ping reply
  71. //Task 3: SendPing (SendEcho)
  72. //Task 4: Close ICMP Handle
  73. //Task 5: Return RoundTripTime
  74. unsigned long ip = ulIP;
  75. HANDLE icmphandle = IcmpCreateFile();
  76. char reply[sizeof(icmp_echo_reply)+8];
  77. icmp_echo_reply* iep = (icmp_echo_reply*)&reply;
  78. iep->RoundTripTime = 0xffffffff;
  79. for (int i = 0; i < ICMP_ECHO_RETRY; i++)
  80. if (IcmpSendEcho(icmphandle,ip,0,0,NULL,reply,sizeof(icmp_echo_reply)+8,iPingTimeout))
  81. break;
  82. IcmpCloseHandle(icmphandle);
  83. return iep->Status == IP_SUCCESS;
  84. }
  85. CString CPing::GetIP(unsigned long ulIP)
  86. {
  87. //Task 1: Given a host order ulIP Address
  88. // Return a IP address in format of xxx.xxx.xxx.xxx
  89. char* szAddr;
  90. struct in_addr inetAddr;
  91. inetAddr.s_addr = (IPAddr)ulIP;
  92. szAddr = inet_ntoa(inetAddr);
  93. USES_CONVERSION;
  94. CString csIP = A2T(szAddr);
  95. return csIP;
  96. }
  97. // Do not edit the following lines, which are needed by ClassWizard.
  98. #if 0
  99. BEGIN_MESSAGE_MAP(CPing, CSocket)
  100. //{{AFX_MSG_MAP(CPing)
  101. //}}AFX_MSG_MAP
  102. END_MESSAGE_MAP()
  103. #endif // 0
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CPing member functions