From cbb7a2a7f1aed2c819d9c5c7c6662b791eb4f0a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Fri, 11 Jun 2021 11:03:21 +0200 Subject: [PATCH] Add ARP cache loading and rename to networkdeviceinfo --- libnymea/libnymea.pro | 8 +- libnymea/network/arpsocket.cpp | 69 ++++++++ libnymea/network/arpsocket.h | 2 + libnymea/network/networkdevice.cpp | 116 ------------- libnymea/network/networkdevicediscovery.cpp | 51 +++--- .../network/networkdevicediscoveryreply.cpp | 4 +- .../network/networkdevicediscoveryreply.h | 6 +- libnymea/network/networkdeviceinfo.cpp | 159 ++++++++++++++++++ .../{networkdevice.h => networkdeviceinfo.h} | 22 ++- ...workdevices.cpp => networkdeviceinfos.cpp} | 58 ++++--- ...{networkdevices.h => networkdeviceinfos.h} | 24 +-- 11 files changed, 328 insertions(+), 191 deletions(-) delete mode 100644 libnymea/network/networkdevice.cpp create mode 100644 libnymea/network/networkdeviceinfo.cpp rename libnymea/network/{networkdevice.h => networkdeviceinfo.h} (76%) rename libnymea/network/{networkdevices.cpp => networkdeviceinfos.cpp} (58%) rename libnymea/network/{networkdevices.h => networkdeviceinfos.h} (77%) diff --git a/libnymea/libnymea.pro b/libnymea/libnymea.pro index 4ec5c0e1..26d05539 100644 --- a/libnymea/libnymea.pro +++ b/libnymea/libnymea.pro @@ -45,10 +45,10 @@ HEADERS += \ network/apikeys/apikeystorage.h \ network/arpsocket.h \ network/macaddressdatabase.h \ - network/networkdevice.h \ network/networkdevicediscovery.h \ network/networkdevicediscoveryreply.h \ - network/networkdevices.h \ + network/networkdeviceinfo.h \ + network/networkdeviceinfos.h \ network/networkutils.h \ network/ping.h \ network/pingreply.h \ @@ -152,10 +152,10 @@ SOURCES += \ network/apikeys/apikeystorage.cpp \ network/arpsocket.cpp \ network/macaddressdatabase.cpp \ - network/networkdevice.cpp \ network/networkdevicediscovery.cpp \ network/networkdevicediscoveryreply.cpp \ - network/networkdevices.cpp \ + network/networkdeviceinfo.cpp \ + network/networkdeviceinfos.cpp \ network/networkutils.cpp \ network/ping.cpp \ network/pingreply.cpp \ diff --git a/libnymea/network/arpsocket.cpp b/libnymea/network/arpsocket.cpp index c90ac5d1..a05e84af 100644 --- a/libnymea/network/arpsocket.cpp +++ b/libnymea/network/arpsocket.cpp @@ -48,6 +48,9 @@ #include #include +#include +#include +#include #include NYMEA_LOGGING_CATEGORY(dcArpSocket, "ArpSocket") @@ -82,6 +85,7 @@ bool ArpSocket::sendRequest(const QString &interfaceName) if (!m_isOpen) return false; + // Get the interface qCDebug(dcArpSocket()) << "Sending ARP request to all network interfaces" << interfaceName << "..."; QNetworkInterface networkInterface = QNetworkInterface::interfaceFromName(interfaceName); @@ -90,6 +94,7 @@ bool ArpSocket::sendRequest(const QString &interfaceName) return false; } + loadArpCache(networkInterface); return sendRequest(networkInterface); } @@ -125,6 +130,8 @@ bool ArpSocket::sendRequest(const QNetworkInterface &networkInterface) return false; } + loadArpCache(networkInterface); + qCDebug(dcArpSocket()) << "Verifying network interface" << networkInterface.name() << networkInterface.hardwareAddress() << "..."; foreach (const QNetworkAddressEntry &entry, networkInterface.addressEntries()) { // Only IPv4 @@ -377,6 +384,68 @@ QHostAddress ArpSocket::getHostAddressString(uint8_t *senderIpAddress) return QHostAddress(values.join(".")); } +bool ArpSocket::loadArpCache(const QNetworkInterface &interface) +{ + QFile arpFile("/proc/net/arp"); + qCDebug(dcArpSocket()) << "Loading ARP cache from system" << arpFile.fileName() << "..."; + if (!arpFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + qCWarning(dcArpSocket()) << "Failed to load ARP cache from" << arpFile.fileName() << arpFile.errorString(); + return false; + } + + // Read all data + QByteArray data = arpFile.readAll(); + arpFile.close(); + + // Parse data line by line + int lineCount = -1; + QTextStream stream(&data); + while (!stream.atEnd()) { + QString line = stream.readLine(); + lineCount += 1; + // Skip the first line since it's just the header + if (lineCount == 0) + continue; + + + //qCDebug(dcArpSocket()) << "Checking line" << line; + + QStringList columns = line.split(QLatin1Char(' ')); + columns.removeAll(""); + + // Make sure we have enought token + if (columns.count() < 6) { + qCWarning(dcArpSocket()) << "Line has invalid column count" << line; + continue; + } + + QHostAddress address(columns.at(0).trimmed()); + if (address.isNull()) { + qCWarning(dcArpSocket()) << "Line has invalid address"; + continue; + } + + QString macAddress = columns.at(3).trimmed(); + if (macAddress.count() != 17) { + qCWarning(dcArpSocket()) << "Line has invalid mac address" << columns << macAddress; + continue; + } + + QNetworkInterface addressInterface = QNetworkInterface::interfaceFromName(columns.at(5)); + if (!addressInterface.isValid()) + continue; + + // Check if we filter for specific interfaces + if (interface.isValid() && addressInterface.name() != interface.name()) + continue; + + qCDebug(dcArpSocket()) << "Loaded from cache" << address.toString() << macAddress << addressInterface.name(); + emit arpResponse(addressInterface, address, macAddress); + } + + return true; +} + void ArpSocket::fillMacAddress(uint8_t *targetArray, const QString &macAddress) { QStringList macValues = macAddress.split(":"); diff --git a/libnymea/network/arpsocket.h b/libnymea/network/arpsocket.h index 4cea59fd..2bc58175 100644 --- a/libnymea/network/arpsocket.h +++ b/libnymea/network/arpsocket.h @@ -76,6 +76,8 @@ private: QString getMacAddressString(uint8_t *senderHardwareAddress); QHostAddress getHostAddressString(uint8_t *senderIpAddress); + bool loadArpCache(const QNetworkInterface &interface = QNetworkInterface()); + void fillMacAddress(uint8_t *targetArray, const QString &macAddress); void fillHostAddress(uint8_t *targetArray, const QHostAddress &hostAddress); diff --git a/libnymea/network/networkdevice.cpp b/libnymea/network/networkdevice.cpp deleted file mode 100644 index 465dc5e3..00000000 --- a/libnymea/network/networkdevice.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* -* Copyright 2013 - 2021, nymea GmbH -* Contact: contact@nymea.io -* -* This file is part of nymea. -* This project including source code and documentation is protected by -* copyright law, and remains the property of nymea GmbH. All rights, including -* reproduction, publication, editing and translation, are reserved. The use of -* this project is subject to the terms of a license agreement to be concluded -* with nymea GmbH in accordance with the terms of use of nymea GmbH, available -* under https://nymea.io/license -* -* GNU Lesser General Public License Usage -* Alternatively, this project may be redistributed and/or modified under the -* terms of the GNU Lesser General Public License as published by the Free -* Software Foundation; version 3. This project is distributed in the hope that -* it will be useful, but WITHOUT ANY WARRANTY; without even the implied -* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public License -* along with this project. If not, see . -* -* For any further details and any questions please contact us under -* contact@nymea.io or see our FAQ/Licensing Information on -* https://nymea.io/license/faq -* -* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include "networkdevice.h" - -NetworkDevice::NetworkDevice() -{ - -} - -NetworkDevice::NetworkDevice(const QString &macAddress): - m_macAddress(macAddress) -{ - -} - -QString NetworkDevice::macAddress() const -{ - return m_macAddress; -} - -void NetworkDevice::setMacAddress(const QString &macAddress) -{ - m_macAddress = macAddress; -} - -QString NetworkDevice::macAddressManufacturer() const -{ - return m_macAddressManufacturer; -} - -void NetworkDevice::setMacAddressManufacturer(const QString &macAddressManufacturer) -{ - m_macAddressManufacturer = macAddressManufacturer; -} - -QHostAddress NetworkDevice::address() const -{ - return m_address; -} - -void NetworkDevice::setAddress(const QHostAddress &address) -{ - m_address = address; -} - -QString NetworkDevice::hostName() const -{ - return m_hostName; -} - -void NetworkDevice::setHostName(const QString &hostName) -{ - m_hostName = hostName; -} - -QNetworkInterface NetworkDevice::networkInterface() const -{ - return m_networkInterface; -} - -void NetworkDevice::setNetworkInterface(const QNetworkInterface &networkInterface) -{ - m_networkInterface = networkInterface; -} - -bool NetworkDevice::isValid() const -{ - return (!m_address.isNull() || !m_macAddress.isEmpty()) && m_networkInterface.isValid(); -} - -QDebug operator<<(QDebug dbg, const NetworkDevice &networkDevice) -{ - dbg.nospace() << "NetworkDevice("; - dbg.nospace() << networkDevice.address().toString() << ", "; - if (!networkDevice.hostName().isEmpty()) - dbg.nospace() << ", " << networkDevice.hostName(); - - dbg.nospace() << networkDevice.macAddress(); - if (!networkDevice.macAddressManufacturer().isEmpty()) - dbg.nospace() << "(" << networkDevice.macAddressManufacturer() << ")"; - - if (networkDevice.networkInterface().isValid()) - dbg.nospace() << ", " << networkDevice.networkInterface().name(); - - dbg.nospace() << ")"; - return dbg.space(); -} - diff --git a/libnymea/network/networkdevicediscovery.cpp b/libnymea/network/networkdevicediscovery.cpp index cadf2be5..cf870490 100644 --- a/libnymea/network/networkdevicediscovery.cpp +++ b/libnymea/network/networkdevicediscovery.cpp @@ -164,18 +164,18 @@ void NetworkDeviceDiscovery::pingAllNetworkDevices() m_runningPingRepies.removeAll(reply); if (reply->error() == PingReply::ErrorNoError) { qCDebug(dcNetworkDeviceDiscovery()) << "Ping response from" << targetAddress.toString() << reply->hostName() << reply->duration() << "ms"; - int index = m_currentReply->networkDevices().indexFromHostAddress(targetAddress); + int index = m_currentReply->networkDeviceInfos().indexFromHostAddress(targetAddress); if (index < 0) { // Add the network device - NetworkDevice networkDevice; - networkDevice.setAddress(targetAddress); - networkDevice.setHostName(reply->hostName()); - m_currentReply->networkDevices().append(networkDevice); + NetworkDeviceInfo networkDeviceInfo; + networkDeviceInfo.setAddress(targetAddress); + networkDeviceInfo.setHostName(reply->hostName()); + m_currentReply->networkDeviceInfos().append(networkDeviceInfo); } else { - m_currentReply->networkDevices()[index].setAddress(targetAddress); - m_currentReply->networkDevices()[index].setHostName(reply->hostName()); - if (!m_currentReply->networkDevices()[index].networkInterface().isValid()) { - m_currentReply->networkDevices()[index].setNetworkInterface(NetworkUtils::getInterfaceForHostaddress(targetAddress)); + m_currentReply->networkDeviceInfos()[index].setAddress(targetAddress); + m_currentReply->networkDeviceInfos()[index].setHostName(reply->hostName()); + if (!m_currentReply->networkDeviceInfos()[index].networkInterface().isValid()) { + m_currentReply->networkDeviceInfos()[index].setNetworkInterface(NetworkUtils::getInterfaceForHostaddress(targetAddress)); } } } @@ -195,8 +195,11 @@ void NetworkDeviceDiscovery::finishDiscovery() m_running = false; emit runningChanged(m_running); + // Sort by host address + m_currentReply->networkDeviceInfos().sortNetworkDevices(); + qint64 durationMilliSeconds = QDateTime::currentMSecsSinceEpoch() - m_currentReply->m_startTimestamp; - qCDebug(dcNetworkDeviceDiscovery()) << "Discovery finished. Found" << m_currentReply->networkDevices().count() << "network devices in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz"); + qCDebug(dcNetworkDeviceDiscovery()) << "Discovery finished. Found" << m_currentReply->networkDeviceInfos().count() << "network devices in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz"); emit m_currentReply->finished(); m_currentReply->deleteLater(); m_currentReply = nullptr; @@ -204,39 +207,39 @@ void NetworkDeviceDiscovery::finishDiscovery() void NetworkDeviceDiscovery::updateOrAddNetworkDeviceArp(const QNetworkInterface &interface, const QHostAddress &address, const QString &macAddress, const QString &manufacturer) { - int index = m_currentReply->networkDevices().indexFromHostAddress(address); + int index = m_currentReply->networkDeviceInfos().indexFromHostAddress(address); if (index >= 0) { // Update the network device - m_currentReply->networkDevices()[index].setMacAddress(macAddress); + m_currentReply->networkDeviceInfos()[index].setMacAddress(macAddress); if (!manufacturer.isEmpty()) - m_currentReply->networkDevices()[index].setMacAddressManufacturer(manufacturer); + m_currentReply->networkDeviceInfos()[index].setMacAddressManufacturer(manufacturer); if (interface.isValid()) { - m_currentReply->networkDevices()[index].setNetworkInterface(interface); + m_currentReply->networkDeviceInfos()[index].setNetworkInterface(interface); } } else { - index = m_currentReply->networkDevices().indexFromMacAddress(macAddress); + index = m_currentReply->networkDeviceInfos().indexFromMacAddress(macAddress); if (index >= 0) { // Update the network device - m_currentReply->networkDevices()[index].setAddress(address); + m_currentReply->networkDeviceInfos()[index].setAddress(address); if (!manufacturer.isEmpty()) - m_currentReply->networkDevices()[index].setMacAddressManufacturer(manufacturer); + m_currentReply->networkDeviceInfos()[index].setMacAddressManufacturer(manufacturer); if (interface.isValid()) { - m_currentReply->networkDevices()[index].setNetworkInterface(interface); + m_currentReply->networkDeviceInfos()[index].setNetworkInterface(interface); } } else { // Add the network device - NetworkDevice networkDevice; - networkDevice.setAddress(address); - networkDevice.setMacAddress(macAddress); + NetworkDeviceInfo networkDeviceInfo; + networkDeviceInfo.setAddress(address); + networkDeviceInfo.setMacAddress(macAddress); if (!manufacturer.isEmpty()) - networkDevice.setMacAddressManufacturer(manufacturer); + networkDeviceInfo.setMacAddressManufacturer(manufacturer); if (interface.isValid()) - networkDevice.setNetworkInterface(interface); + networkDeviceInfo.setNetworkInterface(interface); - m_currentReply->networkDevices().append(networkDevice); + m_currentReply->networkDeviceInfos().append(networkDeviceInfo); } } } diff --git a/libnymea/network/networkdevicediscoveryreply.cpp b/libnymea/network/networkdevicediscoveryreply.cpp index 71f7a453..958f1788 100644 --- a/libnymea/network/networkdevicediscoveryreply.cpp +++ b/libnymea/network/networkdevicediscoveryreply.cpp @@ -36,7 +36,7 @@ NetworkDeviceDiscoveryReply::NetworkDeviceDiscoveryReply(QObject *parent) : } -NetworkDevices &NetworkDeviceDiscoveryReply::networkDevices() +NetworkDeviceInfos &NetworkDeviceDiscoveryReply::networkDeviceInfos() { - return m_networkDevices; + return m_networkDeviceInfos; } diff --git a/libnymea/network/networkdevicediscoveryreply.h b/libnymea/network/networkdevicediscoveryreply.h index 670294d1..439e949f 100644 --- a/libnymea/network/networkdevicediscoveryreply.h +++ b/libnymea/network/networkdevicediscoveryreply.h @@ -34,7 +34,7 @@ #include #include "libnymea.h" -#include "networkdevices.h" +#include "networkdeviceinfos.h" class LIBNYMEA_EXPORT NetworkDeviceDiscoveryReply : public QObject { @@ -43,14 +43,14 @@ class LIBNYMEA_EXPORT NetworkDeviceDiscoveryReply : public QObject friend class NetworkDeviceDiscovery; public: - NetworkDevices &networkDevices(); + NetworkDeviceInfos &networkDeviceInfos(); signals: void finished(); private: explicit NetworkDeviceDiscoveryReply(QObject *parent = nullptr); - NetworkDevices m_networkDevices; + NetworkDeviceInfos m_networkDeviceInfos; qint64 m_startTimestamp; }; diff --git a/libnymea/network/networkdeviceinfo.cpp b/libnymea/network/networkdeviceinfo.cpp new file mode 100644 index 00000000..6d36deec --- /dev/null +++ b/libnymea/network/networkdeviceinfo.cpp @@ -0,0 +1,159 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright 2013 - 2021, nymea GmbH +* Contact: contact@nymea.io +* +* This file is part of nymea. +* This project including source code and documentation is protected by +* copyright law, and remains the property of nymea GmbH. All rights, including +* reproduction, publication, editing and translation, are reserved. The use of +* this project is subject to the terms of a license agreement to be concluded +* with nymea GmbH in accordance with the terms of use of nymea GmbH, available +* under https://nymea.io/license +* +* GNU Lesser General Public License Usage +* Alternatively, this project may be redistributed and/or modified under the +* terms of the GNU Lesser General Public License as published by the Free +* Software Foundation; version 3. This project is distributed in the hope that +* it will be useful, but WITHOUT ANY WARRANTY; without even the implied +* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this project. If not, see . +* +* For any further details and any questions please contact us under +* contact@nymea.io or see our FAQ/Licensing Information on +* https://nymea.io/license/faq +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "networkdeviceinfo.h" + +NetworkDeviceInfo::NetworkDeviceInfo() +{ + +} + +NetworkDeviceInfo::NetworkDeviceInfo(const QString &macAddress): + m_macAddress(macAddress) +{ + +} + +QString NetworkDeviceInfo::macAddress() const +{ + return m_macAddress; +} + +void NetworkDeviceInfo::setMacAddress(const QString &macAddress) +{ + m_macAddress = macAddress; +} + +QString NetworkDeviceInfo::macAddressManufacturer() const +{ + return m_macAddressManufacturer; +} + +void NetworkDeviceInfo::setMacAddressManufacturer(const QString &macAddressManufacturer) +{ + m_macAddressManufacturer = macAddressManufacturer; +} + +QHostAddress NetworkDeviceInfo::address() const +{ + return m_address; +} + +void NetworkDeviceInfo::setAddress(const QHostAddress &address) +{ + m_address = address; +} + +QString NetworkDeviceInfo::hostName() const +{ + return m_hostName; +} + +void NetworkDeviceInfo::setHostName(const QString &hostName) +{ + m_hostName = hostName; +} + +QNetworkInterface NetworkDeviceInfo::networkInterface() const +{ + return m_networkInterface; +} + +void NetworkDeviceInfo::setNetworkInterface(const QNetworkInterface &networkInterface) +{ + m_networkInterface = networkInterface; +} + +bool NetworkDeviceInfo::isValid() const +{ + return (!m_address.isNull() || !m_macAddress.isEmpty()) && m_networkInterface.isValid(); +} + +//bool NetworkDeviceInfo::operator!=(const NetworkDeviceInfo &other) const +//{ +// return !(*this == other); +//} + +//bool NetworkDeviceInfo::operator==(const NetworkDeviceInfo &other) const +//{ +// return m_address == other.address() && +// m_macAddress == other.macAddress() && +// m_macAddressManufacturer == other.macAddressManufacturer() && +// m_hostName == other.hostName(); +//} + +//bool NetworkDeviceInfo::operator<(const NetworkDeviceInfo &other) const +//{ + +// return m_address.toIPv4Address() < other.address().toIPv4Address(); +//} + +//bool NetworkDeviceInfo::operator<=(const NetworkDeviceInfo &other) const +//{ +// return m_address.toIPv4Address() <= other.address().toIPv4Address(); +//} + +//bool NetworkDeviceInfo::operator>(const NetworkDeviceInfo &other) const +//{ +// return m_address.toIPv4Address() > other.address().toIPv4Address(); +//} + +//bool NetworkDeviceInfo::operator>=(const NetworkDeviceInfo &other) const +//{ +// return m_address.toIPv4Address() != other.address().toIPv4Address(); +//} + +//bool NetworkDeviceInfo::operator-(const NetworkDeviceInfo &other) const +//{ +// return m_address.toIPv4Address() - other.address().toIPv4Address(); +//} + +//bool NetworkDeviceInfo::operator+(const NetworkDeviceInfo &other) const +//{ +// return m_address.toIPv4Address() + other.address().toIPv4Address(); +//} + +QDebug operator<<(QDebug dbg, const NetworkDeviceInfo &networkDeviceInfo) +{ + dbg.nospace() << "NetworkDeviceInfo(" << networkDeviceInfo.address().toString(); + if (!networkDeviceInfo.hostName().isEmpty()) + dbg.nospace() << " (" << networkDeviceInfo.hostName() << ")"; + + dbg.nospace() << ", " << networkDeviceInfo.macAddress(); + if (!networkDeviceInfo.macAddressManufacturer().isEmpty()) + dbg.nospace() << " (" << networkDeviceInfo.macAddressManufacturer() << ") "; + + if (networkDeviceInfo.networkInterface().isValid()) + dbg.nospace() << ", " << networkDeviceInfo.networkInterface().name(); + + dbg.nospace() << ")"; + return dbg.space(); +} + diff --git a/libnymea/network/networkdevice.h b/libnymea/network/networkdeviceinfo.h similarity index 76% rename from libnymea/network/networkdevice.h rename to libnymea/network/networkdeviceinfo.h index a2e57395..75285540 100644 --- a/libnymea/network/networkdevice.h +++ b/libnymea/network/networkdeviceinfo.h @@ -28,8 +28,8 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#ifndef NETWORKDEVICE_H -#define NETWORKDEVICE_H +#ifndef NETWORKDEVICEINFO_H +#define NETWORKDEVICEINFO_H #include #include @@ -38,11 +38,12 @@ #include "libnymea.h" -class LIBNYMEA_EXPORT NetworkDevice +class LIBNYMEA_EXPORT NetworkDeviceInfo { public: - explicit NetworkDevice(); - explicit NetworkDevice(const QString &macAddress); + explicit NetworkDeviceInfo(); + explicit NetworkDeviceInfo(const QString &macAddress); + ~NetworkDeviceInfo() = default; QString macAddress() const; void setMacAddress(const QString &macAddress); @@ -61,6 +62,13 @@ public: bool isValid() const; +// bool operator!=(const NetworkDeviceInfo& other) const; +// bool operator==(const NetworkDeviceInfo& other) const; +// bool operator<(const NetworkDeviceInfo& other) const; +// bool operator<=(const NetworkDeviceInfo& other) const; +// bool operator>(const NetworkDeviceInfo& other) const; +// bool operator>=(const NetworkDeviceInfo& other) const; + private: QHostAddress m_address; QString m_macAddress; @@ -70,7 +78,7 @@ private: }; -QDebug operator<<(QDebug debug, const NetworkDevice &networkDevice); +QDebug operator<<(QDebug debug, const NetworkDeviceInfo &networkDeviceInfo); -#endif // NETWORKDEVICE_H +#endif // NETWORKDEVICEINFO_H diff --git a/libnymea/network/networkdevices.cpp b/libnymea/network/networkdeviceinfos.cpp similarity index 58% rename from libnymea/network/networkdevices.cpp rename to libnymea/network/networkdeviceinfos.cpp index 8f940f8e..45edcec2 100644 --- a/libnymea/network/networkdevices.cpp +++ b/libnymea/network/networkdeviceinfos.cpp @@ -28,27 +28,24 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#include "networkdevices.h" +#include "networkdeviceinfos.h" -NetworkDevices::NetworkDevices() : - QList() +#include + +NetworkDeviceInfos::NetworkDeviceInfos() : + QVector() { } -NetworkDevices::NetworkDevices(const QList &other) : - QList(other) +NetworkDeviceInfos::NetworkDeviceInfos(const QVector &other) : + QVector(other) { } -NetworkDevices NetworkDevices::operator<<(const NetworkDevice &networkDevice) -{ - this->append(networkDevice); - return *this; -} -int NetworkDevices::indexFromHostAddress(const QHostAddress &address) +int NetworkDeviceInfos::indexFromHostAddress(const QHostAddress &address) { for (int i = 0; i < this->size(); i++) { if (at(i).address().toIPv4Address() == address.toIPv4Address()) { @@ -59,7 +56,7 @@ int NetworkDevices::indexFromHostAddress(const QHostAddress &address) return -1; } -int NetworkDevices::indexFromMacAddress(const QString &macAddress) +int NetworkDeviceInfos::indexFromMacAddress(const QString &macAddress) { for (int i = 0; i < size(); i++) { if (at(i).macAddress().toLower() == macAddress.toLower()) { @@ -70,34 +67,47 @@ int NetworkDevices::indexFromMacAddress(const QString &macAddress) return -1; } -bool NetworkDevices::hasHostAddress(const QHostAddress &address) +bool NetworkDeviceInfos::hasHostAddress(const QHostAddress &address) { return indexFromHostAddress(address) >= 0; } -bool NetworkDevices::hasMacAddress(const QString &macAddress) +bool NetworkDeviceInfos::hasMacAddress(const QString &macAddress) { return indexFromMacAddress(macAddress) >= 0; } -NetworkDevice NetworkDevices::get(const QHostAddress &address) +NetworkDeviceInfo NetworkDeviceInfos::get(const QHostAddress &address) { - foreach (const NetworkDevice &networkDevice, *this) { - if (networkDevice.address() == address) { - return networkDevice; + foreach (const NetworkDeviceInfo &networkDeviceInfo, *this) { + if (networkDeviceInfo.address() == address) { + return networkDeviceInfo; } } - return NetworkDevice(); + return NetworkDeviceInfo(); } -NetworkDevice NetworkDevices::get(const QString &macAddress) +NetworkDeviceInfo NetworkDeviceInfos::get(const QString &macAddress) { - foreach (const NetworkDevice &networkDevice, *this) { - if (networkDevice.macAddress() == macAddress) { - return networkDevice; + foreach (const NetworkDeviceInfo &networkDeviceInfo, *this) { + if (networkDeviceInfo.macAddress() == macAddress) { + return networkDeviceInfo; } } - return NetworkDevice(); + return NetworkDeviceInfo(); +} + +void NetworkDeviceInfos::sortNetworkDevices() +{ +// std::sort(*this->begin(), *this->end(), [](const NetworkDeviceInfo& a, const NetworkDeviceInfo& b) { +// return a.address().toIPv4Address() < b.address().toIPv4Address(); +// }); +} + +NetworkDeviceInfos &NetworkDeviceInfos::operator <<(const NetworkDeviceInfo &networkDeviceInfo) +{ + this->append(networkDeviceInfo); + return *this; } diff --git a/libnymea/network/networkdevices.h b/libnymea/network/networkdeviceinfos.h similarity index 77% rename from libnymea/network/networkdevices.h rename to libnymea/network/networkdeviceinfos.h index 8d3fc2cc..ab23f77f 100644 --- a/libnymea/network/networkdevices.h +++ b/libnymea/network/networkdeviceinfos.h @@ -28,22 +28,20 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#ifndef NETWORKDEVICES_H -#define NETWORKDEVICES_H +#ifndef NETWORKDEVICEINFOS_H +#define NETWORKDEVICEINFOS_H #include #include "libnymea.h" -#include "networkdevice.h" +#include "networkdeviceinfo.h" -class LIBNYMEA_EXPORT NetworkDevices : public QList +class LIBNYMEA_EXPORT NetworkDeviceInfos : public QVector { public: - explicit NetworkDevices(); - NetworkDevices(const QList &other); - - NetworkDevices operator<<(const NetworkDevice &networkDevice); + explicit NetworkDeviceInfos(); + NetworkDeviceInfos(const QVector &other); int indexFromHostAddress(const QHostAddress &address); int indexFromMacAddress(const QString &macAddress); @@ -51,9 +49,13 @@ public: bool hasHostAddress(const QHostAddress &address); bool hasMacAddress(const QString &macAddress); - NetworkDevice get(const QHostAddress &address); - NetworkDevice get(const QString &macAddress); + NetworkDeviceInfo get(const QHostAddress &address); + NetworkDeviceInfo get(const QString &macAddress); + + void sortNetworkDevices(); + + NetworkDeviceInfos &operator<<(const NetworkDeviceInfo &networkDeviceInfo); }; -#endif // NETWORKDEVICES_H +#endif // NETWORKDEVICEINFOS_H