device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / libnm-glib-test.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3  * libnm_glib -- Access network status & information from glib applications
4  *
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.
9  *
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.
14  *
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.
18  *
19  * Copyright (C) 2007 - 2008 Novell, Inc.
20  * Copyright (C) 2007 - 2008 Red Hat, Inc.
21  */
22
23 #include "nm-default.h"
24
25 #include <stdlib.h>
26 #include <signal.h>
27 #include <string.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31
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"
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 static gboolean
43 test_wireless_enabled (NMClient *client)
44 {
45         gboolean wireless;
46
47         wireless = nm_client_wireless_get_enabled (client);
48         g_print ("Wireless enabled? %s\n", wireless ? "yes" : "no");
49
50 //      nm_client_wireless_set_enabled (client, !wireless);
51
52         wireless = nm_client_wireless_hardware_get_enabled (client);
53         g_print ("Wireless HW enabled? %s\n", wireless ? "yes" : "no");
54
55 //      nm_client_wireless_set_enabled (client, !wireless);
56
57         return TRUE;
58 }
59
60 static gboolean
61 test_get_state (NMClient *client)
62 {
63         guint state;
64
65         state = nm_client_get_state (client);
66         g_print ("Current state: %d\n", state);
67
68         return TRUE;
69 }
70
71 static gchar *
72 ip4_address_as_string (guint32 ip)
73 {
74         char buf[INET_ADDRSTRLEN+1];
75         guint32 tmp_addr;
76
77         memset (&buf, '\0', sizeof (buf));
78         tmp_addr = ip;
79
80         if (inet_ntop (AF_INET, &tmp_addr, buf, INET_ADDRSTRLEN)) {
81                 return g_strdup (buf);
82         } else {
83                 g_warning ("%s: error converting IP4 address 0x%X",
84                            __func__, ntohl (tmp_addr));
85                 return NULL;
86         }
87 }
88
89 static void
90 dump_ip4_config (NMIP4Config *cfg)
91 {
92         char *tmp;
93         const GArray *array;
94         const GPtrArray *ptr_array;
95         GSList *iter;
96         int i;
97
98         for (iter = (GSList *) nm_ip4_config_get_addresses (cfg); iter; iter = g_slist_next (iter)) {
99                 NMIP4Address *addr = iter->data;
100                 guint32 u;
101
102                 tmp = ip4_address_as_string (nm_ip4_address_get_address (addr));
103                 g_print ("IP4 address: %s\n", tmp);
104                 g_free (tmp);
105
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);
109                 g_free (tmp);
110
111                 tmp = ip4_address_as_string (nm_ip4_address_get_gateway (addr));
112                 g_print ("IP4 gateway: %s\n\n", tmp);
113                 g_free (tmp);
114         }
115
116         array = nm_ip4_config_get_nameservers (cfg);
117         if (array) {
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);
122                         g_free (tmp);
123                 }
124         }
125
126         ptr_array = nm_ip4_config_get_domains (cfg);
127         if (ptr_array) {
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));
131         }
132
133         array = nm_ip4_config_get_wins_servers (cfg);
134         if (array) {
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);
139                         g_free (tmp);
140                 }
141         }
142 }
143
144 static void
145 print_one_dhcp4_option (gpointer key, gpointer data, gpointer user_data)
146 {
147         const char *option = (const char *) key;
148         const char *value = (const char *) data;
149
150         g_print ("  %s:   %s\n", option, value);
151 }
152
153 static void
154 dump_dhcp4_config (NMDHCP4Config *config)
155 {
156         GHashTable *options = NULL;
157
158         if (!config)
159                 return;
160
161         g_print ("\nDHCP4 Options:\n");
162         g_print ("-------------------------------------\n");
163
164         g_object_get (G_OBJECT (config), NM_DHCP4_CONFIG_OPTIONS, &options, NULL);
165         g_hash_table_foreach (options, print_one_dhcp4_option, NULL);
166 }
167
168 static void
169 dump_access_point (NMAccessPoint *ap)
170 {
171         const GByteArray * ssid;
172         const char * str;
173
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)");
177
178         str = nm_access_point_get_bssid (ap);
179         g_print ("\tMAC Address: %s\n", str);
180
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));
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         g_print ("\tStrength: %d\n", nm_access_point_get_strength (ap));
189 }
190
191 static void
192 dump_wireless (NMDeviceWifi *device)
193 {
194         const char *str;
195         const GPtrArray *aps;
196         int i;
197
198         g_print ("Mode: %d\n", nm_device_wifi_get_mode (device));
199         g_print ("Bitrate: %d\n", nm_device_wifi_get_bitrate (device));
200
201         str = nm_device_wifi_get_hw_address (device);
202         g_print ("MAC: %s\n", str);
203
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                 g_print ("\n");
209         }
210 }
211
212 static void
213 dump_generic (NMDeviceGeneric *device)
214 {
215         g_print ("HW address: %s\n", nm_device_generic_get_hw_address (device));
216 }
217
218 static void
219 dump_wired (NMDeviceEthernet *device)
220 {
221         const char *str;
222
223         g_print ("Speed: %d\n", nm_device_ethernet_get_speed (device));
224
225         str = nm_device_ethernet_get_hw_address (device);
226         g_print ("MAC: %s\n", str);
227 }
228
229 static void
230 dump_device (NMDevice *device)
231 {
232         const char *str;
233         NMDeviceState state;
234
235         str = nm_device_get_iface (device);
236         g_print ("Interface: %s\n", str);
237
238         str = nm_device_get_udi (device);
239         g_print ("Udi: %s\n", str);
240
241         str = nm_device_get_driver (device);
242         g_print ("Driver: %s\n", str);
243
244         str = nm_device_get_vendor (device);
245         g_print ("Vendor: %s\n", str);
246
247         str = nm_device_get_product (device);
248         g_print ("Product: %s\n", str);
249
250         state = nm_device_get_state (device);
251         g_print ("State: %d\n", state);
252
253         if (state == NM_DEVICE_STATE_ACTIVATED)
254                 dump_ip4_config (nm_device_get_ip4_config (device));
255
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));
262
263         dump_dhcp4_config (nm_device_get_dhcp4_config (device));
264 }
265
266 static gboolean
267 test_devices (NMClient *client)
268 {
269         const GPtrArray *devices;
270         int i;
271
272         devices = nm_client_get_devices (client);
273         g_print ("Got devices:\n");
274         if (!devices) {
275                 g_print ("  NONE\n");
276                 return TRUE;
277         }
278
279         for (i = 0; i < devices->len; i++) {
280                 NMDevice *device = g_ptr_array_index (devices, i);
281                 dump_device (device);
282                 g_print ("\n");
283         }
284
285         return TRUE;
286 }
287
288 static void
289 active_connections_changed (NMClient *client, GParamSpec *pspec, gpointer user_data)
290 {
291         const GPtrArray *connections;
292         int i, j;
293
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;
299
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));
307         }
308 }
309
310 static void
311 show_active_connection_device (gpointer data, gpointer user_data)
312 {
313         NMDevice *device = NM_DEVICE (data);
314
315         g_print ("           %s\n", nm_device_get_udi (device));
316 }
317
318 static void
319 test_get_active_connections (NMClient *client)
320 {
321         const GPtrArray *connections;
322         int i;
323
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;
328
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));
331                 if (devices)
332                         g_ptr_array_foreach ((GPtrArray *) devices, show_active_connection_device, NULL);
333         }
334 }
335
336 static void
337 device_state_changed (NMDevice *device, GParamSpec *pspec, gpointer user_data)
338 {
339         g_print ("Device state changed: %s %d\n",
340                  nm_device_get_iface (device),
341                  nm_device_get_state (device));
342 }
343
344 static void
345 device_added_cb (NMClient *client, NMDevice *device, gpointer user_data)
346 {
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);
351 }
352
353 static void
354 device_removed_cb (NMClient *client, NMDevice *device, gpointer user_data)
355 {
356         g_print ("Device removed\n");
357         dump_device (device);
358 }
359
360 static void
361 manager_running (NMClient *client, GParamSpec *pspec, gpointer user_data)
362 {
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);
369         } else
370                 g_print ("NM disappeared\n");
371 }
372
373 static GMainLoop *loop = NULL;
374
375 static void
376 signal_handler (int signo)
377 {
378         if (signo == SIGINT || signo == SIGTERM) {
379                 g_message ("Caught signal %d, shutting down...", signo);
380                 g_main_loop_quit (loop);
381         }
382 }
383
384 static void
385 setup_signals (void)
386 {
387         struct sigaction action;
388         sigset_t mask;
389
390         sigemptyset (&mask);
391         action.sa_handler = signal_handler;
392         action.sa_mask = mask;
393         action.sa_flags = 0;
394         sigaction (SIGTERM,  &action, NULL);
395         sigaction (SIGINT,  &action, NULL);
396 }
397
398 int
399 main (int argc, char *argv[])
400 {
401         NMClient *client;
402
403         nm_g_type_init ();
404
405         client = nm_client_new ();
406         if (!client) {
407                 exit (1);
408         }
409
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);
415
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);
420
421         loop = g_main_loop_new (NULL, FALSE);
422         setup_signals ();
423         g_main_loop_run (loop);
424
425         g_object_unref (client);
426
427         return 0;
428 }