23 September 2007

Remove network interface under GNU/Linux

It is enough to switch down the corresponding alias network interface:
N.B.: All checks have been removed for better legibility, but it is very important to check returned code after each request.

// We consider the existence of the variable (unsigned char *) "aliasInterfaceName" ("eth0:myAlias" for instance).
int sd;
struct ifreq ifr;

sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.lifr_name, aliasInterfaceName, sizeof(ifr.ifr_name));
ifr.ifr_flags = ~IFF_UP;
ioctl(sd, SIOCSIFFLAGS, &ifr);

No comments:

Post a Comment

Thank you for your visit, let's share your point of view: