From 5a98837b23762bf35c788586c950610dfc7f9ce8 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 20 May 2017 22:14:26 +0200 Subject: [PATCH 01/25] clients/trivial: whitespace --- clients/common/nm-meta-setting-desc.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 408fc99178..b72a70572e 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -6141,7 +6141,7 @@ static const NMMetaPropertyInfo *const property_infos_MACSEC[] = { .values_fcn = _values_fcn_gobject_enum, ), .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, - .get_gtype = nm_setting_macsec_mode_get_type, + .get_gtype = nm_setting_macsec_mode_get_type, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_MACSEC_ENCRYPT, @@ -6179,7 +6179,7 @@ static const NMMetaPropertyInfo *const property_infos_MACSEC[] = { .values_fcn = _values_fcn_gobject_enum, ), .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, - .get_gtype = nm_setting_macsec_validation_get_type, + .get_gtype = nm_setting_macsec_validation_get_type, ), ), NULL @@ -6206,9 +6206,9 @@ static const NMMetaPropertyInfo *const property_infos_MACVLAN[] = { .values_fcn = _values_fcn_gobject_enum, ), .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, - .get_gtype = nm_setting_macvlan_mode_get_type, - .min = NM_SETTING_MACVLAN_MODE_UNKNOWN + 1, - .max = G_MAXINT, + .get_gtype = nm_setting_macvlan_mode_get_type, + .min = NM_SETTING_MACVLAN_MODE_UNKNOWN + 1, + .max = G_MAXINT, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_MACVLAN_PROMISCUOUS, @@ -6360,9 +6360,9 @@ static const NMMetaPropertyInfo *const property_infos_PROXY[] = { .values_fcn = _values_fcn_gobject_enum, ), .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, - .get_gtype = nm_setting_proxy_method_get_type, - .min = NM_SETTING_PROXY_METHOD_NONE, - .max = G_MAXINT, + .get_gtype = nm_setting_proxy_method_get_type, + .min = NM_SETTING_PROXY_METHOD_NONE, + .max = G_MAXINT, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_PROXY_BROWSER_ONLY, From 3341e89e7cd8030fadb9d8634efcecf3ce8862ee Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 20 May 2017 21:45:45 +0200 Subject: [PATCH 02/25] clients: implement macvlan.mode property as enum --- clients/common/nm-meta-setting-desc.c | 58 +-------------------------- 1 file changed, 1 insertion(+), 57 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index b72a70572e..82c7e0bc42 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -3808,58 +3808,6 @@ _set_fcn_macsec_validation (ARGS_SET_FCN) return TRUE; } -static gconstpointer -_get_fcn_macvlan_mode (ARGS_GET_FCN) -{ - NMSettingMacvlan *s_macvlan = NM_SETTING_MACVLAN (setting); - NMSettingMacvlanMode mode; - char *tmp, *str; - - RETURN_UNSUPPORTED_GET_TYPE (); - - mode = nm_setting_macvlan_get_mode (s_macvlan); - tmp = nm_utils_enum_to_str (nm_setting_macvlan_mode_get_type (), mode); - - if (get_type != NM_META_ACCESSOR_GET_TYPE_PRETTY) - str = tmp ?: g_strdup (""); - else { - str = g_strdup_printf ("%d (%s)", mode, tmp ?: ""); - g_free (tmp); - } - - RETURN_STR_TO_FREE (str); -} - -static gboolean -_set_fcn_macvlan_mode (ARGS_SET_FCN) -{ - NMSettingMacvlanMode mode; - gs_free const char **options = NULL; - gs_free char *options_str = NULL; - long int t; - gboolean ret; - - if (nmc_string_to_int_base (value, 0, TRUE, 0, _NM_SETTING_MACVLAN_MODE_NUM - 1, &t)) - mode = (NMSettingMacvlanMode) t; - else { - ret = nm_utils_enum_from_str (nm_setting_macvlan_mode_get_type (), value, - (int *) &mode, NULL); - - if (!ret) { - options = nm_utils_enum_get_values (nm_setting_macvlan_mode_get_type(), - NM_SETTING_MACVLAN_MODE_UNKNOWN + 1, - G_MAXINT); - options_str = g_strjoinv (",", (char **) options); - g_set_error (error, 1, 0, _("invalid option '%s', use one of [%s]"), - value, options_str); - return FALSE; - } - } - - g_object_set (setting, property_info->property_name, (guint) mode, NULL); - return TRUE; -} - static gconstpointer _get_fcn_olpc_mesh_ssid (ARGS_GET_FCN) { @@ -6200,11 +6148,7 @@ static const NMMetaPropertyInfo *const property_infos_MACVLAN[] = { .property_alias = "mode", .inf_flags = NM_META_PROPERTY_INF_FLAG_REQD, .prompt = NM_META_TEXT_PROMPT_MACVLAN_MODE, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_macvlan_mode, - .set_fcn = _set_fcn_macvlan_mode, - .values_fcn = _values_fcn_gobject_enum, - ), + .property_type = &_pt_gobject_enum, .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, .get_gtype = nm_setting_macvlan_mode_get_type, .min = NM_SETTING_MACVLAN_MODE_UNKNOWN + 1, From d48998b4e80f3664e99669c4b36dc279bbd807d2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 20 May 2017 22:15:20 +0200 Subject: [PATCH 03/25] clients: implement tun.mode property as enum --- clients/common/nm-meta-setting-desc.c | 55 +++------------------------ 1 file changed, 5 insertions(+), 50 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 82c7e0bc42..2a9a84df16 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -3948,50 +3948,6 @@ _set_fcn_team_config (ARGS_SET_FCN) return TRUE; } -static gconstpointer -_get_fcn_tun_mode (ARGS_GET_FCN) -{ - NMSettingTun *s_tun = NM_SETTING_TUN (setting); - NMSettingTunMode mode; - char *tmp, *str; - - RETURN_UNSUPPORTED_GET_TYPE (); - - mode = nm_setting_tun_get_mode (s_tun); - tmp = nm_utils_enum_to_str (nm_setting_tun_mode_get_type (), mode); - if (get_type != NM_META_ACCESSOR_GET_TYPE_PRETTY) - str = tmp ?: g_strdup (""); - else { - str = g_strdup_printf ("%d (%s)", mode, tmp ? tmp : ""); - g_free (tmp); - } - RETURN_STR_TO_FREE (str); -} - -static gboolean -_set_fcn_tun_mode (ARGS_SET_FCN) -{ - NMSettingTunMode mode; - gboolean ret; - long int t; - - if (nmc_string_to_int_base (value, 0, TRUE, 0, NM_SETTING_TUN_MODE_TAP, &t)) - mode = (NMSettingTunMode) t; - else { - ret = nm_utils_enum_from_str (nm_setting_tun_mode_get_type (), value, - (int *) &mode, NULL); - - if (!ret) { - g_set_error (error, 1, 0, _("invalid option '%s', use '%s' or '%s'"), - value, "tun", "tap"); - return FALSE; - } - } - - g_object_set (setting, property_info->property_name, (guint) mode, NULL); - return TRUE; -} - static gconstpointer _get_fcn_vlan_flags (ARGS_GET_FCN) { @@ -6397,12 +6353,11 @@ static const NMMetaPropertyInfo *const property_infos_TUN[] = { .property_alias = "mode", .prompt = NM_META_TEXT_PROMPT_TUN_MODE, .def_hint = NM_META_TEXT_PROMPT_TUN_MODE_CHOICES, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_tun_mode, - .set_fcn = _set_fcn_tun_mode, - ), - .property_typ_data = DEFINE_PROPERTY_TYP_DATA ( - .values_static = VALUES_STATIC ("tun", "tap", "unknown"), + .property_type = &_pt_gobject_enum, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, + .get_gtype = nm_setting_tun_mode_get_type, + .min = NM_SETTING_TUN_MODE_UNKNOWN + 1, + .max = G_MAXINT, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_TUN_OWNER, From cc79b1a554e9a4decfc57b4670660d7ad8d18307 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 20 May 2017 22:16:39 +0200 Subject: [PATCH 04/25] clients: implement wired.wake-on-lan property as enum --- clients/common/nm-meta-setting-desc.c | 85 +++++++-------------------- 1 file changed, 21 insertions(+), 64 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 2a9a84df16..b9706dab8d 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -4142,67 +4142,6 @@ DEFINE_REMOVER_OPTION (_remove_fcn_vpn_secrets, NM_SETTING_VPN, nm_setting_vpn_remove_secret) -static gconstpointer -_get_fcn_wired_wake_on_lan (ARGS_GET_FCN) -{ - NMSettingWired *s_wired = NM_SETTING_WIRED (setting); - NMSettingWiredWakeOnLan wol; - char *tmp, *str; - - RETURN_UNSUPPORTED_GET_TYPE (); - - wol = nm_setting_wired_get_wake_on_lan (s_wired); - tmp = nm_utils_enum_to_str (nm_setting_wired_wake_on_lan_get_type (), wol); - if (get_type != NM_META_ACCESSOR_GET_TYPE_PRETTY) - str = g_strdup_printf ("%s", tmp && *tmp ? tmp : "none"); - else - str = g_strdup_printf ("%d (%s)", wol, tmp && *tmp ? tmp : "none"); - g_free (tmp); - RETURN_STR_TO_FREE (str); -} - -static gboolean -_set_fcn_wired_wake_on_lan (ARGS_SET_FCN) -{ - NMSettingWiredWakeOnLan wol; - gs_free char *err_token = NULL; - gboolean ret; - long int t; - - if (nmc_string_to_int_base (value, 0, TRUE, 0, - NM_SETTING_WIRED_WAKE_ON_LAN_ALL - | NM_SETTING_WIRED_WAKE_ON_LAN_EXCLUSIVE_FLAGS, - &t)) - wol = (NMSettingWiredWakeOnLan) t; - else { - ret = nm_utils_enum_from_str (nm_setting_wired_wake_on_lan_get_type (), value, - (int *) &wol, &err_token); - - if (!ret) { - if ( g_ascii_strcasecmp (err_token, "none") == 0 - || g_ascii_strcasecmp (err_token, "disable") == 0 - || g_ascii_strcasecmp (err_token, "disabled") == 0) - wol = NM_SETTING_WIRED_WAKE_ON_LAN_NONE; - else { - g_set_error (error, 1, 0, _("invalid option '%s', use a combination of [%s] or 'ignore', 'default' or 'none'"), - err_token, - nm_utils_enum_to_str (nm_setting_wired_wake_on_lan_get_type (), - NM_SETTING_WIRED_WAKE_ON_LAN_ALL)); - return FALSE; - } - } - } - - if ( NM_FLAGS_ANY (wol, NM_SETTING_WIRED_WAKE_ON_LAN_EXCLUSIVE_FLAGS) - && !nm_utils_is_power_of_two (wol)) { - g_set_error_literal (error, 1, 0, _("'default' and 'ignore' are incompatible with other flags")); - return FALSE; - } - - g_object_set (setting, property_info->property_name, (guint) wol, NULL); - return TRUE; -} - DEFINE_SETTER_MAC_BLACKLIST (_set_fcn_wired_mac_address_blacklist, NM_SETTING_WIRED, nm_setting_wired_add_mac_blacklist_item) @@ -6664,9 +6603,27 @@ static const NMMetaPropertyInfo *const property_infos_WIRED[] = { ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_WIRED_WAKE_ON_LAN, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_wired_wake_on_lan, - .set_fcn = _set_fcn_wired_wake_on_lan, + .property_type = &_pt_gobject_enum, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA ( + PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, + .get_gtype = nm_setting_wired_wake_on_lan_get_type, + .value_infos = ENUM_VALUE_INFOS ( + { + .value = NM_SETTING_WIRED_WAKE_ON_LAN_NONE, + .nick = "none", + }, + { + .value = NM_SETTING_WIRED_WAKE_ON_LAN_NONE, + .nick = "disable", + }, + { + .value = NM_SETTING_WIRED_WAKE_ON_LAN_NONE, + .nick = "disabled", + } + ), + ), + .typ_flags = NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PARSABLE_TEXT + | NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PRETTY_TEXT, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_WIRED_WAKE_ON_LAN_PASSWORD, From 1371116ea2c2c244eb06d3a2ac0cbf1b3e9adbe4 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 20 May 2017 22:17:11 +0200 Subject: [PATCH 05/25] clients: implement wireless.mac-address-randomization property as enum --- clients/common/nm-meta-setting-desc.c | 63 +++------------------------ 1 file changed, 7 insertions(+), 56 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index b9706dab8d..cebf175096 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -4253,29 +4253,6 @@ _get_fcn_wireless_ssid (ARGS_GET_FCN) RETURN_STR_TO_FREE (ssid_str); } -static gconstpointer -_get_fcn_wireless_mac_address_randomization (ARGS_GET_FCN) -{ - NMSettingWireless *s_wifi = NM_SETTING_WIRELESS (setting); - NMSettingMacRandomization randomization = nm_setting_wireless_get_mac_address_randomization (s_wifi); - const char *s; - - RETURN_UNSUPPORTED_GET_TYPE (); - - if (randomization == NM_SETTING_MAC_RANDOMIZATION_DEFAULT) - s = N_("default"); - else if (randomization == NM_SETTING_MAC_RANDOMIZATION_NEVER) - s = N_("never"); - else if (randomization == NM_SETTING_MAC_RANDOMIZATION_ALWAYS) - s = N_("always"); - else - s = N_("unknown"); - - if (get_type == NM_META_ACCESSOR_GET_TYPE_PRETTY) - return _(s); - return s; -} - static gboolean _set_fcn_wireless_channel (ARGS_SET_FCN) { @@ -4326,36 +4303,6 @@ DEFINE_REMOVER_INDEX_OR_VALUE (_remove_fcn_wireless_mac_address_blacklist, nm_setting_wireless_remove_mac_blacklist_item, _validate_and_remove_wifi_mac_blacklist_item) -static gboolean -_set_fcn_wireless_mac_address_randomization (ARGS_SET_FCN) -{ - NMSettingMacRandomization randomization; - gs_free char *err_token = NULL; - gboolean ret; - long int t; - - if (nmc_string_to_int_base (value, 0, TRUE, - NM_SETTING_MAC_RANDOMIZATION_DEFAULT, - NM_SETTING_MAC_RANDOMIZATION_ALWAYS, - &t)) - randomization = (NMSettingMacRandomization) t; - else { - ret = nm_utils_enum_from_str (nm_setting_mac_randomization_get_type (), - value, - (int *) &randomization, - &err_token); - - if (!ret) { - g_set_error (error, 1, 0, _("invalid option '%s', use 'default', 'never' or 'always'"), - err_token); - return FALSE; - } - } - - g_object_set (setting, property_info->property_name, (guint) randomization, NULL); - return TRUE; -} - static gconstpointer _get_fcn_wireless_security_wep_key0 (ARGS_GET_FCN) { @@ -6706,9 +6653,13 @@ static const NMMetaPropertyInfo *const property_infos_WIRELESS[] = { ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_WIRELESS_MAC_ADDRESS_RANDOMIZATION, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_wireless_mac_address_randomization, - .set_fcn = _set_fcn_wireless_mac_address_randomization, + .property_type = &_pt_gobject_enum, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA ( + PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, + .get_gtype = nm_setting_mac_randomization_get_type, + ), + .typ_flags = NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PARSABLE_TEXT + | NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PRETTY_TEXT, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_WIRELESS_MTU, From 9a3e35cc0e08bb10ba6b4f51cb5e6189535e72f9 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 20 May 2017 22:17:11 +0200 Subject: [PATCH 06/25] clients: don't use nmc_string_to_int_base() and nmc_string_to_uint_base() util They are redundant. --- clients/common/nm-client-utils.c | 4 +- clients/common/nm-client-utils.h | 12 ----- clients/common/nm-meta-setting-desc.c | 69 +++++++++++++-------------- 3 files changed, 34 insertions(+), 51 deletions(-) diff --git a/clients/common/nm-client-utils.c b/clients/common/nm-client-utils.c index 84e7061772..d5f8b2a336 100644 --- a/clients/common/nm-client-utils.c +++ b/clients/common/nm-client-utils.c @@ -25,7 +25,7 @@ * Convert string to signed integer. * If required, the resulting number is checked to be in the range. */ -gboolean +static gboolean nmc_string_to_int_base (const char *str, int base, gboolean range_check, @@ -49,7 +49,7 @@ nmc_string_to_int_base (const char *str, * Convert string to unsigned integer. * If required, the resulting number is checked to be in the range. */ -gboolean +static gboolean nmc_string_to_uint_base (const char *str, int base, gboolean range_check, diff --git a/clients/common/nm-client-utils.h b/clients/common/nm-client-utils.h index 9ef568991c..46479c7e32 100644 --- a/clients/common/nm-client-utils.h +++ b/clients/common/nm-client-utils.h @@ -33,18 +33,6 @@ const char *nmc_string_is_valid (const char *input, const char **allowed, GError char **nmc_strsplit_set (const char *str, const char *delimiter, int max_tokens); -gboolean nmc_string_to_int_base (const char *str, - int base, - gboolean range_check, - long int min, - long int max, - long int *value); -gboolean nmc_string_to_uint_base (const char *str, - int base, - gboolean range_check, - unsigned long int min, - unsigned long int max, - unsigned long int *value); gboolean nmc_string_to_int (const char *str, gboolean range_check, long int min, diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index cebf175096..f8a50ea9b9 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -1707,7 +1707,6 @@ nmc_property_set_bytes (NMSetting *setting, const char *prop, const char *value, gs_strfreev char **strv = NULL; const char *delimiters = " \t,"; char **iter; - long int val_int; GBytes *bytes; GByteArray *array = NULL; gboolean success = TRUE; @@ -1725,15 +1724,17 @@ nmc_property_set_bytes (NMSetting *setting, const char *prop, const char *value, strv = nmc_strsplit_set (val_strip, delimiters, 0); array = g_byte_array_sized_new (g_strv_length (strv)); for (iter = strv; iter && *iter; iter++) { + int v; guint8 v8; - if (!nmc_string_to_int_base (g_strstrip (*iter), 16, TRUE, 0, 255, &val_int)) { + v = _nm_utils_ascii_str_to_int64 (*iter, 16, 0, 255, -1); + if (v == -1) { g_set_error (error, 1, 0, _("'%s' is not a valid hex character"), *iter); g_byte_array_free (array, TRUE); success = FALSE; goto done; } - v8 = val_int; + v8 = v; g_byte_array_append (array, &v8, 1); } bytes = g_byte_array_free_to_bytes (array); @@ -2697,14 +2698,14 @@ _set_fcn_dcb_flags (ARGS_SET_FCN) g_return_val_if_fail (error == NULL || *error == NULL, FALSE); /* Check for overall hex numeric value */ - if (nmc_string_to_int_base (value, 0, TRUE, 0, DCB_ALL_FLAGS, &t)) + t = _nm_utils_ascii_str_to_int64 (value, 0, 0, DCB_ALL_FLAGS, -1); + if (t != -1) flags = (guint) t; else { /* Check for individual flag numbers */ strv = nmc_strsplit_set (value, " \t,", 0); for (iter = strv; iter && *iter; iter++) { - if (!nmc_string_to_int_base (*iter, 0, TRUE, 0, DCB_ALL_FLAGS, &t)) - t = -1; + t = _nm_utils_ascii_str_to_int64 (*iter, 0, 0, DCB_ALL_FLAGS, -1); if ( g_ascii_strcasecmp (*iter, "enable") == 0 || g_ascii_strcasecmp (*iter, "enabled") == 0 @@ -2763,46 +2764,43 @@ dcb_parse_uint_array (const char *val, guint *out_array, GError **error) { - char **items, **iter; - guint i = 0; + gs_strfreev char **items = NULL; + char **iter; + gsize i; - g_return_val_if_fail (out_array != NULL, FALSE); + nm_assert (out_array); items = g_strsplit_set (val, ",", -1); if (g_strv_length (items) != 8) { g_set_error_literal (error, 1, 0, _("must contain 8 comma-separated numbers")); - goto error; + return FALSE; } - for (iter = items; iter && *iter; iter++) { - long int num = 0; - gboolean success; + i = 0; + for (iter = items; *iter; iter++) { + gint64 num; *iter = g_strstrip (*iter); - success = nmc_string_to_int_base (*iter, 10, TRUE, 0, other ? other : max, &num); + + num = _nm_utils_ascii_str_to_int64 (*iter, 10, 0, other ? other : max, -1); /* If number is greater than 'max' it must equal 'other' */ - if (success && other && (num > max) && (num != other)) - success = FALSE; - - if (!success) { + if ( num == -1 + || (other && (num > max) && (num != other))) { if (other) { g_set_error (error, 1, 0, _("'%s' not a number between 0 and %u (inclusive) or %u"), - *iter, max, other); + *iter, max, other); } else { g_set_error (error, 1, 0, _("'%s' not a number between 0 and %u (inclusive)"), - *iter, max); + *iter, max); } - goto error; + return FALSE; } + nm_assert (i < 8); out_array[i++] = (guint) num; } return TRUE; - -error: - g_strfreev (items); - return FALSE; } static void @@ -2962,25 +2960,22 @@ _set_fcn_gsm_sim_operator_id (ARGS_SET_FCN) static gboolean _set_fcn_infiniband_p_key (ARGS_SET_FCN) { - gboolean p_key_valid = FALSE; - long p_key_int; + const gint64 INVALID = G_MININT64; + gint64 p_key; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - if (!strncasecmp (value, "0x", 2)) - p_key_valid = nmc_string_to_int_base (value + 2, 16, TRUE, 0, G_MAXUINT16, &p_key_int); - else - p_key_valid = nmc_string_to_int (value, TRUE, -1, G_MAXUINT16, &p_key_int); - - if (!p_key_valid) { - if (strcmp (value, "default") == 0) - p_key_int = -1; - else { + if (nm_streq (value, "default")) + p_key = -1; + else { + p_key = _nm_utils_ascii_str_to_int64 (value, 0, -1, G_MAXUINT16, INVALID); + if (p_key == INVALID) { g_set_error (error, 1, 0, _("'%s' is not a valid IBoIP P_Key"), value); return FALSE; } } - g_object_set (setting, property_info->property_name, (gint) p_key_int, NULL); + + g_object_set (setting, property_info->property_name, (int) p_key, NULL); return TRUE; } From 5183399744f1b85481c75e3c4242a1b9a6bd9b64 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 20 May 2017 22:17:11 +0200 Subject: [PATCH 07/25] clients: cleanup parse_ip_address --- clients/common/nm-meta-setting-desc.c | 132 ++++++++------------------ 1 file changed, 42 insertions(+), 90 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index f8a50ea9b9..b6af6f0617 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -78,49 +78,39 @@ _gtype_property_get_gtype (GType gtype, const char *property_name) /*****************************************************************************/ -/* - * Parse IP address from string to NMIPAddress stucture. - * ip_str is the IP address in the form address/prefix - */ static NMIPAddress * -nmc_parse_and_build_address (int family, const char *ip_str, GError **error) +_parse_ip_address (int family, const char *address, GError **error) { - int max_prefix = (family == AF_INET) ? 32 : 128; - NMIPAddress *addr = NULL; - const char *ip; - char *tmp; + gs_free char *ip_str = NULL; + const int MAX_PREFIX = (family == AF_INET) ? 32 : 128; + NMIPAddress *addr; char *plen; - long int prefix; + int prefix; GError *local = NULL; - g_return_val_if_fail (ip_str != NULL, NULL); - g_return_val_if_fail (error == NULL || *error == NULL, NULL); + g_return_val_if_fail (address, NULL); + g_return_val_if_fail (!error || !*error, NULL); - tmp = g_strdup (ip_str); - plen = strchr (tmp, '/'); /* prefix delimiter */ - if (plen) - *plen++ = '\0'; + ip_str = g_strstrip (g_strdup (address)); - ip = tmp; + prefix = MAX_PREFIX; - prefix = max_prefix; + plen = strchr (ip_str, '/'); if (plen) { - if (!nmc_string_to_int (plen, TRUE, 1, max_prefix, &prefix)) { + *plen++ = '\0'; + if ((prefix = _nm_utils_ascii_str_to_int64 (plen, 10, 1, MAX_PREFIX, -1)) == -1) { g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, - _("invalid prefix '%s'; <1-%d> allowed"), plen, max_prefix); - goto finish; + _("invalid prefix '%s'; <1-%d> allowed"), plen, MAX_PREFIX); + return NULL; } } - addr = nm_ip_address_new (family, ip, (guint32) prefix, &local); + addr = nm_ip_address_new (family, ip_str, prefix, &local); if (!addr) { g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, _("invalid IP address: %s"), local->message); g_clear_error (&local); } - -finish: - g_free (tmp); return addr; } @@ -3037,17 +3027,6 @@ _set_fcn_ip_tunnel_mode (ARGS_SET_FCN) return TRUE; } -static NMIPAddress * -_parse_ip_address (int family, const char *address, GError **error) -{ - char *value = g_strdup (address); - NMIPAddress *ipaddr; - - ipaddr = nmc_parse_and_build_address (family, g_strstrip (value), error); - g_free (value); - return ipaddr; -} - static gconstpointer _get_fcn_ip_config_addresses (ARGS_GET_FCN) { @@ -3311,31 +3290,21 @@ DEFINE_REMOVER_INDEX_OR_VALUE (_remove_fcn_ipv4_config_dns_options, nm_setting_ip_config_remove_dns_option, _validate_and_remove_ipv4_dns_option) -static NMIPAddress * -_parse_ipv4_address (const char *address, GError **error) -{ - return _parse_ip_address (AF_INET, address, error); -} - static gboolean _set_fcn_ip4_config_addresses (ARGS_SET_FCN) { - char **strv = NULL, **iter; + gs_strfreev char **strv = NULL; + const char *const*iter; NMIPAddress *ip4addr; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - strv = nmc_strsplit_set (value, ",", 0); - for (iter = strv; iter && *iter; iter++) { - ip4addr = _parse_ipv4_address (*iter, error); - if (!ip4addr) { - g_strfreev (strv); + for (iter = (const char *const*) strv; *iter; iter++) { + ip4addr = _parse_ip_address (AF_INET, *iter, error); + if (!ip4addr) return FALSE; - } nm_setting_ip_config_add_address (NM_SETTING_IP_CONFIG (setting), ip4addr); nm_ip_address_unref (ip4addr); } - g_strfreev (strv); return TRUE; } @@ -3347,14 +3316,15 @@ _validate_and_remove_ipv4_address (NMSettingIPConfig *setting, NMIPAddress *ip4addr; gboolean ret; - ip4addr = _parse_ipv4_address (address, error); + ip4addr = _parse_ip_address (AF_INET, address, error); if (!ip4addr) return FALSE; ret = nm_setting_ip_config_remove_address_by_value (setting, ip4addr); - if (!ret) + if (!ret) { g_set_error (error, 1, 0, _("the property doesn't contain IP address '%s'"), address); + } nm_ip_address_unref (ip4addr); return ret; } @@ -3367,21 +3337,16 @@ DEFINE_REMOVER_INDEX_OR_VALUE (_remove_fcn_ipv4_config_addresses, static gboolean _set_fcn_ip4_config_gateway (ARGS_SET_FCN) { - NMIPAddress *ip4addr; + gs_free char *addr = NULL; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + addr = g_strstrip (g_strdup (value)); - if (strchr (value, '/')) { - g_set_error (error, 1, 0, - _("invalid gateway address '%s'"), value); + if (!nm_utils_ipaddr_valid (AF_INET, addr)) { + g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, + _("invalid gateway address '%s'"), value); return FALSE; } - ip4addr = _parse_ipv4_address (value, error); - if (!ip4addr) - return FALSE; - - g_object_set (setting, property_info->property_name, value, NULL); - nm_ip_address_unref (ip4addr); + g_object_set (setting, property_info->property_name, addr, NULL); return TRUE; } @@ -3589,31 +3554,21 @@ DEFINE_REMOVER_INDEX_OR_VALUE (_remove_fcn_ipv6_config_dns_options, nm_setting_ip_config_remove_dns_option, _validate_and_remove_ipv6_dns_option) -static NMIPAddress * -_parse_ipv6_address (const char *address, GError **error) -{ - return _parse_ip_address (AF_INET6, address, error); -} - static gboolean _set_fcn_ip6_config_addresses (ARGS_SET_FCN) { - char **strv = NULL, **iter; + gs_strfreev char **strv = NULL; + const char *const*iter; NMIPAddress *ip6addr; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - strv = nmc_strsplit_set (value, ",", 0); - for (iter = strv; iter && *iter; iter++) { - ip6addr = _parse_ipv6_address (*iter, error); - if (!ip6addr) { - g_strfreev (strv); + for (iter = (const char *const*) strv; *iter; iter++) { + ip6addr = _parse_ip_address (AF_INET6, *iter, error); + if (!ip6addr) return FALSE; - } nm_setting_ip_config_add_address (NM_SETTING_IP_CONFIG (setting), ip6addr); nm_ip_address_unref (ip6addr); } - g_strfreev (strv); return TRUE; } @@ -3625,7 +3580,7 @@ _validate_and_remove_ipv6_address (NMSettingIPConfig *setting, NMIPAddress *ip6addr; gboolean ret; - ip6addr = _parse_ipv6_address (address, error); + ip6addr = _parse_ip_address (AF_INET6, address, error); if (!ip6addr) return FALSE; @@ -3644,21 +3599,18 @@ DEFINE_REMOVER_INDEX_OR_VALUE (_remove_fcn_ipv6_config_addresses, static gboolean _set_fcn_ip6_config_gateway (ARGS_SET_FCN) { - NMIPAddress *ip6addr; + gs_free char *addr = NULL; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + addr = g_strstrip (g_strdup (value)); - if (strchr (value, '/')) { - g_set_error (error, 1, 0, - _("invalid gateway address '%s'"), value); + if (!nm_utils_ipaddr_valid (AF_INET6, addr)) { + g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, + _("invalid gateway address '%s'"), + addr); return FALSE; } - ip6addr = _parse_ipv6_address (value, error); - if (!ip6addr) - return FALSE; - g_object_set (setting, property_info->property_name, value, NULL); - nm_ip_address_unref (ip6addr); + g_object_set (setting, property_info->property_name, addr, NULL); return TRUE; } From 1e5c5bd4a6da66ac996f050be2536ad9e1a6f985 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 20 May 2017 22:17:11 +0200 Subject: [PATCH 08/25] clients: cleanup parse_ip_route --- clients/common/nm-meta-setting-desc.c | 135 ++++++++++---------------- 1 file changed, 51 insertions(+), 84 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index b6af6f0617..8312fb429f 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -114,79 +114,69 @@ _parse_ip_address (int family, const char *address, GError **error) return addr; } -/* - * nmc_parse_and_build_route: - * @family: AF_INET or AF_INET6 - * @str: route string to be parsed - * @error: location to store GError - * - * Parse route from string and return an #NMIPRoute - * - * Returns: a new #NMIPRoute or %NULL on error - */ static NMIPRoute * -nmc_parse_and_build_route (int family, - const char *str, - GError **error) +_parse_ip_route (int family, + const char *str, + GError **error) { - int max_prefix = (family == AF_INET) ? 32 : 128; + const int MAX_PREFIX = (family == AF_INET) ? 32 : 128; char *plen = NULL; const char *next_hop = NULL; const char *canon_dest; - long int prefix = max_prefix; - unsigned long int tmp_ulong; + int prefix; NMIPRoute *route = NULL; - gboolean success = FALSE; GError *local = NULL; gint64 metric = -1; - guint i, len; + guint i; gs_strfreev char **routev = NULL; - gs_free char *value = NULL; - gs_free char *dest = NULL; + gs_free char *str_clean = NULL; + char *dest; gs_unref_hashtable GHashTable *attrs = NULL; GHashTable *tmp_attrs; - const char *syntax = _("The valid syntax is: 'ip[/prefix] [next-hop] [metric] [attribute=val]... [,ip[/prefix] ...]'"); +#define ROUTE_SYNTAX _("The valid syntax is: 'ip[/prefix] [next-hop] [metric] [attribute=val]... [,ip[/prefix] ...]'") - g_return_val_if_fail (family == AF_INET || family == AF_INET6, FALSE); - g_return_val_if_fail (str, FALSE); - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + nm_assert (NM_IN_SET (family, AF_INET, AF_INET6)); + nm_assert (str); + nm_assert (!error || !*error); - value = g_strdup (str); - routev = nmc_strsplit_set (g_strstrip (value), " \t", 0); - len = g_strv_length (routev); - if (len < 1) { - g_set_error (error, 1, 0, "%s", syntax); - g_prefix_error (error, "'%s' is not valid. ", str); - goto finish; + str_clean = g_strstrip (g_strdup (str)); + routev = nmc_strsplit_set (str_clean, " \t", 0); + if (!routev || !routev[0]) { + g_set_error (error, 1, 0, + "'%s' is not valid. %s", + str, ROUTE_SYNTAX); + return NULL; } - dest = g_strdup (routev[0]); + dest = routev[0]; plen = strchr (dest, '/'); /* prefix delimiter */ if (plen) *plen++ = '\0'; - + prefix = MAX_PREFIX; if (plen) { - if (!nmc_string_to_int (plen, TRUE, 1, max_prefix, &prefix)) { + if ((prefix = _nm_utils_ascii_str_to_int64 (plen, 10, 1, MAX_PREFIX, -1)) == -1) { g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, _("invalid prefix '%s'; <1-%d> allowed"), - plen, max_prefix); - goto finish; + plen, MAX_PREFIX); + return NULL; } } - for (i = 1; i < len; i++) { + for (i = 1; routev[i]; i++) { + gint64 tmp64; + if (nm_utils_ipaddr_valid (family, routev[i])) { if (metric != -1 || attrs) { g_set_error (error, 1, 0, _("the next hop ('%s') must be first"), routev[i]); - goto finish; + return NULL; } next_hop = routev[i]; - } else if (nmc_string_to_uint (routev[i], TRUE, 0, G_MAXUINT32, &tmp_ulong)) { + } else if ((tmp64 = _nm_utils_ascii_str_to_int64 (routev[i], 10, 0, G_MAXUINT32, -1)) != -1) { if (attrs) { g_set_error (error, 1, 0, _("the metric ('%s') must be before attributes"), routev[i]); - goto finish; + return NULL; } - metric = tmp_ulong; + metric = tmp64; } else if (strchr (routev[i], '=')) { GHashTableIter iter; char *iter_key; @@ -197,7 +187,7 @@ nmc_parse_and_build_route (int family, error); if (!tmp_attrs) { g_prefix_error (error, "invalid option '%s': ", routev[i]); - goto finish; + return NULL; } if (!attrs) @@ -208,24 +198,24 @@ nmc_parse_and_build_route (int family, if (!nm_ip_route_attribute_validate (iter_key, iter_value, family, NULL, error)) { g_prefix_error (error, "%s: ", iter_key); g_hash_table_unref (tmp_attrs); - goto finish; + return NULL; } g_hash_table_insert (attrs, iter_key, iter_value); g_hash_table_iter_steal (&iter); } g_hash_table_unref (tmp_attrs); } else { - g_set_error (error, 1, 0, "%s", syntax); - goto finish; + g_set_error (error, 1, 0, "%s", ROUTE_SYNTAX); + return NULL; } } route = nm_ip_route_new (family, dest, prefix, next_hop, metric, &local); if (!route) { - g_set_error (error, 1, 0, "%s", syntax); - g_prefix_error (error, _("invalid route: %s. "), local->message); + g_set_error (error, 1, 0, + _("invalid route: %s. %s"), local->message, ROUTE_SYNTAX); g_clear_error (&local); - goto finish; + return NULL; } /* We don't accept default routes as NetworkManager handles it @@ -237,7 +227,7 @@ nmc_parse_and_build_route (int family, g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, _("default route cannot be added (NetworkManager handles it by itself)")); g_clear_pointer (&route, nm_ip_route_unref); - goto finish; + return NULL; } if (attrs) { @@ -250,9 +240,6 @@ nmc_parse_and_build_route (int family, nm_ip_route_set_attribute (route, name, variant); } - success = TRUE; - -finish: return route; } @@ -3350,31 +3337,21 @@ _set_fcn_ip4_config_gateway (ARGS_SET_FCN) return TRUE; } -static NMIPRoute * -_parse_ipv4_route (const char *route, GError **error) -{ - return nmc_parse_and_build_route (AF_INET, route, error); -} - static gboolean _set_fcn_ip4_config_routes (ARGS_SET_FCN) { - char **strv = NULL, **iter; + gs_strfreev char **strv = NULL; + const char *const*iter; NMIPRoute *ip4route; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - strv = nmc_strsplit_set (value, ",", 0); - for (iter = strv; iter && *iter; iter++) { - ip4route = _parse_ipv4_route (*iter, error); - if (!ip4route) { - g_strfreev (strv); + for (iter = (const char *const*) strv; *iter; iter++) { + ip4route = _parse_ip_route (AF_INET, *iter, error); + if (!ip4route) return FALSE; - } nm_setting_ip_config_add_route (NM_SETTING_IP_CONFIG (setting), ip4route); nm_ip_route_unref (ip4route); } - g_strfreev (strv); return TRUE; } @@ -3386,7 +3363,7 @@ _validate_and_remove_ipv4_route (NMSettingIPConfig *setting, NMIPRoute *ip4route; gboolean ret; - ip4route = _parse_ipv4_route (route, error); + ip4route = _parse_ip_route (AF_INET, route, error); if (!ip4route) return FALSE; @@ -3614,31 +3591,21 @@ _set_fcn_ip6_config_gateway (ARGS_SET_FCN) return TRUE; } -static NMIPRoute * -_parse_ipv6_route (const char *route, GError **error) -{ - return nmc_parse_and_build_route (AF_INET6, route, error); -} - static gboolean _set_fcn_ip6_config_routes (ARGS_SET_FCN) { - char **strv = NULL, **iter; + gs_strfreev char **strv = NULL; + const char *const*iter; NMIPRoute *ip6route; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - strv = nmc_strsplit_set (value, ",", 0); - for (iter = strv; iter && *iter; iter++) { - ip6route = _parse_ipv6_route (*iter, error); - if (!ip6route) { - g_strfreev (strv); + for (iter = (const char *const*) strv; *iter; iter++) { + ip6route = _parse_ip_route (AF_INET6, *iter, error); + if (!ip6route) return FALSE; - } nm_setting_ip_config_add_route (NM_SETTING_IP_CONFIG (setting), ip6route); nm_ip_route_unref (ip6route); } - g_strfreev (strv); return TRUE; } @@ -3650,7 +3617,7 @@ _validate_and_remove_ipv6_route (NMSettingIPConfig *setting, NMIPRoute *ip6route; gboolean ret; - ip6route = _parse_ipv6_route (route, error); + ip6route = _parse_ip_route (AF_INET6, route, error); if (!ip6route) return FALSE; From 8c87a4b8a8ffb0fb37a0b014c0d7501b0b91fe90 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 13:51:12 +0200 Subject: [PATCH 09/25] clients: replace nmc_string_to_int() by _nm_utils_ascii_str_to_int64() --- clients/common/nm-client-utils.c | 34 -------------------- clients/common/nm-client-utils.h | 5 --- clients/common/nm-meta-setting-desc.c | 45 +++++++++++++-------------- 3 files changed, 22 insertions(+), 62 deletions(-) diff --git a/clients/common/nm-client-utils.c b/clients/common/nm-client-utils.c index d5f8b2a336..a15a10b3bc 100644 --- a/clients/common/nm-client-utils.c +++ b/clients/common/nm-client-utils.c @@ -21,30 +21,6 @@ #include "nm-client-utils.h" -/* - * Convert string to signed integer. - * If required, the resulting number is checked to be in the range. - */ -static gboolean -nmc_string_to_int_base (const char *str, - int base, - gboolean range_check, - long int min, - long int max, - long int *value) -{ - char *end; - long int tmp; - - errno = 0; - tmp = strtol (str, &end, base); - if (errno || *end != '\0' || (range_check && (tmp < min || tmp > max))) { - return FALSE; - } - *value = tmp; - return TRUE; -} - /* * Convert string to unsigned integer. * If required, the resulting number is checked to be in the range. @@ -69,16 +45,6 @@ nmc_string_to_uint_base (const char *str, return TRUE; } -gboolean -nmc_string_to_int (const char *str, - gboolean range_check, - long int min, - long int max, - long int *value) -{ - return nmc_string_to_int_base (str, 10, range_check, min, max, value); -} - gboolean nmc_string_to_uint (const char *str, gboolean range_check, diff --git a/clients/common/nm-client-utils.h b/clients/common/nm-client-utils.h index 46479c7e32..aea29e6360 100644 --- a/clients/common/nm-client-utils.h +++ b/clients/common/nm-client-utils.h @@ -33,11 +33,6 @@ const char *nmc_string_is_valid (const char *input, const char **allowed, GError char **nmc_strsplit_set (const char *str, const char *delimiter, int max_tokens); -gboolean nmc_string_to_int (const char *str, - gboolean range_check, - long int min, - long int max, - long int *value); gboolean nmc_string_to_uint (const char *str, gboolean range_check, unsigned long int min, diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 8312fb429f..88f363d7e6 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -791,54 +791,54 @@ _set_fcn_gobject_bool (ARGS_SET_FCN) static gboolean _set_fcn_gobject_trilean (ARGS_SET_FCN) { - long int val_int; + const int INVALID = G_MININT; + int v; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - - if (!nmc_string_to_int (value, TRUE, -1, 1, &val_int)) { + v = _nm_utils_ascii_str_to_int64 (value, 10, -1, 1, INVALID); + if (v == INVALID) { g_set_error (error, 1, 0, _("'%s' is not a valid value; use -1, 0 or 1"), value); return FALSE; } - - g_object_set (setting, property_info->property_name, val_int, NULL); + g_object_set (setting, property_info->property_name, v, NULL); return TRUE; } static gboolean _set_fcn_gobject_int (ARGS_SET_FCN) { - long int val_int; + const gint64 INVALID = G_MININT64; + gint64 v; - if (!nmc_string_to_int (value, TRUE, G_MININT, G_MAXINT, &val_int)) { + v = _nm_utils_ascii_str_to_int64 (value, 10, G_MININT, G_MAXINT, INVALID); + if (v == INVALID) { g_set_error (error, 1, 0, _("'%s' is not a valid number (or out of range)"), value); return FALSE; } /* Validate the number according to the property spec */ - if (!validate_int (setting, property_info->property_name, (gint) val_int, error)) + if (!validate_int (setting, property_info->property_name, v, error)) return FALSE; - g_object_set (setting, property_info->property_name, (gint) val_int, NULL); + g_object_set (setting, property_info->property_name, (int) v, NULL); return TRUE; } static gboolean _set_fcn_gobject_int64 (ARGS_SET_FCN) { - long val_int; + gint64 v; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - - if (!nmc_string_to_int (value, FALSE, 0, 0, &val_int)) { + v = _nm_utils_ascii_str_to_int64 (value, 10, G_MININT64, G_MAXINT64, 0); + if (errno) { g_set_error (error, 1, 0, _("'%s' is not a valid number (or out of range)"), value); return FALSE; } /* Validate the number according to the property spec */ - if (!validate_int64 (setting, property_info->property_name, (gint64) val_int, error)) + if (!validate_int64 (setting, property_info->property_name, v, error)) return FALSE; - g_object_set (setting, property_info->property_name, (gint64) val_int, NULL); + g_object_set (setting, property_info->property_name, v, NULL); return TRUE; } @@ -2717,20 +2717,19 @@ _set_fcn_dcb_flags (ARGS_SET_FCN) static gboolean _set_fcn_dcb_priority (ARGS_SET_FCN) { - long int priority = 0; + const int INVALID = G_MININT; + int v; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - - if (!nmc_string_to_int (value, FALSE, -1, 7, &priority)) { + v = _nm_utils_ascii_str_to_int64 (value, 10, -1, 7, INVALID); + if (v == INVALID) { g_set_error (error, 1, 0, _("'%s' is not a DCB app priority"), value); return FALSE; } - /* Validate the number according to the property spec */ - if (!validate_int (setting, property_info->property_name, (gint) priority, error)) + if (!validate_int (setting, property_info->property_name, v, error)) return FALSE; - g_object_set (setting, property_info->property_name, (gint) priority, NULL); + g_object_set (setting, property_info->property_name, v, NULL); return TRUE; } From c55561d3039138a2857b67d3f651ed8f0c553d9b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 13:58:22 +0200 Subject: [PATCH 10/25] clients: implement connection.autoconnect-slaves property as enum --- clients/common/nm-meta-setting-desc.c | 47 ++------------------------- 1 file changed, 3 insertions(+), 44 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 88f363d7e6..f7c1da76aa 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -788,21 +788,6 @@ _set_fcn_gobject_bool (ARGS_SET_FCN) return TRUE; } -static gboolean -_set_fcn_gobject_trilean (ARGS_SET_FCN) -{ - const int INVALID = G_MININT; - int v; - - v = _nm_utils_ascii_str_to_int64 (value, 10, -1, 1, INVALID); - if (v == INVALID) { - g_set_error (error, 1, 0, _("'%s' is not a valid value; use -1, 0 or 1"), value); - return FALSE; - } - g_object_set (setting, property_info->property_name, v, NULL); - return TRUE; -} - static gboolean _set_fcn_gobject_int (ARGS_SET_FCN) { @@ -1235,24 +1220,6 @@ ip6_privacy_to_string (NMSettingIP6ConfigPrivacy ip6_privacy, NMMetaAccessorGetT } } -static char * -autoconnect_slaves_to_string (NMSettingConnectionAutoconnectSlaves autoconnect_slaves, - NMMetaAccessorGetType get_type) -{ - if (get_type != NM_META_ACCESSOR_GET_TYPE_PRETTY) - return g_strdup_printf ("%d", autoconnect_slaves); - - switch (autoconnect_slaves) { - case NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES_NO: - return g_strdup_printf (_("%d (no)"), autoconnect_slaves); - case NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES_YES: - return g_strdup_printf (_("%d (yes)"), autoconnect_slaves); - case NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES_DEFAULT: - default: - return g_strdup_printf (_("%d (default)"), autoconnect_slaves); - } -} - static char * secret_flags_to_string (guint32 flags, NMMetaAccessorGetType get_type) { @@ -2232,14 +2199,6 @@ _get_fcn_connection_permissions (ARGS_GET_FCN) return NULL; } -static gconstpointer -_get_fcn_connection_autoconnect_slaves (ARGS_GET_FCN) -{ - NMSettingConnection *s_con = NM_SETTING_CONNECTION (setting); - RETURN_UNSUPPORTED_GET_TYPE (); - RETURN_STR_TO_FREE (autoconnect_slaves_to_string (nm_setting_connection_get_autoconnect_slaves (s_con), get_type)); -} - static gboolean _set_fcn_connection_type (ARGS_SET_FCN) { @@ -5237,9 +5196,9 @@ static const NMMetaPropertyInfo *const property_infos_CONNECTION[] = { ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_connection_autoconnect_slaves, - .set_fcn = _set_fcn_gobject_trilean, + .property_type = &_pt_gobject_enum, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, + .get_gtype = nm_setting_connection_autoconnect_slaves_get_type, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_CONNECTION_SECONDARIES, From 772225f8cf0bbc3ad56e0d01b5dee1b413465489 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 14:45:09 +0200 Subject: [PATCH 11/25] clients: implement ip-tunnel.mode property as enum --- clients/common/nm-meta-setting-desc.c | 57 +++++---------------------- 1 file changed, 9 insertions(+), 48 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index f7c1da76aa..cba64c2c78 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -2933,45 +2933,6 @@ _get_fcn_infiniband_p_key (ARGS_GET_FCN) RETURN_STR_TO_FREE (g_strdup_printf ("0x%04x", p_key)); } -static gconstpointer -_get_fcn_ip_tunnel_mode (ARGS_GET_FCN) -{ - NMSettingIPTunnel *s_ip_tunnel = NM_SETTING_IP_TUNNEL (setting); - NMIPTunnelMode mode; - - RETURN_UNSUPPORTED_GET_TYPE (); - - mode = nm_setting_ip_tunnel_get_mode (s_ip_tunnel); - RETURN_STR_TO_FREE (nm_utils_enum_to_str (nm_ip_tunnel_mode_get_type (), mode)); -} - -static gboolean -_set_fcn_ip_tunnel_mode (ARGS_SET_FCN) -{ - NMIPTunnelMode mode; - gboolean ret; - - ret = nm_utils_enum_from_str (nm_ip_tunnel_mode_get_type(), value, - (int *) &mode, NULL); - - if (!ret) { - gs_free const char **values = NULL; - gs_free char *values_str = NULL; - - values = nm_utils_enum_get_values (nm_ip_tunnel_mode_get_type (), - NM_IP_TUNNEL_MODE_UNKNOWN + 1, - G_MAXINT); - values_str = g_strjoinv (",", (char **) values); - g_set_error (error, 1, 0, _("invalid mode '%s', use one of %s"), - value, values_str); - - return FALSE; - } - - g_object_set (setting, property_info->property_name, mode, NULL); - return TRUE; -} - static gconstpointer _get_fcn_ip_config_addresses (ARGS_GET_FCN) { @@ -5724,15 +5685,15 @@ static const NMMetaPropertyInfo *const property_infos_IP_TUNNEL[] = { .property_alias = "mode", .inf_flags = NM_META_PROPERTY_INF_FLAG_REQD, .prompt = NM_META_TEXT_PROMPT_IP_TUNNEL_MODE, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_ip_tunnel_mode, - .set_fcn = _set_fcn_ip_tunnel_mode, - .values_fcn = _values_fcn_gobject_enum, - ), - .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, - .get_gtype = nm_ip_tunnel_mode_get_type, - .min = NM_IP_TUNNEL_MODE_UNKNOWN + 1, - .max = G_MAXINT, + .property_type = &_pt_gobject_enum, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA ( + PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, + .get_gtype = nm_ip_tunnel_mode_get_type, + .min = NM_IP_TUNNEL_MODE_UNKNOWN + 1, + .max = G_MAXINT, + ), + .typ_flags = NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PARSABLE_TEXT + | NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PRETTY_TEXT, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_IP_TUNNEL_PARENT, From ebdc7a6ce7815a67507415f834828c0b6c4dbde1 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 14:46:31 +0200 Subject: [PATCH 12/25] clients: implement proxy.method property as enum --- clients/common/nm-meta-setting-desc.c | 55 ++++----------------------- 1 file changed, 7 insertions(+), 48 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index cba64c2c78..c1c77bde59 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -3674,45 +3674,6 @@ _set_fcn_olpc_mesh_channel (ARGS_SET_FCN) return TRUE; } -static gconstpointer -_get_fcn_proxy_method (ARGS_GET_FCN) -{ - NMSettingProxy *s_proxy = NM_SETTING_PROXY (setting); - NMSettingProxyMethod method; - - RETURN_UNSUPPORTED_GET_TYPE (); - - method = nm_setting_proxy_get_method (s_proxy); - RETURN_STR_TO_FREE (nm_utils_enum_to_str (nm_setting_proxy_method_get_type (), method)); -} - -static gboolean -_set_fcn_proxy_method (ARGS_SET_FCN) -{ - int method; - gboolean ret; - - ret = nm_utils_enum_from_str (nm_setting_proxy_method_get_type(), value, - &method, NULL); - - if (!ret) { - gs_free const char **values = NULL; - gs_free char *values_str = NULL; - - values = nm_utils_enum_get_values (nm_setting_proxy_method_get_type (), - NM_SETTING_PROXY_METHOD_NONE, - G_MAXINT); - values_str = g_strjoinv (",", (char **) values); - g_set_error (error, 1, 0, _("invalid method '%s', use one of %s"), - value, values_str); - - return FALSE; - } - - g_object_set (setting, property_info->property_name, method, NULL); - return TRUE; -} - static gboolean _set_fcn_proxy_pac_script (ARGS_SET_FCN) { @@ -5973,15 +5934,13 @@ static const NMMetaPropertyInfo *const property_infos_PROXY[] = { .property_alias = "method", .prompt = NM_META_TEXT_PROMPT_PROXY_METHOD, .def_hint = NM_META_TEXT_PROMPT_PROXY_METHOD_CHOICES, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_proxy_method, - .set_fcn = _set_fcn_proxy_method, - .values_fcn = _values_fcn_gobject_enum, - ), - .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, - .get_gtype = nm_setting_proxy_method_get_type, - .min = NM_SETTING_PROXY_METHOD_NONE, - .max = G_MAXINT, + .property_type = &_pt_gobject_enum, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA ( + PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, + .get_gtype = nm_setting_proxy_method_get_type, + ), + .typ_flags = NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PARSABLE_TEXT + | NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PRETTY_TEXT, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_PROXY_BROWSER_ONLY, From bd180e489d36475734e724ce43a49c335425a655 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 14:47:54 +0200 Subject: [PATCH 13/25] clients: implement macsec.validation property as enum --- clients/common/nm-meta-setting-desc.c | 47 ++++----------------------- 1 file changed, 7 insertions(+), 40 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index c1c77bde59..28289b205a 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -3608,39 +3608,6 @@ _set_fcn_macsec_mode (ARGS_SET_FCN) return TRUE; } -static gconstpointer -_get_fcn_macsec_validation (ARGS_GET_FCN) -{ - NMSettingMacsec *s_macsec = NM_SETTING_MACSEC (setting); - NMSettingMacsecValidation validation; - - RETURN_UNSUPPORTED_GET_TYPE (); - - validation = nm_setting_macsec_get_validation (s_macsec); - RETURN_STR_TO_FREE (nm_utils_enum_to_str (nm_setting_macsec_validation_get_type (), validation)); -} - -static gboolean -_set_fcn_macsec_validation (ARGS_SET_FCN) -{ - NMSettingMacsecMode validation; - gs_free char *options = NULL; - - if (!nm_utils_enum_from_str (nm_setting_macsec_validation_get_type (), value, - (int *) &validation, NULL)) { - options = g_strjoinv (",", - (char **) nm_utils_enum_get_values (nm_setting_macsec_validation_get_type (), - G_MININT, - G_MAXINT)); - g_set_error (error, 1, 0, _("invalid option '%s', use one of [%s]"), - value, options); - return FALSE; - } - - g_object_set (setting, property_info->property_name, validation, NULL); - return TRUE; -} - static gconstpointer _get_fcn_olpc_mesh_ssid (ARGS_GET_FCN) { @@ -5757,13 +5724,13 @@ static const NMMetaPropertyInfo *const property_infos_MACSEC[] = { .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_MACSEC_VALIDATION, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_macsec_validation, - .set_fcn = _set_fcn_macsec_validation, - .values_fcn = _values_fcn_gobject_enum, - ), - .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, - .get_gtype = nm_setting_macsec_validation_get_type, + .property_type = &_pt_gobject_enum, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA ( + PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, + .get_gtype = nm_setting_macsec_validation_get_type, + ), + .typ_flags = NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PARSABLE_TEXT + | NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PRETTY_TEXT, ), ), NULL From 6c4b12418b66a3f8ec2a0e0422f9eae8eb6e5982 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 15:28:56 +0200 Subject: [PATCH 14/25] clients: implement macsec.mode property as enum --- clients/common/nm-meta-setting-desc.c | 47 ++++----------------------- 1 file changed, 7 insertions(+), 40 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 28289b205a..5293a0895b 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -3575,39 +3575,6 @@ _set_fcn_ip6_config_ip6_privacy (ARGS_SET_FCN) return TRUE; } -static gconstpointer -_get_fcn_macsec_mode (ARGS_GET_FCN) -{ - NMSettingMacsec *s_macsec = NM_SETTING_MACSEC (setting); - NMSettingMacsecMode mode; - - RETURN_UNSUPPORTED_GET_TYPE (); - - mode = nm_setting_macsec_get_mode (s_macsec); - RETURN_STR_TO_FREE (nm_utils_enum_to_str (nm_setting_macsec_mode_get_type (), mode)); -} - -static gboolean -_set_fcn_macsec_mode (ARGS_SET_FCN) -{ - NMSettingMacsecMode mode; - gs_free char *options = NULL; - - if (!nm_utils_enum_from_str (nm_setting_macsec_mode_get_type (), value, - (int *) &mode, NULL)) { - options = g_strjoinv (",", - (char **) nm_utils_enum_get_values (nm_setting_macsec_mode_get_type (), - G_MININT, - G_MAXINT)); - g_set_error (error, 1, 0, _("invalid option '%s', use one of [%s]"), - value, options); - return FALSE; - } - - g_object_set (setting, property_info->property_name, mode, NULL); - return TRUE; -} - static gconstpointer _get_fcn_olpc_mesh_ssid (ARGS_GET_FCN) { @@ -5686,13 +5653,13 @@ static const NMMetaPropertyInfo *const property_infos_MACSEC[] = { .inf_flags = NM_META_PROPERTY_INF_FLAG_REQD, .prompt = NM_META_TEXT_PROMPT_MACSEC_MODE, .def_hint = NM_META_TEXT_PROMPT_MACSEC_MODE_CHOICES, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_macsec_mode, - .set_fcn = _set_fcn_macsec_mode, - .values_fcn = _values_fcn_gobject_enum, - ), - .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, - .get_gtype = nm_setting_macsec_mode_get_type, + .property_type = &_pt_gobject_enum, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA ( + PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, + .get_gtype = nm_setting_macsec_mode_get_type, + ), + .typ_flags = NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PARSABLE_TEXT + | NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PRETTY_TEXT, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_MACSEC_ENCRYPT, From 35d6802724106f439bf8bfbbeb27d19e0403268a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 16:15:29 +0200 Subject: [PATCH 15/25] clients: add validate_fcn for string property --- clients/common/nm-meta-setting-desc.c | 36 ++++++++++++++++----------- clients/common/nm-meta-setting-desc.h | 3 +++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 5293a0895b..77965fbb4c 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -764,13 +764,20 @@ _get_fcn_gobject_enum (ARGS_GET_FCN) static gboolean _set_fcn_gobject_string (ARGS_SET_FCN) { - if ( property_info->property_typ_data - && property_info->property_typ_data->values_static) { - value = nmc_string_is_valid (value, - (const char **) property_info->property_typ_data->values_static, - error); - if (!value) - return FALSE; + gs_free char *to_free = NULL; + + if (property_info->property_typ_data) { + if (property_info->property_typ_data->subtype.gobject_string.validate_fcn) { + value = property_info->property_typ_data->subtype.gobject_string.validate_fcn (value, &to_free, error); + if (!value) + return FALSE; + } else if (property_info->property_typ_data->values_static) { + value = nmc_string_is_valid (value, + (const char **) property_info->property_typ_data->values_static, + error); + if (!value) + return FALSE; + } } g_object_set (setting, property_info->property_name, value, NULL); return TRUE; @@ -4284,15 +4291,14 @@ _set_fcn_wireless_security_wep_key_type (ARGS_SET_FCN) return TRUE; } -static gboolean -_set_fcn_wireless_security_psk (ARGS_SET_FCN) +static const char * +_validate_fcn_wireless_security_psk (const char *value, char **out_to_free, GError **error) { if (!nm_utils_wpa_psk_valid (value)) { g_set_error (error, 1, 0, _("'%s' is not a valid PSK"), value); - return FALSE; + return NULL; } - g_object_set (setting, property_info->property_name, value, NULL); - return TRUE; + return value; } /*****************************************************************************/ @@ -6516,9 +6522,9 @@ static const NMMetaPropertyInfo *const property_infos_WIRELESS_SECURITY[] = { ), PROPERTY_INFO_WITH_DESC (NM_SETTING_WIRELESS_SECURITY_PSK, .is_secret = TRUE, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_gobject, - .set_fcn = _set_fcn_wireless_security_psk, + .property_type = &_pt_gobject_string, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_string, + .validate_fcn = _validate_fcn_wireless_security_psk, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_WIRELESS_SECURITY_PSK_FLAGS, diff --git a/clients/common/nm-meta-setting-desc.h b/clients/common/nm-meta-setting-desc.h index f46b86a010..ea77bf7ee2 100644 --- a/clients/common/nm-meta-setting-desc.h +++ b/clients/common/nm-meta-setting-desc.h @@ -236,6 +236,9 @@ struct _NMMetaPropertyTypData { int max; const struct _NMUtilsEnumValueInfo *value_infos; } gobject_enum; + struct { + const char *(*validate_fcn) (const char *value, char **out_to_free, GError **error); + } gobject_string; struct { guint32 (*get_fcn) (NMSetting *setting); } mtu; From b6d9bdcee86fc6d52b2aa1bc8a0dcfa64bec86e8 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 19:18:04 +0200 Subject: [PATCH 16/25] clients: combine int property setters Instead of having 3 implementations for setting an int (int, uint, int64), combine them. Also, make them more configurable by allowing to specify min/max/base, outside of GParamSpec. --- clients/common/nm-meta-setting-desc.c | 293 ++++++++++++-------------- clients/common/nm-meta-setting-desc.h | 5 + 2 files changed, 142 insertions(+), 156 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 77965fbb4c..9ef8f31fd1 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -34,9 +34,6 @@ /*****************************************************************************/ -static gboolean validate_int (NMSetting *setting, const char* prop, gint val, GError **error); -static gboolean validate_uint (NMSetting *setting, const char* prop, guint val, GError **error); -static gboolean validate_int64 (NMSetting *setting, const char* prop, gint64 val, GError **error); static char *secret_flags_to_string (guint32 flags, NMMetaAccessorGetType get_type); #define ALL_SECRET_FLAGS \ @@ -795,72 +792,109 @@ _set_fcn_gobject_bool (ARGS_SET_FCN) return TRUE; } +static gboolean +_set_fcn_gobject_int_impl (const NMMetaPropertyInfo *property_info, + NMSetting *setting, + const char *value, + GError **error) +{ + int errsv; + const GParamSpec *pspec; + nm_auto_unset_gvalue GValue gval = G_VALUE_INIT; + gint64 v; + gboolean has_minmax = FALSE; + gint64 min = G_MININT64; + gint64 max = G_MAXINT64; + guint base = 10; + + if (property_info->property_typ_data) { + if (property_info->property_typ_data->subtype.gobject_int.base > 0) + base = property_info->property_typ_data->subtype.gobject_int.base; + if ( property_info->property_typ_data->subtype.gobject_int.min + || property_info->property_typ_data->subtype.gobject_int.max) { + min = property_info->property_typ_data->subtype.gobject_int.min; + max = property_info->property_typ_data->subtype.gobject_int.max; + has_minmax = TRUE; + } + } + + pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (setting)), property_info->property_name); + if (!G_IS_PARAM_SPEC (pspec)) + g_return_val_if_reached (FALSE); + switch (pspec->value_type) { + case G_TYPE_INT: + if (!has_minmax) { + const GParamSpecInt *p = (GParamSpecInt *) pspec; + + min = p->minimum; + max = p->maximum; + } + break; + case G_TYPE_UINT: + if (!has_minmax) { + const GParamSpecUInt *p = (GParamSpecUInt *) pspec; + + min = p->minimum; + max = p->maximum; + } + break; + case G_TYPE_INT64: + if (!has_minmax) { + const GParamSpecInt64 *p = (GParamSpecInt64 *) pspec; + + min = p->minimum; + max = p->maximum; + } + break; + default: + g_return_val_if_reached (FALSE); + } + + v = _nm_utils_ascii_str_to_int64 (value, base, min, max, 0); + if ((errsv = errno) != 0) { + if (errsv == ERANGE) { + g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, + _("'%s' is out of range [%lli, %lli]"), + value, + (long long) min, + (long long) max); + } else { + g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, + _("'%s' is not a valid number"), value); + } + return FALSE; + } + + g_value_init (&gval, pspec->value_type); + switch (pspec->value_type) { + case G_TYPE_INT: + g_value_set_int (&gval, v); + break; + case G_TYPE_UINT: + g_value_set_uint (&gval, v); + break; + case G_TYPE_INT64: + g_value_set_int64 (&gval, v); + break; + default: + nm_assert_not_reached (); + break; + } + + /* Validate the number according to the property spec */ + if (!nm_g_object_set_property (G_OBJECT (setting), + property_info->property_name, + &gval, + error)) + g_return_val_if_reached (FALSE); + + return TRUE; +} + static gboolean _set_fcn_gobject_int (ARGS_SET_FCN) { - const gint64 INVALID = G_MININT64; - gint64 v; - - v = _nm_utils_ascii_str_to_int64 (value, 10, G_MININT, G_MAXINT, INVALID); - if (v == INVALID) { - g_set_error (error, 1, 0, _("'%s' is not a valid number (or out of range)"), value); - return FALSE; - } - - /* Validate the number according to the property spec */ - if (!validate_int (setting, property_info->property_name, v, error)) - return FALSE; - - g_object_set (setting, property_info->property_name, (int) v, NULL); - return TRUE; -} - -static gboolean -_set_fcn_gobject_int64 (ARGS_SET_FCN) -{ - gint64 v; - - v = _nm_utils_ascii_str_to_int64 (value, 10, G_MININT64, G_MAXINT64, 0); - if (errno) { - g_set_error (error, 1, 0, _("'%s' is not a valid number (or out of range)"), value); - return FALSE; - } - - /* Validate the number according to the property spec */ - if (!validate_int64 (setting, property_info->property_name, v, error)) - return FALSE; - - g_object_set (setting, property_info->property_name, v, NULL); - return TRUE; -} - -static gboolean -_set_fcn_gobject_uint_impl (const NMMetaPropertyInfo *property_info, - NMSetting *setting, - const char *value, - GError **error) -{ - unsigned long val_int; - - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - - if (!nmc_string_to_uint (value, TRUE, 0, G_MAXUINT, &val_int)) { - g_set_error (error, 1, 0, _("'%s' is not a valid number (or out of range)"), value); - return FALSE; - } - - /* Validate the number according to the property spec */ - if (!validate_uint (setting, property_info->property_name, (guint) val_int, error)) - return FALSE; - - g_object_set (setting, property_info->property_name, (guint) val_int, NULL); - return TRUE; -} - -static gboolean -_set_fcn_gobject_uint (ARGS_SET_FCN) -{ - return _set_fcn_gobject_uint_impl (property_info, setting, value, error); + return _set_fcn_gobject_int_impl (property_info, setting, value, error); } static gboolean @@ -868,7 +902,7 @@ _set_fcn_gobject_mtu (ARGS_SET_FCN) { if (nm_streq0 (value, "auto")) value = "0"; - return _set_fcn_gobject_uint_impl (property_info, setting, value, error); + return _set_fcn_gobject_int_impl (property_info, setting, value, error); } static gboolean @@ -1455,49 +1489,6 @@ validate_int (NMSetting *setting, const char* prop, gint val, GError **error) return success; } -static gboolean -validate_int64 (NMSetting *setting, const char* prop, gint64 val, GError **error) -{ - GParamSpec *pspec; - GValue value = G_VALUE_INIT; - gboolean success = TRUE; - - g_value_init (&value, G_TYPE_INT64); - g_value_set_int64 (&value, val); - pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (setting)), prop); - g_assert (G_IS_PARAM_SPEC (pspec)); - if (g_param_value_validate (pspec, &value)) { - GParamSpecInt64 *pspec_int = (GParamSpecInt64 *) pspec; - G_STATIC_ASSERT (sizeof (long long) >= sizeof (gint64)); - g_set_error (error, 1, 0, _("'%lld' is not valid; use <%lld-%lld>"), - (long long) val, (long long) pspec_int->minimum, (long long) pspec_int->maximum); - success = FALSE; - } - g_value_unset (&value); - return success; -} - -static gboolean -validate_uint (NMSetting *setting, const char* prop, guint val, GError **error) -{ - GParamSpec *pspec; - GValue value = G_VALUE_INIT; - gboolean success = TRUE; - - g_value_init (&value, G_TYPE_UINT); - g_value_set_uint (&value, val); - pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (setting)), prop); - g_assert (G_IS_PARAM_SPEC (pspec)); - if (g_param_value_validate (pspec, &value)) { - GParamSpecUInt *pspec_uint = (GParamSpecUInt *) pspec; - g_set_error (error, 1, 0, _("'%u' is not valid; use <%u-%u>"), - val, pspec_uint->minimum, pspec_uint->maximum); - success = FALSE; - } - g_value_unset (&value); - return success; -} - static char * flag_values_to_string (GFlagsValue *array, guint n) { @@ -4431,16 +4422,6 @@ static const NMMetaPropertyType _pt_gobject_int = { .set_fcn = _set_fcn_gobject_int, }; -static const NMMetaPropertyType _pt_gobject_int64 = { - .get_fcn = _get_fcn_gobject, - .set_fcn = _set_fcn_gobject_int64, -}; - -static const NMMetaPropertyType _pt_gobject_uint = { - .get_fcn = _get_fcn_gobject, - .set_fcn = _set_fcn_gobject_uint, -}; - static const NMMetaPropertyType _pt_gobject_mtu = { .get_fcn = _get_fcn_gobject_mtu, .set_fcn = _set_fcn_gobject_mtu, @@ -4799,10 +4780,10 @@ static const NMMetaPropertyInfo *const property_infos_ADSL[] = { ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_ADSL_VPI, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_ADSL_VCI, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), NULL }; @@ -4871,31 +4852,31 @@ static const NMMetaPropertyInfo *const property_infos_BRIDGE[] = { .is_cli_option = TRUE, .property_alias = "priority", .prompt = N_("STP priority [32768]"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_BRIDGE_FORWARD_DELAY, .is_cli_option = TRUE, .property_alias = "forward-delay", .prompt = N_("Forward delay [15]"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_BRIDGE_HELLO_TIME, .is_cli_option = TRUE, .property_alias = "hello-time", .prompt = N_("Hello time [2]"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_BRIDGE_MAX_AGE, .is_cli_option = TRUE, .property_alias = "max-age", .prompt = N_("Max age [20]"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_BRIDGE_AGEING_TIME, .is_cli_option = TRUE, .property_alias = "ageing-time", .prompt = N_("MAC address ageing time [300]"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_BRIDGE_MULTICAST_SNOOPING, .is_cli_option = TRUE, @@ -4913,13 +4894,13 @@ static const NMMetaPropertyInfo *const property_infos_BRIDGE_PORT[] = { .is_cli_option = TRUE, .property_alias = "priority", .prompt = N_("Bridge port priority [32]"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_BRIDGE_PORT_PATH_COST, .is_cli_option = TRUE, .property_alias = "path-cost", .prompt = N_("Bridge port STP path cost [100]"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_BRIDGE_PORT_HAIRPIN_MODE, .is_cli_option = TRUE, @@ -5078,7 +5059,7 @@ static const NMMetaPropertyInfo *const property_infos_CONNECTION[] = { ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_CONNECTION_GATEWAY_PING_TIMEOUT, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_CONNECTION_METERED, .describe_message = @@ -5409,7 +5390,7 @@ static const NMMetaPropertyInfo *const property_infos_IP4_CONFIG[] = { ), ), PROPERTY_INFO (NM_SETTING_IP_CONFIG_ROUTE_METRIC, DESCRIBE_DOC_NM_SETTING_IP4_CONFIG_ROUTE_METRIC, - .property_type = &_pt_gobject_int64, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, DESCRIBE_DOC_NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, .property_type = &_pt_gobject_bool, @@ -5536,7 +5517,7 @@ static const NMMetaPropertyInfo *const property_infos_IP6_CONFIG[] = { ), ), PROPERTY_INFO (NM_SETTING_IP_CONFIG_ROUTE_METRIC, DESCRIBE_DOC_NM_SETTING_IP6_CONFIG_ROUTE_METRIC, - .property_type = &_pt_gobject_int64, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, DESCRIBE_DOC_NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES, .property_type = &_pt_gobject_bool, @@ -5617,10 +5598,10 @@ static const NMMetaPropertyInfo *const property_infos_IP_TUNNEL[] = { .property_type = &_pt_gobject_string, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_IP_TUNNEL_TTL, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_IP_TUNNEL_TOS, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_IP_TUNNEL_PATH_MTU_DISCOVERY, .property_type = &_pt_gobject_bool, @@ -5632,10 +5613,10 @@ static const NMMetaPropertyInfo *const property_infos_IP_TUNNEL[] = { .property_type = &_pt_gobject_string, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_IP_TUNNEL_ENCAPSULATION_LIMIT, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_IP_TUNNEL_FLOW_LABEL, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_IP_TUNNEL_MTU, .property_type = &_pt_gobject_mtu, @@ -5846,10 +5827,10 @@ static const NMMetaPropertyInfo *const property_infos_PPP[] = { .property_type = &_pt_gobject_bool, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_PPP_BAUD, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_PPP_MRU, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_PPP_MTU, .property_type = &_pt_gobject_mtu, @@ -5858,10 +5839,10 @@ static const NMMetaPropertyInfo *const property_infos_PPP[] = { ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_PPP_LCP_ECHO_FAILURE, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_PPP_LCP_ECHO_INTERVAL, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), NULL }; @@ -5943,10 +5924,10 @@ static const NMMetaPropertyInfo *const property_infos_TEAM_PORT[] = { #define _CURRENT_NM_META_SETTING_TYPE NM_META_SETTING_TYPE_SERIAL static const NMMetaPropertyInfo *const property_infos_SERIAL[] = { PROPERTY_INFO_WITH_DESC (NM_SETTING_SERIAL_BAUD, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_SERIAL_BITS, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_SERIAL_PARITY, .property_type = DEFINE_PROPERTY_TYPE ( @@ -5955,10 +5936,10 @@ static const NMMetaPropertyInfo *const property_infos_SERIAL[] = { ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_SERIAL_STOPBITS, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_SERIAL_SEND_DELAY, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), NULL }; @@ -6026,7 +6007,7 @@ static const NMMetaPropertyInfo *const property_infos_VLAN[] = { .property_alias = "id", .inf_flags = NM_META_PROPERTY_INF_FLAG_REQD, .prompt = N_("VLAN ID (<0-4094>)"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_VLAN_FLAGS, .is_cli_option = TRUE, @@ -6100,7 +6081,7 @@ static const NMMetaPropertyInfo *const property_infos_VPN[] = { .property_type = &_pt_gobject_bool, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_VPN_TIMEOUT, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), NULL }; @@ -6119,7 +6100,7 @@ static const NMMetaPropertyInfo *const property_infos_VXLAN[] = { .property_alias = "id", .inf_flags = NM_META_PROPERTY_INF_FLAG_REQD, .prompt = N_("VXLAN ID"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_VXLAN_LOCAL, .is_cli_option = TRUE, @@ -6138,31 +6119,31 @@ static const NMMetaPropertyInfo *const property_infos_VXLAN[] = { .is_cli_option = TRUE, .property_alias = "source-port-min", .prompt = N_("Minimum source port [0]"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_VXLAN_SOURCE_PORT_MAX, .is_cli_option = TRUE, .property_alias = "source-port-max", .prompt = N_("Maximum source port [0]"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_VXLAN_DESTINATION_PORT, .is_cli_option = TRUE, .property_alias = "destination-port", .prompt = N_("Destination port [8472]"), - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_VXLAN_TOS, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_VXLAN_TTL, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_VXLAN_AGEING, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_VXLAN_LIMIT, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_VXLAN_LEARNING, .property_type = &_pt_gobject_bool, @@ -6212,7 +6193,7 @@ static const NMMetaPropertyInfo *const property_infos_WIRED[] = { .property_type = &_pt_gobject_readonly, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_WIRED_SPEED, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_WIRED_DUPLEX, .property_type = &_pt_gobject_string, @@ -6431,7 +6412,7 @@ static const NMMetaPropertyInfo *const property_infos_WIRELESS_SECURITY[] = { ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX, - .property_type = &_pt_gobject_uint, + .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, .property_type = &_pt_gobject_string, diff --git a/clients/common/nm-meta-setting-desc.h b/clients/common/nm-meta-setting-desc.h index ea77bf7ee2..2e0b03bd29 100644 --- a/clients/common/nm-meta-setting-desc.h +++ b/clients/common/nm-meta-setting-desc.h @@ -236,6 +236,11 @@ struct _NMMetaPropertyTypData { int max; const struct _NMUtilsEnumValueInfo *value_infos; } gobject_enum; + struct { + gint64 min; + gint64 max; + guint base; + } gobject_int; struct { const char *(*validate_fcn) (const char *value, char **out_to_free, GError **error); } gobject_string; From e086cf1887c9ed27da1ad2fc31f4a6a007fc6721 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 19:51:26 +0200 Subject: [PATCH 17/25] clients: add value_infos for int getter This allows to implement a pretty alias for numbers, like "default" for 0. --- clients/common/nm-meta-setting-desc.c | 89 ++++++++++++++++++++++++++- clients/common/nm-meta-setting-desc.h | 6 ++ 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 9ef8f31fd1..f4a1440ae2 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -589,6 +589,64 @@ _get_fcn_gobject (ARGS_GET_FCN) return _get_fcn_gobject_impl (property_info, setting, get_type, out_to_free); } +static gconstpointer +_get_fcn_gobject_int_impl (const NMMetaPropertyInfo *property_info, + NMSetting *setting, + NMMetaAccessorGetType get_type, + const NMMetaUtilsIntValueInfo *value_infos, + gpointer *out_to_free) +{ + const GParamSpec *pspec; + nm_auto_unset_gvalue GValue gval = G_VALUE_INIT; + gint64 v; + + RETURN_UNSUPPORTED_GET_TYPE (); + + pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (setting)), property_info->property_name); + if (!G_IS_PARAM_SPEC (pspec)) + g_return_val_if_reached (FALSE); + + g_value_init (&gval, pspec->value_type); + g_object_get_property (G_OBJECT (setting), property_info->property_name, &gval); + switch (pspec->value_type) { + case G_TYPE_INT: + v = g_value_get_int (&gval); + break; + case G_TYPE_UINT: + v = g_value_get_uint (&gval); + break; + case G_TYPE_INT64: + v = g_value_get_int64 (&gval); + break; + default: + g_return_val_if_reached (NULL); + break; + } + + if ( get_type == NM_META_ACCESSOR_GET_TYPE_PRETTY + && value_infos) { + for (; value_infos->nick; value_infos++) { + if (value_infos->value == v) { + RETURN_STR_TO_FREE (g_strdup_printf ("%lli (%s)", + (long long) v, + value_infos->nick)); + } + } + } + + RETURN_STR_TO_FREE (g_strdup_printf ("%"G_GINT64_FORMAT, v)); +} + +static gconstpointer +_get_fcn_gobject_int (ARGS_GET_FCN) +{ + return _get_fcn_gobject_int_impl (property_info, setting, get_type, + property_info->property_typ_data + ? property_info->property_typ_data->subtype.gobject_int.value_infos + : NULL, + out_to_free); +} + static gconstpointer _get_fcn_gobject_mtu (ARGS_GET_FCN) { @@ -801,13 +859,35 @@ _set_fcn_gobject_int_impl (const NMMetaPropertyInfo *property_info, int errsv; const GParamSpec *pspec; nm_auto_unset_gvalue GValue gval = G_VALUE_INIT; - gint64 v; + gint64 v = 0; gboolean has_minmax = FALSE; gint64 min = G_MININT64; gint64 max = G_MAXINT64; guint base = 10; + const NMMetaUtilsIntValueInfo *value_infos = NULL; + gboolean has_value = FALSE; if (property_info->property_typ_data) { + + if ( value + && (value_infos = property_info->property_typ_data->subtype.gobject_int.value_infos)) { + gs_free char *vv_stripped = NULL; + const char *vv = nm_str_skip_leading_spaces (value); + + if (vv[0] && g_ascii_isspace (vv[strlen (vv) - 1])) { + vv_stripped = g_strstrip (g_strdup (vv)); + vv = vv_stripped; + } + + for (; value_infos->nick; value_infos++) { + if (nm_streq (value_infos->nick, vv)) { + v = value_infos->value; + has_value = TRUE; + break; + } + } + } + if (property_info->property_typ_data->subtype.gobject_int.base > 0) base = property_info->property_typ_data->subtype.gobject_int.base; if ( property_info->property_typ_data->subtype.gobject_int.min @@ -850,7 +930,9 @@ _set_fcn_gobject_int_impl (const NMMetaPropertyInfo *property_info, g_return_val_if_reached (FALSE); } - v = _nm_utils_ascii_str_to_int64 (value, base, min, max, 0); + if (!has_value) + v = _nm_utils_ascii_str_to_int64 (value, base, min, max, 0); + if ((errsv = errno) != 0) { if (errsv == ERANGE) { g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, @@ -4418,7 +4500,7 @@ static const NMMetaPropertyType _pt_gobject_bool = { }; static const NMMetaPropertyType _pt_gobject_int = { - .get_fcn = _get_fcn_gobject, + .get_fcn = _get_fcn_gobject_int, .set_fcn = _set_fcn_gobject_int, }; @@ -4473,6 +4555,7 @@ static const NMMetaPropertyType _pt_gobject_devices = { #define VALUES_STATIC(...) (((const char *[]) { __VA_ARGS__, NULL })) #define ENUM_VALUE_INFOS(...) (((const NMUtilsEnumValueInfo []) { __VA_ARGS__, { 0 } })) +#define INT_VALUE_INFOS(...) (((const NMMetaUtilsIntValueInfo []) { __VA_ARGS__, { 0 } })) #define GET_FCN_WITH_DEFAULT(type, func) \ /* macro that returns @func as const (gboolean(*)(NMSetting*)) type, but checks diff --git a/clients/common/nm-meta-setting-desc.h b/clients/common/nm-meta-setting-desc.h index 2e0b03bd29..e3a3848255 100644 --- a/clients/common/nm-meta-setting-desc.h +++ b/clients/common/nm-meta-setting-desc.h @@ -225,6 +225,11 @@ struct _NMMetaPropertyType { struct _NMUtilsEnumValueInfo; +typedef struct { + const char *nick; + gint64 value; +} NMMetaUtilsIntValueInfo; + struct _NMMetaPropertyTypData { union { struct { @@ -240,6 +245,7 @@ struct _NMMetaPropertyTypData { gint64 min; gint64 max; guint base; + const NMMetaUtilsIntValueInfo *value_infos; } gobject_int; struct { const char *(*validate_fcn) (const char *value, char **out_to_free, GError **error); From 14aefa1d7f9c9b3e19d7331a24fba93594bf0743 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 20:08:22 +0200 Subject: [PATCH 18/25] clients: implement dcb priorities as int property --- clients/common/nm-meta-setting-desc.c | 98 ++++++--------------------- 1 file changed, 22 insertions(+), 76 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index f4a1440ae2..5f37669bae 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -647,6 +647,20 @@ _get_fcn_gobject_int (ARGS_GET_FCN) out_to_free); } +static gconstpointer +_get_fcn_gobject_dcb_priority (ARGS_GET_FCN) +{ + static const NMMetaUtilsIntValueInfo value_infos[] = { + { + .value = -1, + .nick = "unset", + }, + { 0 }, + }; + + return _get_fcn_gobject_int_impl (property_info, setting, get_type, value_infos, out_to_free); +} + static gconstpointer _get_fcn_gobject_mtu (ARGS_GET_FCN) { @@ -1550,27 +1564,6 @@ verify_string_list (char **strv, return TRUE; } -static gboolean -validate_int (NMSetting *setting, const char* prop, gint val, GError **error) -{ - GParamSpec *pspec; - GValue value = G_VALUE_INIT; - gboolean success = TRUE; - - g_value_init (&value, G_TYPE_INT); - g_value_set_int (&value, val); - pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (setting)), prop); - g_assert (G_IS_PARAM_SPEC (pspec)); - if (g_param_value_validate (pspec, &value)) { - GParamSpecInt *pspec_int = (GParamSpecInt *) pspec; - g_set_error (error, 1, 0, _("'%d' is not valid; use <%d-%d>"), - val, pspec_int->minimum, pspec_int->maximum); - success = FALSE; - } - g_value_unset (&value); - return success; -} - static char * flag_values_to_string (GFlagsValue *array, guint n) { @@ -2621,27 +2614,6 @@ dcb_flags_to_string (NMSettingDcbFlags flags) RETURN_STR_TO_FREE (dcb_flags_to_string (v)); \ } -static char * -dcb_app_priority_to_string (gint priority) -{ - return (priority == -1) ? g_strdup (_("-1 (unset)")) : g_strdup_printf ("%d", priority); -} - -#define DEFINE_DCB_APP_PRIORITY_GETTER(func_name, property_name) \ - static gconstpointer \ - func_name (ARGS_GET_FCN) \ - { \ - int v; \ - GValue val = G_VALUE_INIT; \ - \ - RETURN_UNSUPPORTED_GET_TYPE (); \ - g_value_init (&val, G_TYPE_INT); \ - g_object_get_property (G_OBJECT (setting), property_name, &val); \ - v = g_value_get_int (&val); \ - g_value_unset (&val); \ - RETURN_STR_TO_FREE (dcb_app_priority_to_string (v)); \ - } - #define DEFINE_DCB_BOOL_GETTER(func_name, getter_func_name) \ static gconstpointer \ func_name (ARGS_GET_FCN) \ @@ -2686,11 +2658,8 @@ dcb_app_priority_to_string (gint priority) } DEFINE_DCB_FLAGS_GETTER (_get_fcn_dcb_app_fcoe_flags, NM_SETTING_DCB_APP_FCOE_FLAGS) -DEFINE_DCB_APP_PRIORITY_GETTER (_get_fcn_dcb_app_fcoe_priority, NM_SETTING_DCB_APP_FCOE_PRIORITY) DEFINE_DCB_FLAGS_GETTER (_get_fcn_dcb_app_iscsi_flags, NM_SETTING_DCB_APP_ISCSI_FLAGS) -DEFINE_DCB_APP_PRIORITY_GETTER (_get_fcn_dcb_app_iscsi_priority, NM_SETTING_DCB_APP_ISCSI_PRIORITY) DEFINE_DCB_FLAGS_GETTER (_get_fcn_dcb_app_fip_flags, NM_SETTING_DCB_APP_FIP_FLAGS) -DEFINE_DCB_APP_PRIORITY_GETTER (_get_fcn_dcb_app_fip_priority, NM_SETTING_DCB_APP_FIP_PRIORITY) DEFINE_DCB_FLAGS_GETTER (_get_fcn_dcb_priority_flow_control_flags, NM_SETTING_DCB_PRIORITY_FLOW_CONTROL_FLAGS) DEFINE_DCB_BOOL_GETTER (_get_fcn_dcb_priority_flow_control, nm_setting_dcb_get_priority_flow_control) @@ -2753,25 +2722,6 @@ _set_fcn_dcb_flags (ARGS_SET_FCN) return TRUE; } -static gboolean -_set_fcn_dcb_priority (ARGS_SET_FCN) -{ - const int INVALID = G_MININT; - int v; - - v = _nm_utils_ascii_str_to_int64 (value, 10, -1, 7, INVALID); - if (v == INVALID) { - g_set_error (error, 1, 0, _("'%s' is not a DCB app priority"), value); - return FALSE; - } - - if (!validate_int (setting, property_info->property_name, v, error)) - return FALSE; - - g_object_set (setting, property_info->property_name, v, NULL); - return TRUE; -} - static gboolean dcb_parse_uint_array (const char *val, guint max, @@ -4509,6 +4459,11 @@ static const NMMetaPropertyType _pt_gobject_mtu = { .set_fcn = _set_fcn_gobject_mtu, }; +static const NMMetaPropertyType _pt_gobject_dcb_priority = { + .get_fcn = _get_fcn_gobject_dcb_priority, + .set_fcn = _set_fcn_gobject_int, +}; + static const NMMetaPropertyType _pt_gobject_mac = { .get_fcn = _get_fcn_gobject, .set_fcn = _set_fcn_gobject_mac, @@ -5188,10 +5143,7 @@ static const NMMetaPropertyInfo *const property_infos_DCB[] = { ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_DCB_APP_FCOE_PRIORITY, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_dcb_app_fcoe_priority, - .set_fcn = _set_fcn_dcb_priority, - ), + .property_type = &_pt_gobject_dcb_priority, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_DCB_APP_FCOE_MODE, .property_type = &_pt_gobject_string, @@ -5207,10 +5159,7 @@ static const NMMetaPropertyInfo *const property_infos_DCB[] = { ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_DCB_APP_ISCSI_PRIORITY, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_dcb_app_iscsi_priority, - .set_fcn = _set_fcn_dcb_priority, - ), + .property_type = &_pt_gobject_dcb_priority, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_DCB_APP_FIP_FLAGS, .property_type = DEFINE_PROPERTY_TYPE ( @@ -5219,10 +5168,7 @@ static const NMMetaPropertyInfo *const property_infos_DCB[] = { ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_DCB_APP_FIP_PRIORITY, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_dcb_app_fip_priority, - .set_fcn = _set_fcn_dcb_priority, - ), + .property_type = &_pt_gobject_dcb_priority, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_DCB_PRIORITY_FLOW_CONTROL_FLAGS, .property_type = DEFINE_PROPERTY_TYPE ( From df388ea7d5e79942472a72f27eff47acc690849f Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 20:19:00 +0200 Subject: [PATCH 19/25] clients/trivial: rename nmc_vlan_parse_priority_maps function --- clients/common/nm-meta-setting-desc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 5f37669bae..17fd17fa6c 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -252,9 +252,9 @@ _parse_ip_route (int family, * Caller is responsible for freeing the array. */ static char ** -nmc_vlan_parse_priority_maps (const char *priority_map, - NMVlanPriorityMap map_type, - GError **error) +_parse_vlan_priority_maps (const char *priority_map, + NMVlanPriorityMap map_type, + GError **error) { char **mapping = NULL, **iter; unsigned long from, to, from_max, to_max; @@ -3738,7 +3738,7 @@ _set_vlan_xgress_priority_map (NMSetting *setting, { char **prio_map, **p; - prio_map = nmc_vlan_parse_priority_maps (value, map_type, error); + prio_map = _parse_vlan_priority_maps (value, map_type, error); if (!prio_map) return FALSE; @@ -3779,7 +3779,7 @@ _remove_vlan_xgress_priority_map (const NMMetaEnvironment *environment, char **prio_map; gs_free char *v = g_strdup (value); - prio_map = nmc_vlan_parse_priority_maps (v, map_type, error); + prio_map = _parse_vlan_priority_maps (v, map_type, error); if (!prio_map) return FALSE; if (prio_map[1]) { From 6c4428324ebdea13a4075d6ff5de5a5e5e789e75 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 20:25:02 +0200 Subject: [PATCH 20/25] clients: implement proxy.pac-script as string property ... with separate validation function. --- clients/common/nm-meta-setting-desc.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 17fd17fa6c..3550baee07 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -3638,19 +3638,14 @@ _set_fcn_olpc_mesh_channel (ARGS_SET_FCN) return TRUE; } -static gboolean -_set_fcn_proxy_pac_script (ARGS_SET_FCN) +static const char * +_validate_fcn_proxy_pac_script (const char *value, char **out_to_free, GError **error) { char *script = NULL; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - - if (!nmc_proxy_check_script (value, &script, error)) { - return FALSE; - } - g_object_set (setting, property_info->property_name, script, NULL); - g_free (script); - return TRUE; + if (!nmc_proxy_check_script (value, &script, error)) + return NULL; + RETURN_STR_TO_FREE (script); } static gconstpointer @@ -5909,9 +5904,9 @@ static const NMMetaPropertyInfo *const property_infos_PROXY[] = { .is_cli_option = TRUE, .property_alias = "pac-script", .prompt = N_("PAC script"), - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_gobject, - .set_fcn = _set_fcn_proxy_pac_script, + .property_type = &_pt_gobject_string, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_string, + .validate_fcn = _validate_fcn_proxy_pac_script, ), ), NULL From 9a68123827a8c4fe1eeaaf003d365429441d97e9 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 21:55:44 +0200 Subject: [PATCH 21/25] clients: implement serial.parity as enum property Previously we would only consider the first character of value. For example, "Nonsense" was also allowed. Don't do that anymore. --- clients/common/nm-meta-setting-desc.c | 74 ++++++++++++--------------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 3550baee07..2882655380 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -3648,44 +3648,6 @@ _validate_fcn_proxy_pac_script (const char *value, char **out_to_free, GError ** RETURN_STR_TO_FREE (script); } -static gconstpointer -_get_fcn_serial_parity (ARGS_GET_FCN) -{ - NMSettingSerial *s_serial = NM_SETTING_SERIAL (setting); - - RETURN_UNSUPPORTED_GET_TYPE (); - - switch (nm_setting_serial_get_parity (s_serial)) { - case NM_SETTING_SERIAL_PARITY_EVEN: - return "even"; - case NM_SETTING_SERIAL_PARITY_ODD: - return "odd"; - default: - case NM_SETTING_SERIAL_PARITY_NONE: - return "none"; - } -} - -static gboolean -_set_fcn_serial_parity (ARGS_SET_FCN) -{ - NMSettingSerialParity parity; - - if (value[0] == 'E' || value[0] == 'e') - parity = NM_SETTING_SERIAL_PARITY_EVEN; - else if (value[0] == 'O' || value[0] == 'o') - parity = NM_SETTING_SERIAL_PARITY_ODD; - else if (value[0] == 'N' || value[0] == 'n') - parity = NM_SETTING_SERIAL_PARITY_NONE; - else { - g_set_error (error, 1, 0, _("'%s' is not valid; use [e, o, n]"), value); - return FALSE; - } - - g_object_set (setting, property_info->property_name, parity, NULL); - return TRUE; -} - static gboolean _set_fcn_team_config (ARGS_SET_FCN) { @@ -5954,9 +5916,39 @@ static const NMMetaPropertyInfo *const property_infos_SERIAL[] = { .property_type = &_pt_gobject_int, ), PROPERTY_INFO_WITH_DESC (NM_SETTING_SERIAL_PARITY, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_serial_parity, - .set_fcn = _set_fcn_serial_parity, + .property_type = &_pt_gobject_enum, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA ( + PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, + .get_gtype = nm_setting_serial_parity_get_type, + .value_infos = ENUM_VALUE_INFOS ( + { + .value = NM_SETTING_SERIAL_PARITY_EVEN, + .nick = "E", + }, + { + .value = NM_SETTING_SERIAL_PARITY_EVEN, + .nick = "e", + }, + { + .value = NM_SETTING_SERIAL_PARITY_ODD, + .nick = "O", + }, + { + .value = NM_SETTING_SERIAL_PARITY_ODD, + .nick = "o", + }, + { + .value = NM_SETTING_SERIAL_PARITY_NONE, + .nick = "N", + }, + { + .value = NM_SETTING_SERIAL_PARITY_NONE, + .nick = "n", + } + ), + ), + .typ_flags = NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PARSABLE_TEXT + | NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PRETTY_TEXT, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_SERIAL_STOPBITS, From cfc9f5a9fd0e3cc91cf431cf4971b65dbfee002b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 21 May 2017 22:00:27 +0200 Subject: [PATCH 22/25] clients: implement team.config as string property ... with separate validation function. --- clients/common/nm-meta-setting-desc.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 2882655380..530c8ac05d 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -3648,19 +3648,14 @@ _validate_fcn_proxy_pac_script (const char *value, char **out_to_free, GError ** RETURN_STR_TO_FREE (script); } -static gboolean -_set_fcn_team_config (ARGS_SET_FCN) +static const char * +_validate_fcn_team_config (const char *value, char **out_to_free, GError **error) { char *json = NULL; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - - if (!nmc_team_check_config (value, &json, error)) { - return FALSE; - } - g_object_set (setting, property_info->property_name, json, NULL); - g_free (json); - return TRUE; + if (!nmc_team_check_config (value, &json, error)) + return NULL; + RETURN_STR_TO_FREE (json); } static gconstpointer @@ -5882,9 +5877,9 @@ static const NMMetaPropertyInfo *const property_infos_TEAM[] = { .property_alias = "config", .prompt = N_("Team JSON configuration [none]"), .describe_message = TEAM_DESCRIBE_MESSAGE, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_gobject, - .set_fcn = _set_fcn_team_config, + .property_type = &_pt_gobject_string, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_string, + .validate_fcn = _validate_fcn_team_config, ), ), NULL @@ -5898,9 +5893,9 @@ static const NMMetaPropertyInfo *const property_infos_TEAM_PORT[] = { .property_alias = "config", .prompt = N_("Team JSON configuration [none]"), .describe_message = TEAM_DESCRIBE_MESSAGE, - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_gobject, - .set_fcn = _set_fcn_team_config, + .property_type = &_pt_gobject_string, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA_SUBTYPE (gobject_string, + .validate_fcn = _validate_fcn_team_config, ), ), NULL From d6bdf6d9dc0a8c932b47ae14c5df7e39b1dfd619 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 23 May 2017 11:24:01 +0200 Subject: [PATCH 23/25] clients: fix error message for setting flags enums The min/max for flags must be unsigned. We need to keep track of whether we have flags or an enum, and adjust min/max. --- clients/common/nm-meta-setting-desc.c | 51 ++++++++++++++++++--------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 530c8ac05d..57432f7478 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -1071,6 +1071,7 @@ _set_fcn_gobject_enum (ARGS_SET_FCN) gboolean has_gtype = FALSE; nm_auto_unset_gvalue GValue gval = G_VALUE_INIT; nm_auto_unref_gtypeclass GTypeClass *gtype_class = NULL; + gboolean is_flags; int v; if (property_info->property_typ_data) { @@ -1082,21 +1083,24 @@ _set_fcn_gobject_enum (ARGS_SET_FCN) gtype_prop = _gobject_property_get_gtype (G_OBJECT (setting), property_info->property_name); - if ( gtype_prop == G_TYPE_INT - || gtype_prop == G_TYPE_UINT) { - if (!has_gtype) - g_return_val_if_reached (FALSE); - } else if (G_TYPE_IS_CLASSED (gtype_prop)) { - gtype_class = g_type_class_ref (gtype_prop); - if ( !G_IS_ENUM_CLASS (gtype_class) - && !G_IS_FLAGS_CLASS (gtype_class)) - g_return_val_if_reached (FALSE); + if ( has_gtype + && NM_IN_SET (gtype_prop, + G_TYPE_INT, + G_TYPE_UINT) + && G_TYPE_IS_CLASSED (gtype) + && (gtype_class = g_type_class_ref (gtype)) + && ( (is_flags = G_IS_FLAGS_CLASS (gtype_class)) + || G_IS_ENUM_CLASS (gtype_class))) { + /* valid */ + } else if ( !has_gtype + && G_TYPE_IS_CLASSED (gtype_prop) + && (gtype_class = g_type_class_ref (gtype_prop)) + && ( (is_flags = G_IS_FLAGS_CLASS (gtype_class)) + || G_IS_ENUM_CLASS (gtype_class))) { + gtype = gtype_prop; } else g_return_val_if_reached (FALSE); - if (!has_gtype) - gtype = gtype_prop; - if (!_nm_utils_enum_from_str_full (gtype, value, &v, NULL, property_info->property_typ_data ? property_info->property_typ_data->subtype.gobject_enum.value_infos @@ -1124,6 +1128,7 @@ fail: if (error) { gs_free const char **valid_all = NULL; gs_free const char *valid_str = NULL; + gboolean has_minmax = FALSE; int min = G_MININT; int max = G_MAXINT; @@ -1132,14 +1137,28 @@ fail: || property_info->property_typ_data->subtype.gobject_enum.max) { min = property_info->property_typ_data->subtype.gobject_enum.min; max = property_info->property_typ_data->subtype.gobject_enum.max; + has_minmax = TRUE; } } + + if (!has_minmax && is_flags) { + min = 0; + max = (gint) G_MAXUINT; + } + valid_all = nm_utils_enum_get_values (gtype, min, max); valid_str = g_strjoinv (",", (char **) valid_all); - g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, - _("invalid option '%s', use one of [%s]"), - value, - valid_str); + if (is_flags) { + g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, + _("invalid option '%s', use a combination of [%s]"), + value, + valid_str); + } else { + g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT, + _("invalid option '%s', use one of [%s]"), + value, + valid_str); + } } return FALSE; } From 8921e5db714ecd122b877bcc3b9531e458d5a686 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 23 May 2017 12:46:12 +0200 Subject: [PATCH 24/25] libnm: skip meta enum value NM_WEP_KEY_TYPE_LAST from glib-mkenums --- libnm-core/nm-setting-wireless-security.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnm-core/nm-setting-wireless-security.h b/libnm-core/nm-setting-wireless-security.h index 0aa522f0f9..e7641b31d1 100644 --- a/libnm-core/nm-setting-wireless-security.h +++ b/libnm-core/nm-setting-wireless-security.h @@ -68,7 +68,7 @@ typedef enum { NM_WEP_KEY_TYPE_KEY = 1, /* Hex or ASCII */ NM_WEP_KEY_TYPE_PASSPHRASE = 2, /* 104/128-bit Passphrase */ - NM_WEP_KEY_TYPE_LAST = NM_WEP_KEY_TYPE_PASSPHRASE + NM_WEP_KEY_TYPE_LAST = NM_WEP_KEY_TYPE_PASSPHRASE, /*< skip >*/ } NMWepKeyType; /** From c72a07a604d5a1028a84892999158bc38d25cf43 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 23 May 2017 13:00:48 +0200 Subject: [PATCH 25/25] clients: implement wifi-sec.wep-key-type as enum property This way, we get tab completion for the enum values, and can reuse existing code. This requires a pre-set-notify hook, that is invoked before setting the property. --- clients/common/nm-meta-setting-desc.c | 92 ++++++++++++--------------- clients/common/nm-meta-setting-desc.h | 5 ++ 2 files changed, 47 insertions(+), 50 deletions(-) diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 57432f7478..bb73a3a67b 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -1107,6 +1107,15 @@ _set_fcn_gobject_enum (ARGS_SET_FCN) : NULL)) goto fail; + if ( property_info->property_typ_data + && property_info->property_typ_data->subtype.gobject_enum.pre_set_notify) { + property_info->property_typ_data->subtype.gobject_enum.pre_set_notify (property_info, + environment, + environment_user_data, + setting, + v); + } + g_value_init (&gval, gtype_prop); if (gtype_prop == G_TYPE_INT) g_value_set_int (&gval, v); @@ -4068,13 +4077,6 @@ _get_fcn_wireless_security_wep_key3 (ARGS_GET_FCN) RETURN_STR_TO_FREE (g_strdup (nm_setting_wireless_security_get_wep_key (s_wireless_sec, 3))); } -static gconstpointer -_get_fcn_wireless_security_wep_key_type (ARGS_GET_FCN) -{ - RETURN_UNSUPPORTED_GET_TYPE (); - RETURN_STR_TO_FREE (wep_key_type_to_string (nm_setting_wireless_security_get_wep_key_type (NM_SETTING_WIRELESS_SECURITY (setting)))); -} - static const char *wifi_sec_valid_protos[] = { "wpa", "rsn", NULL }; DEFINE_SETTER_STR_LIST_MULTI (check_and_add_wifi_sec_proto, @@ -4237,52 +4239,38 @@ _set_fcn_wireless_wep_key (ARGS_SET_FCN) return TRUE; } -static gboolean -_set_fcn_wireless_security_wep_key_type (ARGS_SET_FCN) +static void +_gobject_enum_pre_set_notify_fcn_wireless_security_wep_key_type (const NMMetaPropertyInfo *property_info, + const NMMetaEnvironment *environment, + gpointer environment_user_data, + NMSetting *setting, + int value) { - unsigned long type_int; - const char *valid_wep_types[] = { "unknown", "key", "passphrase", NULL }; - const char *type_str = NULL; - NMWepKeyType type = NM_WEP_KEY_TYPE_UNKNOWN; - - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - - if (!nmc_string_to_uint (value, TRUE, 0, 2, &type_int)) { - if (!(type_str = nmc_string_is_valid (value, valid_wep_types, NULL))) { - g_set_error (error, 1, 0, _("'%s' not among [0 (unknown), 1 (key), 2 (passphrase)]"), value); - return FALSE; - } - if (type_str == valid_wep_types[1]) - type = NM_WEP_KEY_TYPE_KEY; - else if (type_str == valid_wep_types[2]) - type = NM_WEP_KEY_TYPE_PASSPHRASE; - } else - type = (NMWepKeyType) type_int; + guint i; + const char *key; + const char *keynames[] = { + NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, + NM_SETTING_WIRELESS_SECURITY_WEP_KEY1, + NM_SETTING_WIRELESS_SECURITY_WEP_KEY2, + NM_SETTING_WIRELESS_SECURITY_WEP_KEY3, + }; /* Check type compatibility with set keys */ - { - guint i; - const char *key; - const char *keynames[] = { - NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, - NM_SETTING_WIRELESS_SECURITY_WEP_KEY1, - NM_SETTING_WIRELESS_SECURITY_WEP_KEY2, - NM_SETTING_WIRELESS_SECURITY_WEP_KEY3, - }; + if (!NM_IN_SET (value, + NM_WEP_KEY_TYPE_UNKNOWN, + NM_WEP_KEY_TYPE_KEY, + NM_WEP_KEY_TYPE_PASSPHRASE)) + return; - for (i = 0; i < 4; i++) { - key = nm_setting_wireless_security_get_wep_key (NM_SETTING_WIRELESS_SECURITY (setting), i); - if (key && !nm_utils_wep_key_valid (key, type)) { - _env_warn_fcn (environment, environment_user_data, - NM_META_ENV_WARN_LEVEL_WARN, - N_("'%s' is not compatible with '%s' type, please change or delete the key."), - keynames[i], wep_key_type_to_string (type)); - } + for (i = 0; i < 4; i++) { + key = nm_setting_wireless_security_get_wep_key (NM_SETTING_WIRELESS_SECURITY (setting), i); + if (key && !nm_utils_wep_key_valid (key, value)) { + _env_warn_fcn (environment, environment_user_data, + NM_META_ENV_WARN_LEVEL_WARN, + N_("'%s' is not compatible with '%s' type, please change or delete the key."), + keynames[i], wep_key_type_to_string (value)); } } - - g_object_set (setting, property_info->property_name, type, NULL); - return TRUE; } static const char * @@ -6526,9 +6514,13 @@ static const NMMetaPropertyInfo *const property_infos_WIRELESS_SECURITY[] = { .describe_message = N_("Enter the type of WEP keys. The accepted values are: " "0 or unknown, 1 or key, and 2 or passphrase.\n"), - .property_type = DEFINE_PROPERTY_TYPE ( - .get_fcn = _get_fcn_wireless_security_wep_key_type, - .set_fcn = _set_fcn_wireless_security_wep_key_type, + .property_type = &_pt_gobject_enum, + .property_typ_data = DEFINE_PROPERTY_TYP_DATA ( + PROPERTY_TYP_DATA_SUBTYPE (gobject_enum, + .pre_set_notify = _gobject_enum_pre_set_notify_fcn_wireless_security_wep_key_type, + ), + .typ_flags = NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PARSABLE_TEXT + | NM_META_PROPERTY_TYP_FLAG_ENUM_GET_PRETTY_TEXT, ), ), PROPERTY_INFO_WITH_DESC (NM_SETTING_WIRELESS_SECURITY_PSK, diff --git a/clients/common/nm-meta-setting-desc.h b/clients/common/nm-meta-setting-desc.h index e3a3848255..e21c9f3971 100644 --- a/clients/common/nm-meta-setting-desc.h +++ b/clients/common/nm-meta-setting-desc.h @@ -240,6 +240,11 @@ struct _NMMetaPropertyTypData { int min; int max; const struct _NMUtilsEnumValueInfo *value_infos; + void (*pre_set_notify) (const NMMetaPropertyInfo *property_info, + const NMMetaEnvironment *environment, + gpointer environment_user_data, + NMSetting *setting, + int value); } gobject_enum; struct { gint64 min;