Handle duplicate stale arp cache entries better

This commit is contained in:
Michael Zanetti 2019-01-09 13:23:16 +01:00
parent 81b1ce65b9
commit 6076fd0db2

View File

@ -41,7 +41,7 @@ void DeviceMonitor::lookupArpCache()
void DeviceMonitor::ping()
{
// qCDebug(dcNetworkDetector()) << "Running:" << "ping" << "-c" << "1" << m_host->address();
qCDebug(dcNetworkDetector()) << "Sending ARP Ping to" << m_host->hostName() << m_host->macAddress() << m_host->address();
QNetworkInterface targetInterface;
foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) {
foreach (const QNetworkAddressEntry &addressEntry, interface.addressEntries()) {
@ -73,35 +73,39 @@ void DeviceMonitor::arpLookupFinished(int exitCode)
QString data = QString::fromLatin1(m_arpLookupProcess->readAll());
bool found = false;
bool needsPing = true;
foreach (QString line, data.split('\n')) {
line.replace(QRegExp("[ ]{1,}"), " ");
QStringList parts = line.split(" ");
int lladdrIndex = parts.indexOf("lladdr");
if (lladdrIndex >= 0 && parts.count() > lladdrIndex && parts.at(lladdrIndex+1).toLower() == m_host->macAddress().toLower()) {
found = true;
// Verify if IP address is still the same
if (parts.first() != m_host->address()) {
m_host->setAddress(parts.first());
emit addressChanged(parts.first());
}
if (parts.last() == "REACHABLE") {
qCDebug(dcNetworkDetector()) << "Device" << m_host->macAddress() << "found in ARP cache and claims to be REACHABLE";
m_host->seen();
if (!m_host->reachable()) {
m_host->setReachable(true);
emit reachableChanged(true);
}
m_host->seen();
emit seen();
// Verify if IP address is still the same
if (parts.first() != m_host->address()) {
m_host->setAddress(parts.first());
emit addressChanged(parts.first());
}
// If we have a reachable entry, stop processing here
needsPing = false;
break;
} else {
// ARP claims the device to be stale... try to ping it.
qCDebug(dcNetworkDetector()) << "Device" << m_host->macAddress() << "found in ARP cache but is marked as" << parts.last() << ". Trying to ping it on" << m_host->address();
ping();
// ARP claims the device to be stale... Flagging device to require a ping.
qCDebug(dcNetworkDetector()) << "Device" << m_host->macAddress() << "found in ARP cache but is marked as" << parts.last();
}
break;
}
}
if (!found) {
qCDebug(dcNetworkDetector()) << "Device" << m_host->macAddress() << "not found in ARP cache. Trying to ping it on" << m_host->address();
qCDebug(dcNetworkDetector()) << "Device" << m_host->macAddress() << "not found in ARP cache.";
ping();
} else if (needsPing) {
ping();
}
}