Lines Matching defs:*
0 /* $Id$ */
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
19 USA.
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
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>
39 #include "client.h"
40 #include "internal.h"
42 #define AVAHI_CLIENT_DBUS_API_SUPPORTED ((uint32_t) 0x0201)
44 static int init_server(AvahiClient *client, int *ret_error);
46 int avahi_client_set_errno (AvahiClient *client, int error) {
47 assert(client);
49 return client->error = error;
52 int avahi_client_set_dbus_error(AvahiClient *client, DBusError *error) {
53 assert(client);
54 assert(error);
56 return avahi_client_set_errno(client, avahi_error_dbus_to_number(error->name));
59 static void client_set_state (AvahiClient *client, AvahiServerState state) {
60 assert(client);
62 if (client->state == state)
63 return;
65 client->state = state;
67 switch (client->state) {
68 case AVAHI_CLIENT_FAILURE:
69 if (client->bus) {
70 #ifdef HAVE_DBUS_CONNECTION_CLOSE
71 dbus_connection_close(client->bus);
72 #else
73 dbus_connection_disconnect(client->bus);
74 #endif
75 dbus_connection_unref(client->bus);
76 client->bus = NULL;
79 /* Fall through */
81 case AVAHI_CLIENT_S_COLLISION:
82 case AVAHI_CLIENT_S_REGISTERING:
84 /* Clear cached strings */
85 avahi_free(client->host_name);
86 avahi_free(client->host_name_fqdn);
87 avahi_free(client->domain_name);
89 client->host_name = NULL;
90 client->host_name_fqdn = NULL;
91 client->domain_name = NULL;
92 break;
94 case AVAHI_CLIENT_S_RUNNING:
95 case AVAHI_CLIENT_CONNECTING:
96 break;
100 if (client->callback)
101 client->callback (client, state, client->userdata);
104 static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, void *userdata) {
105 AvahiClient *client = userdata;
106 DBusError error;
108 assert(bus);
109 assert(message);
111 dbus_error_init(&error);
113 /* fprintf(stderr, "dbus: interface=%s, path=%s, member=%s\n", */
114 /* dbus_message_get_interface (message), */
115 /* dbus_message_get_path (message), */
116 /* dbus_message_get_member (message)); */
118 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
120 /* The DBUS server died or kicked us */
121 avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED);
122 goto fail;
124 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameAcquired")) {
126 /* Ignore this message */
128 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
129 char *name, *old, *new;
131 if (!dbus_message_get_args(
132 message, &error,
133 DBUS_TYPE_STRING, &name,
134 DBUS_TYPE_STRING, &old,
135 DBUS_TYPE_STRING, &new,
136 DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
138 fprintf(stderr, "WARNING: Failed to parse NameOwnerChanged signal: %s\n", error.message);
139 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
140 goto fail;
143 if (strcmp(name, AVAHI_DBUS_NAME) == 0) {
145 if (avahi_client_is_connected(client)) {
147 /* Regardless if the server lost or acquired its name or
148 * if the name was transfered: our services are no longer
149 * available, so we disconnect ourselves */
150 avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED);
151 goto fail;
153 } else if (client->state == AVAHI_CLIENT_CONNECTING && (!old || *old == 0)) {
154 int ret;
156 /* Server appeared */
158 if ((ret = init_server(client, NULL)) < 0) {
159 avahi_client_set_errno(client, ret);
160 goto fail;
165 } else if (!avahi_client_is_connected(client)) {
167 /* Ignore messages we get in unconnected state */
169 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged")) {
170 int32_t state;
171 char *e = NULL;
172 int c;
174 if (!dbus_message_get_args(
175 message, &error,
176 DBUS_TYPE_INT32, &state,
177 DBUS_TYPE_STRING, &e,
178 DBUS_TYPE_INVALID) || dbus_error_is_set (&error)) {
180 fprintf(stderr, "WARNING: Failed to parse Server.StateChanged signal: %s\n", error.message);
181 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
182 goto fail;
185 if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK)
186 avahi_client_set_errno(client, c);
188 client_set_state(client, (AvahiClientState) state);
190 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
191 const char *path;
192 AvahiEntryGroup *g;
193 path = dbus_message_get_path(message);
195 for (g = client->groups; g; g = g->groups_next)
196 if (strcmp(g->path, path) == 0)
197 break;
199 if (g) {
200 int32_t state;
201 char *e;
202 int c;
204 if (!dbus_message_get_args(
205 message, &error,
206 DBUS_TYPE_INT32, &state,
207 DBUS_TYPE_STRING, &e,
208 DBUS_TYPE_INVALID) ||
209 dbus_error_is_set(&error)) {
211 fprintf(stderr, "WARNING: Failed to parse EntryGroup.StateChanged signal: %s\n", error.message);
212 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
213 goto fail;
216 if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK)
217 avahi_client_set_errno(client, c);
219 avahi_entry_group_set_state(g, state);
222 } else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemNew"))
223 return avahi_domain_browser_event(client, AVAHI_BROWSER_NEW, message);
224 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemRemove"))
225 return avahi_domain_browser_event(client, AVAHI_BROWSER_REMOVE, message);
226 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "CacheExhausted"))
227 return avahi_domain_browser_event(client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
228 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "AllForNow"))
229 return avahi_domain_browser_event(client, AVAHI_BROWSER_ALL_FOR_NOW, message);
230 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Failure"))
231 return avahi_domain_browser_event(client, AVAHI_BROWSER_FAILURE, message);
233 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew"))
234 return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message);
235 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove"))
236 return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message);
237 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "CacheExhausted"))
238 return avahi_service_type_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
239 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "AllForNow"))
240 return avahi_service_type_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message);
241 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Failure"))
242 return avahi_service_type_browser_event (client, AVAHI_BROWSER_FAILURE, message);
244 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew"))
245 return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message);
246 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove"))
247 return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message);
248 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "CacheExhausted"))
249 return avahi_service_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
250 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "AllForNow"))
251 return avahi_service_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message);
252 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Failure"))
253 return avahi_service_browser_event (client, AVAHI_BROWSER_FAILURE, message);
255 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Found"))
256 return avahi_service_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
257 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Failure"))
258 return avahi_service_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);
260 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Found"))
261 return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
262 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Failure"))
263 return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);
265 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Found"))
266 return avahi_address_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
267 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Failure"))
268 return avahi_address_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);
270 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "ItemNew"))
271 return avahi_record_browser_event (client, AVAHI_BROWSER_NEW, message);
272 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "ItemRemove"))
273 return avahi_record_browser_event (client, AVAHI_BROWSER_REMOVE, message);
274 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "CacheExhausted"))
275 return avahi_record_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
276 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "AllForNow"))
277 return avahi_record_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message);
278 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "Failure"))
279 return avahi_record_browser_event (client, AVAHI_BROWSER_FAILURE, message);
281 else {
283 fprintf(stderr, "WARNING: Unhandled message: interface=%s, path=%s, member=%s\n",
284 dbus_message_get_interface(message),
285 dbus_message_get_path(message),
286 dbus_message_get_member(message));
288 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
291 return DBUS_HANDLER_RESULT_HANDLED;
293 fail:
295 if (dbus_error_is_set(&error)) {
296 avahi_client_set_errno(client, avahi_error_dbus_to_number(error.name));
297 dbus_error_free(&error);
300 client_set_state(client, AVAHI_CLIENT_FAILURE);
302 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
305 static int get_server_state(AvahiClient *client, int *ret_error) {
306 DBusMessage *message = NULL, *reply = NULL;
307 DBusError error;
308 int32_t state;
309 int e = AVAHI_ERR_NO_MEMORY;
311 assert(client);
313 dbus_error_init(&error);
315 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState")))
316 goto fail;
318 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
320 if (!reply || dbus_error_is_set (&error))
321 goto fail;
323 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) ||
324 dbus_error_is_set (&error))
325 goto fail;
327 client_set_state(client, (AvahiServerState) state);
329 dbus_message_unref(message);
330 dbus_message_unref(reply);
332 return AVAHI_OK;
334 fail:
335 if (dbus_error_is_set(&error)) {
336 e = avahi_error_dbus_to_number (error.name);
337 dbus_error_free(&error);
340 if (ret_error)
341 *ret_error = e;
343 if (message)
344 dbus_message_unref(message);
345 if (reply)
346 dbus_message_unref(reply);
348 return e;
351 static int check_version(AvahiClient *client, int *ret_error) {
352 DBusMessage *message = NULL, *reply = NULL;
353 DBusError error;
354 uint32_t version;
355 int e = AVAHI_ERR_NO_MEMORY;
357 assert(client);
359 dbus_error_init(&error);
361 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetAPIVersion")))
362 goto fail;
364 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
366 if (!reply || dbus_error_is_set (&error)) {
367 char *version_str;
369 if (!dbus_error_is_set(&error) || strcmp(error.name, DBUS_ERROR_UNKNOWN_METHOD))
370 goto fail;
372 /* If the method GetAPIVersion is not known, we look if
373 * GetVersionString matches "avahi 0.6" which is the only
374 * version we support which doesn't have GetAPIVersion() .*/
376 dbus_message_unref(message);
377 if (reply) dbus_message_unref(reply);
378 dbus_error_free(&error);
380 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")))
381 goto fail;
383 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
385 if (!reply || dbus_error_is_set (&error))
386 goto fail;
388 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &version_str, DBUS_TYPE_INVALID) ||
389 dbus_error_is_set (&error))
390 goto fail;
392 version = strcmp(version_str, "avahi 0.6") == 0 ? 0x0201 : 0x0000;
394 } else {
396 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &version, DBUS_TYPE_INVALID) ||
397 dbus_error_is_set(&error))
398 goto fail;
401 /*fprintf(stderr, "API Version 0x%04x\n", version);*/
403 if ((version & 0xFF00) != (AVAHI_CLIENT_DBUS_API_SUPPORTED & 0xFF00) ||
404 (version & 0x00FF) < (AVAHI_CLIENT_DBUS_API_SUPPORTED & 0x00FF)) {
405 e = AVAHI_ERR_VERSION_MISMATCH;
406 goto fail;
409 dbus_message_unref(message);
410 dbus_message_unref(reply);
412 return AVAHI_OK;
414 fail:
415 if (dbus_error_is_set(&error)) {
416 e = avahi_error_dbus_to_number (error.name);
417 dbus_error_free(&error);
420 if (ret_error)
421 *ret_error = e;
423 if (message)
424 dbus_message_unref(message);
425 if (reply)
426 dbus_message_unref(reply);
428 return e;
431 static int init_server(AvahiClient *client, int *ret_error) {
432 int r;
434 if ((r = check_version(client, ret_error)) < 0)
435 return r;
437 if ((r = get_server_state(client, ret_error)) < 0)
438 return r;
440 return AVAHI_OK;
443 /* This function acts like dbus_bus_get but creates a private
444 * connection instead. */
445 static DBusConnection* avahi_dbus_bus_get(DBusError *error) {
446 DBusConnection *c;
448 #ifdef HAVE_DBUS_BUS_GET_PRIVATE
449 if (!(c = dbus_bus_get_private(DBUS_BUS_SYSTEM, error)))
450 return NULL;
452 dbus_connection_set_exit_on_disconnect(c, FALSE);
453 #else
454 const char *a;
456 if (!(a = getenv("DBUS_SYSTEM_BUS_ADDRESS")) || !*a)
457 a = DBUS_SYSTEM_BUS_DEFAULT_ADDRESS;
459 if (!(c = dbus_connection_open_private(a, error)))
460 return NULL;
462 dbus_connection_set_exit_on_disconnect(c, FALSE);
464 if (!dbus_bus_register(c, error)) {
465 #ifdef HAVE_DBUS_CONNECTION_CLOSE
466 dbus_connection_close(c);
467 #else
468 dbus_connection_disconnect(c);
469 #endif
470 dbus_connection_unref(c);
471 return NULL;
473 #endif
475 return c;
478 AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags, AvahiClientCallback callback, void *userdata, int *ret_error) {
479 AvahiClient *client = NULL;
480 DBusError error;
482 avahi_init_i18n();
484 dbus_error_init(&error);
486 if (!(client = avahi_new(AvahiClient, 1))) {
487 if (ret_error)
488 *ret_error = AVAHI_ERR_NO_MEMORY;
489 goto fail;
492 client->poll_api = poll_api;
493 client->error = AVAHI_OK;
494 client->callback = callback;
495 client->userdata = userdata;
496 client->state = (AvahiClientState) -1;
497 client->flags = flags;
499 client->host_name = NULL;
500 client->host_name_fqdn = NULL;
501 client->domain_name = NULL;
502 client->version_string = NULL;
503 client->local_service_cookie_valid = 0;
505 AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
506 AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
507 AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
508 AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
509 AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers);
510 AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, client->host_name_resolvers);
511 AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, client->address_resolvers);
512 AVAHI_LLIST_HEAD_INIT(AvahiRecordBrowser, client->record_browsers);
514 if (!(client->bus = avahi_dbus_bus_get(&error)) || dbus_error_is_set(&error)) {
515 if (ret_error)
516 *ret_error = AVAHI_ERR_DBUS_ERROR;
517 goto fail;
520 if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
521 if (ret_error)
522 *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
523 goto fail;
526 if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
527 if (ret_error)
528 *ret_error = AVAHI_ERR_NO_MEMORY;
529 goto fail;
532 dbus_bus_add_match(
533 client->bus,
534 "type='signal', "
535 "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
536 "sender='" AVAHI_DBUS_NAME "', "
537 "path='" AVAHI_DBUS_PATH_SERVER "'",
538 &error);
540 if (dbus_error_is_set(&error))
541 goto fail;
543 dbus_bus_add_match (
544 client->bus,
545 "type='signal', "
546 "interface='" DBUS_INTERFACE_DBUS "', "
547 "sender='" DBUS_SERVICE_DBUS "', "
548 "path='" DBUS_PATH_DBUS "'",
549 &error);
551 if (dbus_error_is_set(&error))
552 goto fail;
554 dbus_bus_add_match (
555 client->bus,
556 "type='signal', "
557 "interface='" DBUS_INTERFACE_LOCAL "'",
558 &error);
560 if (dbus_error_is_set(&error))
561 goto fail;
564 if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error)) ||
565 dbus_error_is_set(&error)) {
567 /* We free the error so its not set, that way the fail target
568 * will return the NO_DAEMON error rather than a DBUS error */
569 dbus_error_free(&error);
571 if (!(flags & AVAHI_CLIENT_NO_FAIL)) {
573 if (ret_error)
574 *ret_error = AVAHI_ERR_NO_DAEMON;
576 goto fail;
579 /* The user doesn't want this call to fail if the daemon is not
580 * available, so let's return succesfully */
581 client_set_state(client, AVAHI_CLIENT_CONNECTING);
583 } else {
585 if (init_server(client, ret_error) < 0)
586 goto fail;
589 return client;
591 fail:
593 if (client)
594 avahi_client_free(client);
596 if (dbus_error_is_set(&error)) {
598 if (ret_error) {
599 if (strcmp(error.name, DBUS_ERROR_FILE_NOT_FOUND) == 0)
600 /* DBUS returns this error when the DBUS daemon is not running */
601 *ret_error = AVAHI_ERR_NO_DAEMON;
602 else
603 *ret_error = avahi_error_dbus_to_number(error.name);
606 dbus_error_free(&error);
609 return NULL;
612 void avahi_client_free(AvahiClient *client) {
613 assert(client);
615 if (client->bus)
616 /* Disconnect in advance, so that the free() functions won't
617 * issue needless server calls */
618 #ifdef HAVE_DBUS_CONNECTION_CLOSE
619 dbus_connection_close(client->bus);
620 #else
621 dbus_connection_disconnect(client->bus);
622 #endif
624 while (client->groups)
625 avahi_entry_group_free(client->groups);
627 while (client->domain_browsers)
628 avahi_domain_browser_free(client->domain_browsers);
630 while (client->service_browsers)
631 avahi_service_browser_free(client->service_browsers);
633 while (client->service_type_browsers)
634 avahi_service_type_browser_free(client->service_type_browsers);
636 while (client->service_resolvers)
637 avahi_service_resolver_free(client->service_resolvers);
639 while (client->host_name_resolvers)
640 avahi_host_name_resolver_free(client->host_name_resolvers);
642 while (client->address_resolvers)
643 avahi_address_resolver_free(client->address_resolvers);
645 while (client->record_browsers)
646 avahi_record_browser_free(client->record_browsers);
648 if (client->bus)
649 dbus_connection_unref(client->bus);
651 avahi_free(client->version_string);
652 avahi_free(client->host_name);
653 avahi_free(client->host_name_fqdn);
654 avahi_free(client->domain_name);
656 avahi_free(client);
659 static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) {
660 DBusMessage *message = NULL, *reply = NULL;
661 DBusError error;
662 char *ret, *n;
664 assert(client);
665 assert(method);
667 dbus_error_init (&error);
669 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method))) {
670 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
671 goto fail;
674 if (param) {
675 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID)) {
676 avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
677 goto fail;
681 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
683 if (!reply || dbus_error_is_set (&error))
684 goto fail;
686 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID) ||
687 dbus_error_is_set (&error))
688 goto fail;
690 if (!(n = avahi_strdup(ret))) {
691 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
692 goto fail;
695 dbus_message_unref(message);
696 dbus_message_unref(reply);
698 return n;
700 fail:
702 if (message)
703 dbus_message_unref(message);
704 if (reply)
705 dbus_message_unref(reply);
707 if (dbus_error_is_set(&error)) {
708 avahi_client_set_dbus_error(client, &error);
709 dbus_error_free(&error);
712 return NULL;
715 const char* avahi_client_get_version_string(AvahiClient *client) {
716 assert(client);
718 if (!avahi_client_is_connected(client)) {
719 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
720 return NULL;
723 if (!client->version_string)
724 client->version_string = avahi_client_get_string_reply_and_block(client, "GetVersionString", NULL);
726 return client->version_string;
729 const char* avahi_client_get_domain_name(AvahiClient *client) {
730 assert(client);
732 if (!avahi_client_is_connected(client)) {
733 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
734 return NULL;
737 if (!client->domain_name)
738 client->domain_name = avahi_client_get_string_reply_and_block(client, "GetDomainName", NULL);
740 return client->domain_name;
743 const char* avahi_client_get_host_name(AvahiClient *client) {
744 assert(client);
746 if (!avahi_client_is_connected(client)) {
747 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
748 return NULL;
751 if (!client->host_name)
752 client->host_name = avahi_client_get_string_reply_and_block(client, "GetHostName", NULL);
754 return client->host_name;
757 const char* avahi_client_get_host_name_fqdn (AvahiClient *client) {
758 assert(client);
760 if (!avahi_client_is_connected(client)) {
761 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
762 return NULL;
765 if (!client->host_name_fqdn)
766 client->host_name_fqdn = avahi_client_get_string_reply_and_block(client, "GetHostNameFqdn", NULL);
768 return client->host_name_fqdn;
771 AvahiClientState avahi_client_get_state(AvahiClient *client) {
772 assert(client);
774 return client->state;
777 int avahi_client_errno(AvahiClient *client) {
778 assert(client);
780 return client->error;
783 /* Just for internal use */
784 int avahi_client_simple_method_call(AvahiClient *client, const char *path, const char *interface, const char *method) {
785 DBusMessage *message = NULL, *reply = NULL;
786 DBusError error;
787 int r = AVAHI_OK;
789 dbus_error_init(&error);
791 assert(client);
792 assert(path);
793 assert(interface);
794 assert(method);
796 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, path, interface, method))) {
797 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
798 goto fail;
801 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
802 dbus_error_is_set (&error)) {
803 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
804 goto fail;
807 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
808 dbus_error_is_set (&error)) {
809 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
810 goto fail;
813 dbus_message_unref(message);
814 dbus_message_unref(reply);
816 return AVAHI_OK;
818 fail:
819 if (dbus_error_is_set(&error)) {
820 r = avahi_client_set_dbus_error(client, &error);
821 dbus_error_free(&error);
824 if (message)
825 dbus_message_unref(message);
827 if (reply)
828 dbus_message_unref(reply);
830 return r;
833 uint32_t avahi_client_get_local_service_cookie(AvahiClient *client) {
834 DBusMessage *message = NULL, *reply = NULL;
835 DBusError error;
836 assert(client);
838 if (!avahi_client_is_connected(client)) {
839 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
840 return AVAHI_SERVICE_COOKIE_INVALID;
843 if (client->local_service_cookie_valid)
844 return client->local_service_cookie;
846 dbus_error_init (&error);
848 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetLocalServiceCookie"))) {
849 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
850 goto fail;
853 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
855 if (!reply || dbus_error_is_set (&error))
856 goto fail;
858 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &client->local_service_cookie, DBUS_TYPE_INVALID) ||
859 dbus_error_is_set (&error))
860 goto fail;
862 dbus_message_unref(message);
863 dbus_message_unref(reply);
865 client->local_service_cookie_valid = 1;
866 return client->local_service_cookie;
868 fail:
870 if (message)
871 dbus_message_unref(message);
872 if (reply)
873 dbus_message_unref(reply);
875 if (dbus_error_is_set(&error)) {
876 avahi_client_set_dbus_error(client, &error);
877 dbus_error_free(&error);
880 return AVAHI_SERVICE_COOKIE_INVALID;
883 int avahi_client_is_connected(AvahiClient *client) {
884 assert(client);
886 return
887 client->bus &&
888 dbus_connection_get_is_connected(client->bus) &&
889 (client->state == AVAHI_CLIENT_S_RUNNING || client->state == AVAHI_CLIENT_S_REGISTERING || client->state == AVAHI_CLIENT_S_COLLISION);
892 int avahi_client_set_host_name(AvahiClient* client, const char *name) {
893 DBusMessage *message = NULL, *reply = NULL;
894 DBusError error;
896 assert(client);
898 if (!avahi_client_is_connected(client))
899 return avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
901 dbus_error_init (&error);
903 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "SetHostName"))) {
904 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
905 goto fail;
908 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
909 avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
910 goto fail;
913 reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error);
915 if (!reply || dbus_error_is_set (&error))
916 goto fail;
918 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
919 dbus_error_is_set (&error))
920 goto fail;
922 dbus_message_unref(message);
923 dbus_message_unref(reply);
925 avahi_free(client->host_name);
926 client->host_name = NULL;
927 avahi_free(client->host_name_fqdn);
928 client->host_name_fqdn = NULL;
930 return 0;
932 fail:
934 if (message)
935 dbus_message_unref(message);
936 if (reply)
937 dbus_message_unref(reply);
939 if (dbus_error_is_set(&error)) {
940 avahi_client_set_dbus_error(client, &error);
941 dbus_error_free(&error);
944 return client->error;