如何自动获得IP地址更改通知 [英] how to get notified for IP address changes automatically
问题描述
在linux中,只要IP地址发生任何变化,都可以使用诸如回调或信号之类的任何方法来得到通知. 我想在内核中注册一个回调,或者从内核中获取信号,这是自动完成的,而不是我在套接字上进行代码轮询.
In linux is there any method like callback or signal to get notified whenever there is any change in the IP Address. I want to register a callback with with kernel or get a signal from kernel which is done automatically rather my code polling on the socket.
提前谢谢.
推荐答案
您会通过netlink套接字从内核接收通知.
You receive notifications from the kernel via netlink sockets.
您将需要创建一个NETLINK_ROUTE
套接字,并通过bind()
将其预订为RTMGRP_IPV4_IFADDR
组,以将其订阅IP更改.然后,您将收到类型为RTM_NEWADDR
和RTM_DELADDR
的网络链接消息,其路由属性为IFA_LOCAL
或IFA_ADDRESS
.
You would need to create a NETLINK_ROUTE
socket and subscribe it to IP changes via bind()
ing it to the RTMGRP_IPV4_IFADDR
group. Then, you'll receive netlink messages of type RTM_NEWADDR
and RTM_DELADDR
with a route attribute of IFA_LOCAL
or IFA_ADDRESS
.
这篇关于如何自动获得IP地址更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!