From: Michael Tremer Date: Sat, 15 Apr 2023 11:15:15 +0000 (+0000) Subject: ports: Require type to be set at all times X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=06bc93d3136c4bd2d75f7c50051b8b96d29dab59;p=network.git ports: Require type to be set at all times Signed-off-by: Michael Tremer --- diff --git a/src/networkd/port.c b/src/networkd/port.c index 7638c7b7..aad62106 100644 --- a/src/networkd/port.c +++ b/src/networkd/port.c @@ -128,14 +128,6 @@ static int nw_port_setup_common(nw_port* port) { return 0; } -static nw_port_type_t nw_port_setup_type(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_set_link(nw_port* port, nw_link* link) { // Do nothing if the same link is being re-assigned if (port->link == link) @@ -184,13 +176,6 @@ static int nw_port_setup(nw_port* port) { if (r) goto ERROR; - // Determine type - port->type = nw_port_setup_type(port); - if (!port->type) { - ERROR("Could not determine type of port %s\n", port->name); - goto ERROR; - } - // Perform some common initialization r = nw_port_setup_common(port); if (r) @@ -211,7 +196,7 @@ ERROR: return r; } -int nw_port_create(nw_port** port, nw_daemon* daemon, const char* name) { +int nw_port_create(nw_port** port, nw_daemon* daemon, nw_port_type_t type, const char* name) { int r; // Allocate a new object @@ -225,6 +210,9 @@ int nw_port_create(nw_port** port, nw_daemon* daemon, const char* name) { // Initialize reference counter p->nrefs = 1; + // Store the type + p->type = type; + // Store the name r = nw_string_set(p->name, name); if (r) @@ -243,6 +231,36 @@ ERROR: return r; } +int nw_port_create_from_config(nw_port** port, nw_daemon* daemon, + const char* name, const char* path) { + nw_config* config = NULL; + int r; + + // Initialize the configuration + r = nw_config_create(&config, path); + if (r) + goto ERROR; + + // Fetch the type + const char* type = nw_config_get(config, "TYPE"); + if (!type) { + ERROR("Port configuration %s has no TYPE\n", path); + r = 1; + goto ERROR; + } + + // Create a new port + r = nw_port_create(port, daemon, nw_port_type_from_string(type), name); + if (r) + goto ERROR; + +ERROR: + if (config) + nw_config_unref(config); + + return r; +} + nw_port* nw_port_ref(nw_port* port) { port->nrefs++; diff --git a/src/networkd/port.h b/src/networkd/port.h index 9dcd6c2d..2f4319b8 100644 --- a/src/networkd/port.h +++ b/src/networkd/port.h @@ -33,7 +33,10 @@ typedef struct nw_port nw_port; #include "address.h" #include "daemon.h" -int nw_port_create(nw_port** port, nw_daemon* daemon, const char* name); +int nw_port_create(nw_port** port, nw_daemon* daemon, + nw_port_type_t type, const char* name); +int nw_port_create_from_config(nw_port** port, nw_daemon* daemon, + const char* name, const char* path); 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 87135d8c..f0a3ebbc 100644 --- a/src/networkd/ports.c +++ b/src/networkd/ports.c @@ -151,7 +151,7 @@ static int __nw_ports_enumerate(const char* path, const struct stat* s, void* da return 0; // Create a new port - r = nw_port_create(&port, ports->daemon, name); + r = nw_port_create_from_config(&port, ports->daemon, name, path); if (r) goto ERROR;