diff options
Diffstat (limited to 'src/core/nm-dhcp-config.c')
-rw-r--r-- | src/core/nm-dhcp-config.c | 99 |
1 files changed, 82 insertions, 17 deletions
diff --git a/src/core/nm-dhcp-config.c b/src/core/nm-dhcp-config.c index 8a02911942..8a89173c0f 100644 --- a/src/core/nm-dhcp-config.c +++ b/src/core/nm-dhcp-config.c @@ -8,9 +8,11 @@ #include "nm-dhcp-config.h" #include "nm-dbus-interface.h" +#include "NetworkManagerUtils.h" #include "nm-utils.h" #include "nm-dbus-object.h" #include "nm-core-utils.h" +#include "nm-l3-config-data.h" /*****************************************************************************/ @@ -46,10 +48,11 @@ static GType nm_dhcp6_config_get_type(void); /*****************************************************************************/ -NM_GOBJECT_PROPERTIES_DEFINE(NMDhcpConfig, PROP_OPTIONS, ); +NM_GOBJECT_PROPERTIES_DEFINE(NMDhcpConfig, PROP_L3CD, PROP_OPTIONS, ); typedef struct { - GVariant *options; + const NML3ConfigData *l3cd; + GVariant * options; } NMDhcpConfigPrivate; struct _NMDhcpConfig { @@ -76,18 +79,49 @@ nm_dhcp_config_get_addr_family(NMDhcpConfig *self) /*****************************************************************************/ +static GVariant * +_l3cd_to_options(const NML3ConfigData *l3cd, int addr_family) +{ + GVariant *options; + + options = NULL; + if (l3cd) { + NMDhcpLease *lease; + + lease = nm_l3_config_data_get_dhcp_lease(l3cd, addr_family); + if (lease) + options = nm_strdict_to_variant_asv(nm_dhcp_lease_get_options(lease)); + } + if (!options) + options = nm_g_variant_singleton_aLsvI(); + return g_variant_ref_sink(options); +} + void -nm_dhcp_config_set_options(NMDhcpConfig *self, GHashTable *options) +nm_dhcp_config_set_lease(NMDhcpConfig *self, const NML3ConfigData *l3cd) { - NMDhcpConfigPrivate *priv; + nm_auto_unref_l3cd const NML3ConfigData *l3cd_old = NULL; + gs_unref_variant GVariant *options2 = NULL; + NMDhcpConfigPrivate * priv; g_return_if_fail(NM_IS_DHCP_CONFIG(self)); - g_return_if_fail(options); priv = NM_DHCP_CONFIG_GET_PRIVATE(self); - nm_g_variant_unref(priv->options); - priv->options = g_variant_ref_sink(nm_strdict_to_variant_asv(options)); + if (priv->l3cd == l3cd) + return; + + l3cd_old = g_steal_pointer(&priv->l3cd); + if (l3cd) + priv->l3cd = nm_l3_config_data_ref_and_seal(l3cd); + + options2 = _l3cd_to_options(priv->l3cd, nm_dhcp_config_get_addr_family(self)); + + if (g_variant_equal(priv->options, options2)) + return; + + NM_SWAP(&priv->options, &options2); + _notify(self, PROP_OPTIONS); } @@ -95,19 +129,22 @@ const char * nm_dhcp_config_get_option(NMDhcpConfig *self, const char *key) { NMDhcpConfigPrivate *priv; - const char * value; + NMDhcpLease * lease; g_return_val_if_fail(NM_IS_DHCP_CONFIG(self), NULL); g_return_val_if_fail(key, NULL); priv = NM_DHCP_CONFIG_GET_PRIVATE(self); - if (priv->options && g_variant_lookup(priv->options, key, "&s", &value)) - return value; - else + if (!priv->l3cd) return NULL; + + lease = nm_l3_config_data_get_dhcp_lease(priv->l3cd, nm_dhcp_config_get_addr_family(self)); + return nm_dhcp_lease_lookup_option(lease, key); } +/*****************************************************************************/ + GVariant * nm_dhcp_config_get_options(NMDhcpConfig *self) { @@ -125,7 +162,25 @@ get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) switch (prop_id) { case PROP_OPTIONS: - g_value_set_variant(value, priv->options ?: nm_g_variant_singleton_aLsvI()); + g_value_set_variant(value, priv->options); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + NMDhcpConfig * self = NM_DHCP_CONFIG(object); + NMDhcpConfigPrivate *priv = NM_DHCP_CONFIG_GET_PRIVATE(self); + + switch (prop_id) { + case PROP_L3CD: + /* construct-only */ + priv->l3cd = nm_l3_config_data_ref_and_seal(g_value_get_pointer(value)); + priv->options = _l3cd_to_options(priv->l3cd, nm_dhcp_config_get_addr_family(self)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); @@ -140,11 +195,12 @@ nm_dhcp_config_init(NMDhcpConfig *self) {} NMDhcpConfig * -nm_dhcp_config_new(int addr_family) +nm_dhcp_config_new(int addr_family, const NML3ConfigData *l3cd) { - nm_assert_addr_family(addr_family); - - return g_object_new(addr_family != AF_INET ? NM_TYPE_DHCP6_CONFIG : NM_TYPE_DHCP4_CONFIG, NULL); + return g_object_new(NM_IS_IPv4(addr_family) ? NM_TYPE_DHCP4_CONFIG : NM_TYPE_DHCP6_CONFIG, + NM_DHCP_CONFIG_L3CD, + l3cd, + NULL); } static void @@ -152,7 +208,9 @@ finalize(GObject *object) { NMDhcpConfigPrivate *priv = NM_DHCP_CONFIG_GET_PRIVATE(object); - nm_g_variant_unref(priv->options); + g_variant_unref(priv->options); + + nm_l3_config_data_unref(priv->l3cd); G_OBJECT_CLASS(nm_dhcp_config_parent_class)->finalize(object); } @@ -163,8 +221,15 @@ nm_dhcp_config_class_init(NMDhcpConfigClass *config_class) GObjectClass *object_class = G_OBJECT_CLASS(config_class); object_class->get_property = get_property; + object_class->set_property = set_property; object_class->finalize = finalize; + obj_properties[PROP_L3CD] = + g_param_spec_pointer(NM_DHCP_CONFIG_L3CD, + "", + "", + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_OPTIONS] = g_param_spec_variant(NM_DHCP_CONFIG_OPTIONS, "", "", |