+#include <avahi-common/dbus.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <glib.h>
+#include <string.h>
+
+#define DBUS_API_SUBJECT_TO_CHANGE
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+#include <stdlib.h>
+
+struct _AvahiClientPriv
+{
+ DBusConnection *bus;
+};
+
+static DBusHandlerResult
+filter_func (DBusConnection *bus, DBusMessage *message, void *data)
+{
+ DBusError error;
+
+ g_assert (bus != NULL);
+ g_assert (message != NULL);
+
+ printf ("dbus: interface=%s, path=%s, member=%s\n",
+ dbus_message_get_interface (message),
+ dbus_message_get_path (message),
+ dbus_message_get_member (message));
+
+ dbus_error_init (&error);
+
+ if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
+ gchar *name, *old, *new;
+ dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID);
+
+ if (dbus_error_is_set (&error)) {
+ fprintf(stderr, "Failed to parse NameOwnerChanged message: %s", error.message);
+ dbus_error_free (&error);
+ goto out;
+ }
+
+ if (strcmp (name, AVAHI_DBUS_NAME) == 0) {
+ if (old == NULL && new != NULL) {
+ fprintf(stderr, "Avahi Daemon connected\n");
+ } else if (old != NULL && new == NULL) {
+ fprintf(stderr, "Avahi Daemon disconnected\n");
+ }
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+out:
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static gint _dbus_add_match (DBusConnection *bus, char *type, char *interface, char *sender, char *path)
+{
+ DBusError error;
+ char *filter;
+
+ g_assert (bus != NULL);
+
+ dbus_error_init (&error);
+ filter = g_strdup_printf ("type='%s', interface='%s', sender='%s', path='%s'", type, interface, sender, path);
+ dbus_bus_add_match (bus, filter, &error);
+ g_free (filter);
+
+ if (dbus_error_is_set (&error))
+ {
+ fprintf (stderr, "Error adding filter match: %s\n", error.message);
+ dbus_error_free (&error);
+ return FALSE;
+ }
+
+ return TRUE;
+}