是否有使用C语言以编程方式设置接口MTU的方法?

问题描述 投票:2回答:2

此刻,我的程序正在对ifconfig进行system()调用。

似乎有点混乱-也许ifconfig不在路径上,或在某些非标准位置。然后,如果发生故障,我需要检查iproute2等效项。

有没有一种方法可以使用C进行编程设置?

非常感谢!

c networking network-programming mtu
2个回答
3
投票

您可以在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一样。)>

参考:http://linux.die.net/man/7/netdevice


0
投票

您也可以使用。完整示例:

© www.soinside.com 2019 - 2024. All rights reserved.