MACOS上面C/C++获取网卡索引,索引获取网卡接口名

依赖函数:

if_nametoindex IF名字 to IF索引

if_indextoname IF索引 to IF名字

MACOS 10.7 版本支援(就是2011年发不OSX的第一个面向用的系统版本)

cpp 复制代码
        int GetInterfaceIndex(const ppp::string& ifrName) noexcept
        {
            if (ifrName.empty())
            {
                return -1;
            }

            int interface_index = (int)if_nametoindex(ifrName.data());
            if (interface_index == 0 || interface_index == -1)
            {
                return -1;
            }

            return interface_index;
        }

        bool GetInterfaceName(int interface_index, ppp::string& ifrName) noexcept
        {
            ifrName.clear();
            if (interface_index == 0 || interface_index == -1)
            {
                return false;
            }

            char buf[255];
            if (if_indextoname((unsigned int)interface_index, buf))
            {
                char ch = *buf;
                if (ch == '\x0')
                {
                    return false;
                }

                ifrName = buf;
                return true;
            }
            else
            {
                return false;
            }
        }
相关推荐
健康平安的活着8 分钟前
java中乐观锁+事务在批量导入,批量审批案例的使用
java·开发语言
feng_you_ying_li11 分钟前
stack的实现,queue的实现,deque的介绍和priority_queue的实现以及仿函数的介绍与运用
c++
lxh011319 分钟前
重复的DNA序列
开发语言·javascript·ecmascript
froginwe1121 分钟前
Web 词汇表
开发语言
im_AMBER25 分钟前
Leetcode 139 最后一个单词的长度 | 找出字符串中第一个匹配项的下标
开发语言·算法·leetcode
2401_8898846633 分钟前
嵌入式C++测试框架
开发语言·c++·算法
1104.北光c°33 分钟前
我理解的Leaf号段模式:美团分布式ID生成系统
java·开发语言·笔记·分布式·github·leaf
DREW_Smile37 分钟前
字符函数和字符串函数2
c语言·开发语言
Z9fish1 小时前
C语言算法专题总结(一)排序
c语言·算法·排序算法
wjs20241 小时前
CSS 颜色
开发语言