Qt/C++ : How to get remote PC (communication peer) MAC address?

前端 未结 3 739
轻奢々
轻奢々 2021-01-07 14:27

I am using Qt5 on Windows 7.
In my application (TCP server), I am currently using some methods from QTcpSocket class:
- QAbstractSocket::peerAddress(

相关标签:
3条回答
  • 2021-01-07 15:05

    Here is the code to get the MAC address of the communication peer.
    Under the hood, it uses the Windows command arp.
    Using Qt5.8, tested on Windows 7:

    QString getMacForIP(QString ipAddress)
    {
        QString MAC;
        QProcess process;
        //
        process.start(QString("arp -a %1").arg(ipAddress));
        if(process.waitForFinished())
        {
            QString result = process.readAll();
            QStringList list = result.split(QRegularExpression("\\s+"));
            if(list.contains(ipAddress))
                MAC = list.at(list.indexOf(ipAddress) + 1);
        }
        //
        return MAC;
    }
    

    Remark: remote peer must be on the same LAN.
    Another remark: you'll get an empty string for MAC if the IP address is not present.

    0 讨论(0)
  • 2021-01-07 15:07

    In general, no, this is not possible, since the communication peer may not even have a MAC address (e.g. if it is using networking hardware that isn't based on Ethernet). In particular, information about MAC addresses is not communicated by the IP, TCP, or UDP layers --- those layers use IP addresses instead. So if you want to find out the peer's MAC address you will need to do that at the application level, by having a program on the peer send it to you.

    (One minor exception to the above: If you are communicating via IPv6 and using self-assigned link-local IPv6 addresses (e.g. fe80::blah), it is possible to derive a computer's MAC address from its self-assigned IPv6 address, because the self-assigned IPv6 address is typically derived from the MAC address and contains the MAC address as a subset of its IPv6 address. [Note this won't work across the Internet since link-local addresses are only useful when both machines are located on the same LAN])

    If the remote peer is on the same LAN as the program's host (and the LAN is an Ethernet LAN), then the program might be using some Windows-specific API to look up the IPAddress<->MACAddress mapping in the machine's ARP table. If the remote peer is elsewhere on the Internet, then I don't know how it could do it.

    0 讨论(0)
  • 2021-01-07 15:17

    If you can run code on remote peer, MAC address can be reported with hardwareAddress() call of the interface.

    For example , to report MAC address of WiFi interface, and all IPv4 addresses on that intrface:

    for(const QNetworkInterface& iface: QNetworkInterface::allInterfaces()){
        if (iface.type() == QNetworkInterface::Wifi){
            qDebug() << "MAC:" << iface.hardwareAddress();
            for (const QHostAddress& addr : iface.allAddresses()){
                if (addr.protocol() == QAbstractSocket::IPv4Protocol)
                    qDebug() << "IPv4 Addr: " << addr;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题