Obtain the MAC address through ARP

Source: Internet
Author: User

ARP is used to convert the IP address and MAC address. It specifies the IP address in the LAN where the MAC address is to be obtained in the ARP packet and then sends it to the LAN, the machine with the specified IP address receives and returns an ARP packet, and specifies its MAC address in the returned packet.

In this way, the Mac is obtained. The MAC address of the local machine can also be obtained in this way.

The following is code implementation. For convenience, I directly used the sendarp function, instead of encapsulating ARP packets at the underlying layer.

* Obtain the MAC address with the IP address ipdst in the LAN and save the result in the bydstmac parameter.
* Whether the return operation is successful
Template <int nsize>
Bool getmacaddress (ipaddr ipdst, byte (& bydstmac) [nsize])
Assert (nsize> = 6 );

Memset (bydstmac, 0, nsize );

DWORD dwbufferlen = nsize;

DWORD dwret =: sendarp (ipdst, 0, (Pulong) & bydstmac, & dwbufferlen );

Return dwret = no_error;

This function is used as follows:

// Example
Byte bydstmac [6];
If (getmacaddress (: inet_addr (""), bydstmac ))
Cstring Strmac;
Strmac. format (_ T ("%. 2X-%. 2X-%. 2X-%. 2X-%. 2X-%. 2x "), bydstmac [0], bydstmac [1], bydstmac [2], bydstmac [3], bydstmac [4], bydstmac [5]);
Afxmessagebox (Strmac );


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.