3 * Copyright 2009 Roy Marples <roy@marples.name>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 /* TODO: Animate the icon from carrier -> address
28 * maybe use network-idle -> network-transmit ->
29 * network-receive -> network-transmit-receive */
31 #include <arpa/inet.h>
36 #include <dbus/dbus-glib.h>
38 #include <libnotify/notify.h>
43 /* Work out if we have a private address or not
49 # define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) || \
50 ((addr & 0xfff00000) == 0xac100000) || \
51 ((addr & IN_CLASSB_NET) == 0xc0a80000))
54 # define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == 0xa9fe0000)
66 static DBusGProxy *bus_proxy;
67 static GtkStatusIcon *status_icon;
68 static GList *interfaces;
69 static gboolean online;
70 static NotifyNotification *nn;
72 static char **interface_order;
74 const char *const up_reasons[] = {
85 const char *const down_reasons[] = {
94 /* Should be in a header */
95 void notify_close(void);
98 ignore_if_msg(const struct if_msg *ifm)
100 if (g_strcmp0(ifm->reason, "STOP") == 0 ||
101 g_strcmp0(ifm->reason, "RELEASE") == 0)
107 free_if_msg(struct if_msg *ifm)
116 error_exit(const char *msg, GError *error)
121 g_critical("%s: %s", msg, error->message);
122 dialog = gtk_message_dialog_new(NULL,
130 g_critical("%s", msg);
131 dialog = gtk_message_dialog_new(NULL,
138 gtk_dialog_run(GTK_DIALOG(dialog));
139 gtk_widget_destroy(dialog);
140 if (gtk_main_level())
146 static struct if_msg *
147 make_if_msg(GHashTable *config)
152 val = g_hash_table_lookup(config, "Interface");
155 ifm = g_malloc0(sizeof(*ifm));
156 ifm->name = g_strdup(g_value_get_string(val));
157 val = g_hash_table_lookup(config, "Reason");
159 ifm->reason = g_strdup(g_value_get_string(val));
160 val = g_hash_table_lookup(config, "Wireless");
162 ifm->wireless = g_value_get_boolean(val);
164 val = g_hash_table_lookup(config, "SSID");
166 ifm->ssid = g_strdup(g_value_get_string(val));
168 val = g_hash_table_lookup(config, "IPAddress");
170 ifm->ip.s_addr = g_value_get_uint(val);
171 val = g_hash_table_lookup(config, "SubnetCIDR");
173 ifm->cidr = g_value_get_uchar(val);
174 val = g_hash_table_lookup(config, "InterfaceOrder");
176 g_strfreev(interface_order);
177 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
183 if_up(const struct if_msg *ifm)
185 const char *const *r;
187 for (r = up_reasons; *r; r++)
188 if (g_strcmp0(*r, ifm->reason) == 0)
194 print_if_msg(const struct if_msg *ifm)
197 const char *reason = NULL;
199 gboolean showip, showssid;
204 reason = "Acquired address";
206 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
207 reason = "Failed to renew";
208 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
210 reason = "Asssociated with";
211 if (ifm->ssid != NULL)
214 reason = "Cable plugged in";
216 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
218 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
219 reason = "Lost association with";
222 reason = "Not associated";
224 reason = "Cable unplugged";
229 reason = ifm->reason;
231 len = strlen(ifm->name) + 3;
232 len += strlen(reason) + 1;
233 if (ifm->ip.s_addr != 0) {
234 len += 16; /* 000. * 4 */
239 len += strlen(ifm->ssid) + 1;
240 msg = p = g_malloc(len);
241 p += g_snprintf(msg, len, "%s: %s", ifm->name, reason);
243 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
244 if (ifm->ip.s_addr != 0 && showip) {
245 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
247 g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
253 if_msg_comparer(gconstpointer a, gconstpointer b)
255 const struct if_msg *ifa, *ifb;
256 const char *const *order;
258 ifa = (const struct if_msg *)a;
259 ifb = (const struct if_msg *)b;
260 for (order = (const char *const *)interface_order; *order; order++) {
261 if (g_strcmp0(*order, ifa->name) == 0)
263 if (g_strcmp0(*order, ifb->name) == 0)
270 update_online(char **buffer)
273 char *msg, *msgs, *tmp;
276 const struct if_msg *ifm;
280 for (gl = interfaces; gl; gl = gl->next) {
281 ifm = (const struct if_msg *)gl->data;
284 msg = print_if_msg(ifm);
286 tmp = g_strconcat(msgs, "\n", msg, NULL);
294 if (online != ison) {
296 icon = online ? "connect_established" : "connect_no";
297 gtk_status_icon_set_from_icon_name(status_icon, icon);
299 gtk_status_icon_set_tooltip(status_icon, msgs);
310 notify_notification_close(nn, NULL);
320 notify(const char *title, const char *msg, const char *icon)
324 msgs = g_strsplit(msg, "\n", 0);
325 for (m = msgs; *m; m++)
329 notify_notification_close(nn, NULL);
330 if (gtk_status_icon_get_visible(status_icon))
331 nn = notify_notification_new_with_status_icon(title,
336 nn = notify_notification_new(title, msg, icon, NULL);
337 notify_notification_set_timeout(nn, 5000);
338 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
339 notify_notification_show(nn, NULL);
343 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
345 struct if_msg *ifm, *ifp;
349 const char *act, *net;
350 const char *const *r;
353 ifm = make_if_msg(config);
357 rem = ignore_if_msg(ifm);
359 for (gl = interfaces; gl; gl = gl->next) {
360 ifp = (struct if_msg *)gl->data;
361 if (g_strcmp0(ifp->name, ifm->name) == 0) {
364 interfaces = g_list_delete_link(interfaces, gl);
370 if (ifp == NULL && !rem)
371 interfaces = g_list_prepend(interfaces, ifm);
372 interfaces = g_list_sort(interfaces, if_msg_comparer);
375 /* We should ignore renew and stop so we don't annoy the user */
376 if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
377 g_strcmp0(ifm->reason, "STOP") == 0)
380 msg = print_if_msg(ifm);
383 act = "Connected to ";
386 for (r = down_reasons; *r; r++) {
387 if (g_strcmp0(*r, ifm->reason) == 0) {
388 act = "Disconnected from ";
392 if (act && ifm->ip.s_addr) {
393 ipn = htonl(ifm->ip.s_addr);
394 if (IN_LINKLOCAL(ipn))
395 net = "private network";
396 else if (IN_PRIVATE(ipn))
400 title = g_strconcat(act, net, NULL);
404 notify(title, msg, GTK_STOCK_NETWORK);
407 notify("Interface event", msg, GTK_STOCK_NETWORK);
412 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
416 ifm = make_if_msg((GHashTable *)value);
417 if (ignore_if_msg(ifm))
420 interfaces = g_list_prepend(interfaces, ifm);
424 dhcpcd_get_interfaces()
427 GError *error = NULL;
431 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
432 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
433 if (!dbus_g_proxy_call(bus_proxy, "GetInterfaces", &error,
435 otype, &ifs, G_TYPE_INVALID))
436 error_exit("GetInterfaces", error);
437 g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
438 g_hash_table_unref(ifs);
440 /* Each interface config only remembers the last order when
441 * that interface was configured, so get the real order now. */
442 g_strfreev(interface_order);
443 interface_order = NULL;
444 if (!dbus_g_proxy_call(bus_proxy, "ListInterfaces", &error,
446 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
447 error_exit("ListInterfaces", error);
448 interfaces = g_list_sort(interfaces, if_msg_comparer);
451 // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
453 notify("Interface status", msg, GTK_STOCK_NETWORK);
459 check_status(const char *status)
461 static char *last = NULL;
466 GError *error = NULL;
468 g_message("status changed to %s", status);
469 if (g_strcmp0(status, "down") == 0) {
470 for (gl = interfaces; gl; gl = gl->next)
471 free_if_msg((struct if_msg *)gl->data);
472 g_list_free(interfaces);
475 msg = last? "Connection to dhcpcd lost" : "dhcpcd not running";
476 gtk_status_icon_set_tooltip(status_icon, msg);
477 notify("No network", msg, GTK_STOCK_NETWORK);
482 if (g_strcmp0(status, "down") != 0)
485 if (g_strcmp0(status, last) == 0)
487 if (g_strcmp0(last, "down") == 0)
491 last = g_strdup(status);
495 if (!dbus_g_proxy_call(bus_proxy, "GetDhcpcdVersion", &error,
497 G_TYPE_STRING, &version, G_TYPE_INVALID))
498 error_exit("GetDhcpcdVersion", error);
499 g_message("Connected to dhcpcd-%s", version);
501 dhcpcd_get_interfaces();
505 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
507 check_status(status);
511 main(int argc, char *argv[])
513 DBusGConnection *bus;
514 GError *error = NULL;
515 char *version = NULL;
518 gtk_init(&argc, &argv);
519 g_set_application_name("dhcpcd Monitor");
520 status_icon = gtk_status_icon_new_from_icon_name("connect_no");
521 if (status_icon == NULL)
522 status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
524 gtk_status_icon_set_tooltip(status_icon, "Connecting to dhcpcd ...");
525 gtk_status_icon_set_visible(status_icon, TRUE);
527 notify_init(PACKAGE);
529 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
530 if (bus == NULL || error != NULL)
531 error_exit("could not connect to system bus", error);
532 bus_proxy = dbus_g_proxy_new_for_name(bus,
536 if (!dbus_g_proxy_call(bus_proxy, "GetVersion", &error,
538 G_TYPE_STRING, &version, G_TYPE_INVALID))
539 error_exit("GetVersion", error);
540 g_message("Connected to dhcpcd-dbus-%s", version);
543 gtk_status_icon_set_tooltip(status_icon, "Triggering dhcpcd ...");
545 menu_init(status_icon);
547 if (!dbus_g_proxy_call(bus_proxy, "GetStatus", &error,
549 G_TYPE_STRING, &version, G_TYPE_INVALID))
550 error_exit("GetStatus", error);
551 check_status(version);
554 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
555 dbus_g_proxy_add_signal(bus_proxy, "Event",
556 otype, G_TYPE_INVALID);
557 dbus_g_proxy_connect_signal(bus_proxy, "Event",
558 G_CALLBACK(dhcpcd_event),
560 dbus_g_proxy_add_signal(bus_proxy, "StatusChanged",
561 G_TYPE_STRING, G_TYPE_INVALID);
562 dbus_g_proxy_connect_signal(bus_proxy, "StatusChanged",
563 G_CALLBACK(dhcpcd_status),