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);
177 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "CacheExhausted"))
178 return avahi_domain_browser_event(client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
179 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "AllForNow"))
180 return avahi_domain_browser_event(client, AVAHI_BROWSER_ALL_FOR_NOW, message);
181 else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Failure"))
182 return avahi_domain_browser_event(client, AVAHI_BROWSER_FAILURE, message);
184 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew"))
185 return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message);
186 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove"))
187 return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message);
188 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "CacheExhausted"))
189 return avahi_service_type_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
190 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "AllForNow"))
191 return avahi_service_type_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message);
192 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Failure"))
193 return avahi_service_type_browser_event (client, AVAHI_BROWSER_FAILURE, message);
195 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew"))
196 return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message);
197 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove"))
198 return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message);
199 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "CacheExhausted"))
200 return avahi_service_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message);
201 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "AllForNow"))
202 return avahi_service_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message);
203 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Failure"))
204 return avahi_service_browser_event (client, AVAHI_BROWSER_FAILURE, message);
206 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Found"))
207 return avahi_service_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
208 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Failure"))
209 return avahi_service_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);
211 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Found"))
212 return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
213 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Failure"))
214 return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);
216 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Found"))
217 return avahi_address_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
218 else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Failure"))
219 return avahi_address_resolver_event (client, AVAHI_RESOLVER_FAILURE, message);
221 return DBUS_HANDLER_RESULT_HANDLED;
225 dbus_error_free (&error);
226 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
229 static int get_server_state(AvahiClient *client, int *ret_error) {
230 DBusMessage *message = NULL, *reply = NULL;
233 int e = AVAHI_ERR_NO_MEMORY;
237 dbus_error_init(&error);
239 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState")))
242 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
244 if (!reply || dbus_error_is_set (&error))
247 if (!(dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID)) ||
248 dbus_error_is_set (&error))
251 client_set_state(client, (AvahiServerState) state);
253 dbus_message_unref(message);
254 dbus_message_unref(reply);
259 if (dbus_error_is_set(&error)) {
260 e = avahi_error_dbus_to_number (error.name);
261 dbus_error_free(&error);
268 dbus_message_unref(message);
270 dbus_message_unref(reply);
275 static int check_version(AvahiClient *client, int *ret_error) {
276 DBusMessage *message = NULL, *reply = NULL;
279 int e = AVAHI_ERR_NO_MEMORY;
283 dbus_error_init(&error);
285 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")))
288 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
290 if (!reply || dbus_error_is_set (&error))
293 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &version, DBUS_TYPE_INVALID) ||
294 dbus_error_is_set (&error))
297 if (strcmp(version, PACKAGE_STRING) != 0) {
298 e = AVAHI_ERR_VERSION_MISMATCH;
302 dbus_message_unref(message);
303 dbus_message_unref(reply);
308 if (dbus_error_is_set(&error)) {
309 e = avahi_error_dbus_to_number (error.name);
310 dbus_error_free(&error);
317 dbus_message_unref(message);
319 dbus_message_unref(reply);
325 /* This function acts like dbus_bus_get but creates a private
326 * connection instead */
327 static DBusConnection*
328 avahi_dbus_bus_get (DBusError *error)
330 DBusConnection *conn;
331 const char *env_addr;
333 env_addr = getenv ("DBUS_SYSTEM_BUS_ADDRESS");
335 if (env_addr == NULL || (*env_addr == 0))
337 env_addr = DBUS_SYSTEM_BUS_DEFAULT_ADDRESS;
340 conn = dbus_connection_open_private (env_addr, error);
345 dbus_connection_set_exit_on_disconnect (conn, FALSE);
347 if (!dbus_bus_register (conn, error))
349 dbus_connection_close (conn);
350 dbus_connection_unref (conn);
358 AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *ret_error) {
359 AvahiClient *client = NULL;
362 dbus_error_init (&error);
364 if (!(client = avahi_new(AvahiClient, 1))) {
366 *ret_error = AVAHI_ERR_NO_MEMORY;
370 client->poll_api = poll_api;
371 client->error = AVAHI_OK;
372 client->callback = callback;
373 client->userdata = userdata;
374 client->state = AVAHI_CLIENT_DISCONNECTED;
376 client->host_name = NULL;
377 client->host_name_fqdn = NULL;
378 client->domain_name = NULL;
379 client->version_string = NULL;
380 client->local_service_cookie_valid = 0;
382 AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
383 AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
384 AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
385 AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
386 AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers);
387 AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, client->host_name_resolvers);
388 AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, client->address_resolvers);
390 if (!(client->bus = avahi_dbus_bus_get(&error)) ||
391 dbus_error_is_set (&error))
394 if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
396 *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
400 if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
402 *ret_error = AVAHI_ERR_NO_MEMORY;
409 "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
410 "sender='" AVAHI_DBUS_NAME "', "
411 "path='" AVAHI_DBUS_PATH_SERVER "'",
414 if (dbus_error_is_set (&error))
420 "interface='" DBUS_INTERFACE_DBUS "', "
421 "sender='" DBUS_SERVICE_DBUS "', "
422 "path='" DBUS_PATH_DBUS "'",
425 if (dbus_error_is_set (&error))
431 "interface='" DBUS_INTERFACE_LOCAL "'",
434 if (dbus_error_is_set (&error))
437 if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error)) ||
438 dbus_error_is_set(&error)) {
440 /* We free the error so its not set, that way the fail target
441 * will return the NO_DAEMON error rather than a DBUS error */
442 dbus_error_free (&error);
445 *ret_error = AVAHI_ERR_NO_DAEMON;
450 if (check_version(client, ret_error) < 0)
453 if (get_server_state(client, ret_error) < 0)
461 avahi_client_free(client);
463 if (dbus_error_is_set(&error)) {
466 *ret_error = avahi_error_dbus_to_number(error.name);
468 dbus_error_free(&error);
474 void avahi_client_free(AvahiClient *client) {
477 while (client->groups)
478 avahi_entry_group_free(client->groups);
480 while (client->domain_browsers)
481 avahi_domain_browser_free(client->domain_browsers);
483 while (client->service_browsers)
484 avahi_service_browser_free(client->service_browsers);
486 while (client->service_type_browsers)
487 avahi_service_type_browser_free(client->service_type_browsers);
489 while (client->service_resolvers)
490 avahi_service_resolver_free(client->service_resolvers);
493 dbus_connection_disconnect(client->bus);
494 dbus_connection_unref(client->bus);
497 avahi_free(client->version_string);
498 avahi_free(client->host_name);
499 avahi_free(client->host_name_fqdn);
500 avahi_free(client->domain_name);
505 static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) {
506 DBusMessage *message = NULL, *reply = NULL;
513 dbus_error_init (&error);
515 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method))) {
516 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
521 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID)) {
522 avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
527 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
529 if (!reply || dbus_error_is_set (&error))
532 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID) ||
533 dbus_error_is_set (&error))
536 if (!(n = avahi_strdup(ret))) {
537 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
541 dbus_message_unref(message);
542 dbus_message_unref(reply);
549 dbus_message_unref(message);
551 dbus_message_unref(reply);
553 if (dbus_error_is_set(&error)) {
554 avahi_client_set_dbus_error(client, &error);
555 dbus_error_free(&error);
561 const char* avahi_client_get_version_string (AvahiClient *client) {
564 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
565 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
569 if (!client->version_string)
570 client->version_string = avahi_client_get_string_reply_and_block(client, "GetVersionString", NULL);
572 return client->version_string;
575 const char* avahi_client_get_domain_name (AvahiClient *client) {
578 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
579 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
583 if (!client->domain_name)
584 client->domain_name = avahi_client_get_string_reply_and_block(client, "GetDomainName", NULL);
586 return client->domain_name;
589 const char* avahi_client_get_host_name (AvahiClient *client) {
592 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
593 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
597 if (!client->host_name)
598 client->host_name = avahi_client_get_string_reply_and_block(client, "GetHostName", NULL);
600 return client->host_name;
603 const char* avahi_client_get_host_name_fqdn (AvahiClient *client) {
606 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
607 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
611 if (!client->host_name_fqdn)
612 client->host_name_fqdn = avahi_client_get_string_reply_and_block(client, "GetHostNameFqdn", NULL);
614 return client->host_name_fqdn;
617 AvahiClientState avahi_client_get_state(AvahiClient *client) {
620 return client->state;
623 int avahi_client_errno(AvahiClient *client) {
626 return client->error;
629 /* Just for internal use */
630 int avahi_client_simple_method_call(AvahiClient *client, const char *path, const char *interface, const char *method) {
631 DBusMessage *message = NULL, *reply = NULL;
635 dbus_error_init(&error);
642 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, path, interface, method))) {
643 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
647 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
648 dbus_error_is_set (&error)) {
649 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
653 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
654 dbus_error_is_set (&error)) {
655 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
659 dbus_message_unref(message);
660 dbus_message_unref(reply);
665 if (dbus_error_is_set(&error)) {
666 r = avahi_client_set_dbus_error(client, &error);
667 dbus_error_free(&error);
671 dbus_message_unref(message);
674 dbus_message_unref(reply);
679 uint32_t avahi_client_get_local_service_cookie(AvahiClient *client) {
680 DBusMessage *message = NULL, *reply = NULL;
684 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
685 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
686 return AVAHI_SERVICE_COOKIE_INVALID;
689 if (client->local_service_cookie_valid)
690 return client->local_service_cookie;
692 dbus_error_init (&error);
694 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetLocalServiceCookie"))) {
695 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
699 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
701 if (!reply || dbus_error_is_set (&error))
704 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &client->local_service_cookie, DBUS_TYPE_INVALID) ||
705 dbus_error_is_set (&error))
708 dbus_message_unref(message);
709 dbus_message_unref(reply);
711 client->local_service_cookie_valid = 1;
712 return client->local_service_cookie;
717 dbus_message_unref(message);
719 dbus_message_unref(reply);
721 if (dbus_error_is_set(&error)) {
722 avahi_client_set_dbus_error(client, &error);
723 dbus_error_free(&error);
726 return AVAHI_SERVICE_COOKIE_INVALID;