]> git.ipfire.org Git - people/ms/network.git/commitdiff
config: Implement option that looks up string tables
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 8 Jun 2023 15:46:20 +0000 (15:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 8 Jun 2023 15:46:20 +0000 (15:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/config.c
src/networkd/config.h
src/networkd/port-bonding.c
src/networkd/port-vlan.c

index 39e98818dcf740db9b14ad0a23773931297ba60e..ee5e8b832a748db7d0cf3e012139991aa23bf8c3 100644 (file)
@@ -570,6 +570,43 @@ int nw_config_write_string(nw_config* config,
        return nw_config_set(config, key, *(const char**)value);
 }
 
+// String Table
+
+int nw_config_read_string_table(nw_config* config, const char* key, void* value, void* data) {
+       const char* s = NULL;
+       int* v = (int*)value;
+
+       const nw_string_table_t* table = (nw_string_table_t*)data;
+
+       // Fetch the string
+       s = nw_config_get(config, key);
+       if (!s)
+               return -errno;
+
+       // Lookup the string in the table
+       *v = nw_string_table_lookup_id(table, s);
+
+       // If the result is negative, nothing was found
+       if (*v < 0)
+               return -EINVAL;
+
+       return 0;
+}
+
+int nw_config_write_string_table(nw_config* config,
+               const char* key, const void* value, void* data) {
+       int* v = (int*)value;
+
+       const nw_string_table_t* table = (nw_string_table_t*)data;
+
+       // Lookup the string
+       const char* s = nw_string_table_lookup_string(table, *v);
+       if (!s)
+               return -errno;
+
+       return nw_config_set(config, key, s);
+}
+
 // Address
 
 int nw_config_read_address(nw_config* config, const char* key, void* value, void* data) {
index d6b8db8b05a0ee7e9e282b96b8b5cd7b76a4b482..63e9d180f217e95b4c24f12587ec3ab70696aaae 100644 (file)
@@ -73,18 +73,32 @@ int nw_config_option_add(nw_config* config, const char* key, void* value,
 #define NW_CONFIG_OPTION(config, key, value, read_callback, write_callback, data) \
        nw_config_option_add(config, key, value, 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);
 
+// 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)
+
+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);
+
+// 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)
 
 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);
 
+// 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)
 
index b1d2c182e1bc34ef7d378917c4726182238ca3f1..4e4c8a71786ba688afa637aceb843e32cfc16a8b 100644 (file)
@@ -40,33 +40,12 @@ const nw_string_table_t nw_port_bonding_mode[] = {
 
 NW_STRING_TABLE_LOOKUP(nw_port_bonding_mode_t, nw_port_bonding_mode)
 
-static int nw_port_bonding_read_mode(nw_config* config,
-               const char* key, void* value, void* data) {
-       int* mode = (int*)value;
-
-       const char* p = nw_config_get(config, key);
-       if (p) {
-               *mode = nw_port_bonding_mode_from_string(p);
-               if (!*mode)
-                       return -errno;
-       }
-
-       return 0;
-}
-
-static int nw_port_bonding_write_mode(nw_config* config,
-               const char* key, const void* value, void* data) {
-       const int* mode = (int*)value;
-
-       return nw_config_set(config, key, nw_port_bonding_mode_to_string(*mode));
-}
-
 static int nw_port_bonding_setup(nw_port* port) {
        int r;
 
        // Mode
-       r = NW_CONFIG_OPTION(port->config, "BONDING_MODE", &port->bonding.mode,
-                       nw_port_bonding_read_mode, nw_port_bonding_write_mode, NULL);
+       r = NW_CONFIG_OPTION_STRING_TABLE(port->config,
+                       "BONDING_MODE", &port->bonding.mode, nw_port_bonding_mode);
        if (r < 0)
                return r;
 
index 792fd28e26c8af95cc95e532fa8c72714626cd7a..8f5372eb85212d97764c6d3414f3a8a52889c6ac 100644 (file)
@@ -38,27 +38,6 @@ const nw_string_table_t nw_port_vlan_proto[] = {
 
 NW_STRING_TABLE_LOOKUP(nw_port_vlan_proto_t, nw_port_vlan_proto)
 
-static int nw_port_vlan_read_proto(nw_config* config,
-               const char* key, void* value, void* data) {
-       nw_port_vlan_proto_t* proto = (nw_port_vlan_proto_t*)value;
-
-       const char* p = nw_config_get(config, key);
-       if (p) {
-               *proto = nw_port_vlan_proto_from_string(p);
-               if (!*proto)
-                       return -errno;
-       }
-
-       return 0;
-}
-
-static int nw_port_vlan_write_proto(nw_config* config,
-               const char* key, const void* value, void* data) {
-       const nw_port_vlan_proto_t* proto = (nw_port_vlan_proto_t*)value;
-
-       return nw_config_set(config, key, nw_port_vlan_proto_to_string(*proto));
-}
-
 static int nw_port_vlan_setup(nw_port* port) {
        int r;
 
@@ -68,8 +47,8 @@ static int nw_port_vlan_setup(nw_port* port) {
                return r;
 
        // VLAN Protocol
-       r = NW_CONFIG_OPTION(port->config, "VLAN_PROTO", &port->vlan.proto,
-                       nw_port_vlan_read_proto, nw_port_vlan_write_proto, NULL);
+       r = NW_CONFIG_OPTION_STRING_TABLE(port->config,
+                       "VLAN_PROTO", &port->vlan.proto, nw_port_vlan_proto);
        if (r < 0)
                return r;