Linux网络接口操作之if_nameindex

系统信息

操作系统:

# lsb_release -ir
Distributor ID:    CentOS
Release:           6.7

内核版本:

# uname -r
2.6.32-573.26.1.el6.x86_64

gcc版本:

# gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

glibc版本:

# ldd $(which ls)
    linux-vdso.so.1 =>  (0x00007ffc14d08000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003b2ec00000)
    librt.so.1 => /lib64/librt.so.1 (0x0000003b2e400000)
    libcap.so.2 => /lib64/libcap.so.2 (0x0000003b2fc00000)
    libacl.so.1 => /lib64/libacl.so.1 (0x0000003b36000000)
    libc.so.6 => /lib64/libc.so.6 (0x0000003b2d400000)
    libdl.so.2 => /lib64/libdl.so.2 (0x0000003b2dc00000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003b2d000000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003b2d800000)
    libattr.so.1 => /lib64/libattr.so.1 (0x0000003b2f800000)
    
# /lib64/libc.so.6
GNU C Library stable release version 2.12, by Roland McGrath et al.
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.4.7 20120313 (Red Hat 4.4.7-16).
Compiled on a Linux 2.6.32 system on 2016-02-16.
Available extensions:
    The C stubs add-on version 2.1.2.
    crypt add-on version 2.1 by Michael Glad and others
    GNU Libidn by Simon Josefsson
    Native POSIX Threads Library by Ulrich Drepper et al
    BIND-8.2.3-T5B
    RT using linux kernel aio
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.

函数定义

#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

#include <stdio.h>
#include <stdlib.h>
#include <net/if.h>
    
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);
}

编译并运行

# gcc if_name_index.c -o if_name_index
# ./if_name_index 
Interfece 1 : lo
Interfece 2 : eth0
Interfece 3 : eth1    

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);
}

编译并运行

# gcc if_index_to_name.c -o if_index_to_name
# ./if_index_to_name
Usage: ./if_index_to_name [interface index]
# ./if_index_to_name 1
Index 1 : lo
# ./if_index_to_name 2
Index 2 : eth0
# ./if_index_to_name 3
Index 3 : eth1
# ./if_index_to_name 4
Index 4 : No such device

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);
}

编译并运行

# gcc if_name_to_index.c -o if_name_to_index
# ./if_name_to_index eth0
Interface eth0 : 2
# ./if_name_to_index eth3
Interface eth3 : No such device

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值