当前位置: 技术问答>linux和unix
求助 用程序读取ARP缓存
来源: 互联网 发布时间:2016-10-22
本文导语: 如题,没有找到能够直接读取ARP缓存的库函数或系统调用,难道要直接读文件?请众高手指教。。。。 | 用ioctl可以实现,详情见链接 http://blog.chinaunix.net/u2/76292/showart_2158555.html http://blog.csdn.ne...
如题,没有找到能够直接读取ARP缓存的库函数或系统调用,难道要直接读文件?请众高手指教。。。。
|
用ioctl可以实现,详情见链接
http://blog.chinaunix.net/u2/76292/showart_2158555.html
http://blog.csdn.net/fyang2007/archive/2010/04/20/5506432.aspx
以下是抄来的代码
http://blog.chinaunix.net/u2/76292/showart_2158555.html
http://blog.csdn.net/fyang2007/archive/2010/04/20/5506432.aspx
以下是抄来的代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEV_NAME "eth0"
//#define SIOCDARP 0x8953 /* delete ARP table entry */
//#define SIOCGARP 0x8954 /* get ARP table entry */
//#define SIOCSARP 0x8955 /* set ARP table entry */
static int sd;
/* Delete an entry from the ARP cache. */
static int arp_del(char* ip)
{
struct arpreq arpreq;
struct sockaddr_in *sin;
struct in_addr ina;
int rc;
printf("del arp entry for IP : %sn", ip);
/*you must add this becasue some system will return "Invlid argument"
because some argument isn't zero*/
memset(&arpreq, 0, sizeof(struct arpreq));
sin = (struct sockaddr_in *) &arpreq.arp_pa;
memset(sin, 0, sizeof(struct sockaddr_in));
sin->sin_family = AF_INET;
ina.s_addr = inet_addr(ip);
memcpy(&sin->sin_addr, (char *)&ina, sizeof(struct in_addr));
strcpy(arpreq.arp_dev, DEV_NAME);
rc = ioctl(sd, SIOCDARP, &arpreq);
if (rc sin_family = AF_INET;
ina.s_addr = inet_addr(ip);
memcpy(&sin->sin_addr, (char *)&ina, sizeof(struct in_addr));
strcpy(arpreq.arp_dev, DEV_NAME);
rc = ioctl(sd, SIOCGARP, &arpreq);
if (rc