From: Michael Tremer Date: Fri, 10 Feb 2023 16:26:36 +0000 (+0000) Subject: networkd: Add port container X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b8db2713c790000860e1c3b0d4f8e588cfa5ac04;p=network.git networkd: Add port container Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 4bd39753..d78d61ee 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/networkd/daemon.c b/src/networkd/daemon.c index dc2aed6f..aee27e3d 100644 --- a/src/networkd/daemon.c +++ b/src/networkd/daemon.c @@ -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 index 00000000..500153a4 --- /dev/null +++ b/src/networkd/ports.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include +#include + +#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 index 00000000..76a4a3ad --- /dev/null +++ b/src/networkd/ports.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_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 */