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