This repository has been archived on 2026-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
powersync-core/libnymea/network/networkutils.cpp
Simon Stürz c59185672f Add mac address class and unit tests
Finish updated network discovery
Improve ARP and monitor handling
Introduce network device info cache housekeeping
2022-04-13 11:08:29 +02:00

46 lines
1.3 KiB
C++

#include "networkutils.h"
NetworkUtils::NetworkUtils()
{
}
QNetworkInterface NetworkUtils::getInterfaceForHostaddress(const QHostAddress &address)
{
foreach (const QNetworkInterface &networkInterface, QNetworkInterface::allInterfaces()) {
foreach (const QNetworkAddressEntry &entry, networkInterface.addressEntries()) {
// Only IPv4
if (entry.ip().protocol() != QAbstractSocket::IPv4Protocol)
continue;
if (address.isInSubnet(entry.ip(), entry.prefixLength())) {
return networkInterface;
}
}
}
return QNetworkInterface();
}
QNetworkInterface NetworkUtils::getInterfaceForMacAddress(const QString &macAddress)
{
foreach (const QNetworkInterface &networkInterface, QNetworkInterface::allInterfaces()) {
if (networkInterface.hardwareAddress().toLower() == macAddress.toLower()) {
return networkInterface;
}
}
return QNetworkInterface();
}
QNetworkInterface NetworkUtils::getInterfaceForMacAddress(const MacAddress &macAddress)
{
foreach (const QNetworkInterface &networkInterface, QNetworkInterface::allInterfaces()) {
if (MacAddress(networkInterface.hardwareAddress()) == macAddress) {
return networkInterface;
}
}
return QNetworkInterface();
}