]> git.ipfire.org Git - people/ms/network.git/blob - src/networkd/zones.c
networkd: Refactor enumerating zones
[people/ms/network.git] / src / networkd / zones.c
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 <dirent.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/queue.h>
25
26 #include "daemon.h"
27 #include "logging.h"
28 #include "string.h"
29 #include "util.h"
30 #include "zone.h"
31 #include "zones.h"
32
33 struct nw_zones_entry {
34 nw_zone* zone;
35
36 // Link to the other entries
37 STAILQ_ENTRY(nw_zones_entry) nodes;
38 };
39
40 struct nw_zones {
41 nw_daemon* daemon;
42 int nrefs;
43
44 // Zone Entries
45 STAILQ_HEAD(entries, nw_zones_entry) entries;
46
47 // A counter of the zone entries
48 unsigned int num;
49 };
50
51 int nw_zones_create(nw_zones** zones, nw_daemon* daemon) {
52 nw_zones* z = calloc(1, sizeof(*z));
53 if (!z)
54 return 1;
55
56 // Store a reference to the daemon
57 z->daemon = nw_daemon_ref(daemon);
58
59 // Initialize the reference counter
60 z->nrefs = 1;
61
62 // Initialize entries
63 STAILQ_INIT(&z->entries);
64
65 // Reference the pointer
66 *zones = z;
67
68 return 0;
69 }
70
71 static void nw_zones_free(nw_zones* zones) {
72 struct nw_zones_entry* entry = NULL;
73
74 while (!STAILQ_EMPTY(&zones->entries)) {
75 entry = STAILQ_FIRST(&zones->entries);
76
77 // Dereference the zone
78 nw_zone_unref(entry->zone);
79
80 // Remove the entry from the list
81 STAILQ_REMOVE_HEAD(&zones->entries, nodes);
82
83 // Free the entry
84 free(entry);
85 }
86
87 if (zones->daemon)
88 nw_daemon_unref(zones->daemon);
89
90 free(zones);
91 }
92
93 nw_zones* nw_zones_ref(nw_zones* zones) {
94 zones->nrefs++;
95
96 return zones;
97 }
98
99 nw_zones* nw_zones_unref(nw_zones* zones) {
100 if (--zones->nrefs > 0)
101 return zones;
102
103 nw_zones_free(zones);
104 return NULL;
105 }
106
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));
110 if (!entry)
111 return 1;
112
113 // Reference the zone
114 entry->zone = nw_zone_ref(zone);
115
116 // Add it to the list
117 STAILQ_INSERT_TAIL(&zones->entries, entry, nodes);
118
119 // Increment the counter
120 zones->num++;
121
122 return 0;
123 }
124
125 static int __nw_zones_enumerate(const char* path, const struct stat* s, void* data) {
126 nw_zone* zone = NULL;
127 int r;
128
129 nw_zones* zones = (nw_zones*)data;
130
131 // Skip anything that isn't a directory
132 if (!S_ISDIR(s->st_mode))
133 return 0;
134
135 // Find the basename of the file
136 const char* name = nw_path_basename(path);
137
138 // Break on invalid paths
139 if (!name)
140 return 0;
141
142 // Skip any hidden files
143 if (*name == '.')
144 return 0;
145
146 // Create a new zone
147 r = nw_zone_create(&zone, zones->daemon, name);
148 if (r)
149 goto ERROR;
150
151 // Add the zone to the list
152 r = nw_zones_add_zone(zones, zone);
153 if (r)
154 goto ERROR;
155
156 ERROR:
157 if (zone)
158 nw_zone_unref(zone);
159
160 return r;
161 }
162
163 int nw_zones_enumerate(nw_zones* zones) {
164 return nw_ftw(ZONE_CONFIG_DIR, ZONE_CONFIG_DIR "/*", __nw_zones_enumerate, zones);
165 }
166
167 size_t nw_zones_num(nw_zones* zones) {
168 struct nw_zones_entry* entry = NULL;
169 size_t length = 0;
170
171 // Count all zones
172 STAILQ_FOREACH(entry, &zones->entries, nodes)
173 length++;
174
175 return length;
176 }
177
178 nw_zone* nw_zones_get_by_name(nw_zones* zones, const char* name) {
179 struct nw_zones_entry* entry = NULL;
180
181 STAILQ_FOREACH(entry, &zones->entries, nodes) {
182 const char* __name = nw_zone_name(entry->zone);
183
184 // If the name matches, return a reference to the zone
185 if (strcmp(name, __name) == 0)
186 return nw_zone_ref(entry->zone);
187 }
188
189 // No match found
190 return NULL;
191 }
192
193 int nw_zones_bus_paths(nw_zones* zones, char*** paths) {
194 struct nw_zones_entry* entry = NULL;
195 char* path = NULL;
196
197 // Allocate an array for all paths
198 char** p = calloc(zones->num + 1, sizeof(*p));
199 if (!p)
200 return 1;
201
202 unsigned int i = 0;
203
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);
208 if (!path)
209 goto ERROR;
210
211 // Append the bus path to the array
212 p[i++] = path;
213 }
214
215 // Return pointer
216 *paths = p;
217
218 return 0;
219
220 ERROR:
221 if (p) {
222 for (char** e = p; *e; e++)
223 free(*e);
224 free(p);
225 }
226
227 return 1;
228 }