ImperialViolet

Did you know you can rena... (14 Feb 2003)

Did you know you can rename network interfaces under Linux. It could be quite useful to have a utility that reads a mapping of MAC addresses to names and sets all the interface names. That way you could work with inside and outside and not eth0 and eth1. A quick utility for your renaming pleasure:

/* if_rename.c - Renames linux network interfaces
 * Adam Langley <aglREMOVETHIS@imperialviolet.org>
 * % gcc -o if_rename if_rename.c -Wall
 */

#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <stdio.h>
#include <string.h>

int
main (int argc, char **argv)
{
	struct ifreq ifr;
	int fd;

	if (argc != 3) {
		printf ("Usage: %s <old interface name> <new interface name>\n", argv[0]);
		return 1;
	}
	
	if (strlen (argv[1]) > IFNAMSIZ - 1 || strlen (argv[2]) > IFNAMSIZ - 1) {
		printf ("String too long (max length is %d chars)\n", IFNAMSIZ - 1);
		return 2;
	}

	strcpy (ifr.ifr_name, argv[1]);
	strcpy (ifr.ifr_newname, argv[2]);

	fd = socket (PF_INET, SOCK_STREAM, 0);
	if (fd < 0) {
		printf ("I cannot create a normal TCP socket. It is, of course, possible "
		"to not build your kernel with TCP/IP support, in which case you have to " 
		"hack this utility to work you wizard you.\n");
		return 3;
	}
	if (ioctl (fd, SIOCSIFNAME, &ifr) == -1) {
		perror ("ioctl");
		printf ("Are you root? Is %s down? Does %s even exist?\n", argv[0], 
			argv[0]);
		return 4;
	}

	return 0;
}