Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Ping.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Frameworks_NetworkMonitor_Ping_h_
5#define _Stroika_Frameworks_NetworkMonitor_Ping_h_ 1
6
7#include "Stroika/Frameworks/StroikaPreComp.h"
8
9#include <limits>
10#include <optional>
11#include <random>
12
14#include "Stroika/Foundation/IO/Network/ConnectionlessSocket.h"
16#include "Stroika/Foundation/IO/Network/InternetProtocol/ICMP.h"
17#include "Stroika/Foundation/IO/Network/InternetProtocol/IP.h"
20
21/**
22 * \file
23 *
24 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
25 *
26 */
27
28namespace Stroika::Frameworks::NetworkMonitor::Ping {
29
30 using namespace Stroika::Foundation;
31
33 using Time::Duration;
34
35 /**
36 */
37 struct Options {
38 /**
39 */
40 static constexpr unsigned int kDefaultMaxHops = 64;
41
42 /**
43 */
44 optional<unsigned int> fMaxHops;
45
46 /**
47 */
48 static const inline Duration kDefaultTimeout{3.0s};
49
50 /**
51 * time after a single ping is sent before we treat the ping as having timed out (so not total time if multiple samples taken).
52 */
53 optional<Duration> fTimeout;
54
55 /*
56 * No standard for this, but just what this library does.
57 */
58 static constexpr size_t kDefaultPayloadSize = 32;
59
60 /**
61 * The range of supported payload (not including ICMP and IP packet headers)
62 *
63 * @see http://stackoverflow.com/questions/9449837/maximum-legal-size-of-icmp-echo-packet
64 *
65 * This does NOT include the IP header, nor the ICMP Header
66 */
67 static inline constexpr Traversal::Range<size_t> kAllowedICMPPayloadSizeRange{
68 0,
69 numeric_limits<uint16_t>::max () -
70 (sizeof (IO::Network::InternetProtocol::ICMP::V4::PacketHeader) + sizeof (IO::Network::InternetProtocol::IP::V4::PacketHeader)),
71 Traversal::Openness::eClosed, Traversal::Openness::eClosed};
72
73 /**
74 * \not including ICMP nor IP header overhead.
75 */
76 optional<size_t> fPacketPayloadSize;
77
78 /**
79 * @see Characters::ToString ();
80 */
81 nonvirtual Characters::String ToString () const;
82 };
83
84 /**
85 *
86 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety">C++-Standard-Thread-Safety</a>
87 */
88 class Pinger {
89 public:
90 Pinger () = delete;
91 Pinger (const Pinger&) = delete;
92 Pinger (const InternetAddress& addr, const Options& options = {});
93
94 public:
95 struct ResultType;
96
97 public:
98 /**
99 * \brief run the Ping () operation one time, and return the (timing) results.
100 *
101 * \note - the argument 'ttl' is really a number of hops (perhaps should change name). But
102 * its typically referred to as a TTL because its implemented with a TTL field in the IP
103 * header
104 *
105 * Can throw:
106 * TimeoutException
107 * InternetProtocol::ICMP::DestinationUnreachableException
108 * InternetProtocol::ICMP::UnknownICMPPacket
109 * InternetProtocol::ICMP::TTLExpiredException
110 * others...
111 */
112 nonvirtual ResultType RunOnce (const optional<unsigned int>& ttl = {});
113
114 private:
115 nonvirtual ResultType RunOnce_ICMP_ (unsigned int ttl);
116
117 private:
118 std::uniform_int_distribution<std::mt19937::result_type> fAllUInt16Distribution_{0, numeric_limits<uint16_t>::max ()};
119 std::mt19937 fRng_{std::random_device{}()};
120 InternetAddress fDestination_;
121 Options fOptions_;
122 size_t fICMPPacketSize_;
123 Memory::InlineBuffer<byte> fSendPacket_;
125 uint16_t fNextSequenceNumber_;
126 Time::DurationSeconds fPingTimeout_;
127 };
128
129 /**
130 */
131 struct Pinger::ResultType {
132 Duration fPingTime;
133 unsigned int fHopCount{};
134
135 /**
136 * @see Characters::ToString ();
137 */
138 nonvirtual Characters::String ToString () const;
139 };
140
141 /**
142 */
143 struct SampleOptions {
144 Duration fInterval;
145 unsigned int fSampleCount{};
146
147 /**
148 * @see Characters::ToString ();
149 */
150 nonvirtual Characters::String ToString () const;
151 };
152
153 /**
154 */
155 struct SampleResults {
156 optional<Duration> fMedianPingTime; // excludes timeouts
157 optional<unsigned int> fMedianHopCount;
158 unsigned int fExceptionCount{};
159
160 /**
161 * @see Characters::ToString ();
162 */
163 nonvirtual Characters::String ToString () const;
164 };
165
166 /**
167 * \brief Send network packets to the argument internet address (govered by sample Options and optins) - and return the sampled results (e.g. averages)
168 *
169 * 'options' govern how the pinging is done.
170 *
171 * 'sampleOptions' govern how the results are statistically processed.
172 *
173 * Sample () will summarize most exceptions in Results.
174 */
175 SampleResults Sample (const InternetAddress& addr, const SampleOptions& sampleOptions = {}, const Options& options = {});
176
177}
178
179/*
180 ********************************************************************************
181 ***************************** Implementation Details ***************************
182 ********************************************************************************
183 */
184#include "Ping.inl"
185
186#endif /*_Stroika_Frameworks_NetworkMonitor_Ping_h_*/
SampleResults Sample(const InternetAddress &addr, const SampleOptions &sampleOptions={}, const Options &options={})
Send network packets to the argument internet address (govered by sample Options and optins) - and re...
Definition Ping.cpp:254
chrono::duration< double > DurationSeconds
chrono::duration<double> - a time span (length of time) measured in seconds, but high precision.
Definition Realtime.h:57
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
Logically halfway between std::array and std::vector; Smart 'direct memory array' - which when needed...
Duration is a chrono::duration<double> (=.
Definition Duration.h:96
nonvirtual ResultType RunOnce(const optional< unsigned int > &ttl={})
run the Ping () operation one time, and return the (timing) results.
Definition Ping.cpp:98
STRING_TYPE ToString(FLOAT_TYPE f, const ToStringOptions &options={})