if_nametoindex, if_indextoname——网络接口名与索引值之间的映射

说明

NAME
       if_nametoindex, if_indextoname - mappings between network interface names and indexes

SYNOPSIS
       #include <net/if.h>

       unsigned int if_nametoindex(const char *ifname);

       char *if_indextoname(unsigned int ifindex, char *ifname);

DESCRIPTION
       The  if_nametoindex() function returns the index of the network interface corresponding to the name
       ifname.

       The if_indextoname() function returns the name of the network interface corresponding to the inter‐
       face  index ifindex.  The name is placed in the buffer pointed to by ifname.  The buffer must allow
       for the storage of at least IF_NAMESIZE bytes.

RETURN VALUE
       On success, if_nametoindex() returns the index number of the network  interface;  on  error,  0  is
       returned and errno is set appropriately.

       On  success, if_indextoname() returns ifname; on error, NULL is returned and errno is set appropri‐
       ately.
ERRORS
       if_nametoindex() may fail and set errno if:

       ENODEV No interface found with given name.

       if_indextoname() may fail and set errno if:

       ENXIO  No interface found for the index.

       if_nametoindex() and if_indextoname() may also fail for any of the errors specified  for  socket(2)
       or ioctl(2).

  • if_nametoindex():指定网络接口名称字符串作为参数;若该接口存在,则返回相应的索引,否则返回0
  • if_indextoname():指定网络接口索引以及一块长度至少为IF_NAMESIZE(16)字节的内存区域作为参数;若索引对应的网络接口存在,则在内存区域中返回该接口的名称字符串,否则返回NULL,并将errno设置为相应的值
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值