2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 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>
34 typedef struct AvahiDNSServerInfo AvahiDNSServerInfo;
36 struct AvahiDNSServerInfo {
37 AvahiSDNSServerBrowser *browser;
39 AvahiIfIndex interface;
40 AvahiProtocol protocol;
41 AvahiRecord *srv_record;
42 AvahiSHostNameResolver *host_name_resolver;
44 AvahiLookupResultFlags flags;
46 AVAHI_LLIST_FIELDS(AvahiDNSServerInfo, info);
49 struct AvahiSDNSServerBrowser {
52 AvahiSRecordBrowser *record_browser;
53 AvahiSDNSServerBrowserCallback callback;
55 AvahiProtocol aprotocol;
56 AvahiLookupFlags user_flags;
60 AVAHI_LLIST_FIELDS(AvahiSDNSServerBrowser, browser);
61 AVAHI_LLIST_HEAD(AvahiDNSServerInfo, info);
64 static AvahiDNSServerInfo* get_server_info(AvahiSDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r) {
65 AvahiDNSServerInfo *i;
70 for (i = b->info; i; i = i->info_next)
71 if (i->interface == interface &&
72 i->protocol == protocol &&
73 avahi_record_equal_no_ttl(r, i->srv_record))
79 static void server_info_free(AvahiSDNSServerBrowser *b, AvahiDNSServerInfo *i) {
83 avahi_record_unref(i->srv_record);
84 if (i->host_name_resolver)
85 avahi_s_host_name_resolver_free(i->host_name_resolver);
87 AVAHI_LLIST_REMOVE(AvahiDNSServerInfo, info, b->info, i);
89 assert(b->n_info >= 1);
95 static void host_name_resolver_callback(
96 AvahiSHostNameResolver *r,
97 AVAHI_GCC_UNUSED AvahiIfIndex interface,
98 AVAHI_GCC_UNUSED AvahiProtocol protocol,
99 AvahiResolverEvent event,
100 const char *host_name,
101 const AvahiAddress *a,
102 AvahiLookupResultFlags flags,
105 AvahiDNSServerInfo *i = userdata;
112 case AVAHI_RESOLVER_FOUND: {
115 i->browser->callback(
120 i->srv_record->data.srv.name,
122 i->srv_record->data.srv.port,
124 i->browser->userdata);
129 case AVAHI_RESOLVER_FAILURE:
134 avahi_s_host_name_resolver_free(i->host_name_resolver);
135 i->host_name_resolver = NULL;
138 static void record_browser_callback(
139 AvahiSRecordBrowser*rr,
140 AvahiIfIndex interface,
141 AvahiProtocol protocol,
142 AvahiBrowserEvent event,
144 AvahiLookupResultFlags flags,
147 AvahiSDNSServerBrowser *b = userdata;
153 flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA;
156 case AVAHI_BROWSER_NEW: {
157 AvahiDNSServerInfo *i;
160 assert(record->key->type == AVAHI_DNS_TYPE_SRV);
162 if (get_server_info(b, interface, protocol, record))
168 if (!(i = avahi_new(AvahiDNSServerInfo, 1)))
172 i->interface = interface;
173 i->protocol = protocol;
174 i->srv_record = avahi_record_ref(record);
175 i->host_name_resolver = avahi_s_host_name_resolver_new(
178 record->data.srv.name,
181 host_name_resolver_callback, i);
184 AVAHI_LLIST_PREPEND(AvahiDNSServerInfo, info, b->info, i);
190 case AVAHI_BROWSER_REMOVE: {
191 AvahiDNSServerInfo *i;
194 assert(record->key->type == AVAHI_DNS_TYPE_SRV);
196 if (!(i = get_server_info(b, interface, protocol, record)))
199 if (!i->host_name_resolver)
205 i->srv_record->data.srv.name,
207 i->srv_record->data.srv.port,
211 server_info_free(b, i);
215 case AVAHI_BROWSER_FAILURE:
216 case AVAHI_BROWSER_ALL_FOR_NOW:
217 case AVAHI_BROWSER_CACHE_EXHAUSTED:
234 AvahiSDNSServerBrowser *avahi_s_dns_server_browser_new(
236 AvahiIfIndex interface,
237 AvahiProtocol protocol,
239 AvahiDNSServerType type,
240 AvahiProtocol aprotocol,
241 AvahiLookupFlags flags,
242 AvahiSDNSServerBrowserCallback callback,
245 static const char * const type_table[AVAHI_DNS_SERVER_MAX] = {
250 AvahiSDNSServerBrowser *b;
252 char n[AVAHI_DOMAIN_NAME_MAX];
258 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
259 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
260 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(aprotocol), AVAHI_ERR_INVALID_PROTOCOL);
261 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
262 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
263 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, type < AVAHI_DNS_SERVER_MAX, AVAHI_ERR_INVALID_FLAGS);
266 domain = server->domain_name;
268 if ((r = avahi_service_name_join(n, sizeof(n), NULL, type_table[type], domain)) < 0) {
269 avahi_server_set_errno(server, r);
273 if (!(b = avahi_new(AvahiSDNSServerBrowser, 1))) {
274 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
279 b->callback = callback;
280 b->userdata = userdata;
281 b->aprotocol = aprotocol;
283 b->user_flags = flags;
285 AVAHI_LLIST_HEAD_INIT(AvahiDNSServerInfo, b->info);
286 AVAHI_LLIST_PREPEND(AvahiSDNSServerBrowser, browser, server->dns_server_browsers, b);
288 if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV))) {
289 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
293 if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b)))
305 avahi_s_dns_server_browser_free(b);
309 void avahi_s_dns_server_browser_free(AvahiSDNSServerBrowser *b) {
313 server_info_free(b, b->info);
315 AVAHI_LLIST_REMOVE(AvahiSDNSServerBrowser, browser, b->server->dns_server_browsers, b);
317 if (b->record_browser)
318 avahi_s_record_browser_free(b->record_browser);