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 gboolean carrier;
71 static NotifyNotification *nn;
73 static char **interface_order;
75 const char *const up_reasons[] = {
86 const char *const down_reasons[] = {
95 /* Should be in a header */
96 void notify_close(void);
99 ignore_if_msg(const struct if_msg *ifm)
101 if (g_strcmp0(ifm->reason, "STOP") == 0 ||
102 g_strcmp0(ifm->reason, "RELEASE") == 0)
108 free_if_msg(struct if_msg *ifm)
117 error_exit(const char *msg, GError *error)
122 g_critical("%s: %s", msg, error->message);
123 dialog = gtk_message_dialog_new(NULL,
131 g_critical("%s", msg);
132 dialog = gtk_message_dialog_new(NULL,
139 gtk_dialog_run(GTK_DIALOG(dialog));
140 gtk_widget_destroy(dialog);
141 if (gtk_main_level())
147 static struct if_msg *
148 make_if_msg(GHashTable *config)
153 val = g_hash_table_lookup(config, "Interface");
156 ifm = g_malloc0(sizeof(*ifm));
157 ifm->name = g_strdup(g_value_get_string(val));
158 val = g_hash_table_lookup(config, "Reason");
160 ifm->reason = g_strdup(g_value_get_string(val));
161 val = g_hash_table_lookup(config, "Wireless");
163 ifm->wireless = g_value_get_boolean(val);
165 val = g_hash_table_lookup(config, "SSID");
167 ifm->ssid = g_strdup(g_value_get_string(val));
169 val = g_hash_table_lookup(config, "IPAddress");
171 ifm->ip.s_addr = g_value_get_uint(val);
172 val = g_hash_table_lookup(config, "SubnetCIDR");
174 ifm->cidr = g_value_get_uchar(val);
175 val = g_hash_table_lookup(config, "InterfaceOrder");
177 g_strfreev(interface_order);
178 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
184 if_up(const struct if_msg *ifm)
186 const char *const *r;
188 for (r = up_reasons; *r; r++)
189 if (g_strcmp0(*r, ifm->reason) == 0)
195 print_if_msg(const struct if_msg *ifm)
198 const char *reason = NULL;
200 gboolean showip, showssid;
205 reason = "Acquired address";
207 if (g_strcmp0(ifm->reason, "EXPIRE") == 0)
208 reason = "Failed to renew";
209 else if (g_strcmp0(ifm->reason, "CARRIER") == 0) {
211 reason = "Asssociated with";
212 if (ifm->ssid != NULL)
215 reason = "Cable plugged in";
217 } else if (g_strcmp0(ifm->reason, "NOCARRIER") == 0) {
219 if (ifm->ssid != NULL || ifm->ip.s_addr != 0) {
220 reason = "Lost association with";
223 reason = "Not associated";
225 reason = "Cable unplugged";
230 reason = ifm->reason;
232 len = strlen(ifm->name) + 3;
233 len += strlen(reason) + 1;
234 if (ifm->ip.s_addr != 0) {
235 len += 16; /* 000. * 4 */
240 len += strlen(ifm->ssid) + 1;
241 msg = p = g_malloc(len);
242 p += g_snprintf(msg, len, "%s: %s", ifm->name, reason);
244 p += g_snprintf(p, len - (p - msg), " %s", ifm->ssid);
245 if (ifm->ip.s_addr != 0 && showip) {
246 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
248 g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
254 if_msg_comparer(gconstpointer a, gconstpointer b)
256 const struct if_msg *ifa, *ifb;
257 const char *const *order;
259 ifa = (const struct if_msg *)a;
260 ifb = (const struct if_msg *)b;
261 for (order = (const char *const *)interface_order; *order; order++) {
262 if (g_strcmp0(*order, ifa->name) == 0)
264 if (g_strcmp0(*order, ifb->name) == 0)
271 update_online(char **buffer)
273 gboolean ison, iscarrier;
274 char *msg, *msgs, *tmp;
277 const struct if_msg *ifm;
279 ison = iscarrier = FALSE;
281 for (gl = interfaces; gl; gl = gl->next) {
282 ifm = (const struct if_msg *)gl->data;
284 ison = iscarrier = TRUE;
285 if (!iscarrier && g_strcmp0(ifm->reason, "CARRIER") == 0)
287 msg = print_if_msg(ifm);
289 tmp = g_strconcat(msgs, "\n", msg, NULL);
297 if (online != ison || carrier != iscarrier) {
300 icon = "network-transmit-receive";
302 icon = "network-transmit";
304 icon = "network-offline";
305 gtk_status_icon_set_from_icon_name(status_icon, icon);
307 gtk_status_icon_set_tooltip(status_icon, msgs);
318 notify_notification_close(nn, NULL);
328 notify(const char *title, const char *msg, const char *icon)
332 msgs = g_strsplit(msg, "\n", 0);
333 for (m = msgs; *m; m++)
337 notify_notification_close(nn, NULL);
338 if (gtk_status_icon_get_visible(status_icon))
339 nn = notify_notification_new_with_status_icon(title,
344 nn = notify_notification_new(title, msg, icon, NULL);
345 notify_notification_set_timeout(nn, 5000);
346 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
347 notify_notification_show(nn, NULL);
351 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
353 struct if_msg *ifm, *ifp;
357 const char *act, *net;
358 const char *const *r;
361 ifm = make_if_msg(config);
365 rem = ignore_if_msg(ifm);
367 for (gl = interfaces; gl; gl = gl->next) {
368 ifp = (struct if_msg *)gl->data;
369 if (g_strcmp0(ifp->name, ifm->name) == 0) {
372 interfaces = g_list_delete_link(interfaces, gl);
378 if (ifp == NULL && !rem)
379 interfaces = g_list_prepend(interfaces, ifm);
380 interfaces = g_list_sort(interfaces, if_msg_comparer);
383 /* We should ignore renew and stop so we don't annoy the user */
384 if (g_strcmp0(ifm->reason, "RENEW") == 0 ||
385 g_strcmp0(ifm->reason, "STOP") == 0)
388 msg = print_if_msg(ifm);
391 act = "Connected to ";
394 for (r = down_reasons; *r; r++) {
395 if (g_strcmp0(*r, ifm->reason) == 0) {
396 act = "Disconnected from ";
400 if (act && ifm->ip.s_addr) {
401 ipn = htonl(ifm->ip.s_addr);
402 if (IN_LINKLOCAL(ipn))
403 net = "private network";
404 else if (IN_PRIVATE(ipn))
408 title = g_strconcat(act, net, NULL);
412 notify(title, msg, GTK_STOCK_NETWORK);
415 notify("Interface event", msg, GTK_STOCK_NETWORK);
420 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
424 ifm = make_if_msg((GHashTable *)value);
425 if (ignore_if_msg(ifm))
428 interfaces = g_list_prepend(interfaces, ifm);
432 dhcpcd_get_interfaces()
435 GError *error = NULL;
439 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
440 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
441 if (!dbus_g_proxy_call(bus_proxy, "GetInterfaces", &error,
443 otype, &ifs, G_TYPE_INVALID))
444 error_exit("GetInterfaces", error);
445 g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
446 g_hash_table_unref(ifs);
448 /* Each interface config only remembers the last order when
449 * that interface was configured, so get the real order now. */
450 g_strfreev(interface_order);
451 interface_order = NULL;
452 if (!dbus_g_proxy_call(bus_proxy, "ListInterfaces", &error,
454 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
455 error_exit("ListInterfaces", error);
456 interfaces = g_list_sort(interfaces, if_msg_comparer);
459 // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
461 notify("Interface status", msg, GTK_STOCK_NETWORK);
467 check_status(const char *status)
469 static char *last = NULL;
474 GError *error = NULL;
476 g_message("status changed to %s", status);
477 if (g_strcmp0(status, "down") == 0) {
478 for (gl = interfaces; gl; gl = gl->next)
479 free_if_msg((struct if_msg *)gl->data);
480 g_list_free(interfaces);
483 msg = last? "Connection to dhcpcd lost" : "dhcpcd not running";
484 gtk_status_icon_set_tooltip(status_icon, msg);
485 notify("No network", msg, GTK_STOCK_NETWORK);
490 if (g_strcmp0(status, "down") != 0)
493 if (g_strcmp0(status, last) == 0)
495 if (g_strcmp0(last, "down") == 0)
499 last = g_strdup(status);
503 if (!dbus_g_proxy_call(bus_proxy, "GetDhcpcdVersion", &error,
505 G_TYPE_STRING, &version, G_TYPE_INVALID))
506 error_exit("GetDhcpcdVersion", error);
507 g_message("Connected to dhcpcd-%s", version);
509 dhcpcd_get_interfaces();
513 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
515 check_status(status);
519 main(int argc, char *argv[])
521 DBusGConnection *bus;
522 GError *error = NULL;
523 char *version = NULL;
527 gtk_init(&argc, &argv);
528 g_set_application_name("dhcpcd Monitor");
529 status_icon = gtk_status_icon_new_from_icon_name("network-offline");
530 if (status_icon == NULL)
531 status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
533 gtk_status_icon_set_tooltip(status_icon, "Connecting to dhcpcd ...");
534 gtk_status_icon_set_visible(status_icon, TRUE);
536 notify_init(PACKAGE);
538 g_message("connecting to dbus ...");
539 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
540 if (bus == NULL || error != NULL)
541 error_exit("could not connect to system bus", error);
542 bus_proxy = dbus_g_proxy_new_for_name(bus,
547 g_message("connecting to dhcpcd-dbus ...");
548 while (--tries > 0) {
549 g_clear_error(&error);
550 if (dbus_g_proxy_call_with_timeout(bus_proxy,
561 error_exit("GetVersion", error);
562 g_message("Connected to dhcpcd-dbus-%s", version);
565 gtk_status_icon_set_tooltip(status_icon, "Triggering dhcpcd ...");
567 menu_init(status_icon);
569 if (!dbus_g_proxy_call(bus_proxy, "GetStatus", &error,
571 G_TYPE_STRING, &version, G_TYPE_INVALID))
572 error_exit("GetStatus", error);
573 check_status(version);
576 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
577 dbus_g_proxy_add_signal(bus_proxy, "Event",
578 otype, G_TYPE_INVALID);
579 dbus_g_proxy_connect_signal(bus_proxy, "Event",
580 G_CALLBACK(dhcpcd_event),
582 dbus_g_proxy_add_signal(bus_proxy, "StatusChanged",
583 G_TYPE_STRING, G_TYPE_INVALID);
584 dbus_g_proxy_connect_signal(bus_proxy, "StatusChanged",
585 G_CALLBACK(dhcpcd_status),