Var*_*ári 3 linux kernel-module network-interface
我正在编写一个内核模块,需要有关本地机器接口的信息,就像通过一个简单的'ifconfig'命令重新调整的那些,我已经搜索了很多,但找不到任何东西
您可以通过这种struct net_device或那种方式获得所有这些信息.正如Albert Veli所说,你可以struct net_device使用这个指针__dev_get_by_name().
如果您告诉我们您需要哪些信息,我们甚至可以指出您正确的字段.
查找MAC地址非常简单:
struct net_device *dev = __dev_get_by_name("eth0");
dev->dev_addr; // is the MAC address
dev->stats.rx_dropped; // RX dropped packets. (stats has more statistics)
Run Code Online (Sandbox Code Playgroud)
找到IP地址相当困难,但并非不可能:
struct in_device *in_dev = rcu_dereference(dev->ip_ptr);
// in_dev has a list of IP addresses (because an interface can have multiple)
struct in_ifaddr *ifap;
for (ifap = in_dev->ifa_list; ifap != NULL;
ifap = ifap->ifa_next) {
ifap->ifa_address; // is the IPv4 address
}
Run Code Online (Sandbox Code Playgroud)
(这些都没有经过编译测试,因此可以进行拼写错误.)