Ensure that the notify pointer is valid.
[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 #include <arpa/inet.h>
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <dbus/dbus-glib.h>
33 #include <gtk/gtk.h>
34 #include <libnotify/notify.h>
35
36 #include "config.h"
37 #include "menu.h"
38
39 /* Work out if we have a private address or not
40  * 10/8
41  * 172.16/12
42  * 192.168/16
43  */
44 #ifndef IN_PRIVATE
45 # define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) || \
46                            ((addr & 0xfff00000)    == 0xac100000) || \
47                            ((addr & IN_CLASSB_NET) == 0xc0a80000))
48 #endif
49 #ifndef IN_LINKLOCAL
50 # define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == 0xa9fe0000)
51 #endif
52
53 struct if_msg {
54         char *name;
55         char *reason;
56         struct in_addr ip;
57         unsigned char cidr;
58 };
59
60 static DBusGProxy *bus_proxy;
61 static GtkStatusIcon *status_icon;
62 static GList *interfaces;
63 static gboolean online;
64 static NotifyNotification *nn;
65
66 static char **interface_order;
67
68 const char *const up_reasons[] = {
69         "BOUND",
70         "RENEW",
71         "REBIND",
72         "REBOOT",
73         "IPV4LL",
74         "INFORM",
75         "TIMEOUT",
76         NULL
77 };
78
79 const char *const down_reasons[] = {
80         "EXPIRE",
81         "FAIL",
82         "NAK",
83         "NOCARRIER",
84         "STOP",
85         NULL
86 };
87
88 /* Should be in a header */
89 void notify_close(void);
90
91 static gboolean
92 ignore_if_msg(const struct if_msg *ifm)
93 {
94         if (g_strcmp0(ifm->reason, "STOP") == 0 ||
95             g_strcmp0(ifm->reason, "RELEASE") == 0)
96                 return TRUE;
97         return FALSE;
98 }
99
100 static void
101 free_if_msg(struct if_msg *ifm)
102 {
103         g_free(ifm->name);
104         g_free(ifm->reason);
105         g_free(ifm);
106 }
107
108 static void
109 error_exit(const char *msg, GError *error)
110 {
111         GtkWidget *dialog;
112
113         if (error) {
114                 g_critical("%s: %s", msg, error->message);
115                 dialog = gtk_message_dialog_new(NULL,
116                                                 0,
117                                                 GTK_MESSAGE_ERROR,
118                                                 GTK_BUTTONS_CLOSE,
119                                                 "%s: %s",
120                                                 msg,
121                                                 error->message);
122         } else {
123                 g_critical("%s", msg);
124                 dialog = gtk_message_dialog_new(NULL,
125                                                 0,
126                                                 GTK_MESSAGE_ERROR,
127                                                 GTK_BUTTONS_CLOSE,
128                                                 "%s",
129                                                 msg);
130         }
131         gtk_dialog_run(GTK_DIALOG(dialog));
132         gtk_widget_destroy(dialog);
133         if (gtk_main_level())
134                 gtk_main_quit();
135         else
136                 exit(EXIT_FAILURE);
137 }
138
139 static struct if_msg *
140 make_if_msg(GHashTable *config)
141 {
142         GValue *val;
143         struct if_msg *ifm;
144
145         val = g_hash_table_lookup(config, "Interface");
146         if (val == NULL)
147                 return NULL;
148         ifm = g_malloc0(sizeof(*ifm));
149         ifm->name = g_strdup(g_value_get_string(val));
150         val = g_hash_table_lookup(config, "Reason");
151         if (val)
152                 ifm->reason = g_strdup(g_value_get_string(val));
153         val = g_hash_table_lookup(config, "IPAddress");
154         if (val)
155                 ifm->ip.s_addr = g_value_get_uint(val);
156         val = g_hash_table_lookup(config, "SubnetCIDR");
157         if (val)
158                 ifm->cidr = g_value_get_uchar(val);
159         val = g_hash_table_lookup(config, "InterfaceOrder");
160         if (val) {
161                 g_strfreev(interface_order);
162                 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
163         }
164         return ifm;
165 }
166
167 static char *
168 print_if_msg(const struct if_msg *ifm)
169 {
170         char *msg, *p;
171         size_t len;
172         gboolean showip;
173
174         len = strlen(ifm->name) + 3;
175         len += strlen(ifm->reason) + 1;
176         if (ifm->ip.s_addr != 0) {
177                 len += 16; /* 000. * 4 */
178                 if (ifm->cidr != 0)
179                         len += 3; /* /32 */
180         }
181         msg = p = g_malloc(len);
182         p += g_snprintf(msg, len, "%s: %s", ifm->name, ifm->reason);
183         showip = TRUE;
184         if (g_strcmp0(ifm->reason, "NOCARRIER") == 0)
185                 showip = FALSE;
186         if (ifm->ip.s_addr != 0 && showip) {
187                 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
188                 if (ifm->cidr != 0)
189                         g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
190         }
191         return msg;
192 }
193
194 static gint
195 if_msg_comparer(gconstpointer a, gconstpointer b)
196 {
197         const struct if_msg *ifa, *ifb;
198         const char *const *order;
199
200         ifa = (const struct if_msg *)a;
201         ifb = (const struct if_msg *)b;
202         for (order = (const char *const *)interface_order; *order; order++) {
203                 if (g_strcmp0(*order, ifa->name) == 0)
204                         return -1;
205                 if (g_strcmp0(*order, ifb->name) == 0)
206                         return 1;
207         }
208         return 0;
209 }
210
211 static void
212 update_online(char **buffer)
213 {
214         gboolean ison;
215         char *msg, *msgs, *tmp;
216         const char *icon;
217         const char *const *r;
218         const GList *gl;
219         const struct if_msg *ifm;
220
221         ison = FALSE;
222         msgs = NULL;
223         for (gl = interfaces; gl; gl = gl->next) {
224                 ifm = (const struct if_msg *)gl->data;          
225                 for (r = up_reasons; *r; r++) {
226                         if (g_strcmp0(*r, ifm->reason) == 0) {
227                                 ison = TRUE;
228                                 break;
229                         }
230                 }
231                 msg = print_if_msg(ifm);
232                 if (msgs) {
233                         tmp = g_strconcat(msgs, "\n", msg, NULL);
234                         g_free(msgs);
235                         g_free(msg);
236                         msgs = tmp;
237                 } else
238                         msgs = msg;
239         }
240
241         if (online != ison) {
242                 online = ison;
243                 icon = online ? GTK_STOCK_CONNECT : GTK_STOCK_DISCONNECT;
244                 gtk_status_icon_set_from_stock(status_icon, icon);
245         }
246         gtk_status_icon_set_tooltip(status_icon, msgs);
247         if (buffer)
248                 *buffer = msgs;
249         else
250                 g_free(msgs);
251 }
252
253 void
254 notify_close(void)
255 {
256         if (nn != NULL)
257                 notify_notification_close(nn, NULL);
258 }
259
260 static void
261 notify_closed(void)
262 {
263         nn = NULL;
264 }
265
266 static void
267 notify(const char *title, const char *msg, const char *icon)
268 {
269         char **msgs, **m;
270
271         msgs = g_strsplit(msg, "\n", 0);
272         for (m = msgs; *m; m++)
273                 g_message("%s", *m);
274         g_strfreev(msgs);
275         if (nn != NULL)
276                 notify_notification_close(nn, NULL);
277         if (gtk_status_icon_get_visible(status_icon))
278                 nn = notify_notification_new_with_status_icon(title,
279                                                               msg,
280                                                               icon,
281                                                               status_icon);
282         else
283                 nn = notify_notification_new(title, msg, icon, NULL);
284         notify_notification_set_timeout(nn, 5000);
285         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
286         notify_notification_show(nn, NULL);
287 }
288
289 static void
290 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
291 {
292         struct if_msg *ifm, *ifp;
293         gboolean rem;
294         GList *gl;
295         char *msg, *title;
296         const char *act, *net;
297         const char *const *r;
298         in_addr_t ipn;
299
300         ifm = make_if_msg(config);
301         if (ifm == NULL)
302                 return;
303
304         rem = ignore_if_msg(ifm);
305         ifp = NULL;
306         for (gl = interfaces; gl; gl = gl->next) {
307                 ifp = (struct if_msg *)gl->data;
308                 if (g_strcmp0(ifp->name, ifm->name) == 0) {
309                         free_if_msg(ifp);
310                         if (rem)
311                                 interfaces = g_list_delete_link(interfaces, gl);
312                         else
313                                 gl->data = ifm;
314                         break;
315                 }
316         }
317         if (ifp == NULL && !rem)
318                 interfaces = g_list_prepend(interfaces, ifm);
319         interfaces = g_list_sort(interfaces, if_msg_comparer);
320
321         msg = print_if_msg(ifm);
322         act = NULL;
323         title = NULL;
324         for (r = up_reasons; *r; r++) {
325                 if (g_strcmp0(*r, ifm->reason) == 0) {
326                         act = "Connected to ";
327                         break;
328                 }
329         }
330         for (r = down_reasons; *r; r++) {
331                 if (g_strcmp0(*r, ifm->reason) == 0) {
332                         act = "Disconnected from ";
333                         break;
334                 }
335         }
336         if (act && ifm->ip.s_addr) {
337                 ipn = htonl(ifm->ip.s_addr);
338                 if (IN_LINKLOCAL(ipn))
339                         net = "private network";
340                 else if (IN_PRIVATE(ipn))
341                         net = "LAN";
342                 else
343                         net = "internet";
344                 title = g_strconcat(act, net, NULL);
345         }
346
347         update_online(NULL);
348         if (title) {
349                 notify(title, msg, GTK_STOCK_NETWORK);
350                 g_free(title);
351         } else
352                 notify("Interface event", msg, GTK_STOCK_NETWORK);
353         g_free(msg);
354 }
355
356 static void
357 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
358 {
359         struct if_msg *ifm;
360
361         ifm = make_if_msg((GHashTable *)value);
362         if (ignore_if_msg(ifm))
363                 g_free(ifm);
364         else if (ifm)
365                 interfaces = g_list_prepend(interfaces, ifm);
366 }
367
368 static void
369 dhcpcd_get_interfaces()
370 {
371         GHashTable *ifs;
372         GError *error = NULL;
373         GType otype;
374         char *msg;
375
376         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
377         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
378         if (!dbus_g_proxy_call(bus_proxy, "GetInterfaces", &error,
379                                G_TYPE_INVALID,
380                                otype, &ifs, G_TYPE_INVALID))
381                 error_exit("GetInterfaces", error);
382         g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
383         g_hash_table_unref(ifs);
384
385         /* Each interface config only remembers the last order when
386          * that interface was configured, so get the real order now. */
387         g_strfreev(interface_order);
388         interface_order = NULL;
389         if (!dbus_g_proxy_call(bus_proxy, "ListInterfaces", &error,
390                                G_TYPE_INVALID,
391                                G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
392                 error_exit("ListInterfaces", error);
393         interfaces = g_list_sort(interfaces, if_msg_comparer);
394         msg = NULL;
395         update_online(&msg);
396         // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
397         if (msg != NULL) {
398                 notify("Interface status", msg, GTK_STOCK_NETWORK);
399                 g_free(msg);
400         }
401 }
402
403 static void
404 check_status(const char *status)
405 {
406         static char *last = NULL;
407         GList *gl;
408         char *version;
409         const char *msg;
410         gboolean refresh;
411         GError *error = NULL;
412
413         g_message("status changed to %s", status);
414         if (g_strcmp0(status, "down") == 0) {
415                 for (gl = interfaces; gl; gl = gl->next)
416                         free_if_msg((struct if_msg *)gl->data);
417                 g_list_free(interfaces);
418                 interfaces = NULL;
419                 update_online(NULL);
420                 msg = last? "Connection to dhcpcd lost" : "dhcpcd not running";
421                 gtk_status_icon_set_tooltip(status_icon, msg);
422                 notify("No network", msg, GTK_STOCK_NETWORK);
423         }
424
425         refresh = FALSE;
426         if (last == NULL) {
427                 if (g_strcmp0(status, "down") != 0)
428                         refresh = TRUE;
429         } else {
430                 if (g_strcmp0(status, last) == 0)
431                         return;
432                 if (g_strcmp0(last, "down") == 0)
433                         refresh = TRUE;
434                 g_free(last);
435         }
436         last = g_strdup(status);
437
438         if (!refresh)
439                 return;
440         if (!dbus_g_proxy_call(bus_proxy, "GetDhcpcdVersion", &error,
441                                G_TYPE_INVALID,
442                                G_TYPE_STRING, &version, G_TYPE_INVALID))
443                 error_exit("GetDhcpcdVersion", error);
444         g_message("Connected to dhcpcd-%s", version);
445         g_free(version);
446         dhcpcd_get_interfaces();
447 }
448
449 static void
450 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
451 {
452         check_status(status);
453 }
454
455 int
456 main(int argc, char *argv[])
457 {
458         DBusGConnection *bus;
459         GError *error = NULL;
460         char *version = NULL;
461         GType otype;
462         
463         gtk_init(&argc, &argv);
464         g_set_application_name("dhcpcd Monitor");
465         status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
466         gtk_status_icon_set_tooltip(status_icon, "Connecting to dhcpcd ...");
467         gtk_status_icon_set_visible(status_icon, TRUE);
468
469         notify_init(PACKAGE);
470
471         bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
472         if (bus == NULL || error != NULL)
473                 error_exit("could not connect to system bus", error);
474         bus_proxy = dbus_g_proxy_new_for_name(bus,
475                                               DHCPCD_SERVICE,
476                                               DHCPCD_PATH,
477                                               DHCPCD_SERVICE);
478         if (!dbus_g_proxy_call(bus_proxy, "GetVersion", &error,
479                                G_TYPE_INVALID,
480                                G_TYPE_STRING, &version, G_TYPE_INVALID))
481                 error_exit("GetVersion", error);
482         g_message("Connected to dhcpcd-dbus-%s", version);
483         g_free(version);
484
485         gtk_status_icon_set_tooltip(status_icon, "Triggering dhcpcd ...");
486         online = FALSE;
487         menu_init(status_icon);
488
489         if (!dbus_g_proxy_call(bus_proxy, "GetStatus", &error,
490                                G_TYPE_INVALID,
491                                G_TYPE_STRING, &version, G_TYPE_INVALID))
492                 error_exit("GetStatus", error);
493         check_status(version);
494         g_free(version);
495
496         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
497         dbus_g_proxy_add_signal(bus_proxy, "Event",
498                                 otype, G_TYPE_INVALID);
499         dbus_g_proxy_connect_signal(bus_proxy, "Event",
500                                     G_CALLBACK(dhcpcd_event),
501                                     NULL, NULL);
502         dbus_g_proxy_add_signal(bus_proxy, "StatusChanged",
503                                 G_TYPE_STRING, G_TYPE_INVALID);
504         dbus_g_proxy_connect_signal(bus_proxy, "StatusChanged",
505                                     G_CALLBACK(dhcpcd_status),
506                                     NULL, NULL);
507
508         gtk_main();
509         return 0;
510 }