removing the assumption that udpip1 fill be updated along with udpip2

This commit is contained in:
maliakal_d 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 <unistd.h>
void Arping::ClearIpsAndInterfaces() { commands.clear(); }
void Arping::AddInterfacesAndIps(const std::string &interface,
void Arping::SetInterfacesAndIps(const int index, const std::string &interface,
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
std::ostringstream os;
os << "arping -c 1 -U -I " << interface << " " << ip;
// to read error messages
os << " 2>&1";
std::string cmd = os.str();
commands.push_back(cmd);
commands[index] = cmd;
}
pid_t Arping::GetThreadId() const { return threadId; }
@ -41,7 +45,7 @@ void Arping::StopThread() {
void Arping::ThreadExecution() {
threadId = syscall(SYS_gettid);
LOG(logINFOBLUE) << "Created [ Arping Thread, Tid: " << threadId << "]";
LOG(logINFOBLUE) << "Created [ Arping Thread, Tid: " << threadId << " ]";
while (runningFlag) {
std::string error = ExecuteCommands();
@ -63,6 +67,12 @@ void Arping::ThreadExecution() {
}
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();
if (!error.empty()) {
throw sls::RuntimeError(error);
@ -71,6 +81,11 @@ void Arping::TestCommands() {
std::string Arping::ExecuteCommands() {
for (auto cmd : commands) {
// empty if 2nd interface not enabled
if (cmd.empty())
continue;
LOG(logDEBUG) << "Executing Arping Command: " << cmd;
// execute command

View File

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

View File

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

View File

@ -192,5 +192,6 @@ class ClientInterface : private virtual slsDetectorDefs {
pid_t parentThreadId{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) {
arping->StopThread();
} else {
arping->ClearIpsAndInterfaces();
arping->AddInterfacesAndIps(eth[0], ips[0]);
if (numUDPInterfaces == 2 && detType != EIGER) {
arping->AddInterfacesAndIps(eth[1], ips[1]);
// setup interface
for (int i = 0; i != numUDPInterfaces; ++i) {
// ignore eiger with 2 interfaces (only udp port)
if (i == 1 && (numUDPInterfaces == 1 || detType == EIGER)) {
break;
}
arping->SetInterfacesAndIps(i, eth[i], ips[i]);
}
arping->StartThread();
}