Linux中通过C程序获取网络接口类型 [英] Get the network interface type in Linux via C program

查看:74
本文介绍了Linux中通过C程序获取网络接口类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Linux 中的 C 程序获取网络接口的类型名称?

我尝试使用 getifaddr 访问网络接口,我得到了我需要的一切,除了它的类型名称.有什么想法可以做到吗?

我还是 Linux 的新手,因此对任何解决方法或示例代码的任何评论都将不胜感激.

编辑

详细说明问题.基本上,在执行 ifconfig 时,我可以得到这样的结果:

<前>eth0 链接封装:以太网 HWaddr 00:0C:29:40:93:9Cinet 地址:192.168.154.102 广播:192.168.154.255 掩码:255.255.255.0inet6 地址:fe80::20c:29ff:fe40:939c/64 范围:链接上行广播运行多播 MTU:1500 公制:1RX 数据包:14785 错误:0 丢弃:0 溢出:0 帧:0TX 数据包:429 错误:0 丢弃:0 溢出:0 载波:0碰撞:0 txqueuelen:1000RX 字节:961439 (938.9 KiB) TX 字节:71866 (70.1 KiB)中断:67 基地址:0x2000

我的理解是 eth0 是接口名称.虽然可以有多个网络接口(eth0、eth1、eth2)和多个虚拟接口(tap0、tap1 等)和无线接口,但它们都是以太网接口.

Loopbacks 也一样,lo 作为接口名,它的接口是Local Loopback.

那么问题是,我如何使用 C 程序打印 eth0,tap0,wlan0 属于以太网类型,并且 lo ->到本地环回类型,当我事先不知道使用了哪些接口以及它们在机器中的数量.

希望我能正确解释这一点.

解决方案

您可以尝试使用 Netlink 的 RTNETLINK(1,2).有关示例,请参见使用 RTNETLINK 操作网络环境.

How can I get the type name for a network interface from C program in Linux?

I tried using getifaddr to access network interfaces, and I got everything I needed, except its type name. Any ideas how it could be done?

I'm still new to Linux, so any comments on any workarounds or example codes would be appreciated.

Edit

To elaborate the question. Basically, while doing ifconfig I can get something like this:

eth0 Link encap:Ethernet HWaddr 00:0C:29:40:93:9C
inet addr:192.168.154.102 Bcast:192.168.154.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe40:939c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:14785 errors:0 dropped:0 overruns:0 frame:0
TX packets:429 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:961439 (938.9 KiB) TX bytes:71866 (70.1 KiB)
Interrupt:67 Base address:0x2000

Here is what i understood that eth0 is interface name. While there can be several network interfaces (eth0, eth1, eth2) and several virtual interfaces (tap0, tap1, etc) and wireless ones, all of them are Ethernet interfaces.

The same for loopbacks, being lo as interface name, its interface is Local Loopback.

So the question is, how can i, using C program, print that eth0,tap0,wlan0 belong to type of Ethernet, and lo -> to Local Loopback type, when i don`t know beforehand what interfaces are used and how many of them are in the machine.

Hope I could explain this correctly.

解决方案

You might try to use RTNETLINK from Netlink(1,2). See Manipulating the Networking Environment Using RTNETLINK for an exemple.

这篇关于Linux中通过C程序获取网络接口类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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