]> git.ipfire.org Git - network.git/commitdiff
networkd: Add port container
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Feb 2023 16:26:36 +0000 (16:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Feb 2023 16:26:36 +0000 (16:26 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/networkd/daemon.c
src/networkd/ports.c [new file with mode: 0644]
src/networkd/ports.h [new file with mode: 0644]

index 4bd397531a407da4c912f9341d6ec7c37f6ba01d..d78d61ee602b10040a595485c79c29b514db72c6 100644 (file)
@@ -326,6 +326,8 @@ dist_networkd_SOURCES = \
        src/networkd/main.c \
        src/networkd/port.c \
        src/networkd/port.h \
+       src/networkd/ports.c \
+       src/networkd/ports.h \
        src/networkd/string.h \
        src/networkd/zones.c \
        src/networkd/zones.h \
index dc2aed6ff7bbed7c7559b5861d800f2f4e83aea9..aee27e3d076a795f0ce56ac745c86942d75f37d3 100644 (file)
@@ -34,6 +34,7 @@
 #include "link.h"
 #include "links.h"
 #include "logging.h"
+#include "ports.h"
 #include "zone.h"
 #include "zones.h"
 
@@ -62,6 +63,10 @@ struct nw_daemon {
 
        // Zones
        struct nw_zones* zones;
+
+       // Ports
+       struct nw_ports* ports;
+
 };
 
 static int __nw_daemon_terminate(sd_event_source* source, const struct signalfd_siginfo* si,
@@ -248,6 +253,17 @@ static int nw_daemon_enumerate_links(struct nw_daemon* daemon) {
        return nw_links_enumerate(daemon->links);
 }
 
+static int nw_daemon_enumerate_ports(struct nw_daemon* daemon) {
+       int r;
+
+       // Create a new ports container
+       r = nw_ports_create(&daemon->ports, daemon);
+       if (r)
+               return r;
+
+       return nw_ports_enumerate(daemon->ports);
+}
+
 static int nw_daemon_enumerate(struct nw_daemon* daemon) {
        int r;
 
@@ -256,6 +272,11 @@ static int nw_daemon_enumerate(struct nw_daemon* daemon) {
        if (r)
                return r;
 
+       // Ports
+       r = nw_daemon_enumerate_ports(daemon);
+       if (r)
+               return r;
+
        return 0;
 }
 
@@ -322,6 +343,8 @@ ERROR:
 }
 
 static void nw_daemon_cleanup(struct nw_daemon* daemon) {
+       if (daemon->ports)
+               nw_ports_unref(daemon->ports);
        if (daemon->zones)
                nw_zones_unref(daemon->zones);
        if (daemon->links)
diff --git a/src/networkd/ports.c b/src/networkd/ports.c
new file mode 100644 (file)
index 0000000..500153a
--- /dev/null
@@ -0,0 +1,100 @@
+/*#############################################################################
+#                                                                             #
+# 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 <stdlib.h>
+#include <string.h>
+#include <sys/queue.h>
+
+#include "logging.h"
+#include "port.h"
+#include "ports.h"
+
+struct nw_ports_entry {
+       struct nw_port* port;
+
+       // Link to the other entries
+       STAILQ_ENTRY(nw_ports_entry) nodes;
+};
+
+struct nw_ports {
+       struct nw_daemon* daemon;
+       int nrefs;
+
+       // Port Entries
+       STAILQ_HEAD(entries, nw_ports_entry) entries;
+
+       // A counter of the port entries
+       unsigned int num;
+};
+
+int nw_ports_create(struct nw_ports** ports, struct nw_daemon* daemon) {
+       struct nw_ports* p = calloc(1, sizeof(*p));
+       if (!p)
+               return 1;
+
+       // Store a reference to the daemon
+       p->daemon = nw_daemon_ref(daemon);
+
+       // Initialize the reference counter
+       p->nrefs = 1;
+
+       // Initialize entries
+       STAILQ_INIT(&p->entries);
+
+       // Reference the pointer
+       *ports = p;
+
+       return 0;
+}
+
+static void nw_ports_free(struct nw_ports* ports) {
+       struct nw_ports_entry* entry = NULL;
+
+       while (!STAILQ_EMPTY(&ports->entries)) {
+               entry = STAILQ_FIRST(&ports->entries);
+
+               // Dereference the port
+               nw_port_unref(entry->port);
+
+               // Remove the entry from the list
+               STAILQ_REMOVE_HEAD(&ports->entries, nodes);
+
+               // Free the entry
+               free(entry);
+       }
+}
+
+struct nw_ports* nw_ports_ref(struct nw_ports* ports) {
+       ports->nrefs++;
+
+       return ports;
+}
+
+struct nw_ports* nw_ports_unref(struct nw_ports* ports) {
+       if (--ports->nrefs > 0)
+               return ports;
+
+       nw_ports_free(ports);
+       return NULL;
+}
+
+int nw_ports_enumerate(struct nw_ports* ports) {
+       return 0; // XXX TODO
+}
diff --git a/src/networkd/ports.h b/src/networkd/ports.h
new file mode 100644 (file)
index 0000000..76a4a3a
--- /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_PORTS_H
+#define NETWORKD_PORTS_H
+
+struct nw_ports;
+
+#include "daemon.h"
+
+int nw_ports_create(struct nw_ports** ports, struct nw_daemon* daemon);
+
+struct nw_ports* nw_ports_ref(struct nw_ports* ports);
+struct nw_ports* nw_ports_unref(struct nw_ports* ports);
+
+int nw_ports_enumerate(struct nw_ports* ports);
+
+#endif /* NETWORKD_ZONES_H */