NetworkDeviceDiscovery: wait for pending MAC address manufacturer lookups before finishing a discovery

This commit is contained in:
Simon Stürz 2022-11-10 00:30:11 +01:00
parent 8367f60886
commit 8271e527eb
2 changed files with 68 additions and 54 deletions

View File

@ -64,7 +64,8 @@ NetworkDeviceDiscoveryImpl::NetworkDeviceDiscoveryImpl(QObject *parent) :
m_discoveryTimer->setInterval(20000); m_discoveryTimer->setInterval(20000);
m_discoveryTimer->setSingleShot(true); m_discoveryTimer->setSingleShot(true);
connect(m_discoveryTimer, &QTimer::timeout, this, [=](){ connect(m_discoveryTimer, &QTimer::timeout, this, [=](){
if (m_runningPingReplies.isEmpty() && m_currentReply) { if (m_runningPingReplies.isEmpty() && m_runningMacDatabaseReplies.isEmpty() && m_currentDiscoveryReply) {
qCDebug(dcNetworkDeviceDiscovery()) << "Discovery timeout occurred and all pending replies have finished.";
finishDiscovery(); finishDiscovery();
} }
}); });
@ -103,27 +104,27 @@ NetworkDeviceDiscoveryReply *NetworkDeviceDiscoveryImpl::discover()
// reply and owns the object, even if the discovery has been finished. // reply and owns the object, even if the discovery has been finished.
// Create the internal object if required // Create the internal object if required
bool alreadyRunning = (m_currentReply != nullptr); bool alreadyRunning = (m_currentDiscoveryReply != nullptr);
if (alreadyRunning) { if (alreadyRunning) {
if (m_currentReply->isFinished()) { if (m_currentDiscoveryReply->isFinished()) {
qCDebug(dcNetworkDeviceDiscovery()) << "Discovery internally already running and finished."; qCDebug(dcNetworkDeviceDiscovery()) << "Discovery internally already running and finished.";
} else { } else {
qCDebug(dcNetworkDeviceDiscovery()) << "Discovery internally already running. Re-using the current running discovery reply."; qCDebug(dcNetworkDeviceDiscovery()) << "Discovery internally already running. Re-using the current running discovery reply.";
} }
} else { } else {
qCDebug(dcNetworkDeviceDiscovery()) << "Starting internally a new discovery."; qCDebug(dcNetworkDeviceDiscovery()) << "Starting internally a new discovery.";
m_currentReply = new NetworkDeviceDiscoveryReplyImpl(this); m_currentDiscoveryReply = new NetworkDeviceDiscoveryReplyImpl(this);
connect(m_currentReply, &NetworkDeviceDiscoveryReplyImpl::networkDeviceInfoAdded, this, &NetworkDeviceDiscoveryImpl::updateCache); connect(m_currentDiscoveryReply, &NetworkDeviceDiscoveryReplyImpl::networkDeviceInfoAdded, this, &NetworkDeviceDiscoveryImpl::updateCache);
connect(m_currentReply, &NetworkDeviceDiscoveryReplyImpl::finished, this, [this](){ connect(m_currentDiscoveryReply, &NetworkDeviceDiscoveryReplyImpl::finished, this, [this](){
// Finish all pending replies // Finish all pending replies
foreach (NetworkDeviceDiscoveryReplyImpl *reply, m_pendingReplies) { foreach (NetworkDeviceDiscoveryReplyImpl *reply, m_pendingDiscoveryReplies) {
// Sync all network device infos with all pending replies // Sync all network device infos with all pending replies
foreach (const NetworkDeviceInfo &info, m_currentReply->networkDeviceInfos()) { foreach (const NetworkDeviceInfo &info, m_currentDiscoveryReply->networkDeviceInfos()) {
reply->addCompleteNetworkDeviceInfo(info); reply->addCompleteNetworkDeviceInfo(info);
} }
foreach (const NetworkDeviceInfo &info, m_currentReply->virtualNetworkDeviceInfos()) { foreach (const NetworkDeviceInfo &info, m_currentDiscoveryReply->virtualNetworkDeviceInfos()) {
reply->addVirtualNetworkDeviceInfo(info); reply->addVirtualNetworkDeviceInfo(info);
} }
} }
@ -131,11 +132,11 @@ NetworkDeviceDiscoveryReply *NetworkDeviceDiscoveryImpl::discover()
// Delete the current reply before finishing the pending replies. // Delete the current reply before finishing the pending replies.
// Just in case some one restarts a discovery on finished, a new internal // Just in case some one restarts a discovery on finished, a new internal
// object should be created // object should be created
m_currentReply->deleteLater(); m_currentDiscoveryReply->deleteLater();
m_currentReply = nullptr; m_currentDiscoveryReply = nullptr;
foreach (NetworkDeviceDiscoveryReplyImpl *reply, m_pendingReplies) { foreach (NetworkDeviceDiscoveryReplyImpl *reply, m_pendingDiscoveryReplies) {
m_pendingReplies.removeAll(reply); m_pendingDiscoveryReplies.removeAll(reply);
reply->setFinished(true); reply->setFinished(true);
emit reply->finished(); emit reply->finished();
} }
@ -144,9 +145,9 @@ NetworkDeviceDiscoveryReply *NetworkDeviceDiscoveryImpl::discover()
// Create the reply for the user // Create the reply for the user
NetworkDeviceDiscoveryReplyImpl *reply = new NetworkDeviceDiscoveryReplyImpl(this); NetworkDeviceDiscoveryReplyImpl *reply = new NetworkDeviceDiscoveryReplyImpl(this);
connect(m_currentReply, &NetworkDeviceDiscoveryReplyImpl::networkDeviceInfoAdded, reply, &NetworkDeviceDiscoveryReplyImpl::addCompleteNetworkDeviceInfo); connect(m_currentDiscoveryReply, &NetworkDeviceDiscoveryReplyImpl::networkDeviceInfoAdded, reply, &NetworkDeviceDiscoveryReplyImpl::addCompleteNetworkDeviceInfo);
connect(m_currentReply, &NetworkDeviceDiscoveryReplyImpl::hostAddressDiscovered, reply, &NetworkDeviceDiscoveryReplyImpl::hostAddressDiscovered); connect(m_currentDiscoveryReply, &NetworkDeviceDiscoveryReplyImpl::hostAddressDiscovered, reply, &NetworkDeviceDiscoveryReplyImpl::hostAddressDiscovered);
m_pendingReplies.append(reply); m_pendingDiscoveryReplies.append(reply);
if (!available()) { if (!available()) {
qCWarning(dcNetworkDeviceDiscovery()) << "The network discovery is not available. Please make sure the binary has the required capability (CAP_NET_RAW) or start the application as root."; qCWarning(dcNetworkDeviceDiscovery()) << "The network discovery is not available. Please make sure the binary has the required capability (CAP_NET_RAW) or start the application as root.";
@ -159,10 +160,10 @@ NetworkDeviceDiscoveryReply *NetworkDeviceDiscoveryImpl::discover()
// Add already discovered network device infos in the next event loop // Add already discovered network device infos in the next event loop
// so any connections after this method call will work as expected // so any connections after this method call will work as expected
QTimer::singleShot(0, reply, [this, reply](){ QTimer::singleShot(0, reply, [this, reply](){
if (!m_currentReply) if (!m_currentDiscoveryReply)
return; return;
foreach (const NetworkDeviceInfo &networkDeviceInfo, m_currentReply->networkDeviceInfos()) { foreach (const NetworkDeviceInfo &networkDeviceInfo, m_currentDiscoveryReply->networkDeviceInfos()) {
reply->addCompleteNetworkDeviceInfo(networkDeviceInfo); reply->addCompleteNetworkDeviceInfo(networkDeviceInfo);
} }
}); });
@ -372,13 +373,13 @@ void NetworkDeviceDiscoveryImpl::pingAllNetworkDevices()
m_runningPingReplies.removeAll(reply); m_runningPingReplies.removeAll(reply);
if (reply->error() == PingReply::ErrorNoError) { if (reply->error() == PingReply::ErrorNoError) {
qCDebug(dcNetworkDeviceDiscovery()) << "Ping response from" << targetAddress.toString() << reply->hostName() << reply->duration() << "ms"; qCDebug(dcNetworkDeviceDiscovery()) << "Ping response from" << targetAddress.toString() << reply->hostName() << reply->duration() << "ms";
if (m_currentReply) { if (m_currentDiscoveryReply) {
m_currentReply->processPingResponse(targetAddress, reply->hostName()); m_currentDiscoveryReply->processPingResponse(targetAddress, reply->hostName());
} }
} }
if (m_runningPingReplies.isEmpty() && m_currentReply && !m_discoveryTimer->isActive()) { if (m_runningPingReplies.isEmpty() && m_runningMacDatabaseReplies.isEmpty() && m_currentDiscoveryReply && !m_discoveryTimer->isActive()) {
qCWarning(dcNetworkDeviceDiscovery()) << "All ping replies finished for discovery." << m_currentReply->networkDeviceInfos().count(); qCDebug(dcNetworkDeviceDiscovery()) << "All pending replies have finished.";
finishDiscovery(); finishDiscovery();
} }
}); });
@ -592,39 +593,51 @@ void NetworkDeviceDiscoveryImpl::processArpTraffic(const QNetworkInterface &inte
} }
} }
// Check if we have a reply running // Check if we have currently reply running
if (m_currentReply) { if (!m_currentDiscoveryReply)
return;
// First process the response // First process the response
m_currentReply->processArpResponse(interface, address, macAddress); m_currentDiscoveryReply->processArpResponse(interface, address, macAddress);
// Check if we know the mac address manufacturer from the cache // Check if we know the mac address manufacturer from the cache
bool requiresMacAddressLookup = true; bool requiresMacAddressLookup = true;
if (m_networkInfoCache.contains(macAddress)) { if (m_networkInfoCache.contains(macAddress)) {
QString cachedManufacturer = m_networkInfoCache[macAddress].macAddressManufacturer(); QString cachedManufacturer = m_networkInfoCache[macAddress].macAddressManufacturer();
if (!cachedManufacturer.isEmpty()) { if (!cachedManufacturer.isEmpty()) {
// Found the mac address manufacturer in the cache, let's use that one... // Found the mac address manufacturer in the cache, let's use that one...
m_currentReply->processMacManufacturer(macAddress, cachedManufacturer); qCDebug(dcNetworkDeviceDiscovery()) << "Using cached manufacturer " << cachedManufacturer << "for" << macAddress.toString();
requiresMacAddressLookup = false; m_currentDiscoveryReply->processMacManufacturer(macAddress, cachedManufacturer);
} requiresMacAddressLookup = false;
} }
}
if (requiresMacAddressLookup) { if (!requiresMacAddressLookup)
// Lookup the mac address vendor if possible return;
if (m_macAddressDatabase->available()) {
// Not found in the cache, and the mac address database is available...let's make a query // Lookup the mac address vendor if possible
MacAddressDatabaseReply *reply = m_macAddressDatabase->lookupMacAddress(macAddress.toString()); if (m_macAddressDatabase->available()) {
connect(reply, &MacAddressDatabaseReply::finished, m_currentReply, [=](){ // Not found in the cache, and the mac address database is available...let's make a query
// Note: set the mac manufacturer explicitly to make the info complete (even an empty sring) MacAddressDatabaseReply *reply = m_macAddressDatabase->lookupMacAddress(macAddress.toString());
qCDebug(dcNetworkDeviceDiscovery()) << "MAC manufacturer lookup finished for" << macAddress << ":" << reply->manufacturer(); m_runningMacDatabaseReplies.append(reply);
m_currentReply->processMacManufacturer(macAddress, reply->manufacturer()); connect(reply, &MacAddressDatabaseReply::finished, this, [this, macAddress, reply](){
}); m_runningMacDatabaseReplies.removeAll(reply);
} else {
// Not found in the cache, and no mac address database available...we are done with mac vendor // Note: set the mac manufacturer explicitly to make the info complete (even an empty sring)
// Note: set the mac manufacturer explicitly to make the info complete qCDebug(dcNetworkDeviceDiscovery()) << "MAC manufacturer lookup finished for" << macAddress << ":" << reply->manufacturer();
m_currentReply->processMacManufacturer(macAddress, QString()); if (m_currentDiscoveryReply) {
m_currentDiscoveryReply->processMacManufacturer(macAddress, reply->manufacturer());
if (m_runningPingReplies.isEmpty() && m_runningMacDatabaseReplies.isEmpty() && !m_discoveryTimer->isActive()) {
qCWarning(dcNetworkDeviceDiscovery()) << "All pending replies have finished.";
finishDiscovery();
}
} }
} });
} else {
// Not found in the cache, and no mac address database available...we are done with mac vendor
// Set the mac manufacturer explicitly to make the info complete
m_currentDiscoveryReply->processMacManufacturer(macAddress, QString());
} }
} }
@ -722,8 +735,8 @@ void NetworkDeviceDiscoveryImpl::finishDiscovery()
m_lastDiscovery = QDateTime::currentDateTime(); m_lastDiscovery = QDateTime::currentDateTime();
// Clean up internal reply // Clean up internal reply
if (m_currentReply) { if (m_currentDiscoveryReply) {
m_currentReply->processDiscoveryFinished(); m_currentDiscoveryReply->processDiscoveryFinished();
} }
} }

View File

@ -101,8 +101,9 @@ private:
uint m_monitorInterval = 60; // 1 min uint m_monitorInterval = 60; // 1 min
uint m_cacheCleanupPeriod = 30; // days uint m_cacheCleanupPeriod = 30; // days
NetworkDeviceDiscoveryReplyImpl *m_currentReply = nullptr; NetworkDeviceDiscoveryReplyImpl *m_currentDiscoveryReply = nullptr;
QList<NetworkDeviceDiscoveryReplyImpl *> m_pendingReplies; QList<NetworkDeviceDiscoveryReplyImpl *> m_pendingDiscoveryReplies;
QList<MacAddressDatabaseReply *> m_runningMacDatabaseReplies;
QList<PingReply *> m_runningPingReplies; QList<PingReply *> m_runningPingReplies;
QHash<MacAddress, NetworkDeviceMonitorImpl *> m_monitors; QHash<MacAddress, NetworkDeviceMonitorImpl *> m_monitors;