4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <dbus/dbus.h>
32 #include <avahi-common/dbus.h>
33 #include <avahi-common/llist.h>
34 #include <avahi-common/error.h>
35 #include <avahi-common/dbus.h>
36 #include <avahi-common/malloc.h>
37 #include <avahi-common/dbus-watch-glue.h>
42 int avahi_client_set_errno (AvahiClient *client, int error) {
45 return client->error = error;
48 int avahi_client_set_dbus_error(AvahiClient *client, DBusError *error) {
52 return avahi_client_set_errno(client, avahi_error_dbus_to_number(error->name));
55 static void client_set_state (AvahiClient *client, AvahiServerState state) {
58 if (client->state == state)
61 client->state = state;
63 switch (client->state) {
64 case AVAHI_CLIENT_DISCONNECTED:
66 dbus_connection_disconnect(client->bus);
67 dbus_connection_unref(client->bus);
73 case AVAHI_CLIENT_S_COLLISION:
74 case AVAHI_CLIENT_S_REGISTERING:
76 /* Clear cached strings */
77 avahi_free(client->host_name);
78 avahi_free(client->host_name_fqdn);
79 avahi_free(client->domain_name);
81 client->host_name = NULL;
82 client->host_name_fqdn = NULL;
83 client->domain_name = NULL;
86 case AVAHI_CLIENT_S_INVALID:
87 case AVAHI_CLIENT_S_RUNNING:
93 client->callback (client, state, client->userdata);
96 static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, void *userdata) {
97 AvahiClient *client = userdata;
103 dbus_error_init (&error);
105 /* fprintf(stderr, "dbus: interface=%s, path=%s, member=%s\n", */
106 /* dbus_message_get_interface (message), */
107 /* dbus_message_get_path (message), */
108 /* dbus_message_get_member (message)); */
110 if (client->state == AVAHI_CLIENT_DISCONNECTED)
113 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
115 /* The DBUS server died or kicked us */
116 client_set_state(client, AVAHI_CLIENT_DISCONNECTED);
118 } if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
119 char *name, *old, *new;
121 if (!(dbus_message_get_args(
123 DBUS_TYPE_STRING, &name,
124 DBUS_TYPE_STRING, &old,
125 DBUS_TYPE_STRING, &new,
126 DBUS_TYPE_INVALID) || dbus_error_is_set (&error))) {
128 fprintf(stderr, "WARNING: Failed to parse NameOwnerChanged signal: %s\n", error.message);
132 if (strcmp(name, AVAHI_DBUS_NAME) == 0)
134 /* Regardless if the server lost or acquired its name or
135 * if the name was transfered: our services are no longer
136 * available, so we disconnect ourselves */
138 client_set_state(client, AVAHI_CLIENT_DISCONNECTED);
140 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged")) {
143 if (!(dbus_message_get_args(
145 DBUS_TYPE_INT32, &state,
146 DBUS_TYPE_INVALID) || dbus_error_is_set (&error))) {
147 fprintf(stderr, "WARNING: Failed to parse Server.StateChanged signal: %s\n", error.message);
151 client_set_state(client, (AvahiClientState) state);
153 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
156 path = dbus_message_get_path(message);
158 for (g = client->groups; g; g = g->groups_next)
159 if (strcmp(g->path, path) == 0)
164 if (!(dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID)) ||
165 dbus_error_is_set(&error)) {
166 fprintf(stderr, "WARNING: Failed to parse EntryGroup.StateChanged signal: %s\n", error.message);
170 avahi_entry_group_set_state(g, state);
173 } else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemNew"))
174 return avahi_domain_browser_event(client, AVAHI_BROWSER_NEW, message);
175 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemRemove"))
176 return avahi_domain_browser_event(client, AVAHI_BROWSER_REMOVE, message);
178 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew"))
179 return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message);
180 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove"))
181 return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message);
183 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew"))
184 return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message);
185 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove"))
186 return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message);
188 return DBUS_HANDLER_RESULT_HANDLED;
192 dbus_error_free (&error);
193 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
196 static int get_server_state(AvahiClient *client, int *ret_error) {
197 DBusMessage *message, *reply;
204 dbus_error_init(&error);
206 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState")))
209 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
210 dbus_message_unref(message);
215 if (!(dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID)))
218 client_set_state(client, (AvahiServerState) state);
223 if (dbus_error_is_set(&error)) {
224 e = avahi_error_dbus_to_number (error.name);
225 dbus_error_free(&error);
227 e = AVAHI_ERR_NO_MEMORY;
235 /* This function acts like dbus_bus_get but creates a private
236 * connection instead */
237 static DBusConnection*
238 avahi_dbus_bus_get (DBusBusType type, DBusError *error)
240 DBusConnection *conn;
243 env_addr = getenv ("DBUS_SYSTEM_BUS_ADDRESS");
245 if (env_addr == NULL || (strcmp (env_addr, "") == 0))
247 env_addr = DBUS_SYSTEM_BUS_DEFAULT_ADDRESS;
250 conn = dbus_connection_open_private (env_addr, error);
254 printf ("Failed to open private connection: %s\n", error->message);
258 dbus_connection_set_exit_on_disconnect (conn, TRUE);
260 if (!dbus_bus_register (conn, error))
262 printf ("Failed to register connection\n");
263 dbus_connection_close (conn);
264 dbus_connection_unref (conn);
272 AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *ret_error) {
273 AvahiClient *client = NULL;
276 dbus_error_init (&error);
278 if (!(client = avahi_new(AvahiClient, 1))) {
280 *ret_error = AVAHI_ERR_NO_MEMORY;
284 client->poll_api = poll_api;
285 client->error = AVAHI_OK;
286 client->callback = callback;
287 client->userdata = userdata;
288 client->state = AVAHI_CLIENT_DISCONNECTED;
290 client->host_name = NULL;
291 client->host_name_fqdn = NULL;
292 client->domain_name = NULL;
293 client->version_string = NULL;
295 AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
296 AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
297 AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
298 AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
299 AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers);
301 if (!(client->bus = avahi_dbus_bus_get(DBUS_BUS_SYSTEM, &error)) ||
302 dbus_error_is_set (&error))
305 if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
307 *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
311 if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
313 *ret_error = AVAHI_ERR_NO_MEMORY;
320 "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
321 "sender='" AVAHI_DBUS_NAME "', "
322 "path='" AVAHI_DBUS_PATH_SERVER "'",
325 if (dbus_error_is_set (&error))
331 "interface='" DBUS_INTERFACE_DBUS "', "
332 "sender='" DBUS_SERVICE_DBUS "', "
333 "path='" DBUS_PATH_DBUS "'",
336 if (dbus_error_is_set (&error))
342 "interface='" DBUS_INTERFACE_LOCAL "'",
345 if (dbus_error_is_set (&error))
348 if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error)) ||
349 dbus_error_is_set(&error)) {
352 *ret_error = AVAHI_ERR_NO_DAEMON;
357 if (get_server_state(client, ret_error) < 0)
365 avahi_client_free(client);
367 if (dbus_error_is_set(&error)) {
370 *ret_error = avahi_error_dbus_to_number(error.name);
372 dbus_error_free(&error);
378 void avahi_client_free(AvahiClient *client) {
381 while (client->groups)
382 avahi_entry_group_free(client->groups);
384 while (client->domain_browsers)
385 avahi_domain_browser_free(client->domain_browsers);
387 while (client->service_browsers)
388 avahi_service_browser_free(client->service_browsers);
390 while (client->service_type_browsers)
391 avahi_service_type_browser_free(client->service_type_browsers);
393 while (client->service_resolvers)
394 avahi_service_resolver_free(client->service_resolvers);
397 dbus_connection_disconnect(client->bus);
398 dbus_connection_unref(client->bus);
401 avahi_free(client->version_string);
402 avahi_free(client->host_name);
403 avahi_free(client->host_name_fqdn);
404 avahi_free(client->domain_name);
409 static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) {
410 DBusMessage *message = NULL, *reply = NULL;
417 dbus_error_init (&error);
419 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method))) {
420 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
425 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID)) {
426 avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
431 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
433 if (!reply || dbus_error_is_set (&error))
436 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID) ||
437 dbus_error_is_set (&error))
440 if (!(n = avahi_strdup(ret))) {
441 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
445 dbus_message_unref(message);
446 dbus_message_unref(reply);
453 dbus_message_unref(message);
455 dbus_message_unref(reply);
457 if (dbus_error_is_set(&error)) {
458 avahi_client_set_dbus_error(client, &error);
459 dbus_error_free(&error);
465 const char* avahi_client_get_version_string (AvahiClient *client) {
468 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
469 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
473 if (!client->version_string)
474 client->version_string = avahi_client_get_string_reply_and_block(client, "GetVersionString", NULL);
476 return client->version_string;
479 const char* avahi_client_get_domain_name (AvahiClient *client) {
482 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
483 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
487 if (!client->domain_name)
488 client->domain_name = avahi_client_get_string_reply_and_block(client, "GetDomainName", NULL);
490 return client->domain_name;
493 const char* avahi_client_get_host_name (AvahiClient *client) {
496 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
497 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
501 if (!client->host_name)
502 client->host_name = avahi_client_get_string_reply_and_block(client, "GetHostName", NULL);
504 return client->host_name;
507 const char* avahi_client_get_host_name_fqdn (AvahiClient *client) {
510 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
511 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
515 if (!client->host_name_fqdn)
516 client->host_name_fqdn = avahi_client_get_string_reply_and_block(client, "GetHostNameFqdn", NULL);
518 return client->host_name_fqdn;
521 AvahiClientState avahi_client_get_state(AvahiClient *client) {
524 return client->state;
527 int avahi_client_errno(AvahiClient *client) {
530 return client->error;