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

index 024bf457baf6f868201e48af92326d0863890e00..4bd397531a407da4c912f9341d6ec7c37f6ba01d 100644 (file)
@@ -324,6 +324,8 @@ dist_networkd_SOURCES = \
        src/networkd/links.h \
        src/networkd/logging.h \
        src/networkd/main.c \
+       src/networkd/port.c \
+       src/networkd/port.h \
        src/networkd/string.h \
        src/networkd/zones.c \
        src/networkd/zones.h \
diff --git a/src/networkd/port.c b/src/networkd/port.c
new file mode 100644 (file)
index 0000000..019521f
--- /dev/null
@@ -0,0 +1,84 @@
+/*#############################################################################
+#                                                                             #
+# 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 <net/if.h>
+#include <stdlib.h>
+
+#include "config.h"
+#include "string.h"
+#include "port.h"
+
+struct nw_port {
+       int nrefs;
+
+       char name[IF_NAMESIZE];
+
+       // Configuration
+       struct nw_config *config;
+};
+
+static void nw_port_free(struct nw_port* port) {
+       if (port->config)
+               nw_config_unref(port->config);
+
+       free(port);
+}
+
+int nw_port_create(struct nw_port** port, const char* name) {
+       int r;
+
+       // Allocate a new object
+       struct nw_port* p = calloc(1, sizeof(*p));
+       if (!p)
+               return 1;
+
+       // Initialize reference counter
+       p->nrefs = 1;
+
+       // Store the name
+       r = nw_string_set(p->name, name);
+       if (r)
+               goto ERROR;
+
+       *port = p;
+       return 0;
+
+ERROR:
+       nw_port_free(p);
+       return r;
+}
+
+struct nw_port* nw_port_ref(struct nw_port* port) {
+       port->nrefs++;
+
+       return port;
+}
+
+struct nw_port* nw_port_unref(struct nw_port* port) {
+       if (--port->nrefs > 0)
+               return port;
+
+       nw_port_free(port);
+       return NULL;
+}
+
+const char* nw_port_name(struct nw_port* port) {
+       return port->name;
+}
diff --git a/src/networkd/port.h b/src/networkd/port.h
new file mode 100644 (file)
index 0000000..cea6203
--- /dev/null
@@ -0,0 +1,33 @@
+/*#############################################################################
+#                                                                             #
+# 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_PORT_H
+#define NETWORKD_PORT_H
+
+struct nw_port;
+
+int nw_port_create(struct nw_port** port, const char* name);
+
+struct nw_port* nw_port_ref(struct nw_port* port);
+struct nw_port* nw_port_unref(struct nw_port* port);
+
+const char* nw_port_name(struct nw_port* port);
+
+#endif /* NETWORKD_PORT_H */