dns: pass config data array to plugins

Export NMDnsIPConfigData to DNS plugins and use it to pass additional
information about configurations.
This commit is contained in:
Beniamino Galvani
2016-05-07 10:22:04 +02:00
parent 8e6d442477
commit e53aa0dcff
6 changed files with 47 additions and 114 deletions

View File

@@ -202,17 +202,15 @@ add_global_config (NMDnsDnsmasq *self, GVariantBuilder *dnsmasq_servers, const N
}
static gboolean
add_ip6_config (NMDnsDnsmasq *self, GVariantBuilder *servers, NMIP6Config *ip6, gboolean split)
add_ip6_config (NMDnsDnsmasq *self, GVariantBuilder *servers, NMIP6Config *ip6,
const char *iface, gboolean split)
{
const struct in6_addr *addr;
char *buf = NULL;
int nnameservers, i_nameserver, n, i;
gboolean added = FALSE;
const char *iface;
nnameservers = nm_ip6_config_get_num_nameservers (ip6);
iface = g_object_get_data (G_OBJECT (ip6), IP_CONFIG_IFACE_TAG);
g_assert (iface);
if (split) {
@@ -264,6 +262,24 @@ add_ip6_config (NMDnsDnsmasq *self, GVariantBuilder *servers, NMIP6Config *ip6,
return TRUE;
}
static gboolean
add_ip_config_data (NMDnsDnsmasq *self, GVariantBuilder *servers, const NMDnsIPConfigData *data)
{
if (NM_IS_IP4_CONFIG (data->config)) {
return add_ip4_config (self,
servers,
(NMIP4Config *) data->config,
data->type == NM_DNS_IP_CONFIG_TYPE_VPN);
} else if (NM_IS_IP6_CONFIG (data->config)) {
return add_ip6_config (self,
servers,
(NMIP6Config *) data->config,
data->iface,
data->type == NM_DNS_IP_CONFIG_TYPE_VPN);
} else
g_return_val_if_reached (FALSE);
}
static void
dnsmasq_update_done (GObject *source, GAsyncResult *res, gpointer user_data)
{
@@ -429,15 +445,12 @@ start_dnsmasq (NMDnsDnsmasq *self)
static gboolean
update (NMDnsPlugin *plugin,
const GSList *vpn_configs,
const GSList *dev_configs,
const GSList *other_configs,
const NMDnsIPConfigData **configs,
const NMGlobalDnsConfig *global_config,
const char *hostname)
{
NMDnsDnsmasq *self = NM_DNS_DNSMASQ (plugin);
NMDnsDnsmasqPrivate *priv = NM_DNS_DNSMASQ_GET_PRIVATE (self);
const GSList *iter;
GVariantBuilder servers;
start_dnsmasq (self);
@@ -447,28 +460,9 @@ update (NMDnsPlugin *plugin,
if (global_config)
add_global_config (self, &servers, global_config);
else {
/* Use split DNS for VPN configs */
for (iter = vpn_configs; iter; iter = g_slist_next (iter)) {
if (NM_IS_IP4_CONFIG (iter->data))
add_ip4_config (self, &servers, iter->data, TRUE);
else if (NM_IS_IP6_CONFIG (iter->data))
add_ip6_config (self, &servers, iter->data, TRUE);
}
/* Now add interface configs without split DNS */
for (iter = dev_configs; iter; iter = g_slist_next (iter)) {
if (NM_IS_IP4_CONFIG (iter->data))
add_ip4_config (self, &servers, iter->data, FALSE);
else if (NM_IS_IP6_CONFIG (iter->data))
add_ip6_config (self, &servers, iter->data, FALSE);
}
/* And any other random configs */
for (iter = other_configs; iter; iter = g_slist_next (iter)) {
if (NM_IS_IP4_CONFIG (iter->data))
add_ip4_config (self, &servers, iter->data, FALSE);
else if (NM_IS_IP6_CONFIG (iter->data))
add_ip6_config (self, &servers, iter->data, FALSE);
while (*configs) {
add_ip_config_data (self, &servers, *configs);
configs++;
}
}

View File

@@ -109,12 +109,6 @@ NM_DEFINE_SINGLETON_INSTANCE (NMDnsManager);
/*********************************************************************************************/
typedef struct {
gpointer config;
NMDnsIPConfigType type;
char *iface;
} NMDnsIPConfigData;
typedef struct _NMDnsManagerPrivate {
GPtrArray *configs;
NMDnsIPConfigData *best_conf4, *best_conf6;
@@ -191,12 +185,6 @@ ip_config_data_new (gpointer config, NMDnsIPConfigType type, const char *iface)
data->iface = g_strdup (iface);
data->type = type;
/* Plugins still receive plain config, attach iface through object data */
g_object_set_data_full (G_OBJECT (config),
IP_CONFIG_IFACE_TAG,
g_strdup (iface),
g_free);
return data;
}
@@ -205,7 +193,9 @@ ip_config_data_destroy (gpointer ptr)
{
NMDnsIPConfigData *data = ptr;
g_object_set_data (G_OBJECT (data->config), IP_CONFIG_IFACE_TAG, NULL);
if (!data)
return;
g_object_unref (data->config);
g_free (data->iface);
g_slice_free (NMDnsIPConfigData, data);
@@ -879,43 +869,6 @@ compute_hash (NMDnsManager *self, const NMGlobalDnsConfig *global, guint8 buffer
g_checksum_free (sum);
}
static void
build_plugin_config_lists (NMDnsManager *self,
GSList **out_vpn_configs,
GSList **out_dev_configs,
GSList **out_other_configs)
{
NMDnsManagerPrivate *priv = NM_DNS_MANAGER_GET_PRIVATE (self);
guint i;
g_return_if_fail (out_vpn_configs && !*out_vpn_configs);
g_return_if_fail (out_dev_configs && !*out_dev_configs);
g_return_if_fail (out_other_configs && !*out_other_configs);
/* Build up config lists for plugins; we use the raw configs here, not the
* merged information that we write to resolv.conf so that the plugins can
* still use the domain information in each config to provide split DNS if
* they want to.
*/
for (i = 0; i < priv->configs->len; i++) {
NMDnsIPConfigData *data = priv->configs->pdata[i];
switch (data->type) {
case NM_DNS_IP_CONFIG_TYPE_VPN:
*out_vpn_configs = g_slist_append (*out_vpn_configs, data->config);
break;
case NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE:
*out_dev_configs = g_slist_append (*out_dev_configs, data->config);
break;
case NM_DNS_IP_CONFIG_TYPE_DEFAULT:
*out_other_configs = g_slist_append (*out_other_configs, data->config);
break;
default:
g_return_if_reached ();
}
}
}
static gboolean
merge_global_dns_config (NMResolvConfData *rc, NMGlobalDnsConfig *global_conf)
{
@@ -1065,7 +1018,6 @@ update_dns (NMDnsManager *self,
if (priv->plugin) {
NMDnsPlugin *plugin = priv->plugin;
const char *plugin_name = nm_dns_plugin_get_name (plugin);
GSList *vpn_configs = NULL, *dev_configs = NULL, *other_configs = NULL;
if (nm_dns_plugin_is_caching (plugin)) {
if (no_caching) {
@@ -1076,14 +1028,11 @@ update_dns (NMDnsManager *self,
caching = TRUE;
}
if (!global_config)
build_plugin_config_lists (self, &vpn_configs, &dev_configs, &other_configs);
_LOGD ("update-dns: updating plugin %s", plugin_name);
g_ptr_array_add (priv->configs, NULL);
if (!nm_dns_plugin_update (plugin,
vpn_configs,
dev_configs,
other_configs,
(const NMDnsIPConfigData **) priv->configs->pdata,
global_config,
priv->hostname)) {
_LOGW ("update-dns: plugin %s update failed", plugin_name);
@@ -1093,9 +1042,7 @@ update_dns (NMDnsManager *self,
*/
caching = FALSE;
}
g_slist_free (vpn_configs);
g_slist_free (dev_configs);
g_slist_free (other_configs);
g_ptr_array_remove_index (priv->configs, priv->configs->len - 1);
skip:
;

View File

@@ -36,6 +36,12 @@ typedef enum {
NM_DNS_IP_CONFIG_TYPE_VPN
} NMDnsIPConfigType;
typedef struct {
gpointer config;
NMDnsIPConfigType type;
char *iface;
} NMDnsIPConfigData;
#define NM_TYPE_DNS_MANAGER (nm_dns_manager_get_type ())
#define NM_DNS_MANAGER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), NM_TYPE_DNS_MANAGER, NMDnsManager))
#define NM_DNS_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), NM_TYPE_DNS_MANAGER, NMDnsManagerClass))

View File

@@ -74,18 +74,14 @@ static guint signals[LAST_SIGNAL] = { 0 };
gboolean
nm_dns_plugin_update (NMDnsPlugin *self,
const GSList *vpn_configs,
const GSList *dev_configs,
const GSList *other_configs,
const NMDnsIPConfigData **configs,
const NMGlobalDnsConfig *global_config,
const char *hostname)
{
g_return_val_if_fail (NM_DNS_PLUGIN_GET_CLASS (self)->update != NULL, FALSE);
return NM_DNS_PLUGIN_GET_CLASS (self)->update (self,
vpn_configs,
dev_configs,
other_configs,
configs,
global_config,
hostname);
}

View File

@@ -20,6 +20,7 @@
#define __NETWORKMANAGER_DNS_PLUGIN_H__
#include "nm-default.h"
#include "nm-dns-manager.h"
#include "nm-config-data.h"
@@ -33,8 +34,6 @@
#define NM_DNS_PLUGIN_FAILED "failed"
#define NM_DNS_PLUGIN_CHILD_QUIT "child-quit"
#define IP_CONFIG_IFACE_TAG "dns-manager-iface"
typedef struct {
GObject parent;
} NMDnsPlugin;
@@ -44,18 +43,13 @@ typedef struct {
/* Methods */
/* Called when DNS information is changed. 'vpn_configs' is a list of
* NMIP4Config or NMIP6Config objects from VPN connections, while
* 'dev_configs' is a list of NMPI4Config or NMIP6Config objects from
* active devices. 'other_configs' represent other IP configuration that
* may be in-use. 'global_config' is the optional global DNS
* configuration. Configs of the same IP version are sorted in priority
* order.
/* Called when DNS information is changed. 'configs' is an array
* of pointers to NMDnsIPConfigData sorted by priority.
* 'global_config' is the optional global DNS
* configuration.
*/
gboolean (*update) (NMDnsPlugin *self,
const GSList *vpn_configs,
const GSList *dev_configs,
const GSList *other_configs,
const NMDnsIPConfigData **configs,
const NMGlobalDnsConfig *global_config,
const char *hostname);
@@ -92,9 +86,7 @@ gboolean nm_dns_plugin_is_caching (NMDnsPlugin *self);
const char *nm_dns_plugin_get_name (NMDnsPlugin *self);
gboolean nm_dns_plugin_update (NMDnsPlugin *self,
const GSList *vpn_configs,
const GSList *dev_configs,
const GSList *other_configs,
const NMDnsIPConfigData **configs,
const NMGlobalDnsConfig *global_config,
const char *hostname);

View File

@@ -28,9 +28,7 @@ G_DEFINE_TYPE (NMDnsUnbound, nm_dns_unbound, NM_TYPE_DNS_PLUGIN)
static gboolean
update (NMDnsPlugin *plugin,
const GSList *vpn_configs,
const GSList *dev_configs,
const GSList *other_configs,
const NMDnsIPConfigData **configs,
const NMGlobalDnsConfig *global_config,
const char *hostname)
{