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.

376 lines
13 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. #include "talk/base/winping.h"
  28. #include <Iphlpapi.h>
  29. #include <cassert>
  30. #include "talk/base/byteorder.h"
  31. #include "talk/base/common.h"
  32. #include "talk/base/ipaddress.h"
  33. #include "talk/base/logging.h"
  34. #include "talk/base/nethelpers.h"
  35. #include "talk/base/socketaddress.h"
  36. namespace talk_base {
  37. //////////////////////////////////////////////////////////////////////
  38. // Found in IPExport.h
  39. //////////////////////////////////////////////////////////////////////
  40. typedef struct icmp_echo_reply {
  41. ULONG Address; // Replying address
  42. ULONG Status; // Reply IP_STATUS
  43. ULONG RoundTripTime; // RTT in milliseconds
  44. USHORT DataSize; // Reply data size in bytes
  45. USHORT Reserved; // Reserved for system use
  46. PVOID Data; // Pointer to the reply data
  47. struct ip_option_information Options; // Reply options
  48. } ICMP_ECHO_REPLY, * PICMP_ECHO_REPLY;
  49. typedef struct icmpv6_echo_reply_lh {
  50. sockaddr_in6 Address;
  51. ULONG Status;
  52. unsigned int RoundTripTime;
  53. } ICMPV6_ECHO_REPLY, *PICMPV6_ECHO_REPLY;
  54. //
  55. // IP_STATUS codes returned from IP APIs
  56. //
  57. #define IP_STATUS_BASE 11000
  58. #define IP_SUCCESS 0
  59. #define IP_BUF_TOO_SMALL (IP_STATUS_BASE + 1)
  60. #define IP_DEST_NET_UNREACHABLE (IP_STATUS_BASE + 2)
  61. #define IP_DEST_HOST_UNREACHABLE (IP_STATUS_BASE + 3)
  62. #define IP_DEST_PROT_UNREACHABLE (IP_STATUS_BASE + 4)
  63. #define IP_DEST_PORT_UNREACHABLE (IP_STATUS_BASE + 5)
  64. #define IP_NO_RESOURCES (IP_STATUS_BASE + 6)
  65. #define IP_BAD_OPTION (IP_STATUS_BASE + 7)
  66. #define IP_HW_ERROR (IP_STATUS_BASE + 8)
  67. #define IP_PACKET_TOO_BIG (IP_STATUS_BASE + 9)
  68. #define IP_REQ_TIMED_OUT (IP_STATUS_BASE + 10)
  69. #define IP_BAD_REQ (IP_STATUS_BASE + 11)
  70. #define IP_BAD_ROUTE (IP_STATUS_BASE + 12)
  71. #define IP_TTL_EXPIRED_TRANSIT (IP_STATUS_BASE + 13)
  72. #define IP_TTL_EXPIRED_REASSEM (IP_STATUS_BASE + 14)
  73. #define IP_PARAM_PROBLEM (IP_STATUS_BASE + 15)
  74. #define IP_SOURCE_QUENCH (IP_STATUS_BASE + 16)
  75. #define IP_OPTION_TOO_BIG (IP_STATUS_BASE + 17)
  76. #define IP_BAD_DESTINATION (IP_STATUS_BASE + 18)
  77. #define IP_ADDR_DELETED (IP_STATUS_BASE + 19)
  78. #define IP_SPEC_MTU_CHANGE (IP_STATUS_BASE + 20)
  79. #define IP_MTU_CHANGE (IP_STATUS_BASE + 21)
  80. #define IP_UNLOAD (IP_STATUS_BASE + 22)
  81. #define IP_ADDR_ADDED (IP_STATUS_BASE + 23)
  82. #define IP_MEDIA_CONNECT (IP_STATUS_BASE + 24)
  83. #define IP_MEDIA_DISCONNECT (IP_STATUS_BASE + 25)
  84. #define IP_BIND_ADAPTER (IP_STATUS_BASE + 26)
  85. #define IP_UNBIND_ADAPTER (IP_STATUS_BASE + 27)
  86. #define IP_DEVICE_DOES_NOT_EXIST (IP_STATUS_BASE + 28)
  87. #define IP_DUPLICATE_ADDRESS (IP_STATUS_BASE + 29)
  88. #define IP_INTERFACE_METRIC_CHANGE (IP_STATUS_BASE + 30)
  89. #define IP_RECONFIG_SECFLTR (IP_STATUS_BASE + 31)
  90. #define IP_NEGOTIATING_IPSEC (IP_STATUS_BASE + 32)
  91. #define IP_INTERFACE_WOL_CAPABILITY_CHANGE (IP_STATUS_BASE + 33)
  92. #define IP_DUPLICATE_IPADD (IP_STATUS_BASE + 34)
  93. #define IP_GENERAL_FAILURE (IP_STATUS_BASE + 50)
  94. #define MAX_IP_STATUS IP_GENERAL_FAILURE
  95. #define IP_PENDING (IP_STATUS_BASE + 255)
  96. //
  97. // Values used in the IP header Flags field.
  98. //
  99. #define IP_FLAG_DF 0x2 // Don't fragment this packet.
  100. //
  101. // Supported IP Option Types.
  102. //
  103. // These types define the options which may be used in the OptionsData field
  104. // of the ip_option_information structure. See RFC 791 for a complete
  105. // description of each.
  106. //
  107. #define IP_OPT_EOL 0 // End of list option
  108. #define IP_OPT_NOP 1 // No operation
  109. #define IP_OPT_SECURITY 0x82 // Security option
  110. #define IP_OPT_LSRR 0x83 // Loose source route
  111. #define IP_OPT_SSRR 0x89 // Strict source route
  112. #define IP_OPT_RR 0x7 // Record route
  113. #define IP_OPT_TS 0x44 // Timestamp
  114. #define IP_OPT_SID 0x88 // Stream ID (obsolete)
  115. #define IP_OPT_ROUTER_ALERT 0x94 // Router Alert Option
  116. #define MAX_OPT_SIZE 40 // Maximum length of IP options in bytes
  117. //////////////////////////////////////////////////////////////////////
  118. // Global Constants and Types
  119. //////////////////////////////////////////////////////////////////////
  120. const char * const ICMP_DLL_NAME = "Iphlpapi.dll";
  121. const char * const ICMP_CREATE_FUNC = "IcmpCreateFile";
  122. const char * const ICMP_CLOSE_FUNC = "IcmpCloseHandle";
  123. const char * const ICMP_SEND_FUNC = "IcmpSendEcho";
  124. const char * const ICMP6_CREATE_FUNC = "Icmp6CreateFile";
  125. const char * const ICMP6_CLOSE_FUNC = "Icmp6CloseHandle";
  126. const char * const ICMP6_SEND_FUNC = "Icmp6SendEcho2";
  127. inline uint32 ReplySize(uint32 data_size, int family) {
  128. if (family == AF_INET) {
  129. // A ping error message is 8 bytes long, so make sure we allow for at least
  130. // 8 bytes of reply data.
  131. return sizeof(ICMP_ECHO_REPLY) + talk_base::_max<uint32>(8, data_size);
  132. } else if (family == AF_INET6) {
  133. // Per MSDN, Send6IcmpEcho2 needs at least one ICMPV6_ECHO_REPLY,
  134. // 8 bytes for ICMP header, _and_ an IO_BLOCK_STATUS (2 pointers),
  135. // in addition to the data size.
  136. return sizeof(ICMPV6_ECHO_REPLY) + data_size + 8 + (2 * sizeof(DWORD*));
  137. } else {
  138. return 0;
  139. }
  140. }
  141. //////////////////////////////////////////////////////////////////////
  142. // WinPing
  143. //////////////////////////////////////////////////////////////////////
  144. WinPing::WinPing()
  145. : dll_(0), hping_(INVALID_HANDLE_VALUE), create_(0), close_(0), send_(0),
  146. create6_(0), send6_(0), data_(0), dlen_(0), reply_(0),
  147. rlen_(0), valid_(false) {
  148. dll_ = LoadLibraryA(ICMP_DLL_NAME);
  149. if (!dll_) {
  150. LOG(LERROR) << "LoadLibrary: " << GetLastError();
  151. return;
  152. }
  153. create_ = (PIcmpCreateFile) GetProcAddress(dll_, ICMP_CREATE_FUNC);
  154. close_ = (PIcmpCloseHandle) GetProcAddress(dll_, ICMP_CLOSE_FUNC);
  155. send_ = (PIcmpSendEcho) GetProcAddress(dll_, ICMP_SEND_FUNC);
  156. if (!create_ || !close_ || !send_) {
  157. LOG(LERROR) << "GetProcAddress(ICMP_*): " << GetLastError();
  158. return;
  159. }
  160. hping_ = create_();
  161. if (hping_ == INVALID_HANDLE_VALUE) {
  162. LOG(LERROR) << "IcmpCreateFile: " << GetLastError();
  163. return;
  164. }
  165. if (HasIPv6Enabled()) {
  166. create6_ = (PIcmp6CreateFile) GetProcAddress(dll_, ICMP6_CREATE_FUNC);
  167. send6_ = (PIcmp6SendEcho2) GetProcAddress(dll_, ICMP6_SEND_FUNC);
  168. if (!create6_ || !send6_) {
  169. LOG(LERROR) << "GetProcAddress(ICMP6_*): " << GetLastError();
  170. return;
  171. }
  172. hping6_ = create6_();
  173. if (hping6_ == INVALID_HANDLE_VALUE) {
  174. LOG(LERROR) << "Icmp6CreateFile: " << GetLastError();
  175. }
  176. }
  177. dlen_ = 0;
  178. rlen_ = ReplySize(dlen_, AF_INET);
  179. data_ = new char[dlen_];
  180. reply_ = new char[rlen_];
  181. valid_ = true;
  182. }
  183. WinPing::~WinPing() {
  184. if ((hping_ != INVALID_HANDLE_VALUE) && close_) {
  185. if (!close_(hping_))
  186. LOG(WARNING) << "IcmpCloseHandle: " << GetLastError();
  187. }
  188. if ((hping6_ != INVALID_HANDLE_VALUE) && close_) {
  189. if (!close_(hping6_)) {
  190. LOG(WARNING) << "Icmp6CloseHandle: " << GetLastError();
  191. }
  192. }
  193. if (dll_)
  194. FreeLibrary(dll_);
  195. delete[] data_;
  196. delete[] reply_;
  197. }
  198. WinPing::PingResult WinPing::Ping(
  199. IPAddress ip, uint32 data_size, uint32 timeout, uint8 ttl,
  200. bool allow_fragments) {
  201. if (data_size == 0 || timeout == 0 || ttl == 0) {
  202. LOG(LERROR) << "IcmpSendEcho: data_size/timeout/ttl is 0.";
  203. return PING_INVALID_PARAMS;
  204. }
  205. assert(IsValid());
  206. IP_OPTION_INFORMATION ipopt;
  207. memset(&ipopt, 0, sizeof(ipopt));
  208. if (!allow_fragments)
  209. ipopt.Flags |= IP_FLAG_DF;
  210. ipopt.Ttl = ttl;
  211. uint32 reply_size = ReplySize(data_size, ip.family());
  212. if (data_size > dlen_) {
  213. delete [] data_;
  214. dlen_ = data_size;
  215. data_ = new char[dlen_];
  216. memset(data_, 'z', dlen_);
  217. }
  218. if (reply_size > rlen_) {
  219. delete [] reply_;
  220. rlen_ = reply_size;
  221. reply_ = new char[rlen_];
  222. }
  223. DWORD result = 0;
  224. if (ip.family() == AF_INET) {
  225. result = send_(hping_, ip.ipv4_address().S_un.S_addr,
  226. data_, uint16(data_size), &ipopt,
  227. reply_, reply_size, timeout);
  228. } else if (ip.family() == AF_INET6) {
  229. sockaddr_in6 src = {0};
  230. sockaddr_in6 dst = {0};
  231. src.sin6_family = AF_INET6;
  232. dst.sin6_family = AF_INET6;
  233. dst.sin6_addr = ip.ipv6_address();
  234. result = send6_(hping6_, NULL, NULL, NULL,
  235. &src, &dst,
  236. data_, int16(data_size), &ipopt,
  237. reply_, reply_size, timeout);
  238. }
  239. if (result == 0) {
  240. DWORD error = GetLastError();
  241. if (error == IP_PACKET_TOO_BIG)
  242. return PING_TOO_LARGE;
  243. if (error == IP_REQ_TIMED_OUT)
  244. return PING_TIMEOUT;
  245. LOG(LERROR) << "IcmpSendEcho(" << ip.ToString()
  246. << ", " << data_size << "): " << error;
  247. return PING_FAIL;
  248. }
  249. return PING_SUCCESS;
  250. }
  251. //////////////////////////////////////////////////////////////////////
  252. // Microsoft Documenation
  253. //////////////////////////////////////////////////////////////////////
  254. //
  255. // Routine Name:
  256. //
  257. // IcmpCreateFile
  258. //
  259. // Routine Description:
  260. //
  261. // Opens a handle on which ICMP Echo Requests can be issued.
  262. //
  263. // Arguments:
  264. //
  265. // None.
  266. //
  267. // Return Value:
  268. //
  269. // An open file handle or INVALID_HANDLE_VALUE. Extended error information
  270. // is available by calling GetLastError().
  271. //
  272. //////////////////////////////////////////////////////////////////////
  273. //
  274. // Routine Name:
  275. //
  276. // IcmpCloseHandle
  277. //
  278. // Routine Description:
  279. //
  280. // Closes a handle opened by ICMPOpenFile.
  281. //
  282. // Arguments:
  283. //
  284. // IcmpHandle - The handle to close.
  285. //
  286. // Return Value:
  287. //
  288. // TRUE if the handle was closed successfully, otherwise FALSE. Extended
  289. // error information is available by calling GetLastError().
  290. //
  291. //////////////////////////////////////////////////////////////////////
  292. //
  293. // Routine Name:
  294. //
  295. // IcmpSendEcho
  296. //
  297. // Routine Description:
  298. //
  299. // Sends an ICMP Echo request and returns any replies. The
  300. // call returns when the timeout has expired or the reply buffer
  301. // is filled.
  302. //
  303. // Arguments:
  304. //
  305. // IcmpHandle - An open handle returned by ICMPCreateFile.
  306. //
  307. // DestinationAddress - The destination of the echo request.
  308. //
  309. // RequestData - A buffer containing the data to send in the
  310. // request.
  311. //
  312. // RequestSize - The number of bytes in the request data buffer.
  313. //
  314. // RequestOptions - Pointer to the IP header options for the request.
  315. // May be NULL.
  316. //
  317. // ReplyBuffer - A buffer to hold any replies to the request.
  318. // On return, the buffer will contain an array of
  319. // ICMP_ECHO_REPLY structures followed by the
  320. // options and data for the replies. The buffer
  321. // should be large enough to hold at least one
  322. // ICMP_ECHO_REPLY structure plus
  323. // MAX(RequestSize, 8) bytes of data since an ICMP
  324. // error message contains 8 bytes of data.
  325. //
  326. // ReplySize - The size in bytes of the reply buffer.
  327. //
  328. // Timeout - The time in milliseconds to wait for replies.
  329. //
  330. // Return Value:
  331. //
  332. // Returns the number of ICMP_ECHO_REPLY structures stored in ReplyBuffer.
  333. // The status of each reply is contained in the structure. If the return
  334. // value is zero, extended error information is available via
  335. // GetLastError().
  336. //
  337. //////////////////////////////////////////////////////////////////////
  338. } // namespace talk_base