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