Add ARP cache loading and rename to networkdeviceinfo
This commit is contained in:
parent
cbeac35059
commit
cbb7a2a7f1
@ -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 \
|
||||
|
||||
@ -48,6 +48,9 @@
|
||||
#include <netinet/if_ether.h>
|
||||
|
||||
#include <QHostInfo>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QTextStream>
|
||||
#include <QDataStream>
|
||||
|
||||
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(":");
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* 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();
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,7 +36,7 @@ NetworkDeviceDiscoveryReply::NetworkDeviceDiscoveryReply(QObject *parent) :
|
||||
|
||||
}
|
||||
|
||||
NetworkDevices &NetworkDeviceDiscoveryReply::networkDevices()
|
||||
NetworkDeviceInfos &NetworkDeviceDiscoveryReply::networkDeviceInfos()
|
||||
{
|
||||
return m_networkDevices;
|
||||
return m_networkDeviceInfos;
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@
|
||||
#include <QObject>
|
||||
|
||||
#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;
|
||||
|
||||
};
|
||||
|
||||
159
libnymea/network/networkdeviceinfo.cpp
Normal file
159
libnymea/network/networkdeviceinfo.cpp
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* 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();
|
||||
}
|
||||
|
||||
@ -28,8 +28,8 @@
|
||||
*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#ifndef NETWORKDEVICE_H
|
||||
#define NETWORKDEVICE_H
|
||||
#ifndef NETWORKDEVICEINFO_H
|
||||
#define NETWORKDEVICEINFO_H
|
||||
|
||||
#include <QDebug>
|
||||
#include <QObject>
|
||||
@ -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
|
||||
@ -28,27 +28,24 @@
|
||||
*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#include "networkdevices.h"
|
||||
#include "networkdeviceinfos.h"
|
||||
|
||||
NetworkDevices::NetworkDevices() :
|
||||
QList<NetworkDevice>()
|
||||
#include <algorithm>
|
||||
|
||||
NetworkDeviceInfos::NetworkDeviceInfos() :
|
||||
QVector<NetworkDeviceInfo>()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
NetworkDevices::NetworkDevices(const QList<NetworkDevice> &other) :
|
||||
QList<NetworkDevice>(other)
|
||||
NetworkDeviceInfos::NetworkDeviceInfos(const QVector<NetworkDeviceInfo> &other) :
|
||||
QVector<NetworkDeviceInfo>(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;
|
||||
}
|
||||
@ -28,22 +28,20 @@
|
||||
*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#ifndef NETWORKDEVICES_H
|
||||
#define NETWORKDEVICES_H
|
||||
#ifndef NETWORKDEVICEINFOS_H
|
||||
#define NETWORKDEVICEINFOS_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "libnymea.h"
|
||||
#include "networkdevice.h"
|
||||
#include "networkdeviceinfo.h"
|
||||
|
||||
class LIBNYMEA_EXPORT NetworkDevices : public QList<NetworkDevice>
|
||||
class LIBNYMEA_EXPORT NetworkDeviceInfos : public QVector<NetworkDeviceInfo>
|
||||
{
|
||||
|
||||
public:
|
||||
explicit NetworkDevices();
|
||||
NetworkDevices(const QList<NetworkDevice> &other);
|
||||
|
||||
NetworkDevices operator<<(const NetworkDevice &networkDevice);
|
||||
explicit NetworkDeviceInfos();
|
||||
NetworkDeviceInfos(const QVector<NetworkDeviceInfo> &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
|
||||
Reference in New Issue
Block a user