mirror of
https://github.com/YACReader/yacreader
synced 2025-07-20 05:54:39 -04:00
Move network interface filtering to separate file
This commit is contained in:
33
YACReaderLibrary/ip_config_helper.cpp
Normal file
33
YACReaderLibrary/ip_config_helper.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include <QNetworkInterface>
|
||||
#include "ip_config_helper.h"
|
||||
#include "qnaturalsorting.h"
|
||||
|
||||
// 192.168 (most comon local subnet for ips are always put first)
|
||||
// IPs are sorted using natoral sorting
|
||||
|
||||
QList<QString> getIpAddresses()
|
||||
{
|
||||
auto ipComparator = [](const QString &ip1, const QString &ip2) {
|
||||
if (ip1.startsWith("192.168") && ip2.startsWith("192.168"))
|
||||
return naturalSortLessThanCI(ip1, ip2);
|
||||
|
||||
if (ip1.startsWith("192.168"))
|
||||
return true;
|
||||
|
||||
if (ip2.startsWith("192.168"))
|
||||
return false;
|
||||
|
||||
return naturalSortLessThanCI(ip1, ip2);
|
||||
};
|
||||
|
||||
QList<QString> addresses;
|
||||
for (auto add : QNetworkInterface::allAddresses()) {
|
||||
// Exclude loopback, local, multicast
|
||||
if (add.isGlobal()) {
|
||||
addresses.push_back(add.toString());
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(addresses.begin(), addresses.end(), ipComparator);
|
||||
return addresses;
|
||||
}
|
Reference in New Issue
Block a user