我知道我们可以使用带有 RTM_F_NOTIFY 标志的 'NETLINK_ROUTE' 套接字来通知用户是否根据 RFC3549 更改路由。但是我找不到一种在 arp 表更改时通知用户的方法。
PS:我用的是linux内核3.0.6
一个应该使用NETLINK_ROUTE
套接字和bind()
组RTNLGRP_NEIGH
。
之后,可以通过 获取ndmsg
通知recv()
。请注意,必须使用所有通知,否则套接字将在溢出后引发ENOBUF
异常。recv()
或使用外部ip(8)
实用程序:
$ ip monitor all # get all the notifications
$ ip monitor neigh # get only arp notifications
或者使用 Python库:
from pyroute2 import IPRoute
from pprint import pprint
ip = IPRoute()
ip.bind() # subscribe to all the events
while True:
pprint(ip.get())
如果我理解你的话 - 我建议你分析“ip”程序的来源。当您运行“ip monitor”并添加或删除任何 ARP 条目时,“ip monitor”将向您显示相应的消息。
我不知道纯 arp 通知,但您可以使用在用户空间中运行的 arpd,它适用于 arp 表非常大的情况。如果您使用 arpd 并对其进行修改,则可以使其向您发送有关 arp 表更改的通知。