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 28 #ifndef TALK_BASE_WINPING_H__ 29 #define TALK_BASE_WINPING_H__ 30 31 #ifdef WIN32 32 33 #include "talk/base/win32.h" 34 #include "talk/base/basictypes.h" 35 #include "talk/base/IPAddress.h" 36 37 namespace talk_base { 38 39 // This class wraps a Win32 API for doing ICMP pinging. This API, unlike the 40 // the normal socket APIs (as implemented on Win9x), will return an error if 41 // an ICMP packet with the dont-fragment bit set is too large. This means this 42 // class can be used to detect the MTU to a given address. 43 44 typedef struct ip_option_information { 45 UCHAR Ttl; // Time To Live 46 UCHAR Tos; // Type Of Service 47 UCHAR Flags; // IP header flags 48 UCHAR OptionsSize; // Size in bytes of options data 49 PUCHAR OptionsData; // Pointer to options data 50 } IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION; 51 52 typedef HANDLE (WINAPI *PIcmpCreateFile)(); 53 54 typedef BOOL (WINAPI *PIcmpCloseHandle)(HANDLE icmp_handle); 55 56 typedef HANDLE (WINAPI *PIcmp6CreateFile)(); 57 58 typedef BOOL (WINAPI *PIcmp6CloseHandle)(HANDLE icmp_handle); 59 60 typedef DWORD (WINAPI *PIcmpSendEcho)( 61 HANDLE IcmpHandle, 62 ULONG DestinationAddress, 63 LPVOID RequestData, 64 WORD RequestSize, 65 PIP_OPTION_INFORMATION RequestOptions, 66 LPVOID ReplyBuffer, 67 DWORD ReplySize, 68 DWORD Timeout); 69 70 typedef DWORD (WINAPI *PIcmp6SendEcho2)( 71 HANDLE IcmpHandle, 72 HANDLE Event, 73 FARPROC ApcRoutine, 74 PVOID ApcContext, 75 struct sockaddr_in6 *SourceAddress, 76 struct sockaddr_in6 *DestinationAddress, 77 LPVOID RequestData, 78 WORD RequestSize, 79 PIP_OPTION_INFORMATION RequestOptions, 80 LPVOID ReplyBuffer, 81 DWORD ReplySize, 82 DWORD Timeout 83 ); 84 85 class WinPing { 86 public: 87 WinPing(); 88 ~WinPing(); 89 90 // Determines whether the class was initialized correctly. 91 bool IsValid() { return valid_; } 92 93 // Attempts to send a ping with the given parameters. 94 enum PingResult { PING_FAIL, PING_INVALID_PARAMS, 95 PING_TOO_LARGE, PING_TIMEOUT, PING_SUCCESS }; 96 PingResult Ping( 97 IPAddress ip, uint32 data_size, uint32 timeout_millis, uint8 ttl, 98 bool allow_fragments); 99 100 private: 101 HMODULE dll_; 102 HANDLE hping_; 103 HANDLE hping6_; 104 PIcmpCreateFile create_; 105 PIcmpCloseHandle close_; 106 PIcmpSendEcho send_; 107 PIcmp6CreateFile create6_; 108 PIcmp6SendEcho2 send6_; 109 char* data_; 110 uint32 dlen_; 111 char* reply_; 112 uint32 rlen_; 113 bool valid_; 114 }; 115 116 } // namespace talk_base 117 118 #endif // WIN32 119 120 #endif // TALK_BASE_WINPING_H__ 121