1 #include <avahi-client/client.h>
2 #include <avahi-common/dbus.h>
8 #define DBUS_API_SUBJECT_TO_CHANGE
10 #include <dbus/dbus-glib-lowlevel.h>
14 struct _AvahiClientPriv
19 static DBusHandlerResult
20 filter_func (DBusConnection *bus, DBusMessage *message, void *data)
24 g_assert (bus != NULL);
25 g_assert (message != NULL);
27 printf ("dbus: interface=%s, path=%s, member=%s\n",
28 dbus_message_get_interface (message),
29 dbus_message_get_path (message),
30 dbus_message_get_member (message));
32 dbus_error_init (&error);
34 if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
35 gchar *name, *old, *new;
36 dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID);
38 if (dbus_error_is_set (&error)) {
39 fprintf(stderr, "Failed to parse NameOwnerChanged message: %s", error.message);
40 dbus_error_free (&error);
44 if (strcmp (name, AVAHI_DBUS_NAME) == 0) {
45 if (old == NULL && new != NULL) {
46 fprintf(stderr, "Avahi Daemon connected\n");
47 } else if (old != NULL && new == NULL) {
48 fprintf(stderr, "Avahi Daemon disconnected\n");
52 return DBUS_HANDLER_RESULT_HANDLED;
56 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
59 static gint _dbus_add_match (DBusConnection *bus, char *type, char *interface, char *sender, char *path)
64 g_assert (bus != NULL);
66 dbus_error_init (&error);
67 filter = g_strdup_printf ("type='%s', interface='%s', sender='%s', path='%s'", type, interface, sender, path);
68 dbus_bus_add_match (bus, filter, &error);
71 if (dbus_error_is_set (&error))
73 fprintf (stderr, "Error adding filter match: %s\n", error.message);
74 dbus_error_free (&error);
87 tmp = g_new (AvahiClient, 1);
88 tmp->priv = g_new (AvahiClientPriv, 1);
90 g_assert (tmp != NULL);
91 g_assert (tmp->priv != NULL);
93 dbus_error_init (&error);
95 tmp->priv->bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
97 dbus_connection_setup_with_g_main (tmp->priv->bus, NULL);
99 if (dbus_error_is_set (&error)) {
100 fprintf(stderr, "Error getting system d-bus: %s\n", error.message);
101 dbus_error_free (&error);
105 dbus_connection_set_exit_on_disconnect (tmp->priv->bus, FALSE);
107 if (!dbus_connection_add_filter (tmp->priv->bus, filter_func, tmp, NULL))
109 fprintf (stderr, "Failed to add d-bus filter\n");
113 if (!_dbus_add_match (tmp->priv->bus, "signal", AVAHI_DBUS_INTERFACE_SERVER, AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER)) goto fail;
114 if (!_dbus_add_match (tmp->priv->bus, "signal", DBUS_INTERFACE_DBUS, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS)) goto fail;
119 if (tmp->priv) free (tmp->priv);
125 avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char *param)
127 DBusMessage *message;
132 g_assert (client != NULL);
133 g_assert (method != NULL);
135 dbus_error_init (&error);
137 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method);
138 fprintf (stderr, "message = dbus_message_new_method_call (%s, %s, %s, %s)\n", AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method);
142 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID))
144 fprintf (stderr, "Failed to append string argument to %s message\n", method);
149 reply = dbus_connection_send_with_reply_and_block (client->priv->bus, message, -1, &error);
151 if (dbus_error_is_set (&error))
153 fprintf (stderr, "Error sending %s message: %s\n", method, error.message);
154 dbus_error_free (&error);
155 dbus_message_unref (message);
161 dbus_message_unref (message);
162 fprintf (stderr, "Could not connect to Avahi daemon\n");
166 dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
168 if (dbus_error_is_set (&error))
170 fprintf (stderr, "Failed to parse %s reply: %s\n", method, error.message);
171 dbus_error_free (&error);
179 avahi_client_get_version_string (AvahiClient *client)
181 return avahi_client_get_string_reply_and_block (client, "GetVersionString", NULL);
185 avahi_client_get_domain_name (AvahiClient *client)
187 return avahi_client_get_string_reply_and_block (client, "GetDomainName", NULL);
191 avahi_client_get_host_name (AvahiClient *client)
193 return avahi_client_get_string_reply_and_block (client, "GetHostName", NULL);
197 avahi_client_get_host_name_fqdn (AvahiClient *client)
199 return avahi_client_get_string_reply_and_block (client, "GetHostNameFqdn", NULL);
203 avahi_client_get_alternative_host_name (AvahiClient *client, char *host)
205 return avahi_client_get_string_reply_and_block (client, "GetAlternativeHostName", host);
209 avahi_client_get_alternative_service_name (AvahiClient *client, char *service)
211 return avahi_client_get_string_reply_and_block (client, "GetAlternativeServiceName", service);