]> git.ipfire.org Git - network.git/blame - src/networkd/link.c
networkd: Add a container for links
[network.git] / src / networkd / link.c
CommitLineData
87a1e1e0
MT
1/*#############################################################################
2# #
3# IPFire.org - A linux based firewall #
4# Copyright (C) 2023 IPFire Network Development Team #
5# #
6# This program is free software: you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation, either version 3 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program. If not, see <http://www.gnu.org/licenses/>. #
18# #
19#############################################################################*/
20
21#include <stddef.h>
22#include <stdlib.h>
23
24#include "daemon.h"
25#include "link.h"
26
27struct nw_link {
28 struct nw_daemon* daemon;
29 int nrefs;
30
31 // Interface Index
32 int ifindex;
33};
34
35int nw_link_create(struct nw_link** link, struct nw_daemon* daemon, int ifindex) {
36 // Allocate a new object
37 struct nw_link* l = calloc(1, sizeof(*l));
38 if (!l)
39 return 1;
40
41 // Store a reference to the daemon
42 l->daemon = nw_daemon_ref(daemon);
43
44 // Initialize the reference counter
45 l->nrefs = 1;
46
47 // Store the ifindex
48 l->ifindex = ifindex;
49
50 *link = l;
51
52 return 0;
53}
54
55static void nw_link_free(struct nw_link* link) {
56 if (link->daemon)
57 nw_daemon_unref(link->daemon);
58}
59
60struct nw_link* nw_link_ref(struct nw_link* link) {
61 link->nrefs++;
62
63 return link;
64}
65
66struct nw_link* nw_link_unref(struct nw_link* link) {
67 if (--link->nrefs > 0)
68 return link;
69
70 nw_link_free(link);
71 return NULL;
72}