]> git.meshlink.io Git - catta/blob - avahi-core/querier.c
fix querier instantiation
[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
130     if ((--q->n_used) <= 0)
131         avahi_querier_free(q);
132 }
133
134 static void remove_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
135     assert(m);
136     assert(i);
137     assert(userdata);
138
139     if (i->announcing)
140         avahi_querier_remove(i, (AvahiKey*) userdata);
141 }
142
143 void avahi_querier_remove_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key) {
144     assert(s);
145     assert(key);
146     
147     avahi_interface_monitor_walk(s->monitor, idx, protocol, remove_querier_callback, key);
148 }
149
150 struct cbdata {
151     AvahiKey *key;
152     struct timeval *ret_ctime;
153 };
154
155 static void add_querier_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
156     struct cbdata *cbdata = userdata;
157     
158     assert(m);
159     assert(i);
160     assert(cbdata);
161
162     if (i->announcing) {
163         struct timeval tv;
164         avahi_querier_add(i, cbdata->key, &tv);
165
166         if (cbdata->ret_ctime && avahi_timeval_compare(&tv, cbdata->ret_ctime) > 0)
167             *cbdata->ret_ctime = tv;
168     }
169 }
170
171 void avahi_querier_add_for_all(AvahiServer *s, AvahiIfIndex idx, AvahiProtocol protocol, AvahiKey *key, struct timeval *ret_ctime) {
172     struct cbdata cbdata;
173     
174     assert(s);
175     assert(key);
176
177     cbdata.key = key;
178     cbdata.ret_ctime = ret_ctime;
179
180     if (ret_ctime)
181         ret_ctime->tv_sec = ret_ctime->tv_usec = 0;
182     
183     avahi_interface_monitor_walk(s->monitor, idx, protocol, add_querier_callback, &cbdata);
184 }
185
186 int avahi_querier_exists(AvahiInterface *i, AvahiKey *key) {
187     assert(i);
188     assert(key);
189
190     if (avahi_hashmap_lookup(i->queriers_by_key, key))
191         return 1;
192
193     return 0;
194 }
195
196 void avahi_querier_free_all(AvahiInterface *i) {
197     assert(i);
198
199     while (i->queriers) 
200         avahi_querier_free(i->queriers);
201 }