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;
            }
        }
相关推荐
River4162 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥5 小时前
C++ std::set
c++
侃侃_天下5 小时前
最终的信号类
开发语言·c++·算法
博笙困了6 小时前
AcWing学习——差分
c++·算法
echoarts6 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix6 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
青草地溪水旁6 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁6 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
感哥6 小时前
C++ std::vector
c++
zl_dfq7 小时前
C++ 之【C++11的简介】(可变参数模板、lambda表达式、function\bind包装器)
c++