linux下网络接口名ifname与接口索引ifindex查询

相关函数接口信息 :

#include <net/if.h>

unsigned if_nametoindex(const char *ifname);
char *if_indextoname(unsigned ifindex, char *ifname);
struct if_nameindex *if_nameindex(void);
void if_freenameindex(struct if_nameindex *ptr);
if_nametoindex():指定网络接口名称字符串作为参数;若该接口存在,则返回相应的索引,否则返回0

if_indextoname():指定网络接口索引以及一块长度至少为IF_NAMESIZE(16)字节的内存区域作为参数;若索引对应的网络接口存在,则在内存区域中返回该接口的名称字符串,否则返回NULL,并将errno设置为相应的值

if_nameindex():返回动态分配的struct if_nameindex结构数组,数组中的每一个元素分别对应一个本地网络接口;struct if_nameindex结构的if_index字段为接口索引,if_name字段为接口名称字符串;索引为0且名称字符串为NULL表示结构数组的末尾;调用出错时,返回NULL,并将errno设置为相应的值

if_freenameindex():通过if_nameindex()获取完毕接口名称与索引后,调用该函数以释放动态分配的内存区域

以上4个函数在系统的man文档中都可以查看相应的描述,且都是POSIX标准支持的,Linux内核可能未实现这些函数,或已实现但不同于POSIX标准。这些函数的原型声明与定义并未出现在CentOS 6.7的定制内核2.6.32-573.26.1.el6.x86_64以及原版内核2.6.32.5中,而是由系统的glibc-2.12实现:在glibc-2.12.2源码树中,函数的原型声明位于sysdeps/gnu/net/if.h与sysdeps/generic/net/if.h,函数的定义位于sysdeps/unix/sysv/linux/if_index.c中,本质上是对ioctl(2)的SIOCGIFNAME,SIOCGIFCONF,SIOCGIFINDEX等操作以及netlink套接字进行了封装

示例程序 :

1.if_name_index.c 示例:

int main(void)
{
    struct if_nameindex *head, *ifni;
    ifni = if_nameindex();
    head = ifni;
    
    if (head == NULL) {
        perror("if_nameindex()");
        exit(EXIT_FAILURE);
    }   
    
    while (ifni->if_index != 0) {
        printf("Interfece %d : %s\n", ifni->if_index, ifni->if_name);
        ifni++;
    }   
    
    if_freenameindex(head);
    head = NULL;
    ifni = NULL;
    
    exit(EXIT_SUCCESS);
}

编译并运行 :

[lilei@gw ~]$ gcc if_name_index.c -o if_name_index                 
if_name_index.c
[lilei@gw ~]$ gcc if_name_index.c -o if_name_index
[lilei@gw ~]$ ls -l if_name_index
-rwxrwxr-x 1 lilei lilei 7153 5月   1 11:19 if_name_index
[lilei@gw ~]$ ./if_name_index 
Interfece 1 : lo
Interfece 2 : eth0
Interfece 3 : eth2
Interfece 4 : eth1
[lilei@gw ~]$ 

2.if_index_to_name.c示例 :

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <net/if.h>

int main(int argc, char *argv[])
{
    if (argc != 2) {
        fprintf(stderr, "Usage: %s [interface index]\n", argv[0]);
        exit(EXIT_FAILURE);
    }   

    int saved_errno = errno;
    char if_name[IFNAMSIZ] = {'\0'};
    unsigned int if_index = (unsigned int )atoi(argv[1]);

    char *name = if_indextoname(if_index, if_name);
    if (name == NULL && errno == ENXIO) {
        fprintf(stderr, "Index %d : No such device\n", if_index);
        exit(EXIT_FAILURE); 
    } 
    
    errno = saved_errno;  

    printf("Index %d : %s\n", if_index, if_name);

    exit(EXIT_SUCCESS);
}

编译并运行 :

[lilei@gw ~]$ gcc if_index_to_name.c -o if_index_to_name
[lilei@gw ~]$ ./if_index_to_name 
Usage: ./if_index_to_name [interface index]
[lilei@gw ~]$ ./if_index_to_name 0
Index 0 : No such device
[lilei@gw ~]$ ./if_index_to_name 1
Index 1 : lo
[lilei@gw ~]$ ./if_index_to_name 2
Index 2 : eth0
[lilei@gw ~]$ ./if_index_to_name 3
Index 3 : eth2
[lilei@gw ~]$ ./if_index_to_name 4
Index 4 : eth1
[lilei@gw ~]$ ./if_index_to_name 5
Index 5 : No such device
[lilei@gw ~]$ 

3.if_name_to_index.c示例 :

#include <stdio.h>
#include <stdlib.h>
#include <net/if.h>

int main(int argc, char *argv[])
{
    if (argc != 2) {
        fprintf(stderr, "Usage: %s [interface name]\n", argv[0]);
        exit(EXIT_FAILURE);
    }   

    unsigned int if_index;
    if_index = if_nametoindex(argv[1]);
    if (if_index == 0) {
        fprintf(stderr, "Interface %s : No such device\n", argv[1]);
        exit(EXIT_FAILURE);
    }   

    printf("Interface %s : %d\n", argv[1], if_index);   

    exit(EXIT_SUCCESS);
}

编译并运行 :

[lilei@gw ~]$ gcc if_name_to_index.c -o if_name_to_index
[lilei@gw ~]$ ./if_name_to_index eth0
Interface eth0 : 2
[lilei@gw ~]$ ./if_name_to_index eth1
Interface eth1 : 4
[lilei@gw ~]$ ./if_name_to_index eth2
Interface eth2 : 3
[lilei@gw ~]$ ./if_name_to_index eth3
Interface eth3 : No such device
[lilei@gw ~]$ 

原文链接:https://www.jianshu.com/p/389a8870a51d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值