]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/nc-config.c
ui/ncurses: Populate configuration interface with current defaults
[petitboot] / ui / ncurses / nc-config.c
index 0b2a8d7b06738f60185b70c1b94615b1a460ad81..47e6b72df4379964a7752108ca46e0ff373ce12d 100644 (file)
 
 #define _GNU_SOURCE
 
-
+#include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 
+#include <pb-config/pb-config.h>
 #include <talloc/talloc.h>
 #include <types/types.h>
 #include <log/log.h>
@@ -48,6 +50,8 @@ struct config_screen {
        int                     field_x;
        int                     network_config_y;
 
+       enum net_conf_type      net_conf_type;
+
        struct {
                struct nc_widget_checkbox       *autoboot_f;
                struct nc_widget_label          *autoboot_l;
@@ -123,11 +127,111 @@ struct nc_scr *config_screen_scr(struct config_screen *screen)
        return &screen->scr;
 }
 
+static int screen_process_form(struct config_screen *screen)
+{
+       const struct system_info *sysinfo = screen->cui->sysinfo;
+       struct config *config = talloc_zero(screen, struct config);
+       enum net_conf_type net_conf_type;
+       struct interface_config *iface;
+       char *str, *end;
+       int rc;
+
+       config_set_defaults(config);
+
+       config->autoboot_enabled =
+               widget_checkbox_get_value(screen->widgets.autoboot_f);
+
+
+       str = widget_textbox_get_value(screen->widgets.timeout_f);
+       if (str) {
+               unsigned long x;
+               errno = 0;
+               x = strtoul(str, &end, 10);
+               if (!errno && end != str)
+                       config->autoboot_timeout_sec = x;
+       }
+
+       net_conf_type = widget_select_get_value(screen->widgets.network_f);
+
+       /* if we don't have any network interfaces, prevent per-interface
+        * configuration */
+       if (sysinfo->n_interfaces == 0)
+               net_conf_type = NET_CONF_TYPE_DHCP_ALL;
+
+       if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
+               config->network.n_interfaces = 0;
+
+       } else {
+               int idx;
+
+               iface = talloc_zero(config, struct interface_config);
+               config->network.n_interfaces = 1;
+               config->network.interfaces = talloc_array(config,
+                               struct interface_config *, 1);
+               config->network.interfaces[0] = iface;
+
+               /* copy hwaddr (from the sysinfo interface data) to
+                * the configuration */
+               idx = widget_select_get_value(screen->widgets.iface_f);
+               memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
+                               sizeof(iface->hwaddr));
+       }
+
+       if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
+               iface->method = CONFIG_METHOD_DHCP;
+       }
+
+       if (net_conf_type == NET_CONF_TYPE_STATIC) {
+               iface->method = CONFIG_METHOD_STATIC;
+               iface->static_config.address = talloc_asprintf(iface, "%s/%s",
+                               widget_textbox_get_value(
+                                       screen->widgets.ip_addr_f),
+                               widget_textbox_get_value(
+                                       screen->widgets.ip_mask_f));
+               iface->static_config.gateway = talloc_strdup(iface,
+                               widget_textbox_get_value(
+                                       screen->widgets.gateway_f));
+       }
+
+       str = widget_textbox_get_value(screen->widgets.dns_f);
+       if (str && strlen(str)) {
+               char *dns, *tmp;
+               int i;
+
+               for (;;) {
+                       dns = strtok_r(str, " \t", &tmp);
+
+                       if (!dns)
+                               break;
+
+                       i = config->network.n_dns_servers++;
+                       config->network.dns_servers = talloc_realloc(config,
+                                       config->network.dns_servers,
+                                       const char *,
+                                       config->network.n_dns_servers);
+                       config->network.dns_servers[i] =
+                               talloc_strdup(config, dns);
+
+                       str = NULL;
+               }
+       }
+
+       rc = cui_send_config(screen->cui, config);
+       talloc_free(config);
+
+       if (rc)
+               pb_log("cui_send_config failed!\n");
+       else
+               pb_debug("config sent!\n");
+
+       return 0;
+}
+
 static void ok_click(void *arg)
 {
        struct config_screen *screen = arg;
-       /* todo: save config */
-       screen->on_exit(screen->cui);
+       screen_process_form(screen);
+       screen->exit = true;
 }
 
 static void cancel_click(void *arg)
@@ -210,15 +314,12 @@ static void config_screen_layout_widgets(struct config_screen *screen,
        widget_set_visible(wf, show);
 
        if (show)
-               y += layout_pair(screen, y, screen->widgets.gateway_l, wf);
+               y += layout_pair(screen, y, screen->widgets.gateway_l, wf) + 1;
 
-       wl = widget_label_base(screen->widgets.dns_l);
-       wf = widget_textbox_base(screen->widgets.dns_f);
-       widget_set_visible(wl, show);
-       widget_set_visible(wf, show);
+       y += layout_pair(screen, y, screen->widgets.dns_l,
+                       widget_textbox_base(screen->widgets.dns_f));
 
-       if (show)
-               y += 1 + layout_pair(screen, y, screen->widgets.dns_l, wf);
+       y += 1;
 
        widget_move(widget_button_base(screen->widgets.ok_b),
                        y, screen->field_x);
@@ -229,22 +330,60 @@ static void config_screen_layout_widgets(struct config_screen *screen,
 static void config_screen_network_change(void *arg, int value)
 {
        struct config_screen *screen = arg;
+       screen->net_conf_type = value;
        widgetset_unpost(screen->widgetset);
        config_screen_layout_widgets(screen, value);
        widgetset_post(screen->widgetset);
 }
 
+static struct interface_config *first_active_interface(
+               const struct config *config)
+{
+       unsigned int i;
+
+       for (i = 0; i < config->network.n_interfaces; i++) {
+               if (config->network.interfaces[i]->ignore)
+                       continue;
+               return config->network.interfaces[i];
+       }
+       return NULL;
+}
+
+static enum net_conf_type find_net_conf_type(const struct config *config)
+{
+       struct interface_config *ifcfg;
+
+       ifcfg = first_active_interface(config);
+
+       if (!ifcfg)
+               return NET_CONF_TYPE_DHCP_ALL;
+
+       else if (ifcfg->method == CONFIG_METHOD_DHCP)
+               return NET_CONF_TYPE_DHCP_ONE;
+
+       else if (ifcfg->method == CONFIG_METHOD_STATIC)
+               return NET_CONF_TYPE_STATIC;
+
+       assert(0);
+       return NET_CONF_TYPE_DHCP_ALL;
+}
+
 static void config_screen_setup_widgets(struct config_screen *screen,
                const struct config *config,
                const struct system_info *sysinfo)
 {
        struct nc_widgetset *set = screen->widgetset;
+       struct interface_config *ifcfg;
+       char *str, *ip, *mask, *gw;
+       enum net_conf_type type;
        unsigned int i;
-       char *str;
 
        build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
                        == N_FIELDS);
 
+       type = screen->net_conf_type;
+       ifcfg = first_active_interface(config);
+
        screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
        screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0,
                                        config->autoboot_enabled);
@@ -259,15 +398,15 @@ static void config_screen_setup_widgets(struct config_screen *screen,
        widget_select_add_option(screen->widgets.network_f,
                                        NET_CONF_TYPE_DHCP_ALL,
                                        "DHCP on all active interfaces",
-                                       true);
+                                       type == NET_CONF_TYPE_DHCP_ALL);
        widget_select_add_option(screen->widgets.network_f,
                                        NET_CONF_TYPE_DHCP_ONE,
                                        "DHCP on a specific interface",
-                                       false);
+                                       type == NET_CONF_TYPE_DHCP_ONE);
        widget_select_add_option(screen->widgets.network_f,
                                        NET_CONF_TYPE_STATIC,
                                        "Static IP configuration",
-                                       false);
+                                       type == NET_CONF_TYPE_STATIC);
 
        widget_select_on_change(screen->widgets.network_f,
                        config_screen_network_change, screen);
@@ -277,20 +416,47 @@ static void config_screen_setup_widgets(struct config_screen *screen,
 
        for (i = 0; i < sysinfo->n_interfaces; i++) {
                struct interface_info *info = sysinfo->interfaces[i];
+               bool is_default;
+
+               is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
+                                       sizeof(ifcfg->hwaddr));
+
                widget_select_add_option(screen->widgets.iface_f,
-                                               i, info->name, false);
+                                               i, info->name, is_default);
+       }
+
+       gw = ip = mask = NULL;
+       if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
+               char *sep;
+
+               str = talloc_strdup(screen, ifcfg->static_config.address);
+               sep = strchr(str, '/');
+               ip = str;
+
+               if (sep) {
+                       *sep = '\0';
+                       mask = sep + 1;
+               }
+               gw = ifcfg->static_config.gateway;
        }
 
        screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, "IP/mask:");
-       screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, "");
+       screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
        screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
-       screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, "");
+       screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
 
        screen->widgets.gateway_l = widget_new_label(set, 0, 0, "Gateway:");
-       screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, "");
+       screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
 
-       screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server:");
-       screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 16, "");
+       str = talloc_strdup(screen, "");
+       for (i = 0; i < config->network.n_dns_servers; i++) {
+               str = talloc_asprintf_append(str, "%s%s",
+                               (i == 0) ? "" : " ",
+                               config->network.dns_servers[i]);
+       }
+
+       screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server(s):");
+       screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
 
        screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
                        ok_click, screen);
@@ -314,7 +480,7 @@ struct config_screen *config_screen_init(struct cui *cui,
        screen->cui = cui;
        screen->on_exit = on_exit;
        screen->label_x = 2;
-       screen->field_x = 16;
+       screen->field_x = 17;
 
        screen->scr.frame.ltitle = talloc_strdup(screen,
                        "Petitboot System Configuration");
@@ -325,8 +491,10 @@ struct config_screen *config_screen_init(struct cui *cui,
 
        screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
                        screen->scr.sub_ncw);
+       screen->net_conf_type = find_net_conf_type(config);
+
        config_screen_setup_widgets(screen, config, sysinfo);
-       config_screen_layout_widgets(screen, NET_CONF_TYPE_DHCP_ALL);
+       config_screen_layout_widgets(screen, screen->net_conf_type);
 
        wrefresh(screen->scr.main_ncw);
        scrollok(screen->scr.sub_ncw, true);