removing the assumption that udpip1 fill be updated along with udpip2

This commit is contained in:
2022-02-03 12:53:07 +01:00
parent 47c6954044
commit 97417737b6
5 changed files with 35 additions and 17 deletions

View File

@@ -7,17 +7,21 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
void Arping::ClearIpsAndInterfaces() { commands.clear(); } void Arping::SetInterfacesAndIps(const int index, const std::string &interface,
void Arping::AddInterfacesAndIps(const std::string &interface,
const std::string &ip) { const std::string &ip) {
if (interface.empty() || ip.empty()) {
throw sls::RuntimeError("Could not arping. Interface name and ip not "
"set up for interface " +
std::to_string(index));
}
// create commands to arping // create commands to arping
std::ostringstream os; std::ostringstream os;
os << "arping -c 1 -U -I " << interface << " " << ip; os << "arping -c 1 -U -I " << interface << " " << ip;
// to read error messages // to read error messages
os << " 2>&1"; os << " 2>&1";
std::string cmd = os.str(); std::string cmd = os.str();
commands.push_back(cmd); commands[index] = cmd;
} }
pid_t Arping::GetThreadId() const { return threadId; } pid_t Arping::GetThreadId() const { return threadId; }
@@ -41,7 +45,7 @@ void Arping::StopThread() {
void Arping::ThreadExecution() { void Arping::ThreadExecution() {
threadId = syscall(SYS_gettid); threadId = syscall(SYS_gettid);
LOG(logINFOBLUE) << "Created [ Arping Thread, Tid: " << threadId << "]"; LOG(logINFOBLUE) << "Created [ Arping Thread, Tid: " << threadId << " ]";
while (runningFlag) { while (runningFlag) {
std::string error = ExecuteCommands(); std::string error = ExecuteCommands();
@@ -63,6 +67,12 @@ void Arping::ThreadExecution() {
} }
void Arping::TestCommands() { void Arping::TestCommands() {
// atleast one interface must be set up
if (commands[0].empty()) {
throw sls::RuntimeError(
"Could not arping. Interface not set up in apring thread");
}
// test if arping commands throw an error
std::string error = ExecuteCommands(); std::string error = ExecuteCommands();
if (!error.empty()) { if (!error.empty()) {
throw sls::RuntimeError(error); throw sls::RuntimeError(error);
@@ -71,6 +81,11 @@ void Arping::TestCommands() {
std::string Arping::ExecuteCommands() { std::string Arping::ExecuteCommands() {
for (auto cmd : commands) { for (auto cmd : commands) {
// empty if 2nd interface not enabled
if (cmd.empty())
continue;
LOG(logDEBUG) << "Executing Arping Command: " << cmd; LOG(logDEBUG) << "Executing Arping Command: " << cmd;
// execute command // execute command

View File

@@ -6,8 +6,8 @@
is listening to. is listening to.
*/ */
#include "receiver_defs.h"
#include "sls/logger.h" #include "sls/logger.h"
#include "sls/sls_detector_defs.h"
#include <atomic> #include <atomic>
#include <thread> #include <thread>
@@ -15,8 +15,7 @@ is listening to.
class Arping : private virtual slsDetectorDefs { class Arping : private virtual slsDetectorDefs {
public: public:
void ClearIpsAndInterfaces(); void SetInterfacesAndIps(const int index, const std::string &interface,
void AddInterfacesAndIps(const std::string &interface,
const std::string &ip); const std::string &ip);
pid_t GetThreadId() const; pid_t GetThreadId() const;
bool IsRunning() const; bool IsRunning() const;
@@ -28,7 +27,8 @@ class Arping : private virtual slsDetectorDefs {
std::string ExecuteCommands(); std::string ExecuteCommands();
void ThreadExecution(); void ThreadExecution();
std::vector<std::string> commands; std::vector<std::string> commands =
std::vector<std::string>(MAX_NUMBER_OF_LISTENING_THREADS);
std::atomic<bool> runningFlag{false}; std::atomic<bool> runningFlag{false};
std::thread t; std::thread t;
pid_t threadId{0}; pid_t threadId{0};

View File

@@ -1401,8 +1401,7 @@ sls::MacAddr ClientInterface::setUdpIp(sls::IpAddr arg) {
} }
// update locally to use for arping // update locally to use for arping
udpips.clear(); udpips[0] = arg.str();
udpips.push_back(arg.str());
// get mac address // get mac address
auto retval = sls::InterfaceNameToMac(eth); auto retval = sls::InterfaceNameToMac(eth);
@@ -1437,7 +1436,7 @@ sls::MacAddr ClientInterface::setUdpIp2(sls::IpAddr arg) {
impl()->setEthernetInterface2(eth); impl()->setEthernetInterface2(eth);
// update locally to use for arping // update locally to use for arping
udpips.push_back(arg.str()); udpips[1] = arg.str();
// get mac address // get mac address
auto retval = sls::InterfaceNameToMac(eth); auto retval = sls::InterfaceNameToMac(eth);

View File

@@ -192,5 +192,6 @@ class ClientInterface : private virtual slsDetectorDefs {
pid_t parentThreadId{0}; pid_t parentThreadId{0};
pid_t tcpThreadId{0}; pid_t tcpThreadId{0};
std::vector<std::string> udpips; std::vector<std::string> udpips =
std::vector<std::string>(MAX_NUMBER_OF_LISTENING_THREADS);
}; };

View File

@@ -341,10 +341,13 @@ void Implementation::setArping(const bool i,
if (!i) { if (!i) {
arping->StopThread(); arping->StopThread();
} else { } else {
arping->ClearIpsAndInterfaces(); // setup interface
arping->AddInterfacesAndIps(eth[0], ips[0]); for (int i = 0; i != numUDPInterfaces; ++i) {
if (numUDPInterfaces == 2 && detType != EIGER) { // ignore eiger with 2 interfaces (only udp port)
arping->AddInterfacesAndIps(eth[1], ips[1]); if (i == 1 && (numUDPInterfaces == 1 || detType == EIGER)) {
break;
}
arping->SetInterfacesAndIps(i, eth[i], ips[i]);
} }
arping->StartThread(); arping->StartThread();
} }