检测Linux中的网络连接类型
如何在 C++ Linux 应用程序中检测网络连接类型,例如是有线还是 Wi-Fi?
如果设备有多个网络接口,我想检测正在使用的接口的连接类型。
谢谢。
How can I detect the network connection type, e.g., whether it is wired or Wi-Fi, in a C++ Linux application?
If the device has multiple network interfaces, I would like to detect the connection type for the interface being used.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也一直在寻找这个答案。我在 opensuse gitorious 中发现了一段有趣的代码。显然,他们使用下面的代码来获取接口类型:
我仍然需要找到官方文档来确认 /sys/class/net/$IF/type 中的值意味着什么,但是这个函数已经解释了很多。
编辑:好的,我已经阅读了更多关于 sysfs 的内容,发现这一点是一件令人痛苦的事情。我没有找到任何合适的文档。
您可能知道,这些信息是从内核中提取的,以便在用户空间中显示。因此,我最终查看了 sysfs 的源代码和内核,以了解这个“类型”属性是什么。我相信部分答案应该在 net-sysfs 中找到。 c,也在 linux/device.h 中。我只是不明白这些东西是如何连接的。当我看到我需要理解所有这些宏时,我停了下来......
I have been looking for that answer, too. I found an interesting piece of code in opensuse gitorious. Apparently, they use the following piece of code to get the interface type:
I still have to find official documentation to confirm what the value in /sys/class/net/$IF/type means, but this function already explains a lot.
EDIT: ok, I've read about sysfs a little more, and finding that out is a pain in the ass. I've not found any proper documentation.
As you may know, this information is pulled from the kernel in order to be presented in userspace. So I ended up looking in the sources of sysfs and in the kernel in order to understand what is this "type" attribute. I believe that part of the answer should be found in net-sysfs.c, and also in linux/device.h. I just can't figure out how this stuff is connected. I stopped when I saw that I needed to understand all these macros...
如果该接口存在于 /proc/net/wireless 中,则它是无线接口。否则,就不是。
If the interface is present in /proc/net/wireless, it is a wireless interface. Otherwise, it is not.
感谢您的所有投入。
我提出的解决方案:
从
/proc/net/dev 获取所有活动接口的名称和 ip 地址
通过映射使用的ip地址获取当前接口
通过查看<来检查当前接口是否是无线的code>/proc/net/wireless
Thanks for all the inputs.
The solution I come up:
get all active interfaces' names and ip addresses from
/proc/net/dev
get the current interface by mapping the ip address used
Check whether the current interface is wireless by looking at
/proc/net/wireless
根据所使用的接口,eth 或 wlan。
By the interface which is used, eth or wlan.