]>
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 static int nw_zones_add_zone(nw_zones
* zones
, nw_zone
* zone
) {
108 // Allocate a new entry
109 struct nw_zones_entry
* entry
= calloc(1, sizeof(*entry
));
113 // Reference the zone
114 entry
->zone
= nw_zone_ref(zone
);
116 // Add it to the list
117 STAILQ_INSERT_TAIL(&zones
->entries
, entry
, nodes
);
119 // Increment the counter
125 static int __nw_zones_enumerate(const char* path
, const struct stat
* s
, void* data
) {
126 nw_zone
* zone
= NULL
;
129 nw_zones
* zones
= (nw_zones
*)data
;
131 // Skip anything that isn't a directory
132 if (!S_ISDIR(s
->st_mode
))
135 // Find the basename of the file
136 const char* name
= nw_path_basename(path
);
138 // Break on invalid paths
142 // Skip any hidden files
147 r
= nw_zone_create(&zone
, zones
->daemon
, name
);
151 // Add the zone to the list
152 r
= nw_zones_add_zone(zones
, zone
);
163 int nw_zones_enumerate(nw_zones
* zones
) {
164 return nw_ftw(ZONE_CONFIG_DIR
, ZONE_CONFIG_DIR
"/*", __nw_zones_enumerate
, zones
);
167 size_t nw_zones_num(nw_zones
* zones
) {
168 struct nw_zones_entry
* entry
= NULL
;
172 STAILQ_FOREACH(entry
, &zones
->entries
, nodes
)
178 nw_zone
* nw_zones_get_by_name(nw_zones
* zones
, const char* name
) {
179 struct nw_zones_entry
* entry
= NULL
;
181 STAILQ_FOREACH(entry
, &zones
->entries
, nodes
) {
182 const char* __name
= nw_zone_name(entry
->zone
);
184 // If the name matches, return a reference to the zone
185 if (strcmp(name
, __name
) == 0)
186 return nw_zone_ref(entry
->zone
);
193 int nw_zones_bus_paths(nw_zones
* zones
, char*** paths
) {
194 struct nw_zones_entry
* entry
= NULL
;
197 // Allocate an array for all paths
198 char** p
= calloc(zones
->num
+ 1, sizeof(*p
));
204 // Walk through all zones
205 STAILQ_FOREACH(entry
, &zones
->entries
, nodes
) {
206 // Generate the bus path
207 path
= nw_zone_bus_path(entry
->zone
);
211 // Append the bus path to the array
222 for (char** e
= p
; *e
; e
++)