* start implementing error handling in avahi-client
[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 <avahi-client/client.h>
27 #include <avahi-common/dbus.h>
28 #include <avahi-common/llist.h>
29 #include <avahi-common/error.h>
30 #include <avahi-common/malloc.h>
31 #include <avahi-common/dbus-watch-glue.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #include <dbus/dbus.h>
37
38 #include <stdlib.h>
39
40 #include "client.h"
41 #include "internal.h"
42
43 int
44 avahi_client_set_errno (AvahiClient *client, int error)
45 {
46     assert(client);
47
48     client->error = error;
49
50     return error;
51 }
52     
53 static void avahi_client_set_state (AvahiClient *client, AvahiServerState state) {
54     assert(state);
55
56     if (client->state == state)
57         return;
58
59     client->state = state;
60
61     if (client->callback)
62         client->callback (client, state, client->userdata);
63 }
64
65 static void
66 avahi_client_state_request_callback (DBusPendingCall *call, void *data)
67 {
68     AvahiClient *client = data;
69     DBusError error;
70     DBusMessage *reply;
71     int state, type;
72
73     dbus_error_init (&error);
74
75     reply = dbus_pending_call_steal_reply (call);
76
77     type = dbus_message_get_type (reply);
78
79     if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
80     {
81         dbus_message_get_args (reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
82         
83         if (dbus_error_is_set (&error))
84             return;
85         
86         avahi_client_set_state (client, state);
87     } else if (type == DBUS_MESSAGE_TYPE_ERROR) {
88         dbus_set_error_from_message (&error, reply);
89     }
90
91     dbus_pending_call_unref (call);
92 }
93
94 static void
95 avahi_client_schedule_state_request (AvahiClient *client)
96 {
97     DBusMessage *message;
98     DBusPendingCall *pcall;
99
100     /*** Lennart says that this can't happen this way since it will
101      * never be called if no main loop is used. This call has to
102      * happen synchronously */
103
104     if (client == NULL) return;
105
106     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState");
107
108     dbus_connection_send_with_reply (client->bus, message, &pcall, -1);
109
110     dbus_pending_call_set_notify (pcall, avahi_client_state_request_callback, client, NULL);
111 }
112
113 static DBusHandlerResult
114 filter_func (DBusConnection *bus, DBusMessage *message, void *data)
115 {
116     AvahiClient *client = data;
117     DBusError error;
118     
119     printf ("dbus: interface=%s, path=%s, member=%s\n",
120             dbus_message_get_interface (message),
121             dbus_message_get_path (message),
122             dbus_message_get_member (message));
123
124     dbus_error_init (&error);
125
126     if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
127         char *name, *old, *new;
128         dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID);
129         
130         if (dbus_error_is_set (&error)) {
131             dbus_error_free (&error);
132             goto out;
133         }
134
135         if (strcmp (name, AVAHI_DBUS_NAME) == 0) {
136
137             if (old == NULL && new != NULL) {
138                 avahi_client_set_state (client, AVAHI_CLIENT_RECONNECTED);
139             } else if (old != NULL && new == NULL) {
140                 avahi_client_set_state (client, AVAHI_CLIENT_DISCONNECTED);
141                 /* XXX: we really need to expire all entry groups */
142             }
143         }
144     } else if (dbus_message_is_signal (message, AVAHI_DBUS_NAME, "StateChanged")) {
145         /* XXX: todo */
146         printf ("server statechange\n");
147     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
148         const char *path;
149         AvahiEntryGroup *n, *group = NULL;
150         path = dbus_message_get_path (message);
151
152         for (n = client->groups; n != NULL; n = n->groups_next)
153         {
154             if (strcmp (n->path, path) == 0)
155             {
156                 group = n;
157                 break;
158             }
159         }
160         
161         if (group != NULL) {
162             int state;
163             dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
164             if (dbus_error_is_set (&error))
165                 goto out;
166             
167             avahi_entry_group_state_change (group, state);
168         }
169     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemNew")) {
170         return avahi_domain_browser_event (client, AVAHI_BROWSER_NEW, message);
171     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemRemove")) {
172         return avahi_domain_browser_event (client, AVAHI_BROWSER_REMOVE, message);
173     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew")) {
174         return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message);
175     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove")) {
176         return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message);
177     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew")) {
178         return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message);
179     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove")) {
180         return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message);
181     }
182
183     return DBUS_HANDLER_RESULT_HANDLED;
184
185 out: 
186     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
187 }
188
189 static int translate_dbus_error(const DBusError *error) {
190     assert(error);
191
192     /*** FIXME! Some more eloquent error translation should happen here */
193     
194     return AVAHI_ERR_DBUS_ERROR;
195 }
196
197 AvahiClient *
198 avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *ret_error)
199 {
200     AvahiClient *client = NULL;
201     DBusError error;
202
203     dbus_error_init (&error);
204
205     if (!(client = avahi_new(AvahiClient, 1))) {
206         if (ret_error)
207             *ret_error = AVAHI_ERR_NO_MEMORY;
208         goto fail;
209     }
210
211     client->poll_api = poll_api;
212     client->error = AVAHI_OK;
213     client->callback = callback;
214     client->userdata = userdata;
215     client->state = AVAHI_SERVER_INVALID;
216
217     AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
218     AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
219     AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
220     AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
221
222     client->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
223     if (dbus_error_is_set (&error)) {
224         if (ret_error)
225             *ret_error = translate_dbus_error(&error);
226         goto fail;
227     }
228
229     if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
230         if (ret_error)
231             *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
232         goto fail;
233     }
234
235     if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
236         if (ret_error)
237             *ret_error = AVAHI_ERR_NO_MEMORY; 
238         goto fail;
239     }
240         
241     dbus_bus_add_match(
242         client->bus,
243         "type='signal', "
244         "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
245         "sender='" AVAHI_DBUS_NAME "', "
246         "path='" AVAHI_DBUS_PATH_SERVER "'",
247         &error);
248
249     if (dbus_error_is_set (&error)) {
250         if (ret_error)
251             *ret_error = translate_dbus_error(&error);
252         goto fail;
253     }   
254
255     dbus_bus_add_match (
256         client->bus,
257         "type='signal', "
258         "interface='" DBUS_INTERFACE_DBUS "', "
259         "sender='" DBUS_SERVICE_DBUS "', "
260         "path='" DBUS_PATH_DBUS "'",
261         &error);
262
263     if (dbus_error_is_set (&error)) {
264         if (ret_error)
265             *ret_error = translate_dbus_error(&error);
266         goto fail;
267     }
268
269     if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error))) {
270
271         if (dbus_error_is_set (&error)) {
272             if (ret_error)
273                 *ret_error = translate_dbus_error(&error);
274             goto fail;
275         }
276         
277         if (ret_error)
278             *ret_error = AVAHI_ERR_NO_DAEMON;
279         goto fail;
280     }
281
282     /* This can't happen asynchronously, since it is not guaranteed that a main loop is used */
283     
284     /*client_get_server_state (client);*/
285
286     return client;
287
288 fail:
289
290     if (client) {
291
292         if (client->bus) {
293             dbus_connection_disconnect(client->bus);
294             dbus_connection_unref(client->bus);
295         }
296         
297         avahi_free(client);
298     }
299
300     if (dbus_error_is_set(&error))
301         dbus_error_free(&error);
302         
303     return NULL;
304 }
305
306 static char*
307 avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param)
308 {
309     DBusMessage *message;
310     DBusMessage *reply;
311     DBusError error;
312     char *ret, *new;
313
314     if (client == NULL || method == NULL) return NULL;
315
316     dbus_error_init (&error);
317
318     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method);
319
320     if (param != NULL)
321     {
322         if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID))
323         {
324             avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
325             return NULL;
326         }
327     }
328     
329     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
330
331     if (dbus_error_is_set (&error))
332     {
333         dbus_error_free (&error);
334         dbus_message_unref (message);
335
336         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
337         return NULL;
338     }
339
340     if (reply == NULL)
341     {
342         dbus_message_unref (message);
343
344         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
345         return NULL;
346     }
347
348     dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
349
350     if (dbus_error_is_set (&error))
351     {
352         dbus_error_free (&error);
353
354         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
355         return NULL;
356     }
357
358     new = avahi_strdup (ret);
359
360     avahi_client_set_errno (client, AVAHI_OK);
361     return new;
362 }
363
364 char*
365 avahi_client_get_version_string (AvahiClient *client)
366 {
367     return avahi_client_get_string_reply_and_block (client, "GetVersionString", NULL);
368 }
369
370 char*
371 avahi_client_get_domain_name (AvahiClient *client)
372 {
373     return avahi_client_get_string_reply_and_block (client, "GetDomainName", NULL);
374 }
375
376 char*
377 avahi_client_get_host_name (AvahiClient *client)
378 {
379     return avahi_client_get_string_reply_and_block (client, "GetHostName", NULL);
380 }
381
382 char*
383 avahi_client_get_host_name_fqdn (AvahiClient *client)
384 {
385     return avahi_client_get_string_reply_and_block (client, "GetHostNameFqdn", NULL);
386 }