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).
This commit is contained in:
Thomas Haller 2021-06-29 21:53:55 +02:00
parent bb5c89e017
commit d652e0f534
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
3 changed files with 35 additions and 11 deletions

View file

@ -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

View file

@ -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

View file

@ -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);