]> git.ipfire.org Git - network.git/commitdiff
networkd: Add a container for links
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 9 Feb 2023 20:25:29 +0000 (20:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 9 Feb 2023 20:25:29 +0000 (20:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/networkd/daemon.c
src/networkd/link.h
src/networkd/links.c [new file with mode: 0644]
src/networkd/links.h [new file with mode: 0644]

index e8946b7747c9516517b8f93789a2dd92e57bfda1..024bf457baf6f868201e48af92326d0863890e00 100644 (file)
@@ -320,6 +320,8 @@ dist_networkd_SOURCES = \
        src/networkd/devmon.h \
        src/networkd/link.c \
        src/networkd/link.h \
+       src/networkd/links.c \
+       src/networkd/links.h \
        src/networkd/logging.h \
        src/networkd/main.c \
        src/networkd/string.h \
index 13e12571ac08b8ca2a70caec4e987038efacd070..27b118e9f197b9fd0716adf400cef2402d8d532b 100644 (file)
@@ -31,6 +31,7 @@
 #include "config.h"
 #include "daemon.h"
 #include "devmon.h"
+#include "links.h"
 #include "logging.h"
 #include "zone.h"
 #include "zones.h"
@@ -55,6 +56,9 @@ struct nw_daemon {
        // udev Connection
        sd_device_monitor* devmon;
 
+       // Links
+       struct nw_links* links;
+
        // Zones
        struct nw_zones* zones;
 };
@@ -230,6 +234,28 @@ static int nw_daemon_connect_rtnl(struct nw_daemon* daemon, int fd) {
        return 0;
 }
 
+static int nw_daemon_enumerate_links(struct nw_daemon* daemon) {
+       int r;
+
+       // Create a new links container
+       r = nw_links_create(&daemon->links, daemon);
+       if (r)
+               return r;
+
+       return nw_links_enumerate(daemon->links);
+}
+
+static int nw_daemon_enumerate(struct nw_daemon* daemon) {
+       int r;
+
+       // Links
+       r = nw_daemon_enumerate_links(daemon);
+       if (r)
+               return r;
+
+       return 0;
+}
+
 static int nw_daemon_setup(struct nw_daemon* daemon) {
        int r;
 
@@ -258,6 +284,11 @@ static int nw_daemon_setup(struct nw_daemon* daemon) {
        if (r)
                return r;
 
+       // Enumerate everything we need to know
+       r = nw_daemon_enumerate(daemon);
+       if (r)
+               return r;
+
        return 0;
 }
 
@@ -287,11 +318,19 @@ ERROR:
        return r;
 }
 
-static void nw_daemon_free(struct nw_daemon* daemon) {
+static void nw_daemon_cleanup(struct nw_daemon* daemon) {
        if (daemon->zones)
                nw_zones_unref(daemon->zones);
+       if (daemon->links)
+               nw_links_unref(daemon->links);
        if (daemon->config)
                nw_config_unref(daemon->config);
+}
+
+static void nw_daemon_free(struct nw_daemon* daemon) {
+       // Cleanup common objects
+       nw_daemon_cleanup(daemon);
+
        if (daemon->bus)
                sd_bus_unref(daemon->bus);
        if (daemon->loop)
@@ -330,15 +369,20 @@ int nw_daemon_run(struct nw_daemon* daemon) {
                goto ERROR;
        }
 
-
        // Let systemd know that we are shutting down
        sd_notify(0, "STOPPING=1\n" "STATUS=Shutting down...");
 
+       // Cleanup everything
+       nw_daemon_cleanup(daemon);
+
        return 0;
 
 ERROR:
        sd_notifyf(0, "ERRNO=%i", -r);
 
+       // Cleanup everything
+       nw_daemon_cleanup(daemon);
+
        return 1;
 }
 
index cd79fde7760ed28e2af3874f38a2742063573db7..6ffe843ce2307c552fd8ae606b28ac53fd86f6c3 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef NETWORKD_LINK_H
 #define NETWORKD_LINK_H
 
+#include "daemon.h"
+
 struct nw_link;
 
 int nw_link_create(struct nw_link** link, struct nw_daemon* daemon, int ifindex);
diff --git a/src/networkd/links.c b/src/networkd/links.c
new file mode 100644 (file)
index 0000000..b81d6a5
--- /dev/null
@@ -0,0 +1,105 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2023 IPFire Network Development Team                          #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <sys/queue.h>
+
+#include "daemon.h"
+#include "link.h"
+#include "links.h"
+
+struct nw_links_entry {
+       struct nw_link* link;
+
+       // Link to the other entries
+       STAILQ_ENTRY(nw_links_entry) nodes;
+};
+
+struct nw_links {
+       struct nw_daemon* daemon;
+       int nrefs;
+
+       // Link Entries
+       STAILQ_HEAD(entries, nw_links_entry) entries;
+
+       // A counter of the link entries
+       unsigned int num;
+};
+
+int nw_links_create(struct nw_links** links, struct nw_daemon* daemon) {
+       struct nw_links* l = calloc(1, sizeof(*l));
+       if (!l)
+               return 1;
+
+       // Store a reference to the daemon
+       l->daemon = nw_daemon_ref(daemon);
+
+       // Initialize the reference counter
+       l->nrefs = 1;
+
+       // Initialize entries
+       STAILQ_INIT(&l->entries);
+
+       // Reference the pointer
+       *links = l;
+
+       return 0;
+}
+
+static void nw_links_free(struct nw_links* links) {
+       struct nw_links_entry* entry = NULL;
+
+       while (!STAILQ_EMPTY(&links->entries)) {
+               entry = STAILQ_FIRST(&links->entries);
+
+               // Dereference the zone
+               nw_link_unref(entry->link);
+
+               // Remove the entry from the list
+               STAILQ_REMOVE_HEAD(&links->entries, nodes);
+
+               // Free the entry
+               free(entry);
+       }
+
+       if (links->daemon)
+               nw_daemon_unref(links->daemon);
+}
+
+struct nw_links* nw_links_ref(struct nw_links* links) {
+       links->nrefs++;
+
+       return links;
+}
+
+struct nw_links* nw_links_unref(struct nw_links* links) {
+       if (--links->nrefs > 0)
+               return links;
+
+       nw_links_free(links);
+       return NULL;
+}
+
+int nw_links_enumerate(struct nw_links* links) {
+       // TODO
+
+       return 0;
+}
diff --git a/src/networkd/links.h b/src/networkd/links.h
new file mode 100644 (file)
index 0000000..b2976e4
--- /dev/null
@@ -0,0 +1,35 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2023 IPFire Network Development Team                          #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef NETWORKD_LINKS_H
+#define NETWORKD_LINKS_H
+
+#include "daemon.h"
+
+struct nw_links;
+
+int nw_links_create(struct nw_links** links, struct nw_daemon* daemon);
+
+struct nw_links* nw_links_ref(struct nw_links* links);
+struct nw_links* nw_links_unref(struct nw_links* links);
+
+int nw_links_enumerate(struct nw_links* links);
+
+#endif /* NETWORKD_LINKS_H */