目前,我的程序正在对 ifconfig
进行 system()
调用来执行此操作。
看起来有点困惑 - 也许ifconfig
不在路径上,或者在某个非标准位置。然后我需要检查 iproute2 等效项,以防失败。
有没有办法使用 C 以编程方式设置它?
最佳答案
您可以在 ioctl 调用中设置 SIOCSIFMTU 字段,如下所示:
struct ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;//address family
strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));//interface name where you want to set the MTU
ifr.ifr_mtu = 9100; //your MTU size here
if (ioctl(sockfd, SIOCSIFMTU, (caddr_t)&ifr) < 0)
//failed to set MTU. handle error.
上面的代码将使用 ifreq 结构中的 ifr_mtu 字段设置设备的 MTU(如 ifr.name 中)。
关于c - 有没有办法在 Linux 中使用 C 以编程方式设置接口(interface) MTU?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25026228/