set MTU in C programmatically

前端 未结 4 545
温柔的废话
温柔的废话 2021-02-10 18:53

A client requested that the MTU limit should be 1492.

Is there a way to do it in the source code (program in C)?

Is there any other way to do it in general? (if

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-10 19:35

    Programmaticaly way using C:

    int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
    struct ifreq ifr;
    strcpy(ifr.ifr_name, "eth0");
    if(!ioctl(sock, SIOCGIFMTU, &ifr)) {
      ifr.ifr_mtu // Contains current mtu value
    }
    ifr.ifr_mtu = ... // Change value if it needed
    if(!ioctl(sock, SIOCSIFMTU, &ifr)) {
      // Mtu changed successfully
    }
    

    It works at least on Ubuntu, see man netdevice.

提交回复
热议问题