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

index 80218d93bb3892e43e47527076247eaf30529c82..e8946b7747c9516517b8f93789a2dd92e57bfda1 100644 (file)
@@ -318,6 +318,8 @@ dist_networkd_SOURCES = \
        src/networkd/daemon-bus.h \
        src/networkd/devmon.c \
        src/networkd/devmon.h \
+       src/networkd/link.c \
+       src/networkd/link.h \
        src/networkd/logging.h \
        src/networkd/main.c \
        src/networkd/string.h \
diff --git a/src/networkd/link.c b/src/networkd/link.c
new file mode 100644 (file)
index 0000000..06e3101
--- /dev/null
@@ -0,0 +1,72 @@
+/*#############################################################################
+#                                                                             #
+# 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 "daemon.h"
+#include "link.h"
+
+struct nw_link {
+       struct nw_daemon* daemon;
+       int nrefs;
+
+       // Interface Index
+       int ifindex;
+};
+
+int nw_link_create(struct nw_link** link, struct nw_daemon* daemon, int ifindex) {
+       // Allocate a new object
+       struct nw_link* 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;
+
+       // Store the ifindex
+       l->ifindex = ifindex;
+
+       *link = l;
+
+       return 0;
+}
+
+static void nw_link_free(struct nw_link* link) {
+       if (link->daemon)
+               nw_daemon_unref(link->daemon);
+}
+
+struct nw_link* nw_link_ref(struct nw_link* link) {
+       link->nrefs++;
+
+       return link;
+}
+
+struct nw_link* nw_link_unref(struct nw_link* link) {
+       if (--link->nrefs > 0)
+               return link;
+
+       nw_link_free(link);
+       return NULL;
+}
diff --git a/src/networkd/link.h b/src/networkd/link.h
new file mode 100644 (file)
index 0000000..cd79fde
--- /dev/null
@@ -0,0 +1,31 @@
+/*#############################################################################
+#                                                                             #
+# 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_LINK_H
+#define NETWORKD_LINK_H
+
+struct nw_link;
+
+int nw_link_create(struct nw_link** link, struct nw_daemon* daemon, int ifindex);
+
+struct nw_link* nw_link_ref(struct nw_link* link);
+struct nw_link* nw_link_unref(struct nw_link* link);
+
+#endif /* NETWORKD_LINK_H */