From bf83c9b3e23bd389ab06a460b62ec79d9cfcd08c Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Tue, 1 Feb 2022 14:03:48 +0100 Subject: [PATCH] arping ip and interface from client interface --- slsReceiverSoftware/src/ClientInterface.cpp | 10 ++++- slsReceiverSoftware/src/ClientInterface.h | 1 + slsReceiverSoftware/src/Implementation.cpp | 12 +++++- slsReceiverSoftware/src/Implementation.h | 3 +- slsReceiverSoftware/src/ThreadArping.cpp | 47 ++++++++++++++++++--- slsReceiverSoftware/src/ThreadArping.h | 6 ++- 6 files changed, 67 insertions(+), 12 deletions(-) diff --git a/slsReceiverSoftware/src/ClientInterface.cpp b/slsReceiverSoftware/src/ClientInterface.cpp index ab4023a5b..4e2cbc12a 100644 --- a/slsReceiverSoftware/src/ClientInterface.cpp +++ b/slsReceiverSoftware/src/ClientInterface.cpp @@ -1399,6 +1399,11 @@ sls::MacAddr ClientInterface::setUdpIp(sls::IpAddr arg) { if (detType == EIGER) { impl()->setEthernetInterface2(eth); } + + // update locally to use for arping + udpips.clear(); + udpips.push_back(arg.str()); + // get mac address auto retval = sls::InterfaceNameToMac(eth); if (retval == 0) { @@ -1431,6 +1436,9 @@ sls::MacAddr ClientInterface::setUdpIp2(sls::IpAddr arg) { } impl()->setEthernetInterface2(eth); + // update locally to use for arping + udpips.push_back(arg.str()); + // get mac address auto retval = sls::InterfaceNameToMac(eth); if (retval == 0) { @@ -1712,6 +1720,6 @@ int ClientInterface::set_arping(Interface &socket) { } verifyIdle(socket); LOG(logDEBUG1) << "Starting/ Killing arping thread:" << value; - impl()->setArping(value); + impl()->setArping(value, udpips); return socket.Send(OK); } diff --git a/slsReceiverSoftware/src/ClientInterface.h b/slsReceiverSoftware/src/ClientInterface.h index fb478cd59..ad7ebc668 100644 --- a/slsReceiverSoftware/src/ClientInterface.h +++ b/slsReceiverSoftware/src/ClientInterface.h @@ -192,4 +192,5 @@ class ClientInterface : private virtual slsDetectorDefs { pid_t parentThreadId{0}; pid_t tcpThreadId{0}; + std::vector udpips; }; diff --git a/slsReceiverSoftware/src/Implementation.cpp b/slsReceiverSoftware/src/Implementation.cpp index 99fe27f3a..3fbb4c0a4 100644 --- a/slsReceiverSoftware/src/Implementation.cpp +++ b/slsReceiverSoftware/src/Implementation.cpp @@ -330,13 +330,21 @@ std::array Implementation::getThreadIds() const { bool Implementation::getArping() const { return threadArping->IsRunning(); } -void Implementation::setArping(const bool i) { +pid_t Implementation::getArpingThreadId() const { + return threadArping->GetThreadId(); +} + +void Implementation::setArping(const bool i, + const std::vector ips) { if (i != threadArping->IsRunning()) { if (!i) { threadArping->StopRunning(); } else { threadArping->ClearIpsAndInterfaces(); - threadArping->AddIpsAndInterfaces(eth[0], "10.0.0.1"); + threadArping->AddIpsAndInterfaces(eth[0], ips[0]); + if (numUDPInterfaces == 2) { + threadArping->AddIpsAndInterfaces(eth[1], ips[1]); + } threadArping->StartRunning(); } } diff --git a/slsReceiverSoftware/src/Implementation.h b/slsReceiverSoftware/src/Implementation.h index e4da56383..df71aa78c 100644 --- a/slsReceiverSoftware/src/Implementation.h +++ b/slsReceiverSoftware/src/Implementation.h @@ -51,7 +51,8 @@ class Implementation : private virtual slsDetectorDefs { void setThreadIds(const pid_t parentTid, const pid_t tcpTid); std::array getThreadIds() const; bool getArping() const; - void setArping(const bool i); + pid_t getArpingThreadId() const; + void setArping(const bool i, const std::vector ips); /************************************************** * * diff --git a/slsReceiverSoftware/src/ThreadArping.cpp b/slsReceiverSoftware/src/ThreadArping.cpp index 674d12e4e..6af8e86ac 100644 --- a/slsReceiverSoftware/src/ThreadArping.cpp +++ b/slsReceiverSoftware/src/ThreadArping.cpp @@ -11,6 +11,8 @@ ThreadArping::ThreadArping() {} ThreadArping::~ThreadArping() { StopRunning(); } +pid_t ThreadArping::GetThreadId() const { return threadId; } + bool ThreadArping::IsRunning() const { return runningFlag; } void ThreadArping::StartRunning() { @@ -33,9 +35,8 @@ void ThreadArping::StartRunning() { void ThreadArping::StopRunning() { pthread_cancel(threadObject); - LOG(logINFOBLUE) << "Killing [ Arping Thread, Tid: " << threadId << "]"; -} -runningFlag = false; + LOG(logINFOBLUE) << "Killing [ Arping Thread, Tid: " << threadId << " ]"; + runningFlag = false; } void ThreadArping::ClearIpsAndInterfaces() { arpInterfaceIp.clear(); } @@ -47,14 +48,48 @@ void ThreadArping::AddIpsAndInterfaces(std::string interface, std::string ip) { void ThreadArping::RunningThread() { threadId = syscall(SYS_gettid); - LOG(logINFOBLUE) << "Created [ Arping Thread, Tid: " << threadId << "]"; + { + std::ostringstream os; + os << "Created [ Arping Thread, Tid: " << threadId << " ] for "; + for (auto ethip : arpInterfaceIp) { + os << "\n\t[ " << ethip.first << ", " << ethip.second << " ]"; + } + LOG(logINFOBLUE) << os.str(); + } + + // create the commands to ping necessary interfaces + std::vector commands; + for (auto ethip : arpInterfaceIp) { + std::ostringstream os; + os << "arping -c 1 -U -I " << ethip.first << " " << ethip.second; + // to read error messages + os << " 2>&1"; + std::string cmd = os.str(); + commands.push_back(cmd); + } while (IsRunning()) { - LOG(logINFOBLUE) << "Going to sleep"; + + // arping + for (auto cmd : commands) { + LOG(logDEBUG) << "Executing Arping Command: " << cmd; + + // execute command and check for errors + FILE *sysFile = popen(cmd.c_str(), "r"); + char output[MAX_STR_LENGTH] = {0}; + fgets(output, sizeof(output), sysFile); + output[sizeof(output) - 1] = '\0'; + if (pclose(sysFile)) { + LOG(logERROR) << "Executing cmd[" << cmd + << "]\n\tError Message : " << output; + } else { + LOG(logDEBUG) << output; + } + } // wait for 60s usleep(60 * 1000 * 1000); } - LOG(logINFOBLUE) << "Exiting [ Arping Thread, Tid: " << threadId << "]"; + LOG(logINFOBLUE) << "Exiting [ Arping Thread, Tid: " << threadId << " ]"; } \ No newline at end of file diff --git a/slsReceiverSoftware/src/ThreadArping.h b/slsReceiverSoftware/src/ThreadArping.h index f9595a725..9f41041f9 100644 --- a/slsReceiverSoftware/src/ThreadArping.h +++ b/slsReceiverSoftware/src/ThreadArping.h @@ -22,11 +22,13 @@ class ThreadArping : private virtual slsDetectorDefs { pthread_t threadObject; std::vector> arpInterfaceIp; - pid_t threadIds; + std::vector commands; + pid_t threadId; public: ThreadArping(); virtual ~ThreadArping(); + pid_t GetThreadId() const; bool IsRunning() const; void StartRunning(); void StopRunning(); @@ -39,5 +41,5 @@ class ThreadArping : private virtual slsDetectorDefs { * RunningMask is satisfied Then it exits the thread on its own if * killThread is true */ - void RunningThread(std::string interface, std::string ip); + void RunningThread(); };