blob: 75f82b7b4ad6e4258c17b9f441f0518ee8c657e7 [file] [log] [blame]
henrike@webrtc.orgf7795df2014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_BASE_WINPING_H__
12#define WEBRTC_BASE_WINPING_H__
13
14#if defined(WEBRTC_WIN)
15
16#include "webrtc/base/win32.h"
17#include "webrtc/base/basictypes.h"
18#include "webrtc/base/IPAddress.h"
19
20namespace rtc {
21
22// This class wraps a Win32 API for doing ICMP pinging. This API, unlike the
23// the normal socket APIs (as implemented on Win9x), will return an error if
24// an ICMP packet with the dont-fragment bit set is too large. This means this
25// class can be used to detect the MTU to a given address.
26
27typedef struct ip_option_information {
28 UCHAR Ttl; // Time To Live
29 UCHAR Tos; // Type Of Service
30 UCHAR Flags; // IP header flags
31 UCHAR OptionsSize; // Size in bytes of options data
32 PUCHAR OptionsData; // Pointer to options data
33} IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION;
34
35typedef HANDLE (WINAPI *PIcmpCreateFile)();
36
37typedef BOOL (WINAPI *PIcmpCloseHandle)(HANDLE icmp_handle);
38
39typedef HANDLE (WINAPI *PIcmp6CreateFile)();
40
41typedef BOOL (WINAPI *PIcmp6CloseHandle)(HANDLE icmp_handle);
42
43typedef DWORD (WINAPI *PIcmpSendEcho)(
44 HANDLE IcmpHandle,
45 ULONG DestinationAddress,
46 LPVOID RequestData,
47 WORD RequestSize,
48 PIP_OPTION_INFORMATION RequestOptions,
49 LPVOID ReplyBuffer,
50 DWORD ReplySize,
51 DWORD Timeout);
52
53typedef DWORD (WINAPI *PIcmp6SendEcho2)(
54 HANDLE IcmpHandle,
55 HANDLE Event,
56 FARPROC ApcRoutine,
57 PVOID ApcContext,
58 struct sockaddr_in6 *SourceAddress,
59 struct sockaddr_in6 *DestinationAddress,
60 LPVOID RequestData,
61 WORD RequestSize,
62 PIP_OPTION_INFORMATION RequestOptions,
63 LPVOID ReplyBuffer,
64 DWORD ReplySize,
65 DWORD Timeout
66);
67
68class WinPing {
69public:
70 WinPing();
71 ~WinPing();
72
73 // Determines whether the class was initialized correctly.
74 bool IsValid() { return valid_; }
75
76 // Attempts to send a ping with the given parameters.
77 enum PingResult { PING_FAIL, PING_INVALID_PARAMS,
78 PING_TOO_LARGE, PING_TIMEOUT, PING_SUCCESS };
79 PingResult Ping(
80 IPAddress ip, uint32 data_size, uint32 timeout_millis, uint8 ttl,
81 bool allow_fragments);
82
83private:
84 HMODULE dll_;
85 HANDLE hping_;
86 HANDLE hping6_;
87 PIcmpCreateFile create_;
88 PIcmpCloseHandle close_;
89 PIcmpSendEcho send_;
90 PIcmp6CreateFile create6_;
91 PIcmp6SendEcho2 send6_;
92 char* data_;
93 uint32 dlen_;
94 char* reply_;
95 uint32 rlen_;
96 bool valid_;
97};
98
99} // namespace rtc
100
101#endif // WEBRTC_WIN
102
103#endif // WEBRTC_BASE_WINPING_H__