2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <dbus/dbus.h>
30 #include <avahi-common/dbus.h>
31 #include <avahi-common/llist.h>
32 #include <avahi-common/error.h>
33 #include <avahi-common/dbus.h>
34 #include <avahi-common/malloc.h>
35 #include <avahi-common/dbus-watch-glue.h>
36 #include <avahi-common/i18n.h>
41 #define AVAHI_CLIENT_DBUS_API_SUPPORTED ((uint32_t) 0x0201)
43 static int init_server(AvahiClient *client, int *ret_error);
45 int avahi_client_set_errno (AvahiClient *client, int error) {
48 return client->error = error;
51 int avahi_client_set_dbus_error(AvahiClient *client, DBusError *error) {
55 return avahi_client_set_errno(client, avahi_error_dbus_to_number(error->name));
58 static void client_set_state (AvahiClient *client, AvahiServerState state) {
61 if (client->state == state)
64 client->state = state;
66 switch (client->state) {
67 case AVAHI_CLIENT_FAILURE:
69 #ifdef HAVE_DBUS_CONNECTION_CLOSE
70 dbus_connection_close(client->bus);
72 dbus_connection_disconnect(client->bus);
74 dbus_connection_unref(client->bus);
80 case AVAHI_CLIENT_S_COLLISION:
81 case AVAHI_CLIENT_S_REGISTERING:
83 /* Clear cached strings */
84 avahi_free(client->host_name);
85 avahi_free(client->host_name_fqdn);
86 avahi_free(client->domain_name);
88 client->host_name = NULL;
89 client->host_name_fqdn = NULL;
90 client->domain_name = NULL;
93 case AVAHI_CLIENT_S_RUNNING:
94 case AVAHI_CLIENT_CONNECTING:
100 client->callback (client, state, client->userdata);
103 static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, void *userdata) {
104 AvahiClient *client = userdata;
110 dbus_error_init(&error);
112 /* fprintf(stderr, "dbus: interface=%s, path=%s, member=%s\n", */
113 /* dbus_message_get_interface (message), */
114 /* dbus_message_get_path (message), */
115 /* dbus_message_get_member (message)); */
117 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
119 /* The DBUS server died or kicked us */
120 avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED);
123 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameAcquired")) {
125 /* Ignore this message */
127 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
128 char *name, *old, *new;
130 if (!dbus_message_get_args(
132 DBUS_TYPE_STRING, &name,
133 DBUS_TYPE_STRING, &old,
134 DBUS_TYPE_STRING, &new,
135 DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
137 fprintf(stderr, "WARNING: Failed to parse NameOwnerChanged signal: %s\n", error.message);
138 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
142 if (strcmp(name, AVAHI_DBUS_NAME) == 0) {
144 if (avahi_client_is_connected(client)) {
146 /* Regardless if the server lost or acquired its name or
147 * if the name was transfered: our services are no longer
148 * available, so we disconnect ourselves */
149 avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED);
152 } else if (client->state == AVAHI_CLIENT_CONNECTING && (!old || *old == 0)) {
155 /* Server appeared */
157 if ((ret = init_server(client, NULL)) < 0) {
158 avahi_client_set_errno(client, ret);
164 } else if (!avahi_client_is_connected(client)) {
166 /* Ignore messages we get in unconnected state */
168 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged")) {
173 if (!dbus_message_get_args(
175 DBUS_TYPE_INT32, &state,
176 DBUS_TYPE_STRING, &e,
177 DBUS_TYPE_INVALID) || dbus_error_is_set (&error)) {
179 fprintf(stderr, "WARNING: Failed to parse Server.StateChanged signal: %s\n", error.message);
180 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
184 if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK)
185 avahi_client_set_errno(client, c);
187 client_set_state(client, (AvahiClientState) state);
189 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
192 path = dbus_message_get_path(message);
194 for (g = client->groups; g; g = g->groups_next)
195 if (strcmp(g->path, path) == 0)
203 if (!dbus_message_get_args(
205 DBUS_TYPE_INT32, &state,
206 DBUS_TYPE_STRING, &e,
207 DBUS_TYPE_INVALID) ||
208 dbus_error_is_set(&error)) {
210 fprintf(stderr, "WARNING: Failed to parse EntryGroup.StateChanged signal: %s\n", error.message);
211 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
215 if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK)
216 avahi_client_set_errno(client, c);
218 avahi_entry_group_set_state(g, state);
221 } else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemNew"))
222 return avahi_domain_browser_event(client, AVAHI_BROWSER_NEW, message);
223 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemRemove"))
224 return avahi_domain_browser_event(client, AVAHI_BROWSER_REMOVE, message);
225 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "CacheExhausted"))
226 return avahi_domain_browser_event(client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
227 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "AllForNow"))
228 return avahi_domain_browser_event(client, AVAHI_BROWSER_ALL_FOR_NOW, message);
229 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Failure"))
230 return avahi_domain_browser_event(client, AVAHI_BROWSER_FAILURE, message);
232 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew"))
233 return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message);
234 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove"))
235 return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message);
236 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "CacheExhausted"))
237 return avahi_service_type_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
238 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "AllForNow"))
239 return avahi_service_type_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message);
240 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Failure"))
241 return avahi_service_type_browser_event (client, AVAHI_BROWSER_FAILURE, message);
243 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew"))
244 return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message);
245 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove"))
246 return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message);
247 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "CacheExhausted"))
248 return avahi_service_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
249 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "AllForNow"))
250 return avahi_service_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message);
251 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Failure"))
252 return avahi_service_browser_event (client, AVAHI_BROWSER_FAILURE, message);
254 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Found"))
255 return avahi_service_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
256 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Failure"))
257 return avahi_service_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);
259 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Found"))
260 return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
261 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Failure"))
262 return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);
264 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Found"))
265 return avahi_address_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
266 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Failure"))
267 return avahi_address_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);
269 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "ItemNew"))
270 return avahi_record_browser_event (client, AVAHI_BROWSER_NEW, message);
271 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "ItemRemove"))
272 return avahi_record_browser_event (client, AVAHI_BROWSER_REMOVE, message);
273 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "CacheExhausted"))
274 return avahi_record_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
275 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "AllForNow"))
276 return avahi_record_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message);
277 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "Failure"))
278 return avahi_record_browser_event (client, AVAHI_BROWSER_FAILURE, message);
282 fprintf(stderr, "WARNING: Unhandled message: interface=%s, path=%s, member=%s\n",
283 dbus_message_get_interface(message),
284 dbus_message_get_path(message),
285 dbus_message_get_member(message));
287 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
290 return DBUS_HANDLER_RESULT_HANDLED;
294 if (dbus_error_is_set(&error)) {
295 avahi_client_set_errno(client, avahi_error_dbus_to_number(error.name));
296 dbus_error_free(&error);
299 client_set_state(client, AVAHI_CLIENT_FAILURE);
301 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
304 static int get_server_state(AvahiClient *client, int *ret_error) {
305 DBusMessage *message = NULL, *reply = NULL;
308 int e = AVAHI_ERR_NO_MEMORY;
312 dbus_error_init(&error);
314 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState")))
317 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
319 if (!reply || dbus_error_is_set (&error))
322 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) ||
323 dbus_error_is_set (&error))
326 client_set_state(client, (AvahiServerState) state);
328 dbus_message_unref(message);
329 dbus_message_unref(reply);
334 if (dbus_error_is_set(&error)) {
335 e = avahi_error_dbus_to_number (error.name);
336 dbus_error_free(&error);
343 dbus_message_unref(message);
345 dbus_message_unref(reply);
350 static int check_version(AvahiClient *client, int *ret_error) {
351 DBusMessage *message = NULL, *reply = NULL;
354 int e = AVAHI_ERR_NO_MEMORY;
358 dbus_error_init(&error);
360 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetAPIVersion")))
363 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
365 if (!reply || dbus_error_is_set (&error)) {
368 if (!dbus_error_is_set(&error) || strcmp(error.name, DBUS_ERROR_UNKNOWN_METHOD))
371 /* If the method GetAPIVersion is not known, we look if
372 * GetVersionString matches "avahi 0.6" which is the only
373 * version we support which doesn't have GetAPIVersion() .*/
375 dbus_message_unref(message);
376 if (reply) dbus_message_unref(reply);
377 dbus_error_free(&error);
379 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")))
382 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
384 if (!reply || dbus_error_is_set (&error))
387 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &version_str, DBUS_TYPE_INVALID) ||
388 dbus_error_is_set (&error))
391 version = strcmp(version_str, "avahi 0.6") == 0 ? 0x0201 : 0x0000;
395 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &version, DBUS_TYPE_INVALID) ||
396 dbus_error_is_set(&error))
400 /*fprintf(stderr, "API Version 0x%04x\n", version);*/
402 if ((version & 0xFF00) != (AVAHI_CLIENT_DBUS_API_SUPPORTED & 0xFF00) ||
403 (version & 0x00FF) < (AVAHI_CLIENT_DBUS_API_SUPPORTED & 0x00FF)) {
404 e = AVAHI_ERR_VERSION_MISMATCH;
408 dbus_message_unref(message);
409 dbus_message_unref(reply);
414 if (dbus_error_is_set(&error)) {
415 e = avahi_error_dbus_to_number (error.name);
416 dbus_error_free(&error);
423 dbus_message_unref(message);
425 dbus_message_unref(reply);
430 static int init_server(AvahiClient *client, int *ret_error) {
433 if ((r = check_version(client, ret_error)) < 0)
436 if ((r = get_server_state(client, ret_error)) < 0)
442 /* This function acts like dbus_bus_get but creates a private
443 * connection instead. */
444 static DBusConnection* avahi_dbus_bus_get(DBusError *error) {
447 #ifdef HAVE_DBUS_BUS_GET_PRIVATE
448 if (!(c = dbus_bus_get_private(DBUS_BUS_SYSTEM, error)))
451 dbus_connection_set_exit_on_disconnect(c, FALSE);
455 if (!(a = getenv("DBUS_SYSTEM_BUS_ADDRESS")) || !*a)
456 a = DBUS_SYSTEM_BUS_DEFAULT_ADDRESS;
458 if (!(c = dbus_connection_open_private(a, error)))
461 dbus_connection_set_exit_on_disconnect(c, FALSE);
463 if (!dbus_bus_register(c, error)) {
464 #ifdef HAVE_DBUS_CONNECTION_CLOSE
465 dbus_connection_close(c);
467 dbus_connection_disconnect(c);
469 dbus_connection_unref(c);
477 AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags, AvahiClientCallback callback, void *userdata, int *ret_error) {
478 AvahiClient *client = NULL;
483 dbus_error_init(&error);
485 if (!(client = avahi_new(AvahiClient, 1))) {
487 *ret_error = AVAHI_ERR_NO_MEMORY;
491 client->poll_api = poll_api;
492 client->error = AVAHI_OK;
493 client->callback = callback;
494 client->userdata = userdata;
495 client->state = (AvahiClientState) -1;
496 client->flags = flags;
498 client->host_name = NULL;
499 client->host_name_fqdn = NULL;
500 client->domain_name = NULL;
501 client->version_string = NULL;
502 client->local_service_cookie_valid = 0;
504 AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
505 AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
506 AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
507 AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
508 AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers);
509 AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, client->host_name_resolvers);
510 AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, client->address_resolvers);
511 AVAHI_LLIST_HEAD_INIT(AvahiRecordBrowser, client->record_browsers);
513 if (!(client->bus = avahi_dbus_bus_get(&error)) || dbus_error_is_set(&error)) {
515 *ret_error = AVAHI_ERR_DBUS_ERROR;
519 if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
521 *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
525 if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
527 *ret_error = AVAHI_ERR_NO_MEMORY;
534 "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
535 "sender='" AVAHI_DBUS_NAME "', "
536 "path='" AVAHI_DBUS_PATH_SERVER "'",
539 if (dbus_error_is_set(&error))
545 "interface='" DBUS_INTERFACE_DBUS "', "
546 "sender='" DBUS_SERVICE_DBUS "', "
547 "path='" DBUS_PATH_DBUS "'",
550 if (dbus_error_is_set(&error))
556 "interface='" DBUS_INTERFACE_LOCAL "'",
559 if (dbus_error_is_set(&error))
563 if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error)) ||
564 dbus_error_is_set(&error)) {
566 /* We free the error so its not set, that way the fail target
567 * will return the NO_DAEMON error rather than a DBUS error */
568 dbus_error_free(&error);
570 if (!(flags & AVAHI_CLIENT_NO_FAIL)) {
573 *ret_error = AVAHI_ERR_NO_DAEMON;
578 /* The user doesn't want this call to fail if the daemon is not
579 * available, so let's return succesfully */
580 client_set_state(client, AVAHI_CLIENT_CONNECTING);
584 if (init_server(client, ret_error) < 0)
593 avahi_client_free(client);
595 if (dbus_error_is_set(&error)) {
598 if (strcmp(error.name, DBUS_ERROR_FILE_NOT_FOUND) == 0)
599 /* DBUS returns this error when the DBUS daemon is not running */
600 *ret_error = AVAHI_ERR_NO_DAEMON;
602 *ret_error = avahi_error_dbus_to_number(error.name);
605 dbus_error_free(&error);
611 void avahi_client_free(AvahiClient *client) {
615 /* Disconnect in advance, so that the free() functions won't
616 * issue needless server calls */
617 #ifdef HAVE_DBUS_CONNECTION_CLOSE
618 dbus_connection_close(client->bus);
620 dbus_connection_disconnect(client->bus);
623 while (client->groups)
624 avahi_entry_group_free(client->groups);
626 while (client->domain_browsers)
627 avahi_domain_browser_free(client->domain_browsers);
629 while (client->service_browsers)
630 avahi_service_browser_free(client->service_browsers);
632 while (client->service_type_browsers)
633 avahi_service_type_browser_free(client->service_type_browsers);
635 while (client->service_resolvers)
636 avahi_service_resolver_free(client->service_resolvers);
638 while (client->host_name_resolvers)
639 avahi_host_name_resolver_free(client->host_name_resolvers);
641 while (client->address_resolvers)
642 avahi_address_resolver_free(client->address_resolvers);
644 while (client->record_browsers)
645 avahi_record_browser_free(client->record_browsers);
648 dbus_connection_unref(client->bus);
650 avahi_free(client->version_string);
651 avahi_free(client->host_name);
652 avahi_free(client->host_name_fqdn);
653 avahi_free(client->domain_name);
658 static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) {
659 DBusMessage *message = NULL, *reply = NULL;
666 dbus_error_init (&error);
668 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method))) {
669 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
674 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID)) {
675 avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
680 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
682 if (!reply || dbus_error_is_set (&error))
685 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID) ||
686 dbus_error_is_set (&error))
689 if (!(n = avahi_strdup(ret))) {
690 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
694 dbus_message_unref(message);
695 dbus_message_unref(reply);
702 dbus_message_unref(message);
704 dbus_message_unref(reply);
706 if (dbus_error_is_set(&error)) {
707 avahi_client_set_dbus_error(client, &error);
708 dbus_error_free(&error);
714 const char* avahi_client_get_version_string(AvahiClient *client) {
717 if (!avahi_client_is_connected(client)) {
718 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
722 if (!client->version_string)
723 client->version_string = avahi_client_get_string_reply_and_block(client, "GetVersionString", NULL);
725 return client->version_string;
728 const char* avahi_client_get_domain_name(AvahiClient *client) {
731 if (!avahi_client_is_connected(client)) {
732 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
736 if (!client->domain_name)
737 client->domain_name = avahi_client_get_string_reply_and_block(client, "GetDomainName", NULL);
739 return client->domain_name;
742 const char* avahi_client_get_host_name(AvahiClient *client) {
745 if (!avahi_client_is_connected(client)) {
746 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
750 if (!client->host_name)
751 client->host_name = avahi_client_get_string_reply_and_block(client, "GetHostName", NULL);
753 return client->host_name;
756 const char* avahi_client_get_host_name_fqdn (AvahiClient *client) {
759 if (!avahi_client_is_connected(client)) {
760 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
764 if (!client->host_name_fqdn)
765 client->host_name_fqdn = avahi_client_get_string_reply_and_block(client, "GetHostNameFqdn", NULL);
767 return client->host_name_fqdn;
770 AvahiClientState avahi_client_get_state(AvahiClient *client) {
773 return client->state;
776 int avahi_client_errno(AvahiClient *client) {
779 return client->error;
782 /* Just for internal use */
783 int avahi_client_simple_method_call(AvahiClient *client, const char *path, const char *interface, const char *method) {
784 DBusMessage *message = NULL, *reply = NULL;
788 dbus_error_init(&error);
795 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, path, interface, method))) {
796 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
800 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
801 dbus_error_is_set (&error)) {
802 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
806 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
807 dbus_error_is_set (&error)) {
808 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
812 dbus_message_unref(message);
813 dbus_message_unref(reply);
818 if (dbus_error_is_set(&error)) {
819 r = avahi_client_set_dbus_error(client, &error);
820 dbus_error_free(&error);
824 dbus_message_unref(message);
827 dbus_message_unref(reply);
832 uint32_t avahi_client_get_local_service_cookie(AvahiClient *client) {
833 DBusMessage *message = NULL, *reply = NULL;
837 if (!avahi_client_is_connected(client)) {
838 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
839 return AVAHI_SERVICE_COOKIE_INVALID;
842 if (client->local_service_cookie_valid)
843 return client->local_service_cookie;
845 dbus_error_init (&error);
847 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetLocalServiceCookie"))) {
848 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
852 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
854 if (!reply || dbus_error_is_set (&error))
857 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &client->local_service_cookie, DBUS_TYPE_INVALID) ||
858 dbus_error_is_set (&error))
861 dbus_message_unref(message);
862 dbus_message_unref(reply);
864 client->local_service_cookie_valid = 1;
865 return client->local_service_cookie;
870 dbus_message_unref(message);
872 dbus_message_unref(reply);
874 if (dbus_error_is_set(&error)) {
875 avahi_client_set_dbus_error(client, &error);
876 dbus_error_free(&error);
879 return AVAHI_SERVICE_COOKIE_INVALID;
882 int avahi_client_is_connected(AvahiClient *client) {
887 dbus_connection_get_is_connected(client->bus) &&
888 (client->state == AVAHI_CLIENT_S_RUNNING || client->state == AVAHI_CLIENT_S_REGISTERING || client->state == AVAHI_CLIENT_S_COLLISION);
891 int avahi_client_set_host_name(AvahiClient* client, const char *name) {
892 DBusMessage *message = NULL, *reply = NULL;
897 if (!avahi_client_is_connected(client))
898 return avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
900 dbus_error_init (&error);
902 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "SetHostName"))) {
903 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
907 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
908 avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
912 reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error);
914 if (!reply || dbus_error_is_set (&error))
917 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
918 dbus_error_is_set (&error))
921 dbus_message_unref(message);
922 dbus_message_unref(reply);
924 avahi_free(client->host_name);
925 client->host_name = NULL;
926 avahi_free(client->host_name_fqdn);
927 client->host_name_fqdn = NULL;
934 dbus_message_unref(message);
936 dbus_message_unref(reply);
938 if (dbus_error_is_set(&error)) {
939 avahi_client_set_dbus_error(client, &error);
940 dbus_error_free(&error);
943 return client->error;