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