249881f14257f54a7deb09ff0a6cb06375fd7f25
[dhcpcd-ui] / src / dhcpcd-gtk / main.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009-2014 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 <errno.h>
28 #include <locale.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifdef NOTIFY
33 #  include <libnotify/notify.h>
34 #ifndef NOTIFY_CHECK_VERSION
35 #  define NOTIFY_CHECK_VERSION(a,b,c) 0
36 #endif
37 static NotifyNotification *nn;
38 #endif
39
40 #include "config.h"
41 #include "dhcpcd.h"
42 #include "dhcpcd-gtk.h"
43
44 static GtkStatusIcon *status_icon;
45 static guint ani_timer;
46 static int ani_counter;
47 static bool online;
48 static bool carrier;
49
50 struct watch {
51         gpointer ref;
52         int fd;
53         guint eventid;
54         GIOChannel *gio;
55         struct watch *next;
56 };
57 static struct watch *watches;
58
59 WI_SCANS wi_scans;
60
61 static gboolean dhcpcd_try_open(gpointer data);
62 static gboolean dhcpcd_wpa_try_open(gpointer data);
63
64 WI_SCAN *
65 wi_scan_find(DHCPCD_WI_SCAN *scan)
66 {
67         WI_SCAN *w;
68         DHCPCD_WI_SCAN *dw;
69
70         TAILQ_FOREACH(w, &wi_scans, next) {
71                 for (dw = w->scans; dw; dw = dw->next)
72                         if (dw == scan)
73                                 return w;
74         }
75         return NULL;
76 }
77
78 static gboolean
79 animate_carrier(_unused gpointer data)
80 {
81         const char *icon;
82
83         if (ani_timer == 0)
84                 return false;
85
86         switch(ani_counter++) {
87         case 0:
88                 icon = "network-transmit";
89                 break;
90         case 1:
91                 icon = "network-receive";
92                 break;
93         default:
94                 icon = "network-idle";
95                 ani_counter = 0;
96                 break;
97         }
98         gtk_status_icon_set_from_icon_name(status_icon, icon);
99         return true;
100 }
101
102 static gboolean
103 animate_online(_unused gpointer data)
104 {
105         const char *icon;
106
107         if (ani_timer == 0)
108                 return false;
109
110         if (ani_counter++ > 6) {
111                 ani_timer = 0;
112                 ani_counter = 0;
113                 return false;
114         }
115
116         if (ani_counter % 2 == 0)
117                 icon = "network-idle";
118         else
119                 icon = "network-transmit-receive";
120         gtk_status_icon_set_from_icon_name(status_icon, icon);
121         return true;
122 }
123
124 static void
125 update_online(DHCPCD_CONNECTION *con, bool showif)
126 {
127         bool ison, iscarrier;
128         char *msg, *msgs, *tmp;
129         DHCPCD_IF *ifs, *i;
130
131         ison = iscarrier = false;
132         msgs = NULL;
133         ifs = dhcpcd_interfaces(con);
134         for (i = ifs; i; i = i->next) {
135                 if (g_strcmp0(i->type, "link") == 0) {
136                         if (i->up)
137                                 iscarrier = true;
138                 } else {
139                         if (i->up)
140                                 ison = true;
141                 }
142                 msg = dhcpcd_if_message(i, NULL);
143                 if (msg) {
144                         if (showif)
145                                 g_message("%s", msg);
146                         if (msgs) {
147                                 tmp = g_strconcat(msgs, "\n", msg, NULL);
148                                 g_free(msgs);
149                                 g_free(msg);
150                                 msgs = tmp;
151                         } else
152                                 msgs = msg;
153                 } else if (showif)
154                         g_message("%s: %s", i->ifname, i->reason);
155         }
156
157         if (online != ison || carrier != iscarrier) {
158                 online = ison;
159                 carrier = iscarrier;
160                 if (ani_timer != 0) {
161                         g_source_remove(ani_timer);
162                         ani_timer = 0;
163                         ani_counter = 0;
164                 }
165                 if (ison) {
166                         animate_online(NULL);
167                         ani_timer = g_timeout_add(300, animate_online, NULL);
168                 } else if (iscarrier) {
169                         animate_carrier(NULL);
170                         ani_timer = g_timeout_add(500, animate_carrier, NULL);
171                 } else {
172                         gtk_status_icon_set_from_icon_name(status_icon,
173                             "network-offline");
174                 }
175         }
176         gtk_status_icon_set_tooltip_text(status_icon, msgs);
177         g_free(msgs);
178 }
179
180 void
181 notify_close(void)
182 {
183 #ifdef NOTIFY
184         if (nn != NULL)
185                 notify_notification_close(nn, NULL);
186 #endif
187 }
188
189 #ifdef NOTIFY
190 static char *notify_last_msg;
191
192 static void
193 notify_closed(void)
194 {
195         nn = NULL;
196 }
197
198 static void
199 notify(const char *title, const char *msg, const char *icon)
200 {
201
202         if (msg == NULL)
203                 return;
204         /* Don't spam the same message */
205         if (notify_last_msg) {
206                 if (notify_last_msg && strcmp(msg, notify_last_msg) == 0)
207                         return;
208                 g_free(notify_last_msg);
209         }
210         notify_last_msg = g_strdup(msg);
211
212         if (nn != NULL)
213                 notify_notification_close(nn, NULL);
214
215 #if NOTIFY_CHECK_VERSION(0,7,0)
216         nn = notify_notification_new(title, msg, icon);
217         notify_notification_set_hint(nn, "transient",
218             g_variant_new_boolean(TRUE));
219 #else
220         if (gtk_status_icon_get_visible(status_icon))
221                 nn = notify_notification_new_with_status_icon(title,
222                     msg, icon, status_icon);
223         else
224                 nn = notify_notification_new(title, msg, icon, NULL);
225 #endif
226
227         notify_notification_set_timeout(nn, 5000);
228         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
229         notify_notification_show(nn, NULL);
230 }
231 #else
232 #  define notify(a, b, c)
233 #endif
234
235 static struct watch *
236 dhcpcd_findwatch(int fd, gpointer data, struct watch **last)
237 {
238         struct watch *w;
239
240         if (last)
241                 *last = NULL;
242         for (w = watches; w; w = w->next) {
243                 if (w->fd == fd || w->ref == data)
244                         return w;
245                 if (last)
246                         *last = w;
247         }
248         return NULL;
249 }
250
251 static void
252 dhcpcd_unwatch(int fd, gpointer data)
253 {
254         struct watch *w, *l;
255
256         if ((w = dhcpcd_findwatch(fd, data, &l))) {
257                 if (l)
258                         l->next = w->next;
259                 else
260                         watches = w->next;
261                 g_source_remove(w->eventid);
262                 g_io_channel_unref(w->gio);
263                 g_free(w);
264         }
265 }
266
267 static gboolean
268 dhcpcd_watch(int fd,
269     gboolean (*cb)(GIOChannel *, GIOCondition, gpointer),
270     gpointer data)
271 {
272         struct watch *w, *l;
273         GIOChannel *gio;
274         GIOCondition flags;
275         guint eventid;
276
277         /* Sanity */
278         if ((w = dhcpcd_findwatch(fd, data, &l))) {
279                 if (w->fd == fd)
280                         return TRUE;
281                 if (l)
282                         l->next = w->next;
283                 else
284                         watches = w->next;
285                 g_source_remove(w->eventid);
286                 g_io_channel_unref(w->gio);
287                 g_free(w);
288         }
289
290         gio = g_io_channel_unix_new(fd);
291         if (gio == NULL) {
292                 g_warning(_("Error creating new GIO Channel\n"));
293                 return FALSE;
294         }
295         flags = G_IO_IN | G_IO_ERR | G_IO_HUP;
296         if ((eventid = g_io_add_watch(gio, flags, cb, data)) == 0) {
297                 g_warning(_("Error creating watch\n"));
298                 g_io_channel_unref(gio);
299                 return FALSE;
300         }
301
302         w = g_try_malloc(sizeof(*w));
303         if (w == NULL) {
304                 g_warning(_("g_try_malloc\n"));
305                 g_source_remove(eventid);
306                 g_io_channel_unref(gio);
307                 return FALSE;
308         }
309
310         w->ref = data;
311         w->fd = fd;
312         w->eventid = eventid;
313         w->gio = gio;
314         w->next = watches;
315         watches = w;
316
317         return TRUE;
318 }
319
320 static void
321 dhcpcd_status_cb(DHCPCD_CONNECTION *con, const char *status,
322     _unused void *data)
323 {
324         static char *last = NULL;
325         const char *msg;
326         bool refresh;
327         WI_SCAN *w;
328
329         g_message("Status changed to %s", status);
330         if (g_strcmp0(status, "down") == 0) {
331                 msg = N_(last ?
332                     "Connection to dhcpcd lost" : "dhcpcd not running");
333                 if (ani_timer != 0) {
334                         g_source_remove(ani_timer);
335                         ani_timer = 0;
336                         ani_counter = 0;
337                 }
338                 online = carrier = false;
339                 gtk_status_icon_set_from_icon_name(status_icon,
340                     "network-offline");
341                 gtk_status_icon_set_tooltip_text(status_icon, msg);
342                 prefs_abort();
343                 menu_abort();
344                 wpa_abort();
345                 while ((w = TAILQ_FIRST(&wi_scans))) {
346                         TAILQ_REMOVE(&wi_scans, w, next);
347                         dhcpcd_wi_scans_free(w->scans);
348                         g_free(w);
349                 }
350                 dhcpcd_unwatch(-1, con);
351                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
352         } else {
353                 if ((last == NULL || g_strcmp0(last, "down") == 0)) {
354                         g_message(_("Connected to %s-%s"), "dhcpcd",
355                             dhcpcd_version(con));
356                         refresh = true;
357                 } else
358                         refresh = g_strcmp0(last, "opened") ? false : true;
359                 update_online(con, refresh);
360         }
361
362         g_free(last);
363         last = g_strdup(status);
364 }
365
366 static gboolean
367 dhcpcd_cb(_unused GIOChannel *gio, _unused GIOCondition c, gpointer data)
368 {
369         DHCPCD_CONNECTION *con;
370
371         con = (DHCPCD_CONNECTION *)data;
372         if (dhcpcd_get_fd(con) == -1) {
373                 g_warning(_("dhcpcd connection lost"));
374                 dhcpcd_unwatch(-1, con);
375                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
376                 return FALSE;
377         }
378
379         dhcpcd_dispatch(con);
380         return TRUE;
381 }
382
383 static gboolean
384 dhcpcd_try_open(gpointer data)
385 {
386         DHCPCD_CONNECTION *con;
387         int fd;
388         static int last_error;
389
390         con = (DHCPCD_CONNECTION *)data;
391         fd = dhcpcd_open(con, true);
392         if (fd == -1) {
393                 if (errno == EACCES || errno == EPERM) {
394                         if ((fd = dhcpcd_open(con, false)) != -1)
395                                 goto unprived;
396                 }
397                 if (errno != last_error) {
398                         g_critical("dhcpcd_open: %s", strerror(errno));
399                         last_error = errno;
400                 }
401                 return TRUE;
402         }
403
404 unprived:
405         if (!dhcpcd_watch(fd, dhcpcd_cb, con)) {
406                 dhcpcd_close(con);
407                 return TRUE;
408         }
409
410         /* Start listening to WPA events */
411         dhcpcd_wpa_start(con);
412
413         return FALSE;
414 }
415
416 static void
417 dhcpcd_if_cb(DHCPCD_IF *i, _unused void *data)
418 {
419         DHCPCD_CONNECTION *con;
420         char *msg;
421         const char *icon;
422         bool new_msg;
423
424         /* We should ignore renew and stop so we don't annoy the user */
425         if (g_strcmp0(i->reason, "RENEW") &&
426             g_strcmp0(i->reason, "STOP") &&
427             g_strcmp0(i->reason, "STOPPED"))
428         {
429                 msg = dhcpcd_if_message(i, &new_msg);
430                 if (msg) {
431                         g_message("%s", msg);
432                         if (new_msg) {
433                                 if (i->up)
434                                         icon = "network-transmit-receive";
435                                 //else
436                                 //      icon = "network-transmit";
437                                 if (!i->up)
438                                         icon = "network-offline";
439                                 notify(_("Network event"), msg, icon);
440                         }
441                         g_free(msg);
442                 }
443         }
444
445         /* Update the tooltip with connection information */
446         con = dhcpcd_if_connection(i);
447         update_online(con, false);
448 }
449
450 static gboolean
451 dhcpcd_wpa_cb(_unused GIOChannel *gio, _unused GIOCondition c,
452     gpointer data)
453 {
454         DHCPCD_WPA *wpa;
455         DHCPCD_IF *i;
456
457         wpa = (DHCPCD_WPA *)data;
458         if (dhcpcd_wpa_get_fd(wpa) == -1) {
459                 dhcpcd_unwatch(-1, wpa);
460
461                 /* If the interface hasn't left, try re-opening */
462                 i = dhcpcd_wpa_if(wpa);
463                 if (i == NULL ||
464                     g_strcmp0(i->reason, "DEPARTED") == 0 ||
465                     g_strcmp0(i->reason, "STOPPED") == 0)
466                         return TRUE;
467                 g_warning(_("dhcpcd WPA connection lost: %s"), i->ifname);
468                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_wpa_try_open, wpa);
469                 return FALSE;
470         }
471
472         dhcpcd_wpa_dispatch(wpa);
473         return TRUE;
474 }
475
476 static gboolean
477 dhcpcd_wpa_try_open(gpointer data)
478 {
479         DHCPCD_WPA *wpa;
480         int fd;
481         static int last_error;
482
483         wpa = (DHCPCD_WPA *)data;
484         fd = dhcpcd_wpa_open(wpa);
485         if (fd == -1) {
486                 if (errno != last_error)
487                         g_critical("dhcpcd_wpa_open: %s", strerror(errno));
488                 last_error = errno;
489                 return TRUE;
490         }
491
492         if (!dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa)) {
493                 dhcpcd_wpa_close(wpa);
494                 return TRUE;
495         }
496
497         return FALSE;
498 }
499
500 static void
501 dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, _unused void *data)
502 {
503         DHCPCD_IF *i;
504         WI_SCAN *w;
505         DHCPCD_WI_SCAN *scans, *s1, *s2;
506         char *txt, *t;
507         int lerrno, fd;
508         const char *msg;
509
510         /* This could be a new WPA so watch it */
511         fd = dhcpcd_wpa_get_fd(wpa);
512         if (fd == -1) {
513                 g_critical("No fd for WPA %p", wpa);
514                 dhcpcd_unwatch(-1, wpa);
515                 return;
516         }
517         dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa);
518
519         i = dhcpcd_wpa_if(wpa);
520         if (i == NULL) {
521                 g_critical("No interface for WPA %p", wpa);
522                 return;
523         }
524         g_message(_("%s: Received scan results"), i->ifname);
525         lerrno = errno;
526         errno = 0;
527         scans = dhcpcd_wi_scans(i);
528         if (scans == NULL && errno)
529                 g_warning("%s: %s", i->ifname, strerror(errno));
530         errno = lerrno;
531         TAILQ_FOREACH(w, &wi_scans, next) {
532                 if (w->interface == i)
533                         break;
534         }
535         if (w == NULL) {
536                 w = g_malloc(sizeof(*w));
537                 w->interface = i;
538                 w->scans = scans;
539                 w->ifmenu = NULL;
540                 TAILQ_INIT(&w->menus);
541                 TAILQ_INSERT_TAIL(&wi_scans, w, next);
542         } else {
543                 txt = NULL;
544                 msg = N_("New Access Point");
545                 for (s1 = scans; s1; s1 = s1->next) {
546                         for (s2 = w->scans; s2; s2 = s2->next)
547                                 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
548                                         break;
549                         if (s2 == NULL) {
550                                 if (txt == NULL)
551                                         txt = g_strdup(s1->ssid);
552                                 else {
553                                         msg = N_("New Access Points");
554                                         t = g_strconcat(txt, "\n",
555                                             s1->ssid, NULL);
556                                         g_free(txt);
557                                         txt = t;
558                                 }
559                         }
560                 }
561                 if (txt) {
562                         notify(msg, txt, "network-wireless");
563                         g_free(txt);
564                 }
565                 menu_update_scans(w, scans);
566         }
567 }
568
569 static void
570 dhcpcd_wpa_status_cb(DHCPCD_WPA *wpa, const char *status, _unused void *data)
571 {
572         DHCPCD_IF *i;
573
574         i = dhcpcd_wpa_if(wpa);
575         g_message("%s: WPA status %s", i->ifname, status);
576         if (g_strcmp0(status, "down") == 0)
577                 dhcpcd_unwatch(-1, wpa);
578 }
579
580 int
581 main(int argc, char *argv[])
582 {
583         DHCPCD_CONNECTION *con;
584
585         setlocale(LC_ALL, "");
586         bindtextdomain(PACKAGE, NULL);
587         bind_textdomain_codeset(PACKAGE, "UTF-8");
588         textdomain(PACKAGE);
589
590         gtk_init(&argc, &argv);
591         g_set_application_name("Network Configurator");
592         gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
593             ICONDIR);
594         status_icon = gtk_status_icon_new_from_icon_name("network-offline");
595
596         gtk_status_icon_set_tooltip_text(status_icon,
597             _("Connecting to dhcpcd ..."));
598         gtk_status_icon_set_visible(status_icon, true);
599         online = false;
600 #ifdef NOTIFY
601         notify_init(PACKAGE);
602 #endif
603
604         TAILQ_INIT(&wi_scans);
605         g_message(_("Connecting ..."));
606         con = dhcpcd_new();
607         if (con ==  NULL) {
608                 g_critical("libdhcpcd: %s", strerror(errno));
609                 exit(EXIT_FAILURE);
610         }
611         dhcpcd_set_progname(con, "dhcpcd-gtk");
612         dhcpcd_set_status_callback(con, dhcpcd_status_cb, NULL);
613         dhcpcd_set_if_callback(con, dhcpcd_if_cb, NULL);
614         dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, NULL);
615         dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, NULL);
616         if (dhcpcd_try_open(con))
617                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
618
619         menu_init(status_icon, con);
620
621         gtk_main();
622         dhcpcd_close(con);
623         dhcpcd_free(con);
624         return 0;
625 }