4#include "Stroika/Foundation/StroikaPreComp.h"
9#include "Stroika/Foundation/Characters/String2Int.h"
11#include "Stroika/Foundation/Execution/Exceptions.h"
12#include "Stroika/Foundation/Execution/ProcessRunner.h"
15#include "Stroika/Foundation/Streams/MemoryStream.h"
26using namespace Stroika::Foundation::Memory;
27using namespace Stroika::Foundation::IO;
29using namespace Stroika::Foundation::Streams;
42#if USE_NOISY_TRACE_IN_THIS_MODULE_
45#if qStroika_Foundation_Common_Platform_Windows
50#if qStroika_Foundation_Common_Platform_POSIX
52#elif qStroika_Foundation_Common_Platform_Windows
53 ProcessRunner pr{includePurgedEntries ?
"arp -av"sv :
"arp -a"sv};
56 pr.Run (
nullptr, useStdOut);
58#if qStroika_Foundation_Common_Platform_Windows
63#if qStroika_Foundation_Common_Platform_POSIX
78 if (s[1].StartsWith (
"("sv) and s[1].EndsWith (
")"sv)) {
79 if (not includePurgedEntries and s[3].Contains (
"incomplete"sv)) {
84 if (l >= 6 and s[l - 2] ==
"on"sv) {
85 interfaceID = s[l - 1];
90#elif qStroika_Foundation_Common_Platform_Windows
91 if (i.StartsWith (
"Interface:"sv)) {
96 curInterface = iface->fInternalInterfaceID;
104 if (i.StartsWith (
" "sv) and not curInterface.empty ()) {
106 if (s.
length () >= 3 and (s[2] ==
"static"sv or s[2] ==
"dynamic"sv)) {
109 static const String kDash_ =
"-"sv;
110 static const String kColon_ =
":"sv;
111 if (omitAllFFHardwareAddresses) {
112 static const String kFFFF_ =
"ff-ff-ff-ff-ff-ff"sv;
113 if (s[1] == kFFFF_) {
123#if USE_NOISY_TRACE_IN_THIS_MODULE_
124 DbgTrace (
"returning: {}"_f, result);
130#if qStroika_Foundation_Common_Platform_Linux
134#if USE_NOISY_TRACE_IN_THIS_MODULE_
138 using Characters::String2Int;
140 static const filesystem::path kProcFileName_{
"/proc/net/arp"sv};
148 bool readFirstLine =
false;
151 reader.ReadMatrix (IO::FileSystem::FileInputStream::New (kProcFileName_, IO::FileSystem::FileInputStream::eNotSeekable))) {
152#if USE_NOISY_TRACE_IN_THIS_MODULE_
153 DbgTrace (
"in ProcNetArp_ capture_ line={}"_f, line);
155 if (not readFirstLine) {
156 readFirstLine =
true;
159 if (line[2] ==
"0x0"sv) {
164#if USE_NOISY_TRACE_IN_THIS_MODULE_
165 DbgTrace (
"returning: {}"_f, result);
181 sb <<
"InternetAddress:"sv << fInternetAddress <<
","sv;
182 sb <<
"HardwareAddress:"sv << fHardwareAddress <<
","sv;
183 sb <<
"InterfaceID:"sv << fInterfaceID;
193class NeighborsMonitor::Rep_ {
195 Rep_ (
const Options& o)
198 Require (not o.fMonitor.has_value ());
202 std::exception_ptr e =
nullptr;
205 if (not fOptions_.fStategies or fOptions_.fStategies->Contains (s)) {
207 return GetNeighbors_ (s);
211 e = current_exception ();
218#if qStroika_Foundation_Common_Platform_Linux
219 if (
auto o = tryStrategy (Options::Strategy::eProcNetArp)) {
223 if (
auto o = tryStrategy (Options::Strategy::eArpProgram)) {
227 rethrow_exception (e);
229 Throw (
Exception{
"No matching / available neighbors strategy"sv});
233#if USE_NOISY_TRACE_IN_THIS_MODULE_
237 case Options::Strategy::eArpProgram:
238 return ArpDashA_ (fOptions_.fIncludePurgedEntries.value_or (
false), fOptions_.fOmitAllFFHardwareAddresses.value_or (
true));
239#if qStroika_Foundation_Common_Platform_Linux
240 case Options::Strategy::eProcNetArp:
242 return ProcNetArp_ (fOptions_.fIncludePurgedEntries.value_or (
false));
252NeighborsMonitor::NeighborsMonitor (
const Options& options)
253 : fRep_{make_shared<Rep_> (options)}
259 return fRep_->GetNeighbors ();
#define WeakAssert(c)
A WeakAssert() is for things that aren't guaranteed to be true, but are overwhelmingly likely to be t...
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
A Collection<T> is a container to manage an un-ordered collection of items, without equality defined ...
A generalization of a vector: a container whose elements are keyed by the natural numbers.
This COULD be easily used to read CSV files, or tab-delimited files, for example.
Exception<> is a replacement (subclass) for any std c++ exception class (e.g. the default 'std::excep...
Run the given command, and optionally support stdin/stdout/stderr as streams (either sync with Run,...
nonvirtual optional< Interface > GetContainingAddress(const InternetAddress &ia)
nonvirtual size_t length() const
STL-ish alias for size() - really in STL only used in string, I think, but still makes sense as an al...
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Ptr New(const InputStream::Ptr< byte > &src, optional< AutomaticCodeCvtFlags > codeCvtFlags={}, optional< SeekableFlag > seekable={}, ReadAhead readAhead=eReadAheadAllowed)
Create an InputStream::Ptr<Character> from the arguments (usually binary source) - which can be used ...
nonvirtual String ToString() const