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>
33 struct AvahiSRecordBrowser {
38 AvahiIfIndex interface;
39 AvahiProtocol protocol;
42 AvahiTimeEvent *query_time_event;
43 AvahiTimeEvent *scan_time_event;
45 AvahiSRecordBrowserCallback callback;
48 AVAHI_LLIST_FIELDS(AvahiSRecordBrowser, browser);
49 AVAHI_LLIST_FIELDS(AvahiSRecordBrowser, by_key);
52 static void elapse_callback(AvahiTimeEvent *e, void *userdata) {
53 AvahiSRecordBrowser *s = userdata;
59 avahi_server_post_query(s->server, s->interface, s->protocol, s->key);
63 if (s->sec_delay >= 60*60) /* 1h */
66 /* avahi_log_debug("Continuous querying for %s (%i)", t = avahi_key_to_string(s->key), s->sec_delay); */
69 avahi_elapse_time(&tv, s->sec_delay*1000, 0);
70 avahi_time_event_update(s->query_time_event, &tv);
74 AvahiSRecordBrowser *record_browser;
75 AvahiInterface *interface;
78 static void* scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) {
79 struct cbdata *cbdata = userdata;
86 if (cbdata->record_browser->dead)
89 cbdata->record_browser->callback(
90 cbdata->record_browser,
91 cbdata->interface->hardware->index,
92 cbdata->interface->protocol,
95 cbdata->record_browser->userdata);
100 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
101 AvahiSRecordBrowser *b = userdata;
102 struct cbdata cbdata;
104 cbdata.record_browser = b;
105 cbdata.interface = i;
112 avahi_cache_walk(i->cache, b->key, scan_cache_callback, &cbdata);
115 static void scan_callback(AvahiTimeEvent *e, void *userdata) {
116 AvahiSRecordBrowser *b = userdata;
119 /* Scan the caches */
121 avahi_interface_monitor_walk(b->server->monitor, b->interface, b->protocol, scan_interface_callback, b);
123 if (b->scan_time_event) {
124 avahi_time_event_free(b->scan_time_event);
125 b->scan_time_event = NULL;
129 void avahi_s_record_browser_restart(AvahiSRecordBrowser *b) {
132 if (!b->scan_time_event) {
133 b->scan_time_event = avahi_time_event_new(b->server->time_event_queue, NULL, scan_callback, b);
134 assert(b->scan_time_event);
137 avahi_server_post_query(b->server, b->interface, b->protocol, b->key);
140 AvahiSRecordBrowser *avahi_s_record_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key, AvahiSRecordBrowserCallback callback, void* userdata) {
141 AvahiSRecordBrowser *b, *t;
148 if (avahi_key_is_pattern(key)) {
149 avahi_server_set_errno(server, AVAHI_ERR_IS_PATTERN);
153 if (!avahi_key_is_valid(key)) {
154 avahi_server_set_errno(server, AVAHI_ERR_INVALID_KEY);
158 if (!(b = avahi_new(AvahiSRecordBrowser, 1))) {
159 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
165 b->key = avahi_key_ref(key);
166 b->interface = interface;
167 b->protocol = protocol;
168 b->callback = callback;
169 b->userdata = userdata;
172 avahi_server_post_query(b->server, b->interface, b->protocol, b->key);
174 avahi_elapse_time(&tv, b->sec_delay*1000, 0);
175 b->query_time_event = avahi_time_event_new(server->time_event_queue, &tv, elapse_callback, b);
177 AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, browser, server->record_browsers, b);
179 /* Add the new entry to the record_browser hash table */
180 t = avahi_hashmap_lookup(server->record_browser_hashmap, key);
181 AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, by_key, t, b);
182 avahi_hashmap_replace(server->record_browser_hashmap, key, t);
184 /* The currenlty cached entries are scanned a bit later */
185 b->scan_time_event = avahi_time_event_new(server->time_event_queue, NULL, scan_callback, b);
186 assert(b->scan_time_event);
190 void avahi_s_record_browser_free(AvahiSRecordBrowser *b) {
195 b->server->need_browser_cleanup = 1;
197 if (b->query_time_event) {
198 avahi_time_event_free(b->query_time_event);
199 b->query_time_event = NULL;
202 if (b->scan_time_event) {
203 avahi_time_event_free(b->scan_time_event);
204 b->scan_time_event = NULL;
208 void avahi_s_record_browser_destroy(AvahiSRecordBrowser *b) {
209 AvahiSRecordBrowser *t;
213 AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, browser, b->server->record_browsers, b);
215 t = avahi_hashmap_lookup(b->server->record_browser_hashmap, b->key);
216 AVAHI_LLIST_REMOVE(AvahiSRecordBrowser, by_key, t, b);
218 avahi_hashmap_replace(b->server->record_browser_hashmap, t->key, t);
220 avahi_hashmap_remove(b->server->record_browser_hashmap, b->key);
222 if (b->query_time_event)
223 avahi_time_event_free(b->query_time_event);
224 if (b->scan_time_event)
225 avahi_time_event_free(b->scan_time_event);
227 avahi_key_unref(b->key);
232 void avahi_browser_cleanup(AvahiServer *server) {
233 AvahiSRecordBrowser *b;
234 AvahiSRecordBrowser *n;
238 for (b = server->record_browsers; b; b = n) {
242 avahi_s_record_browser_destroy(b);
245 server->need_browser_cleanup = 0;
248 void avahi_browser_notify(AvahiServer *server, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
249 AvahiSRecordBrowser *b;
254 for (b = avahi_hashmap_lookup(server->record_browser_hashmap, record->key); b; b = b->by_key_next)
255 if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
256 b->callback(b, i->hardware->index, i->protocol, event, record, b->userdata);
259 int avahi_is_subscribed(AvahiServer *server, AvahiInterface *i, AvahiKey *k) {
260 AvahiSRecordBrowser *b;
264 for (b = avahi_hashmap_lookup(server->record_browser_hashmap, k); b; b = b->by_key_next)
265 if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
271 void avahi_browser_new_interface(AvahiServer*s, AvahiInterface *i) {
272 AvahiSRecordBrowser *b;
277 for (b = s->record_browsers; b; b = b->browser_next)
278 if (avahi_interface_match(i, b->interface, b->protocol))
279 avahi_interface_post_query(i, b->key, 0);