是否有任何包含 ifconfig API 的 C 库?

发布于 2024-11-07 18:21:13 字数 310 浏览 0 评论 0原文

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

有什么办法可以用C实现吗?有些喜欢 libifconfig.so?

MTU的源代码可以找到此处

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

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

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

楠木可依 2024-11-14 18:21:13

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

编辑

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

int sock;
struct ifconf *conf;

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

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

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.

EDIT

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.

初见终念 2024-11-14 18:21:13

当然在C中是有办法做到的。问问自己ifconfig是用什么写的?

由于 Linux 是开源的,因此您可以下载 ifconfig 的源代码并查看它使用的 API。我认为这是最简单的方法。

如果我想自己查看代码,我可能会稍后对其进行编辑以包含 API。

Of course there are ways to do it in C. Ask yourself what ifconfig is written in?

Since Linux is open source, you can download the source code for ifconfig and look at the APIs that it uses. That is the easiest method in my opinion.

If I feel like looking at the code myself, I may later edit this to include the APIs.

蓝天 2024-11-14 18:21:13

if_nameindex 是获取所有接口列表的函数。

实际上,对它们的很多操作都依赖于系统特定的 ioctl 调用。

老实说,最好的选择是运行 strace ifconfig ... ,而不是浪费时间获取和阅读 100 倍过于复杂的 ifconfig 遗留源代码等。看看它正在执行什么ioctl 系统调用。实际上,这是一种从源逻辑过于复杂或没有源代码的程序中复制行为的通用方法。

if_nameindex is the function to get a list of all interfaces.

Actually doing much with them relies on system-specific ioctl calls.

Honestly, your best bet, rather than wasting your time obtaining and reading 100x over-complicated legacy sources of ifconfig, etc., is to just run strace ifconfig ... and look at what ioctl syscalls it's making. Actually, this is a great general approach to replicating behavior from programs which have overly complicated source logic or for which you don't have the source.

弄潮 2024-11-14 18:21:13

在我的 debian 盒子上,我可以

sudo apt-get install iproute{,-dev,-doc}
sudo apt-get build-dep iproute
apt-get source iproute --compile

这将为我提供 ip 的源代码(新的 ifconfig)、用于构建 iproute 扩展的文档和开发标头。哦,源代码将被编译:)

通过 /usr/share/doc/iproute-do 访问文档(examples/ 看起来很有趣)。

On my debian box I can

sudo apt-get install iproute{,-dev,-doc}
sudo apt-get build-dep iproute
apt-get source iproute --compile

That will give me the source to ip (the new ifconfig), the documentation and the development header for building iproute extensions. Oh and the source will have been compiled :)

Access the documentation via /usr/share/doc/iproute-do (the examples/ looks interesting).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文