From: Michael Tremer Date: Thu, 9 Feb 2023 20:25:29 +0000 (+0000) Subject: networkd: Add a container for links X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=18b8c8415c595442f796638f85a244915339c974;p=network.git networkd: Add a container for links Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index e8946b77..024bf457 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/networkd/daemon.c b/src/networkd/daemon.c index 13e12571..27b118e9 100644 --- a/src/networkd/daemon.c +++ b/src/networkd/daemon.c @@ -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; } diff --git a/src/networkd/link.h b/src/networkd/link.h index cd79fde7..6ffe843c 100644 --- a/src/networkd/link.h +++ b/src/networkd/link.h @@ -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 index 00000000..b81d6a59 --- /dev/null +++ b/src/networkd/links.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include +#include + +#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 index 00000000..b2976e4a --- /dev/null +++ b/src/networkd/links.h @@ -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 . # +# # +#############################################################################*/ + +#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 */