QHostAddress:isInSubnet() always returns false
-
Hello,
I am trying to search for a specific IPv4 address on a VirtualBox Linux VM. No matter what I set the argument parameters to for the isInSubnet() function call, it always returns false. The address I am searching for is 10.0.2.15. Even if I set the first parameter to 10.0.2.15 it still returns false, even though the documentation says it will return true. What gives?
// get a list of all IP addresses on the workstation QList<QHostAddress> listOfAddresses = QNetworkInterface::allAddresses(); QHostAddress workstationIPAddress; for(int i = 0; i<listOfAddresses.count(); i++) { if(!listOfAddresses[i].isLoopback()) { if (listOfAddresses[i].protocol() == QAbstractSocket::IPv4Protocol) { if (listOfAddresses[i].isInSubnet(QHostAddress("10.0.2.0"), QHostAddress("255.255.255.0") .toIPv4Address())) { workstationIPAddress = listOfAddresses[i]; break; } } } }
Thank you
-
Hi,
Which guest OS are you running ?
Did you print the value of each address you test ?
-
This post is deleted!
-
@SGaist
I am running RedHat 8.And yes I did verify each value that is being tested by placing breakpoints and seeing what address passes through each if statement. QNetworkInterface::allAddresses() returns 4 addresses, one of which is the address I am looking for.
-
Then please write a minimal, compilable example with hard-coded addresses so we can try to reproduce it here.
-
Hello,
I am trying to search for a specific IPv4 address on a VirtualBox Linux VM. No matter what I set the argument parameters to for the isInSubnet() function call, it always returns false. The address I am searching for is 10.0.2.15. Even if I set the first parameter to 10.0.2.15 it still returns false, even though the documentation says it will return true. What gives?
// get a list of all IP addresses on the workstation QList<QHostAddress> listOfAddresses = QNetworkInterface::allAddresses(); QHostAddress workstationIPAddress; for(int i = 0; i<listOfAddresses.count(); i++) { if(!listOfAddresses[i].isLoopback()) { if (listOfAddresses[i].protocol() == QAbstractSocket::IPv4Protocol) { if (listOfAddresses[i].isInSubnet(QHostAddress("10.0.2.0"), QHostAddress("255.255.255.0") .toIPv4Address())) { workstationIPAddress = listOfAddresses[i]; break; } } } }
Thank you
@itanitarek10 mask is not "mask"(255.255.255.0), should prefixLength. like linux format.
example: 192.168.0.123/24testCncConnect() { QString lDevIp = RcUtilsGlobal::GetInstance()->getDeviceIP(); QHostAddress lDevAddr; if(!lDevAddr.setAddress(lDevIp)) { LOG_DBG("set dev ip to QHostAddress false, devIP:" + lDevIp); return false; } LOG_DEBUG("DevIP to QHostAddress: " + lDevIp + "->" + lDevAddr.toString()); QList<QNetworkInterface> lInterfaceList = QNetworkInterface::allInterfaces(); QList<QNetworkInterface> lRunningEthernetInterfaces; for(const auto & lInterface: lInterfaceList) { if(lInterface.type() != QNetworkInterface::Ethernet || (lInterface.flags() & QNetworkInterface::IsRunning) == 0) { LOG_DEBUG(QString("Network: %1(%2) is not Ethernet or not Running").arg(lInterface.name()).arg(lInterface.humanReadableName())); continue; } lRunningEthernetInterfaces << lInterface; } for(const auto & lInterface: lRunningEthernetInterfaces) { const QList<QNetworkAddressEntry> & lInteraceAddressEnteries = lInterface.addressEntries(); for(const auto & lNetworkAddressEntry : lInteraceAddressEnteries) { const QHostAddress lIp = lNetworkAddressEntry.ip(); if(lIp.protocol() != QAbstractSocket::NetworkLayerProtocol::IPv4Protocol) continue; const QHostAddress lMask = lNetworkAddressEntry.netmask(); bool bInSub = lDevAddr.isInSubnet(lIp, lNetworkAddressEntry.prefixLength()); LOG_DEBUG(QString("Network: %1(%2) lIp: %3/%4 inThisSub: %5") .arg(lInterface.name()).arg(lInterface.humanReadableName()).arg(lIp.toString()).arg(lNetworkAddressEntry.prefixLength()) .arg(bInSub ? "in": "not")); } } return false; }