From: Michael Tremer Date: Fri, 10 Feb 2023 10:03:08 +0000 (+0000) Subject: networkd: Import interface name X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f650f6cd802686e545d41d00be4a0abd67765391;p=network.git networkd: Import interface name Signed-off-by: Michael Tremer --- diff --git a/src/networkd/link.c b/src/networkd/link.c index 32a66cdf..40f809f5 100644 --- a/src/networkd/link.c +++ b/src/networkd/link.c @@ -18,8 +18,10 @@ # # #############################################################################*/ +#include #include #include +#include #include @@ -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;