Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QHostAddress:isInSubnet() always returns false
Forum Updated to NodeBB v4.3 + New Features

QHostAddress:isInSubnet() always returns false

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 731 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    itanitarek10
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Which guest OS are you running ?

      Did you print the value of each address you test ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      I 1 Reply Last reply
      0
      • I Offline
        I Offline
        itanitarek10
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Which guest OS are you running ?

          Did you print the value of each address you test ?

          I Offline
          I Offline
          itanitarek10
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Then please write a minimal, compilable example with hard-coded addresses so we can try to reproduce it here.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • I itanitarek10

              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

              J Offline
              J Offline
              Jerry.Liu
              wrote on last edited by
              #6

              @itanitarek10 mask is not "mask"(255.255.255.0), should prefixLength. like linux format.
              example: 192.168.0.123/24

              testCncConnect() {
                  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;
              }
              
              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved