set MTU in C programmatically

cateof picture cateof · May 5, 2011 · Viewed 8.6k times · Source

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? (ifconfig?)

Why does somebody needs to modify MTU to a certain limit? What is the benefit? And the most important: By changing the MTU is there any risk to break the code?

Answer

Alessar picture Alessar · Jan 14, 2016

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.