]> git.meshlink.io Git - catta/blob - avahi-compat-howl/compat.c
* implement compat-howl core
[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 static const char *add_trailing_dot(const char *s, char *buf, size_t buf_len) {
84     if (!s)
85         return NULL;
86
87     if (*s == 0)
88         return s;
89
90     if (s[strlen(s)-1] == '.')
91         return s;
92
93     snprintf(buf, buf_len, "%s.", s);
94     return buf;
95 }
96
97 static sw_result map_error(int error) {
98     switch (error) {
99         case AVAHI_OK:
100             return SW_OKAY;
101             
102         case AVAHI_ERR_NO_MEMORY:
103             return SW_E_MEM;
104     }
105
106     return SW_E_UNKNOWN;
107 }
108
109 static int read_command(int fd) {
110     ssize_t r;
111     char command;
112
113     assert(fd >= 0);
114     
115     if ((r = read(fd, &command, 1)) != 1) {
116         fprintf(stderr, __FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
117         return -1;
118     }
119
120     return command;
121 }
122
123 static int write_command(int fd, char reply) {
124     assert(fd >= 0);
125
126     if (write(fd, &reply, 1) != 1) {
127         fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
128         return -1;
129     }
130
131     return 0;
132 }
133
134 static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
135     sw_discovery self = userdata;
136     int ret;
137     
138     assert(self);
139     
140     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
141     ret = poll(ufds, nfds, timeout);
142     ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
143
144     return ret;
145 }
146
147 static void * thread_func(void *data) {
148     sw_discovery self = data;
149     sigset_t mask;
150
151     sigfillset(&mask);
152     pthread_sigmask(SIG_BLOCK, &mask, NULL);
153     
154     self->thread = pthread_self();
155     self->thread_running = 1;
156
157     for (;;) {
158         char command;
159
160         if ((command = read_command(self->thread_fd)) < 0)
161             break;
162
163 /*         fprintf(stderr, "Command: %c\n", command); */
164         
165         switch (command) {
166
167             case COMMAND_POLL:
168
169                 ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
170                 
171                 if (avahi_simple_poll_run(self->simple_poll) < 0) {
172                     fprintf(stderr, __FILE__": avahi_simple_poll_run() failed.\n");
173                     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
174                     break;
175                 }
176
177                 ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
178                 
179                 if (write_command(self->thread_fd, COMMAND_POLL_DONE) < 0)
180                     break;
181                 
182                 break;
183
184             case COMMAND_QUIT:
185                 return NULL;
186         }
187         
188     }
189
190     return NULL;
191 }
192
193 static int oid_alloc(sw_discovery self, oid_type type) {
194     sw_discovery_oid i;
195     assert(self);
196
197     for (i = 0; i < OID_MAX; i++) {
198
199         while (self->oid_index >= OID_MAX)
200             self->oid_index -= OID_MAX;
201         
202         if (self->oid_table[self->oid_index].type == OID_UNUSED) {
203             self->oid_table[self->oid_index].type = type;
204             self->oid_table[self->oid_index].discovery = self;
205             return self->oid_index ++;
206         }
207
208         self->oid_index ++;
209     }
210
211     /* No free entry found */
212     
213     return (sw_discovery_oid) -1;
214 }
215
216 static void oid_release(sw_discovery self, sw_discovery_oid oid) {
217     assert(self);
218     assert(oid < OID_MAX);
219
220     assert(self->oid_table[oid].type != OID_UNUSED);
221
222     self->oid_table[oid].type = OID_UNUSED;
223     self->oid_table[oid].discovery = NULL;
224     self->oid_table[oid].reply = NULL;
225     self->oid_table[oid].object = NULL;
226     self->oid_table[oid].extra = NULL;
227 }
228
229 static oid_data* oid_get(sw_discovery self, sw_discovery_oid oid) {
230     assert(self);
231
232     if (oid >= OID_MAX)
233         return NULL;
234
235     if (self->oid_table[oid].type == OID_UNUSED)
236         return NULL;
237     
238     return &self->oid_table[oid];
239 }
240
241 sw_result sw_discovery_init(sw_discovery * self) {
242     int fd[2] = { -1, -1};
243     sw_result result = SW_E_UNKNOWN;
244     pthread_mutexattr_t mutex_attr;
245     int error;
246     
247     assert(self);
248     
249     AVAHI_WARN_LINKAGE;
250
251     *self = NULL;
252
253     if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0)
254         goto fail;
255     
256     if (!(*self = avahi_new(struct _sw_discovery, 1))) {
257         result = SW_E_MEM;
258         goto fail;
259     }
260
261     (*self)->n_ref = 1;
262     (*self)->thread_fd = fd[0];
263     (*self)->main_fd = fd[1];
264
265     (*self)->client = NULL;
266     (*self)->simple_poll = NULL;
267
268     memset((*self)->oid_table, 0, sizeof((*self)->oid_table));
269     (*self)->oid_index = 0;
270     
271     (*self)->thread_running = 0;
272
273     ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr));
274     pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
275     ASSERT_SUCCESS(pthread_mutex_init(&(*self)->mutex, NULL));
276
277     if (!((*self)->simple_poll = avahi_simple_poll_new()))
278         goto fail;
279
280     avahi_simple_poll_set_func((*self)->simple_poll, poll_func, *self);
281
282     if (!((*self)->client = avahi_client_new(avahi_simple_poll_get((*self)->simple_poll), NULL, *self, &error))) {
283         result = map_error(error);
284         goto fail;
285     }
286     
287     /* Start simple poll */
288     if (avahi_simple_poll_prepare((*self)->simple_poll, -1) < 0)
289         goto fail;
290
291     /* Queue an initial POLL command for the thread */
292     if (write_command((*self)->main_fd, COMMAND_POLL) < 0)
293         goto fail;
294     
295     if (pthread_create(&(*self)->thread, NULL, thread_func, *self) != 0)
296         goto fail;
297
298     (*self)->thread_running = 1;
299     
300     return SW_OKAY;
301
302 fail:
303
304     if (*self)
305         sw_discovery_fina(*self);
306
307     return result;
308 }
309
310 static int stop_thread(sw_discovery self) {
311     assert(self);
312
313     if (!self->thread_running)
314         return 0;
315
316     if (write_command(self->main_fd, COMMAND_QUIT) < 0)
317         return -1;
318     
319     avahi_simple_poll_wakeup(self->simple_poll);
320     
321     ASSERT_SUCCESS(pthread_join(self->thread, NULL));
322     self->thread_running = 0;
323     return 0;
324 }
325
326 static sw_discovery discover_ref(sw_discovery self) {
327     assert(self);
328     assert(self->n_ref >= 1);
329
330     self->n_ref++;
331
332     return self;
333 }
334
335 static void discover_unref(sw_discovery self) {
336     assert(self);
337     assert(self->n_ref >= 1);
338
339     if (--self->n_ref > 0)
340         return;
341
342     stop_thread(self);
343
344     if (self->client)
345         avahi_client_free(self->client);
346
347     if (self->simple_poll)
348         avahi_simple_poll_free(self->simple_poll);
349
350     if (self->thread_fd >= 0)
351         close(self->thread_fd);
352
353     if (self->main_fd >= 0)
354         close(self->main_fd);
355
356     ASSERT_SUCCESS(pthread_mutex_destroy(&self->mutex));
357     
358     avahi_free(self);
359 }
360
361 sw_result sw_discovery_fina(sw_discovery self) {
362     assert(self);
363     
364     AVAHI_WARN_LINKAGE;
365
366     stop_thread(self);
367     discover_unref(self);
368     
369     return SW_OKAY;
370 }
371
372 sw_result sw_discovery_run(sw_discovery self) {
373     assert(self);
374     
375     AVAHI_WARN_LINKAGE;
376
377     return sw_salt_run((sw_salt) self);
378 }
379
380 sw_result sw_discovery_stop_run(sw_discovery self) {
381     assert(self);
382     
383     AVAHI_WARN_LINKAGE;
384
385     return sw_salt_stop_run((sw_salt) self);
386 }
387
388 int sw_discovery_socket(sw_discovery self) {
389     assert(self);
390     
391     AVAHI_WARN_LINKAGE;
392
393     return self->main_fd;
394 }
395
396 sw_result sw_discovery_read_socket(sw_discovery self) {
397     sw_result result = SW_E_UNKNOWN;
398     
399     assert(self);
400
401     discover_ref(self);
402
403     ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
404     
405     /* Cleanup notification socket */
406     if (read_command(self->main_fd) != COMMAND_POLL_DONE)
407         goto finish;
408     
409     if (avahi_simple_poll_dispatch(self->simple_poll) < 0)
410         goto finish;
411
412     if (self->n_ref > 1) /* Perhaps we should die */
413
414         /* Dispatch events */
415         if (avahi_simple_poll_prepare(self->simple_poll, -1) < 0)
416             goto finish;
417
418     if (self->n_ref > 1)
419
420         /* Request the poll */
421         if (write_command(self->main_fd, COMMAND_POLL) < 0)
422             goto finish;
423     
424     result = SW_OKAY;
425     
426 finish:
427
428     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
429
430     discover_unref(self);
431     
432     return result;
433 }
434
435 sw_result sw_discovery_salt(sw_discovery self, sw_salt *salt) {
436     assert(self);
437     assert(salt);
438     
439     AVAHI_WARN_LINKAGE;
440
441     *salt = (sw_salt) self;
442     
443     return SW_OKAY;
444 }
445
446 sw_result sw_salt_step(sw_salt self, sw_uint32 * msec) {
447     struct pollfd p;
448     int r;
449     sw_result result;
450
451     AVAHI_WARN_LINKAGE;
452
453     if (!((sw_discovery) self)->thread_running)
454         return SW_E_UNKNOWN;
455     
456     memset(&p, 0, sizeof(p));
457     p.fd = ((sw_discovery) self)->main_fd;
458     p.events = POLLIN;
459
460     if ((r = poll(&p, 1, msec ? (int) *msec : -1)) < 0) {
461         
462         /* Don't treat EINTR as error */
463         if (errno == EINTR)
464             return SW_OKAY;
465         
466         return SW_E_UNKNOWN;
467         
468     } else if (r == 0) {
469         
470         /* Timeoout */
471         return SW_OKAY;
472
473     } else {
474         /* Success */
475     
476         if (p.revents != POLLIN)
477             return SW_E_UNKNOWN;
478
479         if ((result = sw_discovery_read_socket((sw_discovery) self)) != SW_OKAY)
480             return result;
481     }
482     
483     return SW_OKAY;
484 }
485
486 sw_result sw_salt_run(sw_salt self) {
487     sw_result ret;
488     
489     AVAHI_WARN_LINKAGE;
490     
491     for (;;)
492         if ((ret = sw_salt_step(self, NULL)) != SW_OKAY)
493             return ret;
494 }
495
496 sw_result sw_salt_stop_run(sw_salt self) {
497     AVAHI_WARN_LINKAGE;
498
499     if (stop_thread((sw_discovery) self) < 0)
500         return SW_E_UNKNOWN;
501
502     return SW_OKAY;
503 }
504
505 sw_result sw_discovery_publish(
506     sw_discovery self,
507     sw_uint32 interface_index,
508     sw_const_string name,
509     sw_const_string type,
510     sw_const_string domain,
511     sw_const_string host,
512     sw_port port,
513     sw_octets text_record,
514     sw_uint32 text_record_len,
515     sw_discovery_publish_reply reply,
516     sw_opaque extra,
517     sw_discovery_oid * oid) {
518     AVAHI_WARN_UNSUPPORTED;
519     return SW_E_NO_IMPL;
520 }
521
522 sw_result sw_discovery_browse_domains(
523     sw_discovery self,
524     sw_uint32 interface_index,
525     sw_discovery_browse_reply reply,
526     sw_opaque extra,
527     sw_discovery_oid * oid) {
528     AVAHI_WARN_UNSUPPORTED;
529     return SW_E_NO_IMPL;
530 }
531
532 sw_result sw_discovery_resolve(
533     sw_discovery self,
534     sw_uint32 interface_index,
535     sw_const_string name,
536     sw_const_string type,
537     sw_const_string domain,
538     sw_discovery_resolve_reply reply,
539     sw_opaque extra,
540     sw_discovery_oid * oid) {
541     AVAHI_WARN_UNSUPPORTED;
542     return SW_E_NO_IMPL;
543 }
544
545 #define OID_GET_INDEX(data) ((sw_discovery_oid) (((data) - ((data)->discovery->oid_table))/sizeof(oid_data)))
546
547 static void service_browser_callback(
548     AvahiServiceBrowser *b,
549     AvahiIfIndex interface,
550     AvahiProtocol protocol,
551     AvahiBrowserEvent event,
552     const char *name,
553     const char *type,
554     const char *domain,
555     AvahiLookupResultFlags flags,
556     void *userdata) {
557
558     oid_data* data = userdata;
559     char type_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
560     sw_discovery_browse_reply reply;
561     
562     assert(b);
563     assert(data);
564
565     reply = (sw_discovery_browse_reply) data->reply;
566
567     type = add_trailing_dot(type, type_fixed, sizeof(type_fixed));
568     domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
569
570     switch (event) {
571         case AVAHI_BROWSER_NEW:
572             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_ADD_SERVICE, interface, name, type, domain, data->extra);
573             break;
574
575         case AVAHI_BROWSER_REMOVE:
576             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_REMOVE_SERVICE, interface, name, type, domain, data->extra);
577             break;
578
579         case AVAHI_BROWSER_FAILURE:
580             reply(data->discovery, OID_GET_INDEX(data), SW_DISCOVERY_BROWSE_INVALID, interface, name, type, domain, data->extra);
581             break;
582             
583         case AVAHI_BROWSER_CACHE_EXHAUSTED:
584         case AVAHI_BROWSER_ALL_FOR_NOW:
585             break;
586     }
587 }
588
589
590 sw_result sw_discovery_browse(
591     sw_discovery self,
592     sw_uint32 interface_index,
593     sw_const_string type,
594     sw_const_string domain,
595     sw_discovery_browse_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(type);
605     assert(reply);
606     assert(oid);
607     
608     AVAHI_WARN_LINKAGE;
609
610     if ((*oid = oid_alloc(self, OID_SERVICE_BROWSER)) == (sw_discovery_oid) -1)
611         return SW_E_UNKNOWN;
612
613     data = oid_get(self, *oid);
614     assert(data);
615     data->reply = (sw_result (*)(void)) reply;
616     data->extra = extra;
617     
618     ifindex = interface_index == 0 ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface_index;
619
620     ASSERT_SUCCESS(pthread_mutex_lock(&self->mutex));
621     
622     if (!(data->object = avahi_service_browser_new(self->client, ifindex, AVAHI_PROTO_UNSPEC, type, domain, 0, service_browser_callback, data))) {
623         result = map_error(avahi_client_errno(self->client));
624         goto finish;
625     }
626
627     result = SW_OKAY;
628     
629 finish:
630
631     ASSERT_SUCCESS(pthread_mutex_unlock(&self->mutex));
632     
633     if (result != SW_OKAY)
634         if (*oid != (sw_discovery_oid) -1)
635             oid_release(self, *oid);
636
637     return result;
638 }
639
640 sw_result sw_discovery_cancel(sw_discovery self, sw_discovery_oid oid) {
641     oid_data *data;
642     assert(self);                             
643
644     AVAHI_WARN_LINKAGE;
645
646     if (!(data = oid_get(self, oid)))
647         return SW_E_UNKNOWN;
648
649     switch (data->type) {
650         case OID_SERVICE_BROWSER:
651             avahi_service_browser_free(data->object);
652             break;
653
654         case OID_UNUSED:
655             ;
656     }
657
658     oid_release(self, oid);
659     
660     return SW_OKAY;
661 }