]> git.meshlink.io Git - catta/blob - avahi-core/querier.c
get rid of a lot of old svn cruft
[catta] / avahi-core / querier.c
1 /***
2   This file is part of avahi.
3
4   avahi 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   avahi 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 avahi; 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 <avahi-common/timeval.h>
27 #include <avahi-common/malloc.h>
28 #include <avahi-common/error.h>
29 #include <avahi-common/domain.h>
30
31 #include "querier.h"
32 #include "log.h"
33
34 struct AvahiQuerier {
35     AvahiInterface *interface;
36
37     AvahiKey *key;
38     int n_used;
39
40     unsigned sec_delay;
41
42     AvahiTimeEvent *time_event;
43
44     struct timeval creation_time;
45
46     unsigned post_id;
47     int post_id_valid;
48
49     AVAHI_LLIST_FIELDS(AvahiQuerier, queriers);
50 };
51
52 void avahi_querier_free(AvahiQuerier *q) {
53     assert(q);
54
55     AVAHI_LLIST_REMOVE(AvahiQuerier, queriers, q->interface->queriers, q);
56     avahi_hashmap_remove(q->interface->queriers_by_key, q->key);
57
58     avahi_key_unref(q->key);
59     avahi_time_event_free(q->time_event);
60
61     avahi_free(q);
62 }
63
64 static void querier_elapse_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void *userdata) {
65     AvahiQuerier *q = userdata;
66     struct timeval tv;
67
68     assert(q);
69
70     if (q->n_used <= 0) {
71
72         /* We are not referenced by anyone anymore, so let's free
73          * ourselves. We should not send out any further queries from
74          * this querier object anymore. */
75
76         avahi_querier_free(q);
77         return;
78     }
79
80     if (avahi_interface_post_query(q->interface, q->key, 0, &q->post_id)) {
81
82         /* The queue accepted our query. We store the query id here,
83          * that allows us to drop the query at a later point if the
84          * query is very short-lived. */
85
86         q->post_id_valid = 1;
87     }
88
89     q->sec_delay *= 2;
90
91     if (q->sec_delay >= 60*60)  /* 1h */
92         q->sec_delay = 60*60;
93
94     avahi_elapse_time(&tv, q->sec_delay*1000, 0);
95     avahi_time_event_update(q->time_event, &tv);
96 }
97
98 void avahi_querier_add(AvahiInterface *i, AvahiKey *key, struct timeval *ret_ctime) {
99     AvahiQuerier *q;
100     struct timeval tv;
101
102     assert(i);
103     assert(key);
104
105     if ((q = avahi_hashmap_lookup(i->queriers_by_key, key))) {
106
107         /* Someone is already browsing for records of this RR key */
108         q->n_used++;
109
110         /* Return the creation time. This is used for generating the
111          * ALL_FOR_NOW event one second after the querier was
112          * initially created. */
113         if (ret_ctime)
114             *ret_ctime = q->creation_time;
115         return;
116     }
117
118     /* No one is browsing for this RR key, so we add a new querier */
119     if (!(q = avahi_new(AvahiQuerier, 1)))
120         return; /* OOM */
121
122     q->key = avahi_key_ref(key);
123     q->interface = i;
124     q->n_used = 1;
125     q->sec_delay = 1;
126     q->post_id_valid = 0;
127     gettimeofday(&q->creation_time, NULL);
128
129     /* Do the initial query */
130     if (avahi_interface_post_query(i, key, 0, &q->post_id))
131         q->post_id_valid = 1;
132
133     /* Schedule next queries */
134     q->time_event = avahi_time_event_new(i->monitor->server->time_event_queue, avahi_elapse_time(&tv, q->sec_delay*1000, 0), querier_elapse_callback, q);
135
136     AVAHI_LLIST_PREPEND(AvahiQuerier, queriers, i->queriers, q);
137     avahi_hashmap_insert(i->queriers_by_key, q->key, q);
138
139     /* Return the creation time. This is used for generating the
140      * ALL_FOR_NOW event one second after the querier was initially
141      * created. */
142     if (ret_ctime)
143         *ret_ctime = q->creation_time;
144 }
145
146 void avahi_querier_remove(AvahiInterface *i, AvahiKey *key) {
147     AvahiQuerier *q;
148
149     if (!(q = avahi_hashmap_lookup(i->queriers_by_key, key)) || q->n_used <= 0) {
150         /* There was no querier for this RR key, or it wasn't referenced by anyone */
151         avahi_log_warn(__FILE__": querier_remove() called but no querier to remove.");
152         return;
153     }
154
155     if ((--q->n_used) <= 0) {
156
157         /* Nobody references us anymore. */
158
159         if (q->post_id_valid && avahi_interface_withraw_query(i, q->post_id)) {
160
161             /* We succeeded in withdrawing our query from the queue,
162              * so let's drop dead. */
163
164             avahi_querier_free(q);
165         }
166
167         /* If we failed to withdraw our query from the queue, we stay
168          * alive, in case someone else might recycle our querier at a
169          * later point. We are freed at our next expiry, in case
170          * nobody recycled us. */
171     }
172 }
173
174 static void remove_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
175     assert(m);
176     assert(i);
177     assert(userdata);
178
179     if (i->announcing)
180         avahi_querier_remove(i, (AvahiKey*) userdata);
181 }
182
183 void avahi_querier_remove_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key) {
184     assert(s);
185     assert(key);
186
187     avahi_interface_monitor_walk(s->monitor, idx, protocol, remove_querier_callback, key);
188 }
189
190 struct cbdata {
191     AvahiKey *key;
192     struct timeval *ret_ctime;
193 };
194
195 static void add_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
196     struct cbdata *cbdata = userdata;
197
198     assert(m);
199     assert(i);
200     assert(cbdata);
201
202     if (i->announcing) {
203         struct timeval tv;
204         avahi_querier_add(i, cbdata->key, &tv);
205
206         if (cbdata->ret_ctime && avahi_timeval_compare(&tv, cbdata->ret_ctime) > 0)
207             *cbdata->ret_ctime = tv;
208     }
209 }
210
211 void avahi_querier_add_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key, struct timeval *ret_ctime) {
212     struct cbdata cbdata;
213
214     assert(s);
215     assert(key);
216
217     cbdata.key = key;
218     cbdata.ret_ctime = ret_ctime;
219
220     if (ret_ctime)
221         ret_ctime->tv_sec = ret_ctime->tv_usec = 0;
222
223     avahi_interface_monitor_walk(s->monitor, idx, protocol, add_querier_callback, &cbdata);
224 }
225
226 int avahi_querier_shall_refresh_cache(AvahiInterface *i, AvahiKey *key) {
227     AvahiQuerier *q;
228
229     assert(i);
230     assert(key);
231
232     /* Called by the cache maintainer */
233
234     if (!(q = avahi_hashmap_lookup(i->queriers_by_key, key)))
235         /* This key is currently not subscribed at all, so no cache
236          * refresh is needed */
237         return 0;
238
239     if (q->n_used <= 0) {
240
241         /* If this is an entry nobody references right now, don't
242          * consider it "existing". */
243
244         /* Remove this querier since it is referenced by nobody
245          * and the cached data will soon be out of date */
246         avahi_querier_free(q);
247
248         /* Tell the cache that no refresh is needed */
249         return 0;
250
251     } else {
252         struct timeval tv;
253
254         /* We can defer our query a little, since the cache will now
255          * issue a refresh query anyway. */
256         avahi_elapse_time(&tv, q->sec_delay*1000, 0);
257         avahi_time_event_update(q->time_event, &tv);
258
259         /* Tell the cache that a refresh should be issued */
260         return 1;
261     }
262 }
263
264 void avahi_querier_free_all(AvahiInterface *i) {
265     assert(i);
266
267     while (i->queriers)
268         avahi_querier_free(i->queriers);
269 }