]> git.meshlink.io Git - catta/blob - avahi-compat-howl/compat.c
Minor cleanups
[catta] / avahi-compat-howl / compat.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 <assert.h>
27
28 #include <pthread.h>
29
30 #include <avahi-common/strlst.h>
31 #include <avahi-common/malloc.h>
32 #include <avahi-common/domain.h>
33 #include <avahi-common/simple-watch.h>
34 #include <avahi-common/error.h>
35 #include <avahi-client/client.h>
36
37 #include "howl.h"
38 #include "warn.h"
39
40 #define OID_MAX 50
41
42 enum {
43     COMMAND_POLL = 'p',
44     COMMAND_QUIT = 'q',
45     COMMAND_POLL_DONE = 'P'
46 };
47
48 typedef enum {
49     OID_UNUSED = 0,
50     OID_SERVICE_BROWSER,
51     OID_SERVICE_RESOLVER,
52     OID_DOMAIN_BROWSER,
53     OID_ENTRY_GROUP
54 } oid_type;
55
56 typedef struct oid_data {
57     oid_type type;
58     sw_opaque extra;
59     sw_discovery discovery;
60     void *object;
61     sw_result (*reply)(void);
62 } oid_data;
63
64 struct _sw_discovery {
65     int n_ref;
66     AvahiSimplePoll *simple_poll;
67     AvahiClient *client;
68
69     oid_data oid_table[OID_MAX];
70     sw_discovery_oid oid_index;
71
72     int thread_fd, main_fd;
73     
74     pthread_t thread;
75     int thread_running;
76
77     pthread_mutex_t mutex;
78
79 };
80
81 #define ASSERT_SUCCESS(r) { int __ret = (r); assert(__ret == 0); }
82
83 #define OID_GET_INDEX(data) ((sw_discovery_oid) (((data) - ((data)->discovery->oid_table))))
84
85 static const char *add_trailing_dot(const char *s, char *buf, size_t buf_len) {
86     if (!s)
87         return NULL;
88
89     if (*s == 0)
90         return s;
91
92     if (s[strlen(s)-1] == '.')
93         return s;
94
95     snprintf(buf, buf_len, "%s.", s);
96     return buf;
97 }
98
99 static sw_result map_error(int error) {
100     switch (error) {
101         case AVAHI_OK:
102             return SW_OKAY;
103             
104         case AVAHI_ERR_NO_MEMORY:
105             return SW_E_MEM;
106     }
107
108     return SW_E_UNKNOWN;
109 }
110
111 static int read_command(int fd) {
112     ssize_t r;
113     char command;
114
115     assert(fd >= 0);
116     
117     if ((r = read(fd, &command, 1)) != 1) {
118         fprintf(stderr, __FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
119         return -1;
120     }
121
122     return command;
123 }
124
125 static int write_command(int fd, char reply) {
126     assert(fd >= 0);
127
128     if (write(fd, &reply, 1) != 1) {
129         fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
130         return -1;
131     }
132
133     return 0;
134 }
135
136 static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
137     sw_discovery self = userdata;
138     int ret;
139     
140     assert(self);
141     
142     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
143     ret = poll(ufds, nfds, timeout);
144     ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
145
146     return ret;
147 }
148
149 static void * thread_func(void *data) {
150     sw_discovery self = data;
151     sigset_t mask;
152
153     sigfillset(&mask);
154     pthread_sigmask(SIG_BLOCK, &mask, NULL);
155     
156     self->thread = pthread_self();
157     self->thread_running = 1;
158
159     for (;;) {
160         char command;
161
162         if ((command = read_command(self->thread_fd)) < 0)
163             break;
164
165 /*         fprintf(stderr, "Command: %c\n", command); */
166         
167         switch (command) {
168
169             case COMMAND_POLL:
170
171                 ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
172                 
173                 if (avahi_simple_poll_run(self->simple_poll) < 0) {
174                     fprintf(stderr, __FILE__": avahi_simple_poll_run() failed.\n");
175                     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
176                     break;
177                 }
178
179                 ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
180                 
181                 if (write_command(self->thread_fd, COMMAND_POLL_DONE) < 0)
182                     break;
183                 
184                 break;
185
186             case COMMAND_QUIT:
187                 return NULL;
188         }
189         
190     }
191
192     return NULL;
193 }
194
195 static int oid_alloc(sw_discovery self, oid_type type) {
196     sw_discovery_oid i;
197     assert(self);
198
199     for (i = 0; i < OID_MAX; i++) {
200
201         while (self->oid_index >= OID_MAX)
202             self->oid_index -= OID_MAX;
203         
204         if (self->oid_table[self->oid_index].type == OID_UNUSED) {
205             self->oid_table[self->oid_index].type = type;
206             self->oid_table[self->oid_index].discovery = self;
207
208             assert(OID_GET_INDEX(&self->oid_table[self->oid_index]) == self->oid_index);
209             
210             return self->oid_index ++;
211         }
212
213         self->oid_index ++;
214     }
215
216     /* No free entry found */
217     
218     return (sw_discovery_oid) -1;
219 }
220
221 static void oid_release(sw_discovery self, sw_discovery_oid oid) {
222     assert(self);
223     assert(oid < OID_MAX);
224
225     assert(self->oid_table[oid].type != OID_UNUSED);
226
227     self->oid_table[oid].type = OID_UNUSED;
228     self->oid_table[oid].discovery = NULL;
229     self->oid_table[oid].reply = NULL;
230     self->oid_table[oid].object = NULL;
231     self->oid_table[oid].extra = NULL;
232 }
233
234 static oid_data* oid_get(sw_discovery self, sw_discovery_oid oid) {
235     assert(self);
236
237     if (oid >= OID_MAX)
238         return NULL;
239
240     if (self->oid_table[oid].type == OID_UNUSED)
241         return NULL;
242     
243     return &self->oid_table[oid];
244 }
245
246 sw_result sw_discovery_init(sw_discovery * self) {
247     int fd[2] = { -1, -1};
248     sw_result result = SW_E_UNKNOWN;
249     pthread_mutexattr_t mutex_attr;
250     int error;
251     
252     assert(self);
253     
254     AVAHI_WARN_LINKAGE;
255
256     *self = NULL;
257
258     if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0)
259         goto fail;
260     
261     if (!(*self = avahi_new(struct _sw_discovery, 1))) {
262         result = SW_E_MEM;
263         goto fail;
264     }
265
266     (*self)->n_ref = 1;
267     (*self)->thread_fd = fd[0];
268     (*self)->main_fd = fd[1];
269
270     (*self)->client = NULL;
271     (*self)->simple_poll = NULL;
272
273     memset((*self)->oid_table, 0, sizeof((*self)->oid_table));
274     (*self)->oid_index = 0;
275     
276     (*self)->thread_running = 0;
277
278     ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr));
279     pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
280     ASSERT_SUCCESS(pthread_mutex_init(&(*self)->mutex, &mutex_attr));
281
282     if (!((*self)->simple_poll = avahi_simple_poll_new()))
283         goto fail;
284
285     avahi_simple_poll_set_func((*self)->simple_poll, poll_func, *self);
286
287     if (!((*self)->client = avahi_client_new(avahi_simple_poll_get((*self)->simple_poll), NULL, *self, &error))) {
288         result = map_error(error);
289         goto fail;
290     }
291     
292     /* Start simple poll */
293     if (avahi_simple_poll_prepare((*self)->simple_poll, -1) < 0)
294         goto fail;
295
296     /* Queue an initial POLL command for the thread */
297     if (write_command((*self)->main_fd, COMMAND_POLL) < 0)
298         goto fail;
299     
300     if (pthread_create(&(*self)->thread, NULL, thread_func, *self) != 0)
301         goto fail;
302
303     (*self)->thread_running = 1;
304     
305     return SW_OKAY;
306
307 fail:
308
309     if (*self)
310         sw_discovery_fina(*self);
311
312     return result;
313 }
314
315 static int stop_thread(sw_discovery self) {
316     assert(self);
317
318     if (!self->thread_running)
319         return 0;
320
321     if (write_command(self->main_fd, COMMAND_QUIT) < 0)
322         return -1;
323     
324     avahi_simple_poll_wakeup(self->simple_poll);
325     
326     ASSERT_SUCCESS(pthread_join(self->thread, NULL));
327     self->thread_running = 0;
328     return 0;
329 }
330
331 static sw_discovery discover_ref(sw_discovery self) {
332     assert(self);
333     assert(self->n_ref >= 1);
334
335     self->n_ref++;
336
337     return self;
338 }
339
340 static void discover_unref(sw_discovery self) {
341     assert(self);
342     assert(self->n_ref >= 1);
343
344     if (--self->n_ref > 0)
345         return;
346
347     stop_thread(self);
348
349     if (self->client)
350         avahi_client_free(self->client);
351
352     if (self->simple_poll)
353         avahi_simple_poll_free(self->simple_poll);
354
355     if (self->thread_fd >= 0)
356         close(self->thread_fd);
357
358     if (self->main_fd >= 0)
359         close(self->main_fd);
360
361     ASSERT_SUCCESS(pthread_mutex_destroy(&self->mutex));
362     
363     avahi_free(self);
364 }
365
366 sw_result sw_discovery_fina(sw_discovery self) {
367     assert(self);
368     
369     AVAHI_WARN_LINKAGE;
370
371     stop_thread(self);
372     discover_unref(self);
373     
374     return SW_OKAY;
375 }
376
377 sw_result sw_discovery_run(sw_discovery self) {
378     assert(self);
379     
380     AVAHI_WARN_LINKAGE;
381
382     return sw_salt_run((sw_salt) self);
383 }
384
385 sw_result sw_discovery_stop_run(sw_discovery self) {
386     assert(self);
387     
388     AVAHI_WARN_LINKAGE;
389
390     return sw_salt_stop_run((sw_salt) self);
391 }
392
393 int sw_discovery_socket(sw_discovery self) {
394     assert(self);
395     
396     AVAHI_WARN_LINKAGE;
397
398     return self->main_fd;
399 }
400
401 sw_result sw_discovery_read_socket(sw_discovery self) {
402     sw_result result = SW_E_UNKNOWN;
403     
404     assert(self);
405
406     discover_ref(self);
407
408     ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
409     
410     /* Cleanup notification socket */
411     if (read_command(self->main_fd) != COMMAND_POLL_DONE)
412         goto finish;
413     
414     if (avahi_simple_poll_dispatch(self->simple_poll) < 0)
415         goto finish;
416
417     if (self->n_ref > 1) /* Perhaps we should die */
418
419         /* Dispatch events */
420         if (avahi_simple_poll_prepare(self->simple_poll, -1) < 0)
421             goto finish;
422
423     if (self->n_ref > 1)
424
425         /* Request the poll */
426         if (write_command(self->main_fd, COMMAND_POLL) < 0)
427             goto finish;
428     
429     result = SW_OKAY;
430     
431 finish:
432
433     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
434
435     discover_unref(self);
436     
437     return result;
438 }
439
440 sw_result sw_discovery_salt(sw_discovery self, sw_salt *salt) {
441     assert(self);
442     assert(salt);
443     
444     AVAHI_WARN_LINKAGE;
445
446     *salt = (sw_salt) self;
447     
448     return SW_OKAY;
449 }
450
451 sw_result sw_salt_step(sw_salt self, sw_uint32 * msec) {
452     struct pollfd p;
453     int r;
454     sw_result result;
455
456     AVAHI_WARN_LINKAGE;
457
458     if (!((sw_discovery) self)->thread_running)
459         return SW_E_UNKNOWN;
460     
461     memset(&p, 0, sizeof(p));
462     p.fd = ((sw_discovery) self)->main_fd;
463     p.events = POLLIN;
464
465     if ((r = poll(&p, 1, msec ? (int) *msec : -1)) < 0) {
466         
467         /* Don't treat EINTR as error */
468         if (errno == EINTR)
469             return SW_OKAY;
470         
471         return SW_E_UNKNOWN;
472         
473     } else if (r == 0) {
474         
475         /* Timeoout */
476         return SW_OKAY;
477
478     } else {
479         /* Success */
480     
481         if (p.revents != POLLIN)
482             return SW_E_UNKNOWN;
483
484         if ((result = sw_discovery_read_socket((sw_discovery) self)) != SW_OKAY)
485             return result;
486     }
487     
488     return SW_OKAY;
489 }
490
491 sw_result sw_salt_run(sw_salt self) {
492     sw_result ret;
493     
494     AVAHI_WARN_LINKAGE;
495     
496     for (;;)
497         if ((ret = sw_salt_step(self, NULL)) != SW_OKAY)
498             return ret;
499 }
500
501 sw_result sw_salt_stop_run(sw_salt self) {
502     AVAHI_WARN_LINKAGE;
503
504     if (stop_thread((sw_discovery) self) < 0)
505         return SW_E_UNKNOWN;
506
507     return SW_OKAY;
508 }
509
510 sw_result sw_discovery_publish(
511     sw_discovery self,
512     sw_uint32 interface_index,
513     sw_const_string name,
514     sw_const_string type,
515     sw_const_string domain,
516     sw_const_string host,
517     sw_port port,
518     sw_octets text_record,
519     sw_uint32 text_record_len,
520     sw_discovery_publish_reply reply,
521     sw_opaque extra,
522     sw_discovery_oid * oid) {
523     AVAHI_WARN_UNSUPPORTED;
524     return SW_E_NO_IMPL;
525 }
526
527 static void domain_browser_callback(
528     AvahiDomainBrowser *b,
529     AvahiIfIndex interface,
530     AvahiProtocol protocol,
531     AvahiBrowserEvent event,
532     const char *domain,
533     AvahiLookupResultFlags flags,
534     void *userdata) {
535
536     oid_data* data = userdata;
537     sw_discovery_browse_reply reply;
538     static char domain_fixed[AVAHI_DOMAIN_NAME_MAX];
539
540     assert(b);
541     assert(data);
542
543     reply = (sw_discovery_browse_reply) data->reply;
544
545     domain  = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
546
547     switch (event) {
548         case AVAHI_BROWSER_NEW:
549             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_ADD_DOMAIN, interface, NULL, NULL, domain, data->extra);
550             break;
551
552         case AVAHI_BROWSER_REMOVE:
553             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_REMOVE_DOMAIN, interface, NULL, NULL, domain, data->extra);
554             break;
555
556         case AVAHI_BROWSER_FAILURE:
557             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_INVALID, interface, NULL, NULL, domain, data->extra);
558             break;
559             
560         case AVAHI_BROWSER_CACHE_EXHAUSTED:
561         case AVAHI_BROWSER_ALL_FOR_NOW:
562             break;
563     }
564 }
565
566 sw_result sw_discovery_browse_domains(
567     sw_discovery self,
568     sw_uint32 interface_index,
569     sw_discovery_browse_reply reply,
570     sw_opaque extra,
571     sw_discovery_oid * oid) {
572     
573     oid_data *data;
574     AvahiIfIndex ifindex;
575     sw_result result = SW_E_UNKNOWN;
576     
577     assert(self);
578     assert(reply);
579     assert(oid);
580     
581     AVAHI_WARN_LINKAGE;
582
583     if ((*oid = oid_alloc(self, OID_DOMAIN_BROWSER)) == (sw_discovery_oid) -1)
584         return SW_E_UNKNOWN;
585
586     data = oid_get(self, *oid);
587     assert(data);
588     data->reply = (sw_result (*)(void)) reply;
589     data->extra = extra;
590     
591     ifindex = interface_index == 0 ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface_index;
592
593     ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
594     
595     if (!(data->object = avahi_domain_browser_new(self->client, ifindex, AVAHI_PROTO_INET, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, 0, domain_browser_callback, data))) {
596         result = map_error(avahi_client_errno(self->client));
597         goto finish;
598     }
599
600     result = SW_OKAY;
601
602 finish:
603
604     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
605     
606     if (result != SW_OKAY)
607         if (*oid != (sw_discovery_oid) -1)
608             oid_release(self, *oid);
609
610     return result;
611 }
612
613 static void service_resolver_callback(
614     AvahiServiceResolver *r,
615     AvahiIfIndex interface,
616     AvahiProtocol protocol,
617     AvahiResolverEvent event,
618     const char *name,
619     const char *type,
620     const char *domain,
621     const char *host_name,
622     const AvahiAddress *a,
623     uint16_t port,
624     AvahiStringList *txt,
625     AvahiLookupResultFlags flags,
626     void *userdata) {
627
628     oid_data* data = userdata;
629     sw_discovery_resolve_reply reply;
630     
631     assert(r);
632     assert(data);
633
634     reply = (sw_discovery_resolve_reply) data->reply;
635
636     switch (event) {
637         case AVAHI_RESOLVER_FOUND: {
638
639             char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
640             uint8_t *p = NULL;
641             size_t l = 0;
642             sw_ipv4_address addr;
643
644             sw_ipv4_address_init_from_saddr(&addr, a->data.ipv4.address);
645
646             host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
647             
648             if ((p = avahi_new0(uint8_t, (l = avahi_string_list_serialize(txt, NULL, 0))+1)))
649                 avahi_string_list_serialize(txt, p, l);
650
651             reply(data->discovery, OID_GET_INDEX(data), interface, name, type, domain, addr, port, p, l, data->extra);
652
653             avahi_free(p);
654             break;
655         }
656
657         case AVAHI_RESOLVER_FAILURE:
658
659             /* Apparently there is no way in HOWL to inform about failed resolvings ... */
660             
661             avahi_warn("A service failed to resolve in the HOWL compatiblity layer of Avahi which is used by '%s'. "
662                        "Since the HOWL API doesn't offer any means to inform the application about this, we have to ignore the failure. "
663                        "Please fix your application to use the native API of Avahi!",
664                        avahi_exe_name());
665
666             break;
667     }
668 }
669
670 sw_result sw_discovery_resolve(
671     sw_discovery self,
672     sw_uint32 interface_index,
673     sw_const_string name,
674     sw_const_string type,
675     sw_const_string domain,
676     sw_discovery_resolve_reply reply,
677     sw_opaque extra,
678     sw_discovery_oid * oid) {
679
680     oid_data *data;
681     AvahiIfIndex ifindex;
682     sw_result result = SW_E_UNKNOWN;
683     
684     assert(self);
685     assert(name);
686     assert(type);
687     assert(reply);
688     assert(oid);
689     
690     AVAHI_WARN_LINKAGE;
691
692     if ((*oid = oid_alloc(self, OID_SERVICE_RESOLVER)) == (sw_discovery_oid) -1)
693         return SW_E_UNKNOWN;
694
695     data = oid_get(self, *oid);
696     assert(data);
697     data->reply = (sw_result (*)(void)) reply;
698     data->extra = extra;
699     
700     ifindex = interface_index == 0 ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface_index;
701
702     ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
703     
704     if (!(data->object = avahi_service_resolver_new(self->client, ifindex, AVAHI_PROTO_INET, name, type, domain, AVAHI_PROTO_INET, 0, service_resolver_callback, data))) {
705         result = map_error(avahi_client_errno(self->client));
706         goto finish;
707     }
708
709     result = SW_OKAY;
710     
711 finish:
712
713     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
714     
715     if (result != SW_OKAY)
716         if (*oid != (sw_discovery_oid) -1)
717             oid_release(self, *oid);
718
719     return result;
720 }
721
722 static void service_browser_callback(
723     AvahiServiceBrowser *b,
724     AvahiIfIndex interface,
725     AvahiProtocol protocol,
726     AvahiBrowserEvent event,
727     const char *name,
728     const char *type,
729     const char *domain,
730     AvahiLookupResultFlags flags,
731     void *userdata) {
732
733     oid_data* data = userdata;
734     char type_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
735     sw_discovery_browse_reply reply;
736     
737     assert(b);
738     assert(data);
739
740     reply = (sw_discovery_browse_reply) data->reply;
741
742     type = add_trailing_dot(type, type_fixed, sizeof(type_fixed));
743     domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
744
745     switch (event) {
746         case AVAHI_BROWSER_NEW:
747             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_ADD_SERVICE, interface, name, type, domain, data->extra);
748             break;
749
750         case AVAHI_BROWSER_REMOVE:
751             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_REMOVE_SERVICE, interface, name, type, domain, data->extra);
752             break;
753
754         case AVAHI_BROWSER_FAILURE:
755             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_INVALID, interface, name, type, domain, data->extra);
756             break;
757             
758         case AVAHI_BROWSER_CACHE_EXHAUSTED:
759         case AVAHI_BROWSER_ALL_FOR_NOW:
760             break;
761     }
762 }
763
764 sw_result sw_discovery_browse(
765     sw_discovery self,
766     sw_uint32 interface_index,
767     sw_const_string type,
768     sw_const_string domain,
769     sw_discovery_browse_reply reply,
770     sw_opaque extra,
771     sw_discovery_oid * oid) {
772
773     oid_data *data;
774     AvahiIfIndex ifindex;
775     sw_result result = SW_E_UNKNOWN;
776     
777     assert(self);
778     assert(type);
779     assert(reply);
780     assert(oid);
781     
782     AVAHI_WARN_LINKAGE;
783
784     if ((*oid = oid_alloc(self, OID_SERVICE_BROWSER)) == (sw_discovery_oid) -1)
785         return SW_E_UNKNOWN;
786
787     data = oid_get(self, *oid);
788     assert(data);
789     data->reply = (sw_result (*)(void)) reply;
790     data->extra = extra;
791     
792     ifindex = interface_index == 0 ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface_index;
793
794     ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
795     
796     if (!(data->object = avahi_service_browser_new(self->client, ifindex, AVAHI_PROTO_INET, type, domain, 0, service_browser_callback, data))) {
797         result = map_error(avahi_client_errno(self->client));
798         goto finish;
799     }
800
801     result = SW_OKAY;
802     
803 finish:
804
805     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
806     
807     if (result != SW_OKAY)
808         if (*oid != (sw_discovery_oid) -1)
809             oid_release(self, *oid);
810
811     return result;
812 }
813
814 sw_result sw_discovery_cancel(sw_discovery self, sw_discovery_oid oid) {
815     oid_data *data;
816     assert(self);                             
817
818     AVAHI_WARN_LINKAGE;
819
820     if (!(data = oid_get(self, oid)))
821         return SW_E_UNKNOWN;
822
823     switch (data->type) {
824         case OID_SERVICE_BROWSER:
825             avahi_service_browser_free(data->object);
826             break;
827
828         case OID_SERVICE_RESOLVER:
829             avahi_service_resolver_free(data->object);
830             break;
831
832         case OID_DOMAIN_BROWSER:
833             avahi_domain_browser_free(data->object);
834             break;
835
836         case OID_ENTRY_GROUP:
837             avahi_entry_group_free(data->object);
838             break;
839             
840         case OID_UNUSED:
841             ;
842     }
843
844     oid_release(self, oid);
845     
846     return SW_OKAY;
847 }