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.

120 lines
4.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_WINPING_H__
  28. #define TALK_BASE_WINPING_H__
  29. #ifdef WIN32
  30. #include "talk/base/win32.h"
  31. #include "talk/base/basictypes.h"
  32. #include "talk/base/IPAddress.h"
  33. namespace talk_base {
  34. // This class wraps a Win32 API for doing ICMP pinging. This API, unlike the
  35. // the normal socket APIs (as implemented on Win9x), will return an error if
  36. // an ICMP packet with the dont-fragment bit set is too large. This means this
  37. // class can be used to detect the MTU to a given address.
  38. typedef struct ip_option_information {
  39. UCHAR Ttl; // Time To Live
  40. UCHAR Tos; // Type Of Service
  41. UCHAR Flags; // IP header flags
  42. UCHAR OptionsSize; // Size in bytes of options data
  43. PUCHAR OptionsData; // Pointer to options data
  44. } IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION;
  45. typedef HANDLE (WINAPI *PIcmpCreateFile)();
  46. typedef BOOL (WINAPI *PIcmpCloseHandle)(HANDLE icmp_handle);
  47. typedef HANDLE (WINAPI *PIcmp6CreateFile)();
  48. typedef BOOL (WINAPI *PIcmp6CloseHandle)(HANDLE icmp_handle);
  49. typedef DWORD (WINAPI *PIcmpSendEcho)(
  50. HANDLE IcmpHandle,
  51. ULONG DestinationAddress,
  52. LPVOID RequestData,
  53. WORD RequestSize,
  54. PIP_OPTION_INFORMATION RequestOptions,
  55. LPVOID ReplyBuffer,
  56. DWORD ReplySize,
  57. DWORD Timeout);
  58. typedef DWORD (WINAPI *PIcmp6SendEcho2)(
  59. HANDLE IcmpHandle,
  60. HANDLE Event,
  61. FARPROC ApcRoutine,
  62. PVOID ApcContext,
  63. struct sockaddr_in6 *SourceAddress,
  64. struct sockaddr_in6 *DestinationAddress,
  65. LPVOID RequestData,
  66. WORD RequestSize,
  67. PIP_OPTION_INFORMATION RequestOptions,
  68. LPVOID ReplyBuffer,
  69. DWORD ReplySize,
  70. DWORD Timeout
  71. );
  72. class WinPing {
  73. public:
  74. WinPing();
  75. ~WinPing();
  76. // Determines whether the class was initialized correctly.
  77. bool IsValid() { return valid_; }
  78. // Attempts to send a ping with the given parameters.
  79. enum PingResult { PING_FAIL, PING_INVALID_PARAMS,
  80. PING_TOO_LARGE, PING_TIMEOUT, PING_SUCCESS };
  81. PingResult Ping(
  82. IPAddress ip, uint32 data_size, uint32 timeout_millis, uint8 ttl,
  83. bool allow_fragments);
  84. private:
  85. HMODULE dll_;
  86. HANDLE hping_;
  87. HANDLE hping6_;
  88. PIcmpCreateFile create_;
  89. PIcmpCloseHandle close_;
  90. PIcmpSendEcho send_;
  91. PIcmp6CreateFile create6_;
  92. PIcmp6SendEcho2 send6_;
  93. char* data_;
  94. uint32 dlen_;
  95. char* reply_;
  96. uint32 rlen_;
  97. bool valid_;
  98. };
  99. } // namespace talk_base
  100. #endif // WIN32
  101. #endif // TALK_BASE_WINPING_H__