Add support for renaming network interfaces - smdev - suckless mdev
 (HTM) git clone git://git.suckless.org/smdev
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit eed577a75f4e9ce9658cd83d729a561e18b39b70
 (DIR) parent 801a44b69bb6d791197e3cd4a442ba1cc079a4cf
 (HTM) Author: sin <sin@2f30.org>
       Date:   Thu,  4 Sep 2014 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:
         M config.def.h                        |       7 +++++++
         M smdev.c                             |      48 +++++++++++++++++++++++++++++++
       
       2 files changed, 55 insertions(+), 0 deletions(-)
       ---
 (DIR) 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 }
       +};
 (DIR) 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;
       +}