From: Michael Tremer Date: Sat, 11 Feb 2023 11:21:02 +0000 (+0000) Subject: networkd: Perform port setup from configuration X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=827435c82f60ba1b7717d74faeac6cb6b85d6588;p=network.git networkd: Perform port setup from configuration Signed-off-by: Michael Tremer --- diff --git a/src/networkd/port.c b/src/networkd/port.c index 019521f6..f78489b0 100644 --- a/src/networkd/port.c +++ b/src/networkd/port.c @@ -18,10 +18,12 @@ # # #############################################################################*/ +#include #include #include #include "config.h" +#include "logging.h" #include "string.h" #include "port.h" @@ -29,11 +31,31 @@ struct nw_port { int nrefs; char name[IF_NAMESIZE]; + nw_port_type_t type; // Configuration struct nw_config *config; }; +static const struct nw_port_type_map { + nw_port_type_t type; + const char* name; +} nw_port_type_map[] = { + { NW_PORT_DUMMY, "dummy" }, + { NW_PORT_UNKNOWN, NULL }, +}; + +static nw_port_type_t nw_port_type_from_string(const char* s) { + const struct nw_port_type_map* map = NULL; + + for (map = nw_port_type_map; *map->name; map++) { + if (strcmp(map->name, s) == 0) + return map->type; + } + + return NW_PORT_UNKNOWN; +} + static void nw_port_free(struct nw_port* port) { if (port->config) nw_config_unref(port->config); @@ -41,6 +63,55 @@ static void nw_port_free(struct nw_port* port) { free(port); } +static int nw_port_setup_common(struct nw_port* port) { + return 0; // XXX TODO +} + +static nw_port_type_t nw_port_setup_type(struct nw_port* port) { + const char* type = nw_config_get(port->config, "TYPE"); + if (!type) + return NW_PORT_UNKNOWN; + + return nw_port_type_from_string(type); +} + +static int nw_port_setup(struct nw_port* port) { + char path[PATH_MAX]; + int r; + + // Compose the path to the main configuration file + r = nw_path_join(path, PORT_CONFIG_DIR, port->name); + if (r) + return r; + + // Initialize the configuration + r = nw_config_create(&port->config, path); + if (r) + return r; + + // Determine type + port->type = nw_port_setup_type(port); + if (!port->type) { + ERROR("Could not determine type of port %s\n", port->name); + return 0; + } + + // Perform some common initialization + r = nw_port_setup_common(port); + if (r) + return r; + + // Call any custom initialization + switch (port->type) { + // These do not need any special initialization + case NW_PORT_DUMMY: + case NW_PORT_UNKNOWN: + break; + } + + return 0; +} + int nw_port_create(struct nw_port** port, const char* name) { int r; @@ -57,6 +128,11 @@ int nw_port_create(struct nw_port** port, const char* name) { if (r) goto ERROR; + // Setup the port + r = nw_port_setup(p); + if (r) + goto ERROR; + *port = p; return 0; diff --git a/src/networkd/port.h b/src/networkd/port.h index 250a694c..af7fbddd 100644 --- a/src/networkd/port.h +++ b/src/networkd/port.h @@ -23,6 +23,11 @@ #define PORT_CONFIG_DIR CONFIG_DIR "/ports" +typedef enum nw_port_type { + NW_PORT_UNKNOWN = 0, + NW_PORT_DUMMY, +} nw_port_type_t; + struct nw_port; int nw_port_create(struct nw_port** port, const char* name);