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