是否有任何包含 ifconfig API 的 C 库?
我想设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可以通过ioctl来完成。请参阅
netdevice(7)
。我猜您正在寻找SIOCGIFMTU
和SIOCSIFMTU
。我不知道它是否是标准的,如果您需要支持其他 Unix,请更新您的问题。编辑
要在您的计算机上获取接口(未经测试):
不要马虎,不要忘记检查返回值。我不在这里检查它们,因为我不在乎;你可能会的。
It can be done with
ioctl
. Seenetdevice(7)
. I'm guessingSIOCGIFMTU
andSIOCSIFMTU
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):
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是用什么写的?
由于 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.
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 runstrace ifconfig ...
and look at whatioctl
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.在我的 debian 盒子上,我可以
这将为我提供 ip 的源代码(新的 ifconfig)、用于构建 iproute 扩展的文档和开发标头。哦,源代码将被编译:)
通过
/usr/share/doc/iproute-do
访问文档(examples/
看起来很有趣)。On my debian box I can
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
(theexamples/
looks interesting).