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
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;
46 AVAHI_LLIST_FIELDS(AvahiQuerier, queriers);
49 void avahi_querier_free(AvahiQuerier *q) {
52 AVAHI_LLIST_REMOVE(AvahiQuerier, queriers, q->interface->queriers, q);
53 avahi_hashmap_remove(q->interface->queriers_by_key, q->key);
55 avahi_key_unref(q->key);
56 avahi_time_event_free(q->time_event);
61 static void querier_elapse_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void *userdata) {
62 AvahiQuerier *q = userdata;
67 avahi_interface_post_query(q->interface, q->key, 0);
71 if (q->sec_delay >= 60*60) /* 1h */
74 avahi_elapse_time(&tv, q->sec_delay*1000, 0);
75 avahi_time_event_update(q->time_event, &tv);
78 void avahi_querier_add(AvahiInterface *i, AvahiKey *key, struct timeval *ret_ctime) {
85 if ((q = avahi_hashmap_lookup(i->queriers_by_key, key))) {
86 /* Someone is already browsing for records of this RR key */
89 /* Return the creation time */
91 *ret_ctime = q->creation_time;
95 /* No one is browsing for this RR key, so we add a new querier */
96 if (!(q = avahi_new(AvahiQuerier, 1)))
99 q->key = avahi_key_ref(key);
103 gettimeofday(&q->creation_time, NULL);
105 /* Do the initial query */
106 avahi_interface_post_query(i, key, 0);
108 /* Schedule next queries */
109 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);
111 AVAHI_LLIST_PREPEND(AvahiQuerier, queriers, i->queriers, q);
112 avahi_hashmap_insert(i->queriers_by_key, q->key, q);
114 /* Return the creation time */
116 *ret_ctime = q->creation_time;
119 void avahi_querier_remove(AvahiInterface *i, AvahiKey *key) {
122 if (!(q = avahi_hashmap_lookup(i->queriers_by_key, key))) {
123 /* The was no querier for this RR key */
124 avahi_log_warn(__FILE__": querier_remove() called but no querier to remove");
128 assert(q->n_used >= 1);
130 if ((--q->n_used) <= 0)
131 avahi_querier_free(q);
134 static void remove_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
140 avahi_querier_remove(i, (AvahiKey*) userdata);
143 void avahi_querier_remove_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key) {
147 avahi_interface_monitor_walk(s->monitor, idx, protocol, remove_querier_callback, key);
152 struct timeval *ret_ctime;
155 static void add_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
156 struct cbdata *cbdata = userdata;
164 avahi_querier_add(i, cbdata->key, &tv);
166 if (cbdata->ret_ctime && avahi_timeval_compare(&tv, cbdata->ret_ctime) > 0)
167 *cbdata->ret_ctime = tv;
171 void avahi_querier_add_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key, struct timeval *ret_ctime) {
172 struct cbdata cbdata;
178 cbdata.ret_ctime = ret_ctime;
181 ret_ctime->tv_sec = ret_ctime->tv_usec = 0;
183 avahi_interface_monitor_walk(s->monitor, idx, protocol, add_querier_callback, &cbdata);
186 int avahi_querier_exists(AvahiInterface *i, AvahiKey *key) {
190 if (avahi_hashmap_lookup(i->queriers_by_key, key))
196 void avahi_querier_free_all(AvahiInterface *i) {
200 avahi_querier_free(i->queriers);