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.
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.
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).
精彩评论