0918a72e4439deee87a6c66447198e87b705a0eb
[dhcpcd-ui] / main.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /* TODO: Animate the icon from carrier -> address
28  * maybe use network-idle -> network-transmit ->
29  * network-receive -> network-transmit-receive */
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <libnotify/notify.h>
35
36 #include "dhcpcd-gtk.h"
37 #include "menu.h"
38
39 DBusGProxy *dbus = NULL;
40 GList *interfaces = NULL;
41
42 static GtkStatusIcon *status_icon = NULL;
43 static NotifyNotification *nn;
44 static gboolean online;
45 static gboolean carrier;
46 static char **interface_order;
47
48 const char *const up_reasons[] = {
49         "BOUND",
50         "RENEW",
51         "REBIND",
52         "REBOOT",
53         "IPV4LL",
54         "INFORM",
55         "TIMEOUT",
56         NULL
57 };
58
59 const char *const down_reasons[] = {
60         "EXPIRE",
61         "FAIL",
62         "NAK",
63         "NOCARRIER",
64         "STOP",
65         NULL
66 };
67
68 static gboolean
69 ignore_if_msg(const struct if_msg *ifm)
70 {
71         if (g_strcmp0(ifm->reason, "STOP") == 0 ||
72             g_strcmp0(ifm->reason, "RELEASE") == 0)
73                 return TRUE;
74         return FALSE;
75 }
76
77 static void
78 free_if_msg(struct if_msg *ifm)
79 {
80         g_free(ifm->name);
81         g_free(ifm->reason);
82         g_free(ifm->ssid);
83         g_free(ifm);
84 }
85
86 static void
87 error_exit(const char *msg, GError *error)
88 {
89         GtkWidget *dialog;
90
91         if (error) {
92                 g_critical("%s: %s", msg, error->message);
93                 dialog = gtk_message_dialog_new(NULL,
94                                                 0,
95                                                 GTK_MESSAGE_ERROR,
96                                                 GTK_BUTTONS_CLOSE,
97                                                 "%s: %s",
98                                                 msg,
99                                                 error->message);
100         } else {
101                 g_critical("%s", msg);
102                 dialog = gtk_message_dialog_new(NULL,
103                                                 0,
104                                                 GTK_MESSAGE_ERROR,
105                                                 GTK_BUTTONS_CLOSE,
106                                                 "%s",
107                                                 msg);
108         }
109         gtk_dialog_run(GTK_DIALOG(dialog));
110         gtk_widget_destroy(dialog);
111         if (gtk_main_level())
112                 gtk_main_quit();
113         else
114                 exit(EXIT_FAILURE);
115 }
116
117 static struct if_msg *
118 make_if_msg(GHashTable *config)
119 {
120         GValue *val;
121         struct if_msg *ifm;
122
123         val = g_hash_table_lookup(config, "Interface");
124         if (val == NULL)
125                 return NULL;
126         ifm = g_malloc0(sizeof(*ifm));
127         ifm->name = g_strdup(g_value_get_string(val));
128         val = g_hash_table_lookup(config, "Reason");
129         if (val)
130                 ifm->reason = g_strdup(g_value_get_string(val));
131         val = g_hash_table_lookup(config, "Wireless");
132         if (val)
133                 ifm->wireless = g_value_get_boolean(val);
134         if (ifm->wireless) {
135                 val = g_hash_table_lookup(config, "SSID");
136                 if (val)
137                         ifm->ssid = g_strdup(g_value_get_string(val));
138         }
139         val = g_hash_table_lookup(config, "IPAddress");
140         if (val)
141                 ifm->ip.s_addr = g_value_get_uint(val);
142         val = g_hash_table_lookup(config, "SubnetCIDR");
143         if (val)
144                 ifm->cidr = g_value_get_uchar(val);
145         val = g_hash_table_lookup(config, "InterfaceOrder");
146         if (val) {
147                 g_strfreev(interface_order);
148                 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
149         }
150         return ifm;
151 }
152
153 static gboolean
154 if_up(const struct if_msg *ifm)
155 {
156         const char *const *r;
157
158         for (r = up_reasons; *r; r++)
159                 if (g_strcmp0(*r, ifm->reason) == 0)
160                         return TRUE;
161         return FALSE;
162 }
163
164 static char *
165 print_if_msg(const struct if_msg *ifm)
166 {
167         char *msg, *p;
168         const char *reason = NULL;
169         size_t len;
170         gboolean showip, showssid;
171     
172         showip = TRUE;
173         showssid = FALSE;
174         if (if_up(ifm))
175                 reason = "Acquired address";
176         else {
177                 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
178                         reason = "Failed to renew";
179                 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
180                         if (ifm->wireless) {
181                                 reason = "Asssociated with";
182                                 if (ifm->ssid != NULL)
183                                         showssid = TRUE;
184                         } else
185                                 reason = "Cable plugged in";
186                         showip = FALSE;
187                 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
188                         if (ifm->wireless) {
189                                 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
190                                         reason = "Lost association with";
191                                         showssid = TRUE;
192                                 } else
193                                     reason = "Not associated";
194                         } else
195                                 reason = "Cable unplugged";
196                         showip = FALSE;
197                 }
198         }
199         if (reason == NULL)
200                 reason = ifm->reason;
201         
202         len = strlen(ifm->name) + 3;
203         len += strlen(reason) + 1;
204         if (ifm->ip.s_addr != 0) {
205                 len += 16; /* 000. * 4 */
206                 if (ifm->cidr != 0)
207                         len += 3; /* /32 */
208         }
209         if (showssid)
210                 len += strlen(ifm->ssid) + 1;
211         msg = p = g_malloc(len);
212         p += g_snprintf(msg, len, "%s: %s", ifm->name, reason);
213         if (showssid)
214                 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
215         if (ifm->ip.s_addr != 0 && showip) {
216                 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
217                 if (ifm->cidr != 0)
218                         g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
219         }
220         return msg;
221 }
222
223 static gint
224 if_msg_comparer(gconstpointer a, gconstpointer b)
225 {
226         const struct if_msg *ifa, *ifb;
227         const char *const *order;
228
229         ifa = (const struct if_msg *)a;
230         ifb = (const struct if_msg *)b;
231         for (order = (const char *const *)interface_order; *order; order++) {
232                 if (g_strcmp0(*order, ifa->name) == 0)
233                         return -1;
234                 if (g_strcmp0(*order, ifb->name) == 0)
235                         return 1;
236         }
237         return 0;
238 }
239
240 static void
241 update_online(char **buffer)
242 {
243         gboolean ison, iscarrier;
244         char *msg, *msgs, *tmp;
245         const char *icon;
246         const GList *gl;
247         const struct if_msg *ifm;
248
249         ison = iscarrier = FALSE;
250         msgs = NULL;
251         for (gl = interfaces; gl; gl = gl->next) {
252                 ifm = (const struct if_msg *)gl->data;
253                 if (if_up(ifm))
254                         ison = iscarrier = TRUE;
255                 if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
256                         iscarrier = TRUE;
257                 msg = print_if_msg(ifm);
258                 if (msgs) {
259                         tmp = g_strconcat(msgs, "\n", msg, NULL);
260                         g_free(msgs);
261                         g_free(msg);
262                         msgs = tmp;
263                 } else
264                         msgs = msg;
265         }
266
267         if (online != ison || carrier != iscarrier) {
268                 online = ison;
269                 if (ison)
270                         icon = "network-transmit-receive";
271                 else if (iscarrier)
272                         icon = "network-transmit";
273                 else
274                         icon = "network-offline";
275                 gtk_status_icon_set_from_icon_name(status_icon, icon);
276         }
277         gtk_status_icon_set_tooltip(status_icon, msgs);
278         if (buffer)
279                 *buffer = msgs;
280         else
281                 g_free(msgs);
282 }
283
284 void
285 notify_close(void)
286 {
287         if (nn != NULL)
288                 notify_notification_close(nn, NULL);
289 }
290
291 static void
292 notify_closed(void)
293 {
294         nn = NULL;
295 }
296
297 static void
298 notify(const char *title, const char *msg, const char *icon)
299 {
300         char **msgs, **m;
301
302         msgs = g_strsplit(msg, "\n", 0);
303         for (m = msgs; *m; m++)
304                 g_message("%s", *m);
305         g_strfreev(msgs);
306         if (nn != NULL)
307                 notify_notification_close(nn, NULL);
308         if (gtk_status_icon_get_visible(status_icon))
309                 nn = notify_notification_new_with_status_icon(title,
310                                                               msg,
311                                                               icon,
312                                                               status_icon);
313         else
314                 nn = notify_notification_new(title, msg, icon, NULL);
315         notify_notification_set_timeout(nn, 5000);
316         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
317         notify_notification_show(nn, NULL);
318 }
319
320 static void
321 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
322 {
323         struct if_msg *ifm, *ifp;
324         gboolean rem;
325         GList *gl;
326         char *msg, *title;
327         const char *act, *net;
328         const char *const *r;
329         in_addr_t ipn;
330
331         ifm = make_if_msg(config);
332         if (ifm == NULL)
333                 return;
334
335         rem = ignore_if_msg(ifm);
336         ifp = NULL;
337         for (gl = interfaces; gl; gl = gl->next) {
338                 ifp = (struct if_msg *)gl->data;
339                 if (g_strcmp0(ifp->name, ifm->name) == 0) {
340                         free_if_msg(ifp);
341                         if (rem)
342                                 interfaces = g_list_delete_link(interfaces, gl);
343                         else
344                                 gl->data = ifm;
345                         break;
346                 }
347         }
348         if (ifp == NULL && !rem)
349                 interfaces = g_list_prepend(interfaces, ifm);
350         interfaces = g_list_sort(interfaces, if_msg_comparer);
351         update_online(NULL);
352
353         /* We should ignore renew and stop so we don't annoy the user */
354         if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
355             g_strcmp0(ifm->reason, "STOP") == 0)
356                 return;
357
358         msg = print_if_msg(ifm);
359         title = NULL;
360         if (if_up(ifm))
361                 act = "Connected to ";
362         else
363                 act = NULL;
364         for (r = down_reasons; *r; r++) {
365                 if (g_strcmp0(*r, ifm->reason) == 0) {
366                         act = "Disconnected from ";
367                         break;
368                 }
369         }
370         if (act && ifm->ip.s_addr) {
371                 ipn = htonl(ifm->ip.s_addr);
372                 if (IN_LINKLOCAL(ipn))
373                         net = "private network";
374                 else if (IN_PRIVATE(ipn))
375                         net = "LAN";
376                 else
377                         net = "internet";
378                 title = g_strconcat(act, net, NULL);
379         }
380
381         if (title) {
382                 notify(title, msg, GTK_STOCK_NETWORK);
383                 g_free(title);
384         } else
385                 notify("Interface event", msg, GTK_STOCK_NETWORK);
386         g_free(msg);
387 }
388
389 static void
390 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
391 {
392         struct if_msg *ifm;
393
394         ifm = make_if_msg((GHashTable *)value);
395         if (ignore_if_msg(ifm))
396                 g_free(ifm);
397         else if (ifm)
398                 interfaces = g_list_prepend(interfaces, ifm);
399 }
400
401 static void
402 dhcpcd_get_interfaces()
403 {
404         GHashTable *ifs;
405         GError *error = NULL;
406         GType otype;
407         char *msg;
408
409         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
410         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
411         if (!dbus_g_proxy_call(dbus, "GetInterfaces", &error,
412                                G_TYPE_INVALID,
413                                otype, &ifs, G_TYPE_INVALID))
414                 error_exit("GetInterfaces", error);
415         g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
416         g_hash_table_unref(ifs);
417
418         /* Each interface config only remembers the last order when
419          * that interface was configured, so get the real order now. */
420         g_strfreev(interface_order);
421         interface_order = NULL;
422         if (!dbus_g_proxy_call(dbus, "ListInterfaces", &error,
423                                G_TYPE_INVALID,
424                                G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
425                 error_exit("ListInterfaces", error);
426         interfaces = g_list_sort(interfaces, if_msg_comparer);
427         msg = NULL;
428         update_online(&msg);
429         // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
430         if (msg != NULL) {
431                 notify("Interface status", msg, GTK_STOCK_NETWORK);
432                 g_free(msg);
433         }
434 }
435
436 static void
437 check_status(const char *status)
438 {
439         static char *last = NULL;
440         GList *gl;
441         char *version;
442         const char *msg;
443         gboolean refresh;
444         GError *error = NULL;
445
446         g_message("Status changed to %s", status);
447         if (g_strcmp0(status, "down") == 0) {
448                 for (gl = interfaces; gl; gl = gl->next)
449                         free_if_msg((struct if_msg *)gl->data);
450                 g_list_free(interfaces);
451                 interfaces = NULL;
452                 update_online(NULL);
453                 msg = last? "Connection to dhcpcd lost" : "dhcpcd not running";
454                 gtk_status_icon_set_tooltip(status_icon, msg);
455                 notify("No network", msg, GTK_STOCK_NETWORK);
456         }
457
458         refresh = FALSE;
459         if (last == NULL) {
460                 if (g_strcmp0(status, "down") != 0)
461                         refresh = TRUE;
462         } else {
463                 if (g_strcmp0(status, last) == 0)
464                         return;
465                 if (g_strcmp0(last, "down") == 0)
466                         refresh = TRUE;
467                 g_free(last);
468         }
469         last = g_strdup(status);
470
471         if (!refresh)
472                 return;
473         if (!dbus_g_proxy_call(dbus, "GetDhcpcdVersion", &error,
474                                G_TYPE_INVALID,
475                                G_TYPE_STRING, &version, G_TYPE_INVALID))
476                 error_exit("GetDhcpcdVersion", error);
477         g_message("Connected to dhcpcd-%s", version);
478         g_free(version);
479         dhcpcd_get_interfaces();
480 }
481
482 static void
483 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
484 {
485         check_status(status);
486 }
487
488 int
489 main(int argc, char *argv[])
490 {
491         DBusGConnection *bus;
492         GError *error = NULL;
493         char *version = NULL;
494         GType otype;
495         int tries = 5;
496         
497         gtk_init(&argc, &argv);
498         g_set_application_name("dhcpcd Monitor");
499         status_icon = gtk_status_icon_new_from_icon_name("network-offline");
500         if (status_icon == NULL)
501                 status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
502         
503         gtk_status_icon_set_tooltip(status_icon, "Connecting to dhcpcd ...");
504         gtk_status_icon_set_visible(status_icon, TRUE);
505
506         notify_init(PACKAGE);
507
508         g_message("Connecting to dbus ...");
509         bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
510         if (bus == NULL || error != NULL)
511                 error_exit("Could not connect to system bus", error);
512         dbus = dbus_g_proxy_new_for_name(bus,
513                                               DHCPCD_SERVICE,
514                                               DHCPCD_PATH,
515                                               DHCPCD_SERVICE);
516
517         g_message("Connecting to dhcpcd-dbus ...");
518         while (--tries > 0) {
519                 g_clear_error(&error);
520                 if (dbus_g_proxy_call_with_timeout(dbus,
521                                                    "GetVersion",
522                                                    500,
523                                                    &error,
524                                                    G_TYPE_INVALID,
525                                                    G_TYPE_STRING,
526                                                    &version,
527                                                    G_TYPE_INVALID))
528                         break;
529         }
530         if (tries == 0)
531                 error_exit("GetVersion", error);
532         g_message("Connected to dhcpcd-dbus-%s", version);
533         g_free(version);
534
535         gtk_status_icon_set_tooltip(status_icon, "Triggering dhcpcd ...");
536         online = FALSE;
537         menu_init(status_icon);
538
539         if (!dbus_g_proxy_call(dbus, "GetStatus", &error,
540                                G_TYPE_INVALID,
541                                G_TYPE_STRING, &version, G_TYPE_INVALID))
542                 error_exit("GetStatus", error);
543         check_status(version);
544         g_free(version);
545
546         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
547         dbus_g_proxy_add_signal(dbus, "Event",
548                                 otype, G_TYPE_INVALID);
549         dbus_g_proxy_connect_signal(dbus, "Event",
550                                     G_CALLBACK(dhcpcd_event),
551                                     NULL, NULL);
552         dbus_g_proxy_add_signal(dbus, "StatusChanged",
553                                 G_TYPE_STRING, G_TYPE_INVALID);
554         dbus_g_proxy_connect_signal(dbus, "StatusChanged",
555                                     G_CALLBACK(dhcpcd_status),
556                                     NULL, NULL);
557
558         gtk_main();
559         return 0;
560 }