]> git.ipfire.org Git - network.git/commitdiff
config: Add string buffer type
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 11:19:30 +0000 (11:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 11:19:30 +0000 (11:19 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/config.c
src/networkd/config.h
src/networkd/port-veth.c
src/networkd/port-vlan.c

index 3d444c41c19e338178de722fce438ef27f6633e1..c6281cb249021a4c1d25111b75f2d5af58d4d008 100644 (file)
@@ -43,6 +43,7 @@ struct nw_config_option {
 
        const char* key;
        void* value;
+       size_t length;
 
        // Callbacks
        nw_config_option_read_callback_t read_callback;
@@ -427,7 +428,8 @@ 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, option->data);
+               r = option->read_callback(config,
+                       option->key, option->value, option->length, option->data);
                if (r < 0)
                        return r;
        }
@@ -440,7 +442,8 @@ 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, option->data);
+               r = option->write_callback(config,
+                       option->key, option->value, option->length, option->data);
                if (r < 0)
                        return r;
        }
@@ -448,7 +451,8 @@ int nw_config_options_write(nw_config* config) {
        return 0;
 }
 
-int nw_config_option_add(nw_config* config, const char* key, void* value,
+int nw_config_option_add(nw_config* config,
+               const char* key, void* value, const size_t length,
                nw_config_option_read_callback_t read_callback,
                nw_config_option_write_callback_t write_callback, void* data) {
        // Check input
@@ -465,6 +469,7 @@ int nw_config_option_add(nw_config* config, const char* key, void* value,
 
        // Set value
        option->value = value;
+       option->length = length;
 
        // Set callbacks
        option->read_callback = read_callback;
@@ -477,7 +482,8 @@ 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, void* data) {
+int nw_config_read_int(nw_config* config,
+               const char* key, void* value, const size_t length, void* data) {
        // Fetch the value
        *(int*)value = nw_config_get_int(config, key, -1);
 
@@ -485,13 +491,14 @@ int nw_config_read_int(nw_config* config, const char* key, void* value, void* da
 }
 
 int nw_config_write_int(nw_config* config,
-               const char* key, const void* value, void* data) {
+               const char* key, const void* value, const size_t length, void* data) {
        return 0;
 }
 
 // String
 
-int nw_config_read_string(nw_config* config, const char* key, void* value, void* data) {
+int nw_config_read_string(nw_config* config,
+               const char* key, void* value, const size_t length, void* data) {
        // Fetch the value
        const char* p = nw_config_get(config, key);
        if (p)
@@ -501,13 +508,28 @@ int nw_config_read_string(nw_config* config, const char* key, void* value, void*
 }
 
 int nw_config_write_string(nw_config* config,
-               const char* key, const void* value, void* data) {
+               const char* key, const void* value, const size_t length, void* data) {
        return nw_config_set(config, key, *(const char**)value);
 }
 
+// String Buffer
+
+int nw_config_read_string_buffer(nw_config* config,
+               const char* key, void* value, const size_t length, void* data) {
+       char* string = (char*)value;
+
+       // Fetch the value
+       const char* p = nw_config_get(config, key);
+       if (p)
+               return __nw_string_set(string, length, p);
+
+       return 0;
+}
+
 // String Table
 
-int nw_config_read_string_table(nw_config* config, const char* key, void* value, void* data) {
+int nw_config_read_string_table(nw_config* config,
+               const char* key, void* value, const size_t length, void* data) {
        const char* s = NULL;
        int* v = (int*)value;
 
@@ -529,7 +551,7 @@ int nw_config_read_string_table(nw_config* config, const char* key, void* value,
 }
 
 int nw_config_write_string_table(nw_config* config,
-               const char* key, const void* value, void* data) {
+               const char* key, const void* value, const size_t length, void* data) {
        int* v = (int*)value;
 
        const nw_string_table_t* table = (nw_string_table_t*)data;
@@ -544,7 +566,8 @@ int nw_config_write_string_table(nw_config* config,
 
 // Address
 
-int nw_config_read_address(nw_config* config, const char* key, void* value, void* data) {
+int nw_config_read_address(nw_config* config,
+               const char* key, void* value, const size_t length, void* data) {
        nw_address_t* address = (nw_address_t*)value;
        int r;
 
@@ -561,7 +584,7 @@ int nw_config_read_address(nw_config* config, const char* key, void* value, void
 }
 
 int nw_config_write_address(nw_config* config,
-               const char* key, const void* value, void* data) {
+               const char* key, const void* value, const size_t length, void* data) {
        const nw_address_t* address = (nw_address_t*)value;
        int r;
 
index b25d05e6e1b7480bfff9d81c4c0b4bc9b37d49fe..4b8bc01fb9570688d9977444a8bdc2b39d46d782 100644 (file)
@@ -60,47 +60,64 @@ 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, void* data);
+       (nw_config* config, const char* key, void* value, const size_t length, void* data);
 typedef int (*nw_config_option_write_callback_t)
-       (nw_config* config, const char* key, const void* value, void* data);
+       (nw_config* config, const char* key, const void* value, const size_t length, void* data);
 
-int nw_config_option_add(nw_config* config, const char* key, void* value,
+int nw_config_option_add(nw_config* config, const char* key, void* value, const size_t length,
        nw_config_option_read_callback_t read_callback,
        nw_config_option_write_callback_t write_callback, void* data);
 
-#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(config, key, value, length, read_callback, write_callback, data) \
+       nw_config_option_add(config, key, value, length, 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);
+int nw_config_read_string(nw_config* config,
+       const char* key, void* value, const size_t length, void* data);
+int nw_config_write_string(nw_config* config,
+       const char* key, const void* value, const size_t length, void* data);
+
+#define NW_CONFIG_OPTION_STRING_BUFFER(config, key, value) \
+       nw_config_option_add(config, key, value, sizeof(value), \
+               nw_config_read_string_buffer, nw_config_write_string_buffer, NULL)
+
+int nw_config_read_string_buffer(nw_config* config,
+       const char* key, void* value, const size_t length, void* data);
+#define nw_config_write_string_buffer nw_config_write_string
 
 // 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)
+       nw_config_option_add(config, key, value, 0, \
+               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);
+int nw_config_read_string_table(nw_config* config,
+       const char* key, void* value, const size_t length, void* data);
+int nw_config_write_string_table(nw_config* config,
+       const char* key, const void* value, const size_t length, 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)
+       nw_config_option_add(config, key, value, 0, 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);
+int nw_config_read_int(nw_config* config,
+       const char* key, void* value, const size_t length, void* data);
+int nw_config_write_int(nw_config* config,
+       const char* key, const void* value, const size_t length, 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)
+       nw_config_option_add(config, key, value, 0, nw_config_read_address, nw_config_write_address, NULL)
 
-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);
+int nw_config_read_address(nw_config* config,
+       const char* key, void* value, const size_t length, void* data);
+int nw_config_write_address(nw_config* config,
+       const char* key, const void* value, const size_t length, void* data);
 
 #endif /* NETWORKD_CONFIG_H */
index 44b1a0d4c4399a3e58a40840739fc32788caa8a2..029ef50c51230b7d47e5bd536abc9816647194b2 100644 (file)
@@ -28,7 +28,7 @@ static int nw_port_veth_setup(nw_port* port) {
        int r;
 
        // Peer
-       r = NW_CONFIG_OPTION_STRING(port->config, "VETH_PEER", &port->veth.peer);
+       r = NW_CONFIG_OPTION_STRING_BUFFER(port->config, "VETH_PEER", port->veth.peer);
        if (r < 0)
                return 1;
 
index 25a59eed28ca50f821ca8245441b80dd24f32e03..c759f71920f14b866d056d1cee84cdaf5f1ecfd3 100644 (file)
@@ -53,7 +53,8 @@ static int nw_port_vlan_setup(nw_port* port) {
                return r;
 
        // Parent Port
-       r = NW_CONFIG_OPTION_STRING(port->config, "VLAN_PARENT", &port->vlan.__parent_name);
+       r = NW_CONFIG_OPTION_STRING_BUFFER(port->config,
+               "VLAN_PARENT", port->vlan.__parent_name);
        if (r < 0)
                return r;