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

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

问题描述

我要设置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的源$ C ​​$ C可以发现<一个href=\"http://cvsweb.netbsd.org/bsdweb.cgi/src/sbin/ifconfig/ifconfig.c?rev=1.169&content-type=text/x-cvsweb-markup\"相对=nofollow>这里。

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)。我猜 SIOCGIFMTU SIOCSIFMTU 是你在找什么。我不知道,如果它是标准的,如果你需要支持其他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库包含API来使用ifconfig?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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