# #
#############################################################################*/
+#include <net/if.h>
#include <stddef.h>
#include <stdlib.h>
+#include <string.h>
#include <systemd/sd-netlink.h>
#include "link.h"
#include "links.h"
#include "logging.h"
+#include "string.h"
struct nw_link {
struct nw_daemon* daemon;
// Interface Index
int ifindex;
+
+ // Interface Name
+ char ifname[IF_NAMESIZE];
};
int nw_link_create(struct nw_link** link, struct nw_daemon* daemon, int ifindex) {
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;
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;