Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set MTU in C programmatically

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?

like image 233
cateof Avatar asked Dec 16 '22 14:12

cateof


1 Answers

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.

like image 55
Alessar Avatar answered Dec 22 '22 15:12

Alessar