4 This file is part of avahi.
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.
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.
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
28 #include <avahi-common/timeval.h>
29 #include <avahi-common/malloc.h>
30 #include <avahi-common/error.h>
31 #include <avahi-common/domain.h>
37 AvahiInterface *interface;
44 AvahiTimeEvent *time_event;
46 struct timeval creation_time;
51 AVAHI_LLIST_FIELDS(AvahiQuerier, queriers);
54 void avahi_querier_free(AvahiQuerier *q) {
57 AVAHI_LLIST_REMOVE(AvahiQuerier, queriers, q->interface->queriers, q);
58 avahi_hashmap_remove(q->interface->queriers_by_key, q->key);
60 avahi_key_unref(q->key);
61 avahi_time_event_free(q->time_event);
66 static void querier_elapse_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void *userdata) {
67 AvahiQuerier *q = userdata;
74 /* We are not referenced by anyone anymore, so let's free
75 * ourselves. We should not send out any further queries from
76 * this querier object anymore. */
78 avahi_querier_free(q);
82 if (avahi_interface_post_query(q->interface, q->key, 0, &q->post_id)) {
84 /* The queue accepted our query. We store the query id here,
85 * that allows us to drop the query at a later point if the
86 * query is very short-lived. */
93 if (q->sec_delay >= 60*60) /* 1h */
96 avahi_elapse_time(&tv, q->sec_delay*1000, 0);
97 avahi_time_event_update(q->time_event, &tv);
100 void avahi_querier_add(AvahiInterface *i, AvahiKey *key, struct timeval *ret_ctime) {
107 if ((q = avahi_hashmap_lookup(i->queriers_by_key, key))) {
109 /* Someone is already browsing for records of this RR key */
112 /* Return the creation time. This is used for generating the
113 * ALL_FOR_NOW event one second after the querier was
114 * initially created. */
116 *ret_ctime = q->creation_time;
120 /* No one is browsing for this RR key, so we add a new querier */
121 if (!(q = avahi_new(AvahiQuerier, 1)))
124 q->key = avahi_key_ref(key);
128 q->post_id_valid = 0;
129 gettimeofday(&q->creation_time, NULL);
131 /* Do the initial query */
132 if (avahi_interface_post_query(i, key, 0, &q->post_id))
133 q->post_id_valid = 1;
135 /* Schedule next queries */
136 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);
138 AVAHI_LLIST_PREPEND(AvahiQuerier, queriers, i->queriers, q);
139 avahi_hashmap_insert(i->queriers_by_key, q->key, q);
141 /* Return the creation time. This is used for generating the
142 * ALL_FOR_NOW event one second after the querier was initially
145 *ret_ctime = q->creation_time;
148 void avahi_querier_remove(AvahiInterface *i, AvahiKey *key) {
151 if (!(q = avahi_hashmap_lookup(i->queriers_by_key, key)) || q->n_used <= 0) {
152 /* There was no querier for this RR key, or it wasn't referenced by anyone */
153 avahi_log_warn(__FILE__": querier_remove() called but no querier to remove.");
157 if ((--q->n_used) <= 0) {
159 /* Nobody references us anymore. */
161 if (q->post_id_valid && avahi_interface_withraw_query(i, q->post_id)) {
163 /* We succeeded in withdrawing our query from the queue,
164 * so let's drop dead. */
166 avahi_querier_free(q);
169 /* If we failed to withdraw our query from the queue, we stay
170 * alive, in case someone else might recycle our querier at a
171 * later point. We are freed at our next expiry, in case
172 * nobody recycled us. */
176 static void remove_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
182 avahi_querier_remove(i, (AvahiKey*) userdata);
185 void avahi_querier_remove_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key) {
189 avahi_interface_monitor_walk(s->monitor, idx, protocol, remove_querier_callback, key);
194 struct timeval *ret_ctime;
197 static void add_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
198 struct cbdata *cbdata = userdata;
206 avahi_querier_add(i, cbdata->key, &tv);
208 if (cbdata->ret_ctime && avahi_timeval_compare(&tv, cbdata->ret_ctime) > 0)
209 *cbdata->ret_ctime = tv;
213 void avahi_querier_add_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key, struct timeval *ret_ctime) {
214 struct cbdata cbdata;
220 cbdata.ret_ctime = ret_ctime;
223 ret_ctime->tv_sec = ret_ctime->tv_usec = 0;
225 avahi_interface_monitor_walk(s->monitor, idx, protocol, add_querier_callback, &cbdata);
228 int avahi_querier_shall_refresh_cache(AvahiInterface *i, AvahiKey *key) {
234 /* Called by the cache maintainer */
236 if (!(q = avahi_hashmap_lookup(i->queriers_by_key, key)))
237 /* This key is currently not subscribed at all, so no cache
238 * refresh is needed */
241 if (q->n_used <= 0) {
243 /* If this is an entry nobody references right now, don't
244 * consider it "existing". */
246 /* Remove this querier since it is referenced by nobody
247 * and the cached data will soon be out of date */
248 avahi_querier_free(q);
250 /* Tell the cache that no refresh is needed */
256 /* We can defer our query a little, since the cache will now
257 * issue a refresh query anyway. */
258 avahi_elapse_time(&tv, q->sec_delay*1000, 0);
259 avahi_time_event_update(q->time_event, &tv);
261 /* Tell the cache that a refresh should be issued */
266 void avahi_querier_free_all(AvahiInterface *i) {
270 avahi_querier_free(i->queriers);