]>
git.ipfire.org Git - people/ms/network.git/blob - src/networkd/ports.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 #############################################################################*/
23 #include <sys/queue.h>
32 struct nw_ports_entry
{
35 // Link to the other entries
36 STAILQ_ENTRY(nw_ports_entry
) nodes
;
44 STAILQ_HEAD(entries
, nw_ports_entry
) entries
;
46 // A counter of the port entries
50 int nw_ports_create(nw_ports
** ports
, nw_daemon
* daemon
) {
51 nw_ports
* p
= calloc(1, sizeof(*p
));
55 // Store a reference to the daemon
56 p
->daemon
= nw_daemon_ref(daemon
);
58 // Initialize the reference counter
62 STAILQ_INIT(&p
->entries
);
64 // Reference the pointer
70 static void nw_ports_free(nw_ports
* ports
) {
71 struct nw_ports_entry
* entry
= NULL
;
73 while (!STAILQ_EMPTY(&ports
->entries
)) {
74 entry
= STAILQ_FIRST(&ports
->entries
);
76 // Dereference the port
77 nw_port_unref(entry
->port
);
79 // Remove the entry from the list
80 STAILQ_REMOVE_HEAD(&ports
->entries
, nodes
);
87 nw_ports
* nw_ports_ref(nw_ports
* ports
) {
93 nw_ports
* nw_ports_unref(nw_ports
* ports
) {
94 if (--ports
->nrefs
> 0)
101 static int nw_ports_add_port(nw_ports
* ports
, nw_port
* port
) {
102 // Allocate a new entry
103 struct nw_ports_entry
* entry
= calloc(1, sizeof(*entry
));
107 // Reference the port
108 entry
->port
= nw_port_ref(port
);
110 // Add it to the list
111 STAILQ_INSERT_TAIL(&ports
->entries
, entry
, nodes
);
113 // Increment the counter
119 static int __nw_ports_enumerate(const char* path
, const struct stat
* s
, void* data
) {
120 nw_port
* port
= NULL
;
123 nw_ports
* ports
= (nw_ports
*)data
;
125 // Skip anything that isn't a regular file
126 if (!S_ISREG(s
->st_mode
))
129 // Find the basename of the file
130 const char* name
= nw_path_basename(path
);
132 // Break on invalid paths
136 // Skip any hidden files
141 r
= nw_port_create(&port
, ports
->daemon
, name
);
145 // Add the port to the list
146 r
= nw_ports_add_port(ports
, port
);
157 int nw_ports_enumerate(nw_ports
* ports
) {
158 return nw_ftw(PORT_CONFIG_DIR
, PORT_CONFIG_DIR
"/*", __nw_ports_enumerate
, ports
);
161 nw_port
* nw_ports_get_by_name(nw_ports
* ports
, const char* name
) {
162 struct nw_ports_entry
* entry
= NULL
;
164 STAILQ_FOREACH(entry
, &ports
->entries
, nodes
) {
165 const char* __name
= nw_port_name(entry
->port
);
167 // If the name matches, return a reference to the zone
168 if (strcmp(name
, __name
) == 0)
169 return nw_port_ref(entry
->port
);
176 int nw_ports_bus_paths(nw_ports
* ports
, char*** paths
) {
177 struct nw_ports_entry
* entry
= NULL
;
180 // Allocate an array for all paths
181 char** p
= calloc(ports
->num
+ 1, sizeof(*p
));
187 // Walk through all ports
188 STAILQ_FOREACH(entry
, &ports
->entries
, nodes
) {
189 // Generate the bus path
190 path
= nw_port_bus_path(entry
->port
);
194 // Append the bus path to the array
205 for (char** e
= p
; *e
; e
++)