0
votes

I am trying to get MAC address for all physical ports available on my laptop for Windows/Linux/MAC OS. I also want to check if I can detect physical connection/disconnection of the network cable.

I tried using QNetworkInterface APIs but it is providing me all physical and logical interfaces. I am unable to get how I can differentiate the physical ports. Do we have any flags to do so?

I have tried some code

    foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
    {
        if (!(netInterface.flags() & QNetworkInterface::Ethernet))
        {
            return netInterface.hardwareAddress();
        }
    }

Following is the result of Network Interface, name, type and hardware address from my MAC

(IsUp|IsRunning|CanBroadcast|CanMulticast) "en0" QNetworkInterface::Wifi "XX:XX:XX:XX:XX:XX"
(IsUp|IsRunning|CanBroadcast|CanMulticast) "p2p0" QNetworkInterface::Wifi "XX:XX:XX:XX:XX:XX"
(IsUp|IsRunning|CanBroadcast|CanMulticast) "awdl0" QNetworkInterface::Wifi "XX:XX:XX:XX:XX:XX"
(IsUp|IsRunning|CanBroadcast|CanMulticast) "en1" QNetworkInterface::Ethernet "XX:XX:XX:XX:XX:XX"
(IsUp|IsRunning|CanBroadcast|CanMulticast) "en2" QNetworkInterface::Ethernet "XX:XX:XX:XX:XX:XX"
(IsUp|IsRunning|CanBroadcast|CanMulticast) "bridge0" QNetworkInterface::Unknown "XX:XX:XX:XX:XX:XX"

PS: "XX:XX:XX:XX:XX:XX" is their MAC address

3

3 Answers

0
votes
0
votes

I got a solution. After doing lot of analysis, I come to the conclusion that Qt can not get deeper OS details. We need to touch OS layer to achieve the same.

  1. For Windows -
MIB_IF_ROW2 ifRow; 
ifRow.InterfaceAndOperStatusFlags.ConnectorPresent

check this flag to achieve the same.

  1. For MAC OS
static_cast<SCNetworkInterfaceRef>(CFArrayGetValueAtIndex(physicalVLANInterfaces, i)) for all CFIndex
  1. For Linux
File *fp = popen("lshw -class network | grep -A 1 \"bus info\" | grep name | awk -F': ' '{print $2}'", "r");

Not sure if there is some better solution than this. But this works best for me.