用于接收网络接口 up/down 接口名称通知的 C++ 库 Ubuntu

C++ library for receiving network interface up/down notification with interface name on Ubuntu

这个post有两个问题。 1. network interface up/down 特定网络设备上的通知 2.列出网络设备。

下面是我提供的答案。 (感谢 Velkan 帮助我达成此目标

我正在寻找一个 c++ 库,它在 Ubuntu 机器上接收带有接口名称的网络接口 up/down 事件通知。我的同事要求我为此使用 dbus 接口。使用下面的 link 相同,它只通知任何没有接口名称的接口 up/down 事件。

https://cgit.freedesktop.org/NetworkManager/NetworkManager/tree/examples/C/glib/monitor-nm-state-gdbus.c

我在谷歌上搜索了很长一段时间,我找不到合适的例子,甚至 dbus 文档也不是直截了当的,或者没有提供我正在寻找的信息。

在这方面的任何帮助都会非常有帮助。

提前致谢

我用 d-feet 实用程序看了一点。

在 'System Bus' 上有一个带有 DeviceAdded 信号的 /org/freedesktop/NetworkManager

/org/freedesktop/NetworkManager/Devices/1东西有:

  • Interface org.freedesktop.NetworkManager.Device
  • 中的字符串
  • Carrier 布尔值和 PropertiesChanged 信号在 org.freedesktop.NetworkManager.Device.Wiredorg.freedesktop.NetworkManager.Device.Bridgeorg.freedesktop.NetworkManager.Device.Adsl...

所以,获取NetworkManager对象,监听设备列表的修改,订阅当前设备的PropertiesChanged和将要出现的设备,观察Carrier属性.

如果您想检测接口已启动但未分配 IP(或者可能处于其他中间状态),请查看其他属性。

如果您使用的是 Ubuntu 的服务器版本(您不是),那么我认为它不使用 NetworkManager,因此它的 D-Bus 接口不太可用 - org.freedesktop.network1.

Shell DeviceAdded/DeviceRemoved 示例:

$ dbus-monitor --system "type='signal',interface='org.freedesktop.NetworkManager'"

插入 Wi-Fi U 盘时:

signal time=1509615532.227067 sender=:1.8 -> destination=(null destination) serial=2051 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=DeviceAdded
   object path "/org/freedesktop/NetworkManager/Devices/10"
signal time=1509615532.228150 sender=:1.8 -> destination=(null destination) serial=2056 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=PropertiesChanged
   array [
      dict entry(
         string "AllDevices"
         variant             array [
               object path "/org/freedesktop/NetworkManager/Devices/0"
               object path "/org/freedesktop/NetworkManager/Devices/1"
               object path "/org/freedesktop/NetworkManager/Devices/2"
               object path "/org/freedesktop/NetworkManager/Devices/3"
               object path "/org/freedesktop/NetworkManager/Devices/4"
               object path "/org/freedesktop/NetworkManager/Devices/5"
               object path "/org/freedesktop/NetworkManager/Devices/6"
               object path "/org/freedesktop/NetworkManager/Devices/8"
               object path "/org/freedesktop/NetworkManager/Devices/10"
            ]
      )
      dict entry(
         string "Devices"
         variant             array [
               object path "/org/freedesktop/NetworkManager/Devices/0"
               object path "/org/freedesktop/NetworkManager/Devices/1"
               object path "/org/freedesktop/NetworkManager/Devices/2"
               object path "/org/freedesktop/NetworkManager/Devices/3"
               object path "/org/freedesktop/NetworkManager/Devices/4"
               object path "/org/freedesktop/NetworkManager/Devices/5"
               object path "/org/freedesktop/NetworkManager/Devices/6"
               object path "/org/freedesktop/NetworkManager/Devices/8"
               object path "/org/freedesktop/NetworkManager/Devices/10"
            ]
      )
   ]
signal time=1509615533.253596 sender=:1.8 -> destination=(null destination) serial=2072 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=PropertiesChanged
   array [
      dict entry(
         string "WirelessEnabled"
         variant             boolean false
      )
   ]
signal time=1509615533.287235 sender=:1.8 -> destination=(null destination) serial=2080 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=PropertiesChanged
   array [
      dict entry(
         string "ActiveConnections"
         variant             array [
               object path "/org/freedesktop/NetworkManager/ActiveConnection/3"
               object path "/org/freedesktop/NetworkManager/ActiveConnection/2"
               object path "/org/freedesktop/NetworkManager/ActiveConnection/1"
               object path "/org/freedesktop/NetworkManager/ActiveConnection/0"
            ]
      )
   ]

几个信号被触发。因此,如果我们订阅了 DeviceAddedPropertiesChanged,我们可以推断出我们也需要观看设备 #10。

删除时间:

signal time=1509615612.892305 sender=:1.8 -> destination=(null destination) serial=2086 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=DeviceRemoved
   object path "/org/freedesktop/NetworkManager/Devices/10"
signal time=1509615612.893697 sender=:1.8 -> destination=(null destination) serial=2089 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=PropertiesChanged
   array [
      dict entry(
         string "ActiveConnections"
         variant             array [
               object path "/org/freedesktop/NetworkManager/ActiveConnection/3"
               object path "/org/freedesktop/NetworkManager/ActiveConnection/2"
               object path "/org/freedesktop/NetworkManager/ActiveConnection/1"
               object path "/org/freedesktop/NetworkManager/ActiveConnection/0"
            ]
      )
      dict entry(
         string "AllDevices"
         variant             array [
               object path "/org/freedesktop/NetworkManager/Devices/0"
               object path "/org/freedesktop/NetworkManager/Devices/1"
               object path "/org/freedesktop/NetworkManager/Devices/2"
               object path "/org/freedesktop/NetworkManager/Devices/3"
               object path "/org/freedesktop/NetworkManager/Devices/4"
               object path "/org/freedesktop/NetworkManager/Devices/5"
               object path "/org/freedesktop/NetworkManager/Devices/6"
               object path "/org/freedesktop/NetworkManager/Devices/8"
            ]
      )
      dict entry(
         string "Devices"
         variant             array [
               object path "/org/freedesktop/NetworkManager/Devices/0"
               object path "/org/freedesktop/NetworkManager/Devices/1"
               object path "/org/freedesktop/NetworkManager/Devices/2"
               object path "/org/freedesktop/NetworkManager/Devices/3"
               object path "/org/freedesktop/NetworkManager/Devices/4"
               object path "/org/freedesktop/NetworkManager/Devices/5"
               object path "/org/freedesktop/NetworkManager/Devices/6"
               object path "/org/freedesktop/NetworkManager/Devices/8"
            ]
      )
   ]
signal time=1509615612.910829 sender=:1.8 -> destination=(null destination) serial=2095 path=/org/freedesktop/NetworkManager; interface=org.freedesktop.NetworkManager; member=PropertiesChanged
   array [
      dict entry(
         string "WirelessEnabled"
         variant             boolean true
      )
   ]

同样的信号:#10 消失了。

或订阅 DeviceAddedDeviceRemoved 以仅获取它们:

$ dbus-monitor --system type=signal,interface=org.freedesktop.NetworkManager,member=Device{Added,Removed}

任何想要工作代码示例的人,可以替换下面给出的类似行,在问题中分享的 link (https://cgit.freedesktop.org/NetworkManager/NetworkManager/tree/examples/C/glib/monitor-nm-state-gdbus.c)。

用于在特定设备上侦听 up/down 事件的代码片段 在这里,我刚刚将对象路径参数值更改为

  • /org/freedesktop/NetworkManager/Devices/0
  • /org/freedesktop/NetworkManager/Devices/1

    代理 = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, 旗帜, 空,/* GDBusInterfaceInfo / "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Devices/1", "org.freedesktop.NetworkManager.Device", NULL, / GCancellable */ &error);

列出可用设备的代码片段。

proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                       flags,
                                       NULL, /* GDBusInterfaceInfo */
                                       "org.freedesktop.NetworkManager",
                                       "/org/freedesktop/NetworkManager",
                                       "org.freedesktop.NetworkManager",
                                       NULL, /* GCancellable */
                                       &error);

ret = g_dbus_proxy_call_sync (proxy,
                              "GetDevices",
                              NULL,
                              G_DBUS_CALL_FLAGS_NONE, -1,
                              NULL, &error);
if (!ret) {
    g_dbus_error_strip_remote_error (error);
    g_print ("ListConnections failed: %s\n", error->message);
    g_error_free (error);
    return;
}

g_variant_get (ret, "(^ao)", &paths);
g_variant_unref (ret);

for (i = 0; paths[i]; i++)
    g_print ("%s\n", paths[i]);

网络设备列表的输出(我的机器中有 2 个网卡)

/org/freedesktop/NetworkManager/Devices/0

/org/freedesktop/NetworkManager/Devices/1