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;
            }
        }
相关推荐
胖咕噜的稞达鸭1 小时前
自定义shell命令行解释器自制
java·开发语言
草莓熊Lotso1 小时前
Git 分支管理:从基础操作到协作流程(本地篇)
大数据·服务器·开发语言·c++·人工智能·git·sql
报错小能手1 小时前
C++异常处理 终极及总结
开发语言·c++
雨落在了我的手上1 小时前
C语言入门(二十二):字符函数和字符串函数(2)
c语言
Algo-hx1 小时前
C++编程基础(九):预处理指令
c++
qq_401700415 小时前
嵌入式用Unix时间的优势及其C语言转换
服务器·c语言·unix
tobebetter95277 小时前
How to manage python versions on windows
开发语言·windows·python
9***P3348 小时前
PHP代码覆盖率
开发语言·php·代码覆盖率
CoderYanger8 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz8 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab