-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathWinMTRICMPUtils.ixx
More file actions
401 lines (361 loc) · 12 KB
/
WinMTRICMPUtils.ixx
File metadata and controls
401 lines (361 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
WinMTR
Copyright (C) 2010-2019 Appnor MSP S.A. - http://www.appnor.com
Copyright (C) 2019-2021 Leetsoftwerx
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
module;
#pragma warning (disable : 4005)
#include<sal.h>
#ifdef _RESUMABLE_ENABLE_LEGACY_AWAIT_ADAPTERS
#error "don't compile with /await"
#endif
export module WinMTRICMPUtils;
import <concepts>;
import <coroutine>;
import <span>;
import <type_traits>;
import <algorithm>;
import <ppl.h>;
import <ppltasks.h>;
import <pplawait.h>;
import <winrt/base.h>;
import "WinMTRICMPPIOdef.h";
constexpr auto ECHO_REPLY_TIMEOUT = 5000;
export
template<class T>
struct ping_reply {
using type = void;
};
export
template<>
struct ping_reply<sockaddr_in> {
using type = ICMP_ECHO_REPLY;
};
export
template<>
struct ping_reply<sockaddr_in6> {
using type = ICMPV6_ECHO_REPLY;
};
export
template <class T>
using ping_reply_t = ping_reply<T>::type;
export
template<class T>
struct any_address {
static constexpr void value() noexcept {};
};
export
template<>
struct any_address<sockaddr_in> {
static constexpr auto value() noexcept {
return ADDR_ANY;
}
};
export
template<>
struct any_address<sockaddr_in6> {
static SOCKADDR_IN6* value() noexcept {
static sockaddr_in6 anyaddr = { .sin6_family = AF_INET6, .sin6_addr = in6addr_any };
return &anyaddr;
}
};
export
template<class T>
concept icmp_type = requires {
typename T::reply_type;
typename T::reply_type_ptr;
typename T::addrtype;
typename T::storagetype;
};
export
template<class T>
concept supports_ICMP_ping = icmp_type<T> && requires(
_In_ HANDLE IcmpHandle,
_In_opt_ HANDLE Event,
#ifdef PIO_APC_ROUTINE_DEFINED
_In_opt_ PIO_APC_ROUTINE ApcRoutine,
#else
_In_opt_ FARPROC ApcRoutine,
#endif
_In_opt_ PVOID ApcContext,
_In_ typename T::addrtype SourceAddress,
_In_ typename T::addrtype DestinationAddress,
_In_reads_bytes_(RequestSize) LPVOID RequestData,
_In_ WORD RequestSize,
_In_opt_ PIP_OPTION_INFORMATION RequestOptions,
_Out_writes_bytes_(ReplySize) LPVOID ReplyBuffer,
_In_range_(>= , sizeof(ICMP_ECHO_REPLY) + RequestSize + 8)
DWORD ReplySize,
_In_ DWORD Timeout) {
{ T::pingmethod(IcmpHandle,
Event,
ApcRoutine,
ApcContext,
SourceAddress,
DestinationAddress,
RequestData,
RequestSize,
RequestOptions,
ReplyBuffer,
ReplySize,
Timeout) } noexcept -> std::convertible_to<DWORD>;
};
export
template<class T>
concept supports_ICMP_ping_parse = icmp_type<T> && requires(
_Out_writes_bytes_(ReplySize) LPVOID ReplyBuffer,
_In_range_(> , sizeof(T::reply_type) + 8)
DWORD ReplySize) {
{
T::parsemethod(
ReplyBuffer,
ReplySize) } noexcept -> std::convertible_to<DWORD>;
};
export
template<class T>
concept icmp_pingable = icmp_type<T> && supports_ICMP_ping<T> && supports_ICMP_ping_parse<T> && requires(
std::add_const_t<typename T::reply_type_ptr> rtp
, std::add_pointer_t<typename T::storagetype> storage) {
{ T::get_anyaddr() } noexcept -> std::convertible_to<typename T::addrtype>;
{ T::to_addr_from_ping(rtp) } noexcept -> std::convertible_to<typename T::storagetype>;
{ T::to_addr_from_storage(storage) } noexcept -> std::convertible_to<typename T::addrtype>;
};
export
template<typename T>
struct icmp_ping_traits {
using reply_type = ping_reply_t<T>;
using reply_type_ptr = std::add_pointer_t<reply_type>;
using addrtype = void;
using storagetype = void;
static constexpr auto pingmethod = 0;
static constexpr auto parsemethod = 0;
static auto get_anyaddr() noexcept -> addrtype {
return;
}
static storagetype to_addr_from_ping(const reply_type_ptr reply) noexcept {
return {};
}
};
export
template<>
struct icmp_ping_traits<sockaddr_in> {
using reply_type = ping_reply_t<sockaddr_in>;
using reply_type_ptr = std::add_pointer_t<reply_type>;
using addrtype = IPAddr;
using storagetype = sockaddr_in;
static inline DWORD
WINAPI
pingmethod(
_In_ HANDLE IcmpHandle,
_In_opt_ HANDLE Event,
#ifdef PIO_APC_ROUTINE_DEFINED
_In_opt_ PIO_APC_ROUTINE ApcRoutine,
#else
_In_opt_ FARPROC ApcRoutine,
#endif
_In_opt_ PVOID ApcContext,
_In_ addrtype SourceAddress,
_In_ addrtype DestinationAddress,
_In_reads_bytes_(RequestSize) LPVOID RequestData,
_In_ WORD RequestSize,
_In_opt_ PIP_OPTION_INFORMATION RequestOptions,
_Out_writes_bytes_(ReplySize) LPVOID ReplyBuffer,
_In_range_(>= , sizeof(ICMP_ECHO_REPLY) + RequestSize + 8)
DWORD ReplySize,
_In_ DWORD Timeout
) noexcept {
return IcmpSendEcho2Ex(IcmpHandle, Event, ApcRoutine, ApcContext, SourceAddress, DestinationAddress, RequestData, RequestSize, RequestOptions, ReplyBuffer, ReplySize, Timeout);
}
static inline DWORD
WINAPI
parsemethod(
_Out_writes_bytes_(ReplySize) LPVOID ReplyBuffer,
_In_range_(> , sizeof(reply_type) + 8)
DWORD ReplySize
) noexcept {
return IcmpParseReplies(ReplyBuffer, ReplySize);
}
static auto get_anyaddr() noexcept -> addrtype {
return any_address<sockaddr_in>::value();
}
static addrtype to_addr_from_storage(storagetype* storage) noexcept {
return storage->sin_addr.S_un.S_addr;
}
static storagetype to_addr_from_ping(const reply_type_ptr reply) noexcept {
sockaddr_in naddr = { .sin_family = AF_INET };
naddr.sin_addr.S_un.S_addr = reply->Address;
return naddr;
}
};
export
template<>
struct icmp_ping_traits<sockaddr_in6> {
using reply_type = ping_reply_t<sockaddr_in6>;
using reply_type_ptr = std::add_pointer_t<reply_type>;
using addrtype = sockaddr_in6*;
using storagetype = std::remove_pointer_t<addrtype>;
static inline DWORD
WINAPI
pingmethod(
_In_ HANDLE IcmpHandle,
_In_opt_ HANDLE Event,
#ifdef PIO_APC_ROUTINE_DEFINED
_In_opt_ PIO_APC_ROUTINE ApcRoutine,
#else
_In_opt_ FARPROC ApcRoutine,
#endif
_In_opt_ PVOID ApcContext,
_In_ addrtype SourceAddress,
_In_ addrtype DestinationAddress,
_In_reads_bytes_(RequestSize) LPVOID RequestData,
_In_ WORD RequestSize,
_In_opt_ PIP_OPTION_INFORMATION RequestOptions,
_Out_writes_bytes_(ReplySize) LPVOID ReplyBuffer,
_In_range_(>= , sizeof(ICMP_ECHO_REPLY) + RequestSize + 8)
DWORD ReplySize,
_In_ DWORD Timeout
) noexcept {
return Icmp6SendEcho2(IcmpHandle, Event, ApcRoutine, ApcContext, SourceAddress, DestinationAddress, RequestData, RequestSize, RequestOptions, ReplyBuffer, ReplySize, Timeout);
}
static inline DWORD
WINAPI
parsemethod(
_Out_writes_bytes_(ReplySize) LPVOID ReplyBuffer,
_In_range_(> , sizeof(reply_type) + 8)
DWORD ReplySize
) noexcept {
return Icmp6ParseReplies(ReplyBuffer, ReplySize);
}
static auto get_anyaddr() noexcept -> addrtype {
return any_address<sockaddr_in6>::value();
}
static constexpr addrtype to_addr_from_storage(storagetype* storage) noexcept {
return storage;
}
static storagetype to_addr_from_ping(const reply_type_ptr reply) noexcept {
sockaddr_in6 naddr = { .sin6_family = AF_INET6 };
std::ranges::copy(reply->Address.sin6_addr, naddr.sin6_addr.u.Word);
return naddr;
}
};
export
template<icmp_pingable traits>
struct icmp_ping final {
struct wait_traits
{
using type = PTP_WAIT;
static void close(type value) noexcept
{
CloseThreadpoolWait(value);
}
static constexpr type invalid() noexcept
{
return nullptr;
}
};
using coro_handle = std::coroutine_handle<>;
icmp_ping(HANDLE icmpHandle, HANDLE waitevent, traits::addrtype addr, UCHAR ttl, std::span<std::byte> requestData, std::span<std::byte> replyData) noexcept
:m_reqData(requestData)
, m_replyData(replyData)
, m_waitHandle(waitevent)
, m_handle(icmpHandle)
, m_addr(addr)
, m_ttl(ttl)
{
}
void await_suspend(coro_handle resume_handle)
{
m_resume = resume_handle;
IP_OPTION_INFORMATION stIPInfo = {
.Ttl = m_ttl,
.Flags = IP_FLAG_DF
};
auto local = traits::get_anyaddr();
const auto io_res = traits::pingmethod(
m_handle
, m_waitHandle
, nullptr
, this
, local
, m_addr
, m_reqData.data()
, static_cast<WORD>(m_reqData.size())
, &stIPInfo
, m_replyData.data()
, static_cast<DWORD>(m_replyData.size())
, ECHO_REPLY_TIMEOUT);
if (io_res != ERROR_SUCCESS) [[unlikely]] {
winrt::throw_hresult(HRESULT_FROM_WIN32(io_res));
}
if (const auto err = GetLastError(); err != ERROR_IO_PENDING) [[unlikely]] {
winrt::throw_last_error();
}
//
// Set the timer to fire in 5 seconds.
//
ULARGE_INTEGER ulDueTime{
.QuadPart = static_cast<ULONGLONG>(-(5ll * 10ll * 1000ll * 1000ll))
};
FILETIME FileDueTime{
.dwLowDateTime = ulDueTime.LowPart,
.dwHighDateTime = ulDueTime.HighPart
};
m_tpwait.attach(CreateThreadpoolWait(&WaitCallback, this, nullptr));
if (!m_tpwait) [[unlikely]] {
winrt::throw_last_error();
}
SetThreadpoolWait(m_tpwait.get(), m_waitHandle, &FileDueTime);
}
auto await_resume() const noexcept
{
return traits::parsemethod(m_replyData.data(), static_cast<DWORD>(m_replyData.size()));
}
bool await_ready() const noexcept
{
return false;
}
private:
static void CALLBACK WaitCallback(
PTP_CALLBACK_INSTANCE Instance,
PVOID Context,
PTP_WAIT Wait,
TP_WAIT_RESULT WaitResult
) noexcept {
auto context = static_cast<icmp_ping<traits>*>(Context);
concurrency::create_task([context]() noexcept {
context->m_resume();
}, context->m_context);
}
concurrency::task_continuation_context m_context = concurrency::task_continuation_context::get_current_winrt_context();
coro_handle m_resume{ nullptr };
std::span<std::byte> m_reqData;
std::span<std::byte> m_replyData;
winrt::handle_type<wait_traits> m_tpwait;
HANDLE m_waitHandle;
HANDLE m_handle;
traits::addrtype m_addr;
DWORD m_replysize = 0;
UCHAR m_ttl;
};
export
template<class T, class addrtype = std::remove_pointer_t<std::remove_cvref_t<T>>, class traits = icmp_ping_traits<addrtype>>
inline auto IcmpSendEchoAsync(HANDLE icmpHandle, HANDLE waitevent, T addr, UCHAR ttl, std::span<std::byte> requestData, std::span<std::byte> replyData) noexcept {
return icmp_ping<traits>{icmpHandle, waitevent, traits::to_addr_from_storage(addr), ttl, requestData, replyData};
}
export
template<class T>
[[nodiscard]]
constexpr auto reply_reply_buffer_size(unsigned requestSize) noexcept {
return sizeof(ping_reply_t<T>) + 8 + sizeof(IO_STATUS_BLOCK) + requestSize;
}