]> git.meshlink.io Git - catta/blob - src/multicast-lookup.c
rename everything avahi to catta
[catta] / src / multicast-lookup.c
1 /***
2   This file is part of catta.
3
4   catta is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   catta is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with catta; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25
26 #include <catta/malloc.h>
27 #include <catta/timeval.h>
28
29 #include "internal.h"
30 #include "browse.h"
31 #include "socket.h"
32 #include <catta/log.h>
33 #include "hashmap.h"
34 #include "multicast-lookup.h"
35 #include "rr-util.h"
36
37 struct CattaMulticastLookup {
38     CattaMulticastLookupEngine *engine;
39     int dead;
40
41     CattaKey *key, *cname_key;
42
43     CattaMulticastLookupCallback callback;
44     void *userdata;
45
46     CattaIfIndex interface;
47     CattaProtocol protocol;
48
49     int queriers_added;
50
51     CattaTimeEvent *all_for_now_event;
52
53     CATTA_LLIST_FIELDS(CattaMulticastLookup, lookups);
54     CATTA_LLIST_FIELDS(CattaMulticastLookup, by_key);
55 };
56
57 struct CattaMulticastLookupEngine {
58     CattaServer *server;
59
60     /* Lookups */
61     CATTA_LLIST_HEAD(CattaMulticastLookup, lookups);
62     CattaHashmap *lookups_by_key;
63
64     int cleanup_dead;
65 };
66
67 static void all_for_now_callback(CattaTimeEvent *e, void* userdata) {
68     CattaMulticastLookup *l = userdata;
69
70     assert(e);
71     assert(l);
72
73     catta_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, CATTA_BROWSER_ALL_FOR_NOW, CATTA_LOOKUP_RESULT_MULTICAST, NULL, l->userdata);
77 }
78
79 CattaMulticastLookup *catta_multicast_lookup_new(
80     CattaMulticastLookupEngine *e,
81     CattaIfIndex interface,
82     CattaProtocol protocol,
83     CattaKey *key,
84     CattaMulticastLookupCallback callback,
85     void *userdata) {
86
87     CattaMulticastLookup *l, *t;
88     struct timeval tv;
89
90     assert(e);
91     assert(CATTA_IF_VALID(interface));
92     assert(CATTA_PROTO_VALID(protocol));
93     assert(key);
94     assert(callback);
95
96     l = catta_new(CattaMulticastLookup, 1);
97     l->engine = e;
98     l->dead = 0;
99     l->key = catta_key_ref(key);
100     l->cname_key = catta_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 = catta_hashmap_lookup(e->lookups_by_key, l->key);
109     CATTA_LLIST_PREPEND(CattaMulticastLookup, by_key, t, l);
110     catta_hashmap_replace(e->lookups_by_key, catta_key_ref(l->key), t);
111
112     CATTA_LLIST_PREPEND(CattaMulticastLookup, lookups, e->lookups, l);
113
114     catta_querier_add_for_all(e->server, interface, protocol, l->key, &tv);
115     l->queriers_added = 1;
116
117     /* Add a second */
118     catta_timeval_add(&tv, 1000000);
119
120     /* Issue the ALL_FOR_NOW event one second after the querier was initially created */
121     l->all_for_now_event = catta_time_event_new(e->server->time_event_queue, &tv, all_for_now_callback, l);
122
123     return l;
124 }
125
126 static void lookup_stop(CattaMulticastLookup *l) {
127     assert(l);
128
129     l->callback = NULL;
130
131     if (l->queriers_added) {
132         catta_querier_remove_for_all(l->engine->server, l->interface, l->protocol, l->key);
133         l->queriers_added = 0;
134     }
135
136     if (l->all_for_now_event) {
137         catta_time_event_free(l->all_for_now_event);
138         l->all_for_now_event = NULL;
139     }
140 }
141
142 static void lookup_destroy(CattaMulticastLookup *l) {
143     CattaMulticastLookup *t;
144     assert(l);
145
146     lookup_stop(l);
147
148     t = catta_hashmap_lookup(l->engine->lookups_by_key, l->key);
149     CATTA_LLIST_REMOVE(CattaMulticastLookup, by_key, t, l);
150     if (t)
151         catta_hashmap_replace(l->engine->lookups_by_key, catta_key_ref(l->key), t);
152     else
153         catta_hashmap_remove(l->engine->lookups_by_key, l->key);
154
155     CATTA_LLIST_REMOVE(CattaMulticastLookup, lookups, l->engine->lookups, l);
156
157     if (l->key)
158         catta_key_unref(l->key);
159
160     if (l->cname_key)
161         catta_key_unref(l->cname_key);
162
163     catta_free(l);
164 }
165
166 void catta_multicast_lookup_free(CattaMulticastLookup *l) {
167     assert(l);
168
169     if (l->dead)
170         return;
171
172     l->dead = 1;
173     l->engine->cleanup_dead = 1;
174     lookup_stop(l);
175 }
176
177 void catta_multicast_lookup_engine_cleanup(CattaMulticastLookupEngine *e) {
178     CattaMulticastLookup *l, *n;
179     assert(e);
180
181     while (e->cleanup_dead) {
182         e->cleanup_dead = 0;
183
184         for (l = e->lookups; l; l = n) {
185             n = l->lookups_next;
186
187             if (l->dead)
188                 lookup_destroy(l);
189         }
190     }
191 }
192
193 struct cbdata {
194     CattaMulticastLookupEngine *engine;
195     CattaMulticastLookupCallback callback;
196     void *userdata;
197     CattaKey *key, *cname_key;
198     CattaInterface *interface;
199     unsigned n_found;
200 };
201
202 static void* scan_cache_callback(CattaCache *c, CattaKey *pattern, CattaCacheEntry *e, void* userdata) {
203     struct cbdata *cbdata = userdata;
204
205     assert(c);
206     assert(pattern);
207     assert(e);
208     assert(cbdata);
209
210     cbdata->callback(
211         cbdata->engine,
212         cbdata->interface->hardware->index,
213         cbdata->interface->protocol,
214         CATTA_BROWSER_NEW,
215         CATTA_LOOKUP_RESULT_CACHED|CATTA_LOOKUP_RESULT_MULTICAST,
216         e->record,
217         cbdata->userdata);
218
219     cbdata->n_found ++;
220
221     return NULL;
222 }
223
224 static void scan_interface_callback(CattaInterfaceMonitor *m, CattaInterface *i, void* userdata) {
225     struct cbdata *cbdata = userdata;
226
227     assert(m);
228     assert(i);
229     assert(cbdata);
230
231     cbdata->interface = i;
232
233     catta_cache_walk(i->cache, cbdata->key, scan_cache_callback, cbdata);
234
235     if (cbdata->cname_key)
236         catta_cache_walk(i->cache, cbdata->cname_key, scan_cache_callback, cbdata);
237
238     cbdata->interface = NULL;
239 }
240
241 unsigned catta_multicast_lookup_engine_scan_cache(
242     CattaMulticastLookupEngine *e,
243     CattaIfIndex interface,
244     CattaProtocol protocol,
245     CattaKey *key,
246     CattaMulticastLookupCallback callback,
247     void *userdata) {
248
249     struct cbdata cbdata;
250
251     assert(e);
252     assert(key);
253     assert(callback);
254
255     assert(CATTA_IF_VALID(interface));
256     assert(CATTA_PROTO_VALID(protocol));
257
258     cbdata.engine = e;
259     cbdata.key = key;
260     cbdata.cname_key = catta_key_new_cname(key);
261     cbdata.callback = callback;
262     cbdata.userdata = userdata;
263     cbdata.interface = NULL;
264     cbdata.n_found = 0;
265
266     catta_interface_monitor_walk(e->server->monitor, interface, protocol, scan_interface_callback, &cbdata);
267
268     if (cbdata.cname_key)
269         catta_key_unref(cbdata.cname_key);
270
271     return cbdata.n_found;
272 }
273
274 void catta_multicast_lookup_engine_new_interface(CattaMulticastLookupEngine *e, CattaInterface *i) {
275     CattaMulticastLookup *l;
276
277     assert(e);
278     assert(i);
279
280     for (l = e->lookups; l; l = l->lookups_next) {
281
282         if (l->dead || !l->callback)
283             continue;
284
285         if (l->queriers_added && catta_interface_match(i, l->interface, l->protocol))
286             catta_querier_add(i, l->key, NULL);
287     }
288 }
289
290 void catta_multicast_lookup_engine_notify(CattaMulticastLookupEngine *e, CattaInterface *i, CattaRecord *record, CattaBrowserEvent event) {
291     CattaMulticastLookup *l;
292
293     assert(e);
294     assert(record);
295     assert(i);
296
297     for (l = catta_hashmap_lookup(e->lookups_by_key, record->key); l; l = l->by_key_next) {
298         if (l->dead || !l->callback)
299             continue;
300
301         if (catta_interface_match(i, l->interface, l->protocol))
302             l->callback(e, i->hardware->index, i->protocol, event, CATTA_LOOKUP_RESULT_MULTICAST, record, l->userdata);
303     }
304
305
306     if (record->key->clazz == CATTA_DNS_CLASS_IN && record->key->type == CATTA_DNS_TYPE_CNAME) {
307         /* It's a CNAME record, so we have to scan the all lookups to see if one matches */
308
309         for (l = e->lookups; l; l = l->lookups_next) {
310             CattaKey *key;
311
312             if (l->dead || !l->callback)
313                 continue;
314
315             if ((key = catta_key_new_cname(l->key))) {
316                 if (catta_key_equal(record->key, key))
317                     l->callback(e, i->hardware->index, i->protocol, event, CATTA_LOOKUP_RESULT_MULTICAST, record, l->userdata);
318
319                 catta_key_unref(key);
320             }
321         }
322     }
323 }
324
325 CattaMulticastLookupEngine *catta_multicast_lookup_engine_new(CattaServer *s) {
326     CattaMulticastLookupEngine *e;
327
328     assert(s);
329
330     e = catta_new(CattaMulticastLookupEngine, 1);
331     e->server = s;
332     e->cleanup_dead = 0;
333
334     /* Initialize lookup list */
335     e->lookups_by_key = catta_hashmap_new((CattaHashFunc) catta_key_hash, (CattaEqualFunc) catta_key_equal, (CattaFreeFunc) catta_key_unref, NULL);
336     CATTA_LLIST_HEAD_INIT(CattaWideAreaLookup, e->lookups);
337
338     return e;
339 }
340
341 void catta_multicast_lookup_engine_free(CattaMulticastLookupEngine *e) {
342     assert(e);
343
344     while (e->lookups)
345         lookup_destroy(e->lookups);
346
347     catta_hashmap_free(e->lookups_by_key);
348     catta_free(e);
349 }
350