在最近的工作中,由于需要在路由器上检测局域网所有连接的电脑。于是想到一个办法,监控arp缓存。有2个方法。
- 从内核入手,通过修改邻居子系统(通过修改net/ipv4/arp.c) arp_tbl改变后自己通过 netlink 放通知。
- 第二个方法 通过监控 /proc/net/arp 的改变。
但由于 /proc 文件系统是一类特殊的文件系统。inotify无法监控,也是做了后才知道。权当学习 inotify的用法吧,将这段代码贴出来做个记号。
如果哪位仁兄有更好的办法,还望不吝赐教~!
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/inotify.h>
#include <errno.h>
#define INOTIFY_BUF_LEN (sizeof(struct inotify_event) + 1024)
int main(int argc,char * argv[])
{
char buf[INOTIFY_BUF_LEN];
int wd, fd = inotify_init();
size_t i = 0,len = 0;
if(-1 == fd){
printf("inotify_init failed.\n");
return fd;
}
wd = inotify_add_watch(fd, "/proc/net/arp",IN_MODIFY|IN_DELETE_SELF);
if(-1 == wd){
printf("inotify_add_watch %s failed\n", "/proc/net/arp");
close(fd);
return -1;
}
while(true){
do{
size_t len = read(fd,buf, INOTIFY_BUF_LEN);
}while(len < 0 && errno == EINTR);
if(len < 0)
goto out;
i = 0;
while(i < len)
{
struct inotify_event * event = (struct inotify_event *)&buf[i];
if(event->mask & IN_DELETE_SELF) // 文件被删除.
goto out;
if(event->mask & IN_MODIFY)
{
// arp 文件被改变.
printf("Arp file Changed [%s].", event->name);
}
i += sizeof(struct inotify_event) + event->len;
}
}
out:
inotify_rm_watch(fd, wd);
close(fd);
return 0;
}