diff options
-rw-r--r-- | clients/cli/settings.c | 104 |
1 files changed, 59 insertions, 45 deletions
diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 276795b89b..94dd53717f 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -1215,28 +1215,19 @@ nmc_property_ip_get_addresses (NMSetting *setting) GString *printable; guint32 num_addresses, i; NMIPAddress *addr; - const char *gw; printable = g_string_new (NULL); - gw = nm_setting_ip_config_get_gateway (s_ip); num_addresses = nm_setting_ip_config_get_num_addresses (s_ip); for (i = 0; i < num_addresses; i++) { addr = nm_setting_ip_config_get_address (s_ip, i); if (printable->len > 0) - g_string_append (printable, "; "); - - g_string_append (printable, "{ "); + g_string_append (printable, ", "); - g_string_append_printf (printable, "ip = %s/%u", + g_string_append_printf (printable, "%s/%u", nm_ip_address_get_address (addr), nm_ip_address_get_prefix (addr)); - - if (gw) - g_string_append_printf (printable, ", gw = %s", gw); - - g_string_append (printable, " }"); } return g_string_free (printable, FALSE); @@ -3216,44 +3207,31 @@ static const char * nmc_property_ipv4_describe_addresses (NMSetting *setting, const char *prop) { return _("Enter a list of IPv4 addresses formatted as:\n" - " ip[/prefix] [gateway], ip[/prefix] [gateway],...\n" + " ip[/prefix], ip[/prefix],...\n" "Missing prefix is regarded as prefix of 32.\n\n" - "Example: 192.168.1.5/24 192.168.1.1, 10.0.0.11/24\n"); + "Example: 192.168.1.5/24, 10.0.0.11/24\n"); } -/* - * from: { ip = 1.2.3.4/24, gw = 1.2.3.254 }; { ip = 2.2.2.2/16, gw = 5.5.5.5 } - * to: 1.2.3.4/24 1.2.3.254, 2.2.2.2/16 5.5.5.5 - * from: { ip = 11::22/64, gw = 22::33 }; { ip = ab::cd/64, gw = ab::1 } - * to: 11::22/64 22:33, ab::cd/64 ab::1 -*/ -static char * -nmc_property_out2in_addresses (const char *out_format) +/* 'gateway' */ +static gboolean +nmc_property_ipv4_set_gateway (NMSetting *setting, const char *prop, const char *val, GError **error) { - GRegex *regex; - GString *str; - char **strv; - int i; + NMIPAddress *ip4addr; - str = g_string_sized_new (128); - regex = g_regex_new ("\\{ ip = ([^/]+)/([^,]+), gw = ([^ ]+) \\}", 0, 0, NULL); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - strv = g_regex_split (regex, out_format, 0); - for (i = 1; strv && strv[i] && strv[i+1] && strv[i+2]; i=i+4) { - g_string_append (str, strv[i]); /* IP */ - g_string_append_c (str, '/'); - g_string_append (str, strv[i+1]); /* prefix */ - g_string_append_c (str, ' '); - g_string_append (str, strv[i+2]); /* gateway */ - g_string_append (str, ", "); + if (strchr (val, '/')) { + g_set_error (error, 1, 0, + _("invalid gateway address '%s'"), val); + return FALSE; } - if (str->len > 0) - g_string_truncate (str, str->len - 2); - - g_strfreev (strv); - g_regex_unref (regex); + ip4addr = _parse_ipv4_address (val, error); + if (!ip4addr) + return FALSE; - return g_string_free (str, FALSE); + g_object_set (setting, prop, val, NULL); + nm_ip_address_unref (ip4addr); + return TRUE; } /* 'routes' */ @@ -3535,9 +3513,31 @@ static const char * nmc_property_ipv6_describe_addresses (NMSetting *setting, const char *prop) { return _("Enter a list of IPv6 addresses formatted as:\n" - " ip[/prefix] [gateway], ip[/prefix] [gateway],...\n" + " ip[/prefix], ip[/prefix],...\n" "Missing prefix is regarded as prefix of 128.\n\n" - "Example: 2607:f0d0:1002:51::4/64 2607:f0d0:1002:51::1, 1050:0:0:0:5:600:300c:326b\n"); + "Example: 2607:f0d0:1002:51::4/64, 1050:0:0:0:5:600:300c:326b\n"); +} + +/* 'gateway' */ +static gboolean +nmc_property_ipv6_set_gateway (NMSetting *setting, const char *prop, const char *val, GError **error) +{ + NMIPAddress *ip6addr; + + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + if (strchr (val, '/')) { + g_set_error (error, 1, 0, + _("invalid gateway address '%s'"), val); + return FALSE; + } + ip6addr = _parse_ipv6_address (val, error); + if (!ip6addr) + return FALSE; + + g_object_set (setting, prop, val, NULL); + nm_ip_address_unref (ip6addr); + return TRUE; } /* 'routes' */ @@ -5377,7 +5377,14 @@ nmc_properties_init (void) nmc_property_ipv4_remove_addresses, nmc_property_ipv4_describe_addresses, NULL, - nmc_property_out2in_addresses); + NULL); + nmc_add_prop_funcs (GLUE_IP (4, GATEWAY), + nmc_property_ipv4_get_gateway, + nmc_property_ipv4_set_gateway, + NULL, + NULL, + NULL, + NULL); nmc_add_prop_funcs (GLUE_IP (4, ROUTES), nmc_property_ipv4_get_routes, nmc_property_ipv4_set_routes, @@ -5463,7 +5470,14 @@ nmc_properties_init (void) nmc_property_ipv6_remove_addresses, nmc_property_ipv6_describe_addresses, NULL, - nmc_property_out2in_addresses); + NULL); + nmc_add_prop_funcs (GLUE_IP (6, GATEWAY), + nmc_property_ipv6_get_gateway, + nmc_property_ipv6_set_gateway, + NULL, + NULL, + NULL, + NULL); nmc_add_prop_funcs (GLUE_IP (6, ROUTES), nmc_property_ipv6_get_routes, nmc_property_ipv6_set_routes, |