From d652e0f53487cf3f5b1f64038d9ff4a2f5947213 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 29 Jun 2021 21:53:55 +0200 Subject: [PATCH] libnm: refactor to_dbus_fcn() for "ipv4.dns" property The goal is to get rid of gprop_to_dbus_fcn() uses. Note that there is a change in behavior. The "dns" GPtrArray in NMSettingIPConfig is never NULL (the default of the boxed property), thus the previous code always serialized the property, even the empty list. Now, empty dns properties are omitted from D-Bus. Also, there is another change in behavior: nm_utils_ip4_dns_to_variant() will now skip over strings that are not valid IPv4 addresses. Previously, it would have added 0.0.0.0 (or some undefined address). --- src/libnm-core-impl/nm-setting-ip4-config.c | 25 ++++++++++++++++----- src/libnm-core-impl/nm-utils-private.h | 1 + src/libnm-core-impl/nm-utils.c | 20 ++++++++++++----- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/libnm-core-impl/nm-setting-ip4-config.c b/src/libnm-core-impl/nm-setting-ip4-config.c index e2d6a26bbc..2a10098e23 100644 --- a/src/libnm-core-impl/nm-setting-ip4-config.c +++ b/src/libnm-core-impl/nm-setting-ip4-config.c @@ -8,6 +8,7 @@ #include "nm-setting-ip4-config.h" #include "nm-setting-private.h" +#include "nm-utils-private.h" /** * SECTION:nm-setting-ip4-config @@ -321,9 +322,21 @@ verify(NMSetting *setting, NMConnection *connection, GError **error) } static GVariant * -ip4_dns_to_dbus(const GValue *prop_value) +ip4_dns_to_dbus(const NMSettInfoSetting * sett_info, + const NMSettInfoProperty * property_info, + NMConnection * connection, + NMSetting * setting, + NMConnectionSerializationFlags flags, + const NMConnectionSerializationOptions *options) { - return nm_utils_ip4_dns_to_variant(g_value_get_boxed(prop_value)); + GPtrArray *dns; + + dns = _nm_setting_ip_config_get_dns_array(NM_SETTING_IP_CONFIG(setting)); + + if (nm_g_ptr_array_len(dns) == 0) + return NULL; + + return _nm_utils_ip4_dns_to_variant((const char *const *) dns->pdata, dns->len); } static void @@ -955,10 +968,10 @@ nm_setting_ip4_config_class_init(NMSettingIP4ConfigClass *klass) _nm_properties_override_gobj( properties_override, g_object_class_find_property(G_OBJECT_CLASS(setting_class), NM_SETTING_IP_CONFIG_DNS), - NM_SETT_INFO_PROPERT_TYPE_GPROP(NM_G_VARIANT_TYPE("au"), - .compare_fcn = _nm_setting_property_compare_fcn_default, - .gprop_from_dbus_fcn = ip4_dns_from_dbus, ), - .to_dbus_data.gprop_to_dbus_fcn = ip4_dns_to_dbus); + NM_SETT_INFO_PROPERT_TYPE_DBUS(NM_G_VARIANT_TYPE("au"), + .compare_fcn = _nm_setting_property_compare_fcn_default, + .to_dbus_fcn = ip4_dns_to_dbus, + .gprop_from_dbus_fcn = ip4_dns_from_dbus, ), ); /* ---dbus--- * property: addresses diff --git a/src/libnm-core-impl/nm-utils-private.h b/src/libnm-core-impl/nm-utils-private.h index de4500055f..eec84dc637 100644 --- a/src/libnm-core-impl/nm-utils-private.h +++ b/src/libnm-core-impl/nm-utils-private.h @@ -65,6 +65,7 @@ GVariant *_nm_team_settings_property_to_dbus(const NMSettInfoSetting * void _nm_team_settings_property_from_dbus_link_watchers(GVariant *dbus_value, GValue *prop_value); +GVariant *_nm_utils_ip4_dns_to_variant(const char *const *dns, gssize len); GVariant *_nm_utils_ip6_dns_to_variant(const char *const *dns, gssize len); #endif diff --git a/src/libnm-core-impl/nm-utils.c b/src/libnm-core-impl/nm-utils.c index fb97eef0d7..ca7c820535 100644 --- a/src/libnm-core-impl/nm-utils.c +++ b/src/libnm-core-impl/nm-utils.c @@ -1283,19 +1283,29 @@ nm_utils_wpa_psk_valid(const char *psk) **/ GVariant * nm_utils_ip4_dns_to_variant(char **dns) +{ + return _nm_utils_ip4_dns_to_variant(NM_CAST_STRV_CC(dns), -1); +} + +GVariant * +_nm_utils_ip4_dns_to_variant(const char *const *dns, gssize len) { GVariantBuilder builder; + gsize l; gsize i; + if (len < 0) + l = NM_PTRARRAY_LEN(dns); + else + l = len; + g_variant_builder_init(&builder, G_VARIANT_TYPE("au")); - if (dns) { - for (i = 0; dns[i]; i++) { - guint32 ip = 0; + for (i = 0; i < l; i++) { + in_addr_t ip; - inet_pton(AF_INET, dns[i], &ip); + if (inet_pton(AF_INET, dns[i], &ip) == 1) g_variant_builder_add(&builder, "u", ip); - } } return g_variant_builder_end(&builder);