]> git.ipfire.org Git - network.git/commitdiff
networkd: Add method to fetch corresponding link to port
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2023 14:10:16 +0000 (14:10 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2023 14:10:16 +0000 (14:10 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/daemon.c
src/networkd/daemon.h
src/networkd/link.h
src/networkd/links.c
src/networkd/links.h
src/networkd/port.c
src/networkd/port.h
src/networkd/ports.c

index 795a8b761b5d611fa3cc8a3ae1452c569341d629..c4acff55f8ed5fe43c992d24ab0232434024b843 100644 (file)
@@ -448,6 +448,13 @@ struct nw_link* nw_daemon_get_link_by_ifindex(struct nw_daemon* daemon, int ifin
        return nw_links_get_by_ifindex(daemon->links, ifindex);
 }
 
+struct nw_link* nw_daemon_get_link_by_name(struct nw_daemon* daemon, const char* name) {
+       if (!daemon->links)
+               return NULL;
+
+       return nw_links_get_by_name(daemon->links, name);
+}
+
 /*
        Ports
 */
index e08537e891000adf2c9d64b0dd92d82ed895f92b..a40dd60d97e602e6cc63f462ba682c6de113d49b 100644 (file)
@@ -48,6 +48,7 @@ sd_netlink* nw_daemon_get_rtnl(struct nw_daemon* daemon);
 struct nw_links* nw_daemon_links(struct nw_daemon* daemon);
 void nw_daemon_drop_link(struct nw_daemon* daemon, struct nw_link* link);
 struct nw_link* nw_daemon_get_link_by_ifindex(struct nw_daemon* daemon, int ifindex);
+struct nw_link* nw_daemon_get_link_by_name(struct nw_daemon* daemon, const char* name);
 
 /*
        Ports
index e482d71ac67df4ccb58011654445222cfe45901e..72ddfa99d5675ee67435da06b2f93c6fc729142c 100644 (file)
 #ifndef NETWORKD_LINK_H
 #define NETWORKD_LINK_H
 
-#include "daemon.h"
-
 struct nw_link;
 
+#include "daemon.h"
+
 int nw_link_create(struct nw_link** link, struct nw_daemon* daemon, int ifindex);
 
 struct nw_link* nw_link_ref(struct nw_link* link);
 struct nw_link* nw_link_unref(struct nw_link* link);
 
 int nw_link_ifindex(struct nw_link* link);
+const char* nw_link_name(struct nw_link* link);
 
 int nw_link_process(sd_netlink* rtnl, sd_netlink_message* message, void* data);
 
index 044a27dc85909ef2d43f982ea4616fe19781bfd9..7aa83a310957e28b176696a9695f97a9e91ddfb3 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <stddef.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/queue.h>
 
 #include "daemon.h"
@@ -192,3 +193,17 @@ struct nw_link* nw_links_get_by_ifindex(struct nw_links* links, int ifindex) {
 
        return nw_link_ref(entry->link);
 }
+
+struct nw_link* nw_links_get_by_name(struct nw_links* links, const char* name) {
+       struct nw_links_entry* entry = NULL;
+
+       STAILQ_FOREACH(entry, &links->entries, nodes) {
+               const char* n = nw_link_name(entry->link);
+
+               if (strcmp(name, n) == 0)
+                       return nw_link_ref(entry->link);
+       }
+
+       // No match found
+       return NULL;
+}
index 2f2bbeab044a4584f8d663f46bb97a81547cb8e4..2b5c787685a2a28bb6aa9fbba7d14b181e3b287f 100644 (file)
@@ -37,5 +37,6 @@ void nw_links_drop_link(struct nw_links* links, struct nw_link* link);
 int nw_links_enumerate(struct nw_links* links);
 
 struct nw_link* nw_links_get_by_ifindex(struct nw_links* links, int ifindex);
+struct nw_link* nw_links_get_by_name(struct nw_links* links, const char* name);
 
 #endif /* NETWORKD_LINKS_H */
index e59e760626583c9fa280ee8723f1fa327592f064..33e75d70b4245ce801d82cc203d02245a2057d60 100644 (file)
 
 #include "address.h"
 #include "config.h"
+#include "link.h"
 #include "logging.h"
 #include "string.h"
 #include "port.h"
 
 struct nw_port {
+       struct nw_daemon* daemon;
        int nrefs;
 
        char name[IF_NAMESIZE];
@@ -66,6 +68,8 @@ static nw_port_type_t nw_port_type_from_string(const char* s) {
 static void nw_port_free(struct nw_port* port) {
        if (port->config)
                nw_config_unref(port->config);
+       if (port->daemon)
+               nw_daemon_unref(port->daemon);
 
        free(port);
 }
@@ -163,7 +167,7 @@ static int nw_port_setup(struct nw_port* port) {
        return 0;
 }
 
-int nw_port_create(struct nw_port** port, const char* name) {
+int nw_port_create(struct nw_port** port, struct nw_daemon* daemon, const char* name) {
        int r;
 
        // Allocate a new object
@@ -171,6 +175,9 @@ int nw_port_create(struct nw_port** port, const char* name) {
        if (!p)
                return 1;
 
+       // Store a reference to the daemon
+       p->daemon = nw_daemon_ref(daemon);
+
        // Initialize reference counter
        p->nrefs = 1;
 
@@ -222,6 +229,10 @@ char* nw_port_bus_path(struct nw_port* port) {
        return p;
 }
 
+static struct nw_link* nw_port_get_link(struct nw_port* port) {
+       return nw_daemon_get_link_by_name(port->daemon, port->name);
+}
+
 const nw_address_t* nw_port_get_address(struct nw_port* port) {
        return &port->address;
 }
index caf1433b47a7251884171c8caa7e8bbc5be2c81c..92d60b291cbc822c2b22835d0bb2788ab3ca6454 100644 (file)
@@ -31,8 +31,9 @@ typedef enum nw_port_type {
 struct nw_port;
 
 #include "address.h"
+#include "daemon.h"
 
-int nw_port_create(struct nw_port** port, const char* name);
+int nw_port_create(struct nw_port** port, struct nw_daemon* daemon, const char* name);
 
 struct nw_port* nw_port_ref(struct nw_port* port);
 struct nw_port* nw_port_unref(struct nw_port* port);
index 24bd257915cb6a3ce2d7496e6de5e992148664d1..9cec111532ed117fb70a7528a4a2f648b08049ed 100644 (file)
@@ -138,7 +138,7 @@ static int __nw_ports_enumerate(const char* path, const struct stat* s, void* da
                return 0;
 
        // Create a new port
-       r = nw_port_create(&port, name);
+       r = nw_port_create(&port, ports->daemon, name);
        if (r)
                goto ERROR;