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>
32 struct AvahiRecordBrowser {
37 AvahiIfIndex interface;
38 AvahiProtocol protocol;
41 AvahiTimeEvent *query_time_event;
42 AvahiTimeEvent *scan_time_event;
44 AvahiRecordBrowserCallback callback;
47 AVAHI_LLIST_FIELDS(AvahiRecordBrowser, browser);
48 AVAHI_LLIST_FIELDS(AvahiRecordBrowser, by_key);
51 static void elapse_callback(AvahiTimeEvent *e, void *userdata) {
52 AvahiRecordBrowser *s = userdata;
58 avahi_server_post_query(s->server, s->interface, s->protocol, s->key);
62 if (s->sec_delay >= 60*60) /* 1h */
65 /* avahi_log_debug("Continuous querying for %s (%i)", t = avahi_key_to_string(s->key), s->sec_delay); */
68 avahi_elapse_time(&tv, s->sec_delay*1000, 0);
69 avahi_time_event_update(s->query_time_event, &tv);
73 AvahiRecordBrowser *record_browser;
74 AvahiInterface *interface;
77 static void* scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void* userdata) {
78 struct cbdata *cbdata = userdata;
85 if (cbdata->record_browser->dead)
88 cbdata->record_browser->callback(
89 cbdata->record_browser,
90 cbdata->interface->hardware->index,
91 cbdata->interface->protocol,
94 cbdata->record_browser->userdata);
99 static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
100 AvahiRecordBrowser *b = userdata;
101 struct cbdata cbdata = { b, i };
108 avahi_cache_walk(i->cache, b->key, scan_cache_callback, &cbdata);
111 static void scan_callback(AvahiTimeEvent *e, void *userdata) {
112 AvahiRecordBrowser *b = userdata;
115 /* Scan the caches */
117 avahi_interface_monitor_walk(b->server->monitor, b->interface, b->protocol, scan_interface_callback, b);
119 if (b->scan_time_event) {
120 avahi_time_event_free(b->scan_time_event);
121 b->scan_time_event = NULL;
125 AvahiRecordBrowser *avahi_record_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key, AvahiRecordBrowserCallback callback, void* userdata) {
126 AvahiRecordBrowser *b, *t;
133 if (avahi_key_is_pattern(key)) {
134 avahi_server_set_errno(server, AVAHI_ERR_IS_PATTERN);
138 if (!avahi_key_is_valid(key)) {
139 avahi_server_set_errno(server, AVAHI_ERR_INVALID_KEY);
143 if (!(b = avahi_new(AvahiRecordBrowser, 1))) {
144 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
150 b->key = avahi_key_ref(key);
151 b->interface = interface;
152 b->protocol = protocol;
153 b->callback = callback;
154 b->userdata = userdata;
157 avahi_server_post_query(b->server, b->interface, b->protocol, b->key);
159 avahi_elapse_time(&tv, b->sec_delay*1000, 0);
160 b->query_time_event = avahi_time_event_new(server->time_event_queue, &tv, elapse_callback, b);
162 AVAHI_LLIST_PREPEND(AvahiRecordBrowser, browser, server->record_browsers, b);
164 /* Add the new entry to the record_browser hash table */
165 t = avahi_hashmap_lookup(server->record_browser_hashmap, key);
166 AVAHI_LLIST_PREPEND(AvahiRecordBrowser, by_key, t, b);
167 avahi_hashmap_replace(server->record_browser_hashmap, key, t);
169 /* The currenlty cached entries are scanned a bit later */
170 b->scan_time_event = avahi_time_event_new(server->time_event_queue, NULL, scan_callback, b);
171 assert(b->scan_time_event);
175 void avahi_record_browser_free(AvahiRecordBrowser *b) {
180 b->server->need_browser_cleanup = 1;
182 if (b->query_time_event) {
183 avahi_time_event_free(b->query_time_event);
184 b->query_time_event = NULL;
187 if (b->scan_time_event) {
188 avahi_time_event_free(b->scan_time_event);
189 b->scan_time_event = NULL;
193 void avahi_record_browser_destroy(AvahiRecordBrowser *b) {
194 AvahiRecordBrowser *t;
198 AVAHI_LLIST_REMOVE(AvahiRecordBrowser, browser, b->server->record_browsers, b);
200 t = avahi_hashmap_lookup(b->server->record_browser_hashmap, b->key);
201 AVAHI_LLIST_REMOVE(AvahiRecordBrowser, by_key, t, b);
203 avahi_hashmap_replace(b->server->record_browser_hashmap, t->key, t);
205 avahi_hashmap_remove(b->server->record_browser_hashmap, b->key);
207 if (b->query_time_event)
208 avahi_time_event_free(b->query_time_event);
209 if (b->scan_time_event)
210 avahi_time_event_free(b->scan_time_event);
212 avahi_key_unref(b->key);
217 void avahi_browser_cleanup(AvahiServer *server) {
218 AvahiRecordBrowser *b;
219 AvahiRecordBrowser *n;
223 for (b = server->record_browsers; b; b = n) {
227 avahi_record_browser_destroy(b);
230 server->need_browser_cleanup = 0;
233 void avahi_browser_notify(AvahiServer *server, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
234 AvahiRecordBrowser *b;
239 for (b = avahi_hashmap_lookup(server->record_browser_hashmap, record->key); b; b = b->by_key_next)
240 if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
241 b->callback(b, i->hardware->index, i->protocol, event, record, b->userdata);
244 int avahi_is_subscribed(AvahiServer *server, AvahiInterface *i, AvahiKey *k) {
245 AvahiRecordBrowser *b;
249 for (b = avahi_hashmap_lookup(server->record_browser_hashmap, k); b; b = b->by_key_next)
250 if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
256 void avahi_browser_new_interface(AvahiServer*s, AvahiInterface *i) {
257 AvahiRecordBrowser *b;
262 for (b = s->record_browsers; b; b = b->browser_next)
263 if (avahi_interface_match(i, b->interface, b->protocol))
264 avahi_interface_post_query(i, b->key, 0);