]> git.meshlink.io Git - catta/blob - avahi-client/client.c
* add magic identification cookies to service TXT records automatically
[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     else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Found")) 
189         return avahi_service_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
190     else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Timeout")) 
191         return avahi_service_resolver_event (client, AVAHI_RESOLVER_TIMEOUT, message);
192
193     else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Found")) 
194         return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
195     else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Timeout")) 
196         return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_TIMEOUT, message);
197
198     else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Found")) 
199         return avahi_address_resolver_event (client, AVAHI_RESOLVER_FOUND, message);
200     else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Timeout")) 
201         return avahi_address_resolver_event (client, AVAHI_RESOLVER_TIMEOUT, message);
202     
203     
204     return DBUS_HANDLER_RESULT_HANDLED;
205
206 fail:
207
208     dbus_error_free (&error);
209     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
210 }
211
212 static int get_server_state(AvahiClient *client, int *ret_error) {
213     DBusMessage *message, *reply;
214     DBusError error;
215     int32_t state;
216     int e;
217     
218     assert(client);
219
220     dbus_error_init(&error);
221
222     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState")))
223         goto fail;
224
225     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
226     dbus_message_unref(message);
227
228     if (!reply)
229         goto fail;
230
231     if (!(dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID)))
232         goto fail;
233
234     client_set_state(client, (AvahiServerState) state);
235
236     return AVAHI_OK;
237
238 fail:
239     if (dbus_error_is_set(&error)) {
240         e = avahi_error_dbus_to_number (error.name);
241         dbus_error_free(&error);
242     } else
243         e = AVAHI_ERR_NO_MEMORY;
244
245     if (ret_error)
246         *ret_error = e;
247         
248     return e;
249 }
250
251 /* This function acts like dbus_bus_get but creates a private
252  * connection instead */
253 static DBusConnection*
254 avahi_dbus_bus_get (DBusError *error)
255 {
256     DBusConnection *conn;
257     const char *env_addr;
258
259     env_addr = getenv ("DBUS_SYSTEM_BUS_ADDRESS");
260
261     if (env_addr == NULL || (*env_addr == 0))
262     {
263         env_addr = DBUS_SYSTEM_BUS_DEFAULT_ADDRESS;
264     }
265
266     conn = dbus_connection_open_private (env_addr, error);
267
268     if (!conn)
269         return NULL;
270
271     dbus_connection_set_exit_on_disconnect (conn, FALSE);
272
273     if (!dbus_bus_register (conn, error))
274     {
275         dbus_connection_close (conn);
276         dbus_connection_unref (conn);
277
278         return NULL;
279     }
280
281     return conn;
282 }
283
284 AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *ret_error) {
285     AvahiClient *client = NULL;
286     DBusError error;
287
288     dbus_error_init (&error);
289
290     if (!(client = avahi_new(AvahiClient, 1))) {
291         if (ret_error)
292             *ret_error = AVAHI_ERR_NO_MEMORY;
293         goto fail;
294     }
295
296     client->poll_api = poll_api;
297     client->error = AVAHI_OK;
298     client->callback = callback;
299     client->userdata = userdata;
300     client->state = AVAHI_CLIENT_DISCONNECTED;
301
302     client->host_name = NULL;
303     client->host_name_fqdn = NULL;
304     client->domain_name = NULL;
305     client->version_string = NULL;
306     client->local_service_cookie_valid = 0;
307     
308     AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
309     AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
310     AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
311     AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
312     AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers);
313     AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, client->host_name_resolvers);
314     AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, client->address_resolvers);
315
316     if (!(client->bus = avahi_dbus_bus_get(&error)) ||
317         dbus_error_is_set (&error))
318         goto fail;
319
320     if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
321         if (ret_error)
322             *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
323         goto fail;
324     }
325
326     if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
327         if (ret_error)
328             *ret_error = AVAHI_ERR_NO_MEMORY; 
329         goto fail;
330     }
331         
332     dbus_bus_add_match(
333         client->bus,
334         "type='signal', "
335         "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
336         "sender='" AVAHI_DBUS_NAME "', "
337         "path='" AVAHI_DBUS_PATH_SERVER "'",
338         &error);
339
340     if (dbus_error_is_set (&error))
341         goto fail;
342
343     dbus_bus_add_match (
344         client->bus,
345         "type='signal', "
346         "interface='" DBUS_INTERFACE_DBUS "', "
347         "sender='" DBUS_SERVICE_DBUS "', "
348         "path='" DBUS_PATH_DBUS "'",
349         &error);
350
351     if (dbus_error_is_set (&error))
352         goto fail;
353
354         dbus_bus_add_match (
355         client->bus,
356         "type='signal', "
357         "interface='" DBUS_INTERFACE_LOCAL "'",
358         &error);
359
360     if (dbus_error_is_set (&error))
361         goto fail;
362
363     if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error)) ||
364         dbus_error_is_set(&error)) {
365
366         if (ret_error)
367             *ret_error = AVAHI_ERR_NO_DAEMON;
368         
369         goto fail;
370     }
371
372     if (get_server_state(client, ret_error) < 0)
373         goto fail;
374
375     return client;
376
377 fail:
378
379     if (client)
380         avahi_client_free(client);
381
382     if (dbus_error_is_set(&error)) {
383
384         if (ret_error)
385             *ret_error = avahi_error_dbus_to_number(error.name);
386         
387         dbus_error_free(&error);
388     }
389         
390     return NULL;
391 }
392
393 void avahi_client_free(AvahiClient *client) {
394     assert(client);
395
396     while (client->groups)
397         avahi_entry_group_free(client->groups);
398
399     while (client->domain_browsers)
400         avahi_domain_browser_free(client->domain_browsers);
401
402     while (client->service_browsers)
403         avahi_service_browser_free(client->service_browsers);
404
405     while (client->service_type_browsers)
406         avahi_service_type_browser_free(client->service_type_browsers);
407
408     while (client->service_resolvers)
409         avahi_service_resolver_free(client->service_resolvers);
410
411     if (client->bus) {
412         dbus_connection_disconnect(client->bus);
413         dbus_connection_unref(client->bus);
414     }
415
416     avahi_free(client->version_string);
417     avahi_free(client->host_name);
418     avahi_free(client->host_name_fqdn);
419     avahi_free(client->domain_name);
420     
421     avahi_free(client);
422 }
423
424 static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) {
425     DBusMessage *message = NULL, *reply = NULL;
426     DBusError error;
427     char *ret, *n;
428
429     assert(client);
430     assert(method);
431
432     dbus_error_init (&error);
433
434     if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method))) {
435         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
436         goto fail;
437     }
438
439     if (param) {
440         if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID)) {
441             avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
442             goto fail;
443         }
444     }
445     
446     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
447
448     if (!reply || dbus_error_is_set (&error))
449         goto fail;
450
451     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID) ||
452         dbus_error_is_set (&error))
453         goto fail;
454     
455     if (!(n = avahi_strdup(ret))) {
456         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
457         goto fail;
458     }
459
460     dbus_message_unref(message);
461     dbus_message_unref(reply);
462     
463     return n;
464
465 fail:
466
467     if (message)
468         dbus_message_unref(message);
469     if (reply)
470         dbus_message_unref(reply);
471     
472     if (dbus_error_is_set(&error)) {
473         avahi_client_set_dbus_error(client, &error);
474         dbus_error_free(&error);
475     }
476
477     return NULL;
478 }
479
480 const char* avahi_client_get_version_string (AvahiClient *client) {
481     assert(client);
482
483     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
484         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
485         return NULL;
486     }
487
488     if (!client->version_string)
489         client->version_string = avahi_client_get_string_reply_and_block(client, "GetVersionString", NULL);
490
491     return client->version_string;
492 }
493
494 const char* avahi_client_get_domain_name (AvahiClient *client) {
495     assert(client);
496
497     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
498         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
499         return NULL;
500     }
501
502     if (!client->domain_name)
503         client->domain_name = avahi_client_get_string_reply_and_block(client, "GetDomainName", NULL);
504     
505     return client->domain_name;
506 }
507
508 const char* avahi_client_get_host_name (AvahiClient *client) {
509     assert(client);
510     
511     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
512         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
513         return NULL;
514     }
515
516     if (!client->host_name)
517         client->host_name = avahi_client_get_string_reply_and_block(client, "GetHostName", NULL);
518     
519     return client->host_name;
520 }
521
522 const char* avahi_client_get_host_name_fqdn (AvahiClient *client) {
523     assert(client);
524
525     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
526         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
527         return NULL;
528     }
529     
530     if (!client->host_name_fqdn)
531         client->host_name_fqdn = avahi_client_get_string_reply_and_block(client, "GetHostNameFqdn", NULL);
532
533     return client->host_name_fqdn;
534 }
535
536 AvahiClientState avahi_client_get_state(AvahiClient *client) {
537     assert(client);
538
539     return client->state;
540 }
541
542 int avahi_client_errno(AvahiClient *client) {
543     assert(client);
544     
545     return client->error;
546 }
547
548 /* Just for internal use */
549 int avahi_client_simple_method_call(AvahiClient *client, const char *path, const char *interface, const char *method) {
550     DBusMessage *message = NULL, *reply = NULL;
551     DBusError error;
552     int r = AVAHI_OK;
553     
554     dbus_error_init(&error);
555
556     assert(client);
557     assert(path);
558     assert(interface);
559     assert(method);
560     
561     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, path, interface, method))) {
562         r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
563         goto fail;
564     }
565         
566     if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
567         dbus_error_is_set (&error)) {
568         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
569         goto fail;
570     }
571     
572     if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
573         dbus_error_is_set (&error)) {
574         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
575         goto fail;
576     }
577
578     dbus_message_unref(message);
579     dbus_message_unref(reply);
580
581     return AVAHI_OK;
582     
583 fail:
584     if (dbus_error_is_set(&error)) {
585         r = avahi_client_set_dbus_error(client, &error);
586         dbus_error_free(&error);
587     }
588
589     if (message)
590         dbus_message_unref(message);
591
592     if (reply)
593         dbus_message_unref(reply);
594
595     return r;
596 }
597
598 uint32_t avahi_client_get_local_service_cookie(AvahiClient *client) {
599     DBusMessage *message = NULL, *reply = NULL;
600     DBusError error;
601     assert(client);
602
603     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
604         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
605         return AVAHI_SERVICE_COOKIE_INVALID;
606     }
607
608     if (client->local_service_cookie_valid)
609         return client->local_service_cookie;
610
611     dbus_error_init (&error);
612
613     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetLocalServiceCookie"))) {
614         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
615         goto fail;
616     }
617
618     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
619
620     if (!reply || dbus_error_is_set (&error))
621         goto fail;
622
623     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &client->local_service_cookie, DBUS_TYPE_INVALID) ||
624         dbus_error_is_set (&error))
625         goto fail;
626     
627     dbus_message_unref(message);
628     dbus_message_unref(reply);
629
630     client->local_service_cookie_valid = 1;
631     return client->local_service_cookie;
632
633 fail:
634
635     if (message)
636         dbus_message_unref(message);
637     if (reply)
638         dbus_message_unref(reply);
639     
640     if (dbus_error_is_set(&error)) {
641         avahi_client_set_dbus_error(client, &error);
642         dbus_error_free(&error);
643     }
644
645     return AVAHI_SERVICE_COOKIE_INVALID;
646 }