]> git.ipfire.org Git - network.git/commitdiff
networkd: Import interface name
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Feb 2023 10:03:08 +0000 (10:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Feb 2023 10:03:08 +0000 (10:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/link.c

index 32a66cdf04873579b818eb0194399e39c372f099..40f809f532344013d5f13249819338ca42468465 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <net/if.h>
 #include <stddef.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <systemd/sd-netlink.h>
 
@@ -27,6 +29,7 @@
 #include "link.h"
 #include "links.h"
 #include "logging.h"
+#include "string.h"
 
 struct nw_link {
        struct nw_daemon* daemon;
@@ -34,6 +37,9 @@ struct nw_link {
 
        // Interface Index
        int ifindex;
+
+       // Interface Name
+       char ifname[IF_NAMESIZE];
 };
 
 int nw_link_create(struct nw_link** link, struct nw_daemon* daemon, int ifindex) {
@@ -83,6 +89,47 @@ int nw_link_ifindex(struct nw_link* link) {
        return link->ifindex;
 }
 
+static int nw_link_update_ifname(struct nw_link* link, sd_netlink_message* message) {
+       const char* ifname = NULL;
+       int r;
+
+       r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
+       if (r < 0) {
+               ERROR("Could not read link name for link %d: %m\n", link->ifindex);
+               return 1;
+       }
+
+       // Do nothing if the name is already set
+       if (strcmp(link->ifname, ifname) == 0)
+               return 0;
+
+       // Otherwise update the name
+       r = nw_string_set(link->ifname, ifname);
+       if (r) {
+               ERROR("Could not set link name: %m\n");
+               return 1;
+       }
+
+       DEBUG("Link %d has been renamed to '%s'\n", link->ifindex, link->ifname);
+
+       return 0;
+}
+
+/*
+       This function is called whenever anything changes, so that we can
+       update our internal link object.
+*/
+static int nw_link_update(struct nw_link* link, sd_netlink_message* message) {
+       int r;
+
+       // Update the interface name
+       r = nw_link_update_ifname(link, message);
+       if (r)
+               return r;
+
+       return 0;
+}
+
 int nw_link_process(sd_netlink* rtnl, sd_netlink_message* message, void* data) {
        struct nw_links* links = NULL;
        struct nw_link* link = NULL;
@@ -166,7 +213,10 @@ int nw_link_process(sd_netlink* rtnl, sd_netlink_message* message, void* data) {
                                        goto ERROR;
                        }
 
-                       // TODO Import any data from the netlink message
+                       // Import any data from the netlink message
+                       r = nw_link_update(link, message);
+                       if (r)
+                               goto ERROR;
 
                        break;