libnm-glib-test.c 10.5 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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * libnm_glib -- Access network status & information from glib applications
 *
 * 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.
 *
 * Copyright (C) 2007 - 2008 Novell, Inc.
 * Copyright (C) 2007 - 2008 Red Hat, Inc.
 */

23 24
#include "config.h"

25
#include <stdlib.h>
26
#include <signal.h>
27
#include <string.h>
28 29 30 31

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
32

33 34
#include "nm-client.h"
#include "nm-device.h"
35
#include "nm-device-ethernet.h"
36
#include "nm-device-wifi.h"
37
#include "nm-utils.h"
38
#include "nm-active-connection.h"
39
#include "nm-vpn-connection.h"
40
#include "nm-setting-ip4-config.h"
41 42 43 44 45 46 47 48 49

static gboolean
test_wireless_enabled (NMClient *client)
{
	gboolean wireless;

	wireless = nm_client_wireless_get_enabled (client);
	g_print ("Wireless enabled? %s\n", wireless ? "yes" : "no");

50
//	nm_client_wireless_set_enabled (client, !wireless);
51

52 53
	wireless = nm_client_wireless_hardware_get_enabled (client);
	g_print ("Wireless HW enabled? %s\n", wireless ? "yes" : "no");
54

55
//	nm_client_wireless_set_enabled (client, !wireless);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

	return TRUE;
}

static gboolean
test_get_state (NMClient *client)
{
	guint state;

	state = nm_client_get_state (client);
	g_print ("Current state: %d\n", state);

	return TRUE;
}

71 72 73
static gchar *
ip4_address_as_string (guint32 ip)
{
74
	char buf[INET_ADDRSTRLEN+1];
75
	guint32 tmp_addr;
76

77
	memset (&buf, '\0', sizeof (buf));
78
	tmp_addr = ip;
79

80 81 82
	if (inet_ntop (AF_INET, &tmp_addr, buf, INET_ADDRSTRLEN)) {
		return g_strdup (buf);
	} else {
83
		g_warning ("%s: error converting IP4 address 0x%X",
84
		           __func__, ntohl (tmp_addr));
85 86
		return NULL;
	}
87 88 89 90 91 92
}

static void
dump_ip4_config (NMIP4Config *cfg)
{
	char *tmp;
93 94
	const GArray *array;
	const GPtrArray *ptr_array;
95
	GSList *iter;
96 97
	int i;

98
	for (iter = (GSList *) nm_ip4_config_get_addresses (cfg); iter; iter = g_slist_next (iter)) {
99 100
		NMIP4Address *addr = iter->data;
		guint32 u;
101

102
		tmp = ip4_address_as_string (nm_ip4_address_get_address (addr));
103 104
		g_print ("IP4 address: %s\n", tmp);
		g_free (tmp);
105

106 107 108
		u = nm_ip4_address_get_prefix (addr);
		tmp = ip4_address_as_string (nm_utils_ip4_prefix_to_netmask (u));
		g_print ("IP4 prefix: %d (%s)\n", u, tmp);
109
		g_free (tmp);
110

111
		tmp = ip4_address_as_string (nm_ip4_address_get_gateway (addr));
112 113 114
		g_print ("IP4 gateway: %s\n\n", tmp);
		g_free (tmp);
	}
115 116 117 118 119 120 121 122 123 124 125 126 127 128

	array = nm_ip4_config_get_nameservers (cfg);
	if (array) {
		g_print ("IP4 DNS:\n");
		for (i = 0; i < array->len; i++) {
			tmp = ip4_address_as_string (g_array_index (array, guint32, i));
			g_print ("\t%s\n", tmp);
			g_free (tmp);
		}
	}

	ptr_array = nm_ip4_config_get_domains (cfg);
	if (ptr_array) {
		g_print ("IP4 domains:\n");
129 130
		for (i = 0; i < ptr_array->len; i++)
			g_print ("\t%s\n", (const char *) g_ptr_array_index (ptr_array, i));
131
	}
132 133 134 135 136 137 138 139 140 141

	array = nm_ip4_config_get_wins_servers (cfg);
	if (array) {
		g_print ("IP4 WINS:\n");
		for (i = 0; i < array->len; i++) {
			tmp = ip4_address_as_string (g_array_index (array, guint32, i));
			g_print ("\t%s\n", tmp);
			g_free (tmp);
		}
	}
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
static void
print_one_dhcp4_option (gpointer key, gpointer data, gpointer user_data)
{
	const char *option = (const char *) key;
	const char *value = (const char *) data;

	g_print ("  %s:   %s\n", option, value);
}

static void
dump_dhcp4_config (NMDHCP4Config *config)
{
	GHashTable *options = NULL;

	if (!config)
		return;

	g_print ("\nDHCP4 Options:\n");
	g_print ("-------------------------------------\n");

	g_object_get (G_OBJECT (config), NM_DHCP4_CONFIG_OPTIONS, &options, NULL);
	g_hash_table_foreach (options, print_one_dhcp4_option, NULL);
}

168 169 170
static void
dump_access_point (NMAccessPoint *ap)
{
171
	const GByteArray * ssid;
172
	const char * str;
173

174 175 176
	ssid = nm_access_point_get_ssid (ap);
	g_print ("\tSsid: %s\n",
	         ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(none)");
177

178
	str = nm_access_point_get_bssid (ap);
179 180
	g_print ("\tMAC Address: %s\n", str);

181 182 183
	g_print ("\tFlags: 0x%X\n", nm_access_point_get_flags (ap));
	g_print ("\tWPA Flags: 0x%X\n", nm_access_point_get_wpa_flags (ap));
	g_print ("\tRSN Flags: 0x%X\n", nm_access_point_get_rsn_flags (ap));
184
	g_print ("\tFrequency: %u\n", nm_access_point_get_frequency (ap));
185 186

	g_print ("\tMode: %d\n", nm_access_point_get_mode (ap));
187
	g_print ("\tRate: %d\n", nm_access_point_get_max_bitrate (ap));
188 189 190
	g_print ("\tStrength: %d\n", nm_access_point_get_strength (ap));
}

191
static void
192
dump_wireless (NMDeviceWifi *device)
193
{
194
	const char *str;
195
	const GPtrArray *aps;
196
	int i;
197

198 199
	g_print ("Mode: %d\n", nm_device_wifi_get_mode (device));
	g_print ("Bitrate: %d\n", nm_device_wifi_get_bitrate (device));
200

201
	str = nm_device_wifi_get_hw_address (device);
202 203
	g_print ("MAC: %s\n", str);

204
	g_print ("AccessPoints:\n");
205
	aps = nm_device_wifi_get_access_points (device);
206
	for (i = 0; aps && (i < aps->len); i++) {
207
		dump_access_point (NM_ACCESS_POINT (g_ptr_array_index (aps, i)));
208 209 210 211 212
		g_print ("\n");
	}
}

static void
213
dump_wired (NMDeviceEthernet *device)
214
{
215
	const char *str;
216

217
	g_print ("Speed: %d\n", nm_device_ethernet_get_speed (device));
218

219
	str = nm_device_ethernet_get_hw_address (device);
220 221
	g_print ("MAC: %s\n", str);
}
222 223 224 225

static void
dump_device (NMDevice *device)
{
226
	const char *str;
227 228 229 230 231 232 233 234 235 236 237
	NMDeviceState state;

	str = nm_device_get_iface (device);
	g_print ("Interface: %s\n", str);

	str = nm_device_get_udi (device);
	g_print ("Udi: %s\n", str);

	str = nm_device_get_driver (device);
	g_print ("Driver: %s\n", str);

238 239 240 241 242
	str = nm_device_get_vendor (device);
	g_print ("Vendor: %s\n", str);

	str = nm_device_get_product (device);
	g_print ("Product: %s\n", str);
243 244 245 246

	state = nm_device_get_state (device);
	g_print ("State: %d\n", state);

247 248
	if (state == NM_DEVICE_STATE_ACTIVATED)
		dump_ip4_config (nm_device_get_ip4_config (device));
249

250 251
	if (NM_IS_DEVICE_ETHERNET (device))
		dump_wired (NM_DEVICE_ETHERNET (device));
252 253
	else if (NM_IS_DEVICE_WIFI (device))
		dump_wireless (NM_DEVICE_WIFI (device));
254 255

	dump_dhcp4_config (nm_device_get_dhcp4_config (device));
256 257 258 259 260
}

static gboolean
test_devices (NMClient *client)
{
261
	const GPtrArray *devices;
262
	int i;
263

264
	devices = nm_client_get_devices (client);
265
	g_print ("Got devices:\n");
266 267 268 269 270 271 272
	if (!devices) {
		g_print ("  NONE\n");
		return TRUE;
	}

	for (i = 0; i < devices->len; i++) {
		NMDevice *device = g_ptr_array_index (devices, i);
273 274 275 276 277 278 279 280
		dump_device (device);
		g_print ("\n");
	}

	return TRUE;
}

static void
281
active_connections_changed (NMClient *client, GParamSpec *pspec, gpointer user_data)
282
{
283 284 285 286 287
	const GPtrArray *connections;
	int i, j;

	g_print ("Active connections changed:\n");
	connections = nm_client_get_active_connections (client);
288
	for (i = 0; connections && (i < connections->len); i++) {
289 290 291 292 293 294
		NMActiveConnection *connection;
		const GPtrArray *devices;

		connection = g_ptr_array_index (connections, i);
		g_print ("    %s\n", nm_object_get_path (NM_OBJECT (connection)));
		devices = nm_active_connection_get_devices (connection);
295
		for (j = 0; devices && j < devices->len; j++)
296
			g_print ("           %s\n", nm_device_get_udi (g_ptr_array_index (devices, j)));
297 298
		if (NM_IS_VPN_CONNECTION (connection))
			g_print ("           VPN base connection: %s\n", nm_active_connection_get_specific_object (connection));
299
	}
300 301 302
}

static void
303
show_active_connection_device (gpointer data, gpointer user_data)
304
{
305 306 307
	NMDevice *device = NM_DEVICE (data);

	g_print ("           %s\n", nm_device_get_udi (device));
308 309
}

310 311
static void
test_get_active_connections (NMClient *client)
312
{
313 314
	const GPtrArray *connections;
	int i;
315

316 317
	g_print ("Active connections:\n");
	connections = nm_client_get_active_connections (client);
318
	for (i = 0; connections && (i < connections->len); i++) {
319
		const GPtrArray *devices;
320

321 322
		g_print ("    %s\n", nm_object_get_path (g_ptr_array_index (connections, i)));
		devices = nm_active_connection_get_devices (g_ptr_array_index (connections, i));
323 324
		if (devices)
			g_ptr_array_foreach ((GPtrArray *) devices, show_active_connection_device, NULL);
325
	}
326 327 328
}

static void
329
device_state_changed (NMDevice *device, GParamSpec *pspec, gpointer user_data)
330
{
331 332 333 334
	g_print ("Device state changed: %s %d\n",
	         nm_device_get_iface (device),
	         nm_device_get_state (device));
}
335

336 337 338 339 340 341 342
static void
device_added_cb (NMClient *client, NMDevice *device, gpointer user_data)
{
	g_print ("New device added\n");
	dump_device (device);
	g_signal_connect (G_OBJECT (device), "notify::state",
	                  (GCallback) device_state_changed, NULL);
343 344
}

345
static void
346
device_removed_cb (NMClient *client, NMDevice *device, gpointer user_data)
347
{
348 349 350 351 352 353 354 355
	g_print ("Device removed\n");
	dump_device (device);
}

static void
manager_running (NMClient *client, GParamSpec *pspec, gpointer user_data)
{
	if (nm_client_get_manager_running (client)) {
356
		g_print ("NM appeared\n");
357
		test_wireless_enabled (client);
358
		test_get_state (client);
359
		test_get_active_connections (client);
360 361 362 363 364
		test_devices (client);
	} else
		g_print ("NM disappeared\n");
}

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
static GMainLoop *loop = NULL;

static void
signal_handler (int signo)
{
	if (signo == SIGINT || signo == SIGTERM) {
		g_message ("Caught signal %d, shutting down...", signo);
		g_main_loop_quit (loop);
	}
}

static void
setup_signals (void)
{
	struct sigaction action;
	sigset_t mask;

	sigemptyset (&mask);
	action.sa_handler = signal_handler;
	action.sa_mask = mask;
	action.sa_flags = 0;
	sigaction (SIGTERM,  &action, NULL);
	sigaction (SIGINT,  &action, NULL);
}

390 391 392 393 394
int
main (int argc, char *argv[])
{
	NMClient *client;

395
#if !GLIB_CHECK_VERSION (2, 35, 0)
396
	g_type_init ();
397
#endif
398 399 400 401 402 403

	client = nm_client_new ();
	if (!client) {
		exit (1);
	}

404 405 406 407 408
	g_signal_connect (client, "notify::" NM_CLIENT_MANAGER_RUNNING,
	                  G_CALLBACK (manager_running), NULL);
	g_signal_connect (client, "notify::" NM_CLIENT_ACTIVE_CONNECTIONS,
	                  G_CALLBACK (active_connections_changed), NULL);
	manager_running (client, NULL, NULL);
409 410 411 412 413 414

	g_signal_connect (client, "device-added",
					  G_CALLBACK (device_added_cb), NULL);
	g_signal_connect (client, "device-removed",
					  G_CALLBACK (device_removed_cb), NULL);

415 416 417
	loop = g_main_loop_new (NULL, FALSE);
	setup_signals ();
	g_main_loop_run (loop);
418 419 420 421 422

	g_object_unref (client);

	return 0;
}