Pregunta

En este momento mi programa está haciendo un system() llamar a ifconfig para hacer esto.

Parece un poco desordenado - tal vez ifconfig no está en el camino o en alguna ubicación no estándar.Y luego necesitaría verificar el iproute2 equivalente en caso de avería.

¿Hay alguna manera de configurar esto mediante programación usando C?

¡Muchas gracias!

¿Fue útil?

Solución

Puede configurar el campo SIOCSIFMTU en una llamada ioctl, algo como esto:

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.

El código anterior configurará la MTU de un dispositivo (como en ifr.name) usando el campo ifr_mtu en la estructura ifreq.

Referirse: http://linux.die.net/man/7/netdevice

Otros consejos

También puedes usar .El ejemplo completo:

#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <sys/socket.h>
#include <linux/rtnetlink.h>

#define IFACE_NAME "enp5s0"

int main(void) {
     int ret, nl_sock;
     unsigned int mtu = 8000;
     struct rtattr  *rta;
     struct {
          struct nlmsghdr nh;
          struct ifinfomsg  ifinfo;
          char   attrbuf[512];
     } req;

     nl_sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
     if(nl_sock < 0) {
          perror("socket():");
          return -1;
     }

     memset(&req, 0, sizeof req);
     req.nh.nlmsg_len   = NLMSG_LENGTH(sizeof(struct ifinfomsg));
     req.nh.nlmsg_flags = NLM_F_REQUEST;
     req.nh.nlmsg_type  = RTM_NEWLINK; // RTM_SETLINK
     req.ifinfo.ifi_family = AF_UNSPEC;
     req.ifinfo.ifi_index  = if_nametoindex(IFACE_NAME);
     if(!req.ifinfo.ifi_index) {
          perror("if_nametoindex():");
          return -1;
     }
     printf("%s index: %d\n", IFACE_NAME, req.ifinfo.ifi_index);
     req.ifinfo.ifi_change = 0xffffffff;
     rta = (struct rtattr *)(((char *) &req) + NLMSG_ALIGN(req.nh.nlmsg_len));
     rta->rta_type = IFLA_MTU;
     rta->rta_len = RTA_LENGTH(sizeof(unsigned int));
     req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) + RTA_LENGTH(sizeof mtu);
     memcpy(RTA_DATA(rta), &mtu, sizeof mtu);

     ret = send(nl_sock, &req, req.nh.nlmsg_len, 0);
     if(ret < 0) {
          perror("send():");
          return -1;
     }

     return 0;
}

Lea también: enlace rtnet(3), enlace rtnet(7)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top