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 #include <arpa/inet.h>
32 #include <dbus/dbus-glib.h>
34 #include <libnotify/notify.h>
39 /* Work out if we have a private address or not
45 # define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) || \
46 ((addr & 0xfff00000) == 0xac100000) || \
47 ((addr & IN_CLASSB_NET) == 0xc0a80000))
50 # define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == 0xa9fe0000)
60 static DBusGProxy *bus_proxy;
61 static GtkStatusIcon *status_icon;
62 static GList *interfaces;
63 static gboolean online;
64 static NotifyNotification *nn;
66 static char **interface_order;
68 const char *const up_reasons[] = {
79 const char *const down_reasons[] = {
88 /* Should be in a header */
89 void notify_close(void);
92 ignore_if_msg(const struct if_msg *ifm)
94 if (g_strcmp0(ifm->reason, "STOP") == 0 ||
95 g_strcmp0(ifm->reason, "RELEASE") == 0)
101 free_if_msg(struct if_msg *ifm)
109 error_exit(const char *msg, GError *error)
114 g_critical("%s: %s", msg, error->message);
115 dialog = gtk_message_dialog_new(NULL,
123 g_critical("%s", msg);
124 dialog = gtk_message_dialog_new(NULL,
131 gtk_dialog_run(GTK_DIALOG(dialog));
132 gtk_widget_destroy(dialog);
133 if (gtk_main_level())
139 static struct if_msg *
140 make_if_msg(GHashTable *config)
145 val = g_hash_table_lookup(config, "Interface");
148 ifm = g_malloc0(sizeof(*ifm));
149 ifm->name = g_strdup(g_value_get_string(val));
150 val = g_hash_table_lookup(config, "Reason");
152 ifm->reason = g_strdup(g_value_get_string(val));
153 val = g_hash_table_lookup(config, "IPAddress");
155 ifm->ip.s_addr = g_value_get_uint(val);
156 val = g_hash_table_lookup(config, "SubnetCIDR");
158 ifm->cidr = g_value_get_uchar(val);
159 val = g_hash_table_lookup(config, "InterfaceOrder");
161 g_strfreev(interface_order);
162 interface_order = g_strsplit(g_value_get_string(val), " ", 0);
168 print_if_msg(const struct if_msg *ifm)
174 len = strlen(ifm->name) + 3;
175 len += strlen(ifm->reason) + 1;
176 if (ifm->ip.s_addr != 0) {
177 len += 16; /* 000. * 4 */
181 msg = p = g_malloc(len);
182 p += g_snprintf(msg, len, "%s: %s", ifm->name, ifm->reason);
184 if (g_strcmp0(ifm->reason, "NOCARRIER") == 0)
186 if (ifm->ip.s_addr != 0 && showip) {
187 p += g_snprintf(p, len - (p - msg), " %s", inet_ntoa(ifm->ip));
189 g_snprintf(p, len - (p - msg), "/%d", ifm->cidr);
195 if_msg_comparer(gconstpointer a, gconstpointer b)
197 const struct if_msg *ifa, *ifb;
198 const char *const *order;
200 ifa = (const struct if_msg *)a;
201 ifb = (const struct if_msg *)b;
202 for (order = (const char *const *)interface_order; *order; order++) {
203 if (g_strcmp0(*order, ifa->name) == 0)
205 if (g_strcmp0(*order, ifb->name) == 0)
212 update_online(char **buffer)
215 char *msg, *msgs, *tmp;
217 const char *const *r;
219 const struct if_msg *ifm;
223 for (gl = interfaces; gl; gl = gl->next) {
224 ifm = (const struct if_msg *)gl->data;
225 for (r = up_reasons; *r; r++) {
226 if (g_strcmp0(*r, ifm->reason) == 0) {
231 msg = print_if_msg(ifm);
233 tmp = g_strconcat(msgs, "\n", msg, NULL);
241 if (online != ison) {
243 icon = online ? GTK_STOCK_CONNECT : GTK_STOCK_DISCONNECT;
244 gtk_status_icon_set_from_stock(status_icon, icon);
246 gtk_status_icon_set_tooltip(status_icon, msgs);
257 notify_notification_close(nn, NULL);
267 notify(const char *title, const char *msg, const char *icon)
271 msgs = g_strsplit(msg, "\n", 0);
272 for (m = msgs; *m; m++)
276 notify_notification_close(nn, NULL);
277 if (gtk_status_icon_get_visible(status_icon))
278 nn = notify_notification_new_with_status_icon(title,
283 nn = notify_notification_new(title, msg, icon, NULL);
284 notify_notification_set_timeout(nn, 5000);
285 g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
286 notify_notification_show(nn, NULL);
290 dhcpcd_event(_unused DBusGProxy *proxy, GHashTable *config, _unused void *data)
292 struct if_msg *ifm, *ifp;
296 const char *act, *net;
297 const char *const *r;
300 ifm = make_if_msg(config);
304 rem = ignore_if_msg(ifm);
306 for (gl = interfaces; gl; gl = gl->next) {
307 ifp = (struct if_msg *)gl->data;
308 if (g_strcmp0(ifp->name, ifm->name) == 0) {
311 interfaces = g_list_delete_link(interfaces, gl);
317 if (ifp == NULL && !rem)
318 interfaces = g_list_prepend(interfaces, ifm);
319 interfaces = g_list_sort(interfaces, if_msg_comparer);
321 msg = print_if_msg(ifm);
324 for (r = up_reasons; *r; r++) {
325 if (g_strcmp0(*r, ifm->reason) == 0) {
326 act = "Connected to ";
330 for (r = down_reasons; *r; r++) {
331 if (g_strcmp0(*r, ifm->reason) == 0) {
332 act = "Disconnected from ";
336 if (act && ifm->ip.s_addr) {
337 ipn = htonl(ifm->ip.s_addr);
338 if (IN_LINKLOCAL(ipn))
339 net = "private network";
340 else if (IN_PRIVATE(ipn))
344 title = g_strconcat(act, net, NULL);
349 notify(title, msg, GTK_STOCK_NETWORK);
352 notify("Interface event", msg, GTK_STOCK_NETWORK);
357 foreach_make_ifm(_unused gpointer key, gpointer value, _unused gpointer data)
361 ifm = make_if_msg((GHashTable *)value);
362 if (ignore_if_msg(ifm))
365 interfaces = g_list_prepend(interfaces, ifm);
369 dhcpcd_get_interfaces()
372 GError *error = NULL;
376 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
377 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, otype);
378 if (!dbus_g_proxy_call(bus_proxy, "GetInterfaces", &error,
380 otype, &ifs, G_TYPE_INVALID))
381 error_exit("GetInterfaces", error);
382 g_hash_table_foreach(ifs, foreach_make_ifm, NULL);
383 g_hash_table_unref(ifs);
385 /* Each interface config only remembers the last order when
386 * that interface was configured, so get the real order now. */
387 g_strfreev(interface_order);
388 interface_order = NULL;
389 if (!dbus_g_proxy_call(bus_proxy, "ListInterfaces", &error,
391 G_TYPE_STRV, &interface_order, G_TYPE_INVALID))
392 error_exit("ListInterfaces", error);
393 interfaces = g_list_sort(interfaces, if_msg_comparer);
396 // GTK+ 2.16 msg = gtk_status_icon_get_tooltip_text(status_icon);
398 notify("Interface status", msg, GTK_STOCK_NETWORK);
404 check_status(const char *status)
406 static char *last = NULL;
411 GError *error = NULL;
413 g_message("status changed to %s", status);
414 if (g_strcmp0(status, "down") == 0) {
415 for (gl = interfaces; gl; gl = gl->next)
416 free_if_msg((struct if_msg *)gl->data);
417 g_list_free(interfaces);
420 msg = last? "Connection to dhcpcd lost" : "dhcpcd not running";
421 gtk_status_icon_set_tooltip(status_icon, msg);
422 notify("No network", msg, GTK_STOCK_NETWORK);
427 if (g_strcmp0(status, "down") != 0)
430 if (g_strcmp0(status, last) == 0)
432 if (g_strcmp0(last, "down") == 0)
436 last = g_strdup(status);
440 if (!dbus_g_proxy_call(bus_proxy, "GetDhcpcdVersion", &error,
442 G_TYPE_STRING, &version, G_TYPE_INVALID))
443 error_exit("GetDhcpcdVersion", error);
444 g_message("Connected to dhcpcd-%s", version);
446 dhcpcd_get_interfaces();
450 dhcpcd_status(_unused DBusGProxy *proxy, const char *status, _unused void *data)
452 check_status(status);
456 main(int argc, char *argv[])
458 DBusGConnection *bus;
459 GError *error = NULL;
460 char *version = NULL;
463 gtk_init(&argc, &argv);
464 g_set_application_name("dhcpcd Monitor");
465 status_icon = gtk_status_icon_new_from_stock(GTK_STOCK_DISCONNECT);
466 gtk_status_icon_set_tooltip(status_icon, "Connecting to dhcpcd ...");
467 gtk_status_icon_set_visible(status_icon, TRUE);
469 notify_init(PACKAGE);
471 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
472 if (bus == NULL || error != NULL)
473 error_exit("could not connect to system bus", error);
474 bus_proxy = dbus_g_proxy_new_for_name(bus,
478 if (!dbus_g_proxy_call(bus_proxy, "GetVersion", &error,
480 G_TYPE_STRING, &version, G_TYPE_INVALID))
481 error_exit("GetVersion", error);
482 g_message("Connected to dhcpcd-dbus-%s", version);
485 gtk_status_icon_set_tooltip(status_icon, "Triggering dhcpcd ...");
487 menu_init(status_icon);
489 if (!dbus_g_proxy_call(bus_proxy, "GetStatus", &error,
491 G_TYPE_STRING, &version, G_TYPE_INVALID))
492 error_exit("GetStatus", error);
493 check_status(version);
496 otype = dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE);
497 dbus_g_proxy_add_signal(bus_proxy, "Event",
498 otype, G_TYPE_INVALID);
499 dbus_g_proxy_connect_signal(bus_proxy, "Event",
500 G_CALLBACK(dhcpcd_event),
502 dbus_g_proxy_add_signal(bus_proxy, "StatusChanged",
503 G_TYPE_STRING, G_TYPE_INVALID);
504 dbus_g_proxy_connect_signal(bus_proxy, "StatusChanged",
505 G_CALLBACK(dhcpcd_status),