]> git.ipfire.org Git - people/ms/network.git/commitdiff
ports: Require type to be set at all times
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 15 Apr 2023 11:15:15 +0000 (11:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 15 Apr 2023 11:15:15 +0000 (11:15 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/port.c
src/networkd/port.h
src/networkd/ports.c

index 7638c7b78446e9cc55e5d87d536a7f90bfdc4601..aad62106a60cb7cfc0d4bec79c06e2061ab65d31 100644 (file)
@@ -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++;
 
index 9dcd6c2d5dad7324b93622040b67f3c659cfb71d..2f4319b843fc641cf2c87a67c9d3da470e242ba4 100644 (file)
@@ -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);
index 87135d8cb628f19ea378c680f4dbff2c7c3c3021..f0a3ebbc878ac8642e6095e198ad03eb53714fad 100644 (file)
@@ -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;