summaryrefslogtreecommitdiff
path: root/examples/C/glib/get-active-connections-gdbus.c
blob: 217fd7ac0d4d2b2e3308c5300e2e64f6b9d622dc (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* -*- 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 -2014 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 GDBus, plus
 * a few defines from the NetworkManager headers.
 *
 * Compile with:
 *   gcc -Wall get-active-connections-gdbus.c -o get-active-connections-gdbus `pkg-config --cflags --libs libnm`
 */

#include <string.h>
#include <gio/gio.h>

#include <nm-dbus-interface.h>

/* include NetworkManager.h for the defines, but we don't link against
 * libnm. */
#include <NetworkManager.h>

static void
print_setting (const char *setting_name, GVariant *setting)
{
	GVariantIter iter;
	const char *property_name;
	GVariant *value;
	char *printed_value;

	g_print ("  %s:\n", setting_name);
	g_variant_iter_init (&iter, setting);
	while (g_variant_iter_next (&iter, "{&sv}", &property_name, &value)) {
		printed_value = g_variant_print (value, FALSE);
		if (strcmp (printed_value, "[]") != 0)
			g_print ("    %s: %s\n", property_name, printed_value);
		g_free (printed_value);
		g_variant_unref (value);
	}
}

static void
print_connection (const char *path)
{
	GDBusProxy *proxy;
	GError *error = NULL;
	GVariant *ret, *connection = NULL, *s_con = NULL;
	const char *id, *type;
	gboolean found;
	GVariantIter iter;
	const char *setting_name;
	GVariant *setting;

	/* This function asks NetworkManager for the details of the connection */

	/* Create the D-Bus proxy so we can ask it for the connection configuration details. */
	proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
	                                       G_DBUS_PROXY_FLAGS_NONE,
	                                       NULL,
	                                       NM_DBUS_SERVICE,
	                                       path,
	                                       NM_DBUS_INTERFACE_SETTINGS_CONNECTION,
	                                       NULL, NULL);
	g_assert (proxy);

	/* Request the all the configuration of the Connection */
	ret = g_dbus_proxy_call_sync (proxy,
	                              "GetSettings",
	                              NULL,
	                              G_DBUS_CALL_FLAGS_NONE, -1,
	                              NULL, &error);
	if (!ret) {
		g_dbus_error_strip_remote_error (error);
		g_warning ("Failed to get connection settings: %s\n", error->message);
		g_error_free (error);
		goto out;
	}

	g_variant_get (ret, "(@a{sa{sv}})", &connection);

	s_con = g_variant_lookup_value (connection, NM_SETTING_CONNECTION_SETTING_NAME, NULL);
	g_assert (s_con != NULL);
	found = g_variant_lookup (s_con, NM_SETTING_CONNECTION_ID, "&s", &id);
	g_assert (found);
	found = g_variant_lookup (s_con, NM_SETTING_CONNECTION_TYPE, "&s", &type);
	g_assert (found);

	/* Dump the configuration to stdout */
	g_print ("%s <=> %s\n", id, path);

	/* Connection setting first */
	print_setting (NM_SETTING_CONNECTION_SETTING_NAME, s_con);

	/* Then the type-specific setting */
	setting = g_variant_lookup_value (connection, type, NULL);
	if (setting) {
		print_setting (type, setting);
		g_variant_unref (setting);
	}

	g_variant_iter_init (&iter, connection);
	while (g_variant_iter_next (&iter, "{&s@a{sv}}", &setting_name, &setting)) {
		if (   strcmp (setting_name, NM_SETTING_CONNECTION_SETTING_NAME) != 0
		    && strcmp (setting_name, type) != 0)
			print_setting (setting_name, setting);
		g_variant_unref (setting);
	}
	g_print ("\n");

out:
	if (s_con)
		g_variant_unref (s_con);
	if (connection)
		g_variant_unref (connection);
	if (ret)
		g_variant_unref (ret);
	g_object_unref (proxy);
}

static void
get_active_connection_details (const char *obj_path)
{
	GDBusProxy *props_proxy;
	GVariant *ret = NULL, *path_value = NULL;
	const char *path = NULL;
	GError *error = NULL;

	/* 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 */
	props_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
	                                             G_DBUS_PROXY_FLAGS_NONE,
	                                             NULL,
	                                             NM_DBUS_SERVICE,
	                                             obj_path,
	                                             "org.freedesktop.DBus.Properties",
	                                             NULL, NULL);
	g_assert (props_proxy);

	/* Get the object path of the Connection details */
	ret = g_dbus_proxy_call_sync (props_proxy,
	                              "Get",
	                              g_variant_new ("(ss)",
	                                             NM_DBUS_INTERFACE_ACTIVE_CONNECTION,
	                                             "Connection"),
	                              G_DBUS_CALL_FLAGS_NONE, -1,
	                              NULL, &error);
	if (!ret) {
		g_dbus_error_strip_remote_error (error);
		g_warning ("Failed to get active connection Connection property: %s\n",
		           error->message);
		g_error_free (error);
		goto out;
	}

	g_variant_get (ret, "(v)", &path_value);
	if (!g_variant_is_of_type (path_value, G_VARIANT_TYPE_OBJECT_PATH)) {
		g_warning ("Unexpected type returned getting Connection property: %s",
		           g_variant_get_type_string (path_value));
		goto out;
	}

	path = g_variant_get_string (path_value, NULL);

	/* Print out the actual connection details */
	print_connection (path);

out:
	if (path_value)
		g_variant_unref (path_value);
	if (ret)
		g_variant_unref (ret);
	g_object_unref (props_proxy);
}

static void
get_active_connections (GDBusProxy *proxy)
{
	GError *error = NULL;
	GVariant *ret = NULL, *value = NULL;
	char **paths;
	int i;

	/* Get the ActiveConnections property from the NM Manager object */
	ret = g_dbus_proxy_call_sync (proxy,
	                              "Get",
	                              g_variant_new ("(ss)",
	                                             NM_DBUS_INTERFACE,
	                                             "ActiveConnections"),
	                              G_DBUS_CALL_FLAGS_NONE, -1,
	                              NULL, &error);
	if (!ret) {
		g_dbus_error_strip_remote_error (error);
		g_warning ("Failed to get ActiveConnections property: %s\n", error->message);
		g_error_free (error);
		return;
	}

	g_variant_get (ret, "(v)", &value);

	/* Make sure the ActiveConnections property is the type we expect it to be */
	if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("ao"))) {
		g_warning ("Unexpected type returned getting ActiveConnections: %s",
		           g_variant_get_type_string (value));
		goto out;
	}

	/* Extract the active connections array from the GValue */
	paths = g_variant_dup_objv (value, NULL);
	if (!paths) {
		g_warning ("Could not retrieve active connections property");
		goto out;
	}

	/* And print out the details for each active connection */
	for (i = 0; paths[i]; i++) {
		g_print ("Active connection path: %s\n", paths[i]);
		get_active_connection_details (paths[i]);
	}
	g_strfreev (paths);

out:
	if (value)
		g_variant_unref (value);
	if (ret)
		g_variant_unref (ret);
}


int
main (int argc, char *argv[])
{
	GDBusProxy *props_proxy;

#if !GLIB_CHECK_VERSION (2, 35, 0)
	/* Initialize GType system */
	g_type_init ();
#endif

	/* Create a D-Bus proxy to get the object properties from the NM Manager
	 * object.  NM_DBUS_* defines are from nm-dbus-interface.h.
	 */
	props_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
	                                             G_DBUS_PROXY_FLAGS_NONE,
	                                             NULL,
	                                             NM_DBUS_SERVICE,
	                                             NM_DBUS_PATH,
	                                             "org.freedesktop.DBus.Properties",
	                                             NULL, NULL);
	g_assert (props_proxy);

	/* Get active connections */
	get_active_connections (props_proxy);

	g_object_unref (props_proxy);

	return 0;
}