系统信息

操作系统:

# 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:

.

函数定义

#include

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

#include

#include

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

#include

#include

#include

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

#include

#include

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

linux 索引代码的函数定义,Linux网络接口操作之if_nameindex相关推荐

  1. linux中有fd set函数吗,LINUX下FD_SET介绍

    刚刚了解了linux下select系统调用,函数原型是 #include #include int select(int maxfdpl, fd_set *readset, fd_set *write ...

  2. 追踪Linux TCPIP代码运行,01.追踪linux代码运行——socket创建

    [前言] 一直想好好研究linux的网络实现,又不知从何入手,总是片面的学习,形不成体系.最近发现秦健老师的<追踪Linux TCP/IP代码运行>,觉得是个不错的入口点,于是决定借此一来 ...

  3. linux c语言内核函数手册,Linux C函数实例速查手册

    函数学习目录: 第1章 初级I/O函数 1.1 close函数:关闭已经打开的文件 1.2 creat函数:创建一个文件 1.3 dup函数:复制文件描述符 1.4 dup2函数:复制文件描述符到指定 ...

  4. 最小的linux内核代码,带你阅读linux内核源码:下载源码、编译内核并运行一个最小系统...

    要学习linux内核,先要能够编译内核,还需要了解内核如何启动根文件系统.这样你才能在修改linux内核代码之后,完成验证的过程. 本文教你完成下列过程: 1.下载linux并编译linux内核源码 ...

  5. 【作业】条件、循环、函数定义、字符串操作练习

    一.注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式. 二.对前面的代码进行优化,用for,while,if,def实现: 1.用循环画五角星 1 fro ...

  6. 条件、循环、函数定义、字符串操作练习

    用循环画五角星 import turtle for i in range(5):turtle.forward(200)turtle.right(144) 用循环画同心圆 import turtle f ...

  7. Linux中append函数的用法,linux C代码 open函数参数:O_APPEND问题求助

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 没有截图,直接上代码吧. 原代码是这样的: #include #include #include #include #include int main() ...

  8. linux open o_creat 失败,linux C代码 open函数参数:O_APPEND问题求助

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 没有截图,直接上代码吧. 原代码是这样的: #include #include #include #include #include int main() ...

  9. linux编译动态库未定义,linux 编译动态链接库 so,避免运行时才发现函数未 ......

    ldd  查看 elf文件依赖的  so 动态链接库 可以  export LD_LIBRARY_PATH=/path 设置 so文件的路径, nm  -u  *.so  或者 nm |grep  U ...

最新文章

  1. 还只看花书,西瓜书?一文告诉你如何正确学习深度学习,从理论到实战。
  2. LeetCode:326. Power of Three
  3. Javascript(JS)中的大括号{}和中括号[]详解
  4. 自定义Interpolator
  5. python循环10次_开发一个循环 5 次计算的小游戏, 设置随机种子为10,每次随机产生两个 1~10的数字以及随机选择...
  6. tyvj1202 数数食物链
  7. 小女出世,暂停工作,全职照料大人小孩
  8. The problem of maze(经典迷宫问题) DFS版
  9. lammps教程:1D正弦粗糙界面建模建模教程
  10. 计算机机房的维护方案,机房设备维护方案.doc
  11. 【程序人生】回忆职业生涯(一)选择职业时的“图样图森破”
  12. unity打箱子小游戏demo
  13. IT计算机行业都有哪些证可以考
  14. base64字符串转换为图片
  15. Zabbix 5.0 配置短信报警
  16. VisionPro初始化 - C#
  17. [51单片机]按键部分(软件消抖)
  18. VR眼镜全国产化电子元件推荐方案
  19. 第一章 常用半导体器件-----------------本征半导体
  20. Linux系统信号定义

热门文章

  1. 自动化测试框架搭建-日志-2
  2. SQL语言之执行计划(Oracle)
  3. Dubbo监控中心Windows安装
  4. RabbitMQ消息的确认模式
  5. mybatis 传入id_想深入理解MyBatis架构及原理实例分析 把握这些就够了
  6. 【Java】6.2 处理对象
  7. 【PAT】B1048 数字加密
  8. Java 中这些常用关键字,总有那么些被你遗忘的
  9. mybatis教程--延迟加载详解
  10. 关掉magiskhide_Magisk v20.4 – The Magic Mask for Android 安裝流程