]> git.meshlink.io Git - catta/blob - avahi-core/querier.c
* split off lookup.h and publish.h from core.h
[catta] / avahi-core / querier.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
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     AVAHI_LLIST_FIELDS(AvahiQuerier, queriers);
47 };
48
49 void avahi_querier_free(AvahiQuerier *q) {
50     assert(q);
51
52     AVAHI_LLIST_REMOVE(AvahiQuerier, queriers, q->interface->queriers, q);
53     avahi_hashmap_remove(q->interface->queriers_by_key, q->key);
54
55     avahi_key_unref(q->key);
56     avahi_time_event_free(q->time_event);
57     
58     avahi_free(q);
59 }
60
61 static void querier_elapse_callback(AvahiTimeEvent *e, void *userdata) {
62     AvahiQuerier *q = userdata;
63     struct timeval tv;
64     
65     assert(q);
66
67     avahi_interface_post_query(q->interface, q->key, 0);
68
69     q->sec_delay *= 2;
70     
71     if (q->sec_delay >= 60*60)  /* 1h */
72         q->sec_delay = 60*60;
73     
74     avahi_elapse_time(&tv, q->sec_delay*1000, 0);
75     avahi_time_event_update(q->time_event, &tv);
76 }
77
78 void avahi_querier_add(AvahiInterface *i, AvahiKey *key, struct timeval *ret_ctime) {
79     AvahiQuerier *q;
80     struct timeval tv;
81     
82     assert(i);
83     assert(key);
84
85     if ((q = avahi_hashmap_lookup(i->queriers_by_key, key))) {
86         /* Someone is already browsing for records of this RR key */
87         q->n_used++;
88
89         /* Return the creation time */
90         if (ret_ctime)
91             *ret_ctime = q->creation_time;
92         return;
93     }
94
95     /* No one is browsing for this RR key, so we add a new querier */
96     if (!(q = avahi_new(AvahiQuerier, 1)))
97         return; /* OOM */
98     
99     q->key = avahi_key_ref(key);
100     q->interface = i;
101     q->n_used = 1;
102     q->sec_delay = 1;
103     gettimeofday(&q->creation_time, NULL);
104
105     /* Do the initial query */
106     avahi_interface_post_query(i, key, 0);
107
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);
110
111     AVAHI_LLIST_PREPEND(AvahiQuerier, queriers, i->queriers, q);
112     avahi_hashmap_insert(i->queriers_by_key, q->key, q);
113
114     /* Return the creation time */
115     if (ret_ctime)
116         *ret_ctime = q->creation_time;
117 }
118
119 void avahi_querier_remove(AvahiInterface *i, AvahiKey *key) {
120     AvahiQuerier *q;
121
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");
125         return;
126     }
127
128     assert(q->n_used >= 1);
129     if ((--q->n_used) <= 0)
130         avahi_querier_free(q);
131 }
132
133 static void remove_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
134     assert(m);
135     assert(i);
136     assert(userdata);
137
138     if (i->announcing)
139         avahi_querier_remove(i, (AvahiKey*) userdata);
140 }
141
142 void avahi_querier_remove_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key) {
143     assert(s);
144     assert(key);
145     
146     avahi_interface_monitor_walk(s->monitor, idx, protocol, remove_querier_callback, key);
147 }
148
149 struct cbdata {
150     AvahiKey *key;
151     struct timeval *ret_ctime;
152 };
153
154 static void add_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
155     struct cbdata *cbdata = userdata;
156     
157     assert(m);
158     assert(i);
159     assert(cbdata);
160
161     if (i->announcing) {
162         struct timeval tv;
163         avahi_querier_add(i, cbdata->key, &tv);
164
165         if (cbdata->ret_ctime && avahi_timeval_compare(&tv, cbdata->ret_ctime) > 0)
166             *cbdata->ret_ctime = tv;
167     }
168 }
169
170 void avahi_querier_add_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key, struct timeval *ret_ctime) {
171     struct cbdata cbdata;
172     
173     assert(s);
174     assert(key);
175
176     cbdata.key = key;
177     cbdata.ret_ctime = ret_ctime;
178
179     if (ret_ctime)
180         ret_ctime->tv_sec = ret_ctime->tv_usec = 0;
181     
182     avahi_interface_monitor_walk(s->monitor, idx, protocol, add_querier_callback, &cbdata);
183 }
184
185 int avahi_querier_exists(AvahiInterface *i, AvahiKey *key) {
186     assert(i);
187     assert(key);
188
189     if (avahi_hashmap_lookup(i->queriers_by_key, key))
190         return 1;
191
192     return 0;
193 }
194
195 void avahi_querier_free_all(AvahiInterface *i) {
196     assert(i);
197
198     while (i->queriers) 
199         avahi_querier_free(i->queriers);
200 }