smdev

suckless mdev
git clone git://git.2f30.org/smdev.git
Log | Files | Refs | README | LICENSE

commit 90bcc6f324774c71c7d8d762ab73f0e766b34fc6
parent c287bf3cc8f3cd59c4b73c18162cae09bcae5840
Author: sin <sin@2f30.org>
Date:   Thu Sep  4 15:04:26 +0100

Add support for renaming network interfaces

Since this is the brand new thing, add support for renaming
network interfaces in smdev.  Simply populate the small mac2names[]
table in config.h.

By default smdev does not rename network interfaces.  There's no
command line switch to enable renaming, just add entries to the
table and off you go.

Diffstat:
config.def.h | 7+++++++
smdev.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 55 insertions(+), 0 deletions(-)
diff --git a/config.def.h b/config.def.h @@ -40,3 +40,10 @@ struct rule { { "fuse", "root", "root", 0666, NULL, NULL }, { ".*", "root", "root", 0660, NULL, NULL }, }; + +struct mac2name { + unsigned char mac[6]; + const char *name; +} mac2names[] = { + { .mac = { 0 }, .name = NULL } +}; diff --git a/smdev.c b/smdev.c @@ -1,7 +1,13 @@ /* See LICENSE file for copyright and license details. */ +#include <sys/ioctl.h> #include <sys/stat.h> #include <sys/types.h> +#include <linux/if_packet.h> +#include <net/if.h> +#include <netinet/in.h> +#include <ifaddrs.h> + #include <errno.h> #include <fcntl.h> #include <grp.h> @@ -55,6 +61,7 @@ static int createdev(struct event *ev); static int doevent(struct event *ev); static int craftev(char *sysfspath); static void populatedev(const char *path); +static int ifrename(void); static void usage(void) @@ -88,6 +95,8 @@ main(int argc, char *argv[]) if (pregcache[i].cached) regfree(&pregcache[i].preg); + ifrename(); + return 0; } @@ -386,3 +395,42 @@ populatedev(const char *path) free(cwd); } } + +static int +ifrename(void) +{ + struct ifaddrs *ifas, *ifa; + struct ifreq ifr; + int sd; + int i; + int r; + + sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + if (sd < 0) + eprintf("socket:"); + r = getifaddrs(&ifas); + if (r < 0) + eprintf("getifaddrs:"); + for (ifa = ifas; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_flags & IFF_LOOPBACK) + continue; + if (ifa->ifa_addr->sa_family == AF_PACKET) { + struct sockaddr_ll *sa = (struct sockaddr_ll *)ifa->ifa_addr; + for (i = 0; mac2names[i].name; i++) { + if (memcmp(mac2names[i].mac, sa->sll_addr, 6) != 0) + continue; + memset(&ifr, 0, sizeof(ifr)); + strlcpy(ifr.ifr_name, + ifa->ifa_name, sizeof(ifr.ifr_name)); + strlcpy(ifr.ifr_newname, + mac2names[i].name, sizeof(ifr.ifr_newname)); + r = ioctl(sd, SIOCSIFNAME, &ifr); + if (r < 0) + eprintf("SIOCSIFNAME:"); + } + } + } + + close(sd); + return 0; +}