blob: de79b85e14fc0f4313aeacf06a517c3e61b8946b [file] [log] [blame]
henrike@webrtc.orgf0488722014-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"
thakis72b7f932016-07-15 07:21:22 -070018#include "webrtc/base/ipaddress.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
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 };
Peter Boström0c4e06b2015-10-07 12:23:21 +020079 PingResult Ping(IPAddress ip,
80 uint32_t data_size,
81 uint32_t timeout_millis,
82 uint8_t ttl,
83 bool allow_fragments);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084
85private:
86 HMODULE dll_;
87 HANDLE hping_;
88 HANDLE hping6_;
89 PIcmpCreateFile create_;
90 PIcmpCloseHandle close_;
91 PIcmpSendEcho send_;
92 PIcmp6CreateFile create6_;
93 PIcmp6SendEcho2 send6_;
94 char* data_;
Peter Boström0c4e06b2015-10-07 12:23:21 +020095 uint32_t dlen_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 char* reply_;
Peter Boström0c4e06b2015-10-07 12:23:21 +020097 uint32_t rlen_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098 bool valid_;
99};
100
101} // namespace rtc
102
103#endif // WEBRTC_WIN
104
105#endif // WEBRTC_BASE_WINPING_H__