]> git.meshlink.io Git - catta/blob - avahi-client/client.c
* drop useless argument
[catta] / 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
178     else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew")) 
179         return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message);
180     else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove")) 
181         return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message);
182
183     else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew")) 
184         return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message);
185     else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove")) 
186         return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message);
187
188     return DBUS_HANDLER_RESULT_HANDLED;
189
190 fail:
191
192     dbus_error_free (&error);
193     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
194 }
195
196 static int get_server_state(AvahiClient *client, int *ret_error) {
197     DBusMessage *message, *reply;
198     DBusError error;
199     int32_t state;
200     int e;
201     
202     assert(client);
203
204     dbus_error_init(&error);
205
206     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState")))
207         goto fail;
208
209     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
210     dbus_message_unref(message);
211
212     if (!reply)
213         goto fail;
214
215     if (!(dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID)))
216         goto fail;
217
218     client_set_state(client, (AvahiServerState) state);
219
220     return AVAHI_OK;
221
222 fail:
223     if (dbus_error_is_set(&error)) {
224         e = avahi_error_dbus_to_number (error.name);
225         dbus_error_free(&error);
226     } else
227         e = AVAHI_ERR_NO_MEMORY;
228
229     if (ret_error)
230         *ret_error = e;
231         
232     return e;
233 }
234
235 /* This function acts like dbus_bus_get but creates a private
236  * connection instead */
237 static DBusConnection*
238 avahi_dbus_bus_get (DBusError *error)
239 {
240     DBusConnection *conn;
241     char *env_addr;
242
243     env_addr = getenv ("DBUS_SYSTEM_BUS_ADDRESS");
244
245     if (env_addr == NULL || (*env_addr == 0))
246     {
247         env_addr = DBUS_SYSTEM_BUS_DEFAULT_ADDRESS;
248     }
249
250     conn = dbus_connection_open_private (env_addr, error);
251
252     if (!conn)
253         return NULL;
254
255     dbus_connection_set_exit_on_disconnect (conn, FALSE);
256
257     if (!dbus_bus_register (conn, error))
258     {
259         dbus_connection_close (conn);
260         dbus_connection_unref (conn);
261
262         return NULL;
263     }
264
265     return conn;
266 }
267
268 AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *ret_error) {
269     AvahiClient *client = NULL;
270     DBusError error;
271
272     dbus_error_init (&error);
273
274     if (!(client = avahi_new(AvahiClient, 1))) {
275         if (ret_error)
276             *ret_error = AVAHI_ERR_NO_MEMORY;
277         goto fail;
278     }
279
280     client->poll_api = poll_api;
281     client->error = AVAHI_OK;
282     client->callback = callback;
283     client->userdata = userdata;
284     client->state = AVAHI_CLIENT_DISCONNECTED;
285
286     client->host_name = NULL;
287     client->host_name_fqdn = NULL;
288     client->domain_name = NULL;
289     client->version_string = NULL;
290
291     AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
292     AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
293     AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
294     AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
295     AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers);
296
297     if (!(client->bus = avahi_dbus_bus_get(&error)) ||
298         dbus_error_is_set (&error))
299         goto fail;
300
301     if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
302         if (ret_error)
303             *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
304         goto fail;
305     }
306
307     if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
308         if (ret_error)
309             *ret_error = AVAHI_ERR_NO_MEMORY; 
310         goto fail;
311     }
312         
313     dbus_bus_add_match(
314         client->bus,
315         "type='signal', "
316         "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
317         "sender='" AVAHI_DBUS_NAME "', "
318         "path='" AVAHI_DBUS_PATH_SERVER "'",
319         &error);
320
321     if (dbus_error_is_set (&error))
322         goto fail;
323
324     dbus_bus_add_match (
325         client->bus,
326         "type='signal', "
327         "interface='" DBUS_INTERFACE_DBUS "', "
328         "sender='" DBUS_SERVICE_DBUS "', "
329         "path='" DBUS_PATH_DBUS "'",
330         &error);
331
332     if (dbus_error_is_set (&error))
333         goto fail;
334
335         dbus_bus_add_match (
336         client->bus,
337         "type='signal', "
338         "interface='" DBUS_INTERFACE_LOCAL "'",
339         &error);
340
341     if (dbus_error_is_set (&error))
342         goto fail;
343
344     if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error)) ||
345         dbus_error_is_set(&error)) {
346
347         if (ret_error)
348             *ret_error = AVAHI_ERR_NO_DAEMON;
349         
350         goto fail;
351     }
352
353     if (get_server_state(client, ret_error) < 0)
354         goto fail;
355
356     return client;
357
358 fail:
359
360     if (client)
361         avahi_client_free(client);
362
363     if (dbus_error_is_set(&error)) {
364
365         if (ret_error)
366             *ret_error = avahi_error_dbus_to_number(error.name);
367         
368         dbus_error_free(&error);
369     }
370         
371     return NULL;
372 }
373
374 void avahi_client_free(AvahiClient *client) {
375     assert(client);
376
377     while (client->groups)
378         avahi_entry_group_free(client->groups);
379
380     while (client->domain_browsers)
381         avahi_domain_browser_free(client->domain_browsers);
382
383     while (client->service_browsers)
384         avahi_service_browser_free(client->service_browsers);
385
386     while (client->service_type_browsers)
387         avahi_service_type_browser_free(client->service_type_browsers);
388
389     while (client->service_resolvers)
390         avahi_service_resolver_free(client->service_resolvers);
391
392     if (client->bus) {
393         dbus_connection_disconnect(client->bus);
394         dbus_connection_unref(client->bus);
395     }
396
397     avahi_free(client->version_string);
398     avahi_free(client->host_name);
399     avahi_free(client->host_name_fqdn);
400     avahi_free(client->domain_name);
401     
402     avahi_free(client);
403 }
404
405 static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) {
406     DBusMessage *message = NULL, *reply = NULL;
407     DBusError error;
408     char *ret, *n;
409
410     assert(client);
411     assert(method);
412
413     dbus_error_init (&error);
414
415     if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method))) {
416         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
417         goto fail;
418     }
419
420     if (param) {
421         if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID)) {
422             avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
423             goto fail;
424         }
425     }
426     
427     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
428
429     if (!reply || dbus_error_is_set (&error))
430         goto fail;
431
432     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID) ||
433         dbus_error_is_set (&error))
434         goto fail;
435     
436     if (!(n = avahi_strdup(ret))) {
437         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
438         goto fail;
439     }
440
441     dbus_message_unref(message);
442     dbus_message_unref(reply);
443     
444     return n;
445
446 fail:
447
448     if (message)
449         dbus_message_unref(message);
450     if (reply)
451         dbus_message_unref(reply);
452     
453     if (dbus_error_is_set(&error)) {
454         avahi_client_set_dbus_error(client, &error);
455         dbus_error_free(&error);
456     }
457
458     return NULL;
459 }
460
461 const char* avahi_client_get_version_string (AvahiClient *client) {
462     assert(client);
463
464     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
465         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
466         return NULL;
467     }
468
469     if (!client->version_string)
470         client->version_string = avahi_client_get_string_reply_and_block(client, "GetVersionString", NULL);
471
472     return client->version_string;
473 }
474
475 const char* avahi_client_get_domain_name (AvahiClient *client) {
476     assert(client);
477
478     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
479         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
480         return NULL;
481     }
482
483     if (!client->domain_name)
484         client->domain_name = avahi_client_get_string_reply_and_block(client, "GetDomainName", NULL);
485     
486     return client->domain_name;
487 }
488
489 const char* avahi_client_get_host_name (AvahiClient *client) {
490     assert(client);
491     
492     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
493         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
494         return NULL;
495     }
496
497     if (!client->host_name)
498         client->host_name = avahi_client_get_string_reply_and_block(client, "GetHostName", NULL);
499     
500     return client->host_name;
501 }
502
503 const char* avahi_client_get_host_name_fqdn (AvahiClient *client) {
504     assert(client);
505
506     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
507         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
508         return NULL;
509     }
510     
511     if (!client->host_name_fqdn)
512         client->host_name_fqdn = avahi_client_get_string_reply_and_block(client, "GetHostNameFqdn", NULL);
513
514     return client->host_name_fqdn;
515 }
516
517 AvahiClientState avahi_client_get_state(AvahiClient *client) {
518     assert(client);
519
520     return client->state;
521 }
522
523 int avahi_client_errno(AvahiClient *client) {
524     assert(client);
525     
526     return client->error;
527 }