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.

130 lines
2.8 KiB

  1. // ping.cpp
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7. #include <nt.h>
  8. #include <ntrtl.h>
  9. #include <nturtl.h>
  10. #include <snmp.h>
  11. #include <winsock2.h>
  12. #include "llinfo.h"
  13. //#include "tcpcmd.h"
  14. #include "ipexport.h"
  15. #include "icmpapi.h"
  16. //#include "nlstxt.h"
  17. #include "Icmpapi.h"
  18. #include "tstst.h"
  19. #include "testdata.h"
  20. unsigned long get_pingee(char *ahstr, char **hstr, int *was_inaddr, int dnsreq)
  21. {
  22. struct hostent *hostp = NULL;
  23. long inaddr;
  24. if ( strcmp( ahstr, "255.255.255.255" ) == 0 ) {
  25. return(0L);
  26. }
  27. if ((inaddr = inet_addr(ahstr)) == -1L) {
  28. hostp = gethostbyname(ahstr);
  29. if (hostp) {
  30. /*
  31. * If we find a host entry, set up the internet address
  32. */
  33. inaddr = *(long *)hostp->h_addr;
  34. *was_inaddr = 0;
  35. } else {
  36. // Neither dotted, not name.
  37. return(0L);
  38. }
  39. } else {
  40. // Is dotted.
  41. *was_inaddr = 1;
  42. if (dnsreq == 1) {
  43. hostp = gethostbyaddr((char *)&inaddr,sizeof(inaddr),AF_INET);
  44. }
  45. }
  46. *hstr = hostp ? hostp->h_name : (char *)NULL;
  47. return(inaddr);
  48. }
  49. bool CanPing ()
  50. {
  51. if (!CTSTestData::GetMachineName())
  52. return true;
  53. USES_CONVERSION;
  54. const Timeout = 4000L;
  55. WSADATA WsaData;
  56. if (WSAStartup( 0x0101, &WsaData))
  57. {
  58. return false;
  59. }
  60. HANDLE IcmpHandle;
  61. IcmpHandle = IcmpCreateFile();
  62. if (IcmpHandle == INVALID_HANDLE_VALUE)
  63. {
  64. return false;
  65. }
  66. char *hostname = NULL;
  67. int was_inaddr;
  68. int dnsreq = 0;
  69. IPAddr address = 0;
  70. address = get_pingee(T2A(CTSTestData::GetMachineName()), &hostname, &was_inaddr, dnsreq);
  71. if ( !address || (address == INADDR_NONE) )
  72. {
  73. return false;
  74. }
  75. const SendSize = 32;
  76. const RecvSize = 0x2000 - 8;
  77. char *SendBuffer = (char *)LocalAlloc(LMEM_FIXED, SendSize);
  78. char *RcvBuffer = (char *)LocalAlloc(LMEM_FIXED, RecvSize);
  79. if (!RcvBuffer || !SendBuffer)
  80. {
  81. if (RcvBuffer)
  82. LocalFree(RcvBuffer);
  83. if (SendBuffer)
  84. LocalFree(SendBuffer);
  85. return false;
  86. }
  87. IP_OPTION_INFORMATION SendOpts;
  88. SendOpts.OptionsData = NULL;
  89. SendOpts.OptionsSize = 0;
  90. SendOpts.Ttl = 128;
  91. SendOpts.Tos = 0;
  92. SendOpts.Flags = 0;
  93. if (IcmpSendEcho2(IcmpHandle,
  94. 0,
  95. NULL,
  96. NULL,
  97. address,
  98. SendBuffer,
  99. (unsigned short) SendSize,
  100. &SendOpts,
  101. RcvBuffer,
  102. RecvSize,
  103. Timeout) == 0)
  104. {
  105. return false;
  106. }
  107. IcmpCloseHandle(IcmpHandle);
  108. LocalFree(SendBuffer);
  109. LocalFree(RcvBuffer);
  110. return true;
  111. }