#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <sys/types.h>
#include <linux/types.h>
#include <linux/sockios.h>
#define EPARAM -100
#define ESOCK -1
#define EICTRL -2
#define MAXMTU 10000
int get_mtu(const char *devname)
{
int fd, ret;
struct ifreq ifr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("create socket failure/n");
return ESOCK;
}
strcpy(ifr.ifr_ifrn.ifrn_name, devname);
ret = ioctl(fd, SIOCGIFMTU, (__u16 *)&ifr);
if (ret < 0) {
perror("get mtu failure/n");
return EICTRL;
}
close( fd );
printf("current mtu : (%u)/n", ifr.ifr_ifru.ifru_mtu);
return ifr.ifr_ifru.ifru_mtu;
}
int set_mtu(const char *devname, int new_mtu)
{
int fd, ret;
struct ifreq ifr;
if (new_mtu <= 0 || new_mtu > MAXMTU) {
perror("parameter mtu value exceed range/n");
return (-1);
}
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("create socket failure/n");
return ESOCK;
}
strcpy(ifr.ifr_ifrn.ifrn_name, devname);
ret = ioctl(fd, SIOCGIFMTU, (__u16 *)&ifr);
if (ret < 0) {
perror("get mtu failure/n");
return EICTRL;
}
ifr.ifr_ifru.ifru_mtu = new_mtu;
ret = ioctl(fd, SIOCSIFMTU, (__u16 *)&ifr);
if (ret < 0) {
perror("set mtu failure/n");
return EICTRL;
}
close(fd);
return 0;
}
int main(int arg , char *argv[])
{
get_mtu("eth0");
set_mtu("eth0", 1500);
get_mtu("eth0");
return 0;
}
#include <string.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <sys/types.h>
#include <linux/types.h>
#include <linux/sockios.h>
#define EPARAM -100
#define ESOCK -1
#define EICTRL -2
#define MAXMTU 10000
int get_mtu(const char *devname)
{
int fd, ret;
struct ifreq ifr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("create socket failure/n");
return ESOCK;
}
strcpy(ifr.ifr_ifrn.ifrn_name, devname);
ret = ioctl(fd, SIOCGIFMTU, (__u16 *)&ifr);
if (ret < 0) {
perror("get mtu failure/n");
return EICTRL;
}
close( fd );
printf("current mtu : (%u)/n", ifr.ifr_ifru.ifru_mtu);
return ifr.ifr_ifru.ifru_mtu;
}
int set_mtu(const char *devname, int new_mtu)
{
int fd, ret;
struct ifreq ifr;
if (new_mtu <= 0 || new_mtu > MAXMTU) {
perror("parameter mtu value exceed range/n");
return (-1);
}
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("create socket failure/n");
return ESOCK;
}
strcpy(ifr.ifr_ifrn.ifrn_name, devname);
ret = ioctl(fd, SIOCGIFMTU, (__u16 *)&ifr);
if (ret < 0) {
perror("get mtu failure/n");
return EICTRL;
}
ifr.ifr_ifru.ifru_mtu = new_mtu;
ret = ioctl(fd, SIOCSIFMTU, (__u16 *)&ifr);
if (ret < 0) {
perror("set mtu failure/n");
return EICTRL;
}
close(fd);
return 0;
}
int main(int arg , char *argv[])
{
get_mtu("eth0");
set_mtu("eth0", 1500);
get_mtu("eth0");
return 0;
}