summary refs log tree commit diff
path: root/src/NetworkManagerUtils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/NetworkManagerUtils.c')
-rw-r--r--src/NetworkManagerUtils.c279
1 files changed, 244 insertions, 35 deletions
diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c
index 3a28467c..d069eca7 100644
--- a/src/NetworkManagerUtils.c
+++ b/src/NetworkManagerUtils.c
@@ -15,7 +15,7 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
- * Copyright (C) 2004 - 2010 Red Hat, Inc.
+ * Copyright (C) 2004 - 2011 Red Hat, Inc.
  * Copyright (C) 2005 - 2008 Novell, Inc.
  */
 
@@ -36,6 +36,9 @@
 #include "nm-dbus-manager.h"
 #include "nm-dispatcher-action.h"
 #include "nm-dbus-glib-types.h"
+#include "nm-setting-connection.h"
+#include "nm-setting-ip4-config.h"
+#include "nm-setting-ip6-config.h"
 
 #include <netlink/addr.h>
 #include <netinet/in.h>
@@ -371,6 +374,72 @@ nm_utils_merge_ip6_config (NMIP6Config *ip6_config, NMSettingIP6Config *setting)
 }
 
 static void
+dump_object_to_props (GObject *object, GHashTable *hash)
+{
+	GParamSpec **pspecs;
+	guint len = 0, i;
+
+	pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &len);
+	for (i = 0; i < len; i++) {
+		value_hash_add_object_property (hash,
+		                                pspecs[i]->name,
+		                                object,
+		                                pspecs[i]->name,
+		                                pspecs[i]->value_type);
+	}
+	g_free (pspecs);
+}
+
+static void
+fill_device_props (NMDevice *device,
+                   GHashTable *dev_hash,
+                   GHashTable *ip4_hash,
+                   GHashTable *ip6_hash,
+                   GHashTable *dhcp4_hash,
+                   GHashTable *dhcp6_hash)
+{
+	NMIP4Config *ip4_config;
+	NMIP6Config *ip6_config;
+	NMDHCP4Config *dhcp4_config;
+	NMDHCP6Config *dhcp6_config;
+
+	/* If the action is for a VPN, send the VPN's IP interface instead of the device's */
+	value_hash_add_str (dev_hash, NMD_DEVICE_PROPS_IP_INTERFACE, nm_device_get_ip_iface (device));
+	value_hash_add_str (dev_hash, NMD_DEVICE_PROPS_INTERFACE, nm_device_get_iface (device));
+	value_hash_add_uint (dev_hash, NMD_DEVICE_PROPS_TYPE, nm_device_get_device_type (device));
+	value_hash_add_uint (dev_hash, NMD_DEVICE_PROPS_STATE, nm_device_get_state (device));
+	value_hash_add_object_path (dev_hash, NMD_DEVICE_PROPS_PATH, nm_device_get_path (device));
+
+	ip4_config = nm_device_get_ip4_config (device);
+	if (ip4_config)
+		dump_object_to_props (G_OBJECT (ip4_config), ip4_hash);
+
+	ip6_config = nm_device_get_ip6_config (device);
+	if (ip6_config)
+		dump_object_to_props (G_OBJECT (ip6_config), ip6_hash);
+
+	dhcp4_config = nm_device_get_dhcp4_config (device);
+	if (dhcp4_config)
+		dump_object_to_props (G_OBJECT (dhcp4_config), dhcp4_hash);
+
+	dhcp6_config = nm_device_get_dhcp6_config (device);
+	if (dhcp6_config)
+		dump_object_to_props (G_OBJECT (dhcp6_config), dhcp6_hash);
+}
+
+static void
+fill_vpn_props (NMIP4Config *ip4_config,
+                NMIP6Config *ip6_config,
+                GHashTable *ip4_hash,
+                GHashTable *ip6_hash)
+{
+	if (ip4_config)
+		dump_object_to_props (G_OBJECT (ip4_config), ip4_hash);
+	if (ip6_config)
+		dump_object_to_props (G_OBJECT (ip6_config), ip6_hash);
+}
+
+static void
 dispatcher_done_cb (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data)
 {
 	dbus_g_proxy_end_call (proxy, call, NULL, G_TYPE_INVALID);
@@ -381,7 +450,9 @@ void
 nm_utils_call_dispatcher (const char *action,
                           NMConnection *connection,
                           NMDevice *device,
-                          const char *vpn_iface)
+                          const char *vpn_iface,
+                          NMIP4Config *vpn_ip4_config,
+                          NMIP6Config *vpn_ip6_config)
 {
 	NMDBusManager *dbus_mgr;
 	DBusGProxy *proxy;
@@ -389,12 +460,21 @@ nm_utils_call_dispatcher (const char *action,
 	GHashTable *connection_hash;
 	GHashTable *connection_props;
 	GHashTable *device_props;
+	GHashTable *device_ip4_props;
+	GHashTable *device_ip6_props;
+	GHashTable *device_dhcp4_props;
+	GHashTable *device_dhcp6_props;
+	GHashTable *vpn_ip4_props;
+	GHashTable *vpn_ip6_props;
 
 	g_return_if_fail (action != NULL);
 
 	/* All actions except 'hostname' require a device */
-	if (strcmp (action, "hostname"))
+	if (strcmp (action, "hostname") != 0)
 		g_return_if_fail (NM_IS_DEVICE (device));
+	/* VPN actions require at least an IPv4 config (for now) */
+	if (strcmp (action, "vpn-up") == 0)
+		g_return_if_fail (vpn_ip4_config != NULL);
 
 	dbus_mgr = nm_dbus_manager_get ();
 	g_connection = nm_dbus_manager_get_connection (dbus_mgr);
@@ -409,21 +489,10 @@ nm_utils_call_dispatcher (const char *action,
 	}
 
 	if (connection) {
-		connection_hash = nm_connection_to_hash (connection);
+		connection_hash = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_NO_SECRETS);
 
 		connection_props = value_hash_create ();
 
-		/* Service name */
-		if (nm_connection_get_scope (connection) == NM_CONNECTION_SCOPE_USER) {
-			value_hash_add_str (connection_props,
-								NMD_CONNECTION_PROPS_SERVICE_NAME,
-								NM_DBUS_SERVICE_USER_SETTINGS);
-		} else if (nm_connection_get_scope (connection) == NM_CONNECTION_SCOPE_SYSTEM) {
-			value_hash_add_str (connection_props,
-								NMD_CONNECTION_PROPS_SERVICE_NAME,
-								NM_DBUS_SERVICE_SYSTEM_SETTINGS);
-		}
-
 		/* path */
 		value_hash_add_object_path (connection_props,
 									NMD_CONNECTION_PROPS_PATH,
@@ -434,25 +503,23 @@ nm_utils_call_dispatcher (const char *action,
 	}
 
 	device_props = value_hash_create ();
-
-	/* Hostname actions do not require a device */
-	if (strcmp (action, "hostname")) {
-		/* interface */
-		value_hash_add_str (device_props, NMD_DEVICE_PROPS_INTERFACE, nm_device_get_iface (device));
-
-		/* IP interface */
-		if (vpn_iface) {
-			value_hash_add_str (device_props, NMD_DEVICE_PROPS_IP_INTERFACE, vpn_iface);
-		} else if (nm_device_get_ip_iface (device)) {
-			value_hash_add_str (device_props, NMD_DEVICE_PROPS_IP_INTERFACE, nm_device_get_ip_iface (device));
-		}
-
-		/* type */
-		value_hash_add_uint (device_props, NMD_DEVICE_PROPS_TYPE, nm_device_get_device_type (device));
-
-		/* state */
-		value_hash_add_uint (device_props, NMD_DEVICE_PROPS_STATE, nm_device_get_state (device));
-		value_hash_add_object_path (device_props, NMD_DEVICE_PROPS_PATH, nm_device_get_path (device));
+	device_ip4_props = value_hash_create ();
+	device_ip6_props = value_hash_create ();
+	device_dhcp4_props = value_hash_create ();
+	device_dhcp6_props = value_hash_create ();
+	vpn_ip4_props = value_hash_create ();
+	vpn_ip6_props = value_hash_create ();
+
+	/* hostname actions only send the hostname */
+	if (strcmp (action, "hostname") != 0) {
+		fill_device_props (device,
+			               device_props,
+			               device_ip4_props,
+			               device_ip6_props,
+			               device_dhcp4_props,
+			               device_dhcp6_props);
+		if (vpn_iface)
+			fill_vpn_props (vpn_ip4_config, NULL, vpn_ip4_props, vpn_ip6_props);
 	}
 
 	/* Do a non-blocking call, but wait for the reply, because dbus-glib
@@ -463,17 +530,30 @@ nm_utils_call_dispatcher (const char *action,
 	 */
 	dbus_g_proxy_begin_call_with_timeout (proxy, "Action",
 	                                      dispatcher_done_cb,
-	                                      dbus_mgr,
+	                                      dbus_mgr,       /* automatically unref the dbus mgr when call is done */
 	                                      g_object_unref,
 	                                      5000,
 	                                      G_TYPE_STRING, action,
 	                                      DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, connection_hash,
 	                                      DBUS_TYPE_G_MAP_OF_VARIANT, connection_props,
 	                                      DBUS_TYPE_G_MAP_OF_VARIANT, device_props,
+	                                      DBUS_TYPE_G_MAP_OF_VARIANT, device_ip4_props,
+	                                      DBUS_TYPE_G_MAP_OF_VARIANT, device_ip6_props,
+	                                      DBUS_TYPE_G_MAP_OF_VARIANT, device_dhcp4_props,
+	                                      DBUS_TYPE_G_MAP_OF_VARIANT, device_dhcp6_props,
+	                                      G_TYPE_STRING, vpn_iface ? vpn_iface : "",
+	                                      DBUS_TYPE_G_MAP_OF_VARIANT, vpn_ip4_props,
+	                                      DBUS_TYPE_G_MAP_OF_VARIANT, vpn_ip6_props,
 	                                      G_TYPE_INVALID);
 	g_hash_table_destroy (connection_hash);
 	g_hash_table_destroy (connection_props);
 	g_hash_table_destroy (device_props);
+	g_hash_table_destroy (device_ip4_props);
+	g_hash_table_destroy (device_ip6_props);
+	g_hash_table_destroy (device_dhcp4_props);
+	g_hash_table_destroy (device_dhcp6_props);
+	g_hash_table_destroy (vpn_ip4_props);
+	g_hash_table_destroy (vpn_ip6_props);
 }
 
 gboolean
@@ -678,6 +758,21 @@ value_hash_add_bool (GHashTable *hash,
 	value_hash_add (hash, key, value);
 }
 
+void
+value_hash_add_object_property (GHashTable *hash,
+                                const char *key,
+                                GObject *object,
+                                const char *prop,
+                                GType val_type)
+{
+	GValue *value;
+
+	value = g_slice_new0 (GValue);
+	g_value_init (value, val_type);
+	g_object_get_property (object, prop, value);
+	value_hash_add (hash, key, value);
+}
+
 gboolean
 nm_utils_do_sysctl (const char *path, const char *value)
 {
@@ -733,3 +828,117 @@ nm_utils_get_proc_sys_net_value (const char *path,
 	return success;
 }
 
+static char *
+get_new_connection_name (const GSList *existing,
+                         const char *format,
+                         const char *preferred)
+{
+	GSList *names = NULL;
+	const GSList *iter;
+	char *cname = NULL;
+	int i = 0;
+	gboolean preferred_found = FALSE;
+
+	for (iter = existing; iter; iter = g_slist_next (iter)) {
+		NMConnection *candidate = NM_CONNECTION (iter->data);
+		const char *id;
+
+		id = nm_connection_get_id (candidate);
+		g_assert (id);
+		names = g_slist_append (names, (gpointer) id);
+
+		if (preferred && !preferred_found && (strcmp (preferred, id) == 0))
+			preferred_found = TRUE;
+	}
+
+	/* Return the preferred name if it was unique */
+	if (preferred && !preferred_found) {
+		g_slist_free (names);
+		return g_strdup (preferred);
+	}
+
+	/* Otherwise find the next available unique connection name using the given
+	 * connection name template.
+	 */
+	while (!cname && (i++ < 10000)) {
+		char *temp;
+		gboolean found = FALSE;
+
+		temp = g_strdup_printf (format, i);
+		for (iter = names; iter; iter = g_slist_next (iter)) {
+			if (!strcmp (iter->data, temp)) {
+				found = TRUE;
+				break;
+			}
+		}
+		if (!found)
+			cname = temp;
+		else
+			g_free (temp);
+	}
+
+	g_slist_free (names);
+	return cname;
+}
+
+void
+nm_utils_complete_generic (NMConnection *connection,
+                           const char *ctype,
+                           const GSList *existing,
+                           const char *format,
+                           const char *preferred,
+                           gboolean default_enable_ipv6)
+{
+	NMSettingConnection *s_con;
+	NMSettingIP4Config *s_ip4;
+	NMSettingIP6Config *s_ip6;
+	const char *method;
+	char *id, *uuid;
+
+	s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
+	if (!s_con) {
+		s_con = (NMSettingConnection *) nm_setting_connection_new ();
+		nm_connection_add_setting (connection, NM_SETTING (s_con));
+	}
+	g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_TYPE, ctype, NULL);
+
+	if (!nm_setting_connection_get_uuid (s_con)) {
+		uuid = nm_utils_uuid_generate ();
+		g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_UUID, uuid, NULL);
+		g_free (uuid);
+	}
+
+	/* Add a connection ID if absent */
+	if (!nm_setting_connection_get_id (s_con)) {
+		id = get_new_connection_name (existing, format, preferred);
+		g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_ID, id, NULL);
+		g_free (id);
+	}
+
+	/* Add an 'auto' IPv4 connection if present */
+	s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
+	if (!s_ip4) {
+		s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
+		nm_connection_add_setting (connection, NM_SETTING (s_ip4));
+	}
+	method = nm_setting_ip4_config_get_method (s_ip4);
+	if (!method) {
+		g_object_set (G_OBJECT (s_ip4),
+		              NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
+		              NULL);
+	}
+
+	/* Add an 'auto' IPv6 setting if allowed and not preset */
+	s_ip6 = (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
+	if (!s_ip6 && default_enable_ipv6) {
+		s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new ();
+		nm_connection_add_setting (connection, NM_SETTING (s_ip6));
+	}
+	if (s_ip6 && !nm_setting_ip6_config_get_method (s_ip6)) {
+		g_object_set (G_OBJECT (s_ip6),
+		              NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO,
+		              NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE,
+		              NULL);
+	}
+}
+