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 \
#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);
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
+#include <sys/stat.h>
#include "logging.h"
#include "port.h"
#include "ports.h"
+#include "string.h"
+#include "util.h"
struct nw_ports_entry {
struct nw_port* port;
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);
}
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 */
--- /dev/null
+/*#############################################################################
+# #
+# 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 <http://www.gnu.org/licenses/>. #
+# #
+#############################################################################*/
+
+#include <errno.h>
+#include <fnmatch.h>
+#include <ftw.h>
+#include <stddef.h>
+#include <stdio.h>
+
+#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);
+}
--- /dev/null
+/*#############################################################################
+# #
+# 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 <http://www.gnu.org/licenses/>. #
+# #
+#############################################################################*/
+
+#ifndef NETWORKD_UTIL_H
+#define NETWORKD_UTIL_H
+
+#include <sys/stat.h>
+
+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 */