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