+static void defer_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void *userdata) {
+ AvahiSRecordBrowser *b = userdata;
+ int n;
+
+ assert(b);
+ assert(!b->dead);
+
+ /* Remove the defer timeout */
+ if (b->defer_time_event) {
+ avahi_time_event_free(b->defer_time_event);
+ b->defer_time_event = NULL;
+ }
+
+ /* Create initial query */
+ assert(!b->root_lookup);
+ b->root_lookup = lookup_add(b, b->interface, b->protocol, b->flags, b->key);
+ assert(b->root_lookup);
+
+ n = lookup_go(b->root_lookup);
+
+ if (b->dead)
+ return;
+
+ if (n < 0) {
+ /* sending of the initial query failed */
+
+ avahi_server_set_errno(b->server, AVAHI_ERR_FAILURE);
+
+ b->callback(
+ b, b->interface, b->protocol, AVAHI_BROWSER_FAILURE, NULL,
+ b->flags & AVAHI_LOOKUP_USE_WIDE_AREA ? AVAHI_LOOKUP_RESULT_WIDE_AREA : AVAHI_LOOKUP_RESULT_MULTICAST,
+ b->userdata);
+
+ browser_cancel(b);
+ return;
+ }
+
+ /* Tell the client that we're done with the cache */
+ b->callback(
+ b, b->interface, b->protocol, AVAHI_BROWSER_CACHE_EXHAUSTED, NULL,
+ b->flags & AVAHI_LOOKUP_USE_WIDE_AREA ? AVAHI_LOOKUP_RESULT_WIDE_AREA : AVAHI_LOOKUP_RESULT_MULTICAST,
+ b->userdata);
+
+ if (!b->dead && b->root_lookup && b->root_lookup->flags & AVAHI_LOOKUP_USE_WIDE_AREA && n > 0) {
+
+ /* If we do wide area lookups and the the cache contained
+ * entries, we assume that it is complete, and tell the user
+ * so by firing ALL_FOR_NOW. */
+
+ b->callback(b, b->interface, b->protocol, AVAHI_BROWSER_ALL_FOR_NOW, NULL, AVAHI_LOOKUP_RESULT_WIDE_AREA, b->userdata);
+ }
+}
+
+void avahi_s_record_browser_restart(AvahiSRecordBrowser *b) {
+ assert(b);
+ assert(!b->dead);
+
+ browser_cancel(b);
+
+ /* Request a new iteration of the cache scanning */
+ if (!b->defer_time_event) {
+ b->defer_time_event = avahi_time_event_new(b->server->time_event_queue, NULL, defer_callback, b);
+ assert(b->defer_time_event);
+ }
+}
+
+AvahiSRecordBrowser *avahi_s_record_browser_new(
+ AvahiServer *server,
+ AvahiIfIndex interface,
+ AvahiProtocol protocol,
+ AvahiKey *key,
+ AvahiLookupFlags flags,
+ AvahiSRecordBrowserCallback callback,
+ void* userdata) {
+
+ AvahiSRecordBrowser *b;
+
+ assert(server);
+ assert(key);
+ assert(callback);
+
+ AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
+ AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
+ AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !avahi_key_is_pattern(key), AVAHI_ERR_IS_PATTERN);
+ AVAHI_CHECK_VALIDITY_RETURN_NULL(server, avahi_key_is_valid(key), AVAHI_ERR_INVALID_KEY);
+ AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
+ AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !(flags & AVAHI_LOOKUP_USE_WIDE_AREA) || !(flags & AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
+
+ if (!(b = avahi_new(AvahiSRecordBrowser, 1))) {
+ avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
+ return NULL;
+ }
+
+ b->dead = 0;
+ b->server = server;
+ b->interface = interface;
+ b->protocol = protocol;
+ b->key = avahi_key_ref(key);
+ b->flags = flags;
+ b->callback = callback;
+ b->userdata = userdata;
+ b->n_lookups = 0;
+ AVAHI_LLIST_HEAD_INIT(AvahiSRBLookup, b->lookups);
+ b->root_lookup = NULL;
+
+ AVAHI_LLIST_PREPEND(AvahiSRecordBrowser, browser, server->record_browsers, b);
+
+ /* The currently cached entries are scanned a bit later, and than we will start querying, too */
+ b->defer_time_event = avahi_time_event_new(server->time_event_queue, NULL, defer_callback, b);
+ assert(b->defer_time_event);
+
+ return b;