95d13be05977c952a1a0c82533f252ac10b9d834
[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         notify_notification_close(nn, NULL);
257 }
258
259 static void
260 notify_closed(void)
261 {
262         nn = NULL;
263 }
264
265 static void
266 notify(const char *title, const char *msg, const char *icon)
267 {
268         char **msgs, **m;
269
270         msgs = g_strsplit(msg, "\n", 0);
271         for (m = msgs; *m; m++)
272                 g_message("%s", *m);
273         g_strfreev(msgs);
274         if (nn != NULL)
275                 notify_notification_close(nn, NULL);
276         if (gtk_status_icon_get_visible(status_icon))
277                 nn = notify_notification_new_with_status_icon(title,
278                                                               msg,
279                                                               icon,
280                                                               status_icon);
281         else
282                 nn = notify_notification_new(title, msg, icon, NULL);
283         notify_notification_set_timeout(nn, 5000);
284         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
285         notify_notification_show(nn, NULL);
286 }
287
288 static void
289 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
290 {
291         struct if_msg *ifm, *ifp;
292         gboolean rem;
293         GList *gl;
294         char *msg, *title;
295         const char *act, *net;
296         const char *const *r;
297         in_addr_t ipn;
298
299         ifm = make_if_msg(config);
300         if (ifm == NULL)
301                 return;
302
303         rem = ignore_if_msg(ifm);
304         ifp = NULL;
305         for (gl = interfaces; gl; gl = gl->next) {
306                 ifp = (struct if_msg *)gl->data;
307                 if (g_strcmp0(ifp->name, ifm->name) == 0) {
308                         free_if_msg(ifp);
309                         if (rem)
310                                 interfaces = g_list_delete_link(interfaces, gl);
311                         else
312                                 gl->data = ifm;
313                         break;
314                 }
315         }
316         if (ifp == NULL && !rem)
317                 interfaces = g_list_prepend(interfaces, ifm);
318         interfaces = g_list_sort(interfaces, if_msg_comparer);
319
320         msg = print_if_msg(ifm);
321         act = NULL;
322         title = NULL;
323         for (r = up_reasons; *r; r++) {
324                 if (g_strcmp0(*r, ifm->reason) == 0) {
325                         act = "Connected to ";
326                         break;
327                 }
328         }
329         for (r = down_reasons; *r; r++) {
330                 if (g_strcmp0(*r, ifm->reason) == 0) {
331                         act = "Disconnected from ";
332                         break;
333                 }
334         }
335         if (act && ifm->ip.s_addr) {
336                 ipn = htonl(ifm->ip.s_addr);
337                 if (IN_LINKLOCAL(ipn))
338                         net = "private network";
339                 else if (IN_PRIVATE(ipn))
340                         net = "LAN";
341                 else
342                         net = "internet";
343                 title = g_strconcat(act, net, NULL);
344         }
345
346         update_online(NULL);
347         if (title) {
348                 notify(title, msg, GTK_STOCK_NETWORK);
349                 g_free(title);
350         } else
351                 notify("Interface event", msg, GTK_STOCK_NETWORK);
352         g_free(msg);
353 }
354
355 static void
356 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
357 {
358         struct if_msg *ifm;
359
360         ifm = make_if_msg((GHashTable *)value);
361         if (ignore_if_msg(ifm))
362                 g_free(ifm);
363         else if (ifm)
364                 interfaces = g_list_prepend(interfaces, ifm);
365 }
366
367 static void
368 dhcpcd_get_interfaces()
369 {
370         GHashTable *ifs;
371         GError *error = NULL;
372         GType otype;
373         char *msg;
374
375         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
376         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
377         if (!dbus_g_proxy_call(bus_proxy, "GetInterfaces", &error,
378                                G_TYPE_INVALID,
379                                otype, &ifs, G_TYPE_INVALID))
380                 error_exit("GetInterfaces", error);
381         g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
382         g_hash_table_unref(ifs);
383
384         /* Each interface config only remembers the last order when
385          * that interface was configured, so get the real order now. */
386         g_strfreev(interface_order);
387         interface_order = NULL;
388         if (!dbus_g_proxy_call(bus_proxy, "ListInterfaces", &error,
389                                G_TYPE_INVALID,
390                                G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
391                 error_exit("ListInterfaces", error);
392         interfaces = g_list_sort(interfaces, if_msg_comparer);
393         msg = NULL;
394         update_online(&msg);
395         // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
396         if (msg != NULL) {
397                 notify("Interface status", msg, GTK_STOCK_NETWORK);
398                 g_free(msg);
399         }
400 }
401
402 static void
403 check_status(const char *status)
404 {
405         static char *last = NULL;
406         GList *gl;
407         char *version;
408         const char *msg;
409         gboolean refresh;
410         GError *error = NULL;
411
412         g_message("status changed to %s", status);
413         if (g_strcmp0(status, "down") == 0) {
414                 for (gl = interfaces; gl; gl = gl->next)
415                         free_if_msg((struct if_msg *)gl->data);
416                 g_list_free(interfaces);
417                 interfaces = NULL;
418                 update_online(NULL);
419                 msg = last? "Connection to dhcpcd lost" : "dhcpcd not running";
420                 gtk_status_icon_set_tooltip(status_icon, msg);
421                 notify("No network", msg, GTK_STOCK_NETWORK);
422         }
423
424         refresh = FALSE;
425         if (last == NULL) {
426                 if (g_strcmp0(status, "down") != 0)
427                         refresh = TRUE;
428         } else {
429                 if (g_strcmp0(status, last) == 0)
430                         return;
431                 if (g_strcmp0(last, "down") == 0)
432                         refresh = TRUE;
433                 g_free(last);
434         }
435         last = g_strdup(status);
436
437         if (!refresh)
438                 return;
439         if (!dbus_g_proxy_call(bus_proxy, "GetDhcpcdVersion", &error,
440                                G_TYPE_INVALID,
441                                G_TYPE_STRING, &version, G_TYPE_INVALID))
442                 error_exit("GetDhcpcdVersion", error);
443         g_message("Connected to dhcpcd-%s", version);
444         g_free(version);
445         dhcpcd_get_interfaces();
446 }
447
448 static void
449 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
450 {
451         check_status(status);
452 }
453
454 int
455 main(int argc, char *argv[])
456 {
457         DBusGConnection *bus;
458         GError *error = NULL;
459         char *version = NULL;
460         GType otype;
461         
462 #if defined(HAVE_GNOME) || defined(HAVE_XFCE)
463         if (!g_thread_supported())
464                 g_thread_init(NULL);
465 #endif
466
467         gtk_init(&argc, &argv);
468         g_set_application_name("dhcpcd Monitor");
469         status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
470         gtk_status_icon_set_tooltip(status_icon, "Connecting to dhcpcd ...");
471         gtk_status_icon_set_visible(status_icon, TRUE);
472
473         notify_init(PACKAGE);
474
475         bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
476         if (bus == NULL || error != NULL)
477                 error_exit("could not connect to system bus", error);
478         bus_proxy = dbus_g_proxy_new_for_name(bus,
479                                               DHCPCD_SERVICE,
480                                               DHCPCD_PATH,
481                                               DHCPCD_SERVICE);
482         if (!dbus_g_proxy_call(bus_proxy, "GetVersion", &error,
483                                G_TYPE_INVALID,
484                                G_TYPE_STRING, &version, G_TYPE_INVALID))
485                 error_exit("GetVersion", error);
486         g_message("Connected to dhcpcd-dbus-%s", version);
487         g_free(version);
488
489         gtk_status_icon_set_tooltip(status_icon, "Triggering dhcpcd ...");
490         online = FALSE;
491         menu_init(status_icon);
492
493         if (!dbus_g_proxy_call(bus_proxy, "GetStatus", &error,
494                                G_TYPE_INVALID,
495                                G_TYPE_STRING, &version, G_TYPE_INVALID))
496                 error_exit("GetStatus", error);
497         check_status(version);
498         g_free(version);
499
500         otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
501         dbus_g_proxy_add_signal(bus_proxy, "Event",
502                                 otype, G_TYPE_INVALID);
503         dbus_g_proxy_connect_signal(bus_proxy, "Event",
504                                     G_CALLBACK(dhcpcd_event),
505                                     NULL, NULL);
506         dbus_g_proxy_add_signal(bus_proxy, "StatusChanged",
507                                 G_TYPE_STRING, G_TYPE_INVALID);
508         dbus_g_proxy_connect_signal(bus_proxy, "StatusChanged",
509                                     G_CALLBACK(dhcpcd_status),
510                                     NULL, NULL);
511
512         gtk_main();
513         return 0;
514 }