[hackers] [smdev] Add support for renaming network interfaces || sin

From: <git_AT_suckless.org>
Date: Thu, 04 Sep 2014 16:06:26 +0200

commit eed577a75f4e9ce9658cd83d729a561e18b39b70
Author: sin <sin_AT_2f30.org>
Date: Thu Sep 4 15:04:26 2014 +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.

diff --git a/config.def.h b/config.def.h
index cdf034c..68ccdca 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -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
index 252d350..a3194da 100644
--- a/smdev.c
+++ b/smdev.c
_AT_@ -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>
_AT_@ -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)
_AT_@ -88,6 +95,8 @@ main(int argc, char *argv[])
                 if (pregcache[i].cached)
                         regfree(&pregcache[i].preg);
 
+ ifrename();
+
         return 0;
 }
 
_AT_@ -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;
+}
Received on Thu Sep 04 2014 - 16:06:26 CEST

This archive was generated by hypermail 2.3.0 : Thu Sep 04 2014 - 16:12:10 CEST