* many improvements to avahi-client (especially error handling)
[platform/upstream/avahi.git] / avahi-client / client.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <dbus/dbus.h>
31
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>
38
39 #include "client.h"
40 #include "internal.h"
41
42 int avahi_client_set_errno (AvahiClient *client, int error) {
43     assert(client);
44
45     return client->error = error;
46 }
47
48 int avahi_client_set_dbus_error(AvahiClient *client, DBusError *error) {
49     assert(client);
50     assert(error);
51
52     return avahi_client_set_errno(client, avahi_error_dbus_to_number(error->name));
53 }
54
55 static void client_set_state (AvahiClient *client, AvahiServerState state) {
56     assert(client);
57
58     if (client->state == state)
59         return;
60
61     client->state = state;
62
63     switch (client->state) {
64         case AVAHI_CLIENT_DISCONNECTED:
65             if (client->bus) {
66                 dbus_connection_disconnect(client->bus);
67                 dbus_connection_unref(client->bus);
68                 client->bus = NULL;
69             }
70
71             /* Fall through */
72
73         case AVAHI_CLIENT_S_COLLISION:
74         case AVAHI_CLIENT_S_REGISTERING:
75
76             /* Clear cached strings */
77             avahi_free(client->host_name);
78             avahi_free(client->host_name_fqdn);
79             avahi_free(client->domain_name);
80
81             client->host_name =  NULL;
82             client->host_name_fqdn = NULL;
83             client->domain_name = NULL;
84             break;
85
86         case AVAHI_CLIENT_S_INVALID:
87         case AVAHI_CLIENT_S_RUNNING:
88             break;
89             
90     }
91     
92     if (client->callback)
93         client->callback (client, state, client->userdata);
94 }
95
96 static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, void *userdata) {
97     AvahiClient *client = userdata;
98     DBusError error;
99
100     assert(bus);
101     assert(message);
102     
103     dbus_error_init (&error);
104
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));
109
110     if (client->state == AVAHI_CLIENT_DISCONNECTED)
111         goto fail;
112
113     if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
114
115         /* The DBUS server died or kicked us */
116         client_set_state(client, AVAHI_CLIENT_DISCONNECTED);
117
118     } if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
119         char *name, *old, *new;
120         
121         if (!(dbus_message_get_args(
122                   message, &error,
123                   DBUS_TYPE_STRING, &name,
124                   DBUS_TYPE_STRING, &old,
125                   DBUS_TYPE_STRING, &new,
126                   DBUS_TYPE_INVALID) || dbus_error_is_set (&error))) {
127
128             fprintf(stderr, "WARNING: Failed to parse NameOwnerChanged signal: %s\n", error.message);
129             goto fail;
130         }
131
132         if (strcmp(name, AVAHI_DBUS_NAME) == 0)
133
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 */
137             
138             client_set_state(client, AVAHI_CLIENT_DISCONNECTED);
139
140     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged")) {
141         int32_t state;
142         
143         if (!(dbus_message_get_args(
144                   message, &error,
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);
148             goto fail;
149         }
150             
151         client_set_state(client, (AvahiClientState) state);
152
153     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
154         const char *path;
155         AvahiEntryGroup *g;
156         path = dbus_message_get_path(message);
157
158         for (g = client->groups; g; g = g->groups_next)
159             if (strcmp(g->path, path) == 0)
160                 break;
161         
162         if (g) {
163             int32_t state;
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);
167                 goto fail;
168             }
169             
170             avahi_entry_group_set_state(g, state);
171         }
172         
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_SERVICE_TYPE_BROWSER, "ItemNew")) {
178         return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message);
179     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove")) {
180         return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message);
181     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew")) {
182         return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message);
183     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove")) {
184         return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message);
185     }
186
187     return DBUS_HANDLER_RESULT_HANDLED;
188
189 fail:
190
191     dbus_error_free (&error);
192     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
193 }
194
195 static int get_server_state(AvahiClient *client, int *ret_error) {
196     DBusMessage *message, *reply;
197     DBusError error;
198     int32_t state;
199     int e;
200     
201     assert(client);
202
203     dbus_error_init(&error);
204
205     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState")))
206         goto fail;
207
208     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
209     dbus_message_unref(message);
210
211     if (!reply)
212         goto fail;
213
214     if (!(dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID)))
215         goto fail;
216
217     client_set_state(client, (AvahiServerState) state);
218
219     return AVAHI_OK;
220
221 fail:
222     if (dbus_error_is_set(&error)) {
223         e = avahi_error_dbus_to_number (error.name);
224         dbus_error_free(&error);
225     } else
226         e = AVAHI_ERR_NO_MEMORY;
227
228     if (ret_error)
229         *ret_error = e;
230         
231     return e;
232 }
233
234 AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *ret_error) {
235     AvahiClient *client = NULL;
236     DBusError error;
237
238     dbus_error_init (&error);
239
240     if (!(client = avahi_new(AvahiClient, 1))) {
241         if (ret_error)
242             *ret_error = AVAHI_ERR_NO_MEMORY;
243         goto fail;
244     }
245
246     client->poll_api = poll_api;
247     client->error = AVAHI_OK;
248     client->callback = callback;
249     client->userdata = userdata;
250     client->state = AVAHI_CLIENT_DISCONNECTED;
251
252     client->host_name = NULL;
253     client->host_name_fqdn = NULL;
254     client->domain_name = NULL;
255     client->version_string = NULL;
256
257     AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
258     AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
259     AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
260     AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
261
262     client->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
263     if (!client->bus || dbus_error_is_set (&error))
264         goto fail;
265
266     if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
267         if (ret_error)
268             *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
269         goto fail;
270     }
271
272     if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
273         if (ret_error)
274             *ret_error = AVAHI_ERR_NO_MEMORY; 
275         goto fail;
276     }
277         
278     dbus_bus_add_match(
279         client->bus,
280         "type='signal', "
281         "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
282         "sender='" AVAHI_DBUS_NAME "', "
283         "path='" AVAHI_DBUS_PATH_SERVER "'",
284         &error);
285
286     if (dbus_error_is_set (&error))
287         goto fail;
288
289     dbus_bus_add_match (
290         client->bus,
291         "type='signal', "
292         "interface='" DBUS_INTERFACE_DBUS "', "
293         "sender='" DBUS_SERVICE_DBUS "', "
294         "path='" DBUS_PATH_DBUS "'",
295         &error);
296
297     if (dbus_error_is_set (&error))
298         goto fail;
299
300         dbus_bus_add_match (
301         client->bus,
302         "type='signal', "
303         "interface='" DBUS_INTERFACE_LOCAL "'",
304         &error);
305
306     if (dbus_error_is_set (&error))
307         goto fail;
308
309     if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error))) {
310
311         if (ret_error)
312             *ret_error = AVAHI_ERR_NO_DAEMON;
313         
314         goto fail;
315     }
316
317     if (get_server_state(client, ret_error) < 0)
318         goto fail;
319
320     return client;
321
322 fail:
323
324     if (client)
325         avahi_client_free(client);
326
327     if (dbus_error_is_set(&error)) {
328
329         if (ret_error)
330             *ret_error = avahi_error_dbus_to_number(error.name);
331         
332         dbus_error_free(&error);
333     }
334         
335     return NULL;
336 }
337
338 void avahi_client_free(AvahiClient *client) {
339     assert(client);
340
341     if (client->bus) {
342         dbus_connection_disconnect(client->bus);
343         dbus_connection_unref(client->bus);
344     }
345
346     while (client->groups)
347         avahi_entry_group_free(client->groups);
348
349     while (client->domain_browsers)
350         avahi_domain_browser_free(client->domain_browsers);
351
352     while (client->service_browsers)
353         avahi_service_browser_free(client->service_browsers);
354
355     while (client->service_type_browsers)
356         avahi_service_type_browser_free(client->service_type_browsers);
357
358     avahi_free(client->version_string);
359     avahi_free(client->host_name);
360     avahi_free(client->host_name_fqdn);
361     avahi_free(client->domain_name);
362     
363     avahi_free(client);
364 }
365
366 static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) {
367     DBusMessage *message = NULL, *reply = NULL;
368     DBusError error;
369     char *ret, *n;
370
371     assert(client);
372     assert(method);
373
374     dbus_error_init (&error);
375
376     if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method))) {
377         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
378         goto fail;
379     }
380
381     if (param) {
382         if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID)) {
383             avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
384             goto fail;
385         }
386     }
387     
388     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
389
390     if (!reply || dbus_error_is_set (&error))
391         goto fail;
392
393     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID) ||
394         dbus_error_is_set (&error))
395         goto fail;
396     
397     if (!(n = avahi_strdup(ret))) {
398         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
399         goto fail;
400     }
401
402     dbus_message_unref(message);
403     dbus_message_unref(reply);
404     
405     return n;
406
407 fail:
408
409     if (message)
410         dbus_message_unref(message);
411     if (reply)
412         dbus_message_unref(reply);
413     
414     if (dbus_error_is_set(&error)) {
415         avahi_client_set_dbus_error(client, &error);
416         dbus_error_free(&error);
417     }
418
419     return NULL;
420 }
421
422 const char* avahi_client_get_version_string (AvahiClient *client) {
423     assert(client);
424
425     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
426         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
427         return NULL;
428     }
429
430     if (!client->version_string)
431         client->version_string = avahi_client_get_string_reply_and_block(client, "GetVersionString", NULL);
432
433     return client->version_string;
434 }
435
436 const char* avahi_client_get_domain_name (AvahiClient *client) {
437     assert(client);
438
439     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
440         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
441         return NULL;
442     }
443
444     if (!client->domain_name)
445         client->domain_name = avahi_client_get_string_reply_and_block(client, "GetDomainName", NULL);
446     
447     return client->domain_name;
448 }
449
450 const char* avahi_client_get_host_name (AvahiClient *client) {
451     assert(client);
452     
453     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
454         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
455         return NULL;
456     }
457
458     if (!client->host_name)
459         client->host_name = avahi_client_get_string_reply_and_block(client, "GetHostName", NULL);
460     
461     return client->host_name;
462 }
463
464 const char* avahi_client_get_host_name_fqdn (AvahiClient *client) {
465     assert(client);
466
467     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
468         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
469         return NULL;
470     }
471     
472     if (!client->host_name_fqdn)
473         client->host_name_fqdn = avahi_client_get_string_reply_and_block(client, "GetHostNameFqdn", NULL);
474
475     return client->host_name_fqdn;
476 }
477
478 AvahiClientState avahi_client_get_state(AvahiClient *client) {
479     assert(client);
480
481     return client->state;
482 }
483
484 int avahi_client_errno(AvahiClient *client) {
485     assert(client);
486     
487     return client->error;
488 }