]> git.meshlink.io Git - catta/blob - avahi-core/multicast-lookup.c
* remove lots of DOXYGEN_SHOULD_SKIP_THIS from the header files, use doxygen macro...
[catta] / avahi-core / multicast-lookup.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 <avahi-common/malloc.h>
27 #include <avahi-common/timeval.h>
28
29 #include "internal.h"
30 #include "browse.h"
31 #include "socket.h"
32 #include "log.h"
33 #include "hashmap.h"
34 #include "multicast-lookup.h"
35 #include "rr-util.h"
36
37 struct AvahiMulticastLookup {
38     AvahiMulticastLookupEngine *engine;
39     int dead;
40
41     AvahiKey *key, *cname_key;
42     
43     AvahiMulticastLookupCallback callback;
44     void *userdata;
45
46     AvahiIfIndex interface;
47     AvahiProtocol protocol;
48     
49     int queriers_added;
50
51     AvahiTimeEvent *all_for_now_event;
52     
53     AVAHI_LLIST_FIELDS(AvahiMulticastLookup, lookups);
54     AVAHI_LLIST_FIELDS(AvahiMulticastLookup, by_key);
55 };
56
57 struct AvahiMulticastLookupEngine {
58     AvahiServer *server;
59
60     /* Lookups */
61     AVAHI_LLIST_HEAD(AvahiMulticastLookup, lookups);
62     AvahiHashmap *lookups_by_key;
63
64     int cleanup_dead;
65 };
66
67 static void all_for_now_callback(AvahiTimeEvent *e, void* userdata) {
68     AvahiMulticastLookup *l = userdata;
69
70     assert(e);
71     assert(l);
72
73     avahi_time_event_free(l->all_for_now_event);
74     l->all_for_now_event = NULL;
75
76     l->callback(l->engine, l->interface, l->protocol, AVAHI_BROWSER_ALL_FOR_NOW, AVAHI_LOOKUP_RESULT_MULTICAST, NULL, l->userdata);
77 }
78
79 AvahiMulticastLookup *avahi_multicast_lookup_new(
80     AvahiMulticastLookupEngine *e,
81     AvahiIfIndex interface,
82     AvahiProtocol protocol,
83     AvahiKey *key,
84     AvahiMulticastLookupCallback callback,
85     void *userdata) {
86     
87     AvahiMulticastLookup *l, *t;
88     struct timeval tv;
89         
90     assert(e);
91     assert(AVAHI_IF_VALID(interface));
92     assert(AVAHI_PROTO_VALID(protocol));
93     assert(key);
94     assert(callback);
95
96     l = avahi_new(AvahiMulticastLookup, 1);
97     l->engine = e;
98     l->dead = 0;
99     l->key = avahi_key_ref(key);
100     l->cname_key = avahi_key_new_cname(l->key);
101     l->callback = callback;
102     l->userdata = userdata;
103     l->interface = interface;
104     l->protocol = protocol;
105     l->all_for_now_event = NULL;
106     l->queriers_added = 0;
107
108     t = avahi_hashmap_lookup(e->lookups_by_key, l->key);
109     AVAHI_LLIST_PREPEND(AvahiMulticastLookup, by_key, t, l);
110     avahi_hashmap_replace(e->lookups_by_key, avahi_key_ref(l->key), t);
111
112     AVAHI_LLIST_PREPEND(AvahiMulticastLookup, lookups, e->lookups, l);
113
114     avahi_querier_add_for_all(e->server, interface, protocol, l->key, &tv);
115     l->queriers_added = 1;
116
117     /* add a second */
118     avahi_timeval_add(&tv, 1000000);
119
120     l->all_for_now_event = avahi_time_event_new(e->server->time_event_queue, &tv, all_for_now_callback, l);
121     
122     return l;
123 }
124
125 static void lookup_stop(AvahiMulticastLookup *l) {
126     assert(l);
127
128     l->callback = NULL;
129
130     if (l->queriers_added) {
131         avahi_querier_remove_for_all(l->engine->server, l->interface, l->protocol, l->key);
132         l->queriers_added = 0;
133     }
134
135     if (l->all_for_now_event) {
136         avahi_time_event_free(l->all_for_now_event);
137         l->all_for_now_event = NULL;
138     }
139 }
140
141 static void lookup_destroy(AvahiMulticastLookup *l) {
142     AvahiMulticastLookup *t;
143     assert(l);
144
145     lookup_stop(l);
146     
147     t = avahi_hashmap_lookup(l->engine->lookups_by_key, l->key);
148     AVAHI_LLIST_REMOVE(AvahiMulticastLookup, by_key, t, l);
149     if (t)
150         avahi_hashmap_replace(l->engine->lookups_by_key, avahi_key_ref(l->key), t);
151     else
152         avahi_hashmap_remove(l->engine->lookups_by_key, l->key);
153
154     AVAHI_LLIST_REMOVE(AvahiMulticastLookup, lookups, l->engine->lookups, l);
155
156     if (l->key)
157         avahi_key_unref(l->key);
158
159     if (l->cname_key)
160         avahi_key_unref(l->cname_key);
161     
162     avahi_free(l);
163 }
164
165 void avahi_multicast_lookup_free(AvahiMulticastLookup *l) {
166     assert(l);
167
168     if (l->dead)
169         return;
170
171     l->dead = 1;
172     l->engine->cleanup_dead = 1;
173     lookup_stop(l);
174 }
175
176 void avahi_multicast_lookup_engine_cleanup(AvahiMulticastLookupEngine *e) {
177     AvahiMulticastLookup *l, *n;
178     assert(e);
179     
180     while (e->cleanup_dead) {
181         e->cleanup_dead = 0;
182         
183         for (l = e->lookups; l; l = n) {
184             n = l->lookups_next;
185             
186             if (l->dead)
187                 lookup_destroy(l);
188         }
189     }
190 }
191
192 struct cbdata {
193     AvahiMulticastLookupEngine *engine;
194     AvahiMulticastLookupCallback callback;
195     void *userdata;
196     AvahiKey *key, *cname_key;
197     AvahiInterface *interface;
198     unsigned n_found;
199 };
200
201 static void* scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) {
202     struct cbdata *cbdata = userdata;
203
204     assert(c);
205     assert(pattern);
206     assert(e);
207     assert(cbdata);
208
209     cbdata->callback(
210         cbdata->engine,
211         cbdata->interface->hardware->index,
212         cbdata->interface->protocol,
213         AVAHI_BROWSER_NEW,
214         AVAHI_LOOKUP_RESULT_CACHED|AVAHI_LOOKUP_RESULT_MULTICAST,
215         e->record,
216         cbdata->userdata);
217
218     cbdata->n_found ++;
219     
220     return NULL;
221 }
222
223 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
224     struct cbdata *cbdata = userdata;
225
226     assert(m);
227     assert(i);
228     assert(cbdata);
229
230     cbdata->interface = i;
231     
232     avahi_cache_walk(i->cache, cbdata->key, scan_cache_callback, cbdata);
233
234     if (cbdata->cname_key)
235         avahi_cache_walk(i->cache, cbdata->cname_key, scan_cache_callback, cbdata);
236     
237     cbdata->interface = NULL;
238 }
239
240 unsigned avahi_multicast_lookup_engine_scan_cache(
241     AvahiMulticastLookupEngine *e,
242     AvahiIfIndex interface,
243     AvahiProtocol protocol,
244     AvahiKey *key,
245     AvahiMulticastLookupCallback callback,
246     void *userdata) {
247     
248     struct cbdata cbdata;
249     
250     assert(e);
251     assert(key);
252     assert(callback);
253
254     assert(AVAHI_IF_VALID(interface));
255     assert(AVAHI_PROTO_VALID(protocol));
256
257     cbdata.engine = e;
258     cbdata.key = key;
259     cbdata.cname_key = avahi_key_new_cname(key);
260     cbdata.callback = callback;
261     cbdata.userdata = userdata;
262     cbdata.interface = NULL;
263     cbdata.n_found = 0;
264     
265     avahi_interface_monitor_walk(e->server->monitor, interface, protocol, scan_interface_callback, &cbdata);
266     
267     if (cbdata.cname_key)
268         avahi_key_unref(cbdata.cname_key);
269
270     return cbdata.n_found;
271 }
272
273 void avahi_multicast_lookup_engine_new_interface(AvahiMulticastLookupEngine *e, AvahiInterface *i) {
274     AvahiMulticastLookup *l;
275     
276     assert(e);
277     assert(i);
278
279     for (l = e->lookups; l; l = l->lookups_next) {
280         
281         if (l->dead || !l->callback)
282             continue;
283
284         if (l->queriers_added && avahi_interface_match(i, l->interface, l->protocol))
285             avahi_querier_add(i, l->key, NULL);
286     }
287 }
288
289 void avahi_multicast_lookup_engine_notify(AvahiMulticastLookupEngine *e, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
290     AvahiMulticastLookup *l;
291     
292     assert(e);
293     assert(record);
294     assert(i);
295
296     for (l = avahi_hashmap_lookup(e->lookups_by_key, record->key); l; l = l->by_key_next) {
297         if (l->dead || !l->callback)
298             continue;
299
300         if (avahi_interface_match(i, l->interface, l->protocol))
301             l->callback(e, i->hardware->index, i->protocol, event, AVAHI_LOOKUP_RESULT_MULTICAST, record, l->userdata);
302     }
303
304
305     if (record->key->clazz == AVAHI_DNS_CLASS_IN && record->key->type == AVAHI_DNS_TYPE_CNAME) {
306         /* It's a CNAME record, so we have to scan the all lookups to see if one matches */
307
308         for (l = e->lookups; l; l = l->lookups_next) {
309             AvahiKey *key;
310
311             if (l->dead || !l->callback)
312                 continue;
313             
314             if ((key = avahi_key_new_cname(l->key))) {
315                 if (avahi_key_equal(record->key, key))
316                     l->callback(e, i->hardware->index, i->protocol, event, AVAHI_LOOKUP_RESULT_MULTICAST, record, l->userdata);
317
318                 avahi_key_unref(key);
319             }
320         }
321     }
322 }
323
324 AvahiMulticastLookupEngine *avahi_multicast_lookup_engine_new(AvahiServer *s) {
325     AvahiMulticastLookupEngine *e;
326     
327     assert(s);
328     
329     e = avahi_new(AvahiMulticastLookupEngine, 1);
330     e->server = s;
331     e->cleanup_dead = 0;
332
333     /* Initialize lookup list */
334     e->lookups_by_key = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, (AvahiFreeFunc) avahi_key_unref, NULL);
335     AVAHI_LLIST_HEAD_INIT(AvahiWideAreaLookup, e->lookups);
336
337     return e;
338 }
339
340 void avahi_multicast_lookup_engine_free(AvahiMulticastLookupEngine *e) {
341     assert(e);
342
343     while (e->lookups)
344         lookup_destroy(e->lookups);
345
346     avahi_hashmap_free(e->lookups_by_key);
347     avahi_free(e);
348 }
349