1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * libnm_glib -- Access network status & information from glib applications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * Copyright (C) 2007 - 2008 Novell, Inc.
20 * Copyright (C) 2007 - 2008 Red Hat, Inc.
23 #include "nm-default.h"
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
32 #include "nm-client.h"
33 #include "nm-device.h"
34 #include "nm-device-ethernet.h"
35 #include "nm-device-wifi.h"
36 #include "nm-device-generic.h"
38 #include "nm-active-connection.h"
39 #include "nm-vpn-connection.h"
40 #include "nm-setting-ip4-config.h"
43 test_wireless_enabled (NMClient *client)
47 wireless = nm_client_wireless_get_enabled (client);
48 g_print ("Wireless enabled? %s\n", wireless ? "yes" : "no");
50 // nm_client_wireless_set_enabled (client, !wireless);
52 wireless = nm_client_wireless_hardware_get_enabled (client);
53 g_print ("Wireless HW enabled? %s\n", wireless ? "yes" : "no");
55 // nm_client_wireless_set_enabled (client, !wireless);
61 test_get_state (NMClient *client)
65 state = nm_client_get_state (client);
66 g_print ("Current state: %d\n", state);
72 ip4_address_as_string (guint32 ip)
74 char buf[INET_ADDRSTRLEN+1];
77 memset (&buf, '\0', sizeof (buf));
80 if (inet_ntop (AF_INET, &tmp_addr, buf, INET_ADDRSTRLEN)) {
81 return g_strdup (buf);
83 g_warning ("%s: error converting IP4 address 0x%X",
84 __func__, ntohl (tmp_addr));
90 dump_ip4_config (NMIP4Config *cfg)
94 const GPtrArray *ptr_array;
98 for (iter = (GSList *) nm_ip4_config_get_addresses (cfg); iter; iter = g_slist_next (iter)) {
99 NMIP4Address *addr = iter->data;
102 tmp = ip4_address_as_string (nm_ip4_address_get_address (addr));
103 g_print ("IP4 address: %s\n", tmp);
106 u = nm_ip4_address_get_prefix (addr);
107 tmp = ip4_address_as_string (nm_utils_ip4_prefix_to_netmask (u));
108 g_print ("IP4 prefix: %d (%s)\n", u, tmp);
111 tmp = ip4_address_as_string (nm_ip4_address_get_gateway (addr));
112 g_print ("IP4 gateway: %s\n\n", tmp);
116 array = nm_ip4_config_get_nameservers (cfg);
118 g_print ("IP4 DNS:\n");
119 for (i = 0; i < array->len; i++) {
120 tmp = ip4_address_as_string (g_array_index (array, guint32, i));
121 g_print ("\t%s\n", tmp);
126 ptr_array = nm_ip4_config_get_domains (cfg);
128 g_print ("IP4 domains:\n");
129 for (i = 0; i < ptr_array->len; i++)
130 g_print ("\t%s\n", (const char *) g_ptr_array_index (ptr_array, i));
133 array = nm_ip4_config_get_wins_servers (cfg);
135 g_print ("IP4 WINS:\n");
136 for (i = 0; i < array->len; i++) {
137 tmp = ip4_address_as_string (g_array_index (array, guint32, i));
138 g_print ("\t%s\n", tmp);
145 print_one_dhcp4_option (gpointer key, gpointer data, gpointer user_data)
147 const char *option = (const char *) key;
148 const char *value = (const char *) data;
150 g_print (" %s: %s\n", option, value);
154 dump_dhcp4_config (NMDHCP4Config *config)
156 GHashTable *options = NULL;
161 g_print ("\nDHCP4 Options:\n");
162 g_print ("-------------------------------------\n");
164 g_object_get (G_OBJECT (config), NM_DHCP4_CONFIG_OPTIONS, &options, NULL);
165 g_hash_table_foreach (options, print_one_dhcp4_option, NULL);
169 dump_access_point (NMAccessPoint *ap)
171 const GByteArray * ssid;
174 ssid = nm_access_point_get_ssid (ap);
175 g_print ("\tSsid: %s\n",
176 ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(none)");
178 str = nm_access_point_get_bssid (ap);
179 g_print ("\tMAC Address: %s\n", str);
181 g_print ("\tFlags: 0x%X\n", nm_access_point_get_flags (ap));
182 g_print ("\tWPA Flags: 0x%X\n", nm_access_point_get_wpa_flags (ap));
183 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));
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 g_print ("\tStrength: %d\n", nm_access_point_get_strength (ap));
192 dump_wireless (NMDeviceWifi *device)
195 const GPtrArray *aps;
198 g_print ("Mode: %d\n", nm_device_wifi_get_mode (device));
199 g_print ("Bitrate: %d\n", nm_device_wifi_get_bitrate (device));
201 str = nm_device_wifi_get_hw_address (device);
202 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)));
213 dump_generic (NMDeviceGeneric *device)
215 g_print ("HW address: %s\n", nm_device_generic_get_hw_address (device));
219 dump_wired (NMDeviceEthernet *device)
223 g_print ("Speed: %d\n", nm_device_ethernet_get_speed (device));
225 str = nm_device_ethernet_get_hw_address (device);
226 g_print ("MAC: %s\n", str);
230 dump_device (NMDevice *device)
235 str = nm_device_get_iface (device);
236 g_print ("Interface: %s\n", str);
238 str = nm_device_get_udi (device);
239 g_print ("Udi: %s\n", str);
241 str = nm_device_get_driver (device);
242 g_print ("Driver: %s\n", str);
244 str = nm_device_get_vendor (device);
245 g_print ("Vendor: %s\n", str);
247 str = nm_device_get_product (device);
248 g_print ("Product: %s\n", str);
250 state = nm_device_get_state (device);
251 g_print ("State: %d\n", state);
253 if (state == NM_DEVICE_STATE_ACTIVATED)
254 dump_ip4_config (nm_device_get_ip4_config (device));
256 if (NM_IS_DEVICE_ETHERNET (device))
257 dump_wired (NM_DEVICE_ETHERNET (device));
258 else if (NM_IS_DEVICE_WIFI (device))
259 dump_wireless (NM_DEVICE_WIFI (device));
260 else if (NM_IS_DEVICE_GENERIC (device))
261 dump_generic (NM_DEVICE_GENERIC (device));
263 dump_dhcp4_config (nm_device_get_dhcp4_config (device));
267 test_devices (NMClient *client)
269 const GPtrArray *devices;
272 devices = nm_client_get_devices (client);
273 g_print ("Got devices:\n");
279 for (i = 0; i < devices->len; i++) {
280 NMDevice *device = g_ptr_array_index (devices, i);
281 dump_device (device);
289 active_connections_changed (NMClient *client, GParamSpec *pspec, gpointer user_data)
291 const GPtrArray *connections;
294 g_print ("Active connections changed:\n");
295 connections = nm_client_get_active_connections (client);
296 for (i = 0; connections && (i < connections->len); i++) {
297 NMActiveConnection *connection;
298 const GPtrArray *devices;
300 connection = g_ptr_array_index (connections, i);
301 g_print (" %s\n", nm_object_get_path (NM_OBJECT (connection)));
302 devices = nm_active_connection_get_devices (connection);
303 for (j = 0; devices && j < devices->len; j++)
304 g_print (" %s\n", nm_device_get_udi (g_ptr_array_index (devices, j)));
305 if (NM_IS_VPN_CONNECTION (connection))
306 g_print (" VPN base connection: %s\n", nm_active_connection_get_specific_object (connection));
311 show_active_connection_device (gpointer data, gpointer user_data)
313 NMDevice *device = NM_DEVICE (data);
315 g_print (" %s\n", nm_device_get_udi (device));
319 test_get_active_connections (NMClient *client)
321 const GPtrArray *connections;
324 g_print ("Active connections:\n");
325 connections = nm_client_get_active_connections (client);
326 for (i = 0; connections && (i < connections->len); i++) {
327 const GPtrArray *devices;
329 g_print (" %s\n", nm_object_get_path (g_ptr_array_index (connections, i)));
330 devices = nm_active_connection_get_devices (g_ptr_array_index (connections, i));
332 g_ptr_array_foreach ((GPtrArray *) devices, show_active_connection_device, NULL);
337 device_state_changed (NMDevice *device, GParamSpec *pspec, gpointer user_data)
339 g_print ("Device state changed: %s %d\n",
340 nm_device_get_iface (device),
341 nm_device_get_state (device));
345 device_added_cb (NMClient *client, NMDevice *device, gpointer user_data)
347 g_print ("New device added\n");
348 dump_device (device);
349 g_signal_connect (G_OBJECT (device), "notify::state",
350 (GCallback) device_state_changed, NULL);
354 device_removed_cb (NMClient *client, NMDevice *device, gpointer user_data)
356 g_print ("Device removed\n");
357 dump_device (device);
361 manager_running (NMClient *client, GParamSpec *pspec, gpointer user_data)
363 if (nm_client_get_manager_running (client)) {
364 g_print ("NM appeared\n");
365 test_wireless_enabled (client);
366 test_get_state (client);
367 test_get_active_connections (client);
368 test_devices (client);
370 g_print ("NM disappeared\n");
373 static GMainLoop *loop = NULL;
376 signal_handler (int signo)
378 if (signo == SIGINT || signo == SIGTERM) {
379 g_message ("Caught signal %d, shutting down...", signo);
380 g_main_loop_quit (loop);
387 struct sigaction action;
391 action.sa_handler = signal_handler;
392 action.sa_mask = mask;
394 sigaction (SIGTERM, &action, NULL);
395 sigaction (SIGINT, &action, NULL);
399 main (int argc, char *argv[])
405 client = nm_client_new ();
410 g_signal_connect (client, "notify::" NM_CLIENT_MANAGER_RUNNING,
411 G_CALLBACK (manager_running), NULL);
412 g_signal_connect (client, "notify::" NM_CLIENT_ACTIVE_CONNECTIONS,
413 G_CALLBACK (active_connections_changed), NULL);
414 manager_running (client, NULL, NULL);
416 g_signal_connect (client, "device-added",
417 G_CALLBACK (device_added_cb), NULL);
418 g_signal_connect (client, "device-removed",
419 G_CALLBACK (device_removed_cb), NULL);
421 loop = g_main_loop_new (NULL, FALSE);
423 g_main_loop_run (loop);
425 g_object_unref (client);