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 #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) {
49 return client->error = error;
52 int avahi_client_set_dbus_error(AvahiClient *client, DBusError *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) {
62 if (client->state == state)
65 client->state = state;
67 switch (client->state) {
68 case AVAHI_CLIENT_FAILURE:
70 #ifdef HAVE_DBUS_CONNECTION_CLOSE
71 dbus_connection_close(client->bus);
73 dbus_connection_disconnect(client->bus);
75 dbus_connection_unref(client->bus);
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;
94 case AVAHI_CLIENT_S_RUNNING:
95 case AVAHI_CLIENT_CONNECTING:
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;
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);
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(
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);
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);
153 } else if (client->state == AVAHI_CLIENT_CONNECTING && (!old || *old == 0)) {
156 /* Server appeared */
158 if ((ret = init_server(client, NULL)) < 0) {
159 avahi_client_set_errno(client, ret);
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")) {
174 if (!dbus_message_get_args(
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);
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")) {
193 path = dbus_message_get_path(message);
195 for (g = client->groups; g; g = g->groups_next)
196 if (strcmp(g->path, path) == 0)
204 if (!dbus_message_get_args(
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);
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);
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;
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;
309 int e = AVAHI_ERR_NO_MEMORY;
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")))
318 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
320 if (!reply || dbus_error_is_set (&error))
323 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) ||
324 dbus_error_is_set (&error))
327 client_set_state(client, (AvahiServerState) state);
329 dbus_message_unref(message);
330 dbus_message_unref(reply);
335 if (dbus_error_is_set(&error)) {
336 e = avahi_error_dbus_to_number (error.name);
337 dbus_error_free(&error);
344 dbus_message_unref(message);
346 dbus_message_unref(reply);
351 static int check_version(AvahiClient *client, int *ret_error) {
352 DBusMessage *message = NULL, *reply = NULL;
355 int e = AVAHI_ERR_NO_MEMORY;
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")))
364 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
366 if (!reply || dbus_error_is_set (&error)) {
369 if (!dbus_error_is_set(&error) || strcmp(error.name, DBUS_ERROR_UNKNOWN_METHOD))
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")))
383 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
385 if (!reply || dbus_error_is_set (&error))
388 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &version_str, DBUS_TYPE_INVALID) ||
389 dbus_error_is_set (&error))
392 version = strcmp(version_str, "avahi 0.6") == 0 ? 0x0201 : 0x0000;
396 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &version, DBUS_TYPE_INVALID) ||
397 dbus_error_is_set(&error))
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;
409 dbus_message_unref(message);
410 dbus_message_unref(reply);
415 if (dbus_error_is_set(&error)) {
416 e = avahi_error_dbus_to_number (error.name);
417 dbus_error_free(&error);
424 dbus_message_unref(message);
426 dbus_message_unref(reply);
431 static int init_server(AvahiClient *client, int *ret_error) {
434 if ((r = check_version(client, ret_error)) < 0)
437 if ((r = get_server_state(client, ret_error)) < 0)
443 /* This function acts like dbus_bus_get but creates a private
444 * connection instead. */
445 static DBusConnection* avahi_dbus_bus_get(DBusError *error) {
448 #ifdef HAVE_DBUS_BUS_GET_PRIVATE
449 if (!(c = dbus_bus_get_private(DBUS_BUS_SYSTEM, error)))
452 dbus_connection_set_exit_on_disconnect(c, FALSE);
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)))
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);
468 dbus_connection_disconnect(c);
470 dbus_connection_unref(c);
478 AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags, AvahiClientCallback callback, void *userdata, int *ret_error) {
479 AvahiClient *client = NULL;
484 dbus_error_init(&error);
486 if (!(client = avahi_new(AvahiClient, 1))) {
488 *ret_error = AVAHI_ERR_NO_MEMORY;
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)) {
516 *ret_error = AVAHI_ERR_DBUS_ERROR;
520 if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
522 *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
526 if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
528 *ret_error = AVAHI_ERR_NO_MEMORY;
535 "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
536 "sender='" AVAHI_DBUS_NAME "', "
537 "path='" AVAHI_DBUS_PATH_SERVER "'",
540 if (dbus_error_is_set(&error))
546 "interface='" DBUS_INTERFACE_DBUS "', "
547 "sender='" DBUS_SERVICE_DBUS "', "
548 "path='" DBUS_PATH_DBUS "'",
551 if (dbus_error_is_set(&error))
557 "interface='" DBUS_INTERFACE_LOCAL "'",
560 if (dbus_error_is_set(&error))
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)) {
574 *ret_error = AVAHI_ERR_NO_DAEMON;
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);
585 if (init_server(client, ret_error) < 0)
594 avahi_client_free(client);
596 if (dbus_error_is_set(&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;
603 *ret_error = avahi_error_dbus_to_number(error.name);
606 dbus_error_free(&error);
612 void avahi_client_free(AvahiClient *client) {
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);
621 dbus_connection_disconnect(client->bus);
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);
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);
659 static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) {
660 DBusMessage *message = NULL, *reply = NULL;
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);
675 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID)) {
676 avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
681 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
683 if (!reply || dbus_error_is_set (&error))
686 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID) ||
687 dbus_error_is_set (&error))
690 if (!(n = avahi_strdup(ret))) {
691 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
695 dbus_message_unref(message);
696 dbus_message_unref(reply);
703 dbus_message_unref(message);
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);
715 const char* avahi_client_get_version_string(AvahiClient *client) {
718 if (!avahi_client_is_connected(client)) {
719 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
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) {
732 if (!avahi_client_is_connected(client)) {
733 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
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) {
746 if (!avahi_client_is_connected(client)) {
747 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
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) {
760 if (!avahi_client_is_connected(client)) {
761 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
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) {
774 return client->state;
777 int avahi_client_errno(AvahiClient *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;
789 dbus_error_init(&error);
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);
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);
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);
813 dbus_message_unref(message);
814 dbus_message_unref(reply);
819 if (dbus_error_is_set(&error)) {
820 r = avahi_client_set_dbus_error(client, &error);
821 dbus_error_free(&error);
825 dbus_message_unref(message);
828 dbus_message_unref(reply);
833 uint32_t avahi_client_get_local_service_cookie(AvahiClient *client) {
834 DBusMessage *message = NULL, *reply = NULL;
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);
853 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
855 if (!reply || dbus_error_is_set (&error))
858 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &client->local_service_cookie, DBUS_TYPE_INVALID) ||
859 dbus_error_is_set (&error))
862 dbus_message_unref(message);
863 dbus_message_unref(reply);
865 client->local_service_cookie_valid = 1;
866 return client->local_service_cookie;
871 dbus_message_unref(message);
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) {
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;
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);
908 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
909 avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
913 reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error);
915 if (!reply || dbus_error_is_set (&error))
918 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
919 dbus_error_is_set (&error))
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;
935 dbus_message_unref(message);
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;