是否有任何可用的 C 库包含 ifconfig 的 API? [英] is there any C library available that contains APIs for ifconfig?

查看:14
本文介绍了是否有任何可用的 C 库包含 ifconfig 的 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置 MTU 或获取一些设备详细信息,但我不想使用系统(ifconfig ...").

I want to set the MTU in or to get some device details, but I don't want to use system("ifconfig ...").

有没有办法在 C 中做到这一点?有些像 libifconfig.so?

Is there any way to do it in C? Some like libifconfig.so?

MTU的源代码可以找到这里.

The source code of MTU can be found here.

但我现在真的没有太多时间.如何获取所有可用接口的名称(我的系统中有很多)

But I really don't too much time right now. How can I get the name of ALL the available interfaces (I have plenty in my system)

推荐答案

可以通过ioctl来完成.请参阅 netdevice(7).我猜 SIOCGIFMTUSIOCSIFMTU 是你要找的.我不知道它是否是标准的,如果您需要支持其他 Unix,请更新您的问题.

It can be done with ioctl. See netdevice(7). I'm guessing SIOCGIFMTU and SIOCSIFMTU are what you're looking for. I don't know if it's standard, update your question if you need to support other Unixes.

编辑

在您的机器上获取接口(未经测试):

To get the interfaces on your machine (untested):

int sock;
struct ifconf *conf;

sock = socket(AF_INET, SOCK_STREAM, 0);
ioctl(sock, SIOCGIFCONF , (char*) conf);

不要马虎,不要忘记检查返回值.我不在这里检查它们,因为我不在乎;你可能会.

Don't be sloppy and don't forget to check the return values. I don't check them here because I don't care; you probably do.

这篇关于是否有任何可用的 C 库包含 ifconfig 的 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆