From e633147dcf3bc46e0686e106cb013dfccf30b1c6 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 9 Jun 2023 11:19:30 +0000 Subject: [PATCH 01/16] config: Add string buffer type Signed-off-by: Michael Tremer --- src/networkd/config.c | 45 +++++++++++++++++++++++++++--------- src/networkd/config.h | 49 +++++++++++++++++++++++++++------------- src/networkd/port-veth.c | 2 +- src/networkd/port-vlan.c | 3 ++- 4 files changed, 70 insertions(+), 29 deletions(-) diff --git a/src/networkd/config.c b/src/networkd/config.c index 3d444c41..c6281cb2 100644 --- a/src/networkd/config.c +++ b/src/networkd/config.c @@ -43,6 +43,7 @@ struct nw_config_option { const char* key; void* value; + size_t length; // Callbacks nw_config_option_read_callback_t read_callback; @@ -427,7 +428,8 @@ int nw_config_options_read(nw_config* config) { int r; STAILQ_FOREACH(option, &config->options, nodes) { - r = option->read_callback(config, option->key, option->value, option->data); + r = option->read_callback(config, + option->key, option->value, option->length, option->data); if (r < 0) return r; } @@ -440,7 +442,8 @@ int nw_config_options_write(nw_config* config) { int r; STAILQ_FOREACH(option, &config->options, nodes) { - r = option->write_callback(config, option->key, option->value, option->data); + r = option->write_callback(config, + option->key, option->value, option->length, option->data); if (r < 0) return r; } @@ -448,7 +451,8 @@ int nw_config_options_write(nw_config* config) { return 0; } -int nw_config_option_add(nw_config* config, const char* key, void* value, +int nw_config_option_add(nw_config* config, + const char* key, void* value, const size_t length, nw_config_option_read_callback_t read_callback, nw_config_option_write_callback_t write_callback, void* data) { // Check input @@ -465,6 +469,7 @@ int nw_config_option_add(nw_config* config, const char* key, void* value, // Set value option->value = value; + option->length = length; // Set callbacks option->read_callback = read_callback; @@ -477,7 +482,8 @@ int nw_config_option_add(nw_config* config, const char* key, void* value, return 0; } -int nw_config_read_int(nw_config* config, const char* key, void* value, void* data) { +int nw_config_read_int(nw_config* config, + const char* key, void* value, const size_t length, void* data) { // Fetch the value *(int*)value = nw_config_get_int(config, key, -1); @@ -485,13 +491,14 @@ int nw_config_read_int(nw_config* config, const char* key, void* value, void* da } int nw_config_write_int(nw_config* config, - const char* key, const void* value, void* data) { + const char* key, const void* value, const size_t length, void* data) { return 0; } // String -int nw_config_read_string(nw_config* config, const char* key, void* value, void* data) { +int nw_config_read_string(nw_config* config, + const char* key, void* value, const size_t length, void* data) { // Fetch the value const char* p = nw_config_get(config, key); if (p) @@ -501,13 +508,28 @@ int nw_config_read_string(nw_config* config, const char* key, void* value, void* } int nw_config_write_string(nw_config* config, - const char* key, const void* value, void* data) { + const char* key, const void* value, const size_t length, void* data) { return nw_config_set(config, key, *(const char**)value); } +// String Buffer + +int nw_config_read_string_buffer(nw_config* config, + const char* key, void* value, const size_t length, void* data) { + char* string = (char*)value; + + // Fetch the value + const char* p = nw_config_get(config, key); + if (p) + return __nw_string_set(string, length, p); + + return 0; +} + // String Table -int nw_config_read_string_table(nw_config* config, const char* key, void* value, void* data) { +int nw_config_read_string_table(nw_config* config, + const char* key, void* value, const size_t length, void* data) { const char* s = NULL; int* v = (int*)value; @@ -529,7 +551,7 @@ int nw_config_read_string_table(nw_config* config, const char* key, void* value, } int nw_config_write_string_table(nw_config* config, - const char* key, const void* value, void* data) { + const char* key, const void* value, const size_t length, void* data) { int* v = (int*)value; const nw_string_table_t* table = (nw_string_table_t*)data; @@ -544,7 +566,8 @@ int nw_config_write_string_table(nw_config* config, // Address -int nw_config_read_address(nw_config* config, const char* key, void* value, void* data) { +int nw_config_read_address(nw_config* config, + const char* key, void* value, const size_t length, void* data) { nw_address_t* address = (nw_address_t*)value; int r; @@ -561,7 +584,7 @@ int nw_config_read_address(nw_config* config, const char* key, void* value, void } int nw_config_write_address(nw_config* config, - const char* key, const void* value, void* data) { + const char* key, const void* value, const size_t length, void* data) { const nw_address_t* address = (nw_address_t*)value; int r; diff --git a/src/networkd/config.h b/src/networkd/config.h index b25d05e6..4b8bc01f 100644 --- a/src/networkd/config.h +++ b/src/networkd/config.h @@ -60,47 +60,64 @@ int nw_config_options_read(nw_config* config); int nw_config_options_write(nw_config* config); typedef int (*nw_config_option_read_callback_t) - (nw_config* config, const char* key, void* value, void* data); + (nw_config* config, const char* key, void* value, const size_t length, void* data); typedef int (*nw_config_option_write_callback_t) - (nw_config* config, const char* key, const void* value, void* data); + (nw_config* config, const char* key, const void* value, const size_t length, void* data); -int nw_config_option_add(nw_config* config, const char* key, void* value, +int nw_config_option_add(nw_config* config, const char* key, void* value, const size_t length, nw_config_option_read_callback_t read_callback, nw_config_option_write_callback_t write_callback, void* data); -#define NW_CONFIG_OPTION(config, key, value, read_callback, write_callback, data) \ - nw_config_option_add(config, key, value, read_callback, write_callback, data) +#define NW_CONFIG_OPTION(config, key, value, length, read_callback, write_callback, data) \ + nw_config_option_add(config, key, value, length, read_callback, write_callback, data) // String #define NW_CONFIG_OPTION_STRING(config, key, value) \ nw_config_option_add(config, key, value, nw_config_read_string, nw_config_write_string, NULL) -int nw_config_read_string(nw_config* config, const char* key, void* value, void* data); -int nw_config_write_string(nw_config* config, const char* key, const void* value, void* data); +int nw_config_read_string(nw_config* config, + const char* key, void* value, const size_t length, void* data); +int nw_config_write_string(nw_config* config, + const char* key, const void* value, const size_t length, void* data); + +#define NW_CONFIG_OPTION_STRING_BUFFER(config, key, value) \ + nw_config_option_add(config, key, value, sizeof(value), \ + nw_config_read_string_buffer, nw_config_write_string_buffer, NULL) + +int nw_config_read_string_buffer(nw_config* config, + const char* key, void* value, const size_t length, void* data); +#define nw_config_write_string_buffer nw_config_write_string // String Table #define NW_CONFIG_OPTION_STRING_TABLE(config, key, value, table) \ - nw_config_option_add(config, key, value, nw_config_read_string_table, nw_config_write_string_table, (void*)table) + nw_config_option_add(config, key, value, 0, \ + nw_config_read_string_table, nw_config_write_string_table, (void*)table) -int nw_config_read_string_table(nw_config* config, const char* key, void* value, void* data); -int nw_config_write_string_table(nw_config* config, const char* key, const void* value, void* data); +int nw_config_read_string_table(nw_config* config, + const char* key, void* value, const size_t length, void* data); +int nw_config_write_string_table(nw_config* config, + const char* key, const void* value, const size_t length, void* data); // Integer #define NW_CONFIG_OPTION_INT(config, key, value) \ - nw_config_option_add(config, key, value, nw_config_read_int, nw_config_write_int, NULL) + nw_config_option_add(config, key, value, 0, nw_config_read_int, nw_config_write_int, NULL) -int nw_config_read_int(nw_config* config, const char* key, void* value, void* data); -int nw_config_write_int(nw_config* config, const char* key, const void* value, void* data); +int nw_config_read_int(nw_config* config, + const char* key, void* value, const size_t length, void* data); +int nw_config_write_int(nw_config* config, + const char* key, const void* value, const size_t length, void* data); // Address #define NW_CONFIG_OPTION_ADDRESS(config, key, value) \ - nw_config_option_add(config, key, value, nw_config_read_address, nw_config_write_address, NULL) + nw_config_option_add(config, key, value, 0, nw_config_read_address, nw_config_write_address, NULL) -int nw_config_read_address(nw_config* config, const char* key, void* value, void* data); -int nw_config_write_address(nw_config* config, const char* key, const void* value, void* data); +int nw_config_read_address(nw_config* config, + const char* key, void* value, const size_t length, void* data); +int nw_config_write_address(nw_config* config, + const char* key, const void* value, const size_t length, void* data); #endif /* NETWORKD_CONFIG_H */ diff --git a/src/networkd/port-veth.c b/src/networkd/port-veth.c index 44b1a0d4..029ef50c 100644 --- a/src/networkd/port-veth.c +++ b/src/networkd/port-veth.c @@ -28,7 +28,7 @@ static int nw_port_veth_setup(nw_port* port) { int r; // Peer - r = NW_CONFIG_OPTION_STRING(port->config, "VETH_PEER", &port->veth.peer); + r = NW_CONFIG_OPTION_STRING_BUFFER(port->config, "VETH_PEER", port->veth.peer); if (r < 0) return 1; diff --git a/src/networkd/port-vlan.c b/src/networkd/port-vlan.c index 25a59eed..c759f719 100644 --- a/src/networkd/port-vlan.c +++ b/src/networkd/port-vlan.c @@ -53,7 +53,8 @@ static int nw_port_vlan_setup(nw_port* port) { return r; // Parent Port - r = NW_CONFIG_OPTION_STRING(port->config, "VLAN_PARENT", &port->vlan.__parent_name); + r = NW_CONFIG_OPTION_STRING_BUFFER(port->config, + "VLAN_PARENT", port->vlan.__parent_name); if (r < 0) return r; -- 2.39.2 From aa7cc0927e2ea5cde54685e01290be8c0afdc31c Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 9 Jun 2023 12:30:33 +0000 Subject: [PATCH 02/16] networkctl: Add color functions Signed-off-by: Michael Tremer --- Makefile.am | 2 + src/networkctl/terminal.c | 51 ++++++++++++++++++++++++ src/networkctl/terminal.h | 83 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 src/networkctl/terminal.c create mode 100644 src/networkctl/terminal.h diff --git a/Makefile.am b/Makefile.am index caab99e1..95fff11a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -402,6 +402,8 @@ dist_networkctl_SOURCES = \ src/networkctl/main.c \ src/networkctl/port.c \ src/networkctl/port.h \ + src/networkctl/terminal.c \ + src/networkctl/terminal.h \ src/networkctl/zone.c \ src/networkctl/zone.h diff --git a/src/networkctl/terminal.c b/src/networkctl/terminal.c new file mode 100644 index 00000000..de7cd8dd --- /dev/null +++ b/src/networkctl/terminal.c @@ -0,0 +1,51 @@ +/*############################################################################# +# # +# 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 "terminal.h" + +// Cache the color mode +static color_mode_t __color_mode = COLORS_UNKNOWN; + +static color_mode_t detect_color_mode(void) { + const char* s = NULL; + + // Check for NO_COLOR and if found turn off colours + s = secure_getenv("NO_COLOR"); + if (s) + return COLORS_OFF; + + // Disable colours if this isn't an interactive terminal + if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO) || !isatty(STDERR_FILENO)) + return COLORS_OFF; + + // Otherwise we enable colours + return COLORS_ON; +} + +color_mode_t color_mode() { + if (__color_mode == COLORS_UNKNOWN) { + __color_mode = detect_color_mode(); + } + + return __color_mode; +} diff --git a/src/networkctl/terminal.h b/src/networkctl/terminal.h new file mode 100644 index 00000000..b7bffec7 --- /dev/null +++ b/src/networkctl/terminal.h @@ -0,0 +1,83 @@ +/*############################################################################# +# # +# 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 NETWORKCTL_TERMINAL_H +#define NETWORKCTL_TERMINAL_H + +typedef enum color_mode { + COLORS_UNKNOWN = 0, + COLORS_OFF, + COLORS_ON, +} color_mode_t; + +// Reset +#define COLOR_RESET "\x1B[0m" + +// Highlight +#define COLOR_HIGHLIGHT "\x1B[0;1;39m" + +// Regular Colors +#define COLOR_BLACK "\x1B[0;30m" +#define COLOR_RED "\x1B[0;31m" +#define COLOR_GREEN "\x1B[0;32m" +#define COLOR_YELLOW "\x1B[0;33m" +#define COLOR_BLUE "\x1B[0;34m" +#define COLOR_MAGENTA "\x1B[0;35m" +#define COLOR_CYAN "\x1B[0;36m" +#define COLOR_WHITE "\x1B[0;37m" + +#define COLOR_BRIGHT_BLACK "\x1B[0;90m" +#define COLOR_BRIGHT_RED "\x1B[0;91m" +#define COLOR_BRIGHT_GREEN "\x1B[0;92m" +#define COLOR_BRIGHT_YELLOW "\x1B[0;93m" +#define COLOR_BRIGHT_BLUE "\x1B[0;94m" +#define COLOR_BRIGHT_MAGENTA "\x1B[0;95m" +#define COLOR_BRIGHT_CYAN "\x1B[0;96m" +#define COLOR_BRIGHT_WHITE "\x1B[0;97m" + +#define COLOR_HIGHLIGHT_BLACK "\x1B[0;1;30m" +#define COLOR_HIGHLIGHT_RED "\x1B[0;1;31m" +#define COLOR_HIGHLIGHT_GREEN "\x1B[0;1;32m" +#define COLOR_HIGHLIGHT_YELLOW "\x1B[0;1;33m" +#define COLOR_HIGHLIGHT_BLUE "\x1B[0;1;34m" +#define COLOR_HIGHLIGHT_MAGENTA "\x1B[0;1;35m" +#define COLOR_HIGHLIGHT_CYAN "\x1B[0;1;36m" +#define COLOR_HIGHLIGHT_WHITE "\x1B[0;1;37m" + +// Returns the color mode +color_mode_t color_mode(void); + +#define COLOR_FUNC(name, color) \ + static inline const char* color_##name(void) { \ + return (color_mode() == COLORS_ON) ? COLOR_ ## color : ""; \ + } + +COLOR_FUNC(reset, RESET) +COLOR_FUNC(highlight, HIGHLIGHT) +COLOR_FUNC(black, BLACK) +COLOR_FUNC(red, RED) +COLOR_FUNC(green, GREEN) +COLOR_FUNC(yellow, YELLOW) +COLOR_FUNC(blue, BLUE) +COLOR_FUNC(magenta, MAGENTA) +COLOR_FUNC(cyan, CYAN) +COLOR_FUNC(white, WHITE) + +#endif /* NETWORKCTL_TERMINAL_H */ -- 2.39.2 From 8dc40ed861b5ee93f176caf792fc818f681fd9b4 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 9 Jun 2023 12:40:55 +0000 Subject: [PATCH 03/16] networkctl: Move describe into an own function Signed-off-by: Michael Tremer --- src/networkctl/port.c | 48 ++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/networkctl/port.c b/src/networkctl/port.c index 67bc0037..b6a95c55 100644 --- a/src/networkctl/port.c +++ b/src/networkctl/port.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include @@ -80,22 +81,19 @@ ERROR: return r; } -// Dump - -static int networkctl_port_dump(sd_bus* bus, int argc, char* argv[]) { +static int networkctl_port_describe(sd_bus* bus, const char* name, char** text) { sd_bus_message* reply = NULL; sd_bus_error error = SD_BUS_ERROR_NULL; char path[PATH_MAX]; - const char* text = NULL; + const char* t = NULL; int r; - if (argc < 1) { - fprintf(stderr, "Port required\n"); + // Check input + if (!name || !text) return -EINVAL; - } // Make port path - r = nw_string_format(path, "/org/ipfire/network1/port/%s", argv[0]); + r = nw_string_format(path, "/org/ipfire/network1/port/%s", name); if (r < 0) goto ERROR; @@ -108,15 +106,16 @@ static int networkctl_port_dump(sd_bus* bus, int argc, char* argv[]) { } // Read the text - r = sd_bus_message_read(reply, "s", &text); + r = sd_bus_message_read(reply, "s", &t); if (r < 0) { fprintf(stderr, "Could not parse bus message: %s\n", strerror(-r)); goto ERROR; } - // Print the text - if (text) - printf("%s\n", text); + // Copy text to heap + *text = strdup(t); + if (!*text) + r = -errno; ERROR: if (reply) @@ -125,6 +124,31 @@ ERROR: return r; } +// Dump + +static int networkctl_port_dump(sd_bus* bus, int argc, char* argv[]) { + char* text = NULL; + int r; + + if (argc < 1) { + fprintf(stderr, "Port required\n"); + return -EINVAL; + } + + // Describe the port + r = networkctl_port_describe(bus, argv[0], &text); + if (r < 0) + return r; + + // Print the text + printf("%s\n", text); + + if (text) + free(text); + + return 0; +} + // List static int __networkctl_port_list(sd_bus* bus, const char* path, const char* name, void* data) { -- 2.39.2 From 3363ba36e33d5fc9c455825aeeabf4b615cde3c1 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 9 Jun 2023 14:14:42 +0000 Subject: [PATCH 04/16] networkctl: Implement scaffolding to show ports Signed-off-by: Michael Tremer --- Makefile.am | 2 ++ src/networkctl/port.c | 68 +++++++++++++++++++++++++++++++++++++++++++ src/networkd/json.h | 9 ++++++ 3 files changed, 79 insertions(+) diff --git a/Makefile.am b/Makefile.am index 95fff11a..2632ca73 100644 --- a/Makefile.am +++ b/Makefile.am @@ -409,12 +409,14 @@ dist_networkctl_SOURCES = \ networkctl_CFLAGS = \ $(AM_CFLAGS) \ + $(JSON_C_CFLAGS) \ $(SYSTEMD_CFLAGS) networkctl_LDFLAGS = \ $(AM_LDFLAGS) networkctl_LDADD = \ + $(JSON_C_LIBS) \ $(SYSTEMD_LIBS) # ------------------------------------------------------------------------------ diff --git a/src/networkctl/port.c b/src/networkctl/port.c index b6a95c55..439177d4 100644 --- a/src/networkctl/port.c +++ b/src/networkctl/port.c @@ -23,9 +23,11 @@ #include +#include "../networkd/json.h" #include "../networkd/string.h" #include "command.h" #include "port.h" +#include "terminal.h" typedef int (*networkctl_port_walk_callback) (sd_bus* bus, const char* path, const char* name, void* data); @@ -161,10 +163,76 @@ static int networkctl_port_list(sd_bus* bus, int argc, char* argv[]) { return networkctl_port_walk(bus, __networkctl_port_list, NULL); } +// Show + +#define SHOW_LINE " %-12s : %s\n" + +static int __networkctl_port_show(sd_bus* bus, const char* path, const char* name, void* data) { + struct json_object* object = NULL; + enum json_tokener_error json_error; + char* describe = NULL; + int r; + + // Describe this port + r = networkctl_port_describe(bus, name, &describe); + if (r < 0) + goto ERROR; + + // Parse JSON + object = json_tokener_parse_verbose(describe, &json_error); + if (!object) { + fprintf(stderr, "Could not parse port %s: %s\n", + name, json_tokener_error_desc(json_error)); + return -EINVAL; + } + + // Show headline + printf("Port %s%s%s\n", color_highlight(), name, color_reset()); + + // Show type + const char* type = json_object_fetch_string(object, "Type"); + if (type) + printf(SHOW_LINE, "Type", type); + + // Show address + const char* address = json_object_fetch_string(object, "Address"); + if (address) + printf(SHOW_LINE, "Address", address); + + // Show an empty line at the end + printf("\n"); + + // Success! + r = 0; + +ERROR: + if (describe) + free(describe); + if (object) + json_object_unref(object); + + return r; +} + +static int networkctl_port_show(sd_bus* bus, int argc, char* argv[]) { + switch (argc) { + case 0: + return networkctl_port_walk(bus, __networkctl_port_show, NULL); + + case 1: + return __networkctl_port_show(bus, NULL, argv[0], NULL); + + default: + fprintf(stderr, "Too many arguments\n"); + return 1; + } +} + int networkctl_port(sd_bus* bus, int argc, char* argv[]) { static const struct command commands[] = { { "dump", 0, networkctl_port_dump }, { "list", 0, networkctl_port_list }, + { "show", 0, networkctl_port_show }, { NULL }, }; diff --git a/src/networkd/json.h b/src/networkd/json.h index 19ced9ad..6c2f66f1 100644 --- a/src/networkd/json.h +++ b/src/networkd/json.h @@ -87,4 +87,13 @@ static inline int json_to_string(struct json_object* o, char** s, size_t* l) { return 0; } +static inline const char* json_object_fetch_string( + struct json_object* o, const char* key) { + struct json_object* e = json_object_object_get(o, key); + if (!e) + return NULL; + + return json_object_get_string(e); +} + #endif /* NETWORKD_JSON_H */ -- 2.39.2 From 69467266f8b3d8a925604b3ffdeda060c5d409cd Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 9 Jun 2023 14:19:04 +0000 Subject: [PATCH 05/16] ports: Add link stuff to JSON output Signed-off-by: Michael Tremer --- src/networkd/link.c | 14 ++++++++++++++ src/networkd/link.h | 4 ++++ src/networkd/port.c | 7 +++++++ 3 files changed, 25 insertions(+) diff --git a/src/networkd/link.c b/src/networkd/link.c index 603aabe8..60abd466 100644 --- a/src/networkd/link.c +++ b/src/networkd/link.c @@ -27,6 +27,7 @@ #include #include "daemon.h" +#include "json.h" #include "link.h" #include "links.h" #include "logging.h" @@ -529,3 +530,16 @@ ERROR: return r; } + +// JSON + +int nw_link_to_json(nw_link* link, struct json_object* o) { + int r; + + // Add ifindex + r = json_object_add_int64(o, "LinkIndex", link->ifindex); + if (r < 0) + return r; + + return 0; +} diff --git a/src/networkd/link.h b/src/networkd/link.h index 2bab47c5..36c7d9dd 100644 --- a/src/networkd/link.h +++ b/src/networkd/link.h @@ -26,6 +26,7 @@ typedef struct nw_link nw_link; #include "daemon.h" +#include "json.h" int nw_link_create(nw_link** link, nw_daemon* daemon, int ifindex); @@ -45,4 +46,7 @@ int nw_link_process(sd_netlink* rtnl, sd_netlink_message* message, void* data); int nw_link_destroy(nw_link* link); +// JSON +int nw_link_to_json(nw_link* link, struct json_object* o); + #endif /* NETWORKD_LINK_H */ diff --git a/src/networkd/port.c b/src/networkd/port.c index fb625207..84fceee8 100644 --- a/src/networkd/port.c +++ b/src/networkd/port.c @@ -682,6 +682,13 @@ int nw_port_to_json(nw_port* port, struct json_object** object) { goto ERROR; } + // Add link stuff + if (port->link) { + r = nw_link_to_json(port->link, o); + if (r < 0) + goto ERROR; + } + // Call custom stuff if (NW_PORT_TYPE(port)->to_json) { r = NW_PORT_TYPE(port)->to_json(port, o); -- 2.39.2 From cf75e1dbc997b8a785e5f4d75e6a3d0efd594cf5 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 9 Jun 2023 14:44:06 +0000 Subject: [PATCH 06/16] link: Add device stuff to JSON output Signed-off-by: Michael Tremer --- src/networkd/link.c | 113 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 8 deletions(-) diff --git a/src/networkd/link.c b/src/networkd/link.c index 60abd466..23918cba 100644 --- a/src/networkd/link.c +++ b/src/networkd/link.c @@ -24,6 +24,7 @@ #include #include +#include #include #include "daemon.h" @@ -48,6 +49,9 @@ struct nw_link { NW_LINK_DESTROYED, } state; + // Device + struct sd_device* device; + // Stats struct rtnl_link_stats64 stats64; @@ -61,11 +65,35 @@ struct nw_link { uint8_t operstate; }; +static int nw_link_setup_device(nw_link* link) { + int r; + + // Fetch sd-device + r = sd_device_new_from_ifindex(&link->device, link->ifindex); + if (r < 0) { + ERROR("Could not fetch sd-device for link %d: %s\n", link->ifindex, strerror(-r)); + return r; + } + + return 0; +} + +static void nw_link_free(nw_link* link) { + DEBUG("Freeing link (ifindex = %d)\n", link->ifindex); + + if (link->device) + sd_device_unref(link->device); + if (link->daemon) + nw_daemon_unref(link->daemon); +} + int nw_link_create(nw_link** link, nw_daemon* daemon, int ifindex) { + int r; + // Allocate a new object nw_link* l = calloc(1, sizeof(*l)); if (!l) - return 1; + return -errno; // Store a reference to the daemon l->daemon = nw_daemon_ref(daemon); @@ -78,16 +106,16 @@ int nw_link_create(nw_link** link, nw_daemon* daemon, int ifindex) { DEBUG("New link allocated (ifindex = %d)\n", l->ifindex); - *link = l; + r = nw_link_setup_device(l); + if (r < 0) + goto ERROR; + *link = l; return 0; -} - -static void nw_link_free(nw_link* link) { - DEBUG("Freeing link (ifindex = %d)\n", link->ifindex); - if (link->daemon) - nw_daemon_unref(link->daemon); +ERROR: + nw_link_free(l); + return r; } nw_link* nw_link_ref(nw_link* link) { @@ -126,6 +154,19 @@ const char* nw_link_ifname(nw_link* link) { return link->ifname; } +static int nw_link_get_sd_device(nw_link* link, struct sd_device** device) { + int r; + + // Fetch sd-device + r = sd_device_new_from_ifindex(device, link->ifindex); + if (r < 0) { + ERROR("Could not fetch sd-device for link %d: %s\n", link->ifindex, strerror(-r)); + return r; + } + + return 0; +} + // Stats const struct rtnl_link_stats64* nw_link_get_stats64(nw_link* link) { @@ -533,6 +574,57 @@ ERROR: // JSON +static int nw_link_device_to_json(nw_link* link, struct json_object* o) { + const char* driver = NULL; + const char* vendor = NULL; + const char* model = NULL; + int r; + + // Fetch driver + r = sd_device_get_driver(link->device, &driver); + if (r < 0 && r != -ENOENT) + return r; + + // Add driver + if (driver) { + r = json_object_add_string(o, "Driver", driver); + if (r < 0) + return r; + } + + // Fetch vendor + r = sd_device_get_property_value(link->device, "ID_VENDOR_FROM_DATABASE", &vendor); + if (r < 0) { + r = sd_device_get_property_value(link->device, "ID_VENDOR", &vendor); + if (r < 0 && r != -ENOENT) + return r; + } + + // Add vendor + if (vendor) { + r = json_object_add_string(o, "Vendor", vendor); + if (r < 0) + return r; + } + + // Fetch model + r = sd_device_get_property_value(link->device, "ID_MODEL_FROM_DATABASE", &model); + if (r < 0) { + r = sd_device_get_property_value(link->device, "ID_MODEL", &model); + if (r < 0 && r != -ENOENT) + return r; + } + + // Add model + if (model) { + r = json_object_add_string(o, "Model", model); + if (r < 0) + return r; + } + + return 0; +} + int nw_link_to_json(nw_link* link, struct json_object* o) { int r; @@ -541,5 +633,10 @@ int nw_link_to_json(nw_link* link, struct json_object* o) { if (r < 0) return r; + // Add sd-device stuff + r = nw_link_device_to_json(link, o); + if (r < 0) + return r; + return 0; } -- 2.39.2 From f4f98a384fd5cb95b04be989ec488b2ecfc1960f Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 9 Jun 2023 14:59:54 +0000 Subject: [PATCH 07/16] ports: Add scaffolding for physical Ethernet interfaces Signed-off-by: Michael Tremer --- Makefile.am | 2 ++ src/networkd/link.c | 13 -------- src/networkd/port-ethernet.c | 63 ++++++++++++++++++++++++++++++++++++ src/networkd/port-ethernet.h | 34 +++++++++++++++++++ src/networkd/port.c | 16 ++++++--- src/networkd/port.h | 5 +++ 6 files changed, 116 insertions(+), 17 deletions(-) create mode 100644 src/networkd/port-ethernet.c create mode 100644 src/networkd/port-ethernet.h diff --git a/Makefile.am b/Makefile.am index 2632ca73..8c0d0c0f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -338,6 +338,8 @@ dist_networkd_SOURCES = \ src/networkd/port-bus.h \ src/networkd/port-dummy.c \ src/networkd/port-dummy.h \ + src/networkd/port-ethernet.c \ + src/networkd/port-ethernet.h \ src/networkd/port-veth.c \ src/networkd/port-veth.h \ src/networkd/port-vlan.c \ diff --git a/src/networkd/link.c b/src/networkd/link.c index 23918cba..f172208e 100644 --- a/src/networkd/link.c +++ b/src/networkd/link.c @@ -154,19 +154,6 @@ const char* nw_link_ifname(nw_link* link) { return link->ifname; } -static int nw_link_get_sd_device(nw_link* link, struct sd_device** device) { - int r; - - // Fetch sd-device - r = sd_device_new_from_ifindex(device, link->ifindex); - if (r < 0) { - ERROR("Could not fetch sd-device for link %d: %s\n", link->ifindex, strerror(-r)); - return r; - } - - return 0; -} - // Stats const struct rtnl_link_stats64* nw_link_get_stats64(nw_link* link) { diff --git a/src/networkd/port-ethernet.c b/src/networkd/port-ethernet.c new file mode 100644 index 00000000..a48b45e4 --- /dev/null +++ b/src/networkd/port-ethernet.c @@ -0,0 +1,63 @@ +/*############################################################################# +# # +# 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 "config.h" +#include "json.h" +#include "port.h" +#include "port-ethernet.h" + +static int nw_port_ethernet_setup(nw_port* port) { + int r; + + // Permanent Address + r = NW_CONFIG_OPTION_ADDRESS(port->config, + "PERMANENT_ADDRESS", &port->ethernet.permanent_address); + if (r < 0) + return r; + + return 0; +} + +static int nw_port_ethernet_to_json(nw_port* port, struct json_object* o) { + char* address = NULL; + int r = 0; + + // Permanent Address + address = nw_address_to_string(&port->ethernet.permanent_address); + if (address) { + r = json_object_add_string(o, "PermanentAddress", address); + if (r < 0) + goto ERROR; + } + +ERROR: + if (address) + free(address); + + return r; +} + +const nw_port_type_t nw_port_type_ethernet = { + // Configuration + .setup = nw_port_ethernet_setup, + + // JSON + .to_json = nw_port_ethernet_to_json, +}; diff --git a/src/networkd/port-ethernet.h b/src/networkd/port-ethernet.h new file mode 100644 index 00000000..50dade2e --- /dev/null +++ b/src/networkd/port-ethernet.h @@ -0,0 +1,34 @@ +/*############################################################################# +# # +# 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_PORT_ETHERNET_H +#define NETWORKD_PORT_ETHERNET_H + +#include "address.h" +#include "port.h" + +struct nw_port_ethernet { + // Permanent Address + nw_address_t permanent_address; +}; + +extern const nw_port_type_t nw_port_type_ethernet; + +#endif /* NETWORKD_PORT_ETHERNET_H */ diff --git a/src/networkd/port.c b/src/networkd/port.c index 84fceee8..f5479569 100644 --- a/src/networkd/port.c +++ b/src/networkd/port.c @@ -33,16 +33,18 @@ #include "port.h" #include "port-bonding.h" #include "port-dummy.h" +#include "port-ethernet.h" #include "port-veth.h" #include "port-vlan.h" #include "stats-collector.h" #include "string.h" static const nw_string_table_t nw_port_type_id[] = { - { NW_PORT_BONDING, "bonding" }, - { NW_PORT_DUMMY, "dummy" }, - { NW_PORT_VETH, "veth", }, - { NW_PORT_VLAN, "vlan" }, + { NW_PORT_BONDING, "bonding" }, + { NW_PORT_DUMMY, "dummy" }, + { NW_PORT_ETHERNET, "ethernet" }, + { NW_PORT_VETH, "veth", }, + { NW_PORT_VLAN, "vlan" }, { -1, NULL }, }; @@ -167,8 +169,13 @@ int nw_port_create(nw_port** port, nw_daemon* daemon, p->type = &nw_port_type_dummy; break; + case NW_PORT_ETHERNET: + p->type = &nw_port_type_ethernet; + break; + case NW_PORT_VETH: p->type = &nw_port_type_veth; + break; case NW_PORT_VLAN: p->type = &nw_port_type_vlan; @@ -329,6 +336,7 @@ int __nw_port_drop_port(nw_daemon* daemon, nw_port* port, void* data) { case NW_PORT_BONDING: case NW_PORT_DUMMY: + case NW_PORT_ETHERNET: case NW_PORT_VETH: break; } diff --git a/src/networkd/port.h b/src/networkd/port.h index 5c0a2a12..f1cb3d28 100644 --- a/src/networkd/port.h +++ b/src/networkd/port.h @@ -36,6 +36,7 @@ typedef struct nw_port nw_port; typedef enum nw_port_type_id { NW_PORT_BONDING, NW_PORT_DUMMY, + NW_PORT_ETHERNET, NW_PORT_VETH, NW_PORT_VLAN, } nw_port_type_id_t; @@ -67,6 +68,7 @@ typedef struct nw_port_type { #include "daemon.h" #include "json.h" #include "port-bonding.h" +#include "port-ethernet.h" #include "port-veth.h" #include "port-vlan.h" @@ -91,6 +93,9 @@ struct nw_port { // Bonding Settings struct nw_port_bonding bonding; + // Ethernet Settings + struct nw_port_ethernet ethernet; + // VETH Settings struct nw_port_veth veth; -- 2.39.2 From 008488e0f95c140f7df74cadd238db16321a7737 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 10 Jun 2023 11:22:46 +0000 Subject: [PATCH 08/16] logging: Add WARNING log level Signed-off-by: Michael Tremer --- src/networkd/logging.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/networkd/logging.h b/src/networkd/logging.h index ea75ba02..e835064d 100644 --- a/src/networkd/logging.h +++ b/src/networkd/logging.h @@ -30,6 +30,7 @@ void nw_log(int priority, const char *file, int line, const char* fn, This is just something simple which will work for now... */ #define INFO(args...) nw_log(LOG_INFO, __FILE__, __LINE__, __FUNCTION__, ## args) +#define WARNING(args...) nw_log(LOG_WARNING, __FILE__, __LINE__, __FUNCTION__, ## args) #define ERROR(args...) nw_log(LOG_ERR, __FILE__, __LINE__, __FUNCTION__, ## args) #define DEBUG(args...) nw_log(LOG_DEBUG, __FILE__, __LINE__, __FUNCTION__, ## args) -- 2.39.2 From 40e2c8ca02fae3d175e8d49498a3b3b443b1c6ef Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 10 Jun 2023 11:29:22 +0000 Subject: [PATCH 09/16] networkd: Handle any uevents for links Signed-off-by: Michael Tremer --- src/networkd/devmon.c | 67 +++++++++++++++++++++++++++++++++++++++++++ src/networkd/link.c | 19 ++++++++++++ src/networkd/link.h | 5 ++++ 3 files changed, 91 insertions(+) diff --git a/src/networkd/devmon.c b/src/networkd/devmon.c index 0a8c26d4..c751985c 100644 --- a/src/networkd/devmon.c +++ b/src/networkd/devmon.c @@ -20,8 +20,75 @@ #include +#include "daemon.h" #include "devmon.h" +#include "logging.h" + +static int nw_daemon_handle_uevent_net(nw_daemon* daemon, + sd_device* device, sd_device_action_t action) { + nw_link* link = NULL; + int ifindex; + int r; + + // Fetch ifindex + r = sd_device_get_ifindex(device, &ifindex); + if (r < 0) { + ERROR("Could not get ifindex from uevent: %s\n", strerror(-r)); + goto ERROR; + } + + // Fetch the link + link = nw_daemon_get_link_by_ifindex(daemon, ifindex); + if (!link) { + DEBUG("Could not fetch link %d, ignoring\n", ifindex); + r = 0; + goto ERROR; + } + + // Let the link handle its uevent + r = nw_link_handle_uevent(link, device, action); + +ERROR: + if (link) + nw_link_unref(link); + + return r; +} int nw_devmon_handle_uevent(sd_device_monitor* monitor, sd_device* device, void* data) { + sd_device_action_t action; + const char* subsystem = NULL; + int r; + + // Fetch daemon + nw_daemon* daemon = (nw_daemon*)data; + + // Fetch action + r = sd_device_get_action(device, &action); + if (r < 0) { + WARNING("Could not get uevent action, ignoring: %s\n", strerror(-r)); + return r; + } + + // Fetch subsystem + r = sd_device_get_subsystem(device, &subsystem); + if (r < 0) { + ERROR("Could not get uevent subsystem, ignoring: %s\n", strerror(-r)); + return r; + } + + // Handle any links + if (strcmp(subsystem, "net") == 0) { + r = nw_daemon_handle_uevent_net(daemon, device, action); + + } else { + DEBUG("Received an uevent for an unhandled subsystem '%s', ignoring\n", subsystem); + return 0; + } + + // Log if something went wrong + if (r < 0) + ERROR("Failed processing uevent: %s\n", strerror(-r)); + return 0; } diff --git a/src/networkd/link.c b/src/networkd/link.c index f172208e..b0d9e865 100644 --- a/src/networkd/link.c +++ b/src/networkd/link.c @@ -559,6 +559,25 @@ ERROR: return r; } +// uevent + +int nw_link_handle_uevent(nw_link* link, sd_device* device, sd_device_action_t action) { + // We need to remove or replace the stored device as it is now outdated + if (link->device) { + sd_device_unref(link->device); + + // If the device has been removed, we remove its reference + if (action == SD_DEVICE_REMOVE) + link->device = NULL; + + // Otherwise we just store the new one + else + link->device = sd_device_ref(device); + } + + return 0; +} + // JSON static int nw_link_device_to_json(nw_link* link, struct json_object* o) { diff --git a/src/networkd/link.h b/src/networkd/link.h index 36c7d9dd..c2f7b7e6 100644 --- a/src/networkd/link.h +++ b/src/networkd/link.h @@ -23,6 +23,8 @@ #include +#include + typedef struct nw_link nw_link; #include "daemon.h" @@ -46,6 +48,9 @@ int nw_link_process(sd_netlink* rtnl, sd_netlink_message* message, void* data); int nw_link_destroy(nw_link* link); +// uevent +int nw_link_handle_uevent(nw_link* link, sd_device* device, sd_device_action_t action); + // JSON int nw_link_to_json(nw_link* link, struct json_object* o); -- 2.39.2 From 4d7437ddbcb56da5d64ae9189bdb2c8c161e4a9b Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 10 Jun 2023 12:04:18 +0000 Subject: [PATCH 10/16] links: Initialize udev device when links are created Signed-off-by: Michael Tremer --- src/networkd/link.c | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/networkd/link.c b/src/networkd/link.c index b0d9e865..8ba5da59 100644 --- a/src/networkd/link.c +++ b/src/networkd/link.c @@ -245,6 +245,53 @@ static int nw_link_carrier_lost(nw_link* link) { return 0; // XXX TODO } +static int nw_link_initialize(nw_link* link) { + sd_device *device = NULL; + int r; + + // Fetch device + r = sd_device_new_from_ifindex(&device, link->ifindex); + if (r < 0) { + WARNING("Could not find device for link %d: %s\n", link->ifindex, strerror(-r)); + r = 0; + goto ERROR; + } + + // Check if device is initialized + r = sd_device_get_is_initialized(device); + switch (r) { + // Initialized - fallthrough + case 0: + break; + + case 1: + DEBUG("The device has not been initialized, yet\n"); + r = 0; + goto ERROR; + + default: + WARNING("Could not check whether device is initialized, ignoring: %s\n", + strerror(-r)); + goto ERROR; + } + + // XXX Check renaming?! + + if (link->device) + sd_device_unref(link->device); + + // Store the device + link->device = sd_device_ref(device); + + DEBUG("Link %d has been initialized\n", link->ifindex); + +ERROR: + if (device) + sd_device_unref(device); + + return r; +} + static int nw_link_update_ifname(nw_link* link, sd_netlink_message* message) { const char* ifname = NULL; int r; @@ -476,6 +523,11 @@ int nw_link_process(sd_netlink* rtnl, sd_netlink_message* message, void* data) { goto ERROR; } + // Initialize the link + r = nw_link_initialize(link); + if (r < 0) + goto ERROR; + // Add it to the list r = nw_links_add_link(links, link); if (r) -- 2.39.2 From 7d01b16d6ae3502e48bf99b572be627a2d8a2eac Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 11 Jun 2023 11:07:25 +0000 Subject: [PATCH 11/16] link: Skip uevent when the device is renaming Signed-off-by: Michael Tremer --- src/networkd/link.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/networkd/link.c b/src/networkd/link.c index 8ba5da59..cb79dd28 100644 --- a/src/networkd/link.c +++ b/src/networkd/link.c @@ -613,7 +613,42 @@ ERROR: // uevent +static int nw_link_uevent_device_is_renaming(sd_device* device) { + int r; + + r = sd_device_get_property_value(device, "ID_RENAMING", NULL); + switch (r) { + case -ENOENT: + return 0; + + case 0: + return 1; + + default: + return r; + } +} + int nw_link_handle_uevent(nw_link* link, sd_device* device, sd_device_action_t action) { + int r; + + // Check if the device is renaming + r = nw_link_uevent_device_is_renaming(device); + switch (r) { + // Not renaming - Fallthrough + case 0: + break; + + case 1: + DEBUG("Device is renaming, skipping initialization\n"); + return 0; + + default: + ERROR("Could not determine whether the device is being renamed: %s\n", + strerror(-r)); + return r; + } + // We need to remove or replace the stored device as it is now outdated if (link->device) { sd_device_unref(link->device); -- 2.39.2 From 25808f650e39dbcdf3e2d0ba42d079a9d5e43079 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 11 Jun 2023 13:02:35 +0000 Subject: [PATCH 12/16] networkd: Implement smarter handling of the configuration file hierarchy Signed-off-by: Michael Tremer --- src/networkd/config.c | 207 +++++++++++++++++++++++++++++++++++++++++- src/networkd/config.h | 22 +++++ src/networkd/daemon.c | 72 +++++---------- src/networkd/daemon.h | 7 +- src/networkd/port.c | 66 +++++++------- src/networkd/port.h | 2 +- src/networkd/ports.c | 50 +++------- src/networkd/zone.c | 112 +++++++++++++---------- src/networkd/zone.h | 9 +- src/networkd/zones.c | 37 ++++---- 10 files changed, 386 insertions(+), 198 deletions(-) diff --git a/src/networkd/config.c b/src/networkd/config.c index c6281cb2..53fd8e30 100644 --- a/src/networkd/config.c +++ b/src/networkd/config.c @@ -18,12 +18,16 @@ # # #############################################################################*/ +#include #include +#include #include #include #include #include #include +#include +#include #include #include "address.h" @@ -161,7 +165,6 @@ int nw_config_open(nw_config** config, const char* path) { // Create a new configuration r = nw_config_create(config, f); -ERROR: if (f) fclose(f); @@ -419,6 +422,208 @@ int nw_config_set_bool(nw_config* config, const char* key, const int value) { return nw_config_set(config, key, value ? "true" : "false"); } +/* + Directory +*/ + +struct nw_configd { + int nrefs; + + char path[PATH_MAX]; + int fd; +}; + +static void nw_configd_free(nw_configd* dir) { + if (dir->fd >= 0) + close(dir->fd); + + free(dir); +} + +static int __nw_configd_create(nw_configd** dir, int fd, const char* path) { + nw_configd* d = NULL; + int r; + + // Allocate a new object + d = calloc(1, sizeof(*d)); + if (!d) + return -errno; + + // Initialize the reference counter + d->nrefs = 1; + + // Store the file descriptor + d->fd = dup(fd); + if (d->fd < 0) { + r = -errno; + goto ERROR; + } + + // Store path + if (path) { + r = nw_string_set(d->path, path); + if (r < 0) + goto ERROR; + } + + *dir = d; + return 0; + +ERROR: + nw_configd_free(d); + return r; +} + +int nw_configd_create(nw_configd** dir, const char* path) { + int fd; + + // Open the directory + fd = open(path, O_DIRECTORY); + if (fd < 0) { + ERROR("Could not open %s: %m\n", path); + return -errno; + } + + return __nw_configd_create(dir, fd, path); +} + +nw_configd* nw_configd_ref(nw_configd* dir) { + dir->nrefs++; + + return dir; +} + +nw_configd* nw_configd_unref(nw_configd* dir) { + if (--dir->nrefs > 0) + return dir; + + nw_configd_free(dir); + return NULL; +} + +static int nw_configd_open(nw_configd* dir, const char* path, int flags) { + return openat(dir->fd, path, flags); +} + +FILE* nw_configd_fopen(nw_configd* dir, const char* path, const char* mode) { + int fd; + + // Open file + fd = nw_configd_open(dir, path, 0); + if (fd < 0) + return NULL; + + // Return a file handle + return fdopen(fd, mode); +} + +int nw_configd_open_config(nw_config** config, nw_configd* dir, const char* path) { + FILE* f = NULL; + int r; + + // Open the file + f = nw_configd_fopen(dir, path, "r"); + if (!f) + return -errno; + + // Create configuration + r = nw_config_create(config, f); + if (r < 0) + goto ERROR; + +ERROR: + if (f) + fclose(f); + + return r; +} + +int nw_configd_unlink(nw_configd* dir, const char* path, int flags) { + return unlinkat(dir->fd, path, flags); +} + +nw_configd* nw_configd_descend(nw_configd* dir, const char* path) { + nw_configd* d = NULL; + char p[PATH_MAX]; + int fd = -1; + int r; + + // Join paths + r = nw_path_join(p, dir->path, path); + if (r < 0) + goto ERROR; + + // Open directory + fd = nw_configd_open(dir, path, O_DIRECTORY); + if (fd < 0) { + ERROR("Could not open %s: %m\n", p); + goto ERROR; + } + + // Create a new config directory object + r = __nw_configd_create(&d, fd, p); + if (r < 0) + goto ERROR; + +ERROR: + if (fd >= 0) + close(fd); + + return d; +} + +int nw_configd_walk(nw_configd* dir, nw_configd_walk_callback callback, void* data) { + FILE* f = NULL; + DIR* d = NULL; + struct dirent* e = NULL; + int r; + + // Re-open the directory + d = fdopendir(dir->fd); + if (!d) { + r = -errno; + goto ERROR; + } + + // Walk trough everything + for (;;) { + // Read the next entry + e = readdir(d); + if (!e) + break; + + // Skip anything that is not a regular file + if (e->d_type != DT_REG) + continue; + + // Skip hidden files + if (e->d_name[0] == '.') + continue; + + // Open the file + f = nw_configd_fopen(dir, e->d_name, "r"); + if (!f) { + r = -errno; + goto ERROR; + } + + // Call the callback + r = callback(e, f, data); + fclose(f); + + if (r < 0) + goto ERROR; + } + + r = 0; + +ERROR: + if (d) + closedir(d); + + return r; +} + /* Options */ diff --git a/src/networkd/config.h b/src/networkd/config.h index 4b8bc01f..3e7c0977 100644 --- a/src/networkd/config.h +++ b/src/networkd/config.h @@ -21,6 +21,7 @@ #ifndef NETWORKD_CONFIG_H #define NETWORKD_CONFIG_H +#include #include #define NETWORK_CONFIG_KEY_MAX_LENGTH 128 @@ -52,6 +53,27 @@ int nw_config_set_int(nw_config* config, const char* key, const int value); int nw_config_get_bool(nw_config* config, const char* key); int nw_config_set_bool(nw_config* config, const char* key, const int value); +/* + Directory +*/ + +typedef struct nw_configd nw_configd; + +int nw_configd_create(nw_configd** dir, const char* path); + +nw_configd* nw_configd_ref(nw_configd* dir); +nw_configd* nw_configd_unref(nw_configd* dir); + +FILE* nw_configd_fopen(nw_configd* dir, const char* path, const char* mode); +int nw_configd_open_config(nw_config** config, nw_configd* dir, const char* path); +int nw_configd_unlink(nw_configd* dir, const char* path, int flags); + +nw_configd* nw_configd_descend(nw_configd* dir, const char* path); + +typedef int (*nw_configd_walk_callback)(struct dirent* entry, FILE* f, void* data); + +int nw_configd_walk(nw_configd* dir, nw_configd_walk_callback callback, void* data); + /* Options */ diff --git a/src/networkd/daemon.c b/src/networkd/daemon.c index a62e343f..dfbcc151 100644 --- a/src/networkd/daemon.c +++ b/src/networkd/daemon.c @@ -51,7 +51,7 @@ struct nw_daemon { int nrefs; // Configuration - int configfd; + nw_configd* configd; nw_config* config; // Event Loop @@ -103,36 +103,14 @@ static int __nw_daemon_reload(sd_event_source* source, const struct signalfd_sig */ static int nw_daemon_config_open(nw_daemon* daemon, const char* path) { - // Open the directory - daemon->configfd = open(path, O_DIRECTORY); - if (daemon->configfd < 0) { - ERROR("Could not open %s: %m\n", path); - return -errno; - } - - return 0; -} - -FILE* nw_daemon_config_fopen(nw_daemon* daemon, const char* path, const char* mode) { - // Open the file - int fd = openat(daemon->configfd, path, 0); - if (fd < 0) { - ERROR("Could not open configuration file %s: %m\n", path); - return NULL; - } - - // Return a file handle - return fdopen(fd, mode); -} + int r; -DIR* nw_daemon_config_opendir(nw_daemon* daemon, const char* path) { - int fd = openat(daemon->configfd, path, O_DIRECTORY); - if (fd < 0) { - ERROR("Could not open configuration directory %s: %m\n", path); - return NULL; - } + // Open the configuration directory + r = nw_configd_create(&daemon->configd, path); + if (r < 0) + return r; - return fdopendir(fd); + return 0; } static int nw_daemon_parse_argv(nw_daemon* daemon, int argc, char* argv[]) { @@ -213,33 +191,17 @@ static int nw_daemon_setup_loop(nw_daemon* daemon) { } static int nw_daemon_load_config(nw_daemon* daemon) { - FILE* f = NULL; int r; // If no configuration path has been opened yet, we will open something - if (!daemon->configfd) { + if (!daemon->configd) { r = nw_daemon_config_open(daemon, CONFIG_DIR); if (r < 0) - goto ERROR; + return r; } // Open the configuration file - f = nw_daemon_config_fopen(daemon, "settings", "r"); - if (!f) { - r = -errno; - goto ERROR; - } - - // Create configuration - r = nw_config_create(&daemon->config, f); - if (r < 0) - goto ERROR; - -ERROR: - if (f) - fclose(f); - - return r; + return nw_configd_open_config(&daemon->config, daemon->configd, "settings"); } static int nw_start_device_monitor(nw_daemon* daemon) { @@ -540,8 +502,8 @@ static void nw_daemon_free(nw_daemon* daemon) { // Cleanup common objects nw_daemon_cleanup(daemon); - if (daemon->configfd > 0) - close(daemon->configfd); + if (daemon->configd) + nw_configd_unref(daemon->configd); if (daemon->stats_collector_event) sd_event_source_unref(daemon->stats_collector_event); if (daemon->bus) @@ -640,6 +602,16 @@ int nw_daemon_save(nw_daemon* daemon) { return 0; } +nw_configd* nw_daemon_configd(nw_daemon* daemon, const char* path) { + if (!daemon->configd) + return NULL; + + if (path) + return nw_configd_descend(daemon->configd, path); + + return nw_configd_ref(daemon->configd); +} + /* Bus */ diff --git a/src/networkd/daemon.h b/src/networkd/daemon.h index b03086c8..2d56d797 100644 --- a/src/networkd/daemon.h +++ b/src/networkd/daemon.h @@ -29,6 +29,7 @@ typedef struct nw_daemon nw_daemon; +#include "config.h" #include "link.h" #include "links.h" #include "port.h" @@ -47,11 +48,7 @@ int nw_daemon_reload(nw_daemon* daemon); int nw_daemon_save(nw_daemon* daemon); -/* - Configuration -*/ -FILE* nw_daemon_config_fopen(nw_daemon* daemon, const char* path, const char* mode); -DIR* nw_daemon_config_opendir(nw_daemon* daemon, const char* path); +nw_configd* nw_daemon_configd(nw_daemon* daemon, const char* path); /* Bus diff --git a/src/networkd/port.c b/src/networkd/port.c index f5479569..141bba59 100644 --- a/src/networkd/port.c +++ b/src/networkd/port.c @@ -222,24 +222,10 @@ ERROR: return r; } -int nw_port_open(nw_port** port, nw_daemon* daemon, const char* name) { +int nw_port_open(nw_port** port, nw_daemon* daemon, const char* name, FILE* f) { nw_config* config = NULL; - FILE* f = NULL; - char path[PATH_MAX]; int r; - // Make path - r = nw_string_format(path, "ports/%s", name); - if (r < 0) - goto ERROR; - - // Open the configuration file - f = nw_daemon_config_fopen(daemon, path, "r"); - if (!f) { - r = -errno; - goto ERROR; - } - // Initialize the configuration r = nw_config_create(&config, f); if (r < 0) @@ -248,7 +234,7 @@ int nw_port_open(nw_port** port, nw_daemon* daemon, const char* name) { // Fetch the type const char* type = nw_config_get(config, "TYPE"); if (!type) { - ERROR("Port configuration %s has no TYPE\n", path); + ERROR("Port %s has no TYPE\n", name); r = -ENOTSUP; goto ERROR; } @@ -261,8 +247,6 @@ int nw_port_open(nw_port** port, nw_daemon* daemon, const char* name) { ERROR: if (config) nw_config_unref(config); - if (f) - fclose(f); return r; } @@ -292,6 +276,7 @@ static void __nw_port_unref(void* data) { } int nw_port_destroy(nw_port* port) { + nw_configd* configd = NULL; int r; DEBUG("Destroying port %s\n", port->name); @@ -299,26 +284,33 @@ int nw_port_destroy(nw_port* port) { // Destroy the physical link (if exists) if (port->link) { r = nw_link_destroy(port->link); - if (r) - return r; + if (r < 0) + goto ERROR; } // Dereference the port from other ports r = nw_daemon_ports_walk(port->daemon, __nw_port_drop_port, port); - if (r) - return r; + if (r < 0) + goto ERROR; // Dereference the port from other zones r = nw_daemon_zones_walk(port->daemon, __nw_zone_drop_port, port); - if (r) - return r; + if (r < 0) + goto ERROR; - // Destroy the configuration - r = nw_config_destroy(port->config); - if (r) - return r; + // Fetch the configuration directory + configd = nw_daemon_configd(port->daemon, "ports"); + if (configd) { + r = nw_configd_unlink(configd, port->name, 0); + if (r < 0) + goto ERROR; + } - return 0; +ERROR: + if (configd) + nw_configd_unref(configd); + + return r; } int __nw_port_drop_port(nw_daemon* daemon, nw_port* port, void* data) { @@ -345,17 +337,19 @@ int __nw_port_drop_port(nw_daemon* daemon, nw_port* port, void* data) { } int nw_port_save(nw_port* port) { - char path[PATH_MAX]; + nw_configd* configd = NULL; FILE* f = NULL; int r; - // Compose path - r = nw_string_format(path, "ports/%s", port->name); - if (r < 0) - return r; + // Fetch configuration directory + configd = nw_daemon_configd(port->daemon, "ports"); + if (!configd) { + r = -errno; + goto ERROR; + } // Open file - f = nw_daemon_config_fopen(port->daemon, path, "w"); + f = nw_configd_fopen(configd, port->name, "w"); if (!f) { r = -errno; goto ERROR; @@ -372,6 +366,8 @@ int nw_port_save(nw_port* port) { goto ERROR; ERROR: + if (configd) + nw_configd_unref(configd); if (f) fclose(f); if (r) diff --git a/src/networkd/port.h b/src/networkd/port.h index f1cb3d28..3cbe4b09 100644 --- a/src/networkd/port.h +++ b/src/networkd/port.h @@ -105,7 +105,7 @@ struct nw_port { int nw_port_create(nw_port** port, nw_daemon* daemon, const nw_port_type_id_t type, const char* name, nw_config* config); -int nw_port_open(nw_port** port, nw_daemon* daemon, const char* name); +int nw_port_open(nw_port** port, nw_daemon* daemon, const char* name, FILE* f); nw_port* nw_port_ref(nw_port* port); nw_port* nw_port_unref(nw_port* port); diff --git a/src/networkd/ports.c b/src/networkd/ports.c index 761e564a..95a13e37 100644 --- a/src/networkd/ports.c +++ b/src/networkd/ports.c @@ -130,12 +130,14 @@ static int nw_ports_add_port(nw_ports* ports, nw_port* port) { return 0; } -static int nw_ports_enumerate_port(nw_ports* ports, const char* name) { +static int __nw_ports_enumerate(struct dirent* entry, FILE* f, void* data) { nw_port* port = NULL; int r; + nw_ports* ports = (nw_ports*)data; + // Create a new port - r = nw_port_open(&port, ports->daemon, name); + r = nw_port_open(&port, ports->daemon, entry->d_name, f); if (r < 0 || r == 1) goto ERROR; @@ -152,45 +154,19 @@ ERROR: } int nw_ports_enumerate(nw_ports* ports) { - DIR* d = NULL; - struct dirent* entry = NULL; + nw_configd* configd = NULL; int r; - // Open the ports directory - d = nw_daemon_config_opendir(ports->daemon, "ports"); - if (!d) { - switch (errno) { - case ENOENT: - return 0; - - default: - return -errno; - } - } - - for (;;) { - // Read the next entry - entry = readdir(d); - if (!entry) - break; - - // Skip anything that is not a regular file - if (entry->d_type != DT_REG) - continue; + // Fetch ports configuration directory + configd = nw_daemon_configd(ports->daemon, "ports"); + if (!configd) + return 0; - // Skip hidden files - if (entry->d_name[0] == '.') - continue; + // Walk through all files + r = nw_configd_walk(configd, __nw_ports_enumerate, ports); - // Enumerate the port - r = nw_ports_enumerate_port(ports, entry->d_name); - if (r < 0) - goto ERROR; - } - -ERROR: - if (d) - closedir(d); + // Cleanup + nw_configd_unref(configd); return r; } diff --git a/src/networkd/zone.c b/src/networkd/zone.c index cc5fdaf5..19d221fb 100644 --- a/src/networkd/zone.c +++ b/src/networkd/zone.c @@ -32,6 +32,12 @@ #include "string.h" #include "zone.h" +static const nw_string_table_t nw_zone_type_id[] = { + { -1, NULL }, +}; + +NW_STRING_TABLE_LOOKUP(nw_zone_type_id_t, nw_zone_type_id) + struct nw_zone { nw_daemon* daemon; int nrefs; @@ -45,32 +51,6 @@ struct nw_zone { nw_config *config; }; -#define nw_zone_path(zone, path, format, ...) \ - __nw_zone_path(zone, path, sizeof(path), format, __VA_ARGS__) - -static int __nw_zone_path(nw_zone* zone, char* p, const size_t length, - const char* format, ...) { - char prefix[NAME_MAX]; - char suffix[NAME_MAX]; - va_list args; - int r; - - // Format the prefix - r = nw_string_format(prefix, "%s/zones/%s", CONFIG_DIR, zone->name); - if (r) - return r; - - // Format the suffix - va_start(args, format); - r = nw_string_vformat(suffix, format, args); - va_end(args); - if (r) - return r; - - // Join the two parts together - return __nw_path_join(p, length, prefix, suffix); -} - static void nw_zone_free(nw_zone* zone) { if (zone->link) nw_link_unref(zone->link); @@ -109,8 +89,7 @@ static int nw_zone_set_link(nw_zone* zone, nw_link* link) { static int nw_zone_setup(nw_zone* zone) { nw_link* link = NULL; - char path[PATH_MAX]; - int r; + int r = 0; // Find the link link = nw_daemon_get_link_by_name(zone->daemon, zone->name); @@ -120,18 +99,6 @@ static int nw_zone_setup(nw_zone* zone) { goto ERROR; } -#if 0 - // Compose the path to the main configuration file - r = nw_zone_path(zone, path, "%s", "settings"); - if (r) - goto ERROR; - - // Initialize the configuration - r = nw_config_create(&zone->config, path); - if (r) - goto ERROR; -#endif - ERROR: if (link) nw_link_unref(link); @@ -139,13 +106,15 @@ ERROR: return r; } -int nw_zone_create(nw_zone** zone, nw_daemon* daemon, const char* name) { +int nw_zone_create(nw_zone** zone, nw_daemon* daemon, const nw_zone_type_id_t type, + const char* name, nw_config* config) { + nw_zone* z = NULL; int r; // Allocate a new object - nw_zone* z = calloc(1, sizeof(*z)); + z = calloc(1, sizeof(*z)); if (!z) - return 1; + return -errno; // Store a reference to the daemon z->daemon = nw_daemon_ref(daemon); @@ -158,6 +127,11 @@ int nw_zone_create(nw_zone** zone, nw_daemon* daemon, const char* name) { if (r) goto ERROR; + // Copy the configuration + r = nw_config_copy(config, &z->config); + if (r < 0) + goto ERROR; + // Setup the zone r = nw_zone_setup(z); if (r) @@ -171,6 +145,35 @@ ERROR: return r; } +int nw_zone_open(nw_zone** zone, nw_daemon* daemon, const char* name, FILE* f) { + nw_config* config = NULL; + int r; + + // Initialize the configuration + r = nw_config_create(&config, f); + if (r < 0) + goto ERROR; + + // Fetch the type + const char* type = nw_config_get(config, "TYPE"); + if (!type) { + ERROR("Zone %s has no TYPE\n", name); + r = -ENOTSUP; + goto ERROR; + } + + // Create a new zone + r = nw_zone_create(zone, daemon, nw_zone_type_id_from_string(type), name, config); + if (r < 0) + goto ERROR; + +ERROR: + if (config) + nw_config_unref(config); + + return r; +} + nw_zone* nw_zone_ref(nw_zone* zone) { zone->nrefs++; @@ -195,30 +198,41 @@ int __nw_zone_drop_port(nw_daemon* daemon, nw_zone* zone, void* data) { } int nw_zone_save(nw_zone* zone) { - char path[PATH_MAX]; + nw_configd* configd = NULL; FILE* f = NULL; int r; - // Compose path - r = nw_string_format(path, "zones/%s/settings", zone->name); - if (r < 0) + // Fetch configuration directory + configd = nw_daemon_configd(zone->daemon, "zones"); + if (!configd) { + r = -errno; goto ERROR; + } // Open file - f = nw_daemon_config_fopen(zone->daemon, path, "w"); + f = nw_configd_fopen(configd, zone->name, "w"); if (!f) { r = -errno; goto ERROR; } + // Write out the configuration + r = nw_config_options_write(zone->config); + if (r < 0) + goto ERROR; + // Write the configuration r = nw_config_write(zone->config, f); - if (r) + if (r < 0) goto ERROR; ERROR: + if (configd) + nw_configd_unref(configd); if (f) fclose(f); + if (r) + ERROR("Could not save configuration for zone %s: %s\n", zone->name, strerror(-r)); return r; } diff --git a/src/networkd/zone.h b/src/networkd/zone.h index 480440fb..6ab951b4 100644 --- a/src/networkd/zone.h +++ b/src/networkd/zone.h @@ -28,11 +28,18 @@ typedef struct nw_zone nw_zone; +typedef enum nw_zone_type_id { + __EMPTY +} nw_zone_type_id_t; + #include +#include "config.h" #include "daemon.h" -int nw_zone_create(nw_zone** zone, nw_daemon* daemon, const char* name); +int nw_zone_create(nw_zone** zone, nw_daemon* daemon, const nw_zone_type_id_t type, + const char* name, nw_config* config); +int nw_zone_open(nw_zone** zone, nw_daemon* daemon, const char* name, FILE* f); nw_zone* nw_zone_ref(nw_zone* zone); nw_zone* nw_zone_unref(nw_zone* zone); diff --git a/src/networkd/zones.c b/src/networkd/zones.c index 84a6673e..654e637e 100644 --- a/src/networkd/zones.c +++ b/src/networkd/zones.c @@ -135,30 +135,15 @@ static int nw_zones_add_zone(nw_zones* zones, nw_zone* zone) { return 0; } -static int __nw_zones_enumerate(const char* path, const struct stat* s, void* data) { +static int __nw_zones_enumerate(struct dirent* entry, FILE* f, void* data) { nw_zone* zone = NULL; int r; nw_zones* zones = (nw_zones*)data; - // Skip anything that isn't a directory - if (!S_ISDIR(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 zone - r = nw_zone_create(&zone, zones->daemon, name); - if (r) + r = nw_zone_open(&zone, zones->daemon, entry->d_name, f); + if (r < 0 || r == 1) goto ERROR; // Add the zone to the list @@ -174,7 +159,21 @@ ERROR: } int nw_zones_enumerate(nw_zones* zones) { - return nw_ftw(ZONE_CONFIG_DIR, ZONE_CONFIG_DIR "/*", __nw_zones_enumerate, zones); + nw_configd* configd = NULL; + int r; + + // Fetch zones configuration directory + configd = nw_daemon_configd(zones->daemon, "zones"); + if (!configd) + return 0; + + // Walk through all files + r = nw_configd_walk(configd, __nw_zones_enumerate, zones); + + // Cleanup + nw_configd_unref(configd); + + return r; } size_t nw_zones_num(nw_zones* zones) { -- 2.39.2 From 416ba6cd142bc5006b1c977bec86fc12dfffc80c Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 11 Jun 2023 13:09:41 +0000 Subject: [PATCH 13/16] zones: Move "struct nw_zone" into header Signed-off-by: Michael Tremer --- src/networkd/port-vlan.h | 2 +- src/networkd/port.h | 6 +----- src/networkd/zone.c | 13 ------------- src/networkd/zone.h | 16 +++++++++++++++- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/networkd/port-vlan.h b/src/networkd/port-vlan.h index e17f7e7a..c8072389 100644 --- a/src/networkd/port-vlan.h +++ b/src/networkd/port-vlan.h @@ -47,7 +47,7 @@ struct nw_port_vlan { // If the parent has not been read from the configuration we will // save the name and try to find it later. - char __parent_name[IF_NAMESIZE]; + char __parent_name[IFNAMSIZ]; }; extern const nw_port_type_t nw_port_type_vlan; diff --git a/src/networkd/port.h b/src/networkd/port.h index 3cbe4b09..422a65f2 100644 --- a/src/networkd/port.h +++ b/src/networkd/port.h @@ -25,10 +25,6 @@ #include -#ifndef IF_NAMESIZE -#define IF_NAMESIZE 16 -#endif - #define PORT_CONFIG_DIR CONFIG_DIR "/ports" typedef struct nw_port nw_port; @@ -82,7 +78,7 @@ struct nw_port { nw_link* link; const nw_port_type_t* type; - char name[IF_NAMESIZE]; + char name[IFNAMSIZ]; // Configuration nw_config *config; diff --git a/src/networkd/zone.c b/src/networkd/zone.c index 19d221fb..1610dc0c 100644 --- a/src/networkd/zone.c +++ b/src/networkd/zone.c @@ -38,19 +38,6 @@ static const nw_string_table_t nw_zone_type_id[] = { NW_STRING_TABLE_LOOKUP(nw_zone_type_id_t, nw_zone_type_id) -struct nw_zone { - nw_daemon* daemon; - int nrefs; - - // Link - nw_link* link; - - char name[NETWORK_ZONE_NAME_MAX_LENGTH]; - - // Configuration - nw_config *config; -}; - static void nw_zone_free(nw_zone* zone) { if (zone->link) nw_link_unref(zone->link); diff --git a/src/networkd/zone.h b/src/networkd/zone.h index 6ab951b4..2d1e1dc0 100644 --- a/src/networkd/zone.h +++ b/src/networkd/zone.h @@ -23,7 +23,6 @@ #define ZONE_CONFIG_DIR CONFIG_DIR "/zones" -#define NETWORK_ZONE_NAME_MAX_LENGTH 16 #define NETWORK_ZONE_DEFAULT_MTU 1500 typedef struct nw_zone nw_zone; @@ -32,10 +31,25 @@ typedef enum nw_zone_type_id { __EMPTY } nw_zone_type_id_t; +#include #include #include "config.h" #include "daemon.h" +#include "link.h" + +struct nw_zone { + nw_daemon* daemon; + int nrefs; + + // Link + nw_link* link; + + char name[IFNAMSIZ]; + + // Configuration + nw_config *config; +}; int nw_zone_create(nw_zone** zone, nw_daemon* daemon, const nw_zone_type_id_t type, const char* name, nw_config* config); -- 2.39.2 From 5548c63c9425f39472e75fef86ad2b8877657997 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 11 Jun 2023 13:10:31 +0000 Subject: [PATCH 14/16] Drop unused configuration file paths Signed-off-by: Michael Tremer --- src/networkd/port.h | 2 -- src/networkd/zone.h | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/networkd/port.h b/src/networkd/port.h index 422a65f2..97700795 100644 --- a/src/networkd/port.h +++ b/src/networkd/port.h @@ -25,8 +25,6 @@ #include -#define PORT_CONFIG_DIR CONFIG_DIR "/ports" - typedef struct nw_port nw_port; typedef enum nw_port_type_id { diff --git a/src/networkd/zone.h b/src/networkd/zone.h index 2d1e1dc0..2ece268c 100644 --- a/src/networkd/zone.h +++ b/src/networkd/zone.h @@ -21,8 +21,6 @@ #ifndef NETWORKD_ZONE_H #define NETWORKD_ZONE_H -#define ZONE_CONFIG_DIR CONFIG_DIR "/zones" - #define NETWORK_ZONE_DEFAULT_MTU 1500 typedef struct nw_zone nw_zone; -- 2.39.2 From 10edad249159db9d6ef1e2b6e7a8fac78f183fce Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 11 Jun 2023 13:11:11 +0000 Subject: [PATCH 15/16] util: Drop nw_ftw Signed-off-by: Michael Tremer --- src/networkd/util.c | 40 ---------------------------------------- src/networkd/util.h | 5 ----- 2 files changed, 45 deletions(-) diff --git a/src/networkd/util.c b/src/networkd/util.c index 0381ea86..e2f5cf16 100644 --- a/src/networkd/util.c +++ b/src/networkd/util.c @@ -18,44 +18,4 @@ # # #############################################################################*/ -#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 index 950afdac..11317ff1 100644 --- a/src/networkd/util.h +++ b/src/networkd/util.h @@ -21,9 +21,4 @@ #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 */ -- 2.39.2 From b9ac9712ab5dadf45ddc3749d7c9e393bc3eddaf Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 19 Sep 2023 12:54:53 +0000 Subject: [PATCH 16/16] Makefile: Fix typo in localstatedir Signed-off-by: Michael Tremer --- Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile.am b/Makefile.am index 8c0d0c0f..7ca12292 100644 --- a/Makefile.am +++ b/Makefile.am @@ -51,7 +51,7 @@ hooks_zonesdir = $(hooksdir)/zones triggersdir = $(networkdir)/triggers -logdir = $(localestatedir)/log/network +logdir = $(localstatedir)/log/network utildir = $(networkdir) CLEANFILES = -- 2.39.2