]> git.ipfire.org Git - network.git/commitdiff
networkd: Perform port setup from configuration
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2023 11:21:02 +0000 (11:21 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Feb 2023 11:21:02 +0000 (11:21 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/port.c
src/networkd/port.h

index 019521f6ea53595e7157dcfb308506b63c2adb15..f78489b02d29ce3a950ef15dba802bbead9d6ca1 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <limits.h>
 #include <net/if.h>
 #include <stdlib.h>
 
 #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;
 
index 250a694c547444fd746bfd26350114c88e816f9c..af7fbdddca925a639f86204a30bd9c46dd8725d0 100644 (file)
 
 #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);