]> git.meshlink.io Git - catta/blob - avahi-core/browse.c
gcc 2.95 compatibility
[catta] / avahi-core / browse.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
29 #include "browse.h"
30 #include "log.h"
31
32 struct AvahiSRecordBrowser {
33     int dead;
34     
35     AvahiServer *server;
36     AvahiKey *key;
37     AvahiIfIndex interface;
38     AvahiProtocol protocol;
39     unsigned sec_delay;
40
41     AvahiTimeEvent *query_time_event;
42     AvahiTimeEvent *scan_time_event;
43
44     AvahiSRecordBrowserCallback callback;
45     void* userdata;
46
47     AVAHI_LLIST_FIELDS(AvahiSRecordBrowser, browser);
48     AVAHI_LLIST_FIELDS(AvahiSRecordBrowser, by_key);
49 };
50
51 static void elapse_callback(AvahiTimeEvent *e, void *userdata) {
52     AvahiSRecordBrowser *s = userdata;
53     struct timeval tv;
54 /*     char *t;  */
55     
56     assert(s);
57
58     avahi_server_post_query(s->server, s->interface, s->protocol, s->key);
59
60     s->sec_delay *= 2;
61     
62     if (s->sec_delay >= 60*60)  /* 1h */
63         s->sec_delay = 60*60;
64     
65 /*     avahi_log_debug("Continuous querying for %s (%i)", t = avahi_key_to_string(s->key), s->sec_delay);  */
66 /*     avahi_free(t);  */
67     
68     avahi_elapse_time(&tv, s->sec_delay*1000, 0);
69     avahi_time_event_update(s->query_time_event, &tv);
70 }
71
72 struct cbdata {
73     AvahiSRecordBrowser *record_browser;
74     AvahiInterface *interface;
75 };
76
77 static void* scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) {
78     struct cbdata *cbdata = userdata;
79
80     assert(c);
81     assert(pattern);
82     assert(e);
83     assert(cbdata);
84
85     if (cbdata->record_browser->dead)
86         return NULL;
87
88     cbdata->record_browser->callback(
89         cbdata->record_browser,
90         cbdata->interface->hardware->index,
91         cbdata->interface->protocol,
92         AVAHI_BROWSER_NEW,
93         e->record,
94         cbdata->record_browser->userdata);
95
96     return NULL;
97 }
98
99 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
100     AvahiSRecordBrowser *b = userdata;
101     struct cbdata cbdata;
102
103     cbdata.record_browser = b;
104     cbdata.interface = i;
105
106     assert(m);
107     assert(i);
108     assert(b);
109
110     if (!b->dead)
111         avahi_cache_walk(i->cache, b->key, scan_cache_callback, &cbdata);
112 }
113
114 static void scan_callback(AvahiTimeEvent *e, void *userdata) {
115     AvahiSRecordBrowser *b = userdata;
116     assert(b);
117
118     /* Scan the caches */
119     if (!b->dead)
120         avahi_interface_monitor_walk(b->server->monitor, b->interface, b->protocol, scan_interface_callback, b);
121
122     if (b->scan_time_event) {
123         avahi_time_event_free(b->scan_time_event);
124         b->scan_time_event = NULL;
125     }
126 }
127
128 AvahiSRecordBrowser *avahi_s_record_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key, AvahiSRecordBrowserCallback callback, void* userdata) {
129     AvahiSRecordBrowser *b, *t;
130     struct timeval tv;
131
132     assert(server);
133     assert(key);
134     assert(callback);
135
136     if (avahi_key_is_pattern(key)) {
137         avahi_server_set_errno(server, AVAHI_ERR_IS_PATTERN);
138         return NULL;
139     }
140
141     if (!avahi_key_is_valid(key)) {
142         avahi_server_set_errno(server, AVAHI_ERR_INVALID_KEY);
143         return NULL;
144     }
145     
146     if (!(b = avahi_new(AvahiSRecordBrowser, 1))) {
147         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
148         return NULL;
149     }
150     
151     b->dead = 0;
152     b->server = server;
153     b->key = avahi_key_ref(key);
154     b->interface = interface;
155     b->protocol = protocol;
156     b->callback = callback;
157     b->userdata = userdata;
158     b->sec_delay = 1;
159
160     avahi_server_post_query(b->server, b->interface, b->protocol, b->key);
161     
162     avahi_elapse_time(&tv, b->sec_delay*1000, 0);
163     b->query_time_event = avahi_time_event_new(server->time_event_queue, &tv, elapse_callback, b);
164
165     AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, browser, server->record_browsers, b);
166
167     /* Add the new entry to the record_browser hash table */
168     t = avahi_hashmap_lookup(server->record_browser_hashmap, key);
169     AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, by_key, t, b);
170     avahi_hashmap_replace(server->record_browser_hashmap, key, t);
171
172     /* The currenlty cached entries are scanned a bit later */
173     b->scan_time_event = avahi_time_event_new(server->time_event_queue, NULL, scan_callback, b);
174     assert(b->scan_time_event);
175     return b;
176 }
177
178 void avahi_s_record_browser_free(AvahiSRecordBrowser *b) {
179     assert(b);
180     assert(!b->dead);
181
182     b->dead = 1;
183     b->server->need_browser_cleanup = 1;
184
185     if (b->query_time_event) {
186         avahi_time_event_free(b->query_time_event);
187         b->query_time_event = NULL;
188     }
189
190     if (b->scan_time_event) {
191         avahi_time_event_free(b->scan_time_event);
192         b->scan_time_event = NULL;
193     }
194 }
195
196 void avahi_s_record_browser_destroy(AvahiSRecordBrowser *b) {
197     AvahiSRecordBrowser *t;
198     
199     assert(b);
200     
201     AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, browser, b->server->record_browsers, b);
202
203     t = avahi_hashmap_lookup(b->server->record_browser_hashmap, b->key);
204     AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, by_key, t, b);
205     if (t)
206         avahi_hashmap_replace(b->server->record_browser_hashmap, t->key, t);
207     else
208         avahi_hashmap_remove(b->server->record_browser_hashmap, b->key);
209
210     if (b->query_time_event)
211         avahi_time_event_free(b->query_time_event);
212     if (b->scan_time_event)
213         avahi_time_event_free(b->scan_time_event);
214
215     avahi_key_unref(b->key);
216     
217     avahi_free(b);
218 }
219
220 void avahi_browser_cleanup(AvahiServer *server) {
221     AvahiSRecordBrowser *b;
222     AvahiSRecordBrowser *n;
223     
224     assert(server);
225
226     for (b = server->record_browsers; b; b = n) {
227         n = b->browser_next;
228         
229         if (b->dead)
230             avahi_s_record_browser_destroy(b);
231     }
232
233     server->need_browser_cleanup = 0;
234 }
235
236 void avahi_browser_notify(AvahiServer *server, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
237     AvahiSRecordBrowser *b;
238     
239     assert(server);
240     assert(record);
241
242     for (b = avahi_hashmap_lookup(server->record_browser_hashmap, record->key); b; b = b->by_key_next)
243         if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
244                 b->callback(b, i->hardware->index, i->protocol, event, record, b->userdata);
245 }
246
247 int avahi_is_subscribed(AvahiServer *server, AvahiInterface *i, AvahiKey *k) {
248     AvahiSRecordBrowser *b;
249     assert(server);
250     assert(k);
251
252     for (b = avahi_hashmap_lookup(server->record_browser_hashmap, k); b; b = b->by_key_next)
253         if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
254             return 1;
255
256     return 0;
257 }
258
259 void avahi_browser_new_interface(AvahiServer*s, AvahiInterface *i) {
260     AvahiSRecordBrowser *b;
261     
262     assert(s);
263     assert(i);
264     
265     for (b = s->record_browsers; b; b = b->browser_next)
266         if (avahi_interface_match(i, b->interface, b->protocol))
267             avahi_interface_post_query(i, b->key, 0);
268 }