From e633147dcf3bc46e0686e106cb013dfccf30b1c6 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 9 Jun 2023 11:19:30 +0000 Subject: [PATCH] config: Add string buffer type Signed-off-by: Michael Tremer --- src/networkd/config.c | 45 +++++++++++++++++++++++++++--------- src/networkd/config.h | 49 +++++++++++++++++++++++++++------------- src/networkd/port-veth.c | 2 +- src/networkd/port-vlan.c | 3 ++- 4 files changed, 70 insertions(+), 29 deletions(-) diff --git a/src/networkd/config.c b/src/networkd/config.c index 3d444c41..c6281cb2 100644 --- a/src/networkd/config.c +++ b/src/networkd/config.c @@ -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; diff --git a/src/networkd/config.h b/src/networkd/config.h index b25d05e6..4b8bc01f 100644 --- a/src/networkd/config.h +++ b/src/networkd/config.h @@ -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 */ diff --git a/src/networkd/port-veth.c b/src/networkd/port-veth.c index 44b1a0d4..029ef50c 100644 --- a/src/networkd/port-veth.c +++ b/src/networkd/port-veth.c @@ -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; diff --git a/src/networkd/port-vlan.c b/src/networkd/port-vlan.c index 25a59eed..c759f719 100644 --- a/src/networkd/port-vlan.c +++ b/src/networkd/port-vlan.c @@ -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; -- 2.39.2