summaryrefslogtreecommitdiffstats
path: root/avahi-core/resolve.c
diff options
context:
space:
mode:
Diffstat (limited to 'avahi-core/resolve.c')
-rw-r--r--avahi-core/resolve.c151
1 files changed, 104 insertions, 47 deletions
diff --git a/avahi-core/resolve.c b/avahi-core/resolve.c
index 7d0f068..42eba35 100644
--- a/avahi-core/resolve.c
+++ b/avahi-core/resolve.c
@@ -26,8 +26,27 @@
#include "resolve.h"
#include "util.h"
+struct AvahiRecordBrowser {
+ gboolean dead;
+
+ AvahiServer *server;
+ AvahiKey *key;
+ gint interface;
+ guchar protocol;
+ gint n_query;
+ guint sec_delay;
+
+ AvahiTimeEvent *time_event;
+
+ AvahiRecordBrowserCallback callback;
+ gpointer userdata;
+
+ AVAHI_LLIST_FIELDS(AvahiRecordBrowser, resolver);
+ AVAHI_LLIST_FIELDS(AvahiRecordBrowser, by_key);
+};
+
static void elapse(AvahiTimeEvent *e, void *userdata) {
- AvahiRecordResolver *s = userdata;
+ AvahiRecordBrowser *s = userdata;
GTimeVal tv;
/* gchar *t; */
@@ -46,7 +65,7 @@ static void elapse(AvahiTimeEvent *e, void *userdata) {
}
struct cbdata {
- AvahiRecordResolver *record_resolver;
+ AvahiRecordBrowser *record_browser;
AvahiInterface *interface;
};
@@ -58,19 +77,22 @@ static gpointer scan_cache_callback(AvahiCache *c, AvahiKey *pattern, AvahiCache
g_assert(e);
g_assert(cbdata);
- cbdata->record_resolver->callback(
- cbdata->record_resolver,
+ if (cbdata->record_browser->dead)
+ return NULL;
+
+ cbdata->record_browser->callback(
+ cbdata->record_browser,
cbdata->interface->hardware->index,
cbdata->interface->protocol,
AVAHI_BROWSER_NEW,
e->record,
- cbdata->record_resolver->userdata);
+ cbdata->record_browser->userdata);
return NULL;
}
static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
- AvahiRecordResolver *s = userdata;
+ AvahiRecordBrowser *s = userdata;
struct cbdata cbdata = { s, i };
g_assert(m);
@@ -80,8 +102,8 @@ static void scan_interface_callback(AvahiInterfaceMonitor *m, AvahiInterface *i,
avahi_cache_walk(i->cache, s->key, scan_cache_callback, &cbdata);
}
-AvahiRecordResolver *avahi_record_resolver_new(AvahiServer *server, gint interface, guchar protocol, AvahiKey *key, AvahiRecordResolverCallback callback, gpointer userdata) {
- AvahiRecordResolver *s, *t;
+AvahiRecordBrowser *avahi_record_browser_new(AvahiServer *server, gint interface, guchar protocol, AvahiKey *key, AvahiRecordBrowserCallback callback, gpointer userdata) {
+ AvahiRecordBrowser *b, *t;
GTimeVal tv;
g_assert(server);
@@ -90,69 +112,104 @@ AvahiRecordResolver *avahi_record_resolver_new(AvahiServer *server, gint interfa
g_assert(!avahi_key_is_pattern(key));
- s = g_new(AvahiRecordResolver, 1);
- s->server = server;
- s->key = avahi_key_ref(key);
- s->interface = interface;
- s->protocol = protocol;
- s->callback = callback;
- s->userdata = userdata;
- s->n_query = 1;
- s->sec_delay = 1;
-
- avahi_server_post_query(s->server, s->interface, s->protocol, s->key);
+ b = g_new(AvahiRecordBrowser, 1);
+ b->dead = FALSE;
+ b->server = server;
+ b->key = avahi_key_ref(key);
+ b->interface = interface;
+ b->protocol = protocol;
+ b->callback = callback;
+ b->userdata = userdata;
+ b->n_query = 1;
+ b->sec_delay = 1;
+
+ avahi_server_post_query(b->server, b->interface, b->protocol, b->key);
- avahi_elapse_time(&tv, s->sec_delay*1000, 0);
- s->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, elapse, s);
+ avahi_elapse_time(&tv, b->sec_delay*1000, 0);
+ b->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, elapse, b);
- AVAHI_LLIST_PREPEND(AvahiRecordResolver, resolver, server->record_resolvers, s);
+ AVAHI_LLIST_PREPEND(AvahiRecordBrowser, resolver, server->record_browsers, b);
- /* Add the new entry to the record_resolver hash table */
- t = g_hash_table_lookup(server->record_resolver_hashtable, key);
- AVAHI_LLIST_PREPEND(AvahiRecordResolver, by_key, t, s);
- g_hash_table_replace(server->record_resolver_hashtable, key, t);
+ /* Add the new entry to the record_browser hash table */
+ t = g_hash_table_lookup(server->record_browser_hashtable, key);
+ AVAHI_LLIST_PREPEND(AvahiRecordBrowser, by_key, t, b);
+ g_hash_table_replace(server->record_browser_hashtable, key, t);
/* Scan the caches */
- avahi_interface_monitor_walk(s->server->monitor, s->interface, s->protocol, scan_interface_callback, s);
+ avahi_interface_monitor_walk(b->server->monitor, b->interface, b->protocol, scan_interface_callback, b);
- return s;
+ return b;
}
-void avahi_record_resolver_free(AvahiRecordResolver *s) {
- AvahiRecordResolver *t;
-
- g_assert(s);
+void avahi_record_browser_free(AvahiRecordBrowser *b) {
+ g_assert(b);
+ g_assert(!b->dead);
- AVAHI_LLIST_REMOVE(AvahiRecordResolver, resolver, s->server->record_resolvers, s);
+ b->dead = TRUE;
+ b->server->need_browser_cleanup = TRUE;
- t = g_hash_table_lookup(s->server->record_resolver_hashtable, s->key);
- AVAHI_LLIST_REMOVE(AvahiRecordResolver, by_key, t, s);
+ if (b->time_event) {
+ avahi_time_event_queue_remove(b->server->time_event_queue, b->time_event);
+ b->time_event = NULL;
+ }
+}
+
+void avahi_record_browser_destroy(AvahiRecordBrowser *b) {
+ AvahiRecordBrowser *t;
+
+ g_assert(b);
+
+ AVAHI_LLIST_REMOVE(AvahiRecordBrowser, resolver, b->server->record_browsers, b);
+
+ t = g_hash_table_lookup(b->server->record_browser_hashtable, b->key);
+ AVAHI_LLIST_REMOVE(AvahiRecordBrowser, by_key, t, b);
if (t)
- g_hash_table_replace(s->server->record_resolver_hashtable, t->key, t);
+ g_hash_table_replace(b->server->record_browser_hashtable, t->key, t);
else
- g_hash_table_remove(s->server->record_resolver_hashtable, s->key);
+ g_hash_table_remove(b->server->record_browser_hashtable, b->key);
+
+ if (b->time_event)
+ avahi_time_event_queue_remove(b->server->time_event_queue, b->time_event);
+ avahi_key_unref(b->key);
- avahi_time_event_queue_remove(s->server->time_event_queue, s->time_event);
- avahi_key_unref(s->key);
+ g_free(b);
+}
+void avahi_browser_cleanup(AvahiServer *server) {
+ AvahiRecordBrowser *b;
+ AvahiRecordBrowser *n;
- g_free(s);
+ g_assert(server);
+
+ for (b = server->record_browsers; b; b = n) {
+ n = b->resolver_next;
+
+ if (b->dead)
+ avahi_record_browser_destroy(b);
+ }
+
+ server->need_browser_cleanup = FALSE;
}
-void avahi_resolver_notify(AvahiServer *server, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
- AvahiRecordResolver *s;
+void avahi_browser_notify(AvahiServer *server, AvahiInterface *i, AvahiRecord *record, AvahiBrowserEvent event) {
+ AvahiRecordBrowser *b;
g_assert(server);
g_assert(record);
- for (s = g_hash_table_lookup(server->record_resolver_hashtable, record->key); s; s = s->by_key_next)
- if (avahi_interface_match(i, s->interface, s->protocol))
- s->callback(s, i->hardware->index, i->protocol, event, record, s->userdata);
+ for (b = g_hash_table_lookup(server->record_browser_hashtable, record->key); b; b = b->by_key_next)
+ if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
+ b->callback(b, i->hardware->index, i->protocol, event, record, b->userdata);
}
-gboolean avahi_is_subscribed(AvahiServer *server, AvahiKey *k) {
+gboolean avahi_is_subscribed(AvahiServer *server, AvahiInterface *i, AvahiKey *k) {
+ AvahiRecordBrowser *b;
g_assert(server);
g_assert(k);
- return !!g_hash_table_lookup(server->record_resolver_hashtable, k);
+ for (b = g_hash_table_lookup(server->record_browser_hashtable, k); b; b = b->by_key_next)
+ if (!b->dead && avahi_interface_match(i, b->interface, b->protocol))
+ return TRUE;
+
+ return FALSE;
}