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
28 #include <avahi-common/domain.h>
29 #include <avahi-common/malloc.h>
30 #include <avahi-common/error.h>
36 typedef struct AvahiDNSServerInfo AvahiDNSServerInfo;
38 struct AvahiDNSServerInfo {
39 AvahiSDNSServerBrowser *browser;
41 AvahiIfIndex interface;
42 AvahiProtocol protocol;
43 AvahiRecord *srv_record;
44 AvahiSHostNameResolver *host_name_resolver;
46 AvahiLookupResultFlags flags;
48 AVAHI_LLIST_FIELDS(AvahiDNSServerInfo, info);
51 struct AvahiSDNSServerBrowser {
54 AvahiSRecordBrowser *record_browser;
55 AvahiSDNSServerBrowserCallback callback;
57 AvahiProtocol aprotocol;
58 AvahiLookupFlags user_flags;
62 AVAHI_LLIST_FIELDS(AvahiSDNSServerBrowser, browser);
63 AVAHI_LLIST_HEAD(AvahiDNSServerInfo, info);
66 static AvahiDNSServerInfo* get_server_info(AvahiSDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r) {
67 AvahiDNSServerInfo *i;
72 for (i = b->info; i; i = i->info_next)
73 if (i->interface == interface &&
74 i->protocol == protocol &&
75 avahi_record_equal_no_ttl(r, i->srv_record))
81 static void server_info_free(AvahiSDNSServerBrowser *b, AvahiDNSServerInfo *i) {
85 avahi_record_unref(i->srv_record);
86 if (i->host_name_resolver)
87 avahi_s_host_name_resolver_free(i->host_name_resolver);
89 AVAHI_LLIST_REMOVE(AvahiDNSServerInfo, info, b->info, i);
91 assert(b->n_info >= 1);
97 static void host_name_resolver_callback(
98 AvahiSHostNameResolver *r,
99 AVAHI_GCC_UNUSED AvahiIfIndex interface,
100 AVAHI_GCC_UNUSED AvahiProtocol protocol,
101 AvahiResolverEvent event,
102 const char *host_name,
103 const AvahiAddress *a,
104 AvahiLookupResultFlags flags,
107 AvahiDNSServerInfo *i = userdata;
114 case AVAHI_RESOLVER_FOUND: {
117 i->browser->callback(
122 i->srv_record->data.srv.name,
124 i->srv_record->data.srv.port,
126 i->browser->userdata);
131 case AVAHI_RESOLVER_FAILURE:
136 avahi_s_host_name_resolver_free(i->host_name_resolver);
137 i->host_name_resolver = NULL;
140 static void record_browser_callback(
141 AvahiSRecordBrowser*rr,
142 AvahiIfIndex interface,
143 AvahiProtocol protocol,
144 AvahiBrowserEvent event,
146 AvahiLookupResultFlags flags,
149 AvahiSDNSServerBrowser *b = userdata;
155 flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA;
158 case AVAHI_BROWSER_NEW: {
159 AvahiDNSServerInfo *i;
162 assert(record->key->type == AVAHI_DNS_TYPE_SRV);
164 if (get_server_info(b, interface, protocol, record))
170 if (!(i = avahi_new(AvahiDNSServerInfo, 1)))
174 i->interface = interface;
175 i->protocol = protocol;
176 i->srv_record = avahi_record_ref(record);
177 i->host_name_resolver = avahi_s_host_name_resolver_new(
180 record->data.srv.name,
183 host_name_resolver_callback, i);
186 AVAHI_LLIST_PREPEND(AvahiDNSServerInfo, info, b->info, i);
192 case AVAHI_BROWSER_REMOVE: {
193 AvahiDNSServerInfo *i;
196 assert(record->key->type == AVAHI_DNS_TYPE_SRV);
198 if (!(i = get_server_info(b, interface, protocol, record)))
201 if (!i->host_name_resolver)
207 i->srv_record->data.srv.name,
209 i->srv_record->data.srv.port,
213 server_info_free(b, i);
217 case AVAHI_BROWSER_FAILURE:
218 case AVAHI_BROWSER_ALL_FOR_NOW:
219 case AVAHI_BROWSER_CACHE_EXHAUSTED:
236 AvahiSDNSServerBrowser *avahi_s_dns_server_browser_new(
238 AvahiIfIndex interface,
239 AvahiProtocol protocol,
241 AvahiDNSServerType type,
242 AvahiProtocol aprotocol,
243 AvahiLookupFlags flags,
244 AvahiSDNSServerBrowserCallback callback,
247 static const char * const type_table[AVAHI_DNS_SERVER_MAX] = {
252 AvahiSDNSServerBrowser *b;
254 char n[AVAHI_DOMAIN_NAME_MAX];
260 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
261 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
262 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(aprotocol), AVAHI_ERR_INVALID_PROTOCOL);
263 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
264 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
265 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, type < AVAHI_DNS_SERVER_MAX, AVAHI_ERR_INVALID_FLAGS);
268 domain = server->domain_name;
270 if ((r = avahi_service_name_join(n, sizeof(n), NULL, type_table[type], domain)) < 0) {
271 avahi_server_set_errno(server, r);
275 if (!(b = avahi_new(AvahiSDNSServerBrowser, 1))) {
276 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
281 b->callback = callback;
282 b->userdata = userdata;
283 b->aprotocol = aprotocol;
285 b->user_flags = flags;
287 AVAHI_LLIST_HEAD_INIT(AvahiDNSServerInfo, b->info);
288 AVAHI_LLIST_PREPEND(AvahiSDNSServerBrowser, browser, server->dns_server_browsers, b);
290 if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV))) {
291 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
295 if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b)))
307 avahi_s_dns_server_browser_free(b);
311 void avahi_s_dns_server_browser_free(AvahiSDNSServerBrowser *b) {
315 server_info_free(b, b->info);
317 AVAHI_LLIST_REMOVE(AvahiSDNSServerBrowser, browser, b->server->dns_server_browsers, b);
319 if (b->record_browser)
320 avahi_s_record_browser_free(b->record_browser);