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. how to get "preferred" ip4 address?
Forum Updated to NodeBB v4.3 + New Features

how to get "preferred" ip4 address?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 426 Views 2 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.
  • D Offline
    D Offline
    davecotter
    wrote on last edited by
    #1

    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?

    Pl45m4P 1 Reply Last reply
    0
    • D davecotter

      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?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @davecotter

      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.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      4
      • D Offline
        D Offline
        davecotter
        wrote on last edited by davecotter
        #3

        @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);
        			}
        		}
        	}
        }
        
        Pl45m4P 1 Reply Last reply
        0
        • D davecotter

          @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);
          			}
          		}
          	}
          }
          
          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

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


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1

          • Login

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