From 2b41985dde73e71daad90c36fa5d06c199da3ae1 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Thu, 8 Aug 2013 13:54:19 +0800 Subject: [PATCH] config: Split interface configuration from network configuration This change moves the interface configuration into its own 'struct interface_config'. We also remove the _config suffix from the network and interface members. Signed-off-by: Jeremy Kerr --- discover/network.c | 14 +++--- lib/pb-config/pb-config.c | 26 +++++----- lib/pb-config/pb-config.h | 10 ++-- lib/pb-config/storage-powerpc-nvram.c | 68 +++++++++++++-------------- lib/pb-config/storage-test.c | 12 +++-- 5 files changed, 66 insertions(+), 64 deletions(-) diff --git a/discover/network.c b/discover/network.c index 23bf1ee..910649e 100644 --- a/discover/network.c +++ b/discover/network.c @@ -53,7 +53,7 @@ struct network { bool dry_run; }; -static const struct network_config *find_config_by_hwaddr( +static const struct interface_config *find_config_by_hwaddr( uint8_t *hwaddr) { const struct config *config; @@ -63,11 +63,11 @@ static const struct network_config *find_config_by_hwaddr( if (!config) return NULL; - for (i = 0; i < config->n_network_configs; i++) { - struct network_config *netconf = config->network_configs[i]; + for (i = 0; i < config->network.n_interfaces; i++) { + struct interface_config *ifconf = config->network.interfaces[i]; - if (!memcmp(netconf->hwaddr, hwaddr, HWADDR_SIZE)) - return netconf; + if (!memcmp(ifconf->hwaddr, hwaddr, HWADDR_SIZE)) + return ifconf; } return NULL; @@ -191,7 +191,7 @@ static void configure_interface_dhcp(struct network *network, static void configure_interface_static(struct network *network, struct interface *interface, - const struct network_config *config) + const struct interface_config *config) { const char *addr_argv[] = { pb_system_apps.ip, @@ -242,7 +242,7 @@ static void configure_interface_static(struct network *network, static void configure_interface(struct network *network, struct interface *interface, bool up, bool link) { - const struct network_config *config = NULL; + const struct interface_config *config = NULL; if (interface->state == IFSTATE_IGNORED) return; diff --git a/lib/pb-config/pb-config.c b/lib/pb-config/pb-config.c index 6398a69..5cd303b 100644 --- a/lib/pb-config/pb-config.c +++ b/lib/pb-config/pb-config.c @@ -13,8 +13,8 @@ static struct config_storage *storage; static void config_set_defaults(struct config *config) { config->autoboot_enabled = true; - config->network_configs = NULL; - config->n_network_configs = 0; + config->network.interfaces = NULL; + config->network.n_interfaces = 0; } static void dump_config(struct config *config) @@ -29,27 +29,27 @@ static void dump_config(struct config *config) if (config->n_network_configs > 0) pb_log(" network configuration:\n"); - for (i = 0; i < config->n_network_configs; i++) { - struct network_config *netconf = config->network_configs[i]; + for (i = 0; i < config->network.n_interfaces; i++) { + struct interface_config *ifconf = + config->network.interfaces[i]; pb_log(" interface %02x:%02x:%02x:%02x:%02x:%02x\n", - netconf->hwaddr[0], netconf->hwaddr[1], - netconf->hwaddr[2], netconf->hwaddr[3], - netconf->hwaddr[4], netconf->hwaddr[5]); + ifconf->hwaddr[0], ifconf->hwaddr[1], + ifconf->hwaddr[2], ifconf->hwaddr[3], + ifconf->hwaddr[4], ifconf->hwaddr[5]); - if (netconf->ignore) { + if (ifconf->ignore) { pb_log(" ignore\n"); continue; } - if (netconf->method == CONFIG_METHOD_DHCP) { + if (ifconf->method == CONFIG_METHOD_DHCP) { pb_log(" dhcp\n"); - } else if (netconf->method == CONFIG_METHOD_STATIC) { + } else if (ifconf->method == CONFIG_METHOD_STATIC) { pb_log(" static:\n"); - pb_log(" ip: %s\n", netconf->static_config.address); - pb_log(" gw: %s\n", netconf->static_config.gateway); - pb_log(" dns: %s\n", netconf->static_config.dns); + pb_log(" ip: %s\n", ifconf->static_config.address); + pb_log(" gw: %s\n", ifconf->static_config.gateway); } } diff --git a/lib/pb-config/pb-config.h b/lib/pb-config/pb-config.h index c377087..748b409 100644 --- a/lib/pb-config/pb-config.h +++ b/lib/pb-config/pb-config.h @@ -6,7 +6,7 @@ #define HWADDR_SIZE 6 -struct network_config { +struct interface_config { uint8_t hwaddr[HWADDR_SIZE]; bool ignore; enum { @@ -24,10 +24,14 @@ struct network_config { }; }; +struct network_config { + struct interface_config **interfaces; + int n_interfaces; +}; + struct config { bool autoboot_enabled; - struct network_config **network_configs; - int n_network_configs; + struct network_config network; }; diff --git a/lib/pb-config/storage-powerpc-nvram.c b/lib/pb-config/storage-powerpc-nvram.c index 4b16479..e6bf7df 100644 --- a/lib/pb-config/storage-powerpc-nvram.c +++ b/lib/pb-config/storage-powerpc-nvram.c @@ -188,7 +188,7 @@ static const char *get_param(struct powerpc_nvram_storage *nv, return NULL; } -static int parse_hwaddr(struct network_config *config, char *str) +static int parse_hwaddr(struct interface_config *ifconf, char *str) { int i; @@ -207,65 +207,76 @@ static int parse_hwaddr(struct network_config *config, char *str) if (endp != byte + 2) return -1; - config->hwaddr[i] = x & 0xff; + ifconf->hwaddr[i] = x & 0xff; } return 0; } -static int parse_one_network_config(struct network_config *config, +static int parse_one_interface_config(struct config *config, char *confstr) { + struct interface_config *ifconf; char *tok, *saveptr; + ifconf = talloc(config, struct interface_config); + if (!confstr || !strlen(confstr)) - return -1; + goto out_err; /* first token should be the mac address */ tok = strtok_r(confstr, ",", &saveptr); if (!tok) - return -1; + goto out_err; - if (parse_hwaddr(config, tok)) - return -1; + if (parse_hwaddr(ifconf, tok)) + goto out_err; /* second token is the method */ tok = strtok_r(NULL, ",", &saveptr); if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) { - config->ignore = true; - return 0; - } + ifconf->ignore = true; - if (!strcmp(tok, "dhcp")) { - config->method = CONFIG_METHOD_DHCP; + } else if (!strcmp(tok, "dhcp")) { + ifconf->method = CONFIG_METHOD_DHCP; } else if (!strcmp(tok, "static")) { - config->method = CONFIG_METHOD_STATIC; + ifconf->method = CONFIG_METHOD_STATIC; /* ip/mask, [optional] gateway, [optional] dns */ tok = strtok_r(NULL, ",", &saveptr); if (!tok) - return -1; - config->static_config.address = - talloc_strdup(config, tok); + goto out_err; + ifconf->static_config.address = + talloc_strdup(ifconf, tok); tok = strtok_r(NULL, ",", &saveptr); if (tok) { - config->static_config.gateway = - talloc_strdup(config, tok); + ifconf->static_config.gateway = + talloc_strdup(ifconf, tok); tok = strtok_r(NULL, ",", &saveptr); } if (tok) { - config->static_config.dns = + ifconf->static_config.dns = talloc_strdup(config, tok); } } else { pb_log("Unknown network configuration method %s\n", tok); - return -1; + goto out_err; } + config->network.interfaces = talloc_realloc(config, + config->network.interfaces, + struct interface_config *, + ++config->network.n_interfaces); + + config->network.interfaces[config->network.n_interfaces - 1] = ifconf; + return 0; +out_err: + talloc_free(ifconf); + return -1; } static void populate_network_config(struct powerpc_nvram_storage *nv, @@ -282,29 +293,14 @@ static void populate_network_config(struct powerpc_nvram_storage *nv, val = talloc_strdup(config, cval); for (i = 0; ; i++) { - struct network_config *netconf; char *tok, *saveptr; - int rc; tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr); if (!tok) break; - netconf = talloc(nv, struct network_config); - - rc = parse_one_network_config(netconf, tok); - if (rc) { - talloc_free(netconf); - continue; - } - - config->network_configs = talloc_realloc(nv, - config->network_configs, - struct network_config *, - ++config->n_network_configs); + parse_one_interface_config(config, tok); - config->network_configs[config->n_network_configs - 1] = - netconf; } talloc_free(val); diff --git a/lib/pb-config/storage-test.c b/lib/pb-config/storage-test.c index ba4e952..bdb1d0d 100644 --- a/lib/pb-config/storage-test.c +++ b/lib/pb-config/storage-test.c @@ -7,12 +7,12 @@ #include "pb-config.h" #include "storage.h" -struct network_config net1 = { +struct interface_config net1 = { .hwaddr = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }, .method = CONFIG_METHOD_DHCP, }; -struct network_config net2 = { +struct interface_config net2 = { .hwaddr = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x56 }, .method = CONFIG_METHOD_STATIC, .static_config = { @@ -21,14 +21,16 @@ struct network_config net2 = { }, }; -struct network_config *network_configs[] = { &net1, &net2 }; +struct interface_config *interface_configs[] = { &net1, &net2 }; #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) struct config test_config = { .autoboot_enabled = true, - .network_configs = network_configs, - .n_network_configs = ARRAY_SIZE(network_configs), + .network = { + .interfaces = interface_configs, + .n_interfaces = ARRAY_SIZE(interface_configs), + } }; struct test_storage { -- 2.39.2