use arping instead of ping

This commit is contained in:
Michael Zanetti 2018-12-30 23:38:55 +01:00
parent 9a3e32cced
commit c5c760f4a7
3 changed files with 27 additions and 19 deletions

View File

@ -2,6 +2,8 @@
#include "extern-plugininfo.h" #include "extern-plugininfo.h"
#include <QNetworkInterface>
DeviceMonitor::DeviceMonitor(const QString &macAddress, const QString &ipAddress, QObject *parent): DeviceMonitor::DeviceMonitor(const QString &macAddress, const QString &ipAddress, QObject *parent):
QObject(parent) QObject(parent)
{ {
@ -36,13 +38,27 @@ void DeviceMonitor::lookupArpCache()
void DeviceMonitor::ping() void DeviceMonitor::ping()
{ {
// qCDebug(dcNetworkDetector()) << "Running:" << "ping" << "-c" << "1" << m_host->address(); // qCDebug(dcNetworkDetector()) << "Running:" << "ping" << "-c" << "1" << m_host->address();
m_pingProcess->start("ping", {"-c", "1", m_host->address()}); QNetworkInterface targetInterface;
foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) {
foreach (const QNetworkAddressEntry &addressEntry, interface.addressEntries()) {
QHostAddress clientAddress(m_host->address());
if (clientAddress.isInSubnet(addressEntry.ip(), addressEntry.prefixLength())) {
targetInterface = interface;
break;
}
}
}
if (!targetInterface.isValid()) {
qCWarning(dcNetworkDetector()) << "Could not find a suitable interface to ping for" << m_host->address();
return;
}
m_pingProcess->start("arping", {"-I", targetInterface.name(), "-f", "-w", "5", m_host->address()});
} }
void DeviceMonitor::arpLookupFinished(int exitCode) void DeviceMonitor::arpLookupFinished(int exitCode)
{ {
if (exitCode != 0) { if (exitCode != 0) {
qWarning(dcNetworkDetector()) << "Error looking up ARP cache."; qCWarning(dcNetworkDetector()) << "Error looking up ARP cache.";
return; return;
} }
@ -60,7 +76,7 @@ void DeviceMonitor::arpLookupFinished(int exitCode)
emit addressChanged(parts.first()); emit addressChanged(parts.first());
} }
if (parts.last() == "REACHABLE") { if (parts.last() == "REACHABLE") {
qDebug(dcNetworkDetector()) << "Device" << m_host->macAddress() << "found in ARP cache and claims to be REACHABLE"; qCDebug(dcNetworkDetector()) << "Device" << m_host->macAddress() << "found in ARP cache and claims to be REACHABLE";
m_host->seen(); m_host->seen();
if (!m_host->reachable()) { if (!m_host->reachable()) {
m_host->setReachable(true); m_host->setReachable(true);
@ -80,8 +96,8 @@ void DeviceMonitor::arpLookupFinished(int exitCode)
ping(); ping();
} }
if (m_host->reachable() && m_host->lastSeenTime().addSecs(300) < QDateTime::currentDateTime()) { if (m_host->reachable() && m_host->lastSeenTime().addSecs(20) < QDateTime::currentDateTime()) {
qCDebug(dcNetworkDetector()) << "Could not reach device for > 5 mins. Marking it as gone." << m_host->address() << m_host->macAddress(); qCDebug(dcNetworkDetector()) << "Could not reach device for 20 seconds. Marking it as gone." << m_host->address() << m_host->macAddress();
m_host->setReachable(false); m_host->setReachable(false);
emit reachableChanged(false); emit reachableChanged(false);
} }
@ -91,6 +107,7 @@ void DeviceMonitor::pingFinished(int exitCode)
{ {
if (exitCode == 0) { if (exitCode == 0) {
// we were able to ping the device // we were able to ping the device
qCDebug(dcNetworkDetector()) << "Ping successful for" << m_host->macAddress() << m_host->address();
m_host->seen(); m_host->seen();
if (!m_host->reachable()) { if (!m_host->reachable()) {
m_host->setReachable(true); m_host->setReachable(true);
@ -98,7 +115,7 @@ void DeviceMonitor::pingFinished(int exitCode)
} }
emit seen(); emit seen();
} else { } else {
qDebug(dcNetworkDetector()) << "Could not ping device" << m_host->macAddress() << m_host->address(); qCDebug(dcNetworkDetector()) << "Could not ping device" << m_host->macAddress() << m_host->address();
} }
// read data to discard it from socket // read data to discard it from socket
QString data = QString::fromLatin1(m_pingProcess->readAll()); QString data = QString::fromLatin1(m_pingProcess->readAll());

View File

@ -142,9 +142,8 @@ void DevicePluginNetworkDetector::deviceReachableChanged(bool reachable)
{ {
DeviceMonitor *monitor = static_cast<DeviceMonitor*>(sender()); DeviceMonitor *monitor = static_cast<DeviceMonitor*>(sender());
Device *device = m_monitors.value(monitor); Device *device = m_monitors.value(monitor);
if (device->stateValue(networkDeviceConnectedStateTypeId).toBool() != reachable) { if (device->stateValue(networkDeviceIsPresentStateTypeId).toBool() != reachable) {
qCDebug(dcNetworkDetector()) << "Device" << device->paramValue(networkDeviceDeviceMacAddressParamTypeId).toString() << "reachable changed" << reachable; qCDebug(dcNetworkDetector()) << "Device" << device->name() << device->paramValue(networkDeviceDeviceMacAddressParamTypeId).toString() << "reachable changed" << reachable;
device->setStateValue(networkDeviceConnectedStateTypeId, reachable);
device->setStateValue(networkDeviceIsPresentStateTypeId, reachable); device->setStateValue(networkDeviceIsPresentStateTypeId, reachable);
} }
} }
@ -154,6 +153,7 @@ void DevicePluginNetworkDetector::deviceAddressChanged(const QString &address)
DeviceMonitor *monitor = static_cast<DeviceMonitor*>(sender()); DeviceMonitor *monitor = static_cast<DeviceMonitor*>(sender());
Device *device = m_monitors.value(monitor); Device *device = m_monitors.value(monitor);
if (device->paramValue(networkDeviceDeviceAddressParamTypeId).toString() != address) { if (device->paramValue(networkDeviceDeviceAddressParamTypeId).toString() != address) {
qCDebug(dcNetworkDetector()) << "Device" << device->name() << device->paramValue(networkDeviceDeviceMacAddressParamTypeId).toString() << "changed IP address to" << address;
device->setParamValue(networkDeviceDeviceAddressParamTypeId.toString(), address); device->setParamValue(networkDeviceDeviceAddressParamTypeId.toString(), address);
} }
} }

View File

@ -17,7 +17,7 @@
"Device", "Device",
"Sensor" "Sensor"
], ],
"interfaces": [ "connectable", "presencesensor" ], "interfaces": [ "presencesensor" ],
"primaryStateTypeId": "cb43e1b5-4f61-4538-bfa2-c33055c542cf", "primaryStateTypeId": "cb43e1b5-4f61-4538-bfa2-c33055c542cf",
"createMethods": ["user", "discovery"], "createMethods": ["user", "discovery"],
"paramTypes": [ "paramTypes": [
@ -39,15 +39,6 @@
"stateTypes": [ "stateTypes": [
{ {
"id": "cb43e1b5-4f61-4538-bfa2-c33055c542cf", "id": "cb43e1b5-4f61-4538-bfa2-c33055c542cf",
"name": "connected",
"displayName": "Device in network",
"displayNameEvent": "Device in network changed",
"type": "bool",
"defaultValue": false,
"cached": false
},
{
"id": "0a38651c-418d-42b3-80f5-6e7adb6a25fc",
"name": "isPresent", "name": "isPresent",
"displayName": "Device is present", "displayName": "Device is present",
"displayNameEvent": "Device is present changed", "displayNameEvent": "Device is present changed",