How to clear arp cache in linux by program, not by using arp command? Are there library functions avaliable to implement this?
=========================================================================== EDIT
In linux, I want to clear arp cache periodically and send ping packets to find hosts in LAN(by collecting arp response and ICMP reply). As some hosts don't reply ping, I try to receive arp response and ICMP reply in my program. But if arp cache has the IP information, it doesn't send arp request for that IP, and the topology may not be complete. So I want to clear arp cache periodically. How can I clear arp cache periodically in my program?Thanks for your time.
It turns out it isn't that bad. You have to:
int sd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
Get a sockaddr of the IP you want to flush from the cache. You can do this with gethostbyname, or a variety of mechanisms. I'll call our address hostaddy
Create an arpreq struct, and make all fields zero except the arp_pa field.
struct arpreq ar;
memset(&ar, 0, sizeof(ar));
memcpy(&ar.arp_pa, hostaddy, sizeof( struct sockaddr_in ) );
ioctl on the socket and structure, with SIOCDARP.int ret = ioctl( sd, SIOCDARP, &ar );
if( ret ) fprintf( stderr, "Failed to clear entry.\n" );
close(sd)Sources: (1) strace arp -d <ip>
(2) https://svn.nmap.org/nmap/libdnet-stripped/src/arp-ioctl.c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With