Unsolved how to get "preferred" ip4 address?
-
on macOS, in the Network system pref, there is a notion of "Service Order", or "priority order" of the network interfaces. I'm sure other OSs also have a notion of NIC order.
When i query
QNetworkInterface::allInterfaces()
i can see that they DO NOT come in the order the user has set in the system prefs.How do i get the correct order?
-
It's called
interface metric
on Windows. A higher number equals lower priority, but AFAIK this property is not available in Qt - at least not directly.
You can get interfaces by their names or indices (may change).A solution could be to iterate over all interfaces and filter the result by using
QHostAddress
. -
@Pl45m4 said in how to get "preferred" ip4 address?:
A solution could be to iterate over all interfaces and filter the result by using
QHostAddress
.i'm doing that already, the problem is the host has multiple active addresses, but there's no way to determine user-configured service order.
foreach (const QNetworkInterface& netInterface, QNetworkInterface::allInterfaces()) { QNetworkInterface::InterfaceFlags interfaceFlags(netInterface.flags()); QNetworkInterface::InterfaceType interfaceType(netInterface.type()); bool validB(netInterface.isValid()); QString nameStr(netInterface.name()); QString humanStr(netInterface.humanReadableName()); if ( validB && (bool)(interfaceFlags & QNetworkInterface::IsRunning) && (bool)(interfaceFlags & QNetworkInterface::IsUp) && !(bool)(interfaceFlags & QNetworkInterface::IsLoopBack) && !(bool)(interfaceFlags & QNetworkInterface::IsPointToPoint) && interfaceType != QNetworkInterface::Virtual && !nameStr.contains("vnic") && !humanStr.contains("VM") ) { foreach (const QNetworkAddressEntry &curAddr, netInterface.addressEntries()) { QHostAddress hostAddr(curAddr.ip()); bool linkLocalB(hostAddr.isLinkLocal()); bool nullB(hostAddr.isNull()); if ( !nullB && !linkLocalB && hostAddr.protocol() == QAbstractSocket::IPv4Protocol ) { QString ipStr(curAddr.ip().toString()); addressVec.push_back(ipStr); } } } }
-
@davecotter said in how to get "preferred" ip4 address?:
but there's no way to determine user-configured service order.
I guess, yes. It seems that you can not access this value.
Probably there are other libs or extensions for that, but then you have to deal with multi platform issues, since they are mostly for one platform only, if there are any.