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_ports_entry
{
36 // Link to the other entries
37 STAILQ_ENTRY(nw_ports_entry
) nodes
;
45 STAILQ_HEAD(entries
, nw_ports_entry
) entries
;
47 // A counter of the port entries
51 int nw_ports_create(nw_ports
** ports
, nw_daemon
* daemon
) {
52 nw_ports
* p
= calloc(1, sizeof(*p
));
56 // Store a reference to the daemon
57 p
->daemon
= nw_daemon_ref(daemon
);
59 // Initialize the reference counter
63 STAILQ_INIT(&p
->entries
);
65 // Reference the pointer
71 static void nw_ports_free(nw_ports
* ports
) {
72 struct nw_ports_entry
* entry
= NULL
;
74 while (!STAILQ_EMPTY(&ports
->entries
)) {
75 entry
= STAILQ_FIRST(&ports
->entries
);
77 // Dereference the port
78 nw_port_unref(entry
->port
);
80 // Remove the entry from the list
81 STAILQ_REMOVE_HEAD(&ports
->entries
, nodes
);
88 nw_ports
* nw_ports_ref(nw_ports
* ports
) {
94 nw_ports
* nw_ports_unref(nw_ports
* ports
) {
95 if (--ports
->nrefs
> 0)
102 int nw_ports_save(nw_ports
* ports
) {
103 struct nw_ports_entry
* entry
= NULL
;
106 STAILQ_FOREACH(entry
, &ports
->entries
, nodes
) {
107 r
= nw_port_save(entry
->port
);
115 static int nw_ports_add_port(nw_ports
* ports
, nw_port
* port
) {
116 // Allocate a new entry
117 struct nw_ports_entry
* entry
= calloc(1, sizeof(*entry
));
121 // Reference the port
122 entry
->port
= nw_port_ref(port
);
124 // Add it to the list
125 STAILQ_INSERT_TAIL(&ports
->entries
, entry
, nodes
);
127 // Increment the counter
133 static int nw_ports_enumerate_port(nw_ports
* ports
, const char* name
) {
134 nw_port
* port
= NULL
;
138 r
= nw_port_open(&port
, ports
->daemon
, name
);
142 // Add the port to the list
143 r
= nw_ports_add_port(ports
, port
);
154 int nw_ports_enumerate(nw_ports
* ports
) {
156 struct dirent
* entry
= NULL
;
159 // Open the ports directory
160 d
= nw_daemon_config_opendir(ports
->daemon
, "ports");
172 // Read the next entry
177 // Skip anything that is not a regular file
178 if (entry
->d_type
!= DT_REG
)
182 if (entry
->d_name
[0] == '.')
185 // Enumerate the port
186 r
= nw_ports_enumerate_port(ports
, entry
->d_name
);
198 nw_port
* nw_ports_get_by_name(nw_ports
* ports
, const char* name
) {
199 struct nw_ports_entry
* entry
= NULL
;
201 STAILQ_FOREACH(entry
, &ports
->entries
, nodes
) {
202 const char* __name
= nw_port_name(entry
->port
);
204 // If the name matches, return a reference to the zone
205 if (strcmp(name
, __name
) == 0)
206 return nw_port_ref(entry
->port
);
213 int nw_ports_bus_paths(nw_ports
* ports
, char*** paths
) {
214 struct nw_ports_entry
* entry
= NULL
;
217 // Allocate an array for all paths
218 char** p
= calloc(ports
->num
+ 1, sizeof(*p
));
224 // Walk through all ports
225 STAILQ_FOREACH(entry
, &ports
->entries
, nodes
) {
226 // Generate the bus path
227 path
= nw_port_bus_path(entry
->port
);
231 // Append the bus path to the array
242 for (char** e
= p
; *e
; e
++)
250 int nw_ports_walk(nw_ports
* ports
, nw_ports_walk_callback callback
, void* data
) {
251 struct nw_ports_entry
* entry
= NULL
;
254 STAILQ_FOREACH(entry
, &ports
->entries
, nodes
) {
255 r
= callback(ports
->daemon
, entry
->port
, data
);
263 static int __nw_ports_reconfigure(nw_daemon
* daemon
, nw_port
* port
, void* data
) {
264 return nw_port_reconfigure(port
);
267 int nw_ports_reconfigure(nw_ports
* ports
) {
268 return nw_ports_walk(ports
, __nw_ports_reconfigure
, NULL
);