如何自动获得IP地址更改通知 [英] how to get notified for IP address changes automatically

查看:400
本文介绍了如何自动获得IP地址更改通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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_NEWADDRRTM_DELADDR的网络链接消息,其路由属性为IFA_LOCALIFA_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆