Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving MAC address for IP address using C++ on Linux

Tags:

linux

arp

I need to generate an Ethernet header that includes the destination MAC address, (since libnfnetlink gives only the IP header before prerouting takes place), the outgoing interface number is also known, so the lookup can be made in the correct network.

What's the library/function to resolve the MAC address from an IP address?

like image 268
kagali-san Avatar asked Dec 18 '10 23:12

kagali-san


People also ask

Can you resolve an IP from a MAC address?

Can you find an IP address from a MAC address? Yes. Open a Command Prompt window and enter the command arp -a. The output shows all of the IP addresses that are active on your network.

What resolves MAC addresses with IP addresses?

ARP is the Address Resolution Protocol, used to translate between Layer 2 MAC addresses and Layer 3 IP addresses. ARP resolves IPs to MAC addresses by asking, “Who has IP address 192.168. 2.140, tell me.” An example of an ARP reply is “192.168.


1 Answers

It's unclear why you need the MAC address, since that's usually handled for you at a lower level.

However, assuming your target is on your local Ethernet segment, you can use the arp command to look up values in the local cache. If the value is not cached... Well, that's a problem. Perhaps arping would help...

(Normally you'd send a packet to, for example, IP address 10.10.10.10, and your system would send an ARP packet out querying who-has 10.10.10.10, and a response would come back from that target system with its MAC address, and then it would be cached. (You can watch this happening with tcpdump.) Or when a system comes on line it would send out a broadcast message informing everyone else of its MAC address. Naturally, if your destination is on another Ethernet segment, you're routing to a gateway rather than directly to the destination, and no destination-MAC address is available.)

You might read further at:

  • http://linux.die.net/man/8/arp
  • http://linux.die.net/man/8/arping
  • http://linux.die.net/man/7/arp
  • http://www.kernel.org/doc/man-pages/online/pages/man7/arp.7.html
like image 179
Mr.Ree Avatar answered Nov 03 '22 20:11

Mr.Ree