int skfd = -1; /* AF_INET socket for ioctl() calls.*/
int set_if_flags(char *ifname, short flags)
{
struct ifreq ifr;
int res = 0;
ifr.ifr_flags = flags;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("socket error %s\n", strerror(errno));
res = 1;
goto out;
}
res = ioctl(skfd, SIOCSIFFLAGS, &ifr);
if (res < 0) {
printf("Interface '%s': Error: SIOCSIFFLAGS failed: %s\n",
ifname, strerror(errno));
} else {
printf("Interface '%s': flags set to %04X.\n", ifname, flags);
}
out:
return res;
}
int set_if_up(char *ifname, short flags)
{
return set_if_flags(ifname, flags | IFF_UP);
}
использование:
set_if_up("eth0", 1);