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/domain.h>
27 #include <avahi-common/malloc.h>
28 #include <avahi-common/error.h>
33 struct AvahiSDomainBrowser {
38 AvahiSRecordBrowser *record_browser;
40 AvahiDomainBrowserType type;
41 AvahiSDomainBrowserCallback callback;
44 AvahiTimeEvent *defer_event;
46 int all_for_now_scheduled;
48 AVAHI_LLIST_FIELDS(AvahiSDomainBrowser, browser);
51 static void inc_ref(AvahiSDomainBrowser *b) {
58 static void record_browser_callback(
59 AvahiSRecordBrowser*rr,
60 AvahiIfIndex interface,
61 AvahiProtocol protocol,
62 AvahiBrowserEvent event,
64 AvahiLookupResultFlags flags,
67 AvahiSDomainBrowser *b = userdata;
73 if (event == AVAHI_BROWSER_ALL_FOR_NOW &&
76 b->all_for_now_scheduled = 1;
81 flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA;
84 assert(record->key->type == AVAHI_DNS_TYPE_PTR);
85 n = record->data.ptr.name;
87 if (b->type == AVAHI_DOMAIN_BROWSER_BROWSE) {
90 /* Filter out entries defined statically */
92 for (l = b->server->config.browse_domains; l; l = l->next)
93 if (avahi_domain_equal((char*) l->text, n))
99 b->callback(b, interface, protocol, event, n, flags, b->userdata);
102 static void defer_callback(AvahiTimeEvent *e, void *userdata) {
103 AvahiSDomainBrowser *b = userdata;
109 avahi_time_event_free(b->defer_event);
110 b->defer_event = NULL;
112 /* Increase ref counter */
115 for (l = b->server->config.browse_domains; l; l = l->next) {
117 /* Check whether this object still exists outside our own
122 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_BROWSER_NEW, (char*) l->text, AVAHI_LOOKUP_RESULT_STATIC, b->userdata);
126 /* If the ALL_FOR_NOW event has already been scheduled, execute it now */
128 if (b->all_for_now_scheduled)
129 b->callback(b, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_BROWSER_ALL_FOR_NOW, NULL, 0, b->userdata);
132 /* Decrease ref counter */
133 avahi_s_domain_browser_free(b);
136 AvahiSDomainBrowser *avahi_s_domain_browser_new(
138 AvahiIfIndex interface,
139 AvahiProtocol protocol,
141 AvahiDomainBrowserType type,
142 AvahiLookupFlags flags,
143 AvahiSDomainBrowserCallback callback,
146 static const char * const type_table[AVAHI_DOMAIN_BROWSER_MAX] = {
154 AvahiSDomainBrowser *b;
156 char n[AVAHI_DOMAIN_NAME_MAX];
162 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
163 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
164 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, type < AVAHI_DOMAIN_BROWSER_MAX, AVAHI_ERR_INVALID_FLAGS);
165 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
166 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
169 domain = server->domain_name;
171 if ((r = avahi_service_name_join(n, sizeof(n), type_table[type], "_dns-sd._udp", domain)) < 0) {
172 avahi_server_set_errno(server, r);
176 if (!(b = avahi_new(AvahiSDomainBrowser, 1))) {
177 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
183 b->callback = callback;
184 b->userdata = userdata;
185 b->record_browser = NULL;
187 b->all_for_now_scheduled = 0;
189 AVAHI_LLIST_PREPEND(AvahiSDomainBrowser, browser, server->domain_browsers, b);
191 if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR))) {
192 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
196 if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b)))
201 b->defer_event = avahi_time_event_new(server->time_event_queue, NULL, defer_callback, b);
210 avahi_s_domain_browser_free(b);
215 void avahi_s_domain_browser_free(AvahiSDomainBrowser *b) {
222 AVAHI_LLIST_REMOVE(AvahiSDomainBrowser, browser, b->server->domain_browsers, b);
224 if (b->record_browser)
225 avahi_s_record_browser_free(b->record_browser);
228 avahi_time_event_free(b->defer_event);