]> git.meshlink.io Git - catta/blob - avahi-client/client.c
daemon: add some minimal OOM checking against D-Bus
[catta] / avahi-client / client.c
1 /***
2   This file is part of avahi.
3
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.
8
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.
13
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
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <dbus/dbus.h>
29
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>
37
38 #include "client.h"
39 #include "internal.h"
40
41 #define AVAHI_CLIENT_DBUS_API_SUPPORTED ((uint32_t) 0x0201)
42
43 static int init_server(AvahiClient *client, int *ret_error);
44
45 int avahi_client_set_errno (AvahiClient *client, int error) {
46     assert(client);
47
48     return client->error = error;
49 }
50
51 int avahi_client_set_dbus_error(AvahiClient *client, DBusError *error) {
52     assert(client);
53     assert(error);
54
55     return avahi_client_set_errno(client, avahi_error_dbus_to_number(error->name));
56 }
57
58 static void client_set_state (AvahiClient *client, AvahiServerState state) {
59     assert(client);
60
61     if (client->state == state)
62         return;
63
64     client->state = state;
65
66     switch (client->state) {
67         case AVAHI_CLIENT_FAILURE:
68             if (client->bus) {
69 #ifdef HAVE_DBUS_CONNECTION_CLOSE
70                 dbus_connection_close(client->bus);
71 #else
72                 dbus_connection_disconnect(client->bus);
73 #endif
74                 dbus_connection_unref(client->bus);
75                 client->bus = NULL;
76             }
77
78             /* Fall through */
79
80         case AVAHI_CLIENT_S_COLLISION:
81         case AVAHI_CLIENT_S_REGISTERING:
82
83             /* Clear cached strings */
84             avahi_free(client->host_name);
85             avahi_free(client->host_name_fqdn);
86             avahi_free(client->domain_name);
87
88             client->host_name =  NULL;
89             client->host_name_fqdn = NULL;
90             client->domain_name = NULL;
91             break;
92
93         case AVAHI_CLIENT_S_RUNNING:
94         case AVAHI_CLIENT_CONNECTING:
95             break;
96
97     }
98
99     if (client->callback)
100         client->callback (client, state, client->userdata);
101 }
102
103 static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, void *userdata) {
104     AvahiClient *client = userdata;
105     DBusError error;
106
107     assert(bus);
108     assert(message);
109
110     dbus_error_init(&error);
111
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)); */
116
117     if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
118
119         /* The DBUS server died or kicked us */
120         avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED);
121         goto fail;
122
123     } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameAcquired")) {
124
125         /* Ignore this message */
126
127     } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
128         char *name, *old, *new;
129
130         if (!dbus_message_get_args(
131                   message, &error,
132                   DBUS_TYPE_STRING, &name,
133                   DBUS_TYPE_STRING, &old,
134                   DBUS_TYPE_STRING, &new,
135                   DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
136
137             fprintf(stderr, "WARNING: Failed to parse NameOwnerChanged signal: %s\n", error.message);
138             avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
139             goto fail;
140         }
141
142         if (strcmp(name, AVAHI_DBUS_NAME) == 0) {
143
144             if (avahi_client_is_connected(client)) {
145
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);
150                 goto fail;
151
152             } else if (client->state == AVAHI_CLIENT_CONNECTING && (!old || *old == 0)) {
153                 int ret;
154
155                 /* Server appeared */
156
157                 if ((ret = init_server(client, NULL)) < 0) {
158                     avahi_client_set_errno(client, ret);
159                     goto fail;
160                 }
161             }
162         }
163
164     } else if (!avahi_client_is_connected(client)) {
165
166         /* Ignore messages we get in unconnected state */
167
168     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged")) {
169         int32_t state;
170         char *e = NULL;
171         int c;
172
173         if (!dbus_message_get_args(
174                   message, &error,
175                   DBUS_TYPE_INT32, &state,
176                   DBUS_TYPE_STRING, &e,
177                   DBUS_TYPE_INVALID) || dbus_error_is_set (&error)) {
178
179             fprintf(stderr, "WARNING: Failed to parse Server.StateChanged signal: %s\n", error.message);
180             avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
181             goto fail;
182         }
183
184         if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK)
185             avahi_client_set_errno(client, c);
186
187         client_set_state(client, (AvahiClientState) state);
188
189     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
190         const char *path;
191         AvahiEntryGroup *g;
192         path = dbus_message_get_path(message);
193
194         for (g = client->groups; g; g = g->groups_next)
195             if (strcmp(g->path, path) == 0)
196                 break;
197
198         if (g) {
199             int32_t state;
200             char *e;
201             int c;
202
203             if (!dbus_message_get_args(
204                       message, &error,
205                       DBUS_TYPE_INT32, &state,
206                       DBUS_TYPE_STRING, &e,
207                       DBUS_TYPE_INVALID) ||
208                 dbus_error_is_set(&error)) {
209
210                 fprintf(stderr, "WARNING: Failed to parse EntryGroup.StateChanged signal: %s\n", error.message);
211                 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
212                 goto fail;
213             }
214
215             if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK)
216                 avahi_client_set_errno(client, c);
217
218             avahi_entry_group_set_state(g, state);
219         }
220
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);
231
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);
242
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);
253
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);
258
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);
263
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);
268
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);
279
280     else {
281
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));
286
287         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
288     }
289
290     return DBUS_HANDLER_RESULT_HANDLED;
291
292 fail:
293
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);
297     }
298
299     client_set_state(client, AVAHI_CLIENT_FAILURE);
300
301     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
302 }
303
304 static int get_server_state(AvahiClient *client, int *ret_error) {
305     DBusMessage *message = NULL, *reply = NULL;
306     DBusError error;
307     int32_t state;
308     int e = AVAHI_ERR_NO_MEMORY;
309
310     assert(client);
311
312     dbus_error_init(&error);
313
314     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState")))
315         goto fail;
316
317     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
318
319     if (!reply || dbus_error_is_set (&error))
320         goto fail;
321
322     if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) ||
323         dbus_error_is_set (&error))
324         goto fail;
325
326     client_set_state(client, (AvahiServerState) state);
327
328     dbus_message_unref(message);
329     dbus_message_unref(reply);
330
331     return AVAHI_OK;
332
333 fail:
334     if (dbus_error_is_set(&error)) {
335         e = avahi_error_dbus_to_number (error.name);
336         dbus_error_free(&error);
337     }
338
339     if (ret_error)
340         *ret_error = e;
341
342     if (message)
343         dbus_message_unref(message);
344     if (reply)
345         dbus_message_unref(reply);
346
347     return e;
348 }
349
350 static int check_version(AvahiClient *client, int *ret_error) {
351     DBusMessage *message = NULL, *reply  = NULL;
352     DBusError error;
353     uint32_t version;
354     int e = AVAHI_ERR_NO_MEMORY;
355
356     assert(client);
357
358     dbus_error_init(&error);
359
360     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetAPIVersion")))
361         goto fail;
362
363     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
364
365     if (!reply || dbus_error_is_set (&error)) {
366         char *version_str;
367
368         if (!dbus_error_is_set(&error) || strcmp(error.name, DBUS_ERROR_UNKNOWN_METHOD))
369             goto fail;
370
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() .*/
374
375         dbus_message_unref(message);
376         if (reply) dbus_message_unref(reply);
377         dbus_error_free(&error);
378
379         if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")))
380             goto fail;
381
382         reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
383
384         if (!reply || dbus_error_is_set (&error))
385             goto fail;
386
387         if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &version_str, DBUS_TYPE_INVALID) ||
388             dbus_error_is_set (&error))
389             goto fail;
390
391         version = strcmp(version_str, "avahi 0.6") == 0 ? 0x0201 : 0x0000;
392
393     } else {
394
395         if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &version, DBUS_TYPE_INVALID) ||
396             dbus_error_is_set(&error))
397             goto fail;
398     }
399
400     /*fprintf(stderr, "API Version 0x%04x\n", version);*/
401
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;
405         goto fail;
406     }
407
408     dbus_message_unref(message);
409     dbus_message_unref(reply);
410
411     return AVAHI_OK;
412
413 fail:
414     if (dbus_error_is_set(&error)) {
415         e = avahi_error_dbus_to_number (error.name);
416         dbus_error_free(&error);
417     }
418
419     if (ret_error)
420         *ret_error = e;
421
422     if (message)
423         dbus_message_unref(message);
424     if (reply)
425         dbus_message_unref(reply);
426
427     return e;
428 }
429
430 static int init_server(AvahiClient *client, int *ret_error) {
431     int r;
432
433     if ((r = check_version(client, ret_error)) < 0)
434         return r;
435
436     if ((r = get_server_state(client, ret_error)) < 0)
437         return r;
438
439     return AVAHI_OK;
440 }
441
442 /* This function acts like dbus_bus_get but creates a private
443  * connection instead.  */
444 static DBusConnection* avahi_dbus_bus_get(DBusError *error) {
445     DBusConnection *c;
446
447 #ifdef HAVE_DBUS_BUS_GET_PRIVATE
448     if (!(c = dbus_bus_get_private(DBUS_BUS_SYSTEM, error)))
449         return NULL;
450
451     dbus_connection_set_exit_on_disconnect(c, FALSE);
452 #else
453     const char *a;
454
455     if (!(a = getenv("DBUS_SYSTEM_BUS_ADDRESS")) || !*a)
456         a = DBUS_SYSTEM_BUS_DEFAULT_ADDRESS;
457
458     if (!(c = dbus_connection_open_private(a, error)))
459         return NULL;
460
461     dbus_connection_set_exit_on_disconnect(c, FALSE);
462
463     if (!dbus_bus_register(c, error)) {
464 #ifdef HAVE_DBUS_CONNECTION_CLOSE
465         dbus_connection_close(c);
466 #else
467         dbus_connection_disconnect(c);
468 #endif
469         dbus_connection_unref(c);
470         return NULL;
471     }
472 #endif
473
474     return c;
475 }
476
477 AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags, AvahiClientCallback callback, void *userdata, int *ret_error) {
478     AvahiClient *client = NULL;
479     DBusError error;
480
481     avahi_init_i18n();
482
483     dbus_error_init(&error);
484
485     if (!(client = avahi_new(AvahiClient, 1))) {
486         if (ret_error)
487             *ret_error = AVAHI_ERR_NO_MEMORY;
488         goto fail;
489     }
490
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;
497
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;
503
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);
512
513     if (!(client->bus = avahi_dbus_bus_get(&error)) || dbus_error_is_set(&error)) {
514         if (ret_error)
515             *ret_error = AVAHI_ERR_DBUS_ERROR;
516         goto fail;
517     }
518
519     if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
520         if (ret_error)
521             *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
522         goto fail;
523     }
524
525     if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
526         if (ret_error)
527             *ret_error = AVAHI_ERR_NO_MEMORY;
528         goto fail;
529     }
530
531     dbus_bus_add_match(
532         client->bus,
533         "type='signal', "
534         "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
535         "sender='" AVAHI_DBUS_NAME "', "
536         "path='" AVAHI_DBUS_PATH_SERVER "'",
537         &error);
538
539     if (dbus_error_is_set(&error))
540         goto fail;
541
542     dbus_bus_add_match (
543         client->bus,
544         "type='signal', "
545         "interface='" DBUS_INTERFACE_DBUS "', "
546         "sender='" DBUS_SERVICE_DBUS "', "
547         "path='" DBUS_PATH_DBUS "'",
548         &error);
549
550     if (dbus_error_is_set(&error))
551         goto fail;
552
553     dbus_bus_add_match (
554         client->bus,
555         "type='signal', "
556         "interface='" DBUS_INTERFACE_LOCAL "'",
557         &error);
558
559     if (dbus_error_is_set(&error))
560         goto fail;
561
562
563     if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error)) ||
564         dbus_error_is_set(&error)) {
565
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);
569
570         if (!(flags & AVAHI_CLIENT_NO_FAIL)) {
571
572             if (ret_error)
573                 *ret_error = AVAHI_ERR_NO_DAEMON;
574
575             goto fail;
576         }
577
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);
581
582     } else {
583
584         if (init_server(client, ret_error) < 0)
585             goto fail;
586     }
587
588     return client;
589
590 fail:
591
592     if (client)
593         avahi_client_free(client);
594
595     if (dbus_error_is_set(&error)) {
596
597         if (ret_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;
601             else
602                 *ret_error = avahi_error_dbus_to_number(error.name);
603         }
604
605         dbus_error_free(&error);
606     }
607
608     return NULL;
609 }
610
611 void avahi_client_free(AvahiClient *client) {
612     assert(client);
613
614     if (client->bus)
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);
619 #else
620         dbus_connection_disconnect(client->bus);
621 #endif
622
623     while (client->groups)
624         avahi_entry_group_free(client->groups);
625
626     while (client->domain_browsers)
627         avahi_domain_browser_free(client->domain_browsers);
628
629     while (client->service_browsers)
630         avahi_service_browser_free(client->service_browsers);
631
632     while (client->service_type_browsers)
633         avahi_service_type_browser_free(client->service_type_browsers);
634
635     while (client->service_resolvers)
636         avahi_service_resolver_free(client->service_resolvers);
637
638     while (client->host_name_resolvers)
639         avahi_host_name_resolver_free(client->host_name_resolvers);
640
641     while (client->address_resolvers)
642         avahi_address_resolver_free(client->address_resolvers);
643
644     while (client->record_browsers)
645         avahi_record_browser_free(client->record_browsers);
646
647     if (client->bus)
648         dbus_connection_unref(client->bus);
649
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);
654
655     avahi_free(client);
656 }
657
658 static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) {
659     DBusMessage *message = NULL, *reply = NULL;
660     DBusError error;
661     char *ret, *n;
662
663     assert(client);
664     assert(method);
665
666     dbus_error_init (&error);
667
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);
670         goto fail;
671     }
672
673     if (param) {
674         if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID)) {
675             avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
676             goto fail;
677         }
678     }
679
680     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
681
682     if (!reply || dbus_error_is_set (&error))
683         goto fail;
684
685     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID) ||
686         dbus_error_is_set (&error))
687         goto fail;
688
689     if (!(n = avahi_strdup(ret))) {
690         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
691         goto fail;
692     }
693
694     dbus_message_unref(message);
695     dbus_message_unref(reply);
696
697     return n;
698
699 fail:
700
701     if (message)
702         dbus_message_unref(message);
703     if (reply)
704         dbus_message_unref(reply);
705
706     if (dbus_error_is_set(&error)) {
707         avahi_client_set_dbus_error(client, &error);
708         dbus_error_free(&error);
709     }
710
711     return NULL;
712 }
713
714 const char* avahi_client_get_version_string(AvahiClient *client) {
715     assert(client);
716
717     if (!avahi_client_is_connected(client)) {
718         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
719         return NULL;
720     }
721
722     if (!client->version_string)
723         client->version_string = avahi_client_get_string_reply_and_block(client, "GetVersionString", NULL);
724
725     return client->version_string;
726 }
727
728 const char* avahi_client_get_domain_name(AvahiClient *client) {
729     assert(client);
730
731     if (!avahi_client_is_connected(client)) {
732         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
733         return NULL;
734     }
735
736     if (!client->domain_name)
737         client->domain_name = avahi_client_get_string_reply_and_block(client, "GetDomainName", NULL);
738
739     return client->domain_name;
740 }
741
742 const char* avahi_client_get_host_name(AvahiClient *client) {
743     assert(client);
744
745     if (!avahi_client_is_connected(client)) {
746         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
747         return NULL;
748     }
749
750     if (!client->host_name)
751         client->host_name = avahi_client_get_string_reply_and_block(client, "GetHostName", NULL);
752
753     return client->host_name;
754 }
755
756 const char* avahi_client_get_host_name_fqdn (AvahiClient *client) {
757     assert(client);
758
759     if (!avahi_client_is_connected(client)) {
760         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
761         return NULL;
762     }
763
764     if (!client->host_name_fqdn)
765         client->host_name_fqdn = avahi_client_get_string_reply_and_block(client, "GetHostNameFqdn", NULL);
766
767     return client->host_name_fqdn;
768 }
769
770 AvahiClientState avahi_client_get_state(AvahiClient *client) {
771     assert(client);
772
773     return client->state;
774 }
775
776 int avahi_client_errno(AvahiClient *client) {
777     assert(client);
778
779     return client->error;
780 }
781
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;
785     DBusError error;
786     int r = AVAHI_OK;
787
788     dbus_error_init(&error);
789
790     assert(client);
791     assert(path);
792     assert(interface);
793     assert(method);
794
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);
797         goto fail;
798     }
799
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);
803         goto fail;
804     }
805
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);
809         goto fail;
810     }
811
812     dbus_message_unref(message);
813     dbus_message_unref(reply);
814
815     return AVAHI_OK;
816
817 fail:
818     if (dbus_error_is_set(&error)) {
819         r = avahi_client_set_dbus_error(client, &error);
820         dbus_error_free(&error);
821     }
822
823     if (message)
824         dbus_message_unref(message);
825
826     if (reply)
827         dbus_message_unref(reply);
828
829     return r;
830 }
831
832 uint32_t avahi_client_get_local_service_cookie(AvahiClient *client) {
833     DBusMessage *message = NULL, *reply = NULL;
834     DBusError error;
835     assert(client);
836
837     if (!avahi_client_is_connected(client)) {
838         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
839         return AVAHI_SERVICE_COOKIE_INVALID;
840     }
841
842     if (client->local_service_cookie_valid)
843         return client->local_service_cookie;
844
845     dbus_error_init (&error);
846
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);
849         goto fail;
850     }
851
852     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
853
854     if (!reply || dbus_error_is_set (&error))
855         goto fail;
856
857     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &client->local_service_cookie, DBUS_TYPE_INVALID) ||
858         dbus_error_is_set (&error))
859         goto fail;
860
861     dbus_message_unref(message);
862     dbus_message_unref(reply);
863
864     client->local_service_cookie_valid = 1;
865     return client->local_service_cookie;
866
867 fail:
868
869     if (message)
870         dbus_message_unref(message);
871     if (reply)
872         dbus_message_unref(reply);
873
874     if (dbus_error_is_set(&error)) {
875         avahi_client_set_dbus_error(client, &error);
876         dbus_error_free(&error);
877     }
878
879     return AVAHI_SERVICE_COOKIE_INVALID;
880 }
881
882 int avahi_client_is_connected(AvahiClient *client) {
883     assert(client);
884
885     return
886         client->bus &&
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);
889 }
890
891 int avahi_client_set_host_name(AvahiClient* client, const char *name) {
892     DBusMessage *message = NULL, *reply = NULL;
893     DBusError error;
894
895     assert(client);
896
897     if (!avahi_client_is_connected(client))
898         return avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
899
900     dbus_error_init (&error);
901
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);
904         goto fail;
905     }
906
907     if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
908         avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
909         goto fail;
910     }
911
912     reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error);
913
914     if (!reply || dbus_error_is_set (&error))
915         goto fail;
916
917     if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
918         dbus_error_is_set (&error))
919         goto fail;
920
921     dbus_message_unref(message);
922     dbus_message_unref(reply);
923
924     avahi_free(client->host_name);
925     client->host_name = NULL;
926     avahi_free(client->host_name_fqdn);
927     client->host_name_fqdn = NULL;
928
929     return 0;
930
931 fail:
932
933     if (message)
934         dbus_message_unref(message);
935     if (reply)
936         dbus_message_unref(reply);
937
938     if (dbus_error_is_set(&error)) {
939         avahi_client_set_dbus_error(client, &error);
940         dbus_error_free(&error);
941     }
942
943     return client->error;
944 }