]>
git.ipfire.org Git - people/ms/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>
33 struct nw_zones_entry
{
36 // Link to the other entries
37 STAILQ_ENTRY(nw_zones_entry
) nodes
;
45 STAILQ_HEAD(entries
, nw_zones_entry
) entries
;
47 // A counter of the zone entries
51 int nw_zones_create(nw_zones
** zones
, nw_daemon
* daemon
) {
52 nw_zones
* z
= calloc(1, sizeof(*z
));
56 // Store a reference to the daemon
57 z
->daemon
= nw_daemon_ref(daemon
);
59 // Initialize the reference counter
63 STAILQ_INIT(&z
->entries
);
65 // Reference the pointer
71 static void nw_zones_free(nw_zones
* zones
) {
72 struct nw_zones_entry
* entry
= NULL
;
74 while (!STAILQ_EMPTY(&zones
->entries
)) {
75 entry
= STAILQ_FIRST(&zones
->entries
);
77 // Dereference the zone
78 nw_zone_unref(entry
->zone
);
80 // Remove the entry from the list
81 STAILQ_REMOVE_HEAD(&zones
->entries
, nodes
);
88 nw_daemon_unref(zones
->daemon
);
93 nw_zones
* nw_zones_ref(nw_zones
* zones
) {
99 nw_zones
* nw_zones_unref(nw_zones
* zones
) {
100 if (--zones
->nrefs
> 0)
103 nw_zones_free(zones
);
107 int nw_zones_save(nw_zones
* zones
) {
108 struct nw_zones_entry
* entry
= NULL
;
111 STAILQ_FOREACH(entry
, &zones
->entries
, nodes
) {
112 r
= nw_zone_save(entry
->zone
);
120 static int nw_zones_add_zone(nw_zones
* zones
, nw_zone
* zone
) {
121 // Allocate a new entry
122 struct nw_zones_entry
* entry
= calloc(1, sizeof(*entry
));
126 // Reference the zone
127 entry
->zone
= nw_zone_ref(zone
);
129 // Add it to the list
130 STAILQ_INSERT_TAIL(&zones
->entries
, entry
, nodes
);
132 // Increment the counter
138 static int __nw_zones_enumerate(const char* path
, const struct stat
* s
, void* data
) {
139 nw_zone
* zone
= NULL
;
142 nw_zones
* zones
= (nw_zones
*)data
;
144 // Skip anything that isn't a directory
145 if (!S_ISDIR(s
->st_mode
))
148 // Find the basename of the file
149 const char* name
= nw_path_basename(path
);
151 // Break on invalid paths
155 // Skip any hidden files
160 r
= nw_zone_create(&zone
, zones
->daemon
, name
);
164 // Add the zone to the list
165 r
= nw_zones_add_zone(zones
, zone
);
176 int nw_zones_enumerate(nw_zones
* zones
) {
177 return nw_ftw(ZONE_CONFIG_DIR
, ZONE_CONFIG_DIR
"/*", __nw_zones_enumerate
, zones
);
180 size_t nw_zones_num(nw_zones
* zones
) {
181 struct nw_zones_entry
* entry
= NULL
;
185 STAILQ_FOREACH(entry
, &zones
->entries
, nodes
)
191 nw_zone
* nw_zones_get_by_name(nw_zones
* zones
, const char* name
) {
192 struct nw_zones_entry
* entry
= NULL
;
194 STAILQ_FOREACH(entry
, &zones
->entries
, nodes
) {
195 const char* __name
= nw_zone_name(entry
->zone
);
197 // If the name matches, return a reference to the zone
198 if (strcmp(name
, __name
) == 0)
199 return nw_zone_ref(entry
->zone
);
206 int nw_zones_bus_paths(nw_zones
* zones
, char*** paths
) {
207 struct nw_zones_entry
* entry
= NULL
;
210 // Allocate an array for all paths
211 char** p
= calloc(zones
->num
+ 1, sizeof(*p
));
217 // Walk through all zones
218 STAILQ_FOREACH(entry
, &zones
->entries
, nodes
) {
219 // Generate the bus path
220 path
= nw_zone_bus_path(entry
->zone
);
224 // Append the bus path to the array
235 for (char** e
= p
; *e
; e
++)