From: Michael Tremer Date: Thu, 9 Feb 2023 20:05:00 +0000 (+0000) Subject: networkd: Add a link object X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=87a1e1e0b052f814aac355b60aedf736eef0bead;p=network.git networkd: Add a link object Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 80218d93..e8946b77 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 00000000..06e31017 --- /dev/null +++ b/src/networkd/link.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include + +#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 index 00000000..cd79fde7 --- /dev/null +++ b/src/networkd/link.h @@ -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 . # +# # +#############################################################################*/ + +#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 */