日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

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

發布時間:2025/3/20 linux 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux 索引代码的函数定义,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:

.

函數定義

#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

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的linux 索引代码的函数定义,Linux网络接口操作之if_nameindex的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。