]> git.ipfire.org Git - people/ms/network.git/blob - src/networkd/ports.c
daemon: Don't crash when a port could not be loaded
[people/ms/network.git] / src / networkd / ports.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 <stdlib.h>
22 #include <string.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25
26 #include "logging.h"
27 #include "port.h"
28 #include "ports.h"
29 #include "string.h"
30 #include "util.h"
31
32 struct nw_ports_entry {
33 nw_port* port;
34
35 // Link to the other entries
36 STAILQ_ENTRY(nw_ports_entry) nodes;
37 };
38
39 struct nw_ports {
40 nw_daemon* daemon;
41 int nrefs;
42
43 // Port Entries
44 STAILQ_HEAD(entries, nw_ports_entry) entries;
45
46 // A counter of the port entries
47 unsigned int num;
48 };
49
50 int nw_ports_create(nw_ports** ports, nw_daemon* daemon) {
51 nw_ports* p = calloc(1, sizeof(*p));
52 if (!p)
53 return 1;
54
55 // Store a reference to the daemon
56 p->daemon = nw_daemon_ref(daemon);
57
58 // Initialize the reference counter
59 p->nrefs = 1;
60
61 // Initialize entries
62 STAILQ_INIT(&p->entries);
63
64 // Reference the pointer
65 *ports = p;
66
67 return 0;
68 }
69
70 static void nw_ports_free(nw_ports* ports) {
71 struct nw_ports_entry* entry = NULL;
72
73 while (!STAILQ_EMPTY(&ports->entries)) {
74 entry = STAILQ_FIRST(&ports->entries);
75
76 // Dereference the port
77 nw_port_unref(entry->port);
78
79 // Remove the entry from the list
80 STAILQ_REMOVE_HEAD(&ports->entries, nodes);
81
82 // Free the entry
83 free(entry);
84 }
85 }
86
87 nw_ports* nw_ports_ref(nw_ports* ports) {
88 ports->nrefs++;
89
90 return ports;
91 }
92
93 nw_ports* nw_ports_unref(nw_ports* ports) {
94 if (--ports->nrefs > 0)
95 return ports;
96
97 nw_ports_free(ports);
98 return NULL;
99 }
100
101 int nw_ports_save(nw_ports* ports) {
102 struct nw_ports_entry* entry = NULL;
103 int r;
104
105 STAILQ_FOREACH(entry, &ports->entries, nodes) {
106 r = nw_port_save(entry->port);
107 if (r)
108 return r;
109 }
110
111 return 0;
112 }
113
114 static int nw_ports_add_port(nw_ports* ports, nw_port* port) {
115 // Allocate a new entry
116 struct nw_ports_entry* entry = calloc(1, sizeof(*entry));
117 if (!entry)
118 return 1;
119
120 // Reference the port
121 entry->port = nw_port_ref(port);
122
123 // Add it to the list
124 STAILQ_INSERT_TAIL(&ports->entries, entry, nodes);
125
126 // Increment the counter
127 ports->num++;
128
129 return 0;
130 }
131
132 static int __nw_ports_enumerate(const char* path, const struct stat* s, void* data) {
133 nw_port* port = NULL;
134 int r;
135
136 nw_ports* ports = (nw_ports*)data;
137
138 // Skip anything that isn't a regular file
139 if (!S_ISREG(s->st_mode))
140 return 0;
141
142 // Find the basename of the file
143 const char* name = nw_path_basename(path);
144
145 // Break on invalid paths
146 if (!name)
147 return 0;
148
149 // Skip any hidden files
150 if (*name == '.')
151 return 0;
152
153 // Create a new port
154 r = nw_port_create_from_config(&port, ports->daemon, name, path);
155 switch (r) {
156 // All okay
157 case 0:
158 break;
159
160 // Invalid configuration
161 case 1:
162 ERROR("Could not open port %s\n", name);
163 r = 0;
164 goto ERROR;
165
166 default:
167 goto ERROR;
168 }
169
170 // Add the port to the list
171 r = nw_ports_add_port(ports, port);
172 if (r)
173 goto ERROR;
174
175 ERROR:
176 if (port)
177 nw_port_unref(port);
178
179 return r;
180 }
181
182 int nw_ports_enumerate(nw_ports* ports) {
183 return nw_ftw(PORT_CONFIG_DIR, PORT_CONFIG_DIR "/*", __nw_ports_enumerate, ports);
184 }
185
186 nw_port* nw_ports_get_by_name(nw_ports* ports, const char* name) {
187 struct nw_ports_entry* entry = NULL;
188
189 STAILQ_FOREACH(entry, &ports->entries, nodes) {
190 const char* __name = nw_port_name(entry->port);
191
192 // If the name matches, return a reference to the zone
193 if (strcmp(name, __name) == 0)
194 return nw_port_ref(entry->port);
195 }
196
197 // No match found
198 return NULL;
199 }
200
201 int nw_ports_bus_paths(nw_ports* ports, char*** paths) {
202 struct nw_ports_entry* entry = NULL;
203 char* path = NULL;
204
205 // Allocate an array for all paths
206 char** p = calloc(ports->num + 1, sizeof(*p));
207 if (!p)
208 return 1;
209
210 unsigned int i = 0;
211
212 // Walk through all ports
213 STAILQ_FOREACH(entry, &ports->entries, nodes) {
214 // Generate the bus path
215 path = nw_port_bus_path(entry->port);
216 if (!path)
217 goto ERROR;
218
219 // Append the bus path to the array
220 p[i++] = path;
221 }
222
223 // Return pointer
224 *paths = p;
225
226 return 0;
227
228 ERROR:
229 if (p) {
230 for (char** e = p; *e; e++)
231 free(*e);
232 free(p);
233 }
234
235 return 1;
236 }
237
238 int nw_ports_walk(nw_ports* ports, nw_ports_walk_callback callback, void* data) {
239 struct nw_ports_entry* entry = NULL;
240 int r;
241
242 STAILQ_FOREACH(entry, &ports->entries, nodes) {
243 r = callback(ports->daemon, entry->port, data);
244 if (r)
245 return r;
246 }
247
248 return 0;
249 }
250
251 static int __nw_ports_reconfigure(nw_daemon* daemon, nw_port* port, void* data) {
252 return nw_port_reconfigure(port);
253 }
254
255 int nw_ports_reconfigure(nw_ports* ports) {
256 return nw_ports_walk(ports, __nw_ports_reconfigure, NULL);
257 }