Home | History | Annotate | Download | only in base
      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 
     36 namespace talk_base {
     37 
     38 // This class wraps a Win32 API for doing ICMP pinging.  This API, unlike the
     39 // the normal socket APIs (as implemented on Win9x), will return an error if
     40 // an ICMP packet with the dont-fragment bit set is too large.  This means this
     41 // class can be used to detect the MTU to a given address.
     42 
     43 typedef struct ip_option_information {
     44     UCHAR   Ttl;                // Time To Live
     45     UCHAR   Tos;                // Type Of Service
     46     UCHAR   Flags;              // IP header flags
     47     UCHAR   OptionsSize;        // Size in bytes of options data
     48     PUCHAR  OptionsData;        // Pointer to options data
     49 } IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION;
     50 
     51 typedef HANDLE (WINAPI *PIcmpCreateFile)();
     52 
     53 typedef BOOL (WINAPI *PIcmpCloseHandle)(HANDLE icmp_handle);
     54 
     55 typedef DWORD (WINAPI *PIcmpSendEcho)(
     56     HANDLE                   IcmpHandle,
     57     ULONG                    DestinationAddress,
     58     LPVOID                   RequestData,
     59     WORD                     RequestSize,
     60     PIP_OPTION_INFORMATION   RequestOptions,
     61     LPVOID                   ReplyBuffer,
     62     DWORD                    ReplySize,
     63     DWORD                    Timeout);
     64 
     65 class WinPing {
     66 public:
     67     WinPing();
     68     ~WinPing();
     69 
     70     // Determines whether the class was initialized correctly.
     71     bool IsValid() { return valid_; }
     72 
     73     // Attempts to send a ping with the given parameters.
     74     enum PingResult { PING_FAIL, PING_TOO_LARGE, PING_TIMEOUT, PING_SUCCESS };
     75     PingResult Ping(
     76         uint32 ip, uint32 data_size, uint32 timeout_millis, uint8 ttl,
     77         bool allow_fragments);
     78 
     79 private:
     80     HMODULE dll_;
     81     HANDLE hping_;
     82     PIcmpCreateFile create_;
     83     PIcmpCloseHandle close_;
     84     PIcmpSendEcho send_;
     85     char* data_;
     86     uint32 dlen_;
     87     char* reply_;
     88     uint32 rlen_;
     89     bool valid_;
     90 };
     91 
     92 } // namespace talk_base
     93 
     94 #endif // WIN32
     95 
     96 #endif // TALK_BASE_WINPING_H__
     97 
     98