Search code examples
clinuxnetwork-programmingrouterarp

Get Ip address of a local device from its Mac address


I have a local device connected to the router and I can access its Mac address through already existing code. How can I find the IP address of the device using this Mac address in c?

"arp" might be providing the details, but I need it IP address independent from that protocol.


Solution

  • If your requirements allow you to make use of ARP indirectly (that is, by relying on standard networking behavior rather than coding ARP queries yourself)...

    Your best answer is probably to find the MAC address in your local ARP cache, as exposed in /proc/net/arp. Of course, there's only going to be data there for your device if your host has recently seen traffic from it on the network.

    This answer has some suggestions for dealing with that situation: basically, run some sort of network scan to generate traffic to every ip address on your network, and then mine your local ARP table for the answer.