forked from leeter/WinMTR-refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinMTRSNetHost.ixx
More file actions
436 lines (403 loc) · 12.1 KB
/
Copy pathWinMTRSNetHost.ixx
File metadata and controls
436 lines (403 loc) · 12.1 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
WinMTR
Copyright (C) 2010-2019 Appnor MSP S.A. - http://www.appnor.com
Copyright (C) 2019-2022 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 "targetver.h"
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#define NOMCX
#define NOIME
#define NOGDI
#define NONLS
#define NOAPISET
#define NOSERVICE
#define NOMINMAX
#include <WinSock2.h>
#include <ws2ipdef.h>
export module WinMTRSNetHost;
import WinMTRIPUtils;
import WinMTRUtils;
import <algorithm>;
import <string>;
import <vector>;
import <cstddef>;
import <cstdint>;
import <cmath>;
import <cstring>;
import <utility>;
export [[nodiscard]]
inline bool same_network_address(const SOCKADDR_INET& lhs, const SOCKADDR_INET& rhs) noexcept
{
if (lhs.si_family != rhs.si_family) {
return false;
}
if (lhs.si_family == AF_INET) {
return lhs.Ipv4.sin_addr.S_un.S_addr == rhs.Ipv4.sin_addr.S_un.S_addr;
}
if (lhs.si_family == AF_INET6) {
return lhs.Ipv6.sin6_scope_id == rhs.Ipv6.sin6_scope_id
&& std::memcmp(&lhs.Ipv6.sin6_addr, &rhs.Ipv6.sin6_addr, sizeof(IN6_ADDR)) == 0;
}
return false;
}
export struct s_netresponder final {
SOCKADDR_INET addr = {};
std::uint64_t stable_id = 0;
std::wstring name;
std::wstring country;
std::wstring asn;
std::wstring isp;
std::wstring metadata_source;
std::wstring metadata_failure_reason;
std::uint64_t last_seen_sequence = 0;
std::uint64_t last_reply_tick = 0;
std::uint64_t hit_count = 0;
std::uint64_t total_ms = 0;
unsigned last_ms = 0;
unsigned best_ms = 0;
unsigned worst_ms = 0;
double mean_ms = 0.0;
double m2_ms = 0.0;
double stddev_ms = 0.0;
double jitter_ms = 0.0;
double recent_jitter_ms = 0.0;
double previous_reply_ms = 0.0;
bool has_previous_reply = false;
[[nodiscard]]
std::wstring getName() const
{
return name.empty() ? addr_to_string(addr) : name;
}
[[nodiscard]] double getAverageMs() const noexcept
{
return hit_count == 0 ? 0.0 : mean_ms;
}
void noteReply(unsigned round_trip_ms, std::uint64_t sequence,
std::uint64_t tick) noexcept
{
last_seen_sequence = sequence;
last_reply_tick = tick;
++hit_count;
total_ms += round_trip_ms;
last_ms = round_trip_ms;
if (hit_count == 1 || round_trip_ms < best_ms) best_ms = round_trip_ms;
if (hit_count == 1 || round_trip_ms > worst_ms) worst_ms = round_trip_ms;
const auto sample = static_cast<double>(round_trip_ms);
const auto delta = sample - mean_ms;
mean_ms += delta / static_cast<double>(hit_count);
m2_ms += delta * (sample - mean_ms);
stddev_ms = hit_count > 1
? std::sqrt(m2_ms / static_cast<double>(hit_count - 1u))
: 0.0;
if (has_previous_reply) {
recent_jitter_ms = std::abs(sample - previous_reply_ms);
jitter_ms += (recent_jitter_ms - jitter_ms) / 16.0;
}
previous_reply_ms = sample;
has_previous_reply = true;
}
};
export enum class WinMTRProbeOutcome : std::uint8_t {
none,
in_flight,
echo_reply,
ttl_expired,
destination_unreachable,
packet_too_big,
icmp_error,
timeout,
local_error,
cancelled,
scheduler_skipped,
cached,
late_discarded,
post_destination_discarded,
};
export struct s_nethost final {
SOCKADDR_INET addr = {};
std::wstring name;
std::wstring country;
std::wstring asn;
std::wstring isp;
std::wstring metadata_source;
std::wstring metadata_failure_reason;
std::uint64_t xmit = 0; // probes accepted by the transport (Sent column)
std::uint64_t completed = 0; // received + timed_out; excludes in-flight/local failures
std::uint64_t returned = 0; // completed probes with a usable ICMP reply
std::uint64_t timed_out = 0; // logical network deadlines reached
std::uint64_t in_flight = 0; // issued but not logically completed
std::uint64_t local_errors = 0;
std::uint64_t cancelled = 0;
std::uint64_t scheduler_skipped = 0;
std::uint64_t cache_skipped = 0;
std::uint64_t late_completions = 0;
std::uint64_t post_destination_completions = 0;
std::uint64_t scheduler_late_slots = 0;
std::uint64_t scheduler_lateness_total_ms = 0;
std::uint64_t scheduler_lateness_max_ms = 0;
std::uint64_t total = 0; // total round-trip time in milliseconds
int last = 0; // last time
int best = 0; // best time
int worst = 0; // worst time
double jitter = 0.0;
double stddev = 0.0;
unsigned hop = 0;
std::uint64_t last_reply_tick = 0;
std::uint64_t last_reply_cycle = 0;
std::uint64_t last_destination_reply_tick = 0;
std::vector<s_netresponder> responders;
// Welford state is intentionally carried in snapshots as harmless data;
// exporters should consume getAverageMs()/stddev rather than these fields.
double mean_ms = 0.0;
double m2_ms = 0.0;
double previous_reply_ms = 0.0;
double recent_jitter_ms = 0.0;
bool has_previous_reply = false;
WinMTRProbeOutcome last_outcome = WinMTRProbeOutcome::none;
std::uint32_t last_error_code = 0;
[[nodiscard]]
inline int getPercent() const noexcept {
if (completed == 0) {
return 0;
}
return static_cast<int>(std::lround(100.0 * static_cast<double>(timed_out)
/ static_cast<double>(completed)));
}
[[nodiscard]]
inline int getAvg() const noexcept {
return returned == 0 ? 0 : static_cast<int>(std::lround(mean_ms));
}
[[nodiscard]]
inline double getLossPercent() const noexcept {
return completed == 0 ? 0.0 : 100.0 * static_cast<double>(timed_out)
/ static_cast<double>(completed);
}
[[nodiscard]]
inline double getAverageMs() const noexcept {
return returned == 0 ? 0.0 : mean_ms;
}
[[nodiscard]]
auto getName() const -> std::wstring {
if (name.empty()) {
return addr_to_string(addr);
}
return name;
}
void reset(unsigned hop_number = 0) noexcept
{
*this = s_nethost{};
hop = hop_number;
}
void noteIssued(std::uint64_t scheduler_lateness_ms) noexcept
{
++xmit;
++in_flight;
last_outcome = WinMTRProbeOutcome::in_flight;
last_error_code = 0;
if (scheduler_lateness_ms != 0) {
++scheduler_late_slots;
scheduler_lateness_total_ms += scheduler_lateness_ms;
scheduler_lateness_max_ms = std::max(scheduler_lateness_max_ms,
scheduler_lateness_ms);
}
}
void noteTimeout() noexcept
{
++completed;
++timed_out;
if (in_flight != 0) --in_flight;
last_outcome = WinMTRProbeOutcome::timeout;
last_error_code = 0;
}
void noteReply(unsigned round_trip_ms, std::uint64_t cycle, std::uint64_t tick,
WinMTRProbeOutcome outcome, std::uint32_t status_code) noexcept
{
++completed;
++returned;
if (in_flight != 0) --in_flight;
last = static_cast<int>(round_trip_ms);
total += round_trip_ms;
if (returned == 1 || last < best) {
best = last;
}
if (returned == 1 || last > worst) {
worst = last;
}
const auto sample = static_cast<double>(round_trip_ms);
const auto delta = sample - mean_ms;
mean_ms += delta / static_cast<double>(returned);
const auto delta_after_mean = sample - mean_ms;
m2_ms += delta * delta_after_mean;
stddev = returned > 1
? std::sqrt(m2_ms / static_cast<double>(returned - 1u))
: 0.0;
if (has_previous_reply) {
recent_jitter_ms = std::abs(sample - previous_reply_ms);
jitter += (recent_jitter_ms - jitter) / 16.0;
}
previous_reply_ms = sample;
has_previous_reply = true;
last_reply_tick = tick;
last_reply_cycle = cycle;
last_outcome = outcome;
last_error_code = status_code;
}
void noteLocalError(bool was_issued, std::uint32_t error_code) noexcept
{
++local_errors;
if (was_issued && in_flight != 0) --in_flight;
last_outcome = WinMTRProbeOutcome::local_error;
last_error_code = error_code;
}
void noteCancelled() noexcept
{
++cancelled;
if (in_flight != 0) --in_flight;
last_outcome = WinMTRProbeOutcome::cancelled;
last_error_code = ERROR_CANCELLED;
}
void noteSchedulerSkipped() noexcept
{
++scheduler_skipped;
last_outcome = WinMTRProbeOutcome::scheduler_skipped;
last_error_code = 0;
}
void noteCacheSkipped() noexcept
{
++cache_skipped;
last_outcome = WinMTRProbeOutcome::cached;
last_error_code = 0;
}
void noteLateCompletion() noexcept
{
++late_completions;
last_outcome = WinMTRProbeOutcome::late_discarded;
}
void notePostDestinationCompletion() noexcept
{
if (in_flight != 0) --in_flight;
++post_destination_completions;
last_outcome = WinMTRProbeOutcome::post_destination_discarded;
last_error_code = 0;
}
[[nodiscard]]
s_netresponder& observeResponder(const SOCKADDR_INET& responder_address,
unsigned round_trip_ms, std::uint64_t sequence, std::uint64_t tick)
{
auto found = responders.begin();
for (; found != responders.end(); ++found) {
if (same_network_address(found->addr, responder_address)) {
break;
}
}
if (found == responders.end()) {
if (responders.size() >= WinMTRUtils::MAX_ECMP_RESPONDERS) {
const auto oldest = std::min_element(responders.begin(), responders.end(),
[](const auto& lhs, const auto& rhs) {
return lhs.last_seen_sequence < rhs.last_seen_sequence;
});
responders.erase(oldest);
}
responders.insert(responders.begin(), s_netresponder{
.addr = responder_address,
.stable_id = stableResponderId(responder_address),
});
}
else if (found != responders.begin()) {
auto current = std::move(*found);
responders.erase(found);
responders.insert(responders.begin(), std::move(current));
}
auto& primary = responders.front();
primary.noteReply(round_trip_ms, sequence, tick);
addr = primary.addr;
name = primary.name;
country = primary.country;
asn = primary.asn;
isp = primary.isp;
metadata_source = primary.metadata_source;
metadata_failure_reason = primary.metadata_failure_reason;
return primary;
}
private:
[[nodiscard]] static std::uint64_t stableResponderId(
const SOCKADDR_INET& address) noexcept
{
constexpr std::uint64_t offset = 14695981039346656037ull;
constexpr std::uint64_t prime = 1099511628211ull;
std::uint64_t hash = offset;
const auto mix = [&hash](const void* data, std::size_t size) noexcept {
const auto* bytes = static_cast<const unsigned char*>(data);
for (std::size_t index = 0; index < size; ++index) {
hash ^= bytes[index];
hash *= prime;
}
};
mix(&address.si_family, sizeof(address.si_family));
if (address.si_family == AF_INET) {
mix(&address.Ipv4.sin_addr, sizeof(address.Ipv4.sin_addr));
}
else if (address.si_family == AF_INET6) {
mix(&address.Ipv6.sin6_addr, sizeof(address.Ipv6.sin6_addr));
mix(&address.Ipv6.sin6_scope_id, sizeof(address.Ipv6.sin6_scope_id));
}
return hash == 0 ? 1 : hash;
}
public:
bool updateResponder(const SOCKADDR_INET& responder_address,
const std::wstring& responder_name,
const std::wstring& responder_country = {},
const std::wstring& responder_asn = {},
const std::wstring& responder_isp = {},
const std::wstring& responder_source = {},
const std::wstring& responder_failure_reason = {})
{
for (auto& responder : responders) {
if (!same_network_address(responder.addr, responder_address)) {
continue;
}
if (!responder_name.empty()) {
responder.name = responder_name;
}
if (!responder_country.empty()) {
responder.country = responder_country;
}
if (!responder_asn.empty()) {
responder.asn = responder_asn;
}
if (!responder_isp.empty()) {
responder.isp = responder_isp;
}
if (!responder_source.empty()) {
responder.metadata_source = responder_source;
}
if (!responder_failure_reason.empty()) {
responder.metadata_failure_reason = responder_failure_reason;
}
if (&responder == &responders.front()) {
name = responder.name;
country = responder.country;
asn = responder.asn;
isp = responder.isp;
metadata_source = responder.metadata_source;
metadata_failure_reason = responder.metadata_failure_reason;
}
return true;
}
return false;
}
};