echo-cancel-test: Enable debug log level
[platform/upstream/pulseaudio.git] / src / modules / module-zeroconf-publish.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2004-2006 Lennart Poettering
5
6   PulseAudio is free software; you can redistribute it and/or modify
7   it 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   PulseAudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with PulseAudio; 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 <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29
30 #include <avahi-client/client.h>
31 #include <avahi-client/publish.h>
32 #include <avahi-common/alternative.h>
33 #include <avahi-common/error.h>
34 #include <avahi-common/domain.h>
35
36 #include <pulse/xmalloc.h>
37 #include <pulse/util.h>
38
39 #include <pulsecore/parseaddr.h>
40 #include <pulsecore/sink.h>
41 #include <pulsecore/source.h>
42 #include <pulsecore/native-common.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/dynarray.h>
46 #include <pulsecore/modargs.h>
47 #include <pulsecore/avahi-wrap.h>
48 #include <pulsecore/protocol-native.h>
49
50 #include "module-zeroconf-publish-symdef.h"
51
52 PA_MODULE_AUTHOR("Lennart Poettering");
53 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher");
54 PA_MODULE_VERSION(PACKAGE_VERSION);
55 PA_MODULE_LOAD_ONCE(TRUE);
56
57 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
58 #define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
59 #define SERVICE_TYPE_SERVER "_pulse-server._tcp"
60 #define SERVICE_SUBTYPE_SINK_HARDWARE "_hardware._sub."SERVICE_TYPE_SINK
61 #define SERVICE_SUBTYPE_SINK_VIRTUAL "_virtual._sub."SERVICE_TYPE_SINK
62 #define SERVICE_SUBTYPE_SOURCE_HARDWARE "_hardware._sub."SERVICE_TYPE_SOURCE
63 #define SERVICE_SUBTYPE_SOURCE_VIRTUAL "_virtual._sub."SERVICE_TYPE_SOURCE
64 #define SERVICE_SUBTYPE_SOURCE_MONITOR "_monitor._sub."SERVICE_TYPE_SOURCE
65 #define SERVICE_SUBTYPE_SOURCE_NON_MONITOR "_non-monitor._sub."SERVICE_TYPE_SOURCE
66
67 static const char* const valid_modargs[] = {
68     NULL
69 };
70
71 enum service_subtype {
72     SUBTYPE_HARDWARE,
73     SUBTYPE_VIRTUAL,
74     SUBTYPE_MONITOR
75 };
76
77 struct service {
78     struct userdata *userdata;
79     AvahiEntryGroup *entry_group;
80     char *service_name;
81     pa_object *device;
82     enum service_subtype subtype;
83 };
84
85 struct userdata {
86     pa_core *core;
87     pa_module *module;
88
89     AvahiPoll *avahi_poll;
90     AvahiClient *client;
91
92     pa_hashmap *services;
93     char *service_name;
94
95     AvahiEntryGroup *main_entry_group;
96
97     pa_hook_slot *sink_new_slot, *source_new_slot, *sink_unlink_slot, *source_unlink_slot, *sink_changed_slot, *source_changed_slot;
98
99     pa_native_protocol *native;
100 };
101
102 static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_channel_map *ret_map, const char **ret_name, pa_proplist **ret_proplist, enum service_subtype *ret_subtype) {
103     pa_assert(s);
104     pa_assert(ret_ss);
105     pa_assert(ret_proplist);
106     pa_assert(ret_subtype);
107
108     if (pa_sink_isinstance(s->device)) {
109         pa_sink *sink = PA_SINK(s->device);
110
111         *ret_ss = sink->sample_spec;
112         *ret_map = sink->channel_map;
113         *ret_name = sink->name;
114         *ret_proplist = sink->proplist;
115         *ret_subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL;
116
117     } else if (pa_source_isinstance(s->device)) {
118         pa_source *source = PA_SOURCE(s->device);
119
120         *ret_ss = source->sample_spec;
121         *ret_map = source->channel_map;
122         *ret_name = source->name;
123         *ret_proplist = source->proplist;
124         *ret_subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL);
125
126     } else
127         pa_assert_not_reached();
128 }
129
130 static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
131     char s[128];
132     char *t;
133
134     pa_assert(c);
135
136     l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
137
138     t = pa_get_user_name_malloc();
139     l = avahi_string_list_add_pair(l, "user-name", t);
140     pa_xfree(t);
141
142     t = pa_machine_id();
143     l = avahi_string_list_add_pair(l, "machine-id", t);
144     pa_xfree(t);
145
146     t = pa_uname_string();
147     l = avahi_string_list_add_pair(l, "uname", t);
148     pa_xfree(t);
149
150     l = avahi_string_list_add_pair(l, "fqdn", pa_get_fqdn(s, sizeof(s)));
151     l = avahi_string_list_add_printf(l, "cookie=0x%08x", c->cookie);
152
153     return l;
154 }
155
156 static int publish_service(struct service *s);
157
158 static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
159     struct service *s = userdata;
160
161     pa_assert(s);
162
163     switch (state) {
164
165         case AVAHI_ENTRY_GROUP_ESTABLISHED:
166             pa_log_info("Successfully established service %s.", s->service_name);
167             break;
168
169         case AVAHI_ENTRY_GROUP_COLLISION: {
170             char *t;
171
172             t = avahi_alternative_service_name(s->service_name);
173             pa_log_info("Name collision, renaming %s to %s.", s->service_name, t);
174             pa_xfree(s->service_name);
175             s->service_name = t;
176
177             publish_service(s);
178             break;
179         }
180
181         case AVAHI_ENTRY_GROUP_FAILURE: {
182             pa_log("Failed to register service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
183
184             avahi_entry_group_free(g);
185             s->entry_group = NULL;
186
187             break;
188         }
189
190         case AVAHI_ENTRY_GROUP_UNCOMMITED:
191         case AVAHI_ENTRY_GROUP_REGISTERING:
192             ;
193     }
194 }
195
196 static void service_free(struct service *s);
197
198 static uint16_t compute_port(struct userdata *u) {
199     pa_strlist *i;
200
201     pa_assert(u);
202
203     for (i = pa_native_protocol_servers(u->native); i; i = pa_strlist_next(i)) {
204         pa_parsed_address a;
205
206         if (pa_parse_address(pa_strlist_data(i), &a) >= 0 &&
207             (a.type == PA_PARSED_ADDRESS_TCP4 ||
208              a.type == PA_PARSED_ADDRESS_TCP6 ||
209              a.type == PA_PARSED_ADDRESS_TCP_AUTO) &&
210             a.port > 0) {
211
212             pa_xfree(a.path_or_host);
213             return a.port;
214         }
215
216         pa_xfree(a.path_or_host);
217     }
218
219     return PA_NATIVE_DEFAULT_PORT;
220 }
221
222 static int publish_service(struct service *s) {
223     int r = -1;
224     AvahiStringList *txt = NULL;
225     const char *name = NULL, *t;
226     pa_proplist *proplist = NULL;
227     pa_sample_spec ss;
228     pa_channel_map map;
229     char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
230     enum service_subtype subtype;
231
232     const char * const subtype_text[] = {
233         [SUBTYPE_HARDWARE] = "hardware",
234         [SUBTYPE_VIRTUAL] = "virtual",
235         [SUBTYPE_MONITOR] = "monitor"
236     };
237
238     pa_assert(s);
239
240     if (!s->userdata->client || avahi_client_get_state(s->userdata->client) != AVAHI_CLIENT_S_RUNNING)
241         return 0;
242
243     if (!s->entry_group) {
244         if (!(s->entry_group = avahi_entry_group_new(s->userdata->client, service_entry_group_callback, s))) {
245             pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
246             goto finish;
247         }
248     } else
249         avahi_entry_group_reset(s->entry_group);
250
251     txt = txt_record_server_data(s->userdata->core, txt);
252
253     get_service_data(s, &ss, &map, &name, &proplist, &subtype);
254     txt = avahi_string_list_add_pair(txt, "device", name);
255     txt = avahi_string_list_add_printf(txt, "rate=%u", ss.rate);
256     txt = avahi_string_list_add_printf(txt, "channels=%u", ss.channels);
257     txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(ss.format));
258     txt = avahi_string_list_add_pair(txt, "channel_map", pa_channel_map_snprint(cm, sizeof(cm), &map));
259     txt = avahi_string_list_add_pair(txt, "subtype", subtype_text[subtype]);
260
261     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_DESCRIPTION)))
262         txt = avahi_string_list_add_pair(txt, "description", t);
263     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_ICON_NAME)))
264         txt = avahi_string_list_add_pair(txt, "icon-name", t);
265     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_VENDOR_NAME)))
266         txt = avahi_string_list_add_pair(txt, "vendor-name", t);
267     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_PRODUCT_NAME)))
268         txt = avahi_string_list_add_pair(txt, "product-name", t);
269     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_CLASS)))
270         txt = avahi_string_list_add_pair(txt, "class", t);
271     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_FORM_FACTOR)))
272         txt = avahi_string_list_add_pair(txt, "form-factor", t);
273
274     if (avahi_entry_group_add_service_strlst(
275                 s->entry_group,
276                 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
277                 0,
278                 s->service_name,
279                 pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
280                 NULL,
281                 NULL,
282                 compute_port(s->userdata),
283                 txt) < 0) {
284
285         pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
286         goto finish;
287     }
288
289     if (avahi_entry_group_add_service_subtype(
290                 s->entry_group,
291                 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
292                 0,
293                 s->service_name,
294                 pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
295                 NULL,
296                 pa_sink_isinstance(s->device) ? (subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SINK_HARDWARE : SERVICE_SUBTYPE_SINK_VIRTUAL) :
297                 (subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SOURCE_HARDWARE : (subtype == SUBTYPE_VIRTUAL ? SERVICE_SUBTYPE_SOURCE_VIRTUAL : SERVICE_SUBTYPE_SOURCE_MONITOR))) < 0) {
298
299         pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
300         goto finish;
301     }
302
303     if (pa_source_isinstance(s->device) && subtype != SUBTYPE_MONITOR) {
304         if (avahi_entry_group_add_service_subtype(
305                     s->entry_group,
306                     AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
307                     0,
308                     s->service_name,
309                     SERVICE_TYPE_SOURCE,
310                     NULL,
311                     SERVICE_SUBTYPE_SOURCE_NON_MONITOR) < 0) {
312
313             pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
314             goto finish;
315         }
316     }
317
318     if (avahi_entry_group_commit(s->entry_group) < 0) {
319         pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
320         goto finish;
321     }
322
323     r = 0;
324     pa_log_debug("Successfully created entry group for %s.", s->service_name);
325
326 finish:
327
328     /* Remove this service */
329     if (r < 0) {
330         pa_hashmap_remove(s->userdata->services, s->device);
331         service_free(s);
332     }
333
334     avahi_string_list_free(txt);
335
336     return r;
337 }
338
339 static struct service *get_service(struct userdata *u, pa_object *device) {
340     struct service *s;
341     char *hn, *un;
342     const char *n;
343
344     pa_assert(u);
345     pa_object_assert_ref(device);
346
347     if ((s = pa_hashmap_get(u->services, device)))
348         return s;
349
350     s = pa_xnew(struct service, 1);
351     s->userdata = u;
352     s->entry_group = NULL;
353     s->device = device;
354
355     if (pa_sink_isinstance(device)) {
356         if (!(n = pa_proplist_gets(PA_SINK(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
357             n = PA_SINK(device)->name;
358     } else {
359         if (!(n = pa_proplist_gets(PA_SOURCE(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
360             n = PA_SOURCE(device)->name;
361     }
362
363     hn = pa_get_host_name_malloc();
364     un = pa_get_user_name_malloc();
365
366     s->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s", un, hn, n), AVAHI_LABEL_MAX-1);
367
368     pa_xfree(un);
369     pa_xfree(hn);
370
371     pa_hashmap_put(u->services, s->device, s);
372
373     return s;
374 }
375
376 static void service_free(struct service *s) {
377     pa_assert(s);
378
379     if (s->entry_group) {
380         pa_log_debug("Removing entry group for %s.", s->service_name);
381         avahi_entry_group_free(s->entry_group);
382     }
383
384     pa_xfree(s->service_name);
385     pa_xfree(s);
386 }
387
388 static pa_bool_t shall_ignore(pa_object *o) {
389     pa_object_assert_ref(o);
390
391     if (pa_sink_isinstance(o))
392         return !!(PA_SINK(o)->flags & PA_SINK_NETWORK);
393
394     if (pa_source_isinstance(o))
395         return PA_SOURCE(o)->monitor_of || (PA_SOURCE(o)->flags & PA_SOURCE_NETWORK);
396
397     pa_assert_not_reached();
398 }
399
400 static pa_hook_result_t device_new_or_changed_cb(pa_core *c, pa_object *o, struct userdata *u) {
401     pa_assert(c);
402     pa_object_assert_ref(o);
403
404     if (!shall_ignore(o))
405         publish_service(get_service(u, o));
406
407     return PA_HOOK_OK;
408 }
409
410 static pa_hook_result_t device_unlink_cb(pa_core *c, pa_object *o, struct userdata *u) {
411     struct service *s;
412
413     pa_assert(c);
414     pa_object_assert_ref(o);
415
416     if ((s = pa_hashmap_remove(u->services, o)))
417         service_free(s);
418
419     return PA_HOOK_OK;
420 }
421
422 static int publish_main_service(struct userdata *u);
423
424 static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
425     struct userdata *u = userdata;
426     pa_assert(u);
427
428     switch (state) {
429
430         case AVAHI_ENTRY_GROUP_ESTABLISHED:
431             pa_log_info("Successfully established main service.");
432             break;
433
434         case AVAHI_ENTRY_GROUP_COLLISION: {
435             char *t;
436
437             t = avahi_alternative_service_name(u->service_name);
438             pa_log_info("Name collision: renaming main service %s to %s.", u->service_name, t);
439             pa_xfree(u->service_name);
440             u->service_name = t;
441
442             publish_main_service(u);
443             break;
444         }
445
446         case AVAHI_ENTRY_GROUP_FAILURE: {
447             pa_log("Failed to register main service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
448
449             avahi_entry_group_free(g);
450             u->main_entry_group = NULL;
451             break;
452         }
453
454         case AVAHI_ENTRY_GROUP_UNCOMMITED:
455         case AVAHI_ENTRY_GROUP_REGISTERING:
456             break;
457     }
458 }
459
460 static int publish_main_service(struct userdata *u) {
461     AvahiStringList *txt = NULL;
462     int r = -1;
463
464     pa_assert(u);
465
466     if (!u->main_entry_group) {
467         if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
468             pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
469             goto fail;
470         }
471     } else
472         avahi_entry_group_reset(u->main_entry_group);
473
474     txt = txt_record_server_data(u->core, txt);
475
476     if (avahi_entry_group_add_service_strlst(
477                 u->main_entry_group,
478                 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
479                 0,
480                 u->service_name,
481                 SERVICE_TYPE_SERVER,
482                 NULL,
483                 NULL,
484                 compute_port(u),
485                 txt) < 0) {
486
487         pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
488         goto fail;
489     }
490
491     if (avahi_entry_group_commit(u->main_entry_group) < 0) {
492         pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
493         goto fail;
494     }
495
496     r = 0;
497
498 fail:
499     avahi_string_list_free(txt);
500
501     return r;
502 }
503
504 static int publish_all_services(struct userdata *u) {
505     pa_sink *sink;
506     pa_source *source;
507     int r = -1;
508     uint32_t idx;
509
510     pa_assert(u);
511
512     pa_log_debug("Publishing services in Zeroconf");
513
514     for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx)))
515         if (!shall_ignore(PA_OBJECT(sink)))
516             publish_service(get_service(u, PA_OBJECT(sink)));
517
518     for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx)))
519         if (!shall_ignore(PA_OBJECT(source)))
520             publish_service(get_service(u, PA_OBJECT(source)));
521
522     if (publish_main_service(u) < 0)
523         goto fail;
524
525     r = 0;
526
527 fail:
528     return r;
529 }
530
531 static void unpublish_all_services(struct userdata *u, pa_bool_t rem) {
532     void *state = NULL;
533     struct service *s;
534
535     pa_assert(u);
536
537     pa_log_debug("Unpublishing services in Zeroconf");
538
539     while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
540         if (s->entry_group) {
541             if (rem) {
542                 pa_log_debug("Removing entry group for %s.", s->service_name);
543                 avahi_entry_group_free(s->entry_group);
544                 s->entry_group = NULL;
545             } else {
546                 avahi_entry_group_reset(s->entry_group);
547                 pa_log_debug("Resetting entry group for %s.", s->service_name);
548             }
549         }
550     }
551
552     if (u->main_entry_group) {
553         if (rem) {
554             pa_log_debug("Removing main entry group.");
555             avahi_entry_group_free(u->main_entry_group);
556             u->main_entry_group = NULL;
557         } else {
558             avahi_entry_group_reset(u->main_entry_group);
559             pa_log_debug("Resetting main entry group.");
560         }
561     }
562 }
563
564 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
565     struct userdata *u = userdata;
566
567     pa_assert(c);
568     pa_assert(u);
569
570     u->client = c;
571
572     switch (state) {
573         case AVAHI_CLIENT_S_RUNNING:
574             publish_all_services(u);
575             break;
576
577         case AVAHI_CLIENT_S_COLLISION:
578             pa_log_debug("Host name collision");
579             unpublish_all_services(u, FALSE);
580             break;
581
582         case AVAHI_CLIENT_FAILURE:
583             if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
584                 int error;
585
586                 pa_log_debug("Avahi daemon disconnected.");
587
588                 unpublish_all_services(u, TRUE);
589                 avahi_client_free(u->client);
590
591                 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
592                     pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
593                     pa_module_unload_request(u->module, TRUE);
594                 }
595             }
596
597             break;
598
599         default: ;
600     }
601 }
602
603 int pa__init(pa_module*m) {
604
605     struct userdata *u;
606     pa_modargs *ma = NULL;
607     char *hn, *un;
608     int error;
609
610     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
611         pa_log("Failed to parse module arguments.");
612         goto fail;
613     }
614
615     m->userdata = u = pa_xnew(struct userdata, 1);
616     u->core = m->core;
617     u->module = m;
618     u->native = pa_native_protocol_get(u->core);
619
620     u->avahi_poll = pa_avahi_poll_new(m->core->mainloop);
621
622     u->services = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
623
624     u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
625     u->sink_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
626     u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) device_unlink_cb, u);
627     u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
628     u->source_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
629     u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) device_unlink_cb, u);
630
631     u->main_entry_group = NULL;
632
633     un = pa_get_user_name_malloc();
634     hn = pa_get_host_name_malloc();
635     u->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s", un, hn), AVAHI_LABEL_MAX-1);
636     pa_xfree(un);
637     pa_xfree(hn);
638
639     if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
640         pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
641         goto fail;
642     }
643
644     pa_modargs_free(ma);
645
646     return 0;
647
648 fail:
649     pa__done(m);
650
651     if (ma)
652         pa_modargs_free(ma);
653
654     return -1;
655 }
656
657 void pa__done(pa_module*m) {
658     struct userdata*u;
659     pa_assert(m);
660
661     if (!(u = m->userdata))
662         return;
663
664     if (u->services)
665         pa_hashmap_free(u->services, (pa_free_cb_t) service_free);
666
667     if (u->sink_new_slot)
668         pa_hook_slot_free(u->sink_new_slot);
669     if (u->source_new_slot)
670         pa_hook_slot_free(u->source_new_slot);
671     if (u->sink_changed_slot)
672         pa_hook_slot_free(u->sink_changed_slot);
673     if (u->source_changed_slot)
674         pa_hook_slot_free(u->source_changed_slot);
675     if (u->sink_unlink_slot)
676         pa_hook_slot_free(u->sink_unlink_slot);
677     if (u->source_unlink_slot)
678         pa_hook_slot_free(u->source_unlink_slot);
679
680     if (u->main_entry_group)
681         avahi_entry_group_free(u->main_entry_group);
682
683     if (u->client)
684         avahi_client_free(u->client);
685
686     if (u->avahi_poll)
687         pa_avahi_poll_free(u->avahi_poll);
688
689     if (u->native)
690         pa_native_protocol_unref(u->native);
691
692     pa_xfree(u->service_name);
693     pa_xfree(u);
694 }