network-manager-applet r825 - in trunk: . src/gconf-helpers



Author: dcbw
Date: Wed Aug  6 22:29:42 2008
New Revision: 825
URL: http://svn.gnome.org/viewvc/network-manager-applet?rev=825&view=rev

Log:
2008-08-06  Dan Williams  <dcbw redhat com>

	* src/gconf-helpers/gconf-helpers.c
	  src/gconf-helpers/gconf-helpers.h
		- Generalize IP4 helper functions to handle both routes and addresses

	* src/gconf-helpers/gconf-upgrade.c
	  src/gconf-helpers/gconf-upgrade.h
		- (nm_gconf_migrate_0_7_ignore_dhcp_dns, nm_gconf_migrate_0_7_vpn_routes):
			new functions



Modified:
   trunk/ChangeLog
   trunk/src/gconf-helpers/gconf-helpers.c
   trunk/src/gconf-helpers/gconf-helpers.h
   trunk/src/gconf-helpers/gconf-upgrade.c
   trunk/src/gconf-helpers/gconf-upgrade.h

Modified: trunk/src/gconf-helpers/gconf-helpers.c
==============================================================================
--- trunk/src/gconf-helpers/gconf-helpers.c	(original)
+++ trunk/src/gconf-helpers/gconf-helpers.c	Wed Aug  6 22:29:42 2008
@@ -31,6 +31,7 @@
 #include <nm-setting-wireless-security.h>
 #include <nm-setting-8021x.h>
 #include <nm-setting-vpn.h>
+#include <nm-setting-ip4-config.h>
 #include <nm-utils.h>
 #include <nm-settings.h>
 
@@ -401,52 +402,62 @@
 }
 
 gboolean
-nm_gconf_get_ip4_addresses_helper (GConfClient *client,
+nm_gconf_get_ip4_helper (GConfClient *client,
 						  const char *path,
 						  const char *key,
 						  const char *network,
+						  guint32 tuple_len,
 						  GPtrArray **value)
 {
 	char *gc_key;
-	GConfValue *gc_value;
+	GConfValue *gc_value = NULL;
 	GPtrArray *array;
 	gboolean success = FALSE;
+	GSList *values, *iter;
+	GArray *tuple = NULL;
 
 	g_return_val_if_fail (key != NULL, FALSE);
 	g_return_val_if_fail (network != NULL, FALSE);
 	g_return_val_if_fail (value != NULL, FALSE);
+	g_return_val_if_fail (tuple_len > 0, FALSE);
 
 	gc_key = g_strdup_printf ("%s/%s/%s", path, network, key);
 	if (!(gc_value = gconf_client_get (client, gc_key, NULL)))
 		goto out;
 
-	if (gc_value->type == GCONF_VALUE_LIST
-	    && gconf_value_get_list_type (gc_value) == GCONF_VALUE_INT)
-	{
-		GSList *elt;
-		GArray *tuple = NULL;
+	if (   (gc_value->type != GCONF_VALUE_LIST)
+	    || (gconf_value_get_list_type (gc_value) != GCONF_VALUE_INT))
+		goto out;
 
-		array = g_ptr_array_sized_new (1);
-		for (elt = gconf_value_get_list (gc_value); elt != NULL; elt = g_slist_next (elt)) {
-			int i = gconf_value_get_int ((GConfValue *) elt->data);
+	values = gconf_value_get_list (gc_value);
+	if (g_slist_length (values) % tuple_len != 0) {
+		g_warning ("%s: %s format invalid; # elements not divisible by %d",
+		           __func__, gc_key, tuple_len);
+		goto out;
+	}
 
-			if (tuple == NULL)
-				tuple = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 3);
+	array = g_ptr_array_sized_new (1);
+	for (iter = values; iter; iter = g_slist_next (iter)) {
+		int i = gconf_value_get_int ((GConfValue *) iter->data);
 
-			g_array_append_val (tuple, i);
+		if (tuple == NULL)
+			tuple = g_array_sized_new (FALSE, TRUE, sizeof (guint32), tuple_len);
 
-			/* Got addr, netmask, and gateway, add to ptr array */
-			if (tuple->len == 3) {
-				g_ptr_array_add (array, tuple);
-				tuple = NULL;
-			}
-		}
+		g_array_append_val (tuple, i);
 
-		*value = array;
-		success = TRUE;
+		/* Got all members; add to the array */
+		if (tuple->len == tuple_len) {
+			g_ptr_array_add (array, tuple);
+			tuple = NULL;
+		}
 	}
 
+	*value = array;
+	success = TRUE;
+
 out:
+	if (gc_value)
+		gconf_value_free (gc_value);
 	g_free (gc_key);
 	return success;
 }
@@ -690,10 +701,11 @@
 }
 
 gboolean
-nm_gconf_set_ip4_addresses_helper (GConfClient *client,
+nm_gconf_set_ip4_helper (GConfClient *client,
 					  const char *path,
 					  const char *key,
 					  const char *network,
+					  guint32 tuple_len,
 					  GPtrArray *value)
 {
 	char *gc_key;
@@ -703,6 +715,7 @@
 
 	g_return_val_if_fail (key != NULL, FALSE);
 	g_return_val_if_fail (network != NULL, FALSE);
+	g_return_val_if_fail (tuple_len > 0, FALSE);
 
 	if (!value)
 		return TRUE;
@@ -715,22 +728,15 @@
 
 	for (i = 0; i < value->len; i++) {
 		GArray *tuple = g_ptr_array_index (value, i);
+		int j;
 
-		if ((tuple->len < 2) || (tuple->len > 3)) {
-			g_warning ("%s: invalid IPv4 address structure!", __func__);
+		if (tuple->len != tuple_len) {
+			g_warning ("%s: invalid IPv4 address/route structure!", __func__);
 			goto out;
 		}
 
-		/* IP address */
-		list = g_slist_append (list, GUINT_TO_POINTER (g_array_index (tuple, guint32, 0)));
-		/* Netmask */
-		list = g_slist_append (list, GUINT_TO_POINTER (g_array_index (tuple, guint32, 1)));
-
-		/* Gateway */
-		if (tuple->len == 3)
-			list = g_slist_append (list, GUINT_TO_POINTER (g_array_index (tuple, guint32, 2)));
-		else
-			list = g_slist_append (list, GUINT_TO_POINTER (0));
+		for (j = 0; j < tuple_len; j++)
+			list = g_slist_append (list, GUINT_TO_POINTER (g_array_index (tuple, guint32, j)));
 	}
 
 	gconf_client_set_list (client, gc_key, GCONF_VALUE_INT, list, NULL);
@@ -753,6 +759,8 @@
 	nm_gconf_migrate_0_7_wireless_security (client);
 	nm_gconf_migrate_0_7_netmask_to_prefix (client);
 	nm_gconf_migrate_0_7_ip4_method (client);
+	nm_gconf_migrate_0_7_ignore_dhcp_dns (client);
+	nm_gconf_migrate_0_7_vpn_routes (client);	
 
 	connections = gconf_client_all_dirs (client, GCONF_PATH_CONNECTIONS, NULL);
 	if (!connections) {
@@ -879,8 +887,14 @@
 		}
 	} else if (type == DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT) {
 		GPtrArray *pa_val = NULL;
+		guint32 tuple_len = 0;
+
+		if (!strcmp (key, NM_SETTING_IP4_CONFIG_ADDRESSES))
+			tuple_len = 3;
+		else if (!strcmp (key, NM_SETTING_IP4_CONFIG_ROUTES))
+			tuple_len = 4;
 
-		if (nm_gconf_get_ip4_addresses_helper (info->client, info->dir, key, setting->name, &pa_val)) {
+		if (nm_gconf_get_ip4_helper (info->client, info->dir, key, setting->name, tuple_len, &pa_val)) {
 			g_object_set (setting, key, pa_val, NULL);
 			g_ptr_array_foreach (pa_val, (GFunc) free_one_addr, NULL);
 			g_ptr_array_free (pa_val, TRUE);
@@ -1139,8 +1153,15 @@
 								  key, setting->name,
 								  (GArray *) g_value_get_boxed (value));
 	} else if (type == DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT) {
-		nm_gconf_set_ip4_addresses_helper (info->client, info->dir,
-								  key, setting->name,
+		guint32 tuple_len = 0;
+
+		if (!strcmp (key, NM_SETTING_IP4_CONFIG_ADDRESSES))
+			tuple_len = 3;
+		else if (!strcmp (key, NM_SETTING_IP4_CONFIG_ROUTES))
+			tuple_len = 4;
+
+		nm_gconf_set_ip4_helper (info->client, info->dir,
+								  key, setting->name, tuple_len,
 								  (GPtrArray *) g_value_get_boxed (value));
 	} else
 		g_warning ("Unhandled setting property type (write) '%s/%s' : '%s'", 

Modified: trunk/src/gconf-helpers/gconf-helpers.h
==============================================================================
--- trunk/src/gconf-helpers/gconf-helpers.h	(original)
+++ trunk/src/gconf-helpers/gconf-helpers.h	Wed Aug  6 22:29:42 2008
@@ -108,10 +108,11 @@
 			       GHashTable **value);
 
 gboolean
-nm_gconf_get_ip4_addresses_helper (GConfClient *client,
+nm_gconf_get_ip4_helper (GConfClient *client,
 						  const char *path,
 						  const char *key,
 						  const char *network,
+						  guint32 tuple_len,
 						  GPtrArray **value);
 
 /* Setters */
@@ -172,10 +173,11 @@
 			       GHashTable *value);
 
 gboolean
-nm_gconf_set_ip4_addresses_helper (GConfClient *client,
+nm_gconf_set_ip4_helper (GConfClient *client,
 					  const char *path,
 					  const char *key,
 					  const char *network,
+					  guint32 tuple_len,
 					  GPtrArray *value);
 
 GSList *

Modified: trunk/src/gconf-helpers/gconf-upgrade.c
==============================================================================
--- trunk/src/gconf-helpers/gconf-upgrade.c	(original)
+++ trunk/src/gconf-helpers/gconf-upgrade.c	Wed Aug  6 22:29:42 2008
@@ -27,6 +27,9 @@
 #include <string.h>
 #include "wireless-helper.h"
 #include <stdlib.h>
+#include <netinet/in.h>
+#include <errno.h>
+#include <arpa/inet.h>
 
 #include <gnome-keyring.h>
 #include <nm-setting-connection.h>
@@ -504,6 +507,50 @@
 	return s_vpn_props;
 }
 
+static GSList *
+convert_routes (GSList *str_routes)
+{
+	GSList *routes = NULL, *iter;
+
+	for (iter = str_routes; iter; iter = g_slist_next (iter)) {
+		struct in_addr tmp;
+		char *p, *str_route;
+		long int prefix = 32;
+
+		str_route = g_strdup (iter->data);
+		p = strchr (str_route, '/');
+		if (!p || !(*(p + 1))) {
+			g_warning ("Ignoring invalid route '%s'", str_route);
+			goto next;
+		}
+
+		errno = 0;
+		prefix = strtol (p + 1, NULL, 10);
+		if (errno || prefix <= 0 || prefix > 32) {
+			g_warning ("Ignoring invalid route '%s'", str_route);
+			goto next;
+		}
+
+		/* don't pass the prefix to inet_pton() */
+		*p = '\0';
+		if (inet_pton (AF_INET, str_route, &tmp) > 0) {
+			NMSettingIP4Route *route;
+
+			route = g_new0 (NMSettingIP4Route, 1);
+			route->address = tmp.s_addr;
+			route->prefix = (guint32) prefix;
+
+			routes = g_slist_append (routes, route);
+		} else
+			g_warning ("Ignoring invalid route '%s'", str_route);
+
+next:
+		g_free (str_route);
+	}
+
+	return routes;
+}
+
 static NMConnection *
 nm_gconf_read_0_6_vpn_connection (GConfClient *client,
 						    const char *dir)
@@ -512,8 +559,9 @@
 	NMSettingConnection *s_con;
 	NMSettingVPN *s_vpn;
 	NMSettingVPNProperties *s_vpn_props;
+	NMSettingIP4Config *s_ip4;
 	char *path, *network, *id = NULL, *service_name = NULL;
-	GSList *routes = NULL, *vpn_data = NULL;
+	GSList *str_routes = NULL, *vpn_data = NULL;
 
 	path = g_path_get_dirname (dir);
 	network = g_path_get_basename (dir);
@@ -530,10 +578,10 @@
 		return NULL;
 	}
 
-	if (!nm_gconf_get_stringlist_helper (client, path, "routes", network, &routes))
-		routes = NULL;
+	if (!nm_gconf_get_stringlist_helper (client, path, "routes", network, &str_routes))
+		str_routes = NULL;
 	if (!nm_gconf_get_stringlist_helper (client, path, "vpn_data", network, &vpn_data))
-		routes = NULL;
+		vpn_data = NULL;
 
 	s_con = (NMSettingConnection *)nm_setting_connection_new ();
 	s_con->id = id;
@@ -541,7 +589,6 @@
 
 	s_vpn = (NMSettingVPN *)nm_setting_vpn_new ();
 	s_vpn->service_type = service_name;
-	s_vpn->routes = routes;
 
 	if (!strcmp (service_name, "org.freedesktop.NetworkManager.vpnc"))
 		s_vpn_props = nm_gconf_0_6_vpnc_settings (vpn_data);
@@ -556,11 +603,18 @@
 	g_free (path);
 	g_free (network);
 
+	if (str_routes) {
+		s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ());
+		s_ip4->routes = convert_routes (str_routes);
+	}
+
 	connection = nm_connection_new ();
-	nm_connection_add_setting (connection, (NMSetting *)s_con);
-	nm_connection_add_setting (connection, (NMSetting *)s_vpn);
+	nm_connection_add_setting (connection, NM_SETTING (s_con));
+	nm_connection_add_setting (connection, NM_SETTING (s_vpn));
 	if (s_vpn_props)
-		nm_connection_add_setting (connection, (NMSetting *)s_vpn_props);
+		nm_connection_add_setting (connection, NM_SETTING (s_vpn_props));
+	if (s_ip4)
+		nm_connection_add_setting (connection, NM_SETTING (s_vpn_props));
 
 	return connection;
 }
@@ -1101,3 +1155,169 @@
 	gconf_client_suggest_sync (client, NULL);
 }
 
+#define IP4_KEY_IGNORE_DHCP_DNS "ignore-dhcp-dns"
+
+void
+nm_gconf_migrate_0_7_ignore_dhcp_dns (GConfClient *client)
+{
+	GSList *connections, *iter;
+
+	connections = gconf_client_all_dirs (client, GCONF_PATH_CONNECTIONS, NULL);
+	for (iter = connections; iter; iter = iter->next) {
+		char *del_key;
+		gboolean ignore_auto_dns = FALSE;
+
+		if (!nm_gconf_get_bool_helper (client, iter->data,
+		                               IP4_KEY_IGNORE_DHCP_DNS,
+		                               NM_SETTING_IP4_CONFIG_SETTING_NAME,
+		                               &ignore_auto_dns))
+			continue;
+
+		/* add new key with new name */
+		if (ignore_auto_dns) {
+			nm_gconf_set_bool_helper (client, iter->data,
+			                          NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS,
+			                          NM_SETTING_IP4_CONFIG_SETTING_NAME,
+			                          ignore_auto_dns);
+		}
+
+		/* delete old key */
+		del_key = g_strdup_printf ("%s/%s/%s",
+		                           (const char *) iter->data,
+		                           NM_SETTING_IP4_CONFIG_SETTING_NAME,
+		                           IP4_KEY_IGNORE_DHCP_DNS);
+		gconf_client_unset (client, del_key, NULL);
+		g_free (del_key);
+	}
+	free_slist (connections);
+
+	gconf_client_suggest_sync (client, NULL);
+}
+
+static gboolean
+convert_route (const char *in_route, NMSettingIP4Route *converted)
+{
+	struct in_addr tmp;
+	char *p, *str_route;
+	long int prefix = 32;
+	gboolean success = FALSE;
+
+	memset (converted, 0, sizeof (*converted));
+
+	str_route = g_strdup (in_route);
+	p = strchr (str_route, '/');
+	if (!p || !(*(p + 1))) {
+		g_warning ("Ignoring invalid route '%s'", str_route);
+		goto out;
+	}
+
+	errno = 0;
+	prefix = strtol (p + 1, NULL, 10);
+	if (errno || prefix <= 0 || prefix > 32) {
+		g_warning ("Ignoring invalid route '%s'", str_route);
+		goto out;
+	}
+
+	/* don't pass the prefix to inet_pton() */
+	*p = '\0';
+	if (inet_pton (AF_INET, str_route, &tmp) <= 0) {
+		g_warning ("Ignoring invalid route '%s'", str_route);
+		goto out;
+	}
+
+	converted->address = tmp.s_addr;
+	converted->prefix = (guint32) prefix;
+	success = TRUE;
+
+out:
+	g_free (str_route);
+	return success;
+}
+
+#define VPN_KEY_ROUTES "routes"
+
+static void
+free_one_route (gpointer data, gpointer user_data)
+{
+	g_array_free ((GArray *) data, TRUE);
+}
+
+void
+nm_gconf_migrate_0_7_vpn_routes (GConfClient *client)
+{
+	GSList *connections, *iter;
+
+	connections = gconf_client_all_dirs (client, GCONF_PATH_CONNECTIONS, NULL);
+	for (iter = connections; iter; iter = iter->next) {
+		char *del_key;
+		GSList *old_routes = NULL, *routes_iter;
+		GPtrArray *new_routes = NULL;
+
+		if (!nm_gconf_get_stringlist_helper (client, iter->data,
+		                                     VPN_KEY_ROUTES,
+		                                     NM_SETTING_VPN_SETTING_NAME,
+		                                     &old_routes))
+			continue;
+
+		/* Convert 'x.x.x.x/x' into a route structure */
+		for (routes_iter = old_routes; routes_iter; routes_iter = g_slist_next (routes_iter)) {
+			NMSettingIP4Route route;
+
+			if (convert_route (routes_iter->data, &route)) {
+				GArray *tmp_route;
+
+				if (!new_routes)
+					new_routes = g_ptr_array_sized_new (3);
+
+				tmp_route = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 4);
+				g_array_append_val (tmp_route, route.address);
+				g_array_append_val (tmp_route, route.prefix);
+				g_array_append_val (tmp_route, route.next_hop);
+				g_array_append_val (tmp_route, route.metric);
+				g_ptr_array_add (new_routes, tmp_route);
+			}
+		}
+
+		if (new_routes) {
+			char *method = NULL;
+
+			/* Set new routes */
+			nm_gconf_set_ip4_helper (client, iter->data,
+			                         NM_SETTING_IP4_CONFIG_ROUTES,
+			                         NM_SETTING_IP4_CONFIG_SETTING_NAME,
+			                         4,
+			                         new_routes);
+
+			g_ptr_array_foreach (new_routes, (GFunc) free_one_route, NULL);
+			g_ptr_array_free (new_routes, TRUE);
+
+			/* To make a valid ip4 setting, need a method too */
+			if (!nm_gconf_get_string_helper (client, iter->data,
+			                                 NM_SETTING_IP4_CONFIG_METHOD,
+			                                 NM_SETTING_IP4_CONFIG_SETTING_NAME,
+			                                 &method)) {				
+				/* If no method was specified, use 'auto' */
+				nm_gconf_set_string_helper (client, iter->data,
+				                            NM_SETTING_IP4_CONFIG_METHOD,
+				                            NM_SETTING_IP4_CONFIG_SETTING_NAME,
+				                            NM_SETTING_IP4_CONFIG_METHOD_AUTO);
+			}
+			g_free (method);
+		}
+
+		/* delete old key */
+		del_key = g_strdup_printf ("%s/%s/%s",
+		                           (const char *) iter->data,
+		                           NM_SETTING_VPN_SETTING_NAME,
+		                           VPN_KEY_ROUTES);
+		gconf_client_unset (client, del_key, NULL);
+		g_free (del_key);
+
+		g_slist_foreach (old_routes, (GFunc) g_free, NULL);
+		g_slist_free (old_routes);
+	}
+	free_slist (connections);
+
+	gconf_client_suggest_sync (client, NULL);
+}
+

Modified: trunk/src/gconf-helpers/gconf-upgrade.h
==============================================================================
--- trunk/src/gconf-helpers/gconf-upgrade.h	(original)
+++ trunk/src/gconf-helpers/gconf-upgrade.h	Wed Aug  6 22:29:42 2008
@@ -38,5 +38,9 @@
 
 void nm_gconf_migrate_0_7_ip4_method (GConfClient *client);
 
+void nm_gconf_migrate_0_7_ignore_dhcp_dns (GConfClient *client);
+
+void nm_gconf_migrate_0_7_vpn_routes (GConfClient *client);
+
 #endif	/* GCONF_UPGRADE_H */
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]