收到通知的在Linux上的网络接口的改变 [英] Get notified about network interface change on Linux

查看:126
本文介绍了收到通知的在Linux上的网络接口的改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法,当一个网络接口启用或禁用通知我的用户空间应用程序。我希望能做到这一点,而不诉诸投票。请问内核提供某种形式的挂钩时,对网络相关事件发生时触发回调函数?

I need a way to notify my user space app when a network interface is enabled or disabled. I'm hoping to do this without resorting to polling. Does the kernel offer some sort of hook for triggering callback functions when network-related events occur?

推荐答案

我相信网络链路(人7 netlink的)功能提供了通过<$ C有关网络接口的信息$ C> NETLINK_ROUTE 家庭(男子7的rtnetlink )。您可以到选择()上的网络连接套接字来获得您想要的信息调查()。我不能确定这一点,虽然,我没有用它自己。

I believe the netlink (man 7 netlink) facility provides information about network interfaces via the NETLINK_ROUTE family (man 7 rtnetlink). You may be able to select() or poll() on a netlink socket to get the information you want. I'm not certain of this, though; I haven't used it myself.

目前更高的水平,如果在系统运行 NetworkManager的时,那将广播通过D-总线时系统的事件网络状态的变化。主显浏览器使用这些事件,例如,当系统失去其网络连接到自动激活工作离线模式,并且当网络连接恢复切换回在线模式。有对各种语言的D-Bus客户端库,它是特定于平台的netlink的比少,所以这是我推荐使用。

At a higher level, if the system is running NetworkManager, that'll broadcast events via D-Bus when the system's network status changes. The Epiphany browser uses these events, for example, to automatically activate "Work Offline" mode when the system loses its network connection, and switch back to online mode when network connectivity resumes. There are D-Bus client libraries for a variety of languages, and it's less platform-specific than netlink, so this is what I'd recommend using.

这篇关于收到通知的在Linux上的网络接口的改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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