]> git.meshlink.io Git - catta/blob - avahi-compat-howl/compat.c
* fix oid calculation
[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 sw_result sw_discovery_browse_domains(
528     sw_discovery self,
529     sw_uint32 interface_index,
530     sw_discovery_browse_reply reply,
531     sw_opaque extra,
532     sw_discovery_oid * oid) {
533     AVAHI_WARN_UNSUPPORTED;
534     return SW_E_NO_IMPL;
535 }
536
537 static void service_resolver_callback(
538     AvahiServiceResolver *r,
539     AvahiIfIndex interface,
540     AvahiProtocol protocol,
541     AvahiResolverEvent event,
542     const char *name,
543     const char *type,
544     const char *domain,
545     const char *host_name,
546     const AvahiAddress *a,
547     uint16_t port,
548     AvahiStringList *txt,
549     AvahiLookupResultFlags flags,
550     void *userdata) {
551
552     oid_data* data = userdata;
553     sw_discovery_resolve_reply reply;
554     
555     assert(r);
556     assert(data);
557
558     reply = (sw_discovery_resolve_reply) data->reply;
559
560     switch (event) {
561         case AVAHI_RESOLVER_FOUND: {
562
563             char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
564             uint8_t *p = NULL;
565             size_t l = 0;
566             sw_ipv4_address addr;
567
568             sw_ipv4_address_init_from_saddr(&addr, a->data.ipv4.address);
569
570             host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
571             
572             if ((p = avahi_new0(uint8_t, (l = avahi_string_list_serialize(txt, NULL, 0))+1)))
573                 avahi_string_list_serialize(txt, p, l);
574
575             reply(data->discovery, OID_GET_INDEX(data), interface, name, type, domain, addr, port, p, l, data->extra);
576
577             avahi_free(p);
578             break;
579         }
580
581         case AVAHI_RESOLVER_FAILURE:
582
583             /* Apparently there is no way in HOWL to inform about failed resolving ... */
584
585             break;
586     }
587 }
588
589 sw_result sw_discovery_resolve(
590     sw_discovery self,
591     sw_uint32 interface_index,
592     sw_const_string name,
593     sw_const_string type,
594     sw_const_string domain,
595     sw_discovery_resolve_reply reply,
596     sw_opaque extra,
597     sw_discovery_oid * oid) {
598
599     oid_data *data;
600     AvahiIfIndex ifindex;
601     sw_result result = SW_E_UNKNOWN;
602     
603     assert(self);
604     assert(name);
605     assert(type);
606     assert(reply);
607     assert(oid);
608     
609     AVAHI_WARN_LINKAGE;
610
611     if ((*oid = oid_alloc(self, OID_SERVICE_RESOLVER)) == (sw_discovery_oid) -1)
612         return SW_E_UNKNOWN;
613
614     data = oid_get(self, *oid);
615     assert(data);
616     data->reply = (sw_result (*)(void)) reply;
617     data->extra = extra;
618     
619     ifindex = interface_index == 0 ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface_index;
620
621     ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
622     
623     if (!(data->object = avahi_service_resolver_new(self->client, ifindex, AVAHI_PROTO_INET, name, type, domain, AVAHI_PROTO_INET, 0, service_resolver_callback, data))) {
624         result = map_error(avahi_client_errno(self->client));
625         goto finish;
626     }
627
628     result = SW_OKAY;
629     
630 finish:
631
632     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
633     
634     if (result != SW_OKAY)
635         if (*oid != (sw_discovery_oid) -1)
636             oid_release(self, *oid);
637
638     return result;
639 }
640
641 static void service_browser_callback(
642     AvahiServiceBrowser *b,
643     AvahiIfIndex interface,
644     AvahiProtocol protocol,
645     AvahiBrowserEvent event,
646     const char *name,
647     const char *type,
648     const char *domain,
649     AvahiLookupResultFlags flags,
650     void *userdata) {
651
652     oid_data* data = userdata;
653     char type_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
654     sw_discovery_browse_reply reply;
655     
656     assert(b);
657     assert(data);
658
659     reply = (sw_discovery_browse_reply) data->reply;
660
661     type = add_trailing_dot(type, type_fixed, sizeof(type_fixed));
662     domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
663
664     switch (event) {
665         case AVAHI_BROWSER_NEW:
666             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_ADD_SERVICE, interface, name, type, domain, data->extra);
667             break;
668
669         case AVAHI_BROWSER_REMOVE:
670             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_REMOVE_SERVICE, interface, name, type, domain, data->extra);
671             break;
672
673         case AVAHI_BROWSER_FAILURE:
674             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_INVALID, interface, name, type, domain, data->extra);
675             break;
676             
677         case AVAHI_BROWSER_CACHE_EXHAUSTED:
678         case AVAHI_BROWSER_ALL_FOR_NOW:
679             break;
680     }
681 }
682
683 sw_result sw_discovery_browse(
684     sw_discovery self,
685     sw_uint32 interface_index,
686     sw_const_string type,
687     sw_const_string domain,
688     sw_discovery_browse_reply reply,
689     sw_opaque extra,
690     sw_discovery_oid * oid) {
691
692     oid_data *data;
693     AvahiIfIndex ifindex;
694     sw_result result = SW_E_UNKNOWN;
695     
696     assert(self);
697     assert(type);
698     assert(reply);
699     assert(oid);
700     
701     AVAHI_WARN_LINKAGE;
702
703     if ((*oid = oid_alloc(self, OID_SERVICE_BROWSER)) == (sw_discovery_oid) -1)
704         return SW_E_UNKNOWN;
705
706     data = oid_get(self, *oid);
707     assert(data);
708     data->reply = (sw_result (*)(void)) reply;
709     data->extra = extra;
710     
711     ifindex = interface_index == 0 ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface_index;
712
713     ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
714     
715     if (!(data->object = avahi_service_browser_new(self->client, ifindex, AVAHI_PROTO_INET, type, domain, 0, service_browser_callback, data))) {
716         result = map_error(avahi_client_errno(self->client));
717         goto finish;
718     }
719
720     result = SW_OKAY;
721     
722 finish:
723
724     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
725     
726     if (result != SW_OKAY)
727         if (*oid != (sw_discovery_oid) -1)
728             oid_release(self, *oid);
729
730     return result;
731 }
732
733 sw_result sw_discovery_cancel(sw_discovery self, sw_discovery_oid oid) {
734     oid_data *data;
735     assert(self);                             
736
737     AVAHI_WARN_LINKAGE;
738
739     if (!(data = oid_get(self, oid)))
740         return SW_E_UNKNOWN;
741
742     switch (data->type) {
743         case OID_SERVICE_BROWSER:
744             avahi_service_browser_free(data->object);
745             break;
746
747         case OID_SERVICE_RESOLVER:
748             avahi_service_resolver_free(data->object);
749             break;
750
751         case OID_UNUSED:
752             ;
753     }
754
755     oid_release(self, oid);
756     
757     return SW_OKAY;
758 }