]>
git.ipfire.org Git - network.git/blob - src/networkd/zones.c
1 /*#############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
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. #
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. #
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/>. #
19 #############################################################################*/
24 #include <sys/queue.h>
30 struct nw_zones_entry
{
33 // Link to the other entries
34 STAILQ_ENTRY(nw_zones_entry
) nodes
;
41 STAILQ_HEAD(entries
, nw_zones_entry
) entries
;
43 // A counter of the zone entries
47 static int nw_zones_create(struct nw_zones
** zones
) {
48 struct nw_zones
* z
= calloc(1, sizeof(*z
));
52 // Initialize the reference counter
56 STAILQ_INIT(&z
->entries
);
58 // Reference the pointer
64 static void nw_zones_free(struct nw_zones
* zones
) {
65 struct nw_zones_entry
* entry
= NULL
;
67 while (!STAILQ_EMPTY(&zones
->entries
)) {
68 entry
= STAILQ_FIRST(&zones
->entries
);
70 // Dereference the zone
71 nw_zone_unref(entry
->zone
);
73 // Remove the entry from the list
74 STAILQ_REMOVE_HEAD(&zones
->entries
, nodes
);
81 struct nw_zones
* nw_zones_ref(struct nw_zones
* zones
) {
87 struct nw_zones
* nw_zones_unref(struct nw_zones
* zones
) {
88 if (--zones
->nrefs
> 0)
95 static int nw_zones_add_zone(struct nw_zones
* zones
, struct nw_zone
* zone
) {
96 // Allocate a new entry
97 struct nw_zones_entry
* entry
= calloc(1, sizeof(*entry
));
101 // Reference the zone
102 entry
->zone
= nw_zone_ref(zone
);
104 // Add it to the list
105 STAILQ_INSERT_TAIL(&zones
->entries
, entry
, nodes
);
107 // Increment the counter
113 static int nw_zones_load_filter(const struct dirent
* path
) {
114 const char* fn
= path
->d_name
;
116 // Ignore everything starting with '.'
120 // Ignore anything that isn't a directory
121 if (path
->d_type
!= DT_DIR
)
127 static int __nw_zones_load(struct nw_zones
* zones
) {
128 struct dirent
** paths
= NULL
;
132 struct nw_zone
* zone
= NULL
;
134 // Scan the zones directory
135 n
= scandir(CONFIG_DIR
"/zones", &paths
, nw_zones_load_filter
, alphasort
);
137 ERROR("Could not load zones: %m\n");
141 DEBUG("Found %d zone(s)\n", n
);
144 for (int i
= 0; i
< n
; i
++) {
145 const char* name
= paths
[i
]->d_name
;
147 DEBUG("Loading zone '%s'...\n", name
);
149 // Create a new zone object
150 r
= nw_zone_create(&zone
, name
);
155 r
= nw_zones_add_zone(zones
, zone
);
167 for (int i
= 0; i
< n
; i
++) {
176 int nw_zones_load(struct nw_zones
** zones
) {
179 // Create a new zones object
180 r
= nw_zones_create(zones
);
185 r
= __nw_zones_load(*zones
);
192 nw_zones_unref(*zones
);
196 size_t nw_zones_num(struct nw_zones
* zones
) {
197 struct nw_zones_entry
* entry
= NULL
;
201 STAILQ_FOREACH(entry
, &zones
->entries
, nodes
)
207 struct nw_zone
* nw_zones_get_by_name(struct nw_zones
* zones
, const char* name
) {
208 struct nw_zones_entry
* entry
= NULL
;
210 STAILQ_FOREACH(entry
, &zones
->entries
, nodes
) {
211 const char* __name
= nw_zone_name(entry
->zone
);
213 // If the name matches, return a reference to the zone
214 if (strcmp(name
, __name
) == 0)
215 return nw_zone_ref(entry
->zone
);
222 int nw_zones_bus_paths(struct nw_zones
* zones
, char*** paths
) {
223 struct nw_zones_entry
* entry
= NULL
;
226 // Allocate an array for all paths
227 char** p
= calloc(zones
->num
+ 1, sizeof(*p
));
233 // Walk through all zones
234 STAILQ_FOREACH(entry
, &zones
->entries
, nodes
) {
235 // Generate the bus path
236 path
= nw_zone_bus_path(entry
->zone
);
240 // Append the bus path to the array
251 for (char** e
= p
; *e
; e
++)