From: Michael Tremer Date: Sat, 11 Feb 2023 10:59:03 +0000 (+0000) Subject: networkd: Enumerate ports on startup X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c791afc87a22936e9564e449182ee6452659887e;p=network.git networkd: Enumerate ports on startup Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index d78d61ee..505d6791 100644 --- a/Makefile.am +++ b/Makefile.am @@ -329,6 +329,8 @@ dist_networkd_SOURCES = \ src/networkd/ports.c \ src/networkd/ports.h \ src/networkd/string.h \ + src/networkd/util.c \ + src/networkd/util.h \ src/networkd/zones.c \ src/networkd/zones.h \ src/networkd/zone.c \ diff --git a/src/networkd/port.h b/src/networkd/port.h index cea62037..250a694c 100644 --- a/src/networkd/port.h +++ b/src/networkd/port.h @@ -21,6 +21,8 @@ #ifndef NETWORKD_PORT_H #define NETWORKD_PORT_H +#define PORT_CONFIG_DIR CONFIG_DIR "/ports" + struct nw_port; int nw_port_create(struct nw_port** port, const char* name); diff --git a/src/networkd/ports.c b/src/networkd/ports.c index 500153a4..19a3a597 100644 --- a/src/networkd/ports.c +++ b/src/networkd/ports.c @@ -21,10 +21,13 @@ #include #include #include +#include #include "logging.h" #include "port.h" #include "ports.h" +#include "string.h" +#include "util.h" struct nw_ports_entry { struct nw_port* port; @@ -95,6 +98,62 @@ struct nw_ports* nw_ports_unref(struct nw_ports* ports) { return NULL; } +static int nw_ports_add_port(struct nw_ports* ports, struct nw_port* port) { + // Allocate a new entry + struct nw_ports_entry* entry = calloc(1, sizeof(*entry)); + if (!entry) + return 1; + + // Reference the port + entry->port = nw_port_ref(port); + + // Add it to the list + STAILQ_INSERT_TAIL(&ports->entries, entry, nodes); + + // Increment the counter + ports->num++; + + return 0; +} + +static int __nw_ports_enumerate(const char* path, const struct stat* s, void* data) { + struct nw_port* port = NULL; + int r; + + struct nw_ports* ports = (struct nw_ports*)data; + + // Skip anything that isn't a regular file + if (!S_ISREG(s->st_mode)) + return 0; + + // Find the basename of the file + const char* name = nw_path_basename(path); + + // Break on invalid paths + if (!name) + return 0; + + // Skip any hidden files + if (*name == '.') + return 0; + + // Create a new port + r = nw_port_create(&port, name); + if (r) + goto ERROR; + + // Add the port to the list + r = nw_ports_add_port(ports, port); + if (r) + goto ERROR; + +ERROR: + if (port) + nw_port_unref(port); + + return r; +} + int nw_ports_enumerate(struct nw_ports* ports) { - return 0; // XXX TODO + return nw_ftw(PORT_CONFIG_DIR, PORT_CONFIG_DIR "/*", __nw_ports_enumerate, ports); } diff --git a/src/networkd/string.h b/src/networkd/string.h index f3c5b717..6ff44f9e 100644 --- a/src/networkd/string.h +++ b/src/networkd/string.h @@ -142,4 +142,12 @@ static inline int __nw_path_join(char* s, const size_t length, return __nw_string_format(s, length, "%s/%s", first, second); } +static inline const char* nw_path_basename(const char* path) { + const char* basename = strrchr(path, '/'); + if (!basename) + return NULL; + + return basename + 1; +} + #endif /* NETWORKD_STRING_H */ diff --git a/src/networkd/util.c b/src/networkd/util.c new file mode 100644 index 00000000..0381ea86 --- /dev/null +++ b/src/networkd/util.c @@ -0,0 +1,61 @@ +/*############################################################################# +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2023 IPFire Network Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include +#include +#include +#include + +#include "util.h" + +int nw_ftw(const char* path, const char* filter, + int (*callback)(const char* path, const struct stat* s, void* data), void* data) { + /* + This is a nested function which allows us to pass some custom pointer to + the callback function as that isn't possible with nftw(). + */ + int __callback(const char* p, const struct stat* s, int type, struct FTW* ftw) { + int r; + + // Filter out anything we don't want + if (filter) { + r = fnmatch(filter, p, FNM_PATHNAME); + + switch (r) { + // Pattern didn't match + case FNM_NOMATCH: + return 0; + + // Pattern matched + case 0: + break; + + // Error + default: + return 1; + } + } + + return callback(p, s, data); + } + + return nftw(path, __callback, 0, 0); +} diff --git a/src/networkd/util.h b/src/networkd/util.h new file mode 100644 index 00000000..950afdac --- /dev/null +++ b/src/networkd/util.h @@ -0,0 +1,29 @@ +/*############################################################################# +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2023 IPFire Network Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef NETWORKD_UTIL_H +#define NETWORKD_UTIL_H + +#include + +int nw_ftw(const char* path, const char* filter, + int (*callback)(const char* path, const struct stat* s, void* data), void* data); + +#endif /* NETWORKD_UTIL_H */