get-active-connections-dbus-glib.c 8.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * (C) Copyright 2010 Red Hat, Inc.
 */

/*
 * The example shows how to call the D-Bus properties interface to get the
 * list of currently active connections known to NetworkManager.  It uses
 * dbus-glib and libnm-util libraries.
 *
 * Compile with:
Dan Williams's avatar
Dan Williams committed
26
 *   gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util` get-active-connections-dbus-glib.c -o get-active-connections-dbus-glib
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 */

#include <glib.h>
#include <dbus/dbus-glib.h>

#include <nm-connection.h>
#include <nm-setting-connection.h>
#include <nm-setting-wired.h>
#include <nm-setting-ip4-config.h>
#include <NetworkManager.h>
#include <nm-utils.h>

#define DBUS_TYPE_G_MAP_OF_VARIANT          (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))
#define DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT   (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, DBUS_TYPE_G_MAP_OF_VARIANT))
#define DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH    (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH))

static void
print_connection (DBusGConnection *bus, const char *service, const char *path)
{
	DBusGProxy *proxy;
	GError *error = NULL;
	GHashTable *hash = NULL;
	NMConnection *connection = NULL;

Dan Williams's avatar
Dan Williams committed
51 52 53 54 55 56 57
	/* This function asks the Settings Service that provides this network
	 * configuration for the details of that configuration.
	 */

	/* Create the D-Bus proxy for the Settings Service so we can ask it for the
	 * connection configuration details.
	 */
58 59 60 61 62 63
	proxy = dbus_g_proxy_new_for_name (bus,
	                                   service,
	                                   path,
	                                   NM_DBUS_IFACE_SETTINGS_CONNECTION);
	g_assert (proxy);

Dan Williams's avatar
Dan Williams committed
64
	/* Request the all the configuration of the Connection */
65 66 67 68 69 70 71 72 73 74
	if (!dbus_g_proxy_call (proxy, "GetSettings", &error,
	                        G_TYPE_INVALID,
	                        DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, &hash,
	                        G_TYPE_INVALID)) {
		g_warning ("Failed to get active connection Connection property: %s",
		           error->message);
		g_error_free (error);
		goto out;
	}

Dan Williams's avatar
Dan Williams committed
75 76 77
	/* Using the raw configuration, create an NMConnection object for it.  This
	 * step also verifies that the data we got from the settings service is
	 * valid. */
78 79 80 81 82 83 84
	connection = nm_connection_new_from_hash (hash, &error);
	if (!connection) {
		g_warning ("Received invalid connection data: %s", error->message);
		g_error_free (error);
		goto out;
	}

Dan Williams's avatar
Dan Williams committed
85
	/* And finally dump all the configuration to stdout */
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	g_message ("%s => %s", service, path);
	nm_connection_dump (connection);

out:
	if (connection)
		g_object_unref (connection);
	if (hash)
		g_hash_table_destroy (hash);
	g_object_unref (proxy);
}

static void
get_active_connection_details (DBusGConnection *bus, const char *obj_path)
{
	DBusGProxy *props_proxy;
101 102
	GValue path_value = G_VALUE_INIT;
	GValue serv_value = G_VALUE_INIT;
103 104 105
	GError *error = NULL;
	const char *path = NULL, *service = NULL;

Dan Williams's avatar
Dan Williams committed
106 107 108 109 110 111 112
	/* This function gets the backing Connection object that describes the
	 * network configuration that the ActiveConnection object is actually using.
	 * The ActiveConnection object contains the mapping between the configuration
	 * and the actual network interfaces that are using that configuration.
	 */

	/* Create a D-Bus object proxy for the active connection object's properties */
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	props_proxy = dbus_g_proxy_new_for_name (bus,
	                                         NM_DBUS_SERVICE,
	                                         obj_path,
	                                         DBUS_INTERFACE_PROPERTIES);
	g_assert (props_proxy);
	
	/* Get the object path of the Connection details */
	if (!dbus_g_proxy_call (props_proxy, "Get", &error,
	                        G_TYPE_STRING, NM_DBUS_INTERFACE_ACTIVE_CONNECTION,
	                        G_TYPE_STRING, "Connection",
	                        G_TYPE_INVALID,
	                        G_TYPE_VALUE, &path_value,
	                        G_TYPE_INVALID)) {
		g_warning ("Failed to get active connection Connection property: %s",
		           error->message);
		g_error_free (error);
		goto out;
	}

	if (!G_VALUE_HOLDS (&path_value, DBUS_TYPE_G_OBJECT_PATH)) {
		g_warning ("Unexpected type returned getting Connection property: %s",
		           G_VALUE_TYPE_NAME (&path_value));
		goto out;
	}

	path = g_value_get_boxed (&path_value);
	if (!path) {
		g_warning ("Missing connection path!");
		goto out;
	}

	/* Get the service name of the D-Bus service that provides the Connection */
	if (!dbus_g_proxy_call (props_proxy, "Get", &error,
	                        G_TYPE_STRING, NM_DBUS_INTERFACE_ACTIVE_CONNECTION,
	                        G_TYPE_STRING, "ServiceName",
	                        G_TYPE_INVALID,
	                        G_TYPE_VALUE, &serv_value,
	                        G_TYPE_INVALID)) {
		g_warning ("Failed to get active connection ServiceName property: %s",
		           error->message);
		g_error_free (error);
		goto out;
	}

	if (!G_VALUE_HOLDS (&serv_value, G_TYPE_STRING)) {
		g_warning ("Unexpected type returned getting Connection property: %s",
		           G_VALUE_TYPE_NAME (&serv_value));
		goto out;
	}

	service = g_value_get_string (&serv_value);
	if (!service) {
		g_warning ("Missing connection service name!");
		goto out;
	}

Dan Williams's avatar
Dan Williams committed
169
	/* Print out the actual connection details */
170 171 172 173 174 175 176 177 178 179 180 181
	print_connection (bus, service, path);

out:
	g_value_unset (&path_value);
	g_value_unset (&serv_value);
	g_object_unref (props_proxy);
}

static void
get_active_connections (DBusGConnection *bus, DBusGProxy *proxy)
{
	GError *error = NULL;
182
	GValue value = G_VALUE_INIT;
183 184 185
	GPtrArray *paths = NULL;
	int i;

Dan Williams's avatar
Dan Williams committed
186
	/* Get the ActiveConnections property from the NM Manager object */
187 188 189 190 191 192 193 194 195 196 197
	if (!dbus_g_proxy_call (proxy, "Get", &error,
	                        G_TYPE_STRING, NM_DBUS_INTERFACE,
	                        G_TYPE_STRING, "ActiveConnections",
	                        G_TYPE_INVALID,
	                        G_TYPE_VALUE, &value,
	                        G_TYPE_INVALID)) {
		g_warning ("Failed to get ActiveConnections property: %s", error->message);
		g_error_free (error);
		return;
	}

Dan Williams's avatar
Dan Williams committed
198
	/* Make sure the ActiveConnections property is the type we expect it to be */
199 200 201 202 203 204
	if (!G_VALUE_HOLDS (&value, DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH)) {
		g_warning ("Unexpected type returned getting ActiveConnections: %s",
		           G_VALUE_TYPE_NAME (&value));
		goto out;
	}

Dan Williams's avatar
Dan Williams committed
205
	/* Extract the active connections array from the GValue */
206 207 208 209 210 211
	paths = g_value_get_boxed (&value);
	if (!paths) {
		g_warning ("Could not retrieve active connections property");
		goto out;
	}

Dan Williams's avatar
Dan Williams committed
212
	/* And print out the details of each active connection */
213 214 215 216 217 218 219 220 221 222 223 224 225
	for (i = 0; i < paths->len; i++)
		get_active_connection_details (bus, g_ptr_array_index (paths, i));

out:
	g_value_unset (&value);
}


int main (int argc, char *argv[])
{
	DBusGConnection *bus;
	DBusGProxy *props_proxy;

226
#if !GLIB_CHECK_VERSION (2, 35, 0)
227 228
	/* Initialize GType system */
	g_type_init ();
229
#endif
230 231 232 233

	/* Get system bus */
	bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);

Dan Williams's avatar
Dan Williams committed
234 235 236
	/* Create a D-Bus proxy to get the object properties from the NM Manager
	 * object.  NM_DBUS_* defines are from NetworkManager.h.
	 */
237 238 239 240 241 242
	props_proxy = dbus_g_proxy_new_for_name (bus,
	                                         NM_DBUS_SERVICE,
	                                         NM_DBUS_PATH,
	                                         DBUS_INTERFACE_PROPERTIES);
	g_assert (props_proxy);

Jiří Klimeš's avatar
Jiří Klimeš committed
243
	/* Get active connections */
244 245 246 247 248 249 250
	get_active_connections (bus, props_proxy);

	g_object_unref (props_proxy);
	dbus_g_connection_unref (bus);

	return 0;
}