]> git.ipfire.org Git - people/ms/network.git/commitdiff
config: Add data pointer to callbacks
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 8 Jun 2023 15:23:10 +0000 (15:23 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 8 Jun 2023 15:23:10 +0000 (15:23 +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 b142d17d452e1eef405f18415443152351d8068e..39e98818dcf740db9b14ad0a23773931297ba60e 100644 (file)
@@ -47,6 +47,7 @@ struct nw_config_option {
        // Callbacks
        nw_config_option_read_callback_t read_callback;
        nw_config_option_write_callback_t write_callback;
+       void* data;
 };
 
 struct nw_config {
@@ -491,7 +492,7 @@ int nw_config_options_read(nw_config* config) {
        int r;
 
        STAILQ_FOREACH(option, &config->options, nodes) {
-               r = option->read_callback(config, option->key, option->value);
+               r = option->read_callback(config, option->key, option->value, option->data);
                if (r < 0)
                        return r;
        }
@@ -504,7 +505,7 @@ int nw_config_options_write(nw_config* config) {
        int r;
 
        STAILQ_FOREACH(option, &config->options, nodes) {
-               r = option->write_callback(config, option->key, option->value);
+               r = option->write_callback(config, option->key, option->value, option->data);
                if (r < 0)
                        return r;
        }
@@ -514,7 +515,7 @@ int nw_config_options_write(nw_config* config) {
 
 int nw_config_option_add(nw_config* config, const char* key, void* value,
                nw_config_option_read_callback_t read_callback,
-               nw_config_option_write_callback_t write_callback) {
+               nw_config_option_write_callback_t write_callback, void* data) {
        // Check input
        if (!key || !value || !read_callback || !write_callback)
                return -EINVAL;
@@ -533,6 +534,7 @@ int nw_config_option_add(nw_config* config, const char* key, void* value,
        // Set callbacks
        option->read_callback = read_callback;
        option->write_callback = write_callback;
+       option->data = data;
 
        // Append the new option
        STAILQ_INSERT_TAIL(&config->options, option, nodes);
@@ -540,20 +542,21 @@ int nw_config_option_add(nw_config* config, const char* key, void* value,
        return 0;
 }
 
-int nw_config_read_int(nw_config* config, const char* key, void* value) {
+int nw_config_read_int(nw_config* config, const char* key, void* value, void* data) {
        // Fetch the value
        *(int*)value = nw_config_get_int(config, key, -1);
 
        return 0;
 }
 
-int nw_config_write_int(nw_config* config, const char* key, const void* value) {
+int nw_config_write_int(nw_config* config,
+               const char* key, const void* value, void* data) {
        return 0;
 }
 
 // String
 
-int nw_config_read_string(nw_config* config, const char* key, void* value) {
+int nw_config_read_string(nw_config* config, const char* key, void* value, void* data) {
        // Fetch the value
        const char* p = nw_config_get(config, key);
        if (p)
@@ -562,13 +565,14 @@ int nw_config_read_string(nw_config* config, const char* key, void* value) {
        return 0;
 }
 
-int nw_config_write_string(nw_config* config, const char* key, const void* value) {
+int nw_config_write_string(nw_config* config,
+               const char* key, const void* value, void* data) {
        return nw_config_set(config, key, *(const char**)value);
 }
 
 // Address
 
-int nw_config_read_address(nw_config* config, const char* key, void* value) {
+int nw_config_read_address(nw_config* config, const char* key, void* value, void* data) {
        nw_address_t* address = (nw_address_t*)value;
        int r;
 
@@ -584,7 +588,8 @@ int nw_config_read_address(nw_config* config, const char* key, void* value) {
        return r;
 }
 
-int nw_config_write_address(nw_config* config, const char* key, const void* value) {
+int nw_config_write_address(nw_config* config,
+               const char* key, const void* value, void* data) {
        const nw_address_t* address = (nw_address_t*)value;
        int r;
 
index 71850387cf17343d90674ad635e2bc0b05cb87aa..d6b8db8b05a0ee7e9e282b96b8b5cd7b76a4b482 100644 (file)
@@ -62,33 +62,33 @@ int nw_config_options_read(nw_config* config);
 int nw_config_options_write(nw_config* config);
 
 typedef int (*nw_config_option_read_callback_t)
-       (nw_config* config, const char* key, void* value);
+       (nw_config* config, const char* key, void* value, void* data);
 typedef int (*nw_config_option_write_callback_t)
-       (nw_config* config, const char* key, const void* value);
+       (nw_config* config, const char* key, const void* value, void* data);
 
 int nw_config_option_add(nw_config* config, const char* key, void* value,
        nw_config_option_read_callback_t read_callback,
-       nw_config_option_write_callback_t write_callback);
+       nw_config_option_write_callback_t write_callback, void* data);
 
-#define NW_CONFIG_OPTION(config, key, data, read_callback, write_callback) \
-       nw_config_option_add(config, key, data, read_callback, write_callback)
+#define NW_CONFIG_OPTION(config, key, value, read_callback, write_callback, data) \
+       nw_config_option_add(config, key, value, read_callback, write_callback, data)
 
 #define NW_CONFIG_OPTION_STRING(config, key, value) \
-       nw_config_option_add(config, key, value, nw_config_read_string, nw_config_write_string)
+       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);
-int nw_config_write_string(nw_config* config, const char* key, const void* value);
+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);
 
 #define NW_CONFIG_OPTION_INT(config, key, value) \
-       nw_config_option_add(config, key, value, nw_config_read_int, nw_config_write_int)
+       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);
-int nw_config_write_int(nw_config* config, const char* key, const void* value);
+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);
 
 #define NW_CONFIG_OPTION_ADDRESS(config, key, value) \
-       nw_config_option_add(config, key, value, nw_config_read_address, nw_config_write_address)
+       nw_config_option_add(config, key, value, nw_config_read_address, nw_config_write_address, NULL)
 
-int nw_config_read_address(nw_config* config, const char* key, void* value);
-int nw_config_write_address(nw_config* config, const char* key, const void* value);
+int nw_config_read_address(nw_config* config, const char* key, void* value, void* data);
+int nw_config_write_address(nw_config* config, const char* key, const void* value, void* data);
 
 #endif /* NETWORKD_CONFIG_H */
index e7657919844c66db3c01e93d400ab44622a285e2..7bd54e138adb902748869ad00fe49b0832acaa85 100644 (file)
@@ -40,12 +40,13 @@ const struct nw_string_table 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* data) {
-       int* mode = (int*)data;
+static int nw_port_bonding_read_mode(nw_config* config,
+               const char* key, void* value, void* data) {
+       int* mode = (int*)value;
 
-       const char* value = nw_config_get(config, key);
-       if (value) {
-               *mode = nw_port_bonding_mode_from_string(value);
+       const char* p = nw_config_get(config, key);
+       if (p) {
+               *mode = nw_port_bonding_mode_from_string(p);
                if (!*mode)
                        return -errno;
        }
@@ -53,8 +54,9 @@ static int nw_port_bonding_read_mode(nw_config* config, const char* key, void* d
        return 0;
 }
 
-static int nw_port_bonding_write_mode(nw_config* config, const char* key, const void* data) {
-       const int* mode = (int*)data;
+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));
 }
@@ -64,7 +66,7 @@ static int nw_port_bonding_setup(nw_port* port) {
 
        // Mode
        r = NW_CONFIG_OPTION(port->config, "BONDING_MODE", &port->bonding.mode,
-                       nw_port_bonding_read_mode, nw_port_bonding_write_mode);
+                       nw_port_bonding_read_mode, nw_port_bonding_write_mode, NULL);
        if (r < 0)
                return r;
 
index 2d89a09c621388b1aa1f698ca0085ab0d3010d10..e3f93d77617e8e082ebd4e9f9c1cad66a7684da3 100644 (file)
@@ -38,12 +38,13 @@ const struct nw_string_table 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* data) {
-       nw_port_vlan_proto_t* proto = (nw_port_vlan_proto_t*)data;
+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* value = nw_config_get(config, key);
-       if (value) {
-               *proto = nw_port_vlan_proto_from_string(data);
+       const char* p = nw_config_get(config, key);
+       if (p) {
+               *proto = nw_port_vlan_proto_from_string(p);
                if (!*proto)
                        return -errno;
        }
@@ -51,8 +52,9 @@ static int nw_port_vlan_read_proto(nw_config* config, const char* key, void* dat
        return 0;
 }
 
-static int nw_port_vlan_write_proto(nw_config* config, const char* key, const void* data) {
-       const nw_port_vlan_proto_t* proto = (nw_port_vlan_proto_t*)data;
+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));
 }
@@ -67,7 +69,7 @@ static int nw_port_vlan_setup(nw_port* port) {
 
        // VLAN Protocol
        r = NW_CONFIG_OPTION(port->config, "VLAN_PROTO", &port->vlan.proto,
-                       nw_port_vlan_read_proto, nw_port_vlan_write_proto);
+                       nw_port_vlan_read_proto, nw_port_vlan_write_proto, NULL);
        if (r < 0)
                return r;