2 This file is part of avahi.
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.
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.
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
26 #include <avahi-common/timeval.h>
27 #include <avahi-common/malloc.h>
28 #include <avahi-common/error.h>
29 #include <avahi-common/domain.h>
35 AvahiInterface *interface;
42 AvahiTimeEvent *time_event;
44 struct timeval creation_time;
49 AVAHI_LLIST_FIELDS(AvahiQuerier, queriers);
52 void avahi_querier_free(AvahiQuerier *q) {
55 AVAHI_LLIST_REMOVE(AvahiQuerier, queriers, q->interface->queriers, q);
56 avahi_hashmap_remove(q->interface->queriers_by_key, q->key);
58 avahi_key_unref(q->key);
59 avahi_time_event_free(q->time_event);
64 static void querier_elapse_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void *userdata) {
65 AvahiQuerier *q = userdata;
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. */
76 avahi_querier_free(q);
80 if (avahi_interface_post_query(q->interface, q->key, 0, &q->post_id)) {
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. */
91 if (q->sec_delay >= 60*60) /* 1h */
94 avahi_elapse_time(&tv, q->sec_delay*1000, 0);
95 avahi_time_event_update(q->time_event, &tv);
98 void avahi_querier_add(AvahiInterface *i, AvahiKey *key, struct timeval *ret_ctime) {
105 if ((q = avahi_hashmap_lookup(i->queriers_by_key, key))) {
107 /* Someone is already browsing for records of this RR key */
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. */
114 *ret_ctime = q->creation_time;
118 /* No one is browsing for this RR key, so we add a new querier */
119 if (!(q = avahi_new(AvahiQuerier, 1)))
122 q->key = avahi_key_ref(key);
126 q->post_id_valid = 0;
127 gettimeofday(&q->creation_time, NULL);
129 /* Do the initial query */
130 if (avahi_interface_post_query(i, key, 0, &q->post_id))
131 q->post_id_valid = 1;
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);
136 AVAHI_LLIST_PREPEND(AvahiQuerier, queriers, i->queriers, q);
137 avahi_hashmap_insert(i->queriers_by_key, q->key, q);
139 /* Return the creation time. This is used for generating the
140 * ALL_FOR_NOW event one second after the querier was initially
143 *ret_ctime = q->creation_time;
146 void avahi_querier_remove(AvahiInterface *i, AvahiKey *key) {
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.");
155 if ((--q->n_used) <= 0) {
157 /* Nobody references us anymore. */
159 if (q->post_id_valid && avahi_interface_withraw_query(i, q->post_id)) {
161 /* We succeeded in withdrawing our query from the queue,
162 * so let's drop dead. */
164 avahi_querier_free(q);
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. */
174 static void remove_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
180 avahi_querier_remove(i, (AvahiKey*) userdata);
183 void avahi_querier_remove_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key) {
187 avahi_interface_monitor_walk(s->monitor, idx, protocol, remove_querier_callback, key);
192 struct timeval *ret_ctime;
195 static void add_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
196 struct cbdata *cbdata = userdata;
204 avahi_querier_add(i, cbdata->key, &tv);
206 if (cbdata->ret_ctime && avahi_timeval_compare(&tv, cbdata->ret_ctime) > 0)
207 *cbdata->ret_ctime = tv;
211 void avahi_querier_add_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key, struct timeval *ret_ctime) {
212 struct cbdata cbdata;
218 cbdata.ret_ctime = ret_ctime;
221 ret_ctime->tv_sec = ret_ctime->tv_usec = 0;
223 avahi_interface_monitor_walk(s->monitor, idx, protocol, add_querier_callback, &cbdata);
226 int avahi_querier_shall_refresh_cache(AvahiInterface *i, AvahiKey *key) {
232 /* Called by the cache maintainer */
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 */
239 if (q->n_used <= 0) {
241 /* If this is an entry nobody references right now, don't
242 * consider it "existing". */
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);
248 /* Tell the cache that no refresh is needed */
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);
259 /* Tell the cache that a refresh should be issued */
264 void avahi_querier_free_all(AvahiInterface *i) {
268 avahi_querier_free(i->queriers);