如何从 C 程序中获取 NIC 详细信息?

2024-05-24

我想要获取连接到我的计算机的所有 NIC 的以下详细信息:

1) 接口名称(例如eth0)

2) 接口编号(如Windows http://answers.yahoo.com/question/index?qid=20080517041705AAOmJPu)如果 Linux 中存在这样的东西

3)NIC带宽容量和模式(例如1Gb/s全双工)


您可以使用getifaddrs()/freeifaddrs() http://man7.org/linux/man-pages/man3/getifaddrs.3.html获取所有接口的链表,然后ioctl(fd, SIOCGIFINDEX, struct ifreq *)获取每个接口的索引。由于接口是连续的并且始终列出(无论它们是否启动(活动)),我选择使用循环枚举它们ioctl(fd, SIOCGIFNAME, struct ifreq *)反而。在所有情况下fd is an AF_INET socket.

要获取接口的双工和速率,需要使用ioctl(fd, SIOCETHTOOL, struct ifreq *)ifr_data指向一个struct ethtool_cmd having cmd = ETHTOOL_GSET.

如果 ioctl 失败,则应返回 -1;如果成功,则应返回非负值(我相信为零)。

这是一个示例程序:

#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/ip.h>
#include <net/if.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

struct interface {
    int     index;
    int     flags;      /* IFF_UP etc. */
    long    speed;      /* Mbps; -1 is unknown */
    int     duplex;     /* DUPLEX_FULL, DUPLEX_HALF, or unknown */
    char    name[IF_NAMESIZE + 1];
};

static int get_interface_common(const int fd, struct ifreq *const ifr, struct interface *const info)
{
    struct ethtool_cmd  cmd;
    int                 result;

    /* Interface flags. */
    if (ioctl(fd, SIOCGIFFLAGS, ifr) == -1)
        info->flags = 0;
    else
        info->flags = ifr->ifr_flags;

    ifr->ifr_data = (void *)&cmd;
    cmd.cmd = ETHTOOL_GSET; /* "Get settings" */
    if (ioctl(fd, SIOCETHTOOL, ifr) == -1) {
        /* Unknown */
        info->speed = -1L;
        info->duplex = DUPLEX_UNKNOWN;
    } else {
        info->speed = ethtool_cmd_speed(&cmd);
        info->duplex = cmd.duplex;
    }

    do {
        result = close(fd);
    } while (result == -1 && errno == EINTR);
    if (result == -1)
        return errno;

    return 0;
}

int get_interface_by_index(const int index, struct interface *const info)
{
    int             socketfd, result;
    struct ifreq    ifr;

    if (index < 1 || !info)
        return errno = EINVAL;

    socketfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (socketfd == -1)
        return errno;

    ifr.ifr_ifindex = index;
    if (ioctl(socketfd, SIOCGIFNAME, &ifr) == -1) {
        do {
            result = close(socketfd);
        } while (result == -1 && errno == EINTR);
        return errno = ENOENT;
    }

    info->index = index;
    strncpy(info->name, ifr.ifr_name, IF_NAMESIZE);
    info->name[IF_NAMESIZE] = '\0';

    return get_interface_common(socketfd, &ifr, info);
}

int get_interface_by_name(const char *const name, struct interface *const info)
{
    int             socketfd, result;
    struct ifreq    ifr;

    if (!name || !*name || !info)
        return errno = EINVAL;

    socketfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (socketfd == -1)
        return errno;

    strncpy(ifr.ifr_name, name, IF_NAMESIZE);
    if (ioctl(socketfd, SIOCGIFINDEX, &ifr) == -1) {
        do {
            result = close(socketfd);
        } while (result == -1 && errno == EINTR);
        return errno = ENOENT;
    }

    info->index = ifr.ifr_ifindex;
    strncpy(info->name, name, IF_NAMESIZE);
    info->name[IF_NAMESIZE] = '\0';

    return get_interface_common(socketfd, &ifr, info);
}

int main(int argc, char *argv[])
{
    struct interface    iface;
    int                 arg;
    int                 status = 0;

    if (argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
        fprintf(stderr, "\n");
        fprintf(stderr, "Usage: %s { -h | --help }\n", argv[0]);
        fprintf(stderr, "       %s\n", argv[0]);
        fprintf(stderr, "       %s INTERFACE ...\n", argv[0]);
        fprintf(stderr, "\n");
        return 1;
    }

    if (argc > 1) {
        for (arg = 1; arg < argc; arg++) {
            if (get_interface_by_name(argv[arg], &iface) != 0) {
                fprintf(stderr, "%s: No such interface.\n", argv[arg]);
                status = 1;
                continue;
            }

            printf("%s: Interface %d", iface.name, iface.index);
            if (iface.flags & IFF_UP)
                printf(", up");
            if (iface.duplex == DUPLEX_FULL)
                printf(", full duplex");
            else
            if (iface.duplex == DUPLEX_HALF)
                printf(", half duplex");
            if (iface.speed > 0)
                printf(", %ld Mbps", iface.speed);
            printf("\n");
        }

    } else {
        for (arg = 1; ; arg++) {
            if (get_interface_by_index(arg, &iface) != 0)
                break;

            printf("%s: Interface %d", iface.name, iface.index);
            if (iface.flags & IFF_UP)
                printf(", up");
            if (iface.duplex == DUPLEX_FULL)
                printf(", full duplex");
            else
            if (iface.duplex == DUPLEX_HALF)
                printf(", half duplex");
            if (iface.speed > 0)
                printf(", %ld Mbps", iface.speed);
            printf("\n");
        }
    }

    return status;
}

如果您将上面的内容另存为iflist.c,你可以使用编译它

gcc -W -Wall -O3 iflist.c -o iflist

要查看用法,请运行iflist -h。要列出所有接口,请不带参数运行它:

./iflist

上面将使用我描述的枚举方法。要仅列出特定接口,请运行它并命名接口:

./iflist eth0 lo

当然,双工和速度仅针对以太网接口列出。


编辑添加:

如果上述程序不提供接口的带宽和模式,这里有一个简化的版本,它报告确切的原因(错误)。这个将接口名称作为命令行参数;它不枚举接口。

#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/ip.h>
#include <net/if.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

int ethernet_interface(const char *const name,
                       int *const index, int *const speed, int *const duplex)
{
    struct ifreq        ifr;
    struct ethtool_cmd  cmd;
    int                 fd, result;

    if (!name || !*name) {
        fprintf(stderr, "Error: NULL interface name.\n");
        fflush(stderr);
        return errno = EINVAL;
    }

    if (index)  *index = -1;
    if (speed)  *speed = -1;
    if (duplex) *duplex = -1;

    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd == -1) {
        const int err = errno;
        fprintf(stderr, "%s: Cannot create AF_INET socket: %s.\n", name, strerror(err));
        fflush(stderr);
        return errno = err;
    }

    strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
    ifr.ifr_data = (void *)&cmd;
    cmd.cmd = ETHTOOL_GSET;
    if (ioctl(fd, SIOCETHTOOL, &ifr) < 0) {
        const int err = errno;
        do {
            result = close(fd);
        } while (result == -1 && errno == EINTR);
        fprintf(stderr, "%s: SIOCETHTOOL ioctl: %s.\n", name, strerror(err));
        return errno = err;
    }

    if (speed)
        *speed = ethtool_cmd_speed(&cmd);

    if (duplex)
        switch (cmd.duplex) {
        case DUPLEX_HALF: *duplex = 0; break;
        case DUPLEX_FULL: *duplex = 1; break;
        default:
            fprintf(stderr, "%s: Unknown mode (0x%x).\n", name, cmd.duplex);
            fflush(stderr);
            *duplex = -1;
        }

    if (index && ioctl(fd, SIOCGIFINDEX, &ifr) >= 0)
        *index = ifr.ifr_ifindex;

    do {
        result = close(fd);
    } while (result == -1 && errno == EINTR);
    if (result == -1) {
        const int err = errno;
        fprintf(stderr, "%s: Error closing socket: %s.\n", name, strerror(err));
        return errno = err;
    }

    return 0;
}

int main(int argc, char *argv[])
{
    int  arg, speed, index, duplex;

    if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
        fprintf(stderr, "\n");
        fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
        fprintf(stderr, "       %s INTERFACE ...\n", argv[0]);
        fprintf(stderr, "\n");
        return 0;
    }

    for (arg = 1; arg < argc; arg++) {
        if (ethernet_interface(argv[arg], &index, &speed, &duplex))
            return 1;

        if (index == -1)
            printf("%s: (no interface index)", argv[arg]);
        else
            printf("%s: interface %d", argv[arg], index);

        if (speed == -1)
            printf(", unknown bandwidth");
        else
            printf(", %d Mbps bandwidth", speed);

        if (duplex == 0)
            printf(", half duplex.\n");
        else if (duplex == 1)
            printf(", full duplex.\n");
        else
            printf(", unknown mode.\n");
    }

    return 0;
}

问题?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 C 程序中获取 NIC 详细信息? 的相关文章

  • -ffast-math 可以安全地用于典型项目吗?

    在回答我建议的问题时 ffast math 有评论指出这是危险的 我个人的感觉是 在科学计算之外 是可以的 我还假设严肃的金融应用程序使用定点而不是浮点 当然 如果你想在你的项目中使用它 最终的答案是在你的项目上测试它 看看它有多大影响 但
  • 全局变量不好

    好吧 读完这篇文章和一些示例后 我仍然不清楚全局变量的含义 那么你的类中的私有变量是全局的吗 http www c2 com cgi wiki GlobalVariablesAreBad http www c2 com cgi wiki G
  • StackExchange Redis 删除所有以以下开头的键

    我有一个格式的密钥 Error 1 Error 24 Error 32 Using StackExchange Redis 我该怎么办KeyDelete在与格式匹配的所有键上Error 在另一个答案中我看到了 LUA 脚本 EVAL ret
  • 如何获取枚举数作为常量?

    From 枚举中定义的项目总数 https stackoverflow com questions 856154 total number of items defined in an enum 我发现我可以使用以下方法获取枚举数 Enum
  • MVVM:来自 FileOpenPicker 的图像绑定源

    我将 OnActivated 添加到 app xaml cs 中 它可以正常工作 protected async override void OnActivated IActivatedEventArgs args var continua
  • 在编译输出中添加程序集绑定 (app.config)

    如果我编译应用程序 则会在输出中自动添加程序集绑定 具体的程序集绑定不在app config在 Visual Studio 中但在创建的应用程序配置中 有什么办法可以检查为什么会自动添加程序集绑定吗 选项AutoGenerateBindin
  • 如何在 Windows 上的 GCC 中链接 CS50 C 库

    我是 编程新手 一直在尝试使用以下命令编译我的代码MinGW https en wikipedia org wiki MinGW GCC 但我尝试包括CS50 https en wikipedia org wiki CS50 cs50 c
  • 为什么 xcode IDE 认为 `friend` 是保留字

    我一直在开发一个个人项目 并在我创建的新类中包含以下代码 property readonly getter isFriend BOOL friend 它似乎没有任何问题 当我构建它时 它可以编译得很好 但是当我们在xcode IDE看起来像
  • C++:避免​​在重载中将字符串自动转换为布尔值

    我想创建一组方法 这些方法将根据其类型输出具有特殊格式的值 当我这样做时 到目前为止看起来还不错 static void printValue std ostringstream out int value out lt lt value
  • 仅使用一个 #include 表达式一次包含多个头文件?

    是否有任何表达式可以使语法一次包含多个标头 而无需为每个新文件编写 include 表达式 例如 include
  • 在 C++ 中,为什么 const 也可以工作时编译器选择非常量函数? [复制]

    这个问题在这里已经有答案了 例如 假设我有一堂课 class Foo public std string Name m maybe modified true return m name const std string Name cons
  • 将两个垂直滚动条相互绑定

    我在控件中有两个 TextBox 并且它们都有两个 VerticalScrollBar 我想在它们之间绑定 VerticalScrollBars 如果一个向上 第二个也会向上等等 如果可以的话我该怎么做 Thanks 不是真正的绑定 但它有
  • 如何找到进程启动时使用的原始用户名?

    有一个 perl 脚本需要以 root 身份运行 但我们必须确保运行该脚本的用户最初没有以用户 foo 身份登录 因为它将在脚本运行期间被删除 那么 我如何查明自登录以来可能已多次起诉的用户是否在该链中的任何时间都没有模拟过 foo 我发现
  • 括号内声明的对象的范围

    如果我声明一个这样的对象 void main myclass objectA anotherclass true true 0 即 我通过直接调用后者的构造函数来创建一个 objectA 和另一个对象 anotherclass anothe
  • 为什么使用 .AsEnumerable() 而不是转换为 IEnumerable

    扩展方法之一IEnumerable
  • 结构大小与 typedef 版本不同?

    我的代码中有以下结构声明和 typedef struct blockHeaderStruct bool allocated unsigned int length typedef struct blockHeaderStruct block
  • 如何检查日期时间是否发生在今天?

    有没有比下面的代码更好的 net 方法来检查 今天 是否发生了 DateTime if newsStory WhenAdded Day DateTime Now Day newsStory WhenAdded Month DateTime
  • 在 C 中运行 setuid 程序的正确方法

    我有一个权限为4750的进程 我的Linux系统中存在两个用户 root 用户和 appz 用户 该进程继承以 appz 用户身份运行的进程管理器的权限 我有两个基本惯例 void do root void int status statu
  • 在for循环中声明和初始化变量

    可以简单写一下吗 for int i 0 代替 int i for i 0 在 C 或 C 中 并且会变量i只能在循环内部访问 它在 C 中有效 它在 C 的原始版本中是不合法的 但在 C99 中被采用为 C 的一部分 当时一些 C 功能被
  • Web 和 winforms 的 .Net 身份验证

    我有一个为客户端构建的 ASP NET Web 应用程序 它使用默认的 ASP NET 表单身份验证 他们现在请求一个能够 与 Web 应用程序一起工作的桌面 WinForms 应用程序 我已经创建了 Web 服务来访问他们想要从 Web

随机推荐

  • 测试随机值 - 对这种方法的想法?

    好的 我一直在研究随机图像选择器和队列系统 因此您不会经常看到相同的图像 一切都很顺利 就我蹩脚的代码而言 until我到了随机位 我想测试一下 但是如何测试呢 没有Debug Assert i IsRandom 可悲的是 D 所以 我在用
  • PHP使用c++扩展会更快吗?

    我现在正在构建一个推荐系统 一个网站 我想使用PHP作为脚本语言 推荐系统可能会进行复杂的矩阵计算或其他操作 考虑到性能 我想用C 来做矩阵计算或者其他复杂的计算 所以我需要PHP调用C 函数 获取结果并进行其他处理 我知道我可以使用 PH
  • Axon 框架的现实生活体验[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 作为研究用于项目的 CQRS 的一部分 我遇到了轴突框架 http www axonframework org 我想知道是否有人有任何现实生活经
  • 在对象的 NSArray 中搜索与任何属性匹配的字符串

    我有一个 NSArray 对象 这些对象有 10 个属性 我想对这些对象进行文本搜索 我知道如何一次搜索 1 个房产 但有没有一种简单的方法可以一次搜索所有房产 以下是我的对象具有的属性列表 property nonatomic retai
  • 为什么编译器在这里错过矢量化?

    考虑以下valarray类 include
  • 如何反转包裹上的弹性盒方向以实现“蛇形包裹”?

    我想找到一种方法 让单个容器中的元素换行到与前一行相反的方向的新行 就像一条蛇向后弯曲一样 我无法使用 flexbox 以及 flex direction 和 flex wrap 属性的任何组合来实现此结果 Flexbox 结果与期望结果的
  • 为什么 parseInt() 不起作用? [复制]

    这个问题在这里已经有答案了 我正在用 JavaScript 构建一个计算器 我即将完成 但有一个逻辑错误 我正在尝试将字符串转换为数字 以便进行计算 例如 我想要var value 10 要转换为整数 我已经尝试过parseInt valu
  • Dataflow/apache beam:管理自定义模块依赖项

    我有一个使用 apache beam 的 py 管道 它导入另一个模块 py 这是我的自定义模块 我有一个这样的结构 mymain py myothermodule py 我将 myothermodule py 导入到 mymain py
  • 防止 DLL 文件通过 MS Detours 加载到我的进程中

    我想阻止特定的第三方 DLL 文件在运行时加载到我的应用程序进程中 我最初的尝试是使用 MS Detours 产品 我有一个32位的MFC http en wikipedia org wiki Microsoft Foundation Cl
  • Silverlight 5 - 改进/更改的综合列表

    是否有 silverlight 5 新增功能的完整列表 我看到了不同的博客等 但显然问题在于细节 当他们说更好的绑定支持到底有什么不同更好 等等 我想看详细的论文 Pete Brown 列出了 RC 中的主要变化 http 10rem ne
  • 更新到 3.6.5 后,Hibernate 将属性映射到 SQL 公式失败

    我刚刚从 3 3 0 GA 更新到 Hibernate 3 6 5 Final 并且在对 XML 映射属性进行 SQL 公式调用时遇到了问题
  • 使用 Pyinstaller 打包:PyQt4.QtGui.setStyle 被忽略?

    问题 如何在 Ubuntu 14 04 上使用 Pyinstaller 为我的应用程序设置 PyQt 样式 Details 我在 Windows 上使用 pyinstaller 打包了一个 PyQt4 Python 应用程序 它似乎工作正常
  • 带有两个滑块的交互式 matplotlib 绘图

    I used 绘图库创建一些图 这取决于 8 个变量 我想研究一下当我改变其中一些内容时情节如何变化 我创建了一些脚本来调用绘图库一个并生成不同的快照 稍后我将其转换成电影 这还不错 但有点笨拙 我想知道是否可以使用键盘按键与绘图重新生成进
  • 点击jtable中的超链接?

    如何为中的每条记录启用超链接JTable 我想要做的是 用户可以单击超链接 然后该超链接将显示他们可以编辑 更新的信息 或者 如何启用表数据的就地编辑 另一个问题是我目前正在使用以下方式来显示不同的屏幕 但这不是一种优雅的方式 我知道我们应
  • PHP的spl_autoload_register如何解决与require_once的循环依赖?

    PHP的spl autoload register如何解决与require once的循环依赖 循环依赖可以解决某些情况 但不是全部 让我们从一个失败的例子开始 假设我们在不同的文件中定义了三个类 cat php class Cat ext
  • 如何使用实体框架选择单个列?

    有没有办法使用 Entity Framework 4 获取单个列的全部内容 与此 SQL 查询相同 SELECT Name FROM MyTable WHERE UserId 1 您可以使用 LINQ Select http msdn mi
  • 组合 hibernate @Transactional 和 ehcache @Cacheable 总是创建数据库事务

    在对我们的应用程序进行负载测试时 我注意到如果您使用 Transactional 和 Cacheable 注释 hibernate 总是会创建一个数据库事务 有没有简单的方法可以防止这种情况发生 在 Spring 中解决这个问题的一种更详细
  • 无法在 Windows 10 上运行 Python 3.7“权限被拒绝”

    当尝试使用 Git Bash 在 Windows 10 上运行 Python 3 7 时 出现以下错误 python version bash c Users Name AppData Local Microsoft WindowsApps
  • WPF + MVVM + RadioButton:如何处理单个属性的绑定?

    From this https stackoverflow com questions 2284752 mvvm binding radio buttons to a view model and this https stackoverf
  • 如何从 C 程序中获取 NIC 详细信息?

    我想要获取连接到我的计算机的所有 NIC 的以下详细信息 1 接口名称 例如eth0 2 接口编号 如Windows http answers yahoo com question index qid 20080517041705AAOmJ