views:

98

answers:

2

Is there a way to get the destination MAC address, if the destination IP is known, using ANSI C? I would preferably do it with a system call so the kernel deals with it all and I can take advantage of the ARP cache. I know I can build and send my own ARP request, but then I would not have the caching functionality etc unless I implement it myself so it feels like letting the kernel handling it is the way to go.

+2  A: 

Not an exact answer, because it's not ANSI C, but you can read the arp table from /proc/net/arp (in Linux, that is). That's where arp looks. For any other OS, the easiest way is to use strace or an equivalent on the equivalent arp-cache-showing utility.

mvds
A: 

What I decided to do was to send my own ARP packets and caching it internaly in my application. If the destination is outside my local network I parse /proc/net/route and extract the gateway for the given interface and send an arp packet to the gateway to get it's macaddress (since that is the destination macaddress of packets destined outside the local network).

inquam
Since you apparently have access to `/proc/net`, why take this complex route instead of just looking up the arp table?
mvds
Beacuse in my arp table there were several enteries eth0. 192.168.0.1, 192.168.0.2 and 192.168.0.236. But I actually only have one gateway and that's 192.168.0.1. Since that was the only one listed in /proc/net/route as the default gateway it felt safer to use that since in /proc/net/arp there were no sign to tell which one was the default gateway. In the arp table I even had entries for co-workers computers on the network. What I could have done was to extract the ip to the gateway from /proc/net/route and get the mac from /proc/net/arp.
inquam